-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathTQueryHelperItem.cs
More file actions
745 lines (685 loc) · 31.4 KB
/
Copy pathTQueryHelperItem.cs
File metadata and controls
745 lines (685 loc) · 31.4 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
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
using Helper.Core.Library.Translator;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;
namespace Helper.Core.Library.QueryHelper
{
internal class TQueryHelperItem<T> where T : class
{
private string queryType = null;
private List<ExpressionKeyValueItem> queryList;
private string queryTableName = null;
private string fromInTableName = null;
private string fromInNewTableName = null;
private int queryTop = 0;
private bool distinct = false;
private Dictionary<string, Type> joinTableDict;
public void Init(int top, bool distinct, string queryType = TQueryHelperTypeEnum.QUERY_TYPE)
{
this.queryType = queryType;
this.queryList = new List<ExpressionKeyValueItem>();
if (queryType == TQueryHelperTypeEnum.QUERY_TYPE)
{
this.queryTableName = TQueryReflectionHelper.GetTableName(typeof(T), true);
}
else
{
this.queryTableName = TQueryReflectionHelper.GetTableName(typeof(T), false);
}
this.queryTop = top;
this.distinct = distinct;
}
#region 增/改/删
public void Insert(Expression<Func<T, object>> lambda = null)
{
this.Init(0, false, TQueryHelperTypeEnum.INSERT_TYPE);
Dictionary<string, string> fieldMapperDict = null;
if (lambda == null)
{
fieldMapperDict = TQueryReflectionHelper.GetPropertyDict(typeof(T));
}
else
{
OperaterTranslator operaterTranslator = new OperaterTranslator();
operaterTranslator.Translate(lambda);
fieldMapperDict = operaterTranslator.MapperDict;
}
int propertyCount = fieldMapperDict.Count;
int propertyIndex = 0;
StringBuilder fieldStringBuilder = new StringBuilder();
StringBuilder paramStringBuilder = new StringBuilder();
foreach (var keyValueItem in fieldMapperDict)
{
fieldStringBuilder.Append(string.Format(TQueryHelperTemplateEnum.FIELD, keyValueItem.Key));
paramStringBuilder.Append("@");
paramStringBuilder.Append(keyValueItem.Value);
if (propertyIndex < propertyCount - 1)
{
fieldStringBuilder.Append(",");
paramStringBuilder.Append(",");
}
propertyIndex++;
}
string querySql = string.Format(TQueryHelperTemplateEnum.INSERT, this.queryTableName, fieldStringBuilder.ToString(), paramStringBuilder.ToString());
this.queryList.Add(new ExpressionKeyValueItem(TQueryHelperTypeEnum.INSERT, querySql));
}
public void Update(Expression<Func<T, object>> lambda = null)
{
this.Init(0, false, TQueryHelperTypeEnum.UPDATE_TYPE);
Dictionary<string, string> fieldMapperDict = null;
if (lambda == null)
{
fieldMapperDict = TQueryReflectionHelper.GetPropertyDict(typeof(T));
}
else
{
OperaterTranslator operaterTranslator = new OperaterTranslator();
operaterTranslator.Translate(lambda);
fieldMapperDict = operaterTranslator.MapperDict;
}
int propertyCount = fieldMapperDict.Count;
int propertyIndex = 0;
StringBuilder stringBuilder = new StringBuilder();
foreach (var keyValueItem in fieldMapperDict)
{
stringBuilder.Append(string.Format(TQueryHelperTemplateEnum.FIELD, keyValueItem.Key));
stringBuilder.Append("=");
stringBuilder.Append("@");
stringBuilder.Append(keyValueItem.Value);
if (propertyIndex < propertyCount - 1)
{
stringBuilder.Append(",");
}
propertyIndex++;
}
string querySql = string.Format(TQueryHelperTemplateEnum.UPDATE, this.queryTableName, stringBuilder.ToString());
this.queryList.Add(new ExpressionKeyValueItem(TQueryHelperTypeEnum.UPDATE, querySql));
}
public void Delete()
{
this.Init(0, false, TQueryHelperTypeEnum.DELETE_TYPE);
this.queryList.Add(new ExpressionKeyValueItem(TQueryHelperTypeEnum.DELETE, string.Format(TQueryHelperTemplateEnum.DELETE, this.queryTableName)));
}
#endregion
#region Select 语句
public void Select(Type type, Expression lambda = null)
{
string querySql = null;
if (lambda != null)
{
querySql = new QueryTranslator().Translate(lambda);
}
else
{
querySql = string.Format(TQueryHelperTemplateEnum.TABLE_ALL, TQueryReflectionHelper.GetTableName(type));
}
this.queryList.Add(new ExpressionKeyValueItem(TQueryHelperTypeEnum.SELECT, querySql));
}
public void Select(Type type, string fieldNameList)
{
this.queryList.Add(new ExpressionKeyValueItem(TQueryHelperTypeEnum.SELECT, TQueryReflectionHelper.GetFieldString(fieldNameList)));
}
public void Select<K>(TQueryHelper<K> helper, string newFieldName = null) where K : class
{
string querySql = helper.ToSql();
if (!string.IsNullOrEmpty(newFieldName))
{
querySql = string.Format(TQueryHelperTemplateEnum.SELECT_AS, querySql, newFieldName);
}
else
{
querySql = string.Format(TQueryHelperTemplateEnum.BREAK, querySql);
}
this.queryList.Add(new ExpressionKeyValueItem(TQueryHelperTypeEnum.SELECT, querySql));
}
#endregion
#region 聚合函数
public void Aggregate(string newFieldName, string aggregateType)
{
string querySql = null;
if (!string.IsNullOrEmpty(newFieldName))
{
querySql = string.Format(" {0}(0) as {1} ", aggregateType, newFieldName);
}
else
{
querySql = string.Format(" {0}(0) ", aggregateType);
}
this.queryList.Add(new ExpressionKeyValueItem(TQueryHelperTypeEnum.AGGREGATE, querySql));
}
public void Aggregate(Expression lambda, string newFieldName, string aggregateType)
{
string querySql = null;
if (!string.IsNullOrEmpty(newFieldName))
{
querySql = string.Format(TQueryHelperTemplateEnum.AGGREGATEAS, aggregateType, new AggregateTranslator().Translate(lambda), newFieldName);
}
else
{
querySql = string.Format(TQueryHelperTemplateEnum.AGGREGATE, aggregateType, new AggregateTranslator().Translate(lambda));
}
this.queryList.Add(new ExpressionKeyValueItem(TQueryHelperTypeEnum.AGGREGATE, querySql));
}
#endregion
#region 公共函数
private void SetJoinTableName(params Type[] typeList)
{
if (this.joinTableDict == null) this.joinTableDict = new Dictionary<string, Type>();
foreach (Type type in typeList)
{
string tableName = type.Name;
if (!this.joinTableDict.ContainsKey(tableName))
{
this.joinTableDict.Add(tableName, type);
}
}
}
private string GetFieldValueString<K>(IList<K> fieldValueList)
{
bool isInt = (typeof(K) == typeof(int));
int dataCount = fieldValueList.Count;
StringBuilder stringBuilder = new StringBuilder();
for (int index = 0; index < dataCount; index++)
{
if (!isInt)
{
stringBuilder.Append(string.Format("'{0}'", fieldValueList[index].ToString()));
}
else
{
stringBuilder.Append(fieldValueList[index].ToString());
}
if (index < dataCount - 1) stringBuilder.Append(",");
}
return stringBuilder.ToString();
}
#endregion
#region 表连接
public void JoinT<K>(Type type, Expression lambda, string joinType = "inner", bool isGeneric = false) where K : class
{
StringBuilder stringBuilder = new StringBuilder();
stringBuilder.Append(" ");
if (isGeneric)
{
stringBuilder.Append(new JoinOnTranslator<K>().Translate(lambda, joinType, TQueryReflectionHelper.GetTableName(type)));
}
else
{
stringBuilder.Append(new JoinOnTranslator().Translate(lambda, joinType, TQueryReflectionHelper.GetTableName(type)));
}
stringBuilder.Append(" ");
this.queryList.Add(new ExpressionKeyValueItem(TQueryHelperTypeEnum.JOINON, stringBuilder.ToString()));
}
public void Join<K>(Expression<Func<T, K, bool>> lambda, string joinType = "inner") where K : class
{
this.SetJoinTableName(typeof(K));
this.JoinT<T>(typeof(K), lambda, joinType);
}
public void Join<K>(Expression<Func<K, object>> lambda, string joinType = "inner") where K : class
{
this.SetJoinTableName(typeof(K));
this.JoinT<T>(typeof(K), lambda, joinType, true);
}
public void Join<K, P>(Expression<Func<K, P, bool>> lambda, string joinType = "inner")
where K : class
where P : class
{
this.SetJoinTableName(typeof(K), typeof(P));
this.JoinT<K>(typeof(P), lambda, joinType);
}
public void Join<K, P>(Expression<Func<P, object>> lambda, string joinType = "inner")
where K : class
where P : class
{
this.SetJoinTableName(typeof(K), typeof(P));
this.JoinT<K>(typeof(P), lambda, joinType, true);
}
public void JoinAnd(string querySql, bool isBreak = false)
{
querySql = string.Format(TQueryHelperTemplateEnum.BREAK, querySql);
this.queryList.Add(new ExpressionKeyValueItem(TQueryHelperTypeEnum.JOINONAND, querySql));
}
public void JoinAnd(Expression lambda, bool isBreak = false)
{
string querySql = new WhereTranslator().Translate(lambda);
if (isBreak)
{
querySql = string.Format(TQueryHelperTemplateEnum.BREAK, querySql);
}
this.queryList.Add(new ExpressionKeyValueItem(TQueryHelperTypeEnum.JOINONAND, querySql));
}
#endregion
#region Where 语句
public void Where(string querySql, bool isBreak = false)
{
querySql = string.Format(TQueryHelperTemplateEnum.BREAK, querySql);
this.queryList.Add(new ExpressionKeyValueItem(TQueryHelperTypeEnum.WHERE, querySql));
}
public void Where(Expression lambda, string conditionType = null, bool isBreak = false)
{
string querySql = new WhereTranslator().Translate(lambda);
if (string.IsNullOrEmpty(conditionType))
{
if (isBreak) querySql = string.Format(TQueryHelperTemplateEnum.BREAK, querySql);
}
else
{
if (isBreak)
{
querySql = string.Format(TQueryHelperTemplateEnum.WHERE_CONDITION_BREAK, conditionType, querySql);
}
else
{
querySql = string.Format(TQueryHelperTemplateEnum.WHERE_CONDITION, conditionType, querySql);
}
}
this.queryList.Add(new ExpressionKeyValueItem(TQueryHelperTypeEnum.WHERE, querySql));
}
public void WhereIn<K>(string fieldName, string charType, TQueryHelper<K> helper, bool directUseFieldName = false) where K : class
{
string querySql = null;
if (!directUseFieldName)
{
querySql = string.Format(TQueryHelperTemplateEnum.WHERE_IN, TQueryReflectionHelper.GetTableName(typeof(T)), fieldName, charType, helper.ToSql());
}
else
{
querySql = string.Format(TQueryHelperTemplateEnum.WHERE_IN_DIRECT, fieldName, charType, helper.ToSql());
}
this.queryList.Add(new ExpressionKeyValueItem(TQueryHelperTypeEnum.WHERE, querySql));
}
public void WhereIn<K>(string fieldName, string charType, IList<K> fieldValueList, bool directUseFieldName = false)
{
string querySql = null;
if (!directUseFieldName)
{
querySql = string.Format(TQueryHelperTemplateEnum.WHERE_IN, TQueryReflectionHelper.GetTableName(typeof(T)), fieldName, charType, this.GetFieldValueString<K>(fieldValueList));
}
else
{
querySql = string.Format(TQueryHelperTemplateEnum.WHERE_IN_DIRECT, fieldName, charType, this.GetFieldValueString<K>(fieldValueList));
}
this.queryList.Add(new ExpressionKeyValueItem(TQueryHelperTypeEnum.WHERE, querySql));
}
public void WhereIn<K>(Expression<Func<T, object>> lambda, string charType, TQueryHelper<K> helper) where K : class
{
StringBuilder stringBuilder = new StringBuilder();
stringBuilder.Append(new WhereInTranslator().Translate(lambda, charType));
stringBuilder.Append(" ");
stringBuilder.Append(string.Format(TQueryHelperTemplateEnum.BREAK, helper.ToSql()));
this.queryList.Add(new ExpressionKeyValueItem(TQueryHelperTypeEnum.WHERE, stringBuilder.ToString()));
}
public void WhereIn<K>(Expression<Func<T, object>> lambda, string charType, IList<K> fieldValueList)
{
StringBuilder stringBuilder = new StringBuilder();
stringBuilder.Append(new WhereInTranslator().Translate(lambda, charType));
stringBuilder.Append(" ");
stringBuilder.Append(string.Format(TQueryHelperTemplateEnum.BREAK, this.GetFieldValueString<K>(fieldValueList)));
this.queryList.Add(new ExpressionKeyValueItem(TQueryHelperTypeEnum.WHERE, stringBuilder.ToString()));
}
public void WhereIn<K, P>(Expression<Func<K, P, object>> lambda, string charType, TQueryHelper<K> helper)
where K : class
where P : class
{
StringBuilder stringBuilder = new StringBuilder();
stringBuilder.Append(new WhereInTranslator().Translate(lambda, charType));
stringBuilder.Append(" ");
stringBuilder.Append(string.Format(TQueryHelperTemplateEnum.BREAK, helper.ToSql()));
this.queryList.Add(new ExpressionKeyValueItem(TQueryHelperTypeEnum.WHERE, stringBuilder.ToString()));
}
public void WhereIn<K, P>(Expression<Func<K, object>> lambda, string charType, IList<P> fieldValueList) where K : class
{
StringBuilder stringBuilder = new StringBuilder();
stringBuilder.Append(new WhereInTranslator().Translate(lambda, charType));
stringBuilder.Append(" ");
stringBuilder.Append(string.Format(TQueryHelperTemplateEnum.BREAK, this.GetFieldValueString<P>(fieldValueList)));
this.queryList.Add(new ExpressionKeyValueItem(TQueryHelperTypeEnum.WHERE, stringBuilder.ToString()));
}
#endregion
#region Order By 语句
public void Order(Type type, string orderFieldList)
{
this.queryList.Add(new ExpressionKeyValueItem(TQueryHelperTypeEnum.ORDERBY, TQueryReflectionHelper.GetFieldString(orderFieldList)));
}
public void Order(Expression lambda, string orderBy)
{
this.queryList.Add(new ExpressionKeyValueItem(TQueryHelperTypeEnum.ORDERBY, new OrderTranslator().Translate(lambda, orderBy)));
}
#endregion
#region Group By 语句
public void Group(Type type, string groupFieldList)
{
this.queryList.Add(new ExpressionKeyValueItem(TQueryHelperTypeEnum.GROUPBY, TQueryReflectionHelper.GetFieldString(groupFieldList)));
}
public void Group(Expression lambda)
{
this.queryList.Add(new ExpressionKeyValueItem(TQueryHelperTypeEnum.GROUPBY, new GroupTranslator().Translate(lambda)));
}
#endregion
#region From In 语句
public void FromIn<K>(TQueryHelper<K> helper, string newTableName = null) where K : class
{
this.fromInTableName = TQueryReflectionHelper.GetTableName(typeof(K));
this.fromInNewTableName = newTableName;
string querySql = string.Format(TQueryHelperTemplateEnum.FROM_IN, helper.ToSql(), string.IsNullOrEmpty(newTableName) ? this.fromInTableName : newTableName);
this.queryList.Add(new ExpressionKeyValueItem(TQueryHelperTypeEnum.FROMIN, querySql));
}
public void Exists<K>(TQueryHelper<K> helper) where K : class
{
string querySql = string.Format(TQueryHelperTemplateEnum.EXISTS, helper.ToSql());
this.queryList.Add(new ExpressionKeyValueItem(TQueryHelperTypeEnum.WHERE, querySql));
}
public void NotExists<K>(TQueryHelper<K> helper) where K : class
{
string querySql = string.Format(TQueryHelperTemplateEnum.NOTEXISTS, helper.ToSql());
this.queryList.Add(new ExpressionKeyValueItem(TQueryHelperTypeEnum.WHERE, querySql));
}
#endregion
#region Union/Union All 语句
public void Union<K>(TQueryHelper<K> helper) where K : class
{
this.queryList.Add(new ExpressionKeyValueItem(TQueryHelperTypeEnum.UNION, helper.ToSql()));
}
#endregion
#region (/) 括号
public void BreakLeft()
{
this.queryList.Add(new ExpressionKeyValueItem(TQueryHelperTypeEnum.BREAKLEFT, "("));
}
public void BreakRight()
{
this.queryList.Add(new ExpressionKeyValueItem(TQueryHelperTypeEnum.BREAKRIGHT, ")"));
}
#endregion
public string ToSql(bool withNoLock = true)
{
if (this.queryType == TQueryHelperTypeEnum.INSERT_TYPE) return this.CreateInsertSql();
if (this.queryType == TQueryHelperTypeEnum.UPDATE_TYPE) return this.CreateUpdateSql();
if (this.queryType == TQueryHelperTypeEnum.DELETE_TYPE) return this.CreateDeleteSql();
string resultQuerySql = this.CreateQuerySql();
if (!withNoLock)
{
resultQuerySql = resultQuerySql.Replace("with(nolock)", "");
}
return resultQuerySql;
}
#region 生成 增/删/改/查 语句
private string CreateInsertSql()
{
if (this.queryList == null) return "";
StringBuilder stringBuilder = new StringBuilder();
List<ExpressionKeyValueItem> dataList = this.queryList.Where(p => p.Key == TQueryHelperTypeEnum.INSERT).ToList();
int dataCount = dataList.Count;
for (int index = 0; index < dataCount; index++)
{
stringBuilder.Append(dataList[index].Value);
if (index < dataCount - 1)
{
stringBuilder.Append(";");
}
}
return stringBuilder.ToString();
}
private string CreateUpdateSql()
{
if (this.queryList == null) return "";
StringBuilder stringBuilder = new StringBuilder();
bool isFirst = true;
bool isWhere = false;
List<ExpressionKeyValueItem> dataList = this.queryList.Where(p => p.Key == TQueryHelperTypeEnum.UPDATE || p.Key == TQueryHelperTypeEnum.WHERE || p.Key == TQueryHelperTypeEnum.BREAKLEFT || p.Key == TQueryHelperTypeEnum.BREAKRIGHT).ToList();
foreach (var keyValueItem in dataList)
{
if (keyValueItem.Key == TQueryHelperTypeEnum.UPDATE)
{
if (!isFirst)
{
stringBuilder.Append(";");
}
stringBuilder.Append(keyValueItem.Value);
isFirst = false;
}
else if (keyValueItem.Key == TQueryHelperTypeEnum.WHERE)
{
if (!isWhere)
{
stringBuilder.Append(" where ");
}
stringBuilder.Append(keyValueItem.Value);
}
else
{
stringBuilder.Append(keyValueItem.Value);
}
}
return stringBuilder.ToString();
}
private string CreateDeleteSql()
{
if (this.queryList == null) return "";
StringBuilder stringBuilder = new StringBuilder();
bool isFirst = true;
bool isWhere = false;
List<ExpressionKeyValueItem> dataList = this.queryList.Where(p => p.Key == TQueryHelperTypeEnum.DELETE || p.Key == TQueryHelperTypeEnum.WHERE || p.Key == TQueryHelperTypeEnum.BREAKLEFT || p.Key == TQueryHelperTypeEnum.BREAKRIGHT).ToList();
foreach (var keyValueItem in dataList)
{
if (keyValueItem.Key == TQueryHelperTypeEnum.DELETE)
{
if (!isFirst)
{
stringBuilder.Append(";");
}
stringBuilder.Append(keyValueItem.Value);
isFirst = false;
}
else if (keyValueItem.Key == TQueryHelperTypeEnum.WHERE)
{
if (!isWhere)
{
stringBuilder.Append(" where ");
}
stringBuilder.Append(keyValueItem.Value);
}
else
{
stringBuilder.Append(keyValueItem.Value);
}
}
return stringBuilder.ToString();
}
private string CreateQuerySql()
{
string tableName = TQueryReflectionHelper.GetTableName(typeof(T));
#region 当无数据时
if (this.queryList == null || this.queryList.Count == 0)
{
string querySql = null;
if (this.distinct)
{
if (this.queryTop > 0)
{
querySql = string.Format(TQueryHelperTemplateEnum.QUERY_DISTINCT_TOP, tableName, this.queryTop);
}
else
{
querySql = string.Format(TQueryHelperTemplateEnum.QUERY_DISTINCT, tableName);
}
}
else
{
if (this.queryTop > 0)
{
querySql = string.Format(TQueryHelperTemplateEnum.QUERY_TOP, tableName, this.queryTop);
}
else
{
querySql = string.Format(TQueryHelperTemplateEnum.QUERY_DEFAULT, tableName);
}
}
return querySql;
}
#endregion
StringBuilder stringBuilder = new StringBuilder();
int queryCount = this.queryList != null ? this.queryList.Count : 0;
if (queryCount > 0)
{
string expressionName = null;
ExpressionKeyValueItem keyValueItem = null;
#region 处理别名映射
if (!string.IsNullOrEmpty(this.fromInNewTableName))
{
foreach (var mapperKeyValueItem in this.queryList)
{
if (mapperKeyValueItem.Key == TQueryHelperTypeEnum.SELECT || mapperKeyValueItem.Key == TQueryHelperTypeEnum.WHERE || mapperKeyValueItem.Key == TQueryHelperTypeEnum.ORDERBY || mapperKeyValueItem.Key == TQueryHelperTypeEnum.GROUPBY)
{
mapperKeyValueItem.Value = mapperKeyValueItem.Value.Replace(this.fromInTableName, this.fromInNewTableName);
}
}
}
#endregion
#region 创建 Select 语句
stringBuilder.Append("select ");
if (this.distinct) stringBuilder.Append(" distinct ");
if (this.queryTop > 0)
{
stringBuilder.Append(" top ");
stringBuilder.Append(this.queryTop);
stringBuilder.Append(" ");
}
List<ExpressionKeyValueItem> aggregateDataList = this.queryList.Where(p => p.Key == TQueryHelperTypeEnum.AGGREGATE).ToList();
List<ExpressionKeyValueItem> dataList = this.queryList.Where(p => p.Key == TQueryHelperTypeEnum.SELECT).ToList();
int dataCount = dataList != null ? dataList.Count : 0;
if ((dataList == null || dataCount == 0) && (aggregateDataList == null || aggregateDataList.Count == 0))
{
stringBuilder.Append(string.Format(TQueryHelperTemplateEnum.TABLE_ALL, tableName));
if (this.joinTableDict != null)
{
if (this.joinTableDict.Count > 0)
{
stringBuilder.Append(",");
}
int joinIndex = 0;
foreach (var joinKeyValueItem in this.joinTableDict)
{
stringBuilder.Append(string.Format(TQueryHelperTemplateEnum.TABLE_ALL, TQueryReflectionHelper.GetTableName(joinKeyValueItem.Value)));
if (joinIndex < this.joinTableDict.Count - 1) stringBuilder.Append(",");
joinIndex++;
}
}
}
else
{
for (int index = 0; index < dataCount; index++)
{
stringBuilder.Append(dataList[index].Value);
if (index < dataCount - 1)
{
stringBuilder.Append(",");
}
}
}
if (aggregateDataList != null && aggregateDataList.Count > 0)
{
if (dataList != null && dataList.Count > 0) stringBuilder.Append(",");
dataCount = aggregateDataList.Count;
for (int index = 0; index < dataCount; index++)
{
stringBuilder.Append(aggregateDataList[index].Value);
if (index < dataCount - 1)
{
stringBuilder.Append(",");
}
}
}
#endregion
#region From 语句
stringBuilder.Append(" from ");
// 判断子查询
keyValueItem = this.queryList.Where(p => p.Key == TQueryHelperTypeEnum.FROMIN).FirstOrDefault();
// 当没有子查询时,追加
if (keyValueItem == null) stringBuilder.Append(this.queryTableName);
#endregion
#region 遍历
for (int index = 0; index < queryCount; index++)
{
keyValueItem = this.queryList[index];
if (keyValueItem.Key == TQueryHelperTypeEnum.SELECT || keyValueItem.Key == TQueryHelperTypeEnum.AGGREGATE || keyValueItem.Key == TQueryHelperTypeEnum.ORDERBY || keyValueItem.Key == TQueryHelperTypeEnum.GROUPBY || keyValueItem.Key == TQueryHelperTypeEnum.UNION) continue;
if (keyValueItem.Key == TQueryHelperTypeEnum.WHERE && expressionName != TQueryHelperTypeEnum.WHERE)
{
stringBuilder.Append(" where ");
}
if (keyValueItem.Key == TQueryHelperTypeEnum.WHERE && expressionName == TQueryHelperTypeEnum.WHERE)
{
if (!keyValueItem.Value.StartsWith(" and ") && !keyValueItem.Value.StartsWith(" or "))
{
stringBuilder.Append(" and ");
}
}
if (keyValueItem.Key == TQueryHelperTypeEnum.JOINONAND)
{
if (!keyValueItem.Value.StartsWith(" and ") && !keyValueItem.Value.StartsWith(" or "))
{
stringBuilder.Append(" and ");
}
}
stringBuilder.Append(keyValueItem.Value);
expressionName = keyValueItem.Key;
}
#endregion
#region Group By 语句
dataList = this.queryList.Where(p => p.Key == TQueryHelperTypeEnum.GROUPBY).ToList();
dataCount = dataList != null ? dataList.Count : 0;
if (dataList != null && dataCount > 0)
{
stringBuilder.Append(" group by ");
for (int index = 0; index < dataCount; index++)
{
stringBuilder.Append(dataList[index].Value);
if (index < dataCount - 1)
{
stringBuilder.Append(",");
}
}
}
#endregion
#region Order By 语句
dataList = this.queryList.Where(p => p.Key == TQueryHelperTypeEnum.ORDERBY).ToList();
dataCount = dataList != null ? dataList.Count : 0;
if (dataList != null && dataCount > 0)
{
stringBuilder.Append(" order by ");
for (int index = 0; index < dataCount; index++)
{
stringBuilder.Append(dataList[index].Value);
if (index < dataCount - 1)
{
stringBuilder.Append(",");
}
}
}
#endregion
}
#region 处理 Union 语句
List<ExpressionKeyValueItem> unionDataList = this.queryList.Where(p => p.Key == TQueryHelperTypeEnum.UNION).ToList();
if (unionDataList == null || unionDataList.Count == 0)
{
return stringBuilder.ToString();
}
else
{
StringBuilder unionStringBuilder = new StringBuilder();
unionStringBuilder.Append(string.Format(TQueryHelperTemplateEnum.BREAK, stringBuilder.ToString()));
foreach(ExpressionKeyValueItem keyValueItem in unionDataList)
{
unionStringBuilder.Append(" union ");
unionStringBuilder.Append(string.Format(TQueryHelperTemplateEnum.BREAK, keyValueItem.Value));
}
return unionStringBuilder.ToString();
}
#endregion
}
#endregion
}
}