-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathRestrictions.cs
1149 lines (985 loc) · 39.1 KB
/
Restrictions.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
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#region Related components
using System;
using System.Collections.Generic;
using System.Linq;
using System.Xml.Serialization;
using MsgPack.Serialization;
using MongoDB.Driver;
using MongoDB.Bson;
using MongoDB.Bson.Serialization.Attributes;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using Newtonsoft.Json.Converters;
using net.vieapps.Components.Utility;
#endregion
namespace net.vieapps.Components.Repository
{
/// <summary>
/// Presents a filtering expression for using with comparing operators
/// </summary>
public class FilterBy : IFilterBy
{
/// <summary>
/// Initializes a new filtering expression
/// </summary>
public FilterBy() : this(null, null) { }
/// <summary>
/// Initializes a new filtering expression
/// </summary>
/// <param name="attribute">The attribute to compare</param>
/// <param name="operator">The operator to compare</param>
/// <param name="value">The value to compare</param>
public FilterBy(string attribute = null, CompareOperator @operator = CompareOperator.Equals, object value = null)
: this(null, attribute, @operator, value) { }
/// <summary>
/// Initializes a filtering expression
/// </summary>
/// <param name="json">The JSON object that contains the expression</param>
/// <param name="attribute">The attribute to compare</param>
/// <param name="operator">The operator to compare</param>
/// <param name="value">The value to compare</param>
public FilterBy(JObject json, string attribute = null, CompareOperator @operator = CompareOperator.Equals, object value = null)
{
this.Attribute = attribute;
this.Operator = @operator;
this.Value = value;
this.Parse(json);
}
/// <summary>
/// Gets or sets the name of an attribute for comparing
/// </summary>
public string Attribute { get; set; }
/// <summary>
/// Gets or sets the operator for comparing
/// </summary>
[JsonConverter(typeof(StringEnumConverter)), BsonRepresentation(BsonType.String)]
public CompareOperator Operator { get; set; }
/// <summary>
/// Gets or sets the attribute's value for comparing
/// </summary>
public object Value { get; set; }
/// <summary>
/// Gets or sets the extra information
/// </summary>
public string Extra { get; set; }
public void Parse(JObject json)
{
if (json != null)
{
this.Attribute = json.Get<string>("Attribute");
this.Operator = (json.Get<string>("Operator") ?? "Equals").TryToEnum(out CompareOperator @operator) ? @operator : CompareOperator.Equals;
this.Value = (json["Value"] as JValue)?.Value;
this.Extra = json.Get<string>("Extra");
}
}
public JToken ToJson()
=> new JObject
{
{ "Attribute", this.Attribute },
{ "Operator", this.Operator.ToString() },
{ "Value", new JValue(this.Value) },
{ "Extra", this.Extra }
};
/// <summary>
/// Converts this filtering expression to JSON string
/// </summary>
/// <param name="formatting"></param>
/// <returns></returns>
public string ToString(Formatting formatting)
=> this.ToJson().ToString(formatting);
/// <summary>
/// Converts this filtering expression to JSON string
/// </summary>
/// <returns></returns>
public override string ToString()
=> this.ToString(Formatting.None);
}
// ------------------------------------------
/// <summary>
/// Presents a filtering expression for using with comparing operators
/// </summary>
public class FilterBy<T> : FilterBy, IFilterBy<T> where T : class
{
/// <summary>
/// Initializes a new filtering expression
/// </summary>
/// <param name="attribute">The attribute to compare</param>
/// <param name="operator">The operator to compare</param>
/// <param name="value">The value to compare</param>
public FilterBy(string attribute = null, CompareOperator @operator = CompareOperator.Equals, object value = null)
: base(attribute, @operator, value) { }
/// <summary>
/// Initializes a filtering expression
/// </summary>
/// <param name="json">The JSON object that contains the expression</param>
/// <param name="attribute">The attribute to compare</param>
/// <param name="operator">The operator to compare</param>
/// <param name="value">The value to compare</param>
public FilterBy(JObject json, string attribute = null, CompareOperator @operator = CompareOperator.Equals, object value = null)
: base(json, attribute, @operator, value) { }
/// <summary>
/// Gets the filtering expression that mark as parent of this expression
/// </summary>
[JsonIgnore, XmlIgnore, BsonIgnore, MessagePackIgnore]
public FilterBys<T> Parent { get; internal set; }
object GetValue(Dictionary<string, AttributeInfo> standardProperties, Dictionary<string, ExtendedPropertyDefinition> extendedProperties, bool extendedDateTimeAsString = true)
{
try
{
if (this.Attribute == null || this.Value == null)
return this.Value;
else if (standardProperties != null && standardProperties.TryGetValue(this.Attribute, out var standardProperty) && standardProperty != null)
{
if (standardProperty.IsDateTimeType())
{
var value = this.Value.GetType().IsDateTimeType()
? (DateTime)this.Value
: DateTime.Parse(this.Value.ToString());
return standardProperty.IsStoredAsString()
? value.ToDTString(false, standardProperty.IsStoredAsDateTimeString()) as object
: value;
}
else
return standardProperty.IsStringType()
? this.Value.ToString()
: this.Value;
}
else if (extendedProperties != null && extendedProperties.TryGetValue(this.Attribute, out var extendedProperty) && extendedProperty != null)
switch (extendedProperty.Mode)
{
case ExtendedPropertyMode.SmallText:
case ExtendedPropertyMode.MediumText:
case ExtendedPropertyMode.LargeText:
case ExtendedPropertyMode.Select:
case ExtendedPropertyMode.Lookup:
return this.Value.ToString();
case ExtendedPropertyMode.DateTime:
var value = this.Value.GetType().IsDateTimeType()
? (DateTime)this.Value
: DateTime.Parse(this.Value.ToString());
return extendedDateTimeAsString
? value.ToDTString() as object
: value;
default:
return this.Value;
}
else
return this.Value;
}
catch
{
return this.Value;
}
}
#region Working with SQL
internal (string Statement, Dictionary<string, object> Parameters) GetSqlStatement(string suffix, Dictionary<string, AttributeInfo> standardProperties = null, Dictionary<string, ExtendedPropertyDefinition> extendedProperties = null, EntityDefinition definition = null, List<string> parentIDs = null)
{
if (string.IsNullOrWhiteSpace(this.Attribute))
return (null, null);
var statement = "";
var parameters = new Dictionary<string, object>();
var parentMappingProperty = definition?.GetParentMappingAttributeName();
if (definition != null && definition.IsGotMultipleParentMappings() && this.Attribute.Equals(parentMappingProperty) && (this.Operator.Equals(CompareOperator.Equals) || this.Operator.Equals(CompareOperator.Contains)))
{
var multipleParentMapColumn = definition.GetMultiParentMappingsAttribute().GetMapInfo(definition).Item3;
parentIDs?.ForEach((id, index) =>
{
suffix = $"{(string.IsNullOrWhiteSpace(suffix) ? "" : suffix)}_{index}";
statement += (statement.Equals("") ? "" : " OR ")
+ $"Origin.{this.Attribute}=@{this.Attribute}{suffix}"
+ $" OR Link.{multipleParentMapColumn}=@{this.Attribute}{suffix}_L";
parameters.Add($"@{this.Attribute}{suffix}", id);
parameters.Add($"@{this.Attribute}{suffix}_L", id);
});
statement = $"({statement})";
}
else
{
var column = extendedProperties != null && extendedProperties.ContainsKey(this.Attribute)
? extendedProperties[this.Attribute].Column
: standardProperties != null && standardProperties.ContainsKey(this.Attribute)
? !string.IsNullOrWhiteSpace(standardProperties[this.Attribute].Column)
? standardProperties[this.Attribute].Column
: standardProperties[this.Attribute].Name
: this.Attribute;
var name = this.Attribute + (string.IsNullOrEmpty(suffix) ? "" : suffix);
var @operator = "=";
var value = this.GetValue(standardProperties, extendedProperties);
var gotName = true;
switch (this.Operator)
{
case CompareOperator.Equals:
@operator = "=";
break;
case CompareOperator.NotEquals:
@operator = "<>";
break;
case CompareOperator.LessThan:
@operator = "<";
break;
case CompareOperator.LessThanOrEquals:
@operator = "<=";
break;
case CompareOperator.Greater:
@operator = ">";
break;
case CompareOperator.GreaterOrEquals:
@operator = ">=";
break;
case CompareOperator.Contains:
@operator = $"LIKE '%@{name}%'";
gotName = false;
break;
case CompareOperator.StartsWith:
@operator = $"LIKE '@{name}%'";
gotName = false;
break;
case CompareOperator.EndsWith:
@operator = $"LIKE '%@{name}'";
gotName = false;
break;
case CompareOperator.IsNull:
@operator = "IS NULL";
value = null;
gotName = false;
break;
case CompareOperator.IsNotNull:
@operator = "IS NOT NULL";
gotName = false;
value = null;
break;
case CompareOperator.IsEmpty:
@operator = "=";
value = "";
break;
case CompareOperator.IsNotEmpty:
@operator = "<>";
value = "";
break;
default:
break;
}
statement = (extendedProperties != null && extendedProperties.ContainsKey(this.Attribute) ? "Extent" : "Origin") + "." + column + @operator + (gotName ? $"@{name}" : "");
if (value != null)
parameters.Add($"@{name}", value);
}
return (statement, parameters);
}
public (string Statement, Dictionary<string, object> Parameters) GetSqlStatement()
=> this.GetSqlStatement(null);
#endregion
#region Working with No SQL
internal FilterDefinition<T> GetNoSqlStatement(Dictionary<string, AttributeInfo> standardProperties, Dictionary<string, ExtendedPropertyDefinition> extendedProperties, EntityDefinition definition = null, List<string> parentIDs = null)
{
if (string.IsNullOrWhiteSpace(this.Attribute))
return null;
var field = (extendedProperties != null && extendedProperties.ContainsKey(this.Attribute) ? "ExtendedProperties." : "") + this.Attribute;
FilterDefinition<T> filter = null;
var parentMappingProperty = definition?.GetParentMappingAttributeName();
if (definition != null && definition.IsGotMultipleParentMappings() && this.Attribute.Equals(parentMappingProperty) && (this.Operator.Equals(CompareOperator.Equals) || this.Operator.Equals(CompareOperator.Contains)))
{
var multipleParentPropertyName = definition.GetMultiParentMappingsAttributeName();
parentIDs?.ForEach(id =>
{
var filterBy = Builders<T>.Filter.Eq(parentMappingProperty, id) | Builders<T>.Filter.Eq(multipleParentPropertyName, id);
filter = filter == null
? filterBy
: filter | filterBy;
});
}
else
{
var value = this.GetValue(standardProperties, extendedProperties, false);
switch (this.Operator)
{
case CompareOperator.Equals:
filter = Builders<T>.Filter.Eq(field, value);
break;
case CompareOperator.NotEquals:
filter = Builders<T>.Filter.Ne(field, value);
break;
case CompareOperator.LessThan:
filter = Builders<T>.Filter.Lt(field, value);
break;
case CompareOperator.LessThanOrEquals:
filter = Builders<T>.Filter.Lte(field, value);
break;
case CompareOperator.Greater:
filter = Builders<T>.Filter.Gt(field, value);
break;
case CompareOperator.GreaterOrEquals:
filter = Builders<T>.Filter.Gte(field, value);
break;
case CompareOperator.Contains:
filter = value == null || !value.GetType().IsStringType() || value.Equals("")
? Builders<T>.Filter.Eq(field, "")
: Builders<T>.Filter.Regex(field, new BsonRegularExpression($"{value}", "i"));
break;
case CompareOperator.StartsWith:
filter = value == null || !value.GetType().IsStringType() || value.Equals("")
? Builders<T>.Filter.Eq(field, "")
: Builders<T>.Filter.Regex(field, new BsonRegularExpression($"^{value}", "im"));
break;
case CompareOperator.EndsWith:
filter = value == null || !value.GetType().IsStringType() || value.Equals("")
? Builders<T>.Filter.Eq(field, "")
: Builders<T>.Filter.Regex(field, new BsonRegularExpression($"{value}$", "im"));
break;
case CompareOperator.IsNull:
case CompareOperator.IsNotNull:
var type = standardProperties != null && standardProperties.TryGetValue(this.Attribute, out var standardAttribute)
? standardAttribute?.Type
: extendedProperties != null && extendedProperties.TryGetValue(this.Attribute, out var extendedAttribute)
? extendedAttribute?.Type
: null;
filter = this.Operator == CompareOperator.IsNull
? type != null && type.IsStringType()
? Builders<T>.Filter.Eq<string>(field, null)
: Builders<T>.Filter.Eq(field, BsonNull.Value)
: type != null && type.IsStringType()
? Builders<T>.Filter.Ne<string>(field, null)
: Builders<T>.Filter.Ne(field, BsonNull.Value);
break;
case CompareOperator.IsEmpty:
filter = Builders<T>.Filter.Eq(field, "");
break;
case CompareOperator.IsNotEmpty:
filter = Builders<T>.Filter.Ne(field, "");
break;
default:
break;
}
}
return filter;
}
/// <summary>
/// Gets the statement of No SQL
/// </summary>
/// <returns></returns>
public FilterDefinition<T> GetNoSqlStatement()
=> this.GetNoSqlStatement(null, null);
#endregion
}
// ------------------------------------------
/// <summary>
/// Presents a filtering expression for using with with combining operators (AND/OR)
/// </summary>
public class FilterBys : IFilterBy
{
/// <summary>
/// Initializes a group of filtering expressions
/// </summary>
public FilterBys() : this(null) { }
/// <summary>
/// Initializes a group of filtering expressions
/// </summary>
/// <param name="operator">The initializing operator</param>
/// <param name="children">The initializing child expressions</param>
public FilterBys(GroupOperator @operator = GroupOperator.And, List<IFilterBy> children = null)
: this(null, @operator, children) { }
/// <summary>
/// Initializes a group of filtering expressions
/// </summary>
/// <param name="json">The JSON to parse</param>
/// <param name="operator">The initializing operator</param>
/// <param name="children">The initializing child expressions</param>
public FilterBys(JObject json, GroupOperator @operator = GroupOperator.And, List<IFilterBy> children = null)
{
this.Operator = @operator;
this.Children = children ?? new List<IFilterBy>();
this.Parse(json);
}
/// <summary>
/// Gets or sets the operator
/// </summary>
[JsonConverter(typeof(StringEnumConverter)), BsonRepresentation(BsonType.String)]
public GroupOperator Operator { get; set; }
/// <summary>
/// Gets the collection of child expressions
/// </summary>
[MessagePackIgnore]
public List<IFilterBy> Children { get; }
/// <summary>
/// Adds a filtering expression into the collection of children
/// </summary>
/// <param name="filter">The filtering expression</param>
public void Add(IFilterBy filter)
{
if (filter != null)
this.Children.Add(filter);
}
public void Parse(JObject json)
{
if (json != null)
{
this.Operator = (json.Get<string>("Operator") ?? "And").TryToEnum(out GroupOperator op) ? op : GroupOperator.And;
json.Get<JArray>("Children")?.ForEach(cjson =>
{
var @operator = cjson.Get<string>("Operator");
if (!string.IsNullOrWhiteSpace(@operator))
this.Add(@operator.IsEquals("And") || @operator.IsEquals("Or") ? new FilterBys(cjson as JObject) : new FilterBy(cjson as JObject) as IFilterBy);
});
}
}
public JToken ToJson()
=> new JObject
{
{ "Operator", this.Operator.ToString() },
{ "Children", this.Children.Select(filter => filter.ToJson()).ToJArray() }
};
/// <summary>
/// Converts this filtering expression to JSON string
/// </summary>
/// <param name="formatting"></param>
/// <returns></returns>
public string ToString(Formatting formatting)
=> this.ToJson().ToString(formatting);
/// <summary>
/// Converts this filtering expression to JSON string
/// </summary>
/// <returns></returns>
public override string ToString()
=> this.ToString(Formatting.None);
}
// ------------------------------------------
/// <summary>
/// Presents a filtering expression for using with with combining operators (AND/OR)
/// </summary>
public class FilterBys<T> : FilterBys, IFilterBy<T> where T : class
{
/// <summary>
/// Initializes a group of filtering expressions
/// </summary>
/// <param name="operator">The initializing operator</param>
/// <param name="children">The initializing child expressions</param>
public FilterBys(GroupOperator @operator = GroupOperator.And, List<IFilterBy<T>> children = null)
: this(null, @operator, children) { }
/// <summary>
/// Initializes a group of filtering expressions
/// </summary>
/// <param name="json">The JSON to parse</param>
/// <param name="operator">The initializing operator</param>
/// <param name="children">The initializing child expressions</param>
public FilterBys(JObject json, GroupOperator @operator = GroupOperator.And, List<IFilterBy<T>> children = null)
: base(null, @operator, null)
{
children?.ForEach(filter => this.Add(filter));
this.Parse(json);
}
/// <summary>
/// Gets the collection of child expressions
/// </summary>
public new List<IFilterBy<T>> Children => base.Children.Select(filter => filter as IFilterBy<T>).ToList();
/// <summary>
/// Gets the filtering expression that mark as parent of this expression
/// </summary>
[JsonIgnore, XmlIgnore, MessagePackIgnore]
public FilterBys<T> Parent { get; internal set; }
/// <summary>
/// Adds a filtering expression into the collection of children
/// </summary>
/// <param name="filter">The filtering expression</param>
public void Add(IFilterBy<T> filter)
{
if (filter != null)
{
if (filter is FilterBy<T>)
(filter as FilterBy<T>).Parent = this;
else if (filter is FilterBys<T>)
(filter as FilterBys<T>).Parent = this;
base.Children.Add(filter);
}
}
/// <summary>
/// Adds a list of filtering expressions into the collection of children
/// </summary>
/// <param name="filters"></param>
public void Add(params IFilterBy<T>[] filters)
=> filters?.ForEach(item => this.Add(item));
/// <summary>
/// Adds a filtering expression into the collection of children
/// </summary>
/// <param name="filter">The filtering expression</param>
public new void Add(IFilterBy filter)
{
if (filter != null && filter is IFilterBy<T>)
this.Add(filter as IFilterBy<T>);
}
public new void Parse(JObject json)
{
if (json != null)
{
this.Operator = (json.Get<string>("Operator") ?? "And").TryToEnum(out GroupOperator op) ? op : GroupOperator.And;
json.Get<JArray>("Children")?.ForEach(cjson =>
{
var @operator = cjson.Get<string>("Operator");
if (!string.IsNullOrWhiteSpace(@operator))
this.Add(@operator.IsEquals("And") || @operator.IsEquals("Or") ? new FilterBys<T>(cjson as JObject) : new FilterBy<T>(cjson as JObject) as IFilterBy<T>);
});
}
}
#region Working with statement of SQL
internal (string Statement, Dictionary<string, object> Parameters) GetSqlStatement(string suffix, Dictionary<string, AttributeInfo> standardProperties = null, Dictionary<string, ExtendedPropertyDefinition> extendedProperties = null, EntityDefinition definition = null, List<string> parentIDs = null)
{
var children = this.Children;
if (children == null || children.Count < 1)
return (null, null);
if (children.Count.Equals(1))
return children[0] is FilterBys<T>
? (children[0] as FilterBys<T>).GetSqlStatement(suffix, standardProperties, extendedProperties, definition, parentIDs)
: (children[0] as FilterBy<T>).GetSqlStatement(suffix, standardProperties, extendedProperties, definition, parentIDs);
var statement = "";
var parameters = new Dictionary<string, object>();
children.ForEach((child, index) =>
{
var (Statement, Parameters) = child is FilterBys<T>
? (child as FilterBys<T>).GetSqlStatement((!string.IsNullOrEmpty(suffix) ? suffix : "") + "_" + index.ToString(), standardProperties, extendedProperties, definition, parentIDs)
: (child as FilterBy<T>).GetSqlStatement((!string.IsNullOrEmpty(suffix) ? suffix : "") + "_" + index.ToString(), standardProperties, extendedProperties, definition, parentIDs);
if (Statement != null && Parameters != null)
{
statement += (statement.Equals("") ? "" : this.Operator.Equals(GroupOperator.And) ? " AND " : " OR ") + Statement;
Parameters.ForEach(parameter => parameters.Add(parameter.Key, parameter.Value));
}
});
return statement.Equals("") || parameters.Count < 1
? (null, null)
: ((string.IsNullOrWhiteSpace(suffix) ? "" : "(") + statement + (string.IsNullOrWhiteSpace(suffix) ? "" : ")"), parameters);
}
public (string Statement, Dictionary<string, object> Parameters) GetSqlStatement()
=> this.GetSqlStatement(null);
#endregion
#region Working with statement of No SQL
internal FilterDefinition<T> GetNoSqlStatement(Dictionary<string, AttributeInfo> standardProperties, Dictionary<string, ExtendedPropertyDefinition> extendedProperties, EntityDefinition definition = null, List<string> parentIDs = null)
{
FilterDefinition<T> filter = null;
var children = this.Children;
if (children == null || children.Count < 1)
filter = Builders<T>.Filter.Empty;
else if (children.Count.Equals(1))
filter = children[0] is FilterBys<T>
? (children[0] as FilterBys<T>).GetNoSqlStatement(standardProperties, extendedProperties, definition, parentIDs)
: (children[0] as FilterBy<T>).GetNoSqlStatement(standardProperties, extendedProperties, definition, parentIDs);
else
children.ForEach(child =>
{
var childFilter = child is FilterBys<T>
? (child as FilterBys<T>).GetNoSqlStatement(standardProperties, extendedProperties, definition, parentIDs)
: (child as FilterBy<T>).GetNoSqlStatement(standardProperties, extendedProperties, definition, parentIDs);
if (childFilter != null)
filter = filter == null
? childFilter
: this.Operator.Equals(GroupOperator.And)
? filter & childFilter
: filter | childFilter;
});
return filter;
}
/// <summary>
/// Gets the statement of No SQL
/// </summary>
/// <returns></returns>
public FilterDefinition<T> GetNoSqlStatement()
=> this.GetNoSqlStatement(null, null);
#endregion
}
// ------------------------------------------
/// <summary>
/// Wrapper of all available filtering expressions
/// </summary>
public static class Filters<T> where T : class
{
#region Group
/// <summary>
/// Creates a group of filter-by expressions with AND operator
/// </summary>
/// <param name="filters"></param>
/// <returns></returns>
public static FilterBys<T> And(params IFilterBy<T>[] filters)
{
var filter = new FilterBys<T>(GroupOperator.And);
filters?.ForEach(item => filter.Add(item));
return filter;
}
/// <summary>
/// Creates a group of filter-by expressions with AND operator
/// </summary>
/// <param name="filters"></param>
/// <returns></returns>
public static FilterBys<T> And(IEnumerable<IFilterBy<T>> filters)
{
return new FilterBys<T>(GroupOperator.And, filters?.ToList());
}
/// <summary>
/// Creates a group of filter-by expressions with OR operator
/// </summary>
/// <param name="filters"></param>
/// <returns></returns>
public static FilterBys<T> Or(params IFilterBy<T>[] filters)
{
var filter = new FilterBys<T>(GroupOperator.Or);
filters?.ForEach(item => filter.Add(item));
return filter;
}
/// <summary>
/// Creates a group of filter-by expressions with OR operator
/// </summary>
/// <param name="filters"></param>
/// <returns></returns>
public static FilterBys<T> Or(IEnumerable<IFilterBy<T>> filters)
=> new FilterBys<T>(GroupOperator.Or, filters?.ToList());
#endregion
#region Compare
/// <summary>
/// Creates a filter-by expressions with EQUALS operator (==)
/// </summary>
/// <param name="attribute"></param>
/// <param name="value"></param>
/// <returns></returns>
public static FilterBy<T> Equals(string attribute, object value)
=> new FilterBy<T>(attribute, CompareOperator.Equals, value);
/// <summary>
/// Creates a filter-by expressions with NOT-EQUALS operator (!=)
/// </summary>
/// <param name="attribute"></param>
/// <param name="value"></param>
/// <returns></returns>
public static FilterBy<T> NotEquals(string attribute, object value)
=> new FilterBy<T>(attribute, CompareOperator.NotEquals, value);
/// <summary>
/// Creates a filter-by expressions with LESS THAN operator (<)
/// </summary>
/// <param name="attribute"></param>
/// <param name="value"></param>
/// <returns></returns>
public static FilterBy<T> LessThan(string attribute, object value)
=> new FilterBy<T>(attribute, CompareOperator.LessThan, value);
/// <summary>
/// Creates a filter-by expressions with LESS THAN or EQUALS operator (<=)
/// </summary>
/// <param name="attribute"></param>
/// <param name="value"></param>
/// <returns></returns>
public static FilterBy<T> LessThanOrEquals(string attribute, object value)
=> new FilterBy<T>(attribute, CompareOperator.LessThanOrEquals, value);
/// <summary>
/// Creates a filter-by expressions with GREATER operator (>)
/// </summary>
/// <param name="attribute"></param>
/// <param name="value"></param>
/// <returns></returns>
public static FilterBy<T> Greater(string attribute, object value)
=> new FilterBy<T>(attribute, CompareOperator.Greater, value);
/// <summary>
/// Creates a filter-by expressions with GREATER or EQUALS operator (>=)
/// </summary>
/// <param name="attribute"></param>
/// <param name="value"></param>
/// <returns></returns>
public static FilterBy<T> GreaterOrEquals(string attribute, object value)
=> new FilterBy<T>(attribute, CompareOperator.GreaterOrEquals, value);
/// <summary>
/// Creates a filter-by expressions with CONTAINS operator (means the value of attribute must contains the sub-string)
/// </summary>
/// <param name="attribute"></param>
/// <param name="value"></param>
/// <returns></returns>
public static FilterBy<T> Contains(string attribute, object value)
=> new FilterBy<T>(attribute, CompareOperator.Contains, value);
/// <summary>
/// Creates a filter-by expressions with STARTS WITH operator (means the value of attribute must starts with the sub-string)
/// </summary>
/// <param name="attribute"></param>
/// <param name="value"></param>
/// <returns></returns>
public static FilterBy<T> StartsWith(string attribute, string value)
=> new FilterBy<T>(attribute, CompareOperator.StartsWith, value);
/// <summary>
/// Creates a filter-by expressions with ENDS WITH operator (means the value of attribute must ends with the sub-string)
/// </summary>
/// <param name="attribute"></param>
/// <param name="value"></param>
/// <returns></returns>
public static FilterBy<T> EndsWith(string attribute, string value)
=> new FilterBy<T>(attribute, CompareOperator.EndsWith, value);
/// <summary>
/// Creates a filter-by expressions with IS NULL operator (IS NULL)
/// </summary>
/// <param name="attribute"></param>
/// <returns></returns>
public static FilterBy<T> IsNull(string attribute)
=> new FilterBy<T>(attribute, CompareOperator.IsNull, null);
/// <summary>
/// Creates a filter-by expressions with IS NOT NULL operator (IS NOT NULL)
/// </summary>
/// <param name="attribute"></param>
/// <returns></returns>
public static FilterBy<T> IsNotNull(string attribute)
=> new FilterBy<T>(attribute, CompareOperator.IsNotNull, null);
/// <summary>
/// Creates a filter-by expressions with IS EMPTY operator (=='')
/// </summary>
/// <param name="attribute"></param>
/// <returns></returns>
public static FilterBy<T> IsEmpty(string attribute)
=> new FilterBy<T>(attribute, CompareOperator.IsEmpty, null);
/// <summary>
/// Creates a filter-by expressions with IS NOT EMPTY operator (!='')
/// </summary>
/// <param name="attribute"></param>
/// <returns></returns>
public static FilterBy<T> IsNotEmpty(string attribute)
=> new FilterBy<T>(attribute, CompareOperator.IsNotEmpty, null);
#endregion
}
// ------------------------------------------
/// <summary>
/// Presents a sorting expression
/// </summary>
public class SortBy : ISortBy
{
/// <summary>
/// Initializes a sorting expression
/// </summary>
public SortBy() : this(null, null) { }
/// <summary>
/// Initializes a sorting expression
/// </summary>
/// <param name="attribute">The sorting-by attribute</param>
/// <param name="mode">The sorting mode</param>
public SortBy(string attribute = null, SortMode mode = SortMode.Ascending)
: this(null, attribute, mode) { }
/// <summary>
/// Initializes a sorting expression
/// </summary>
/// <param name="json">The JSON object that contains the expression</param>
/// <param name="attribute">The sorting-by attribute</param>
/// <param name="mode">The sorting mode</param>
public SortBy(JObject json, string attribute = null, SortMode mode = SortMode.Ascending)
{
this.Attribute = attribute;
this.Mode = mode;
this.Parse(json);
}
public string Attribute { get; set; }
[JsonConverter(typeof(StringEnumConverter)), BsonRepresentation(BsonType.String)]
public SortMode Mode { get; set; }
[MessagePackIgnore]
public ISortBy ThenBy { get; set; }
public void Parse(JObject json)
{
if (json != null)
{
this.Attribute = json.Get<string>("Attribute");
this.Mode = (json.Get<string>("Mode") ?? "Ascending").TryToEnum(out SortMode sortMode) ? sortMode : SortMode.Ascending;
var thenBy = json.Get<JObject>("ThenBy");
this.ThenBy = thenBy != null
? new SortBy(thenBy)
: null;
}
}
public JToken ToJson()
=> new JObject
{
{ "Attribute", this.Attribute },
{ "Mode", this.Mode.ToString() },
{ "ThenBy", this.ThenBy?.ToJson() }
};
/// <summary>
/// Converts this sorting expression to JSON string
/// </summary>
/// <param name="formatting"></param>
/// <returns></returns>
public string ToString(Formatting formatting)
=> this.ToJson().ToString(formatting);
/// <summary>
/// Converts this sorting expression to JSON string
/// </summary>
/// <returns></returns>
public override string ToString()
=> this.ToString(Formatting.None);
}
// ------------------------------------------
/// <summary>
/// Presents a sorting expression
/// </summary>
public class SortBy<T> : SortBy, ISortBy<T> where T : class
{
/// <summary>
/// Initializes a sorting expression
/// </summary>
/// <param name="attribute">The sorting-by attribute</param>
/// <param name="mode">The sorting mode</param>
public SortBy(string attribute = null, SortMode mode = SortMode.Ascending)
: base(attribute, mode) { }
/// <summary>
/// Initializes a sorting expression
/// </summary>
/// <param name="json">The JSON object that contains the expression</param>
/// <param name="attribute">The sorting-by attribute</param>
/// <param name="mode">The sorting mode</param>
public SortBy(JObject json, string attribute = null, SortMode mode = SortMode.Ascending)
: base(null, attribute, mode)
=> this.Parse(json);
ISortBy<T> _thenBy;
/// <summary>
/// Gets or sets the next-sibling
/// </summary>
[MessagePackIgnore]
public new ISortBy<T> ThenBy
{
get => this._thenBy ?? (this._thenBy = base.ThenBy != null && base.ThenBy is ISortBy<T> ? base.ThenBy as ISortBy<T> : null);
set => base.ThenBy = this._thenBy = value;
}
public new void Parse(JObject json)
{
if (json != null)
{
this.Attribute = json.Get<string>("Attribute");
this.Mode = (json.Get<string>("Mode") ?? "Ascending").TryToEnum(out SortMode sortMode) ? sortMode : SortMode.Ascending;
var thenBy = json.Get<JObject>("ThenBy");
this.ThenBy = thenBy != null
? new SortBy<T>(thenBy)
: null;
}
}
/// <summary>
/// Gets the listing of all attributes that use to sort
/// </summary>
/// <returns></returns>
internal List<string> GetAttributes()
=> new[] { this.Attribute }.Concat((this.ThenBy as SortBy<T>)?.GetAttributes() ?? new List<string>()).ToList();
/// <summary>
/// Gets the statement of SQL
/// </summary>
/// <param name="standardProperties"></param>
/// <param name="extendedProperties"></param>
/// <returns></returns>
internal string GetSqlStatement(Dictionary<string, AttributeInfo> standardProperties, Dictionary<string, ExtendedPropertyDefinition> extendedProperties)
{
if (!string.IsNullOrWhiteSpace(this.Attribute))
{
var next = (this.ThenBy as SortBy<T>)?.GetSqlStatement(standardProperties, extendedProperties);
return this.Attribute + (this.Mode.Equals(SortMode.Ascending) ? " ASC" : " DESC") + (string.IsNullOrWhiteSpace(next) ? "" : $", {next}");
}