【问题标题】:Splitting query on key words (SELECT/FROM/WHERE/AND/OR)拆分关键字查询(SELECT/FROM/WHERE/AND/OR)
【发布时间】:2016-02-24 14:53:55
【问题描述】:

大家好,下面是我一直在处理的以下代码:

var sql = "SELECT pillers, Balloons, Tacks FROM the_database_file WHERE Balloons != 'small' AND Balloons != 'large' AND Blah = 'bobby';";

$(document).ready(function() {
    var findFROM        = sql.indexOf(" FROM");
    var findWHERE       = sql.indexOf(" WHERE");
    var findAND         = sql.indexOf(" AND");
    var findOR          = sql.indexOf(" OR");
    var findSemicolon   = sql.indexOf(";"); 
    var findCountAND    = sql.match(/\AND\b/g);
    var findCountOR     = sql.match(/\OR\b/g);

    var txtSELECT       = sql.substring(0, findFROM);
    var txtFROM         = sql.substring(findFROM, findWHERE);
    var txtWHERE        = "";
    var txtAND          = "";
    var txtOR           = "";

    if (findAND != -1) {
        var _tmpPos     = 0;
        var _tmpAND     = "";

        findCountAND    = (findCountAND? findCountAND.length : 0);
        findCountOR     = (findCountOR? findCountOR.length : 0);

        for (var i = 1; i < findCountAND; i++) {
            console.log(i);
            _tmpPos = nth_occurrence(sql, ' AND', i);
            _tmpPos = findAND;
            findAND = sql.indexOf(" AND");
            _tmpAND = sql.substring(_tmpPos, findAND);
        }

        txtWHERE    = sql.substring(findWHERE, findAND);
    }

    $('#SELECT').text(txtSELECT);
    $('#FROM').text(txtFROM);
    $('#WHERE').text(txtWHERE);
    $('#test').text(findAND);
});

function nth_occurrence(string, char, nth) {
    var first_index = string.indexOf(char);
    var length_up_to_first_index = first_index + 1;

    if (nth == 1) {
        return first_index;
    } else {
        var string_after_first_occurrence = string.slice(length_up_to_first_index);
        var next_occurrence = nth_occurrence(string_after_first_occurrence, char, nth - 1);

        if (next_occurrence === -1) {
            return -1;
        } else {
            return length_up_to_first_index + next_occurrence;  
        }
    }
}

我正在尝试这样格式化:

SELECT pillers, Balloons, Tacks
FROM the_database_file
WHERE Balloons != 'small'
AND Balloons != 'large'
AND Blah = 'bobby';

但是,我陷入了试图在查询字符串中查找多个 ANDOR 的领域。上面的示例只有 2 个 AND 开头,但我似乎无法获得正确的代码。

nth_occurrence函数被发现HERE

如果有人可以帮助我,那就太好了。可能只需要一个 REGEX 吗?谁知道?

JSFIDDLE 也已设置。

更新

感谢一位善良的论坛成员HERE,我能够更新我的JSFIDDLE,尝试重现他所做的事情:

[ { word: 'SELECT', index: 0, text: 'pillers, Balloons, Tacks' },
  { word: 'FROM', index: 32, text: 'the_database_file' },
  { word: 'WHERE', index: 55, text: 'Balloons != \'small\'' },
  { word: 'AND', index: 81, text: 'Balloons != \'large\'' },
  { word: 'AND', index: 105, text: 'Blah = \'bobby\'' },
  { word: ';', index: 123 } ]

但是,我的尝试并不一样。有什么帮助吗?

【问题讨论】:

    标签: javascript jquery regex split indexof


    【解决方案1】:

    你的实现真的很棘手,我可以想象很多情况,当它解析失败时。

    我建议不要重新发明轮子,而是使用现有的库来用 JS 解析 SQL:

    1. SQL parser
    2. simpleSqlParser
    3. node-sqljs
    4. ....

    和其他人。有很多不同复杂性的现成解决方案。

    【讨论】:

      【解决方案2】:

      找到解决方案:

      Update JSFIDDLE

      function parseSql(sql) {
        var found = [];
      
        ["SELECT", "WHERE", "FROM", "AND", "OR", ";"].forEach(function(word) {
          var idx = sql.indexOf(word);
      
          while(idx!==-1) {      
            found.push({word:word, index:idx});
            idx = sql.indexOf(word, idx + 1);
            keptIdx = idx;
          }
        });
      
        found.sort(function(x,y) { return x.index - y.index });
        found.forEach(function(x, i, xs) {
          if (i < xs.length - 1) {
            x.text = sql.substring(x.index, xs[i + 1].index).replace(xs[i].word, "").trim();
          }
        });
      
        return found;
      }
      

      【讨论】:

      • 如果关键字将是小写的 - 没有检测到怎么办?或者例如会有类似的查询“SELECT t1.countryFrom as from FROM t1 ....”。您的 parcer 将在第一次“来自”提及时失败。正如我在下面的回答中所说,这不是一个好方法。
      猜你喜欢
      • 2016-01-31
      • 2014-06-28
      • 1970-01-01
      • 1970-01-01
      • 2011-07-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-11-25
      相关资源
      最近更新 更多