Skip to content

Commit 19baeb2

Browse files
committed
grammar: Fix double quote characters as used for escaping one
SQL allows you to use two quote characters instead of just one in order to escape them. Example: ['aa''bb'] is read as [aa'bb]. Add detection for these doubled quote characters to our grammar parser. See issue sqlitebrowser#128.
1 parent a0f22a1 commit 19baeb2

File tree

3 files changed

+15
-7
lines changed

3 files changed

+15
-7
lines changed

src/grammar/Sqlite3Lexer.cpp

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -528,11 +528,15 @@ void Sqlite3Lexer::mQUOTEDLITERAL(bool _createToken) {
528528
match(L'\"' /* charlit */ );
529529
{ // ( ... )*
530530
for (;;) {
531-
if ((_tokenSet_2.member(LA(1)))) {
531+
if ((LA(1) == 0x22 /* '\"' */ ) && (LA(2) == 0x22 /* '\"' */ )) {
532532
{
533-
match(_tokenSet_2);
533+
match(L'\"' /* charlit */ );
534+
match(L'\"' /* charlit */ );
534535
}
535536
}
537+
else if ((_tokenSet_2.member(LA(1)))) {
538+
matchNot(L'\"' /* charlit */ );
539+
}
536540
else {
537541
goto _loop17;
538542
}
@@ -873,11 +877,15 @@ void Sqlite3Lexer::mSTRINGLITERAL(bool _createToken) {
873877
match(L'\'' /* charlit */ );
874878
{ // ( ... )*
875879
for (;;) {
876-
if ((_tokenSet_4.member(LA(1)))) {
880+
if ((LA(1) == 0x27 /* '\'' */ ) && (LA(2) == 0x27 /* '\'' */ )) {
877881
{
878-
match(_tokenSet_4);
882+
match(L'\'' /* charlit */ );
883+
match(L'\'' /* charlit */ );
879884
}
880885
}
886+
else if ((_tokenSet_4.member(LA(1)))) {
887+
matchNot(L'\'' /* charlit */ );
888+
}
881889
else {
882890
goto _loop46;
883891
}

src/grammar/sqlite3.g

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ QUOTEDID
105105
;
106106

107107
QUOTEDLITERAL
108-
: '"' ( ~('"') )* '"'
108+
: '"' ( ~'"' | ('"' '"' ) )* '"'
109109
;
110110

111111
NUMERIC
@@ -135,7 +135,7 @@ WS :
135135
STRINGLITERAL
136136
:
137137
// '"' ( ESC_SEQ | ~('\\'|'"') )* '"'
138-
'\'' ( ~('\'') )* '\''
138+
'\'' ( ~'\'' | ('\'' '\'') )* '\''
139139
;
140140

141141
//protected

src/tests/testsqlobjects.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -221,7 +221,7 @@ void TestTable::parseSQLEscapedQuotes()
221221

222222
QCOMPARE(tab.name(), QString("double_quotes"));
223223
QCOMPARE(tab.fields().at(0)->name(), QString("a"));
224-
QCOMPARE(tab.fields().at(0)->defaultValue(), QString("a''a"));
224+
QCOMPARE(tab.fields().at(0)->defaultValue(), QString("'a''a'"));
225225
}
226226

227227
void TestTable::createTableWithIn()

0 commit comments

Comments
 (0)