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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
|
#include "sqlenterpriseformatter.h"
#include "formatstatement.h"
#include "common/unused.h"
#include "common/global.h"
#include <QDebug>
#include <parser/lexer.h>
#include <parser/parser.h>
SqlEnterpriseFormatter::SqlEnterpriseFormatter()
{
}
QString SqlEnterpriseFormatter::format(SqliteQueryPtr query)
{
QList<Comment*> comments = collectComments(query->tokens);
int wrapperIdx = cfg.SqlEnterpriseFormatter.Wrappers.get().indexOf(cfg.SqlEnterpriseFormatter.PrefferedWrapper.get());
NameWrapper wrapper = getAllNameWrappers()[wrapperIdx];
FormatStatement *formatStmt = FormatStatement::forQuery(query.data());
if (!formatStmt)
return query->detokenize();
formatStmt->setSelectedWrapper(wrapper);
formatStmt->setConfig(&cfg);
QString formatted = formatStmt->format();
delete formatStmt;
QString formattedWithComments = applyComments(formatted, comments, query->dialect);
for (Comment* c : comments)
delete c;
return formattedWithComments;
}
bool SqlEnterpriseFormatter::init()
{
Q_INIT_RESOURCE(sqlenterpriseformatter);
static_qstring(query1, "SELECT (2 + 4) AND (3 + 5), 4 NOT IN (SELECT t1.'some[_]name' + t2.[some'name2] FROM xyz t1 JOIN zxc t2 ON (t1.aaa = t2.aaa)) "
"FROM a, (SELECT id FROM table2);");
static_qstring(query2, "INSERT INTO table1 (id, value1, value2) VALUES (1, (2 + 5), (SELECT id FROM table2));");
static_qstring(query3, "CREATE TABLE tab (id INTEGER PRIMARY KEY, /*a primary key column*/ value1 VARCHAR(6), "
"value2 /*column with constraints*/ NUMBER(8,2) NOT NULL DEFAULT 1.0"
");");
static_qstring(query4, "CREATE UNIQUE INDEX IF NOT EXISTS dbName.idx1 ON [messages column] (id COLLATE x ASC, lang DESC, description);");
Parser parser(Dialect::Sqlite3);
for (const QString& q : {query1, query2, query3, query4})
{
if (!parser.parse(q))
{
qWarning() << "SqlEnterpriseFormatter preview query parsing error:" << parser.getErrorString();
continue;
}
previewQueries << parser.getQueries().first();
}
updatePreview();
return GenericPlugin::init();
}
void SqlEnterpriseFormatter::deinit()
{
Q_CLEANUP_RESOURCE(sqlenterpriseformatter);
}
void SqlEnterpriseFormatter::updatePreview()
{
QStringList output;
for (const SqliteQueryPtr& q : previewQueries)
output << format(q);
cfg.SqlEnterpriseFormatter.PreviewCode.set(output.join("\n\n"));
}
void SqlEnterpriseFormatter::configModified(CfgEntry* entry)
{
if (entry == &cfg.SqlEnterpriseFormatter.PreviewCode)
return;
updatePreview();
}
QString Cfg::getNameWrapperStr(NameWrapper wrapper)
{
return wrapObjName(QObject::tr("name", "example name wrapper"), wrapper);
}
QStringList Cfg::getNameWrapperStrings()
{
QStringList strings;
for (NameWrapper nw : getAllNameWrappers())
strings << wrapObjName(QObject::tr("name", "example name wrapper"), nw);
return strings;
}
CfgMain* SqlEnterpriseFormatter::getMainUiConfig()
{
return &cfg;
}
QString SqlEnterpriseFormatter::getConfigUiForm() const
{
return "SqlEnterpriseFormatter";
}
void SqlEnterpriseFormatter::configDialogOpen()
{
connect(&cfg.SqlEnterpriseFormatter, SIGNAL(changed(CfgEntry*)), this, SLOT(configModified(CfgEntry*)));
}
void SqlEnterpriseFormatter::configDialogClosed()
{
disconnect(&cfg.SqlEnterpriseFormatter, SIGNAL(changed(CfgEntry*)), this, SLOT(configModified(CfgEntry*)));
}
QList<SqlEnterpriseFormatter::Comment *> SqlEnterpriseFormatter::collectComments(const TokenList &tokens)
{
QList<Comment*> results;
QList<TokenList> tokensInLines = tokensByLines(tokens);
Comment* prevCommentInThisLine = nullptr;
Comment* cmt = nullptr;
bool tokensBefore = false;
int pos = 0;
int line = 0;
for (const TokenList& tokensInLine : tokensInLines)
{
tokensBefore = true;
prevCommentInThisLine = nullptr;
for (const TokenPtr& token : tokensInLine)
{
if (token->type == Token::Type::SPACE)
continue;
if (prevCommentInThisLine)
prevCommentInThisLine->tokensAfter = true;
if (token->type == Token::Type::COMMENT)
{
cmt = new Comment;
cmt->tokensBefore = tokensBefore;
cmt->position = pos;
cmt->multiline = token->value.startsWith("/*");
if (cmt->multiline)
cmt->contents = token->value.mid(2, token->value.length() - 4).trimmed();
else
cmt->contents = token->value.mid(2).trimmed();
results << cmt;
prevCommentInThisLine = cmt;
continue;
}
tokensBefore = true;
pos++;
}
line++;
}
return results;
}
QList<TokenList> SqlEnterpriseFormatter::tokensByLines(const TokenList &tokens, bool includeSpaces)
{
QList<TokenList> tokensInLines;
TokenList tokensInLine;
for (const TokenPtr& token : tokens)
{
if (includeSpaces || token->type != Token::Type::SPACE)
tokensInLine << token;
if (token->type == Token::Type::SPACE && token->value.contains('\n'))
{
tokensInLines << tokensInLine;
tokensInLine.clear();
}
}
if (tokensInLine.size() > 0)
tokensInLines << tokensInLine;
return tokensInLines;
}
TokenList SqlEnterpriseFormatter::adjustCommentsToEnd(const TokenList &inputTokens)
{
QList<TokenList> tokensInLines = tokensByLines(inputTokens, true);
TokenList newTokens;
TokenList commentTokensForLine;
TokenPtr newLineToken;
for (const TokenList& tokensInLine : tokensInLines)
{
commentTokensForLine.clear();
newLineToken.clear();
for (const TokenPtr& token : tokensInLine)
{
if (token->type == Token::Type::COMMENT)
{
wrapComment(token, true);
//token->value = " " + endLineCommentTpl.arg(token->value);
commentTokensForLine << token;
}
else if (token->type == Token::Type::SPACE && token->value.contains("\n"))
newLineToken = token;
else
newTokens << token;
}
newTokens += commentTokensForLine;
if (newLineToken)
newTokens << newLineToken;
}
return newTokens;
}
TokenList SqlEnterpriseFormatter::wrapOnlyComments(const TokenList &inputTokens)
{
QList<TokenList> tokensInLines = tokensByLines(inputTokens, true);
TokenList newTokens;
bool lineEnd = true;
for (const TokenList& tokensInLine : reverse(tokensInLines))
{
lineEnd = true;
for (const TokenPtr& token : reverse(tokensInLine))
{
if (!token->isWhitespace())
lineEnd = false;
if (token->type == Token::Type::COMMENT)
wrapComment(token, lineEnd);
newTokens << token;
}
}
return reverse(newTokens);
}
TokenList SqlEnterpriseFormatter::optimizeInnerComments(const TokenList &inputTokens)
{
// TODO
return inputTokens;
}
TokenList SqlEnterpriseFormatter::optimizeEndLineComments(const TokenList &inputTokens)
{
// TODO
return inputTokens;
}
void SqlEnterpriseFormatter::indentMultiLineComments(const TokenList &inputTokens)
{
// TODO
}
void SqlEnterpriseFormatter::wrapComment(const TokenPtr &token, bool isAtLineEnd)
{
static_qstring(multiCommentTpl, "/* %1 */");
static_qstring(endLineCommentTpl, "-- %1");
bool isMultiLine = token->value.contains("\n");
if (isAtLineEnd && !isMultiLine && cfg.SqlEnterpriseFormatter.PreferredCommentMarker.get() == "--")
token->value = endLineCommentTpl.arg(token->value);
else
token->value = multiCommentTpl.arg(token->value);
}
QString SqlEnterpriseFormatter::applyComments(const QString& formatted, QList<SqlEnterpriseFormatter::Comment*> comments, Dialect dialect)
{
if (comments.size() == 0)
return formatted;
int currentCommentPosition = comments.first()->position;
TokenList allTokens = Lexer::tokenize(formatted, dialect);
TokenList newTokens;
int currentTokenPosition = 0;
for (const TokenPtr& token : allTokens)
{
if (currentTokenPosition == currentCommentPosition)
{
newTokens << TokenPtr::create(Token::Type::COMMENT, comments.first()->contents);
comments.removeFirst();
if (comments.size() > 0)
currentCommentPosition = comments.first()->position;
else
currentCommentPosition = -1;
}
newTokens << token;
if (token->type != Token::Type::SPACE)
currentTokenPosition++;
}
// Any remaining comments
for (Comment* cmt : comments)
newTokens << TokenPtr::create(Token::Type::COMMENT, cmt->contents);
if (cfg.SqlEnterpriseFormatter.MoveAllCommentsToLineEnd.get())
newTokens = adjustCommentsToEnd(newTokens);
else
newTokens = wrapOnlyComments(newTokens);
newTokens = optimizeInnerComments(newTokens);
newTokens = optimizeEndLineComments(newTokens);
indentMultiLineComments(newTokens);
return newTokens.detokenize();
}
|