Skip to content

Commit cb87533

Browse files
committed
Fixed an issue where the parser would get confused between a wildcard * and multiplication *
1 parent cb04001 commit cb87533

File tree

5 files changed

+75
-0
lines changed

5 files changed

+75
-0
lines changed

browser/sql-parser.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,29 @@
2828
i += bytesConsumed;
2929
}
3030
this.token('EOF', '');
31+
this.postProcess();
3132
}
3233

34+
Lexer.prototype.postProcess = function() {
35+
var i, next_token, token, _i, _len, _ref, _results;
36+
_ref = this.tokens;
37+
_results = [];
38+
for (i = _i = 0, _len = _ref.length; _i < _len; i = ++_i) {
39+
token = _ref[i];
40+
if (token[0] === 'STAR') {
41+
next_token = this.tokens[i + 1];
42+
if (!(next_token[0] === 'SEPARATOR' || next_token[0] === 'FROM')) {
43+
_results.push(token[0] = 'MATH_MULTI');
44+
} else {
45+
_results.push(void 0);
46+
}
47+
} else {
48+
_results.push(void 0);
49+
}
50+
}
51+
return _results;
52+
};
53+
3354
Lexer.prototype.token = function(name, value) {
3455
return this.tokens.push([name, value, this.currentLine]);
3556
};

lib/lexer.js

Lines changed: 21 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/lexer.coffee

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,14 @@ class Lexer
2626
throw new Error("NOTHING CONSUMED: Stopped at - '#{@chunk.slice(0,30)}'") if bytesConsumed < 1
2727
i += bytesConsumed
2828
@token('EOF', '')
29+
@postProcess()
30+
31+
postProcess: ->
32+
for token, i in @tokens
33+
if token[0] is 'STAR'
34+
next_token = @tokens[i+1]
35+
unless next_token[0] is 'SEPARATOR' or next_token[0] is 'FROM'
36+
token[0] = 'MATH_MULTI'
2937

3038
token: (name, value) ->
3139
@tokens.push([name, value, @currentLine])

test/grammar.spec.coffee

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -245,3 +245,11 @@ describe "SQL Grammer", ->
245245
FROM `bar`
246246
))
247247
"""
248+
249+
describe "STARS", ->
250+
it "parses stars as multiplcation", ->
251+
parse('SELECT * FROM foo WHERE a = 1*2').toString().should.eql """
252+
SELECT *
253+
FROM `foo`
254+
WHERE (`a` = (1 * 2))
255+
"""

test/lexer.spec.coffee

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,23 @@ describe "SQL Lexer", ->
1111
["EOF", "", 1]
1212
]
1313

14+
it "eats select queries with stars and multiplication", ->
15+
tokens = lexer.tokenize("select * from my_table where foo = 1 * 2")
16+
tokens.should.eql [
17+
["SELECT", "select", 1]
18+
["STAR", "*", 1]
19+
["FROM", "from", 1]
20+
["LITERAL", "my_table", 1]
21+
["WHERE", "where", 1]
22+
["LITERAL", "foo", 1]
23+
["OPERATOR", "=", 1]
24+
["NUMBER", "1", 1]
25+
["MATH_MULTI", "*", 1]
26+
["NUMBER", "2", 1]
27+
["EOF", "", 1]
28+
]
29+
30+
1431
it "eats sub selects", ->
1532
tokens = lexer.tokenize("select * from (select * from my_table) t")
1633
tokens.should.eql [

0 commit comments

Comments
 (0)