-
-
Notifications
You must be signed in to change notification settings - Fork 524
Expand file tree
/
Copy pathCompiler.Conditions.cs
More file actions
267 lines (194 loc) · 8 KB
/
Compiler.Conditions.cs
File metadata and controls
267 lines (194 loc) · 8 KB
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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
namespace SqlKata.Compilers
{
public partial class Compiler
{
protected virtual MethodInfo FindCompilerMethodInfo(Type clauseType, string methodName)
{
return _compileConditionMethodsProvider.GetMethodInfo(clauseType, methodName);
}
protected virtual string CompileCondition(SqlResult ctx, AbstractCondition clause)
{
var clauseType = clause.GetType();
var name = clauseType.Name;
name = name.Substring(0, name.IndexOf("Condition"));
var methodName = "Compile" + name + "Condition";
var methodInfo = FindCompilerMethodInfo(clauseType, methodName);
try
{
var result = methodInfo.Invoke(this, new object[] {
ctx,
clause
});
return result as string;
}
catch (Exception ex)
{
throw new Exception($"Failed to invoke '{methodName}'", ex);
}
}
protected virtual string CompileConditions(SqlResult ctx, List<AbstractCondition> conditions)
{
var result = new StringBuilder();
for (var i = 0; i < conditions.Count; i++)
{
var compiled = CompileCondition(ctx, conditions[i]);
if (string.IsNullOrEmpty(compiled))
{
continue;
}
var boolOperator = i == 0 ? "" : (conditions[i].IsOr ? "OR " : "AND ");
result.Append(boolOperator + compiled + " ");
}
return result.ToString().Trim();
}
protected virtual string CompileRawCondition(SqlResult ctx, RawCondition x)
{
ctx.Bindings.AddRange(x.Bindings);
return WrapIdentifiers(x.Expression);
}
protected virtual string CompileQueryCondition<T>(SqlResult ctx, QueryCondition<T> x) where T : BaseQuery<T>
{
var subCtx = CompileSelectQuery(x.Query);
ctx.Bindings.AddRange(subCtx.Bindings);
return Wrap(x.Column) + " " + checkOperator(x.Operator) + " (" + subCtx.RawSql + ")";
}
protected virtual string CompileSubQueryCondition<T>(SqlResult ctx, SubQueryCondition<T> x) where T : BaseQuery<T>
{
var subCtx = CompileSelectQuery(x.Query);
ctx.Bindings.AddRange(subCtx.Bindings);
return "(" + subCtx.RawSql + ") " + checkOperator(x.Operator) + " " + Parameter(ctx, x.Value);
}
protected virtual string CompileBasicCondition(SqlResult ctx, BasicCondition x)
{
var sql = $"{Wrap(x.Column)} {checkOperator(x.Operator)} {Parameter(ctx, x.Value)}";
if (x.IsNot)
{
return $"NOT ({sql})";
}
return sql;
}
protected virtual string CompileBasicStringCondition(SqlResult ctx, BasicStringCondition x)
{
var column = Wrap(x.Column);
var value = Resolve(ctx, x.Value) as string;
if (value == null)
{
throw new ArgumentException("Expecting a non nullable string");
}
var method = x.Operator;
if (new[] { "starts", "ends", "contains", "like" }.Contains(x.Operator))
{
method = "LIKE";
switch (x.Operator)
{
case "starts":
value = $"{value}%";
break;
case "ends":
value = $"%{value}";
break;
case "contains":
value = $"%{value}%";
break;
}
}
string sql;
if (!x.CaseSensitive)
{
column = CompileLower(column);
value = value.ToLowerInvariant();
}
if (x.Value is UnsafeLiteral)
{
sql = $"{column} {checkOperator(method)} {value}";
}
else
{
sql = $"{column} {checkOperator(method)} {Parameter(ctx, value)}";
}
if (!string.IsNullOrEmpty(x.EscapeCharacter))
{
sql = $"{sql} ESCAPE '{x.EscapeCharacter}'";
}
return x.IsNot ? $"NOT ({sql})" : sql;
}
protected virtual string CompileBasicDateCondition(SqlResult ctx, BasicDateCondition x)
{
var column = Wrap(x.Column);
var op = checkOperator(x.Operator);
var sql = $"{x.Part.ToUpperInvariant()}({column}) {op} {Parameter(ctx, x.Value)}";
return x.IsNot ? $"NOT ({sql})" : sql;
}
protected virtual string CompileNestedCondition<Q>(SqlResult ctx, NestedCondition<Q> x) where Q : BaseQuery<Q>
{
if (!(x.Query.HasComponent("where", EngineCode) || x.Query.HasComponent("having", EngineCode)))
{
return null;
}
var clause = x.Query.HasComponent("where", EngineCode) ? "where" : "having";
var clauses = x.Query.GetComponents<AbstractCondition>(clause, EngineCode);
var sql = CompileConditions(ctx, clauses);
return x.IsNot ? $"NOT ({sql})" : $"({sql})";
}
protected string CompileTwoColumnsCondition(SqlResult ctx, TwoColumnsCondition clause)
{
var op = clause.IsNot ? "NOT " : "";
return $"{op}{Wrap(clause.First)} {checkOperator(clause.Operator)} {Wrap(clause.Second)}";
}
protected virtual string CompileBetweenCondition<T>(SqlResult ctx, BetweenCondition<T> item)
{
var between = item.IsNot ? "NOT BETWEEN" : "BETWEEN";
var lower = Parameter(ctx, item.Lower);
var higher = Parameter(ctx, item.Higher);
return Wrap(item.Column) + $" {between} {lower} AND {higher}";
}
protected virtual string CompileInCondition<T>(SqlResult ctx, InCondition<T> item)
{
var column = Wrap(item.Column);
if (!item.Values.Any())
{
return item.IsNot ? $"1 = 1 /* NOT IN [empty list] */" : "1 = 0 /* IN [empty list] */";
}
var inOperator = item.IsNot ? "NOT IN" : "IN";
var values = Parameterize(ctx, item.Values);
return column + $" {inOperator} ({values})";
}
protected virtual string CompileInQueryCondition(SqlResult ctx, InQueryCondition item)
{
var subCtx = CompileSelectQuery(item.Query);
ctx.Bindings.AddRange(subCtx.Bindings);
var inOperator = item.IsNot ? "NOT IN" : "IN";
return Wrap(item.Column) + $" {inOperator} ({subCtx.RawSql})";
}
protected virtual string CompileNullCondition(SqlResult ctx, NullCondition item)
{
var op = item.IsNot ? "IS NOT NULL" : "IS NULL";
return Wrap(item.Column) + " " + op;
}
protected virtual string CompileBooleanCondition(SqlResult ctx, BooleanCondition item)
{
var column = Wrap(item.Column);
var value = item.Value ? CompileTrue() : CompileFalse();
var op = item.IsNot ? "!=" : "=";
return $"{column} {op} {value}";
}
protected virtual string CompileExistsCondition(SqlResult ctx, ExistsCondition item)
{
var op = item.IsNot ? "NOT EXISTS" : "EXISTS";
// remove unneeded components
var query = item.Query.Clone();
if (OmitSelectInsideExists)
{
query.ClearComponent("select").SelectRaw("1");
}
var subCtx = CompileSelectQuery(query);
ctx.Bindings.AddRange(subCtx.Bindings);
return $"{op} ({subCtx.RawSql})";
}
}
}