Fix for $-quoted strings

Note this does not solve all issues because we are tokenizing contents of strings of which we do not know they contains SQL
when the string is actually not SQL and contains $ the tokenizer gets confused.
This commit is contained in:
eelke 2023-01-07 07:41:58 +01:00
parent f3f1d47f7d
commit 0cd019db92
2 changed files with 37 additions and 0 deletions

View file

@ -255,6 +255,13 @@ bool SqlLexer::parseDoubleQuotedIdentifier(int startpos, int &length, BasicToken
bool SqlLexer::parseDollarQuote(int startpos, int &length, BasicTokenType &tokentype, QString &out)
{
QChar c = nextChar();
if (c == '$') {
tokentype = BasicTokenType::DollarQuote;
length = m_pos - startpos;
out = m_block.mid(startpos, length);
return true;
}
if (c.isDigit()) {
for (;;) {
c = peekChar();

View file

@ -19,6 +19,36 @@ TEST(SqlLexer, emptyInput)
ASSERT_THAT(tokentype, Eq(BasicTokenType::End));
}
TEST(SqlLexer, emptyDollarQuote)
{
QString input = "$$";
SqlLexer lexer(input, LexerState::Null);
int startpos = -1, length = -1;
BasicTokenType tokentype = BasicTokenType::None;
QString out;
lexer.nextBasicToken(startpos, length, tokentype, out);
ASSERT_THAT(startpos, Eq(0));
ASSERT_THAT("$$", Eq(out));
ASSERT_THAT(tokentype, Eq(BasicTokenType::DollarQuote));
}
TEST(SqlLexer, filledDollarQuote)
{
QString input = "$body$";
SqlLexer lexer(input, LexerState::Null);
int startpos = -1, length = -1;
BasicTokenType tokentype = BasicTokenType::None;
QString out;
lexer.nextBasicToken(startpos, length, tokentype, out);
ASSERT_THAT(startpos, Eq(0));
ASSERT_THAT("$body$", Eq(out));
ASSERT_THAT(tokentype, Eq(BasicTokenType::DollarQuote));
}
TEST(SqlLexer, lexer)
{
QString input = " SELECT ";