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

@ -1,8 +1,9 @@
#include "SqlLexer.h"
SqlLexer::SqlLexer(QString block, LexerState currentstate)
SqlLexer::SqlLexer(QString block, LexerState currentstate, bool return_whitespace)
: m_block(std::move(block))
, m_state(currentstate)
, m_returnWhitespace(return_whitespace)
{}
QChar SqlLexer::nextChar()
@ -79,11 +80,32 @@ bool SqlLexer::nextBasicToken(int &startpos, int &length, BasicTokenType &tokent
startpos = m_pos;
QChar c = nextChar();
// if (LexerState::Null == m_state) {
if (c.isSpace()) {
// Just skip whitespace
continue;
if (c == '\n') {
if (m_returnWhitespace) {
length = m_pos - startpos;
tokentype = BasicTokenType::NewLine;
out = "\n";
return true;
}
}
if (c == '-' && peekChar() == '-') { // two dashes, start of comment
else if (c.isSpace()) {
// Just skip whitespace
if (m_returnWhitespace) {
for (;;) {
c = peekChar();
if (c != QChar::Null && c.isSpace() && c != '\n')
nextChar();
else
break;
}
length = m_pos - startpos;
tokentype = BasicTokenType::WhiteSpace;
QStringRef sr(&m_block, startpos, length);
out = sr.toString();
return true;
}
}
else if (c == '-' && peekChar() == '-') { // two dashes, start of comment
// Loop till end of line or end of block
c = nextChar();
for (;;) {
@ -95,9 +117,11 @@ bool SqlLexer::nextBasicToken(int &startpos, int &length, BasicTokenType &tokent
}
length = m_pos - startpos;
tokentype = BasicTokenType::Comment;
QStringRef sr(&m_block, startpos, length);
out = sr.toString();
return true;
}
if (c == ':') {
else if (c == ':') {
c = peekChar();
if (c == ':') {
nextChar();
@ -108,7 +132,7 @@ bool SqlLexer::nextBasicToken(int &startpos, int &length, BasicTokenType &tokent
return true;
}
}
if (isSelf(c)) {
else if (isSelf(c)) {
length = m_pos - startpos;
if (c == ',')
tokentype = BasicTokenType::Comma;
@ -119,7 +143,7 @@ bool SqlLexer::nextBasicToken(int &startpos, int &length, BasicTokenType &tokent
out = sr.toString();
return true;
}
if (isOperatorChar(c)) {
else if (isOperatorChar(c)) {
while (true) {
QChar c = peekChar();
if (isOperatorChar(c)) {
@ -137,11 +161,21 @@ bool SqlLexer::nextBasicToken(int &startpos, int &length, BasicTokenType &tokent
}
else if (c == '\'') {
// Single quoted string so it's an SQL text literal
return parseSingleQuotedString(startpos, length, tokentype);
if (parseSingleQuotedString(startpos, length, tokentype)) {
QStringRef sr(&m_block, startpos, length);
out = sr.toString();
return true;
}
return false;
}
else if (c == '"') {
// Double quoted identifier
return parseDoubleQuotedIdentifier(startpos, length, tokentype);
if (parseDoubleQuotedIdentifier(startpos, length, tokentype)) {
QStringRef sr(&m_block, startpos, length);
out = sr.toString();
return true;
}
return false;
}
// else if (c == '/' && peekChar() == '*') {
// nextChar();

View file

@ -17,7 +17,9 @@ enum class BasicTokenType {
Operator,
Self, // single char representing it self, maybe remove this and replace with token for each possibility
Comma,
Cast
Cast,
WhiteSpace,
NewLine
};
enum class LexerState {
@ -37,7 +39,7 @@ public:
class SqlLexer {
public:
SqlLexer(QString block, LexerState currentstate);
SqlLexer(QString block, LexerState currentstate, bool return_whitespace=false);
QChar nextChar();
QChar peekChar();
/**
@ -61,6 +63,7 @@ private:
QString m_block;
int m_pos = 0;
LexerState m_state;
bool m_returnWhitespace;
bool parseSingleQuotedString(int startpos, int &length, BasicTokenType &tokentype);
bool parseDoubleQuotedIdentifier(int startpos, int &length, BasicTokenType &tokentype);