Improved generation of c/cpp string from query

Extra lines before and after query are removed. Whitespace at end of line
is removed. SQL comments are converted to cpp style comments and are outside
the string literal.

To achieve this the function now uses the SQLLexer to know what is comment.
This also required the additional capability in the lexer to also return whitespace and newline tokens.
Also a few bugs in the lexer were fixed.
This commit is contained in:
eelke 2019-08-19 13:52:23 +02:00
parent fbd630489e
commit 48ac8c6bab
7 changed files with 247 additions and 34 deletions

View file

@ -15,6 +15,7 @@ HEADERS +=
SOURCES += main.cpp \
tst_ConvertLangToSqlString.cpp \
tst_ConvertToMultiLineCString.cpp \
tst_ExplainJsonParser.cpp \
tst_expected.cpp \
tst_SqlLexer.cpp \

View file

@ -60,4 +60,3 @@ TEST(ConvertLangToSqlString, testSemiColon)
auto output = ConvertLangToSqlString(in);
ASSERT_EQ(output, expected);
}

View file

@ -0,0 +1,108 @@
#include <gtest/gtest.h>
#include <gmock/gmock-matchers.h>
#include "util.h"
#include "PrintTo_Qt.h"
using namespace testing;
TEST(ConvertToMultiLineCString, singleLine)
{
QString in(R"__(SELECT 1)__");
QString expected(R"__("SELECT 1")__");
auto output = ConvertToMultiLineCString(in);
ASSERT_EQ(output, expected);
}
TEST(ConvertToMultiLineCString, singleLineTrimWhiteSpace)
{
QString in(R"__(SELECT 1 )__");
QString expected(R"__("SELECT 1")__");
auto output = ConvertToMultiLineCString(in);
ASSERT_EQ(output, expected);
}
TEST(ConvertToMultiLineCString, singleLineWithComment)
{
QString in(R"__(SELECT 1 -- hello)__");
QString expected(R"__("SELECT 1" // hello)__");
auto output = ConvertToMultiLineCString(in);
ASSERT_EQ(output, expected);
}
TEST(ConvertToMultiLineCString, singleLineWithCommentTrimWhiteSpace)
{
// Check whitespace at end is removed but in between is kept
QString in(R"__(SELECT 1 -- hello )__");
QString expected(R"__("SELECT 1" // hello)__");
auto output = ConvertToMultiLineCString(in);
ASSERT_EQ(output, expected);
}
TEST(ConvertToMultiLineCString, multiLine)
{
QString in(
R"__(SELECT kol
FROM table)__");
QString expected(
R"__("SELECT kol\n"
"FROM table")__");
auto output = ConvertToMultiLineCString(in);
ASSERT_EQ(output, expected);
}
TEST(ConvertToMultiLineCString, multiLineWithComment)
{
QString in(
R"__(SELECT kol -- eerste
FROM table -- tweede)__");
QString expected(
R"__("SELECT kol\n" // eerste
"FROM table" // tweede)__");
auto output = ConvertToMultiLineCString(in);
ASSERT_EQ(output, expected);
}
// Test case for a discovered bug
TEST(ConvertToMultiLineCString, multiLineWithCommentNoErronousRepeat)
{
QString in(
R"__(SELECT kol -- eerste
FROM table)__");
QString expected(
R"__("SELECT kol\n" // eerste
"FROM table")__");
auto output = ConvertToMultiLineCString(in);
ASSERT_EQ(output, expected);
}
TEST(ConvertToMultiLineCString, trimExtraEmptyLines)
{
QString in(R"__(
SELECT 1
)__");
QString expected(R"__("SELECT 1")__");
auto output = ConvertToMultiLineCString(in);
ASSERT_EQ(output, expected);
}
TEST(ConvertToMultiLineCString, trimExtraEmptyLines2)
{
QString in(R"__(
SELECT 1
FROM tab
)__");
QString expected(R"__("SELECT 1\n"
"\n"
"FROM tab")__");
auto output = ConvertToMultiLineCString(in);
ASSERT_EQ(output, expected);
}

View file

@ -35,6 +35,27 @@ TEST(SqlLexer, lexer)
ASSERT_THAT( out, Eq(QString("SELECT")) );
}
TEST(SqlLexer, lexerWithWhiteSpace)
{
QString input = " SELECT ";
SqlLexer lexer(input, LexerState::Null, true);
int startpos, length;
BasicTokenType tokentype;
QString out;
lexer.nextBasicToken(startpos, length, tokentype, out);
ASSERT_THAT(startpos, Eq(0));
ASSERT_THAT(length, Eq(1));
ASSERT_THAT(tokentype, Eq(BasicTokenType::WhiteSpace));
ASSERT_THAT(out, Eq(QString(" ")) );
lexer.nextBasicToken(startpos, length, tokentype, out);
ASSERT_THAT(startpos, Eq(1));
ASSERT_THAT(length, Eq(6));
ASSERT_THAT(tokentype, Eq(BasicTokenType::Symbol));
ASSERT_THAT(out, Eq(QString("SELECT")) );
}
TEST(SqlLexer, lexer_quote_in_string)
{
QString input = " 'abc''def' ";
@ -48,6 +69,7 @@ TEST(SqlLexer, lexer_quote_in_string)
ASSERT_THAT(startpos, Eq(1));
ASSERT_THAT(length, Eq(10));
ASSERT_THAT(tokentype, Eq(BasicTokenType::QuotedString));
ASSERT_THAT(out, Eq(QString("'abc''def'")) );
}
TEST(SqlLexer, lexer_comma_handling)