From 0cd019db92a957fa937b7cc385b04dba18ba52eb Mon Sep 17 00:00:00 2001 From: eelke Date: Sat, 7 Jan 2023 07:41:58 +0100 Subject: [PATCH] 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. --- core/SqlLexer.cpp | 7 +++++++ tests/pglabtests/tst_SqlLexer.cpp | 30 ++++++++++++++++++++++++++++++ 2 files changed, 37 insertions(+) diff --git a/core/SqlLexer.cpp b/core/SqlLexer.cpp index fb40150..55cf892 100644 --- a/core/SqlLexer.cpp +++ b/core/SqlLexer.cpp @@ -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(); diff --git a/tests/pglabtests/tst_SqlLexer.cpp b/tests/pglabtests/tst_SqlLexer.cpp index bcdb03e..0be623d 100644 --- a/tests/pglabtests/tst_SqlLexer.cpp +++ b/tests/pglabtests/tst_SqlLexer.cpp @@ -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 ";