-
Notifications
You must be signed in to change notification settings - Fork 934
/
Copy pathSqlSelectBuilder.cs
342 lines (302 loc) · 10.6 KB
/
SqlSelectBuilder.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
using System;
using NHibernate.Engine;
using NHibernate.Type;
using NHibernate.Util;
namespace NHibernate.SqlCommand
{
/// <summary>
/// Builds a <c>SELECT</c> SQL statement.
/// </summary>
public class SqlSelectBuilder : SqlBaseBuilder, ISqlStringBuilder
{
private static readonly INHibernateLogger log = NHibernateLogger.For(typeof(SqlSelectBuilder));
private SqlString selectClause;
private string fromClause;
private SqlString outerJoinsAfterFrom;
private SqlString whereClause;
private SqlString outerJoinsAfterWhere;
private SqlString orderByClause;
private SqlString groupByClause;
private SqlString havingClause;
private LockMode lockMode;
private string mainTableAlias;
private string comment;
public SqlSelectBuilder(ISessionFactoryImplementor factory)
: base(factory.Dialect, factory) {}
public SqlSelectBuilder SetComment(string comment)
{
this.comment = comment;
return this;
}
/// <summary>
/// Sets the text that should appear after the FROM
/// </summary>
/// <param name="fromClause">The fromClause to set</param>
/// <returns>The SqlSelectBuilder</returns>
public SqlSelectBuilder SetFromClause(string fromClause)
{
this.fromClause = fromClause;
return this;
}
/// <summary>
/// Sets the text that should appear after the FROM
/// </summary>
/// <param name="tableName">The name of the Table to get the data from</param>
/// <param name="alias">The Alias to use for the table name.</param>
/// <returns>The SqlSelectBuilder</returns>
public SqlSelectBuilder SetFromClause(string tableName, string alias)
{
fromClause = tableName + " " + alias;
return this;
}
/// <summary>
/// Sets the text that should appear after the FROM
/// </summary>
/// <param name="fromClause">The fromClause in a SqlString</param>
/// <returns>The SqlSelectBuilder</returns>
public SqlSelectBuilder SetFromClause(SqlString fromClause)
{
// it is safe to do this because a fromClause will have no parameters
return SetFromClause(fromClause.ToString());
}
/// <summary>
/// Sets the text that should appear after the ORDER BY.
/// </summary>
/// <param name="orderByClause">The orderByClause to set</param>
/// <returns>The SqlSelectBuilder</returns>
public SqlSelectBuilder SetOrderByClause(SqlString orderByClause)
{
this.orderByClause = orderByClause;
return this;
}
/// <summary>
/// Sets the text that should appear after the GROUP BY.
/// </summary>
/// <param name="groupByClause">The groupByClause to set</param>
/// <returns>The SqlSelectBuilder</returns>
public SqlSelectBuilder SetGroupByClause(SqlString groupByClause)
{
this.groupByClause = groupByClause;
return this;
}
/// <summary>
/// Sets the SqlString for the OUTER JOINs.
/// </summary>
/// <remarks>
/// All of the Sql needs to be included in the SELECT. No OUTER JOINS will automatically be
/// added.
/// </remarks>
/// <param name="outerJoinsAfterFrom">The outerJoinsAfterFrom to set</param>
/// <param name="outerJoinsAfterWhere">The outerJoinsAfterWhere to set</param>
/// <returns>The SqlSelectBuilder</returns>
public SqlSelectBuilder SetOuterJoins(SqlString outerJoinsAfterFrom, SqlString outerJoinsAfterWhere)
{
this.outerJoinsAfterFrom = outerJoinsAfterFrom;
SqlString tmpOuterJoinsAfterWhere = outerJoinsAfterWhere.Trim();
if (tmpOuterJoinsAfterWhere.StartsWithCaseInsensitive("and"))
{
tmpOuterJoinsAfterWhere = tmpOuterJoinsAfterWhere.Substring(4);
}
this.outerJoinsAfterWhere = tmpOuterJoinsAfterWhere;
return this;
}
/// <summary>
/// Sets the text for the SELECT
/// </summary>
/// <param name="selectClause">The selectClause to set</param>
/// <returns>The SqlSelectBuilder</returns>
public SqlSelectBuilder SetSelectClause(SqlString selectClause)
{
this.selectClause = selectClause;
return this;
}
/// <summary>
/// Sets the text for the SELECT
/// </summary>
/// <param name="selectClause">The selectClause to set</param>
/// <returns>The SqlSelectBuilder</returns>
public SqlSelectBuilder SetSelectClause(string selectClause)
{
this.selectClause = new SqlString(selectClause);
return this;
}
/// <summary>
/// Sets the criteria to use for the WHERE. It joins all of the columnNames together with an AND.
/// </summary>
/// <param name="tableAlias"></param>
/// <param name="columnNames">The names of the columns</param>
/// <param name="whereType">The Hibernate Type</param>
/// <returns>The SqlSelectBuilder</returns>
public SqlSelectBuilder SetWhereClause(string tableAlias, string[] columnNames, IType whereType)
{
return SetWhereClause(ToWhereString(tableAlias, columnNames));
}
/// <summary>
/// Sets the prebuilt SqlString to the Where clause
/// </summary>
/// <param name="whereSqlString">The SqlString that contains the sql and parameters to add to the WHERE</param>
/// <returns>This SqlSelectBuilder</returns>
public SqlSelectBuilder SetWhereClause(SqlString whereSqlString)
{
whereClause = whereSqlString;
return this;
}
/// <summary>
/// Sets the criteria to use for the WHERE. It joins all of the columnNames together with an AND.
/// </summary>
/// <param name="tableAlias"></param>
/// <param name="columnNames">The names of the columns</param>
/// <param name="whereType">The Hibernate Type</param>
/// <returns>The SqlSelectBuilder</returns>
public SqlSelectBuilder SetHavingClause(string tableAlias, string[] columnNames, IType whereType)
{
return SetHavingClause(ToWhereString(tableAlias, columnNames));
}
/// <summary>
/// Sets the prebuilt SqlString to the Having clause
/// </summary>
/// <param name="havingSqlString">The SqlString that contains the sql and parameters to add to the HAVING</param>
/// <returns>This SqlSelectBuilder</returns>
public SqlSelectBuilder SetHavingClause(SqlString havingSqlString)
{
havingClause = havingSqlString;
return this;
}
[Obsolete("For some DBMS's such as PostgreSQL, a lock on query with OUTER JOIN is not possible without specifying the not-null side. " +
"Use the new method SetLockMode(LockMode, mainTableAlias) instead.")]
public SqlSelectBuilder SetLockMode(LockMode lockMode)
{
this.lockMode = lockMode;
return this;
}
public SqlSelectBuilder SetLockMode(LockMode lockMode, string mainTableAlias)
{
this.lockMode = lockMode;
this.mainTableAlias = mainTableAlias;
return this;
}
#region ISqlStringBuilder Members
/// <summary>
/// ToSqlString() is named ToStatementString() in H3
/// </summary>
/// <returns></returns>
public SqlString ToStatementString()
{
return ToSqlString();
}
/// <summary></summary>
public SqlString ToSqlString()
{
// 4 = the "SELECT", selectClause, "FROM", fromClause are straight strings
// plus the number of parts in outerJoinsAfterFrom SqlString.
// 1 = the "WHERE"
// plus the number of parts in outerJoinsAfterWhere SqlString.
// 1 = the whereClause
// 2 = the "ORDER BY" and orderByClause
var joinAfterFrom = outerJoinsAfterFrom != null ? outerJoinsAfterFrom.Count : 0;
var joinAfterWhere = outerJoinsAfterWhere != null ? outerJoinsAfterWhere.Count : 0;
int initialCapacity = 4 + joinAfterFrom + 1 + joinAfterWhere + 1 + 2;
if (!string.IsNullOrEmpty(comment))
initialCapacity++;
SqlStringBuilder sqlBuilder = new SqlStringBuilder(initialCapacity + 2);
if (!string.IsNullOrEmpty(comment))
sqlBuilder.Add("/* " + comment + " */ ");
sqlBuilder.Add("SELECT ")
.Add(selectClause)
.Add(" FROM ")
.Add(fromClause);
if (SqlStringHelper.IsNotEmpty(outerJoinsAfterFrom))
{
sqlBuilder.Add(outerJoinsAfterFrom);
}
if (SqlStringHelper.IsNotEmpty(whereClause) || SqlStringHelper.IsNotEmpty(outerJoinsAfterWhere))
{
sqlBuilder.Add(" WHERE ");
// the outerJoinsAfterWhere needs to come before where clause to properly
// handle dynamic filters
if (SqlStringHelper.IsNotEmpty(outerJoinsAfterWhere))
{
sqlBuilder.Add(outerJoinsAfterWhere);
if (SqlStringHelper.IsNotEmpty(whereClause))
{
sqlBuilder.Add(" AND ");
}
}
if (SqlStringHelper.IsNotEmpty(whereClause))
{
sqlBuilder.Add(whereClause);
}
}
if (SqlStringHelper.IsNotEmpty(groupByClause))
{
sqlBuilder.Add(" GROUP BY ")
.Add(groupByClause);
}
if(SqlStringHelper.IsNotEmpty(havingClause))
{
sqlBuilder.Add(" HAVING ")
.Add(havingClause);
}
if (SqlStringHelper.IsNotEmpty(orderByClause))
{
sqlBuilder.Add(" ORDER BY ")
.Add(orderByClause);
}
if (lockMode != null)
{
sqlBuilder.Add(GetForUpdateString());
}
if (log.IsDebugEnabled())
{
if (initialCapacity < sqlBuilder.Count)
{
log.Debug("The initial capacity was set too low at: {0} for the SelectSqlBuilder that needed a capacity of: {1} for the table {2}",
initialCapacity,
sqlBuilder.Count,
fromClause);
}
else if (initialCapacity > 16 && ((float) initialCapacity / sqlBuilder.Count) > 1.2)
{
log.Debug("The initial capacity was set too high at: {0} for the SelectSqlBuilder that needed a capacity of: {1} for the table {2}",
initialCapacity,
sqlBuilder.Count,
fromClause);
}
}
return sqlBuilder.ToSqlString();
}
private string GetForUpdateString()
{
if (!Dialect.SupportsOuterJoinForUpdate && HasOuterJoin())
{
var isUpgrade = Equals(lockMode, LockMode.Upgrade);
var isUpgradeNoWait = !isUpgrade && (
Equals(lockMode, LockMode.UpgradeNoWait) || Equals(lockMode, LockMode.Force));
if (!isUpgrade && !isUpgradeNoWait)
return string.Empty;
if (!Dialect.SupportsForUpdateOf)
{
log.Warn(
"Unsupported 'for update' case: 'for update' query with an outer join using a dialect not" +
"supporting it and not supporting 'for update of' clause. Discarding 'for" +
"update' clause.");
return string.Empty;
}
if (Dialect.UsesColumnsWithForUpdateOf)
{
log.Warn(
"Unimplemented 'for update' case: 'for update' query with an outer join using a dialect not" +
"supporting it and requiring columns for its 'for update of' syntax. Discarding 'for" +
"update' clause.");
return string.Empty;
}
return isUpgrade ? Dialect.GetForUpdateString(mainTableAlias) : Dialect.GetForUpdateNowaitString(mainTableAlias);
}
return Dialect.GetForUpdateString(lockMode);
bool HasOuterJoin() =>
outerJoinsAfterFrom?.IsEmptyOrWhitespace() == false ||
StringHelper.ContainsCaseInsensitive(fromClause, "outer join");
}
#endregion
}
}