-
Notifications
You must be signed in to change notification settings - Fork 935
/
Copy pathQuerySelect.cs
333 lines (298 loc) · 7.85 KB
/
QuerySelect.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
using System.Collections;
using System.Collections.Generic;
using System.Text;
using NHibernate.Util;
namespace NHibernate.SqlCommand
{
/// <summary>
/// Summary description for QuerySelect.
/// </summary>
public class QuerySelect
{
private readonly JoinFragment joins;
private readonly SqlStringBuilder selectBuilder = new SqlStringBuilder();
private readonly SqlStringBuilder whereBuilder = new SqlStringBuilder();
// groupBy, orderBy, and having will for sure have no parameters.
private readonly SqlStringBuilder groupBy = new SqlStringBuilder();
private readonly SqlStringBuilder orderBy = new SqlStringBuilder();
private readonly SqlStringBuilder having = new SqlStringBuilder();
// false by default
private bool distinct;
/// <summary>
/// Certain databases don't like spaces around these operators.
/// </summary>
/// <remarks>
/// This needs to contain both a plain string and a
/// SqlString version of the operator because the portions in
/// the WHERE clause will come in as SqlStrings since there
/// might be parameters, other portions of the clause come in
/// as strings since there are no parameters.
/// </remarks>
private static readonly HashSet<object> dontSpace = new HashSet<object>();
/// <summary></summary>
static QuerySelect()
{
//dontSpace.add("'");
dontSpace.Add(".");
dontSpace.Add("+");
dontSpace.Add("-");
dontSpace.Add("/");
dontSpace.Add("*");
dontSpace.Add("<");
dontSpace.Add(">");
dontSpace.Add("=");
dontSpace.Add("#");
dontSpace.Add("~");
dontSpace.Add("|");
dontSpace.Add("&");
dontSpace.Add("<=");
dontSpace.Add(">=");
dontSpace.Add("=>");
dontSpace.Add("=<");
dontSpace.Add("!=");
dontSpace.Add("<>");
dontSpace.Add("!#");
dontSpace.Add("!~");
dontSpace.Add("!<");
dontSpace.Add("!>");
// MySQL doesn't like spaces around "(" or ")" also.
dontSpace.Add(StringHelper.OpenParen);
dontSpace.Add(StringHelper.ClosedParen);
dontSpace.Add(new SqlString("."));
dontSpace.Add(new SqlString("+"));
dontSpace.Add(new SqlString("-"));
dontSpace.Add(new SqlString("/"));
dontSpace.Add(new SqlString("*"));
dontSpace.Add(new SqlString("<"));
dontSpace.Add(new SqlString(">"));
dontSpace.Add(new SqlString("="));
dontSpace.Add(new SqlString("#"));
dontSpace.Add(new SqlString("~"));
dontSpace.Add(new SqlString("|"));
dontSpace.Add(new SqlString("&"));
dontSpace.Add(new SqlString("<="));
dontSpace.Add(new SqlString(">="));
dontSpace.Add(new SqlString("=>"));
dontSpace.Add(new SqlString("=<"));
dontSpace.Add(new SqlString("!="));
dontSpace.Add(new SqlString("<>"));
dontSpace.Add(new SqlString("!#"));
dontSpace.Add(new SqlString("!~"));
dontSpace.Add(new SqlString("!<"));
dontSpace.Add(new SqlString("!>"));
// MySQL doesn't like spaces around "(" or ")" also.
dontSpace.Add(new SqlString(StringHelper.OpenParen));
dontSpace.Add(new SqlString(StringHelper.ClosedParen));
}
/// <summary>
///
/// </summary>
/// <param name="dialect"></param>
public QuerySelect(Dialect.Dialect dialect)
{
joins = new QueryJoinFragment(dialect, false);
}
/// <summary></summary>
public JoinFragment JoinFragment
{
get { return joins; }
}
/// <summary>
///
/// </summary>
/// <param name="fragment"></param>
public void AddSelectFragmentString(SqlString fragment)
{
if (fragment.StartsWithCaseInsensitive(","))
{
fragment = fragment.Substring(1);
}
fragment = fragment.Trim();
if (fragment.Length > 0)
{
if (selectBuilder.Count > 0)
{
selectBuilder.Add(StringHelper.CommaSpace);
}
selectBuilder.Add(fragment);
}
}
/// <summary>
///
/// </summary>
/// <param name="columnName"></param>
/// <param name="alias"></param>
public void AddSelectColumn(string columnName, string alias)
{
AddSelectFragmentString(new SqlString(columnName + ' ' + alias));
}
/// <summary></summary>
public bool Distinct
{
get { return distinct; }
set { distinct = value; }
}
/// <summary>
///
/// </summary>
/// <param name="tokens"></param>
public void SetWhereTokens(IEnumerable tokens)
{
//if ( conjunctiveWhere.length()>0 ) conjunctiveWhere.append(" and ");
AppendTokens(whereBuilder, tokens);
//AppendTokens(where, tokens);
}
/// <summary>
///
/// </summary>
/// <param name="tokens"></param>
public void SetGroupByTokens(IEnumerable tokens)
{
//if ( groupBy.length()>0 ) groupBy.append(" and ");
AppendTokens(groupBy, tokens);
}
/// <summary>
///
/// </summary>
/// <param name="tokens"></param>
public void SetOrderByTokens(IEnumerable tokens)
{
//if ( orderBy.length()>0 ) orderBy.append(" and ");
AppendTokens(orderBy, tokens);
}
/// <summary>
///
/// </summary>
/// <param name="tokens"></param>
public void SetHavingTokens(IEnumerable tokens)
{
//if ( having.length()>0 ) having.append(" and ");
AppendTokens(having, tokens);
}
/// <summary>
/// Adds a string containing a valid "order by" sql statement
/// to this QuerySelect
/// </summary>
/// <param name="orderBySql">The "order by" sql statement.</param>
public void AddOrderBy(string orderBySql)
{
if (orderBy.Count > 0)
{
orderBy.Add(StringHelper.CommaSpace);
}
orderBy.Add(orderBySql);
}
public SqlString ToQuerySqlString()
{
var builder = new SqlStringBuilder();
builder.Add("select ");
if (distinct)
{
builder.Add("distinct ");
}
SqlString from = joins.ToFromFragmentString;
if (from.StartsWithCaseInsensitive(","))
{
from = from.Substring(1);
}
else if (from.StartsWithCaseInsensitive(" inner join"))
{
from = from.Substring(11);
}
builder.Add(selectBuilder.ToSqlString())
.Add(" from")
.Add(from);
SqlString part1 = joins.ToWhereFragmentString.Trim();
SqlString part2 = whereBuilder.ToSqlString();
bool hasPart1 = part1.Count > 0;
bool hasPart2 = part2.Count > 0;
if (hasPart1 || hasPart2)
{
builder.Add(" where ");
}
if (hasPart1)
{
builder.Add(part1.Substring(4));
}
if (hasPart2)
{
if (hasPart1)
{
builder.Add(" and (");
}
builder.Add(part2);
if (hasPart1)
{
builder.Add(")");
}
}
if (groupBy.Count > 0)
{
builder.Add(" group by ").Add(groupBy.ToSqlString());
}
if (having.Count > 0)
{
builder.Add(" having ").Add(having.ToSqlString());
}
if (orderBy.Count > 0)
{
builder.Add(" order by ").Add(orderBy.ToSqlString());
}
return builder.ToSqlString();
}
/// <summary>
///
/// </summary>
/// <param name="builder"></param>
/// <param name="iter"></param>
private static void AppendTokens(SqlStringBuilder builder, IEnumerable iter)
{
bool lastSpaceable = true;
bool lastQuoted = false;
foreach (object token in iter)
{
var tokenString = token as string;
var tokenSqlString = token as SqlString;
bool spaceable = !dontSpace.Contains(token);
bool quoted;
//TODO: seems HACKish to cast between String and SqlString
if (tokenString != null)
{
quoted = tokenString.StartsWith('\'');
}
else
{
quoted = tokenSqlString.StartsWithCaseInsensitive("'");
}
if (spaceable && lastSpaceable)
{
if (!quoted || !lastQuoted)
{
builder.Add(" ");
}
}
lastSpaceable = spaceable;
if (token.Equals(StringHelper.SqlParameter))
{
Parameter param = Parameter.Placeholder;
builder.Add(param);
}
else
{
// not sure if we have a string or a SqlString here and token is a
// reference to an object - so let the builder figure out what the
// actual object is
builder.AddObject(token);
}
if (tokenString != null)
{
lastQuoted = tokenString.EndsWith("'");
}
else
{
lastQuoted = tokenSqlString.EndsWith("'");
}
}
}
}
}