forked from cyq1162/cyqdata
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMDataTableFilter.cs
781 lines (754 loc) · 29.5 KB
/
MDataTableFilter.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
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
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
using System;
using System.Collections.Generic;
using System.Text;
using CYQ.Data.SQL;
using System.Data;
using System.ComponentModel;
using CYQ.Data.Tool;
namespace CYQ.Data.Table
{
/// <summary>
/// 操作符号
/// </summary>
internal enum Op
{
/// <summary>
/// 没有操作
/// </summary>
None,
/// <summary>
/// 操作符号:">"
/// </summary>
Big,
/// <summary>
/// 操作符号:">="
/// </summary>
BigEqual,
/// <summary>
/// 操作符号:"="
/// </summary>
Equal,
/// <summary>
/// 操作符号:"<>"
/// </summary>
NotEqual,
/// <summary>
/// 操作符号:"<"
/// </summary>
Small,
/// <summary>
/// 操作符号:"<="
/// </summary>
SmallEqual,
/// <summary>
/// 操作符号:"like"
/// </summary>
Like,
/// <summary>
/// 是否Null值
/// </summary>
IsNull,
/// <summary>
/// 非Null值
/// </summary>
IsNotNull,
/// <summary>
/// 操作符号:In
/// </summary>
In,
/// <summary>
/// 操作符号:Not In
/// </summary>
NotIn
}
/// <summary>
/// 查询的基本连接条件
/// </summary>
internal enum Ao
{
None,
And,
Or
}
/// <summary>
/// 条件过滤参数
/// </summary>
internal class TFilter
{
internal Ao _Ao;
internal object _valueA;
internal int _columnAIndex = -1;
internal Op _Op;
internal object _valueB;
internal int _columnBIndex = -1;
public TFilter(Ao ao, object valueA, int columnAIndex, Op op, object valueB, int columnBIndex)
{
_Ao = ao;
_valueA = valueA;
_columnAIndex = columnAIndex;
_Op = op;
_valueB = valueB;
_columnBIndex = columnBIndex;
}
}
/// <summary>
/// MDataTable查询过滤器
/// </summary>
internal static class MDataTableFilter
{
public static MDataTable[] Split(MDataTable table, object whereObj)
{
MDataTable[] mdt2 = new MDataTable[2];
mdt2[0] = table.GetSchema(false);
mdt2[1] = table.GetSchema(false);
if (table.Rows.Count > 0)
{
if (Convert.ToString(whereObj).Trim() == "")
{
mdt2[0] = table;
}
else
{
TFilter[] filters = GetTFilter(whereObj, table.Columns);
if (filters.Length > 0)
{
for (int i = 0; i < table.Rows.Count; i++)
{
MDataRow row = table.Rows[i];
if (CompareMore(row, filters))
{
mdt2[0].Rows.Add(row, false);
}
else
{
mdt2[1].Rows.Add(row, false);
}
}
}
}
}
return mdt2;
}
public static int GetCount(MDataTable table, object whereObj)
{
int count = 0;
if (table.Rows.Count > 0)
{
if (Convert.ToString(whereObj).Trim() == "")
{
return table.Rows.Count;
}
TFilter[] filters = GetTFilter(whereObj, table.Columns);
if (filters.Length > 0)
{
for (int i = 0; i < table.Rows.Count; i++)
{
if (CompareMore(table.Rows[i], filters))
{
count++;
}
}
}
filters = null;
}
return count;
}
public static List<MDataRow> FindAll(MDataTable table, object whereObj)
{
if (table != null && table.Rows.Count > 0)
{
if (Convert.ToString(whereObj).Trim() == "")
{
return table.Rows;
}
string whereStr = SqlFormat.GetIFieldSql(whereObj);
string orderby;
SplitWhereOrderby(ref whereStr, out orderby);
TFilter[] filters = GetTFilter(whereStr, table.Columns);
List<MDataRow> rows = table.Rows;
if (filters.Length > 0)
{
rows = table.Rows.FindAll(delegate(MDataRow row)
{
return CompareMore(row, filters);
}
);
}
filters = null;
if (!string.IsNullOrEmpty(orderby) && rows.Count > 1)//进行数组排序
{
MDataRowCollection sortRows = new MDataRowCollection();
sortRows.AddRange(rows);
sortRows.Sort(orderby);
return sortRows;
}
return rows;
}
return null;
}
public static MDataRow FindRow(MDataTable table, object whereObj)
{
if (table.Rows.Count > 0)
{
if (Convert.ToString(whereObj).Trim() == "")
{
return table.Rows[0];
}
string whereStr = SqlFormat.GetIFieldSql(whereObj);
string orderby;
SplitWhereOrderby(ref whereStr, out orderby);
MDataRowCollection sortRows = null;
if (!string.IsNullOrEmpty(orderby) && table.Rows.Count > 1)//进行数组排序
{
sortRows = new MDataRowCollection();
sortRows.AddRange(table.Rows);
sortRows.Sort(orderby);
}
TFilter[] filters = GetTFilter(whereStr, table.Columns);
if (filters.Length > 0)
{
if (sortRows == null)
{
sortRows = table.Rows;
}
for (int i = 0; i < sortRows.Count; i++)
{
if (CompareMore(sortRows[i], filters))
{
return sortRows[i];
}
}
}
}
return null;
}
public static MDataTable Select(MDataTable table, int pageIndex, int pageSize, object whereObj)
{
return Select(table, pageIndex, pageSize, whereObj);
}
public static MDataTable Select(MDataTable table, int pageIndex, int pageSize, object whereObj, params object[] selectColumns)
{
if (table == null)
{
return null;
}
MDataTable sTable = table.GetSchema(true);
sTable.RecordsAffected = table.Rows.Count;
if (table.Rows.Count == 0)// 正常情况下,看是否需要处理列移除
{
FilterColumns(ref sTable, selectColumns);//列查询过滤
return sTable;
}
List<MDataRow> findRows = FindAll(table, whereObj);
if (findRows != null)
{
FilterPager(findRows, pageIndex, pageSize);//进行分页筛选,再克隆最后的数据。
for (int i = 0; i < findRows.Count; i++)
{
if (i < findRows.Count)//内存表时(表有可能在其它线程被清空)
{
MDataRow row = findRows[i];
if (row == null)
{
break;
}
sTable.NewRow(true).LoadFrom(row);
}
}
findRows = null;
}
if (selectColumns != null && selectColumns.Length > 0)
{
FilterColumns(ref sTable, selectColumns);//列查询过滤,由于查询的条件可能包含被移除列,所以只能在最后才过滤
}
//进行条件查询
return sTable;
}
private static int IndexOf(string where, string andOrSign, int start)
{
//为了处理如下的特殊情况:"B like '%'' and ''%' and A='a' or A<2 order by a,b desc"); 第1个 and 是假的
int index = where.IndexOf(andOrSign, start);
int lastIndex = start;
while (index > -1)//前置的引号检查,如果引号为单,则继续取下一个
{
if (where.Substring(start, index - start).Split('\'').Length % 2 == 0)//为双的话仅有单个的符号,将继续往后截
{
lastIndex = index + andOrSign.Length;
index = where.IndexOf(andOrSign, lastIndex + 1);
}
else
{
return index;
}
}
return index;
}
/// <summary>
/// 多个条件
/// </summary>
private static TFilter[] GetTFilter(object whereObj, MDataColumn mdc)
{
List<TFilter> tFilterList = new List<TFilter>();
string whereStr = SqlFormat.GetIFieldSql(whereObj);
whereStr = SqlCreate.FormatWhere(whereStr, mdc, DalType.None, null);
string lowerWhere = whereStr.ToLower();
string andSign = " and ";
string orSign = " or ";
int andIndex = IndexOf(lowerWhere, andSign, 0);// lowerWhere.IndexOf(andSign);
int orIndex = IndexOf(lowerWhere, orSign, 0);
TFilter filter = null;
if (andIndex == -1 && orIndex == -1)//仅有一个条件
{
filter = GetSingleTFilter(whereStr, mdc);
if (filter != null)
{
tFilterList.Add(filter);
}
}
else if (orIndex == -1) // 只有and条件
{
int andStartIndex = 0;
while (andIndex > -1)
{
filter = GetSingleTFilter(whereStr.Substring(andStartIndex, andIndex - andStartIndex), mdc);
if (filter != null)
{
if (andStartIndex > 0)
{
filter._Ao = Ao.And;
}
tFilterList.Add(filter);
}
andStartIndex = andIndex + andSign.Length;
andIndex = IndexOf(lowerWhere, andSign, andStartIndex + 1);
}
filter = GetSingleTFilter(whereStr.Substring(andStartIndex), mdc);
if (filter != null)
{
filter._Ao = Ao.And;
tFilterList.Add(filter);
}
}
else if (andIndex == -1) //只有or 条件
{
int orStartIndex = 0;
while (orIndex > -1)
{
filter = GetSingleTFilter(whereStr.Substring(orStartIndex, orIndex - orStartIndex), mdc);
if (filter != null)
{
if (orStartIndex > 0)
{
filter._Ao = Ao.Or;
}
tFilterList.Add(filter);
}
orStartIndex = orIndex + orSign.Length;
orIndex = IndexOf(lowerWhere, orSign, orStartIndex + 1);
}
filter = GetSingleTFilter(whereStr.Substring(orStartIndex), mdc);
if (filter != null)
{
filter._Ao = Ao.Or;
tFilterList.Add(filter);
}
}
else //有and 又有 or
{
bool isAnd = andIndex < orIndex;
bool lastAnd = isAnd;
int andOrIndex = isAnd ? andIndex : orIndex;//最小的,前面的先处理
int andOrStartIndex = 0;
while (andOrIndex > -1)
{
filter = GetSingleTFilter(whereStr.Substring(andOrStartIndex, andOrIndex - andOrStartIndex), mdc);
if (filter != null)
{
if (andOrStartIndex > 0)
{
filter._Ao = lastAnd ? Ao.And : Ao.Or;
}
tFilterList.Add(filter);
}
andOrStartIndex = andOrIndex + (isAnd ? andSign.Length : orSign.Length);
if (isAnd)
{
andIndex = IndexOf(lowerWhere, andSign, andOrStartIndex + 1);
}
else
{
orIndex = IndexOf(lowerWhere, orSign, andOrStartIndex + 1);
}
lastAnd = isAnd;
if (andIndex == -1)
{
isAnd = false;
andOrIndex = orIndex;
}
else if (orIndex == -1)
{
isAnd = true;
andOrIndex = andIndex;
}
else
{
isAnd = andIndex < orIndex;
andOrIndex = isAnd ? andIndex : orIndex;//最小的,前面的先处理
}
}
filter = GetSingleTFilter(whereStr.Substring(andOrStartIndex), mdc);
if (filter != null)
{
filter._Ao = lastAnd ? Ao.And : Ao.Or;
tFilterList.Add(filter);
}
}
// string firstFilter=whereStr.su
return tFilterList.ToArray();
}
/// <summary>
/// SQL操作符号
/// </summary>
private static Dictionary<string, Op> Ops
{
get
{
Dictionary<string, Op> _ops = new Dictionary<string, Op>(StringComparer.OrdinalIgnoreCase);
_ops.Add("<>", Op.NotEqual);
_ops.Add(">=", Op.BigEqual);
_ops.Add("<=", Op.SmallEqual);
_ops.Add("=", Op.Equal);
_ops.Add(">", Op.Big);
_ops.Add("<", Op.Small);
_ops.Add(" like ", Op.Like);
_ops.Add(" is null", Op.IsNull);
_ops.Add(" is not null", Op.IsNotNull);
_ops.Add(" not in", Op.NotIn);//not int 顺序要靠前。
_ops.Add(" in", Op.In);
return _ops;
}
}
/// <summary>
/// 单个条件
/// </summary>
private static TFilter GetSingleTFilter(string where, MDataColumn mdc)
{
//id like 'a>b=c'
//id>'a like b'
where = where.TrimStart('(').TrimEnd(')').Trim();
int quoteIndex = where.IndexOf('\'');
quoteIndex = quoteIndex == -1 ? 0 : quoteIndex;
TFilter tFilter = null;
foreach (KeyValuePair<string, Op> opItem in Ops)
{
if (GetTFilterOk(where, quoteIndex, opItem.Key, opItem.Value, mdc, out tFilter))
{
break;
}
}
return tFilter;
}
/// <summary>
/// 条件比较
/// </summary>
private static bool GetTFilterOk(string where, int quoteIndex, string sign, Op op, MDataColumn mdc, out TFilter tFilter)
{
bool result = false;
tFilter = null;
int index = where.ToLower().IndexOf(sign, 0, quoteIndex > 0 ? quoteIndex : where.Length);
if (index > 0)
{
string columnAName = where.Substring(0, index).Trim();
int columnAIndex = mdc.GetIndex(columnAName);
string valueB = where.Substring(index + sign.Length).Trim(' ', '\'').Replace("''", "'");
if (op == Op.In || op == Op.NotIn)
{
valueB = ',' + valueB.TrimStart('(', ')').Replace("'", "") + ",";//去除单引号。
}
int columnBIndex = -1;
if (quoteIndex == 0 && mdc.Contains(valueB)) //判断右侧的是否列名。
{
columnBIndex = mdc.GetIndex(valueB);
}
tFilter = new TFilter(Ao.None, columnAName, columnAIndex, op, valueB, columnBIndex);
if (columnBIndex == -1 && !string.IsNullOrEmpty(Convert.ToString(valueB)))//右侧是值类型,转换值的类型。
{
if (columnAIndex > -1 && DataType.GetGroup(mdc[columnAIndex].SqlType) == 3)//bool型
{
switch (Convert.ToString(tFilter._valueB).ToLower())
{
case "true":
case "1":
case "on":
tFilter._valueB = true;
break;
case "false":
case "0":
case "":
tFilter._valueB = false;
break;
default:
tFilter._valueB = null;
break;
}
}
else
{
try
{
tFilter._valueB = StaticTool.ChangeType(tFilter._valueB, columnAIndex > -1 ? typeof(string) : mdc[columnAIndex].ValueType);
}
catch
{
}
}
}
result = true;
}
return result;
}
/// <summary>
/// 值比较
/// </summary>
private static bool CompareMore(MDataRow row, params TFilter[] filters)
{
if (row == null) { return false; }
bool result = false;
MDataCell cell = null, otherCell = null;
SqlDbType sqlDbType = SqlDbType.Int;
bool moreResult = false;
object valueA = null, valueB = null;
foreach (TFilter item in filters)
{
if (item._Op == Op.None)
{
moreResult = true;
}
else
{
if (item._columnAIndex == -1)
{
valueA = item._valueA;
sqlDbType = SqlDbType.NVarChar;
}
else
{
cell = row[item._columnAIndex];
valueA = cell.Value;
sqlDbType = cell.Struct.SqlType;
}
if (item._columnBIndex > -1)
{
otherCell = row[item._columnBIndex];
valueB = otherCell.Value;
if (DataType.GetGroup(sqlDbType) != DataType.GetGroup(otherCell.Struct.SqlType))
{
sqlDbType = SqlDbType.NVarChar;//不同类型的比较,转成字符串比较。
}
}
else
{
valueB = item._valueB;
}
switch (item._Op)
{
case Op.IsNull:
moreResult = cell != null && cell.IsNull;
break;
case Op.IsNotNull:
moreResult = cell != null && !cell.IsNull;
break;
default:
if (Convert.ToString(valueA) == "" || Convert.ToString(valueB) == "")//空格的问题,稍后回来处理。
{
int a = valueA == null ? 1 : (Convert.ToString(valueA) == "" ? 2 : 3);
int b = valueB == null ? 1 : (Convert.ToString(valueB) == "" ? 2 : 3);
switch (item._Op)
{
case Op.Big:
moreResult = a > b;
break;
case Op.Like:
case Op.Equal:
moreResult = a == b;
break;
case Op.SmallEqual:
moreResult = a <= b;
break;
case Op.BigEqual:
moreResult = a >= b;
break;
case Op.Small:
moreResult = a < b;
break;
case Op.NotEqual:
moreResult = a != b;
break;
//case Op.In:
// break;
//case Op.NotIn:
// break;
}
}
else
{
moreResult = Compare(sqlDbType, valueA, item._Op, valueB);
}
break;
}
}
switch (item._Ao)
{
case Ao.And:
result = result && moreResult;
break;
case Ao.Or:
result = result || moreResult;
break;
default:
result = moreResult;
break;
}
}
return result;
}
private static bool Compare(SqlDbType sqlType, object valueA, Op op, object valueB)
{
try
{
switch (op)
{
case Op.Big:
case Op.BigEqual:
switch (DataType.GetGroup(sqlType))
{
case 1://int
return op == Op.Big ? ((int)valueA > Convert.ToInt32(valueB)) : ((int)valueA >= Convert.ToInt32(valueB));
case 2://datetime
return op == Op.Big ? (DateTime)valueA > Convert.ToDateTime(valueB) : (DateTime)valueA >= Convert.ToDateTime(valueB);
default:
int value = Convert.ToString(valueA).CompareTo(valueB);
return op == Op.Big ? value == 1 : value > -1;
}
case Op.Equal:
return string.Compare(Convert.ToString(valueA), Convert.ToString(valueB), true) == 0;
case Op.NotEqual:
return string.Compare(Convert.ToString(valueA), Convert.ToString(valueB), true) != 0;
case Op.Small:
case Op.SmallEqual:
switch (DataType.GetGroup(sqlType))
{
case 1://int
return op == Op.Small ? ((int)valueA < Convert.ToInt32(valueB)) : ((int)valueA <= Convert.ToInt32(valueB));
case 2://datetime
return op == Op.Small ? (DateTime)valueA < Convert.ToDateTime(valueB) : (DateTime)valueA <= Convert.ToDateTime(valueB);
default:
int value = Convert.ToString(valueA).CompareTo(valueB);
return op == Op.Small ? value == -1 : value <= 0;
}
case Op.Like:
string bValue = Convert.ToString(valueB);
if (!bValue.StartsWith("%"))
{
return Convert.ToString(valueA).StartsWith(bValue.Trim('%'), StringComparison.OrdinalIgnoreCase);
}
else if (!bValue.EndsWith("%"))
{
return Convert.ToString(valueA).EndsWith(bValue.Trim('%'), StringComparison.OrdinalIgnoreCase);
}
else
{
return Convert.ToString(valueA).IndexOf(bValue.Trim('%'), StringComparison.OrdinalIgnoreCase) > -1;
}
case Op.In:
return Convert.ToString(valueB).Contains(',' + Convert.ToString(valueA) + ",");
case Op.NotIn:
return !Convert.ToString(valueB).Contains(',' + Convert.ToString(valueA) + ",");
}
return false;
}
catch
{
return false;
}
}
/// <summary>
/// 分隔where条件中order by 出来
/// </summary>
/// <param name="where"></param>
private static void SplitWhereOrderby(ref string where, out string orderby)
{
orderby = string.Empty;
if (!string.IsNullOrEmpty(where))
{
int orderbyIndex = where.IndexOf("order by", StringComparison.OrdinalIgnoreCase);
if (orderbyIndex > -1)
{
orderby = where.Substring(orderbyIndex);
if (orderbyIndex > 0)
{
where = where.Substring(0, orderbyIndex - 1);//-1是去掉空格
}
else
{
where = string.Empty;
}
}
}
}
private static void FilterPager(List<MDataRow> rows, int pageIndex, int pageSize)
{
if (pageIndex > -1 && pageSize > 0) //分页处理返回
{
pageIndex = pageIndex == 0 ? 1 : pageIndex;
int start = (pageIndex - 1) * pageSize;//从第几条开始查(索引)
int end = start + pageSize;//查到第N条结束(索引)。
int rowCount = rows.Count;
if (rowCount > end)//总数>N
{
rows.RemoveRange(end, rowCount - end);//移除尾数
}
if (start > 0)//总数>起即
{
if (rowCount > start)
{
rows.RemoveRange(0, start);//移除起始数
}
else if (rowCount < start) //查询的超出总数
{
rows.Clear();//直接清空返回
}
}
}
}
/// <summary>
/// 只保留查询的列
/// </summary>
private static void FilterColumns(ref MDataTable table, params object[] selectColumns)
{
if (selectColumns != null && selectColumns.Length > 0)
{
#region 列移除
bool contain = false;
for (int i = 0; i < table.Columns.Count; i++)
{
contain = false;
foreach (string columnName in selectColumns)
{
if (string.Compare(table.Columns[i].ColumnName, columnName, true) == 0)
{
contain = true;
break;
}
}
if (!contain)
{
table.Columns.RemoveAt(i);
i--;
}
}
#endregion
}
}
}
}