-
Notifications
You must be signed in to change notification settings - Fork 935
/
Copy pathTemplate.cs
332 lines (303 loc) · 8.91 KB
/
Template.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
using System;
using System.Collections;
using System.Text;
using NHibernate.Dialect.Function;
using NHibernate.Hql;
using NHibernate.Util;
using System.Collections.Generic;
namespace NHibernate.SqlCommand
{
public static class Template
{
private static readonly HashSet<string> Keywords = new HashSet<string>();
private static readonly HashSet<string> BeforeTableKeywords = new HashSet<string>();
private static readonly HashSet<string> FunctionKeywords = new HashSet<string>();
static Template()
{
Keywords.Add("and");
Keywords.Add("or");
Keywords.Add("not");
Keywords.Add("like");
Keywords.Add("is");
Keywords.Add("in");
Keywords.Add("between");
Keywords.Add("null");
Keywords.Add("select");
Keywords.Add("distinct");
Keywords.Add("from");
Keywords.Add("join");
Keywords.Add("inner");
Keywords.Add("outer");
Keywords.Add("left");
Keywords.Add("right");
Keywords.Add("on");
Keywords.Add("where");
Keywords.Add("having");
Keywords.Add("group");
Keywords.Add("order");
Keywords.Add("by");
Keywords.Add("desc");
Keywords.Add("asc");
Keywords.Add("limit");
Keywords.Add("any");
Keywords.Add("some");
Keywords.Add("exists");
Keywords.Add("all");
BeforeTableKeywords.Add("from");
BeforeTableKeywords.Add("join");
FunctionKeywords.Add("as");
FunctionKeywords.Add("leading");
FunctionKeywords.Add("trailing");
FunctionKeywords.Add("from");
FunctionKeywords.Add("case");
FunctionKeywords.Add("when");
FunctionKeywords.Add("then");
FunctionKeywords.Add("else");
FunctionKeywords.Add("end");
}
public static readonly string Placeholder = "$PlaceHolder$";
public static string RenderWhereStringTemplate(string sqlWhereString, Dialect.Dialect dialect,
SQLFunctionRegistry functionRegistry)
{
return RenderWhereStringTemplate(sqlWhereString, Placeholder, dialect, functionRegistry);
}
public static string RenderWhereStringTemplate(string sqlWhereString, string placeholder, Dialect.Dialect dialect,
SQLFunctionRegistry functionRegistry)
{
//TODO: make this a bit nicer
string symbols = new StringBuilder()
.Append("=><!+-*/()',|&`")
.Append(ParserHelper.Whitespace)
.Append(dialect.OpenQuote)
.Append(dialect.CloseQuote)
.ToString();
StringTokenizer tokens = new StringTokenizer(sqlWhereString, symbols, true);
StringBuilder result = new StringBuilder();
bool quoted = false;
bool quotedIdentifier = false;
bool beforeTable = false;
bool inFromClause = false;
bool afterFromTable = false;
using (var tokensEnum = tokens.GetEnumerator())
{
var hasMore = tokensEnum.MoveNext();
var nextToken = hasMore ? tokensEnum.Current : null;
var lastToken = string.Empty;
while (hasMore)
{
var token = nextToken;
var lcToken = token.ToLowerInvariant();
hasMore = tokensEnum.MoveNext();
nextToken = hasMore ? tokensEnum.Current : null;
var isQuoteCharacter = false;
if (!quotedIdentifier && "'".Equals(token))
{
quoted = !quoted;
isQuoteCharacter = true;
}
if (!quoted)
{
bool isOpenQuote;
if ("`".Equals(token))
{
isOpenQuote = !quotedIdentifier;
token = lcToken = isOpenQuote ? dialect.OpenQuote.ToString() : dialect.CloseQuote.ToString();
quotedIdentifier = isOpenQuote;
isQuoteCharacter = true;
}
else if (!quotedIdentifier && (dialect.OpenQuote == token[0]))
{
isOpenQuote = true;
quotedIdentifier = true;
isQuoteCharacter = true;
}
else if (quotedIdentifier && (dialect.CloseQuote == token[0]))
{
quotedIdentifier = false;
isQuoteCharacter = true;
isOpenQuote = false;
}
else
{
isOpenQuote = false;
}
if (isOpenQuote && !inFromClause && !lastToken.EndsWith("."))
{
result.Append(placeholder).Append('.');
}
}
var quotedOrWhitespace = quoted ||
quotedIdentifier ||
isQuoteCharacter ||
char.IsWhiteSpace(token[0]);
if (quotedOrWhitespace)
{
result.Append(token);
}
else if (beforeTable)
{
result.Append(token);
beforeTable = false;
afterFromTable = true;
}
else if (afterFromTable)
{
if (!"as".Equals(lcToken))
afterFromTable = false;
result.Append(token);
}
else if (IsNamedParameter(token))
{
result.Append(token);
}
else if (
IsIdentifier(token, dialect) &&
!IsFunctionOrKeyword(lcToken, nextToken, dialect, functionRegistry)
)
{
result.Append(placeholder)
.Append('.')
.Append(token);
}
else
{
if (BeforeTableKeywords.Contains(lcToken))
{
beforeTable = true;
inFromClause = true;
}
else if (inFromClause && ",".Equals(lcToken))
{
beforeTable = true;
}
result.Append(token);
}
if ( //Yuck:
inFromClause &&
Keywords.Contains(lcToken) && //"as" is not in Keywords
!BeforeTableKeywords.Contains(lcToken)
)
{
inFromClause = false;
}
lastToken = token;
}
}
return result.ToString();
}
public static string RenderOrderByStringTemplate(string sqlOrderByString, Dialect.Dialect dialect,
SQLFunctionRegistry functionRegistry)
{
//TODO: make this a bit nicer
string symbols = new StringBuilder()
.Append("=><!+-*/()',|&`")
.Append(ParserHelper.Whitespace)
.Append(dialect.OpenQuote)
.Append(dialect.CloseQuote)
.ToString();
StringTokenizer tokens = new StringTokenizer(sqlOrderByString, symbols, true);
StringBuilder result = new StringBuilder();
bool quoted = false;
bool quotedIdentifier = false;
using (var tokensEnum = tokens.GetEnumerator())
{
var hasMore = tokensEnum.MoveNext();
var nextToken = hasMore ? tokensEnum.Current : null;
while (hasMore)
{
var token = nextToken;
var lcToken = token.ToLowerInvariant();
hasMore = tokensEnum.MoveNext();
nextToken = hasMore ? tokensEnum.Current : null;
var isQuoteCharacter = false;
if (!quotedIdentifier && "'".Equals(token))
{
quoted = !quoted;
isQuoteCharacter = true;
}
if (!quoted)
{
bool isOpenQuote;
if ("`".Equals(token))
{
isOpenQuote = !quotedIdentifier;
token = lcToken = isOpenQuote ? dialect.OpenQuote.ToString() : dialect.CloseQuote.ToString();
quotedIdentifier = isOpenQuote;
isQuoteCharacter = true;
}
else if (!quotedIdentifier && (dialect.OpenQuote == token[0]))
{
isOpenQuote = true;
quotedIdentifier = true;
isQuoteCharacter = true;
}
else if (quotedIdentifier && (dialect.CloseQuote == token[0]))
{
quotedIdentifier = false;
isQuoteCharacter = true;
isOpenQuote = false;
}
else
{
isOpenQuote = false;
}
if (isOpenQuote)
{
result.Append(Placeholder).Append('.');
}
}
var quotedOrWhitespace = quoted ||
quotedIdentifier ||
isQuoteCharacter ||
char.IsWhiteSpace(token[0]);
if (quotedOrWhitespace)
{
result.Append(token);
}
else if (
IsIdentifier(token, dialect) &&
!IsFunctionOrKeyword(lcToken, nextToken, dialect, functionRegistry)
)
{
result.Append(Placeholder)
.Append('.')
.Append(token);
}
else
{
result.Append(token);
}
}
}
return result.ToString();
}
internal static string ReplacePlaceholder(string template, string alias)
{
// We have to remove the dot when the alias is empty in order to avoid generating an invalid sql statement.
// Alias will be empty for DML statements that do not support aliases.
var toReplace = !string.IsNullOrEmpty(alias) ? Placeholder : Placeholder + StringHelper.Dot;
return template?.Replace(toReplace, alias);
}
private static bool IsNamedParameter(string token)
{
return token.StartsWith(':');
}
private static bool IsFunctionOrKeyword(string lcToken, string nextToken, Dialect.Dialect dialect,
SQLFunctionRegistry functionRegistry)
{
return "(".Equals(nextToken) ||
Keywords.Contains(lcToken) ||
functionRegistry.HasFunction(lcToken) ||
dialect.IsKeyword(lcToken) ||
dialect.IsKnownToken(lcToken, nextToken) ||
FunctionKeywords.Contains(lcToken);
}
private static bool IsIdentifier(string token, Dialect.Dialect dialect)
{
return token[0] == '`' || ( //allow any identifier quoted with backtick
char.IsLetter(token[0]) && //only recognizes identifiers beginning with a letter
token.IndexOf('.') < 0
);
}
}
}