forked from ThatRendle/Simple.Data
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCommandBuilder.cs
More file actions
328 lines (294 loc) · 13.5 KB
/
CommandBuilder.cs
File metadata and controls
328 lines (294 loc) · 13.5 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
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
using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading;
using Simple.Data.Ado.Schema;
namespace Simple.Data.Ado
{
public class CommandBuilder : ICommandBuilder
{
private int _number;
private Func<IDbCommand, IDbParameterFactory> _getParameterFactory;
private readonly DatabaseSchema _schema;
private readonly ISchemaProvider _schemaProvider;
private readonly Dictionary<ParameterTemplate, object> _parameters = new Dictionary<ParameterTemplate, object>();
private readonly StringBuilder _text;
private readonly string _parameterSuffix;
private readonly ProviderHelper _customInterfaceProvider;
public string Joins { get; set; }
public CommandBuilder(DatabaseSchema schema) : this(schema, -1)
{
}
public CommandBuilder(DatabaseSchema schema, int bulkIndex)
{
_text = new StringBuilder();
_schema = schema;
_schemaProvider = schema.SchemaProvider;
_customInterfaceProvider = schema.ProviderHelper;
_parameterSuffix = (bulkIndex >= 0) ? "_c" + bulkIndex : string.Empty;
}
public CommandBuilder(string text, DatabaseSchema schema, int bulkIndex)
{
_text = new StringBuilder(text);
_schema = schema;
_schemaProvider = schema.SchemaProvider;
_customInterfaceProvider = schema.ProviderHelper;
_parameterSuffix = (bulkIndex >= 0) ? "_c" + bulkIndex : string.Empty;
}
public CommandBuilder(string text, DatabaseSchema schema, IEnumerable<KeyValuePair<ParameterTemplate, Object>> parameters)
: this(text, schema, -1)
{
foreach (var kvp in parameters)
{
_parameters.Add(kvp.Key, kvp.Value);
}
}
public ParameterTemplate AddParameter(object value)
{
string name = _schemaProvider.NameParameter("p" + Interlocked.Increment(ref _number) + _parameterSuffix);
var parameterTemplate = new ParameterTemplate(name, value);
_parameters.Add(parameterTemplate, value);
return parameterTemplate;
}
public ParameterTemplate AddParameter(object value, Column column)
{
string name = _schemaProvider.NameParameter("p" + Interlocked.Increment(ref _number) + _parameterSuffix);
var parameterTemplate = new ParameterTemplate(name, column);
_parameters.Add(parameterTemplate, value);
return parameterTemplate;
}
public ParameterTemplate AddParameter(string name, DbType dbType, object value)
{
name = _schemaProvider.NameParameter(name + _parameterSuffix);
var parameterTemplate = new ParameterTemplate(name, dbType, 0);
_parameters.Add(parameterTemplate, value);
return parameterTemplate;
}
public void Append(string text)
{
_text.Append(text);
}
public void SetText(string text)
{
_text.Clear();
_text.Append(text);
}
public override string ToString()
{
return _text.ToString();
}
public IEnumerable<KeyValuePair<ParameterTemplate, object>> Parameters
{
get { return _parameters.AsEnumerable(); }
}
public string Text
{
get { return _text.ToString(); }
}
public IDbCommand GetCommand(IDbConnection connection, AdoOptions options)
{
var command = connection.CreateCommand(options);
command.CommandText = Text;
SetParameters(command, string.Empty);
if (options != null)
{
command.CommandTimeout = options.CommandTimeout;
}
return command;
}
public IDbCommand GetRepeatableCommand(IDbConnection connection, AdoOptions options)
{
var command = connection.CreateCommand(options);
command.CommandText = Text;
var parameterFactory = CreateParameterFactory(command);
foreach (var parameter in _parameters.Keys)
{
command.Parameters.Add(parameterFactory.CreateParameter(parameter.Name, parameter.Column));
}
if (options != null)
{
command.CommandTimeout = options.CommandTimeout;
}
return command;
}
private IDbParameterFactory CreateParameterFactory(IDbCommand command)
{
CreateGetParameterFactoryFunc();
return _getParameterFactory(command);
}
private Func<IDbCommand, IDbParameterFactory> CreateGetParameterFactoryFunc()
{
if (_getParameterFactory == null)
{
var customParameterFactory =
_customInterfaceProvider.GetCustomProvider<IDbParameterFactory>(_schemaProvider);
if (customParameterFactory != null)
{
_getParameterFactory = _ => customParameterFactory;
}
else
{
_getParameterFactory = c => new GenericDbParameterFactory(c);
}
}
return _getParameterFactory;
}
public CommandTemplate GetCommandTemplate(Table table)
{
var index = table.Columns.Select((c, i) => Tuple.Create(c, i)).ToDictionary(t => t.Item1.ActualName, t => t.Item2);
return new CommandTemplate(CreateGetParameterFactoryFunc(), _text.ToString(), _parameters.Keys.ToArray(), new Dictionary<string, int>(index, HomogenizedEqualityComparer.DefaultInstance));
}
private void SetParameters(IDbCommand command, string suffix)
{
SetParameters(command, _parameters);
}
private void SetParameters(IDbCommand command, IEnumerable<KeyValuePair<ParameterTemplate, object>> parameters)
{
var parameterFactory = CreateParameterFactory(command);
SetParameters(parameterFactory, command, parameters);
}
private void SetParameters(IDbParameterFactory parameterFactory, IDbCommand command, IEnumerable<KeyValuePair<ParameterTemplate, object>> parameters)
{
var parameterList = parameters.ToList();
if (parameterList.Any(kvp => kvp.Value is IRange) ||
parameterList.Any(kvp => kvp.Value is IEnumerable && !(kvp.Value is string)))
{
foreach (var pair in parameterList)
{
foreach (var parameter in CreateParameterComplex(parameterFactory, pair.Key, pair.Value, command))
{
command.Parameters.Add(parameter);
}
}
}
else
{
foreach (var pair in parameterList)
{
command.Parameters.Add(CreateSingleParameter(parameterFactory, pair.Value, pair.Key));
}
}
}
private IEnumerable<IDbDataParameter> CreateParameterComplex(IDbParameterFactory parameterFactory, ParameterTemplate template, object value, IDbCommand command)
{
if (template.Column != null && template.Column.IsBinary)
{
yield return CreateSingleParameter(parameterFactory, value, template.Name, template.Column);
}
else
{
var str = value as string;
if (str != null)
{
yield return CreateSingleParameter(parameterFactory, value, template.Name, template.Column);
}
else
{
var range = value as IRange;
if (range != null)
{
yield return
CreateSingleParameter(parameterFactory, range.Start, template.Name + "_start", template.Column);
yield return CreateSingleParameter(parameterFactory, range.End, template.Name + "_end", template.Column);
SetBetweenInCommandText(command, template.Name);
}
else
{
var list = value as IEnumerable;
if (list != null)
{
var builder = new StringBuilder();
var array = list.Cast<object>().ToArray();
for (int i = 0; i < array.Length; i++)
{
builder.AppendFormat(",{0}_{1}", template.Name, i);
yield return
CreateSingleParameter(parameterFactory, array[i], template.Name + "_" + i, template.Column);
}
if (command.CommandText.Contains("!= " + template.Name))
{
command.CommandText = command.CommandText.Replace("!= " + template.Name,
_schema.Operators.NotIn + " (" +
builder.ToString().Substring(1) + ")");
}
else
{
command.CommandText = command.CommandText.Replace("= " + template.Name,
_schema.Operators.In + " (" +
builder.ToString().Substring(1) +
")");
}
}
else
{
yield return CreateSingleParameter(parameterFactory, value, template);
}
}
}
}
}
public void SetBetweenInCommandText(IDbCommand command, string name)
{
if (command.CommandText.Contains("!= " + name))
{
command.CommandText = command.CommandText.Replace("!= " + name,
string.Format("{0} {1}_start AND {1}_end", _schema.Operators.NotBetween, name));
}
else
{
command.CommandText = command.CommandText.Replace("= " + name,
string.Format("{0} {1}_start AND {1}_end", _schema.Operators.Between, name));
}
}
private static IDbDataParameter CreateSingleParameter(IDbParameterFactory parameterFactory, object value, ParameterTemplate template)
{
if (template.Column != null)
{
return CreateSingleParameter(parameterFactory, value, template.Name, template.Column);
}
var parameter = default(IDbDataParameter);
if (template.Type == ParameterType.NameOnly)
{
parameter = parameterFactory.CreateParameter(template.Name);
}
else
{
parameter = parameterFactory.CreateParameter(template.Name, template.DbType, template.MaxLength);
}
parameter.Value = CommandHelper.FixObjectType(value);
return parameter;
}
private static IDbDataParameter CreateSingleParameter(IDbParameterFactory parameterFactory, object value, string name, Column column)
{
var parameter = parameterFactory.CreateParameter(name, column);
parameter.Value = CommandHelper.FixObjectType(value);
return parameter;
}
internal IDbCommand CreateCommand(IDbParameterFactory parameterFactory, ICommandBuilder[] commandBuilders, IDbConnection connection, AdoOptions options)
{
var command = connection.CreateCommand(options);
parameterFactory = parameterFactory ?? new GenericDbParameterFactory(command);
for (int i = 0; i < commandBuilders.Length; i++)
{
if (!string.IsNullOrWhiteSpace(command.CommandText)) command.CommandText += "; ";
command.CommandText += commandBuilders[i].Text;
SetParameters(parameterFactory, command, commandBuilders[i].Parameters);
}
return command;
}
internal IDbCommand CreateCommand(ICommandBuilder[] commandBuilders, IDbConnection connection, AdoOptions options)
{
var command = connection.CreateCommand(options);
for (int i = 0; i < commandBuilders.Length; i++)
{
if (!string.IsNullOrWhiteSpace(command.CommandText)) command.CommandText += "; ";
command.CommandText += commandBuilders[i].Text;
SetParameters(command, commandBuilders[i].Parameters);
}
return command;
}
}
}