-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathSqlBuilder.cs
More file actions
579 lines (493 loc) · 21.5 KB
/
SqlBuilder.cs
File metadata and controls
579 lines (493 loc) · 21.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
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Reflection;
using System.Text;
using Humanizer;
using Poco.Sql.NetCore.Exceptions;
using Poco.Sql.NetCore.Helpers;
using Poco.Sql.NetCore.Interfaces;
namespace Poco.Sql.NetCore
{
/// <summary>
/// This class is responsible for building the SQL statements
/// </summary>
public class SqlBuilder
{
private static Dictionary<string, string> _cachedQueries = new Dictionary<string, string>(); //TODO: implement caching
private List<SqlBuilderTask> _tasks;
private object _obj;
private SqlBuilderTask currentTask
{
get { return _tasks[_tasks.Count - 1]; }
}
public string Result
{
get
{
return getSqlStatement();
}
}
public SqlBuilder(object obj)
{
_obj = obj;
_tasks = new List<SqlBuilderTask>();
}
private void createTask(SqlBuilderTask task)
{
_tasks.Add(task);
}
private void createTask(SqlBuilderTask.QueryTypeEnum queryType)
{
var task = new SqlBuilderTask()
{
QueryType = queryType
};
createTask(task);
}
public SqlBuilder NamedQuery(string name)
{
createTask(new SqlBuilderTask()
{
QueryType = SqlBuilderTask.QueryTypeEnum.QueryByName,
QueryName = name
});
//var map = getMapping(_obj);
//if (map != null)
// _sql = map.GetQueryByName(name);
return this;
}
#region Select
public SqlBuilder Select()
{
createTask(SqlBuilderTask.QueryTypeEnum.Select);
return this;
}
public SqlBuilder Select(long id)
{
createTask(new SqlBuilderTask()
{
QueryType = SqlBuilderTask.QueryTypeEnum.Select,
PrimaryKeyValue = id
});
//createTask(SqlBuilderTask.QueryTypeEnum.Select, primaryKey + " = " + id);
//string primaryKey = getPrimaryKey(_obj);
//return buildSqlStatement(_obj, null, null, null, null).Where(primaryKey + " = " + id);
return this;
}
public SqlBuilder Select<T>(PocoSqlMapping<T> map)
{
createTask(new SqlBuilderTask()
{
QueryType = SqlBuilderTask.QueryTypeEnum.Select,
Map = map
});
return this;
}
public SqlBuilder Select(string tableName)
{
createTask(new SqlBuilderTask()
{
QueryType = SqlBuilderTask.QueryTypeEnum.Select,
TableName = tableName
});
return this;
}
#endregion
public SqlBuilder SelectScopeIdentity()
{
createTask(SqlBuilderTask.QueryTypeEnum.SelectScopeIdentity);
return this;
}
public SqlBuilder SelectIdentity()
{
createTask(SqlBuilderTask.QueryTypeEnum.SelectIdentity);
return this;
}
public SqlBuilder FullGraph()
{
throw new NotImplementedException();
//PropertyInfo[] propertyInfos = _obj.GetType().GetProperties();
//StringBuilder allFields = new StringBuilder();
//foreach (PropertyInfo propertyInfo in propertyInfos.Where(p => p.PropertyType.FullName.StartsWith("System.Collections")))
//{
// var collectionObject = propertyInfo.GetValue(_obj);
// Type collectionType = collectionObject.GetType().GetGenericArguments()[0];
// object obj = Activator.CreateInstance(collectionType);
// string key = obj.GetType().FullName;
// if (Configuration.HasMap(key))
// {
// var map = Configuration.GetMap(key);;
// var relationship = map.GetRelationship(_obj.GetType().FullName);
// string currentKey = _obj.GetType().FullName;
// var currentMap = Configuration.GetMap(key); ;
// var val = _obj.GetType().GetProperty(currentMap.GetPrimaryKey()).GetValue(_obj);
// string whereConditionStr = relationship.GetForeignKey() + " = " + val;
// SqlBuilder qb = new SqlBuilder(obj);
// string sql = qb.Select().Where(whereConditionStr).ToString();
// _graphSql += Environment.NewLine + sql;
// }
//}
//return this;
}
public SqlBuilder Update()
{
createTask(SqlBuilderTask.QueryTypeEnum.Update);
return this;
}
public SqlBuilder Insert()
{
createTask(SqlBuilderTask.QueryTypeEnum.Insert);
return this;
}
public SqlBuilder Delete()
{
createTask(SqlBuilderTask.QueryTypeEnum.Delete);
return this;
}
public SqlBuilder NoSemicolon()
{
currentTask.EndWithSemicolon = false;
return this;
}
//private SqlBuilder buildSqlStatement(object obj, SqlBuilderTask.QueryTypeEnum queryType, string tableName, bool? fullGraph, bool? pluralizeTableNames, IPocoSqlMapping map)
private SqlBuilder buildSqlStatement(object obj, SqlBuilderTask task)
{
if (task.QueryType == SqlBuilderTask.QueryTypeEnum.SelectScopeIdentity)
{
task.SqlResult = "select scope_identity()";
return this;
}
else if (task.QueryType == SqlBuilderTask.QueryTypeEnum.SelectIdentity)
{
task.SqlResult = "select @@identity";
return this;
}
IPocoSqlMapping map = null;
string tableName = null;
if (Configuration.Comment)
task.SqlResult = getComment(obj);
if (task.Map == null)
map = getMapping(obj);
if (map != null)
{
//
// Custom query
//
var customQuery = getCustomQuery(task.QueryType, map);
if (!String.IsNullOrEmpty(customQuery))
{
task.SqlResult += customQuery;
if (task.SqlResult.EndsWith(";")) currentTask.EndWithSemicolon = false;
return this; // if it's a custom query no other thing needs (the user knew in advanced what he wanted) to be done and the result is returned
}
//
// Stored Procedures mappings
//
var spMappings = map.GetStoredProceduresMappings();
if (spMappings != null)
{
PocoSqlStoredProcedureMap spMap = getStoredProcedureMap(spMappings, task.QueryType);
if (spMap != null)
{
string spName = getStoredProcedureName(spMap, obj, task.QueryType);
task.SqlResult += String.Format("{0}{1}{2}",
spMap.Execution ? "exec " : String.Empty,
spName,
spMap.Parameters ? getQueryFields(task.QueryType, map) : String.Empty);
if (!spMap.Execution && !spMap.Parameters) currentTask.EndWithSemicolon = false;
return this;
}
}
if (task.QueryType != SqlBuilderTask.QueryTypeEnum.Select && map.GetIsVirtual())
throw new CantUpdateVirtualException();
if (String.IsNullOrEmpty(task.TableName))
tableName = map.GetTableName();
}
if (String.IsNullOrEmpty(tableName))
tableName = obj.GetType().Name;
if ((map == null || (map != null && String.IsNullOrEmpty(map.GetTableName()))) && Configuration.IsPluralizeTableNames)
{
tableName = tableName.Pluralize();
}
switch (task.QueryType)
{
case SqlBuilderTask.QueryTypeEnum.Select:
task.SqlResult += String.Format("select {0} from {1}", getQueryFields(SqlBuilderTask.QueryTypeEnum.Select, map), tableName);
break;
case SqlBuilderTask.QueryTypeEnum.Insert:
task.SqlResult += String.Format("insert into {0} {1}", tableName, getQueryFields(SqlBuilderTask.QueryTypeEnum.Insert, map));
break;
case SqlBuilderTask.QueryTypeEnum.Update:
task.SqlResult += String.Format("update {0} set {1}", tableName, getQueryFields(SqlBuilderTask.QueryTypeEnum.Update, map));
break;
case SqlBuilderTask.QueryTypeEnum.Delete:
task.SqlResult += String.Format("delete from {0}", tableName);
string primaryKey = getPrimaryKey(_obj);
string deleteWhereValue = "@" + primaryKey;
if (Configuration.ValuesInQueies)
deleteWhereValue = getPropertyValueAsSql(_obj, primaryKey);
this.Where(String.Format("{0} = {1}", primaryKey, deleteWhereValue));
break;
case SqlBuilderTask.QueryTypeEnum.StoredProcedure:
var queryFields = String.Empty;
var execCmd = String.Empty;
task.SqlResult += String.Format("exec {0} {1}", tableName, getQueryFields(SqlBuilderTask.QueryTypeEnum.StoredProcedure, map));
break;
}
return this;
}
private PocoSqlStoredProcedureMap getStoredProcedureMap(PocoSqlStoredProceduresMapping spMappings, SqlBuilderTask.QueryTypeEnum queryType)
{
switch (queryType)
{
case SqlBuilderTask.QueryTypeEnum.Select:
return spMappings.SelectMap;
case SqlBuilderTask.QueryTypeEnum.Insert:
return spMappings.InsertMap;
case SqlBuilderTask.QueryTypeEnum.Update:
return spMappings.UpdateMap;
case SqlBuilderTask.QueryTypeEnum.Delete:
return spMappings.DeleteMap;
default:
return null;
}
}
private string getComment(object obj)
{
return String.Format(
"/*{0}Poco.Sql{0}Time: {1}{0}Object: {2}{0}*/{0}",
Environment.NewLine,
DateTime.Now,
obj.GetType().FullName
);
}
private string getCustomQuery(SqlBuilderTask.QueryTypeEnum queryType, IPocoSqlMapping map)
{
string customQuery = null;
var customMappings = map.GetCustomMappings();
if (customMappings != null)
{
switch (queryType)
{
case SqlBuilderTask.QueryTypeEnum.Select:
customQuery = customMappings.SelectQuery;
break;
case SqlBuilderTask.QueryTypeEnum.Insert:
customQuery = customMappings.InsertQuery;
break;
case SqlBuilderTask.QueryTypeEnum.Update:
customQuery = customMappings.UpdateQuery;
break;
case SqlBuilderTask.QueryTypeEnum.Delete:
customQuery = customMappings.DeleteQuery;
break;
}
}
return customQuery;
}
private string getQueryFields(SqlBuilderTask.QueryTypeEnum queryType, IPocoSqlMapping map)
{
IEnumerable<PropertyInfo> propertyInfos = _obj.GetType().GetRuntimeProperties();
string primaryKey = null;
if (queryType == SqlBuilderTask.QueryTypeEnum.Update || queryType == SqlBuilderTask.QueryTypeEnum.Insert)
primaryKey = getPrimaryKey(_obj);
StringBuilder allFields = new StringBuilder();
StringBuilder insertValues = new StringBuilder();
foreach (PropertyInfo propertyInfo in propertyInfos.Where(p => p.PropertyType.FullName.StartsWith("System") && !p.PropertyType.FullName.StartsWith("System.Collections"))) // only loop on objects that are not custom class
{
string
dbSelect = null,
fieldName = null,
dbColumnName = null;
if (map != null)
{
PropertyMap propertyMap = map.GetMapping(propertyInfo.Name);
if (propertyMap != null)
{
if (!propertyMap.Ignored)
{
if (!propertyMap.ColumnName.Equals(propertyInfo.Name))
{
if (queryType == SqlBuilderTask.QueryTypeEnum.Select)
dbSelect = String.Format("{0} as {1}",
propertyMap.ColumnName,
propertyInfo.Name);
fieldName = propertyInfo.Name;
dbColumnName = propertyMap.ColumnName;
}
}
else
{
dbSelect = String.Empty;
}
}
}
if (String.IsNullOrEmpty(dbSelect))
dbSelect = fieldName = dbColumnName = propertyInfo.Name;
if (fieldName == primaryKey)
{
if (queryType == SqlBuilderTask.QueryTypeEnum.Update)
{
var property = _obj.GetType().GetRuntimeProperty(dbColumnName);
string val = "@" + primaryKey;
if (Configuration.ValuesInQueies)
property.GetValue(_obj).ToString();
this.Where(primaryKey + " = " + val);
}
if (queryType == SqlBuilderTask.QueryTypeEnum.Update || (queryType == SqlBuilderTask.QueryTypeEnum.Insert && map.GetPrimaryAutoGenerated()))
continue;
}
switch(queryType)
{
case SqlBuilderTask.QueryTypeEnum.Select:
allFields.Append((allFields.Length > 0 && !String.IsNullOrEmpty(fieldName) ? ", " : String.Empty) + dbSelect);
break;
case SqlBuilderTask.QueryTypeEnum.Insert:
if (!String.IsNullOrEmpty(fieldName))
{
string insertVal;
if (Configuration.ValuesInQueies)
insertVal = getPropertyValueAsSql(_obj, fieldName);
else
insertVal = "@" + fieldName;
allFields.Append((allFields.Length > 0 ? ", " : String.Empty) + dbSelect);
insertValues.Append((insertValues.Length > 0 ? ", " : String.Empty) + insertVal);
}
break;
case SqlBuilderTask.QueryTypeEnum.Update:
case SqlBuilderTask.QueryTypeEnum.StoredProcedure:
string updateVal;
if (Configuration.ValuesInQueies)
updateVal = getPropertyValueAsSql(_obj, fieldName, true);
else
updateVal = "@" + fieldName;
allFields.Append((allFields.Length > 0 && !String.IsNullOrEmpty(fieldName) ? ", " : String.Empty) + dbColumnName + " = " + updateVal);
break;
case SqlBuilderTask.QueryTypeEnum.Delete:
break;
}
}
if (queryType == SqlBuilderTask.QueryTypeEnum.Insert)
return String.Format("({0}) values({1})", allFields, insertValues);
else
return allFields.ToString();
}
private string getPropertyValueAsSql(object obj, string propertyName)
{
return getPropertyValueAsSql(obj, propertyName, false);
}
private string getPropertyValueAsSql(object obj, string propertyName, bool cast)
{
PropertyInfo property = obj.GetType().GetRuntimeProperty(propertyName);
string result = property.GetValue(_obj).ToString();
if ((property.PropertyType == typeof(string) || !cast) && property.PropertyType != typeof(int))
result = "'" + result + "'";
else if (property.PropertyType == typeof(DateTime))
result = "cast('" + result + "' as datetime)";
return result;
}
private string getStoredProcedureName(PocoSqlStoredProcedureMap spMap, object obj, SqlBuilderTask.QueryTypeEnum queryType)
{
if (spMap == null) return String.Empty;
if (String.IsNullOrEmpty(spMap.Name))
return String.Format("{0}{1}_{2}", Configuration.StoredProceduresPrefix, obj.GetType().Name, queryType.ToString());
return spMap.Name;
}
private IPocoSqlMapping getMapping(object obj)
{
IPocoSqlMapping map = null;
string key = obj.GetType().FullName;
if (Configuration.HasMap(key))
map = Configuration.GetMap(key);
return map;
}
#region Join
public SqlBuilder Join<T>(Expression<Func<T, bool>> on)
{
return Join<T>(typeof(T).Name, on);
}
public SqlBuilder Join<T>(string tableName, Expression<Func<T, bool>> on)
{
throw new NotImplementedException();
return this;
}
#endregion
#region Where
public SqlBuilder Where(string condition)
{
//TODO: make sure update statements can't have a a where clause
currentTask.WhereStrings.Add(condition);
return this;
}
public SqlBuilder Where<TSource>(Expression<Func<TSource, bool>> condition)
{
//TODO: make sure update statements can't have a a where clause
currentTask.WhereExpressions.Add(condition.Body);
//_where += " where " + ExpressionEvaluator.Eval(condition.Body, Configuration.ValuesInQueies);
return this;
}
#endregion
private string getPrimaryKey(object obj)
{
var map = getMapping(_obj);
if (map != null && !String.IsNullOrEmpty(map.GetPrimaryKey()))
return map.GetPrimaryKey(); // return a mapped primary key
// check by convension
PropertyInfo property = null;
property = obj.GetType().GetRuntimeProperties().Where(p => p.Name.ToLower() == "id").FirstOrDefault();
if (property != null)
return property.Name;
property = obj.GetType().GetRuntimeProperties().Where(p => p.Name.ToLower() == obj.GetType().Name.ToLower() + "id").FirstOrDefault();
if (property != null)
return property.Name;
return null;
}
private void buildWhereCondition(object _obj, SqlBuilderTask task)
{
if (task.WhereStrings.Count > 0)
{
foreach (var whereString in task.WhereStrings)
{
task.WhereCondition += String.IsNullOrEmpty(task.WhereCondition)
? " where (" + whereString + ")"
: " and (" + whereString + ")";
}
}
if (task.WhereExpressions.Count == 0) return;
foreach (var whereExpression in task.WhereExpressions)
{
var expressionStr = ExpressionEvaluator.Eval(whereExpression, Configuration.ValuesInQueies);
task.WhereCondition += String.IsNullOrEmpty(task.WhereCondition)
? " where (" + expressionStr + ")"
: " and (" + expressionStr + ")";
}
}
public override string ToString()
{
return getSqlStatement();
}
public string ToSQL()
{
return getSqlStatement();
}
private string getSqlStatement()
{
if (_tasks.Count == 0) // there must be at least one task
throw new NoSqlBuilderTaskFound();
StringBuilder result = new StringBuilder();
foreach (var task in _tasks)
{
buildSqlStatement(_obj, task);
buildWhereCondition(_obj, task);
var sql = String.Format("{0}{1}{2}",
task.SqlResult,
task.WhereCondition,
task.EndWithSemicolon ? ";" : String.Empty);
result.Append(sql);
}
return result.ToString();
}
}
}