forked from nhibernate/nhibernate-core
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSqlStringBuilder.cs
352 lines (311 loc) · 9.84 KB
/
SqlStringBuilder.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
using System;
using System.Collections;
using System.Collections.Generic;
using NHibernate.Util;
namespace NHibernate.SqlCommand
{
/// <summary>
/// The SqlStringBuilder is used to construct a SqlString.
/// </summary>
/// <remarks>
/// <para>
/// The SqlString is a nonmutable class so it can't have sql parts added
/// to it. Instead this class should be used to generate a new SqlString.
/// The SqlStringBuilder is to SqlString what the StringBuilder is to
/// a String.
/// </para>
/// <para>
/// This is different from the original version of SqlString because this does not
/// hold the sql string in the form of "column1=@column1" instead it uses an array to
/// build the sql statement such that
/// object[0] = "column1="
/// object[1] = ref to column1 parameter
/// </para>
/// <para>
/// What this allows us to do is to delay the generating of the parameter for the sql
/// until the very end - making testing dialect indifferent. Right now all of our test
/// to make sure the correct sql is getting built are specific to MsSql2000Dialect.
/// </para>
/// </remarks>
public class SqlStringBuilder : ISqlStringBuilder
{
// this holds the strings and parameters that make up the full sql statement.
private List<object> sqlParts;
private AddingSqlStringVisitor addingVisitor;
private AddingSqlStringVisitor AddingVisitor
{
get
{
if (addingVisitor == null)
{
addingVisitor = new AddingSqlStringVisitor(this);
}
return addingVisitor;
}
}
/// <summary>
/// Create an empty StringBuilder with the default capacity.
/// </summary>
public SqlStringBuilder() : this(16)
{
}
/// <summary>
/// Create a StringBuilder with a specific capacity.
/// </summary>
/// <param name="partsCapacity">The number of parts expected.</param>
public SqlStringBuilder(int partsCapacity)
{
sqlParts = new List<object>(partsCapacity);
}
/// <summary>
/// Create a StringBuilder to modify the SqlString
/// </summary>
/// <param name="sqlString">The SqlString to modify.</param>
public SqlStringBuilder(SqlString sqlString)
{
sqlParts = new List<object>(sqlString.Count);
Add(sqlString);
}
/// <summary>
/// Adds the preformatted sql to the SqlString that is being built.
/// </summary>
/// <param name="sql">The string to add.</param>
/// <returns>This SqlStringBuilder</returns>
public SqlStringBuilder Add(string sql)
{
if (StringHelper.IsNotEmpty(sql))
{
sqlParts.Add(sql);
}
return this;
}
/// <summary>
/// Adds the Parameter to the SqlString that is being built.
/// The correct operator should be added before the Add(Parameter) is called
/// because there will be no operator ( such as "=" ) placed between the last Add call
/// and this Add call.
/// </summary>
/// <param name="parameter">The Parameter to add.</param>
/// <returns>This SqlStringBuilder</returns>
public SqlStringBuilder Add(Parameter parameter)
{
if (parameter != null)
{
sqlParts.Add(parameter);
}
return this;
}
public SqlStringBuilder AddParameter()
{
return Add(Parameter.Placeholder);
}
/// <summary>
/// Attempts to discover what type of object this is and calls the appropriate
/// method.
/// </summary>
/// <param name="part">The part to add when it is not known if it is a Parameter, String, or SqlString.</param>
/// <returns>This SqlStringBuilder.</returns>
/// <exception cref="ArgumentException">Thrown when the part is not a Parameter, String, or SqlString.</exception>
public SqlStringBuilder AddObject(object part)
{
if (part == null)
{
return this;
}
Parameter paramPart = part as Parameter;
if (paramPart != null)
{
return Add(paramPart); // EARLY EXIT
}
string stringPart = part as string;
if (StringHelper.IsNotEmpty(stringPart))
{
return Add(stringPart);
}
SqlString sqlPart = part as SqlString;
if (SqlStringHelper.IsNotEmpty(sqlPart))
{
return Add(sqlPart);
}
// remarks - we should not get to here - this is a problem with the
// SQL being generated.
if (paramPart == null && stringPart == null && sqlPart == null)
{
throw new ArgumentException("Part was not a Parameter, String, or SqlString.");
}
return this;
}
/// <summary>
/// Adds an existing SqlString to this SqlStringBuilder. It does NOT add any
/// prefix, postfix, operator, or wrap around this. It is equivalent to just
/// adding a string.
/// </summary>
/// <param name="sqlString">The SqlString to add to this SqlStringBuilder</param>
/// <returns>This SqlStringBuilder</returns>
public SqlStringBuilder Add(SqlString sqlString)
{
sqlString?.Visit(AddingVisitor);
return this;
}
/// <summary>
/// Adds an existing SqlString to this SqlStringBuilder
/// </summary>
/// <param name="sqlString">The SqlString to add to this SqlStringBuilder</param>
/// <param name="prefix">String to put at the beginning of the combined SqlString.</param>
/// <param name="op">How these Statements should be junctioned "AND" or "OR"</param>
/// <param name="postfix">String to put at the end of the combined SqlString.</param>
/// <returns>This SqlStringBuilder</returns>
/// <remarks>
/// This calls the overloaded Add method with an array of SqlStrings and wrapStatement=false
/// so it will not be wrapped with a "(" and ")"
/// </remarks>
public SqlStringBuilder Add(SqlString sqlString, string prefix, string op, string postfix)
{
return Add(new SqlString[] {sqlString}, prefix, op, postfix, false);
}
/// <summary>
/// Adds existing SqlStrings to this SqlStringBuilder
/// </summary>
/// <param name="sqlStrings">The SqlStrings to combine.</param>
/// <param name="prefix">String to put at the beginning of the combined SqlString.</param>
/// <param name="op">How these SqlStrings should be junctioned "AND" or "OR"</param>
/// <param name="postfix">String to put at the end of the combined SqlStrings.</param>
/// <returns>This SqlStringBuilder</returns>
/// <remarks>This calls the overloaded Add method with wrapStatement=true</remarks>
public SqlStringBuilder Add(SqlString[] sqlStrings, string prefix, string op, string postfix)
{
return Add(sqlStrings, prefix, op, postfix, true);
}
/// <summary>
/// Adds existing SqlStrings to this SqlStringBuilder
/// </summary>
/// <param name="sqlStrings">The SqlStrings to combine.</param>
/// <param name="prefix">String to put at the beginning of the combined SqlStrings.</param>
/// <param name="op">How these SqlStrings should be junctioned "AND" or "OR"</param>
/// <param name="postfix">String to put at the end of the combined SqlStrings.</param>
/// <param name="wrapStatement">Wrap each SqlStrings with "(" and ")"</param>
/// <returns>This SqlStringBuilder</returns>
public SqlStringBuilder Add(SqlString[] sqlStrings, string prefix, string op, string postfix, bool wrapStatement)
{
if (StringHelper.IsNotEmpty(prefix))
{
sqlParts.Add(prefix);
}
bool opNeeded = false;
foreach (SqlString sqlString in sqlStrings)
{
if (sqlString == null || sqlString.Count == 0)
{
continue;
}
if (opNeeded)
{
sqlParts.Add(" " + op + " ");
}
opNeeded = true;
if (wrapStatement)
{
sqlParts.Add("(");
}
Add(sqlString);
if (wrapStatement)
{
sqlParts.Add(")");
}
}
if (postfix != null)
{
sqlParts.Add(postfix);
}
return this;
}
/// <summary>
/// Gets the number of SqlParts in this SqlStringBuilder.
/// </summary>
/// <returns>
/// The number of SqlParts in this SqlStringBuilder.
/// </returns>
public int Count
{
get { return sqlParts.Count; }
}
/// <summary>
/// Gets or Sets the element at the index
/// </summary>
/// <value>Returns a string or Parameter.</value>
/// <remarks></remarks>
public object this[int index]
{
get { return sqlParts[index]; }
set { sqlParts[index] = value; }
}
/// <summary>
/// Insert a string containing sql into the SqlStringBuilder at the specified index.
/// </summary>
/// <param name="index">The zero-based index at which the sql should be inserted.</param>
/// <param name="sql">The string containing sql to insert.</param>
/// <returns>This SqlStringBuilder</returns>
public SqlStringBuilder Insert(int index, string sql)
{
sqlParts.Insert(index, sql);
return this;
}
/// <summary>
/// Insert a Parameter into the SqlStringBuilder at the specified index.
/// </summary>
/// <param name="index">The zero-based index at which the Parameter should be inserted.</param>
/// <param name="param">The Parameter to insert.</param>
/// <returns>This SqlStringBuilder</returns>
public SqlStringBuilder Insert(int index, Parameter param)
{
sqlParts.Insert(index, param);
return this;
}
/// <summary>
/// Removes the string or Parameter at the specified index.
/// </summary>
/// <param name="index">The zero-based index of the item to remove.</param>
/// <returns>This SqlStringBuilder</returns>
public SqlStringBuilder RemoveAt(int index)
{
sqlParts.RemoveAt(index);
return this;
}
/// <summary>
/// Converts the mutable SqlStringBuilder into the immutable SqlString.
/// </summary>
/// <returns>The SqlString that was built.</returns>
public SqlString ToSqlString()
{
return new SqlString(sqlParts);
}
public override string ToString()
{
return ToSqlString().ToString();
}
private class AddingSqlStringVisitor : ISqlStringVisitor
{
private SqlStringBuilder parent;
public AddingSqlStringVisitor(SqlStringBuilder parent)
{
this.parent = parent;
}
public void String(string text)
{
parent.Add(text);
}
public void String(SqlString sqlString)
{
parent.Add(sqlString);
}
public void Parameter(Parameter parameter)
{
parent.Add(parameter);
}
}
public void Clear()
{
sqlParts.Clear();
}
}
}