Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix two single quotes parsing #20

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 27 additions & 4 deletions browser/sql-parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,27 @@
return this.tokens.push([name, value, this.currentLine]);
};

Lexer.prototype.tokenizeFromStringRegex = function(name, regex, part, lengthPart, output) {
var match, partMatch;
if (part == null) {
part = 0;
}
if (lengthPart == null) {
lengthPart = part;
}
if (output == null) {
output = true;
}
if (!(match = regex.exec(this.chunk))) {
return 0;
}
partMatch = match[part].replace(/''/g, "'");
if (output) {
this.token(name, partMatch);
}
return match[lengthPart].length;
};

Lexer.prototype.tokenizeFromRegex = function(name, regex, part, lengthPart, output) {
var match, partMatch;
if (part == null) {
Expand Down Expand Up @@ -169,7 +190,7 @@
};

Lexer.prototype.stringToken = function() {
return this.tokenizeFromRegex('STRING', STRING, 1, 0) || this.tokenizeFromRegex('DBLSTRING', DBLSTRING, 1, 0);
return this.tokenizeFromStringRegex('STRING', STRING, 1, 0) || this.tokenizeFromRegex('DBLSTRING', DBLSTRING, 1, 0);
};

Lexer.prototype.parensToken = function() {
Expand Down Expand Up @@ -221,7 +242,7 @@

BOOLEAN = ['TRUE', 'FALSE', 'NULL'];

MATH = ['+', '-'];
MATH = ['+', '-', '||', '&&'];

MATH_MULTI = ['/', '*'];

Expand All @@ -237,7 +258,7 @@

NUMBER = /^[0-9]+(\.[0-9]+)?/;

STRING = /^'([^\\']*(?:\\.[^\\']*)*)'/;
STRING = /^'((?:[^\\']+?|\\.|'')*)'(?!')/;

DBLSTRING = /^"([^\\"]*(?:\\.[^\\"]*)*)"/;

Expand Down Expand Up @@ -886,7 +907,9 @@ if (typeof module !== 'undefined' && require.main === module) {
}

StringValue.prototype.toString = function() {
return "" + this.quoteType + this.value + this.quoteType;
var escaped;
escaped = this.quoteType === "'" ? this.value.replace(/(^|[^\\])'/g, "$1''") : this.value;
return "" + this.quoteType + escaped + this.quoteType;
};

return StringValue;
Expand Down
27 changes: 24 additions & 3 deletions lib/lexer.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion lib/nodes.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 9 additions & 3 deletions src/lexer.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,12 @@ class Lexer
token: (name, value) ->
@tokens.push([name, value, @currentLine])

tokenizeFromStringRegex: (name, regex, part=0, lengthPart=part, output=true) ->
return 0 unless match = regex.exec(@chunk)
partMatch = match[part].replace(/''/g, "'")
@token(name, partMatch) if output
return match[lengthPart].length

tokenizeFromRegex: (name, regex, part=0, lengthPart=part, output=true) ->
return 0 unless match = regex.exec(@chunk)
partMatch = match[part]
Expand Down Expand Up @@ -112,7 +118,7 @@ class Lexer
numberToken: -> @tokenizeFromRegex('NUMBER', NUMBER)
parameterToken: -> @tokenizeFromRegex('PARAMETER', PARAMETER)
stringToken: ->
@tokenizeFromRegex('STRING', STRING, 1, 0) ||
@tokenizeFromStringRegex('STRING', STRING, 1, 0) ||
@tokenizeFromRegex('DBLSTRING', DBLSTRING, 1, 0)


Expand Down Expand Up @@ -146,15 +152,15 @@ class Lexer
SQL_CONDITIONALS = ['AND', 'OR']
SQL_BETWEENS = ['BETWEEN', 'NOT BETWEEN']
BOOLEAN = ['TRUE', 'FALSE', 'NULL']
MATH = ['+', '-']
MATH = ['+', '-', '||', '&&']
MATH_MULTI = ['/', '*']
STAR = /^\*/
SEPARATOR = /^,/
WHITESPACE = /^[ \n\r]+/
LITERAL = /^`?([a-z_][a-z0-9_]{0,})`?/i
PARAMETER = /^\$[0-9]+/
NUMBER = /^[0-9]+(\.[0-9]+)?/
STRING = /^'([^\\']*(?:\\.[^\\']*)*)'/
STRING = /^'((?:[^\\']+?|\\.|'')*)'(?!')/
DBLSTRING = /^"([^\\"]*(?:\\.[^\\"]*)*)"/


Expand Down
4 changes: 3 additions & 1 deletion src/nodes.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,9 @@ exports.LiteralValue = class LiteralValue

exports.StringValue = class StringValue
constructor: (@value, @quoteType="''") -> null
toString: -> "#{@quoteType}#{@value}#{@quoteType}"
toString: ->
escaped = if @quoteType is "'" then @value.replace /(^|[^\\])'/g, "$1''" else @value
"#{@quoteType}#{escaped}#{@quoteType}"

exports.NumberValue = class LiteralValue
constructor: (value) -> @value = Number(value)
Expand Down
14 changes: 14 additions & 0 deletions test/grammar.spec.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -292,13 +292,27 @@ describe "SQL Grammar", ->
WHERE (`foo` = 'I\\'m')
"""

it "parses single quote", ->
parse("select * from a where foo = ''''").toString().should.eql """
SELECT *
FROM `a`
WHERE (`foo` = '''')
"""

it "allows using double quotes", ->
parse('select * from a where foo = "a"').toString().should.eql """
SELECT *
FROM `a`
WHERE (`foo` = "a")
"""

it "allows using two single quotes", ->
parse("select * from a where foo = 'I''m'").toString().should.eql """
SELECT *
FROM `a`
WHERE (`foo` = 'I''m')
"""

it "allows nesting different quote styles", ->
parse("""select * from a where foo = "I'm" """).toString().should.eql """
SELECT *
Expand Down