1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
|
#ifndef FORMATSTATEMENT_H
#define FORMATSTATEMENT_H
#include "parser/ast/sqlitestatement.h"
#include "parser/ast/sqlitesortorder.h"
#include "parser/ast/sqliteconflictalgo.h"
#include "common/utils_sql.h"
#include "sqlenterpriseformatter.h"
#include <QString>
#include <QStringList>
#include <QHash>
#include <QStack>
#include <QVariant>
#include <functional>
class FormatStatement
{
public:
enum class ListSeparator
{
NONE,
COMMA,
EXPR_COMMA,
NEW_LINE,
SEMICOLON
};
typedef std::function<void(FormatStatement*)> FormatStatementEnricher;
FormatStatement();
virtual ~FormatStatement();
QString format();
void setSelectedWrapper(NameWrapper wrapper);
void setConfig(Cfg::SqlEnterpriseFormatterConfig* cfg);
static FormatStatement* forQuery(SqliteStatement *query);
FormatStatement& withKeyword(const QString& kw);
FormatStatement& withLinedUpKeyword(const QString& kw, const QString& lineUpName = QString());
FormatStatement& withId(const QString& id);
FormatStatement& withIdList(const QStringList& names, const QString& indentName = QString(), ListSeparator sep = ListSeparator::COMMA);
FormatStatement& withOperator(const QString& oper);
FormatStatement& withStringOrId(const QString& id);
FormatStatement& withIdDot();
FormatStatement& withStar();
FormatStatement& withFloat(double value);
FormatStatement& withInteger(qint64 value);
FormatStatement& withString(const QString& value);
FormatStatement& withBlob(const QString& value);
FormatStatement& withBindParam(const QString& name);
FormatStatement& withParDefLeft();
FormatStatement& withParDefRight();
FormatStatement& withParExprLeft();
FormatStatement& withParExprRight();
FormatStatement& withParFuncLeft();
FormatStatement& withParFuncRight();
FormatStatement& withSemicolon();
FormatStatement& withListComma();
FormatStatement& withCommaOper();
FormatStatement& withSortOrder(SqliteSortOrder sortOrder);
FormatStatement& withConflict(SqliteConflictAlgo onConflict);
FormatStatement& withFuncId(const QString& func);
FormatStatement& withDataType(const QString& dataType);
FormatStatement& withNewLine();
FormatStatement& withLiteral(const QVariant& value);
FormatStatement& withStatement(SqliteStatement* stmt, const QString& indentName = QString(), FormatStatementEnricher enricher = nullptr);
FormatStatement& markIndent(const QString& name);
FormatStatement& markAndKeepIndent(const QString& name);
FormatStatement& withIncrIndent(int newIndent);
FormatStatement& withIncrIndent(const QString& name = QString());
FormatStatement& withDecrIndent();
FormatStatement& markKeywordLineUp(const QString& keyword, const QString& lineUpName = QString());
FormatStatement& withSeparator(ListSeparator sep);
template <class T>
FormatStatement& withStatementList(QList<T*> stmtList, const QString& indentName = QString(), ListSeparator sep = ListSeparator::COMMA,
FormatStatementEnricher enricher = nullptr)
{
if (!indentName.isNull())
markAndKeepIndent(indentName);
bool first = true;
foreach (T* stmt, stmtList)
{
if (!first)
withSeparator(sep);
withStatement(stmt, QString(), enricher);
first = false;
}
if (!indentName.isNull())
withDecrIndent();
return *this;
}
template <class T>
T* getFormatStatement(SqliteStatement* stmt)
{
return dynamic_cast<T*>(forQuery(stmt, dialect, wrapper, cfg));
}
protected:
void handleExplainQuery(SqliteQuery* query);
virtual void formatInternal() = 0;
virtual void resetInternal();
Dialect dialect = Dialect::Sqlite3;
NameWrapper wrapper = NameWrapper::BRACKET;
Cfg::SqlEnterpriseFormatterConfig* cfg = nullptr;
private:
struct FormatToken
{
enum Type
{
KEYWORD,
LINED_UP_KEYWORD,
ID,
STRING_OR_ID,
OPERATOR,
STAR,
FLOAT,
STRING,
INTEGER,
BLOB,
BIND_PARAM,
ID_DOT,
PAR_DEF_LEFT,
PAR_DEF_RIGHT,
PAR_EXPR_LEFT,
PAR_EXPR_RIGHT,
PAR_FUNC_LEFT,
PAR_FUNC_RIGHT,
SEMICOLON,
COMMA_LIST,
COMMA_OPER, // for example in LIMIT
FUNC_ID,
DATA_TYPE,
NEW_LINE,
INDENT_MARKER,
INCR_INDENT,
SET_INDENT,
DECR_INDENT,
MARK_KEYWORD_LINEUP
};
Type type;
QVariant value;
QVariant additionalValue;
};
void withToken(FormatToken::Type type, const QVariant& value, const QVariant& additionalValue = QVariant());
void cleanup();
void buildTokens();
bool applyIndent();
void applySpace(FormatToken::Type type);
bool isSpaceExpectingType(FormatToken::Type type);
bool isMetaType(FormatToken::Type type);
void newLine();
void incrIndent(const QString& name = QString());
void decrIndent();
void setIndent(int newIndent);
bool endsWithSpace();
FormatToken* getLastRealToken(bool skipNewLines = false);
QString detokenize();
void detokenizeLeftPar(FormatToken* token, bool spaceBefore, bool spaceAfter, bool nlBefore, bool nlAfter);
void detokenizeRightPar(FormatToken* token, bool spaceBefore, bool spaceAfter, bool nlBefore, bool nlAfter);
void resetIndents();
void removeAllSpaces();
void removeAllSpacesFromLine();
void updateLastToken(FormatToken* token);
QString getFinalLineUpName(const QString& lineUpName);
int predictCurrentIndent(FormatToken* currentMetaToken);
bool willStartWithNewLine(FormatToken* token);
void formatId(const QString& value);
int getLineUpValue(const QString& lineUpName);
static FormatStatement* forQuery(SqliteStatement *query, Dialect dialect, NameWrapper wrapper, Cfg::SqlEnterpriseFormatterConfig* cfg);
QHash<QString,int> kwLineUpPosition;
QHash<QString,int> namedIndents;
QStack<int> indents;
QList<FormatToken*> tokens;
bool deleteTokens = true;
QStringList lines;
QString line;
FormatToken* lastToken = nullptr;
QString statementName;
FormatStatement* parentFormatStatement = nullptr;
static qint64 nameSeq;
static const QString SPACE;
static const QString NEWLINE;
};
#endif // FORMATSTATEMENT_H
|