forked from serenity-is/Serenity
-
Notifications
You must be signed in to change notification settings - Fork 0
/
SqlQuery_ToString.cs
411 lines (339 loc) · 15.5 KB
/
SqlQuery_ToString.cs
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
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
using System;
using System.Text;
namespace Serenity.Data
{
public partial class SqlQuery
{
/// <summary>
/// Formats SQL Query as string. If paging is used and skip requested, multiple queries
/// might be created one after each other.</summary>
/// <returns>
/// Formatted SELECT statement</returns>
public override string ToString()
{
var sb = new StringBuilder();
if (unionQuery != null)
{
sb.Append(unionQuery.ToString());
sb.Append("\n\n");
sb.Append(dialect.UnionKeyword(unionType));
sb.Append("\n\n");
}
// sub queries should be enclosed in paranthesis
if (this.parent != null)
sb.Append("(");
if (skip > 0 && orderBy == null && !dialect.CanUseSkipKeyword && !dialect.UseRowNum)
throw new InvalidOperationException("A query must be ordered by unique fields " +
"to be able to skip records!");
// no extra filter yet
string extraWhere = null;
bool useSkipKeyword = skip > 0 && dialect.CanUseSkipKeyword;
bool useOffset = skip > 0 && !useSkipKeyword && dialect.CanUseOffsetFetch;
bool useRowNum = (skip > 0 || take > 0) && dialect.UseRowNum;
bool useRowNumber = skip > 0 && !useSkipKeyword && !useOffset && !useRowNum && dialect.CanUseRowNumber;
bool useSecondQuery = skip > 0 && !useSkipKeyword && !useOffset && !useRowNumber;
// skip requested?
if (useRowNumber || useRowNum || useSecondQuery)
{
if (useRowNumber || useRowNum)
{
sb.Append("SELECT * FROM (\n");
}
else
{
// this part is for servers that does not support paging at all (e.g. SQL 2000)
const string AssignCmd = "@Value{0} = {1}";
const string DeclareCmd = "DECLARE @Value{0} SQL_VARIANT;\n";
const string Equality = "(({0} IS NULL AND @Value{1} IS NULL) OR ({0} = @Value{1}))";
const string Greater = "(({0} IS NOT NULL AND @Value{1} IS NULL) OR ({0} > @Value{1}))";
const string LessThan = "(({0} IS NULL AND @Value{1} IS NOT NULL) OR ({0} < @Value{1}))";
// to skip records and bring back remaining, first select only keys of SKIP records in
// the order requested. Find the sorted field values of last record.
// For example, if actual query is
// SELECT ID, A, B, C, D FROM TABLE WHERE X > Y ORDER BY ID.
// If SKIP is 5, need to skip first 5 records and continue from 6th.
// For this, we need to execute a query like
// DECLARE @ID SQL_VARIANT; SELECT TOP 5 @ID = ID FROM TABLE ORDER BY ID
// and get ID of the 5th record. To get the records after 5th:
// SELECT ID, A, B, C, D FROM TABLE WHERE ID > @ID AND X > Y ORDER BY ID
// Here, @ID is the ID of the 5th record determined with prior query.
// When there is more than one ordered field, for example when order is DATE, ID,
// again we first find the DATE and ID values of the last record.
// But, we can't simply write DATE > @DATE AND ID > @ID.
// Because this means query might not bring back records which has a bigger ID, even
// if their DATE value is smaller. The criteria that should be used is:
// (DATE > @DATE) OR (DATE = @DATE AND ID > @ID)
// Null is also another problem, as NULL comparison with any other value returns NULL.
// To eliminate NULL comparison problems the final criteria should be:
// ((DATE IS NOT NULL AND @DATE IS NULL) OR (DATE > @DATE)) OR
// ((ID IS NULL AND @ID IS NULL) OR (ID = @ID))
// In the first line, NULL values are assumed to be sorted before NOT NULL values,
// (which might not be the case for a few databases like FIREBIRD)
// In the second line, we assume two NULL values are equal in order.
// for second part of the query, e.g. records after first SKIP records
var check = new StringBuilder();
// sorted field names minus DESC
var order = new string[orderBy.Count];
// descending flag for sorted field names
var desc = new bool[orderBy.Count];
// scan all order list
for (int i = 0; i < orderBy.Count; i++)
{
// declare a SQL_VARIANT variable for all sorted fields
sb.AppendFormat(DeclareCmd, i);
string o = orderBy[i];
desc[i] = o.EndsWith(SqlKeywords.Desc, StringComparison.OrdinalIgnoreCase);
if (desc[i])
order[i] = o.Substring(0, o.Length - SqlKeywords.Desc.Length);
else
order[i] = o;
}
sb.Append(SqlKeywords.Select);
if (distinct)
sb.Append(SqlKeywords.Distinct);
sb.Append(dialect.TakeKeyword);
sb.Append(' ');
sb.Append(skip);
sb.Append(' ');
// @Value1 = SortedField1, @Value2 = SortedField2...
for (int i = 0; i < order.Length; i++)
{
if (i > 0)
sb.Append(',');
sb.AppendFormat(AssignCmd, i, order[i]);
}
AppendFromWhereOrderByGroupByHaving(sb, null, true);
sb.Append(";\n");
// the criteria to find records after the last one. it gets complexer as number of
// sorted fields increase. For example, for A, B, C, D sorted fields:
// (A > @A) OR
// (A = @A AND B > @B) OR
// (A = @A AND B = @B AND C > @C) OR
// (A = @A AND B = @B AND C = @C AND D > @D)
// NULL is ignored here for simplicity. As seen above, every line contains one more
// comparison the the one before that. First line number - 1 is equality and last
// one is greater than operator (or less than if descending)
// opening paren of all lines
check.Append('(');
for (int statement = 0; statement < order.Length; statement++)
{
if (statement > 0)
check.Append(" OR ");
// opening paren for this line
check.Append('(');
// write equality for line number minus 1 comparisons
for (int equality = 0; equality < statement; equality++)
{
if (equality > 0)
check.Append(" AND ");
// add equality operator with null checks
check.AppendFormat(Equality, order[equality], equality);
}
// add AND before GT/LT comparison. first line has no equality so no need for AND
if (statement > 0)
check.Append(" AND ");
if (desc[statement])
check.AppendFormat(LessThan, order[statement], statement);
else
check.AppendFormat(Greater, order[statement], statement);
check.Append(')');
}
check.Append(')');
// determine this statement as filter for next query
extraWhere = check.ToString();
}
}
// actual SELECT query starts here
sb.Append(SqlKeywords.Select);
if (distinct)
{
sb.Append(SqlKeywords.Distinct);
}
// add TOP N if number of records to fetch is limited
if (take != 0 && (!useOffset) && (!useRowNum) && (useRowNumber || !dialect.UseTakeAtEnd))
{
sb.Append(dialect.TakeKeyword);
sb.Append(' ');
sb.Append(useRowNumber ? (skip + take) : take);
sb.Append(' ');
}
if (useSkipKeyword)
{
sb.Append(dialect.SkipKeyword);
sb.Append(' ');
sb.Append(skip);
}
StringBuilder selCount = null;
if (distinct)
selCount = new StringBuilder();
sb.Append('\n');
// traverse selected columns
for (int i = 0; i < columns.Count; i++)
{
var s = columns[i];
if (i > 0)
{
sb.Append(",\n");
if (distinct)
selCount.Append(',');
}
sb.Append(s.Expression);
if (distinct)
selCount.Append(s.Expression);
// write alias if any
if (!string.IsNullOrEmpty(s.ColumnName))
{
sb.Append(SqlKeywords.As);
var quoted = dialect.QuoteColumnAlias(s.ColumnName);
sb.Append(quoted);
if (distinct)
{
selCount.Append(SqlKeywords.As);
selCount.Append(quoted);
}
}
}
if (useRowNumber || useRowNum)
{
if (columns.Count > 0)
sb.Append(", ");
if (useRowNum)
{
if (orderBy != null)
{
sb.Append("ROW_NUMBER() OVER (ORDER BY ");
for (int i = 0; i < orderBy.Count; i++)
{
if (i > 0)
sb.Append(", ");
sb.Append(orderBy[i]);
}
sb.Append(") AS numberingofrow");
}
else
{
sb.Append("ROWNUM AS numberingofrow");
}
}
else
{
sb.Append("ROW_NUMBER() OVER (ORDER BY ");
if (orderBy != null)
for (int i = 0; i < orderBy.Count; i++)
{
if (i > 0)
sb.Append(", ");
sb.Append(orderBy[i]);
}
sb.Append(") AS __num__");
}
}
// write remaining parts of the select query
AppendFromWhereOrderByGroupByHaving(sb, extraWhere, !useRowNumber);
if (useRowNumber)
{
sb.Append(") __results__ WHERE __num__ > ");
sb.Append(skip);
}
if (useRowNum)
{
sb.Append(") WHERE numberingofrow > " + skip);
if (take > 0)
sb.Append(" AND ROWNUM <= " + take);
}
if (take != 0 && (!useRowNum) && (!useOffset) && !useRowNumber && dialect.UseTakeAtEnd)
{
sb.Append(' ');
sb.Append(dialect.TakeKeyword);
sb.Append(' ');
sb.Append(take);
}
if (useOffset)
{
if (take == 0)
sb.Append(String.Format(dialect.OffsetFormat, skip, take));
else
sb.Append(String.Format(dialect.OffsetFetchFormat, skip, take));
}
if (!string.IsNullOrEmpty(forXml))
{
sb.Append(" FOR XML ");
sb.Append(forXml);
}
if (countRecords)
{
if (!dialect.MultipleResultsets)
sb.Append("\n---\n"); // temporary fix till we find a better solution for firebird
else
sb.Append(";\n");
sb.Append(SqlKeywords.Select);
sb.Append("count(*) ");
if (distinct)
{
sb.Append(SqlKeywords.From);
sb.Append('(');
sb.Append(SqlKeywords.Select);
sb.Append(SqlKeywords.Distinct);
sb.Append(selCount);
}
else if (groupBy != null && groupBy.Length > 0)
{
sb.Append(SqlKeywords.From);
sb.Append('(');
sb.Append(SqlKeywords.Select);
sb.Append(" 1 as _alias_x_ ");
}
AppendFromWhereOrderByGroupByHaving(sb, null, false);
if (distinct || (groupBy != null && groupBy.Length > 0))
{
sb.Append(") _alias_");
}
}
// sub queries should be enclosed in paranthesis
if (this.parent != null)
sb.Append(")");
return sb.ToString();
}
private void AppendFromWhereOrderByGroupByHaving(StringBuilder sb, string extraWhere,
bool includeOrderBy)
{
if (from.Length > 0)
{
sb.Append(SqlKeywords.From);
sb.Append(from.ToString());
}
if (extraWhere != null || where != null)
{
sb.Append(SqlKeywords.Where);
if (where != null)
sb.Append(@where);
if (extraWhere != null)
{
if (where != null)
sb.Append(" AND ");
sb.Append(extraWhere);
}
}
if (groupBy != null)
{
sb.Append(SqlKeywords.GroupBy);
sb.Append(groupBy);
}
if (having != null)
{
sb.Append(SqlKeywords.Having);
sb.Append(having);
}
if (includeOrderBy && orderBy != null)
{
sb.Append(SqlKeywords.OrderBy);
for (int i = 0; i < orderBy.Count; i++)
{
if (i > 0)
sb.Append(", ");
sb.Append(orderBy[i]);
}
}
}
}
}