Skip to content

Commit dfa3528

Browse files
eaallencursoragent
andcommitted
fix: support SQL-standard doubled quotes in string literals
MySQL/SQL allow escaping a quote inside a string by doubling it (e.g. 'O''Hare'). The lexer only recognized backslash escapes, so these literals were split into multiple STRING tokens and failed to parse. Update the STRING lexer rules to accept '' and "" inside quoted strings, matching https://dev.mysql.com/doc/refman/5.7/en/string-literals.html Co-authored-by: Cursor <cursoragent@cursor.com>
1 parent 6b4ef84 commit dfa3528

2 files changed

Lines changed: 13 additions & 2 deletions

File tree

src/sqlParser.jison

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -119,8 +119,8 @@ UNION return 'UNION'
119119
"}" return '}'
120120
";" return ';'
121121

122-
['](\\.|[^'])*['] return 'STRING'
123-
["](\\.|[^"])*["] return 'STRING'
122+
['](['][']|\\.|[^'])*['] return 'STRING'
123+
["](["]["]|\\.|[^"])*["] return 'STRING'
124124
[0][x][0-9a-fA-F]+ return 'HEX_NUMERIC'
125125
[-]?[0-9]+(\.[0-9]+)? return 'NUMERIC'
126126
[-]?[0-9]+(\.[0-9]+)?[eE][-][0-9]+(\.[0-9]+)? return 'EXPONENT_NUMERIC'

test/main.test.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -468,4 +468,15 @@ describe('select grammar support', function () {
468468
SELECT one.name, group_concat(j.value, ', ') FROM one, json_each(one.stringArray) AS j GROUP BY one.id
469469
`)
470470
})
471+
472+
it('support SQL-standard doubled quotes inside string literals', function () {
473+
const ast = testParser("select * from t where name = 'O''Hare'");
474+
assert.equal(ast.value.where.right.value, "'O''Hare'");
475+
476+
const astDouble = testParser('select * from t where name = "say ""hi"""');
477+
assert.equal(astDouble.value.where.right.value, '"say ""hi"""');
478+
479+
// empty string and backslash escapes should keep working
480+
testParser("select * from t where a = '' and b = 'O\\'Hare'");
481+
})
471482
});

0 commit comments

Comments
 (0)