forked from exilon/QuickLib
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Quick.Linq.pas
831 lines (747 loc) · 22.3 KB
/
Quick.Linq.pas
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
{ ***************************************************************************
Copyright (c) 2016-2019 Kike Pérez
Unit : Quick.Linq
Description : Arrays and Generic Lists Linq functions
Author : Kike Pérez
Version : 1.0
Created : 04/04/2019
Modified : 22/03/20120
This file is part of QuickLib: https://github.com/exilon/QuickLib
***************************************************************************
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*************************************************************************** }
unit Quick.Linq;
{$i QuickLib.inc}
interface
uses
SysUtils,
Generics.Collections,
Generics.Defaults,
RTTI,
Quick.RTTI.Utils,
Quick.Expression,
Quick.Commons,
Quick.Value,
{$IFDEF FPC}
Quick.Value.RTTI,
{$ELSE}
System.RegularExpressions,
{$ENDIF}
Quick.Arrays;
type
TOrderDirection = (odAscending, odDescending);
{$IFNDEF FPC}
TLinqExpression<T : class> = class(TExpression)
private
fPredicate : TPredicate<T>;
public
constructor Create(aPredicate : TPredicate<T>);
function Validate(const aValue : TValue) : Boolean; override;
function IsNull : Boolean; override;
end;
{$ENDIF}
ILinqArray<T> = interface
['{3133DCAB-06C5-434B-B169-B32DC8C6308B}']
function Any : Boolean; overload;
function Any(const aMatchString : string; aUseRegEx : Boolean) : Boolean; overload;
function Where(const aMatchString : string; aUseRegEx : Boolean) : ILinqArray<T>; overload;
function OrderAscending : ILinqArray<T>;
function OrderDescending : ILinqArray<T>;
function SelectFirst : T;
function SelectLast : T;
function SelectTop(aLimit : Integer) : TArray<T>;
function Select : TArray<T>; overload;
function Count : Integer;
function Update(const aNewValue : T) : Integer;
function Delete : Integer;
end;
{$IFNDEF FPC}
TLinqArray<T> = class(TInterfacedObject,ILinqArray<T>)
type
TLinqComparer = class(TInterfacedObject,IComparer<T>)
private
fSortAscending : Boolean;
public
constructor Create(aSortAscending : Boolean);
property SortAscending : Boolean read fSortAscending;
function Compare(const L, R: T): Integer;
end;
private
fArray : TArray<T>;
fMatchString : string;
fUseRegEx : Boolean;
function Validate(aValue : T) : Boolean;
public
constructor Create(aArray : TArray<T>);
function Any : Boolean; overload;
function Any(const aMatchString : string; aUseRegEx : Boolean) : Boolean; overload;
function Where(const aMatchString : string; aUseRegEx : Boolean) : ILinqArray<T>; overload;
function OrderAscending : ILinqArray<T>;
function OrderDescending : ILinqArray<T>;
function SelectFirst : T;
function SelectLast : T;
function SelectTop(aLimit : Integer) : TArray<T>;
function Select : TArray<T>; overload;
function Count : Integer;
function Update(const aNewValue : T) : Integer;
function Delete : Integer;
end;
TLinqArrayHelper = record helper for TArray<string>
function Add(const aValue : string) : Integer;
function AddIfNotExists(const aValue : string) : Integer;
function Remove(const aValue : string) : Boolean;
function Any : Boolean; overload;
function Any(const aValue : string) : Boolean; overload;
function Any(const aMatchString : string; aUseRegEx : Boolean) : Boolean; overload;
function Where(const aMatchString : string; aUseRegEx : Boolean) : ILinqArray<string>; overload;
end;
{$ENDIF}
ILinqQuery<T> = interface
['{16B68C0B-EA38-488A-99D9-BAD1E8560E8E}']
function Where(const aWhereClause : string; aWhereValues : array of const) : ILinqQuery<T>; overload;
function Where(const aWhereClause: string): ILinqQuery<T>; overload;
{$IFNDEF FPC}
function Where(aPredicate : TPredicate<T>) : ILinqQuery<T>; overload;
{$ENDIF}
function OrderBy(const aFieldNames : string) : ILinqQuery<T>;
function OrderByDescending(const aFieldNames : string) : ILinqQuery<T>;
function SelectFirst : T;
function SelectLast : T;
function SelectTop(aLimit : Integer) : TxArray<T>;
function Select : TxArray<T>; overload;
function Select(const aPropertyName : string) : TFlexArray; overload;
function Count : Integer;
function Update(const aFields : array of string; aValues : array of const) : Integer;
function Delete : Integer;
end;
TLinqQuery<T : class> = class(TInterfacedObject,ILinqQuery<T>)
private type
arrayOfT = array of T;
private
fWhereClause : TExpression;
fOrderBy : TArray<string>;
fOrderDirection : TOrderDirection;
//fPList : Pointer;
fList : arrayOfT;
function FormatParams(const aWhereClause : string; aWhereParams : array of const) : string;
procedure DoOrderBy(vArray : ArrayOfT);
function Compare(const aPropertyName : string; L, R : T) : Integer;
procedure Clear;
public
{$IFNDEF FPC}
constructor Create(aObjectList : TObjectList<T>); overload;
{$ENDIF}
constructor Create(aList : TList<T>); overload;
constructor Create(aXArray : TxArray<T>); overload;
constructor Create(aArray : TArray<T>); overload;
destructor Destroy; override;
function Where(const aWhereClause : string; aWhereParams : array of const) : ILinqQuery<T>; overload;
function Where(const aWhereClause: string): ILinqQuery<T>; overload;
{$IFNDEF FPC}
function Where(aPredicate : TPredicate<T>) : ILinqQuery<T>; overload;
{$ENDIF}
function OrderBy(const aFieldNames : string) : ILinqQuery<T>;
function OrderByDescending(const aFieldNames : string) : ILinqQuery<T>;
function SelectFirst : T;
function SelectLast : T;
function SelectTop(aLimit : Integer) : TxArray<T>;
function Select : TxArray<T>; overload;
function Select(const aPropertyName : string) : TFlexArray; overload;
function Count : Integer;
function Update(const aFields : array of string; aValues : array of const) : Integer;
function Delete : Integer;
end;
TLinq<T : class> = class
public
{$IFNDEF FPC}
class function From(aObjectList : TObjectList<T>) : ILinqQuery<T>; overload;
{$ENDIF}
class function From(aArray : TArray<T>) : ILinqQuery<T>; overload;
class function From(aXArray : TXArray<T>) : ILinqQuery<T>; overload;
end;
ELinqNotValidExpression = class(Exception);
ELinqError = class(Exception);
implementation
{ TLinqQuery<T> }
procedure TLinqQuery<T>.Clear;
begin
SetLength(fOrderBy,0);
end;
constructor TLinqQuery<T>.Create(aArray: TArray<T>);
begin
Clear;
fList := aArray;
end;
{$IFNDEF FPC}
constructor TLinqQuery<T>.Create(aObjectList: TObjectList<T>);
begin
Clear;
//Create(aObjectList.List);
//fPList := Pointer(aObjectList.List);
//fList := arrayOfT(fPList);
fList := aObjectList.List;
end;
{$ENDIF}
constructor TLinqQuery<T>.Create(aXArray: TxArray<T>);
begin
Clear;
fList := aXArray;
end;
constructor TLinqQuery<T>.Create(aList: TList<T>);
begin
Clear;
fList := aList.ToArray;
end;
function TLinqQuery<T>.Compare(const aPropertyName: string; L, R: T): Integer;
var
valueL : TValue;
valueR : TValue;
begin
Result := 0;
valueL := TRTTI.GetPathValue(L,aPropertyName);
valueR := TRTTI.GetPathValue(R,aPropertyName);
if (valueL.IsEmpty) and (not valueR.IsEmpty) then Exit(1)
else if (not valueL.IsEmpty) and (valueR.IsEmpty) then Exit(-1);
case valueL.Kind of
{$IFNDEF FPC}
tkString,
{$ENDIF}
tkChar, tkWString, tkUString : Result := CompareText(valueL.AsString, valueR.AsString);
tkInteger, tkInt64 :
begin
if valueL.AsInteger > valueR.AsInteger then Result := 1
else if valueL.AsInteger < valueR.AsInteger then Result := -1;
end;
tkFloat :
begin
if valueL.AsExtended > valueR.AsExtended then Result := 1
else if valueL.AsExtended < valueR.AsExtended then Result := -1;
end;
end;
end;
function TLinqQuery<T>.Count: Integer;
var
i : Integer;
begin
Result := 0;
if fWhereClause = nil then raise ELinqNotValidExpression.Create('Not valid expression defined!');
for i := High(fList) downto Low(fList) do
begin
if fWhereClause.Validate(fList[i]) then
begin
Inc(Result);
end;
end;
end;
function TLinqQuery<T>.Delete: Integer;
var
i : Integer;
begin
Result := 0;
if fWhereClause = nil then raise ELinqNotValidExpression.Create('Not valid expression defined!');
for i := High(fList) downto Low(fList) do
begin
if fWhereClause.Validate(fList[i]) then
begin
TObject(fList[i]).Free;
//System.Delete(fList,i,1);
Inc(Result);
end;
end;
end;
destructor TLinqQuery<T>.Destroy;
begin
if Assigned(fWhereClause) then fWhereClause.Free;
inherited;
end;
procedure TLinqQuery<T>.DoOrderBy(vArray : ArrayOfT);
begin
if High(fOrderBy) < 0 then Exit;
{$IFNDEF FPC}
TArray.Sort<T>(vArray, TComparer<T>.Construct(
function (const A, B: T): Integer
var
field : string;
begin
for field in fOrderBy do
begin
Result := Compare(field,A,B);
if Result <> 0 then Break;
end;
if fOrderDirection = TOrderDirection.odDescending then Result := Result * -1;
end)
);
{$ENDIF}
end;
function TLinqQuery<T>.OrderBy(const aFieldNames: string): ILinqQuery<T>;
begin
Result := Self;
if aFieldNames = '' then raise ELinqError.Create('No order fields specified!');
fOrderBy := aFieldNames.Split([',']);
fOrderDirection := TOrderDirection.odAscending;
end;
function TLinqQuery<T>.OrderByDescending(const aFieldNames: string): ILinqQuery<T>;
begin
Result := Self;
if aFieldNames = '' then raise ELinqError.Create('No order fields specified!');
fOrderBy := aFieldNames.Split([',']);
fOrderDirection := TOrderDirection.odDescending;
end;
function TLinqQuery<T>.Select(const aPropertyName: string): TFlexArray;
var
obj : T;
value : TFlexValue;
begin
if fWhereClause = nil then raise ELinqNotValidExpression.Create('Not valid expression defined!');
for obj in fList do
begin
if fWhereClause.Validate(obj) then
begin
//value := TRTTI.GetProperty(obj,aPropertyName);
{$IFNDEF FPC}
value := TRTTI.GetPathValue(obj,aPropertyName).AsVariant;
{$ELSE}
value.AsTValue := TRTTI.GetPathValue(obj,aPropertyName);
{$ENDIF}
Result.Add(value);
end;
end;
end;
function TLinqQuery<T>.Select: TxArray<T>;
var
obj : T;
begin
if fWhereClause = nil then raise ELinqNotValidExpression.Create('Not valid expression defined!');
for obj in fList do
begin
if fWhereClause.Validate(obj) then Result.Add(obj);
end;
DoOrderBy(Result);
end;
function TLinqQuery<T>.SelectFirst: T;
var
obj : T;
begin
{$IFNDEF FPC}
Result := nil;
{$ENDIF}
DoOrderBy(fList);
if fWhereClause = nil then raise ELinqNotValidExpression.Create('Not valid expression defined!');
for obj in fList do
begin
if fWhereClause.Validate(obj) then Exit(obj);
end;
end;
function TLinqQuery<T>.SelectLast: T;
var
obj : T;
begin
{$IFNDEF FPC}
Result := nil;
{$ENDIF}
DoOrderBy(fList);
if fWhereClause = nil then raise ELinqNotValidExpression.Create('Not valid expression defined!');
for obj in fList do
begin
if fWhereClause.Validate(obj) then Result := obj;
end;
end;
function TLinqQuery<T>.SelectTop(aLimit: Integer): TxArray<T>;
var
obj : T;
i : Integer;
begin
DoOrderBy(fList);
if fWhereClause = nil then raise ELinqNotValidExpression.Create('Not valid expression defined!');
i := 0;
for obj in fList do
begin
if fWhereClause.Validate(obj) then
begin
Result.Add(obj);
Inc(i);
if i > aLimit then Exit;
end;
end;
end;
function TLinqQuery<T>.Update(const aFields: array of string; aValues: array of const): Integer;
var
obj : T;
i : Integer;
{$IFDEF FPC}
value : TValue;
{$ENDIF}
begin
Result := 0;
if fWhereClause = nil then raise ELinqNotValidExpression.Create('Not valid expression defined!');
for obj in fList do
begin
{$IFNDEF FPC}
if obj = nil then continue;
{$ELSE}
if Pointer(obj) = nil then continue;
{$ENDIF}
if fWhereClause.Validate(obj) then
begin
for i := Low(aFields) to High(aFields) do
begin
if not TRTTI.PropertyExists(TypeInfo(T),aFields[i]) then raise ELinqError.CreateFmt('Linq update field "%s" not found in obj',[aFields[i]]);
try
{$IFNDEF FPC}
TRTTI.SetPropertyValue(obj,aFields[i],aValues[i]);
{$ELSE}
case aValues[i].VType of
vtString : value := string(aValues[i].VString^);
vtChar : value := string(aValues[i].VChar);
{$IFDEF MSWINDOWS}
vtAnsiString : value := AnsiString(aValues[i].VAnsiString);
vtWideString : value := WideString(aValues[i].VWideString);
{$ENDIF}
{$IFDEF UNICODE}
vtUnicodeString: AsString := string(aValues[i].VUnicodeString);
{$ENDIF UNICODE}
vtInteger : value := aValues[i].VInteger;
vtInt64 : value := aValues[i].VInt64^;
vtExtended : value := aValues[i].VExtended^;
vtBoolean : value := aValues[i].VBoolean;
else raise Exception.Create('DataType not supported by Linq update');
end;
TRTTI.SetPropertyValue(obj,aFields[i],value);
{$ENDIF}
except
on E : Exception do raise ELinqError.CreateFmt('Linq update error: %s',[e.Message]);
end;
end;
Inc(Result);
end;
end;
end;
function TLinqQuery<T>.FormatParams(const aWhereClause : string; aWhereParams : array of const) : string;
var
i : Integer;
begin
Result := aWhereClause;
if aWhereClause = '' then
begin
Result := '1 = 1';
Exit;
end;
for i := 0 to aWhereClause.CountChar('?') - 1 do
begin
case aWhereParams[i].VType of
vtInteger : Result := StringReplace(Result,'?',IntToStr(aWhereParams[i].VInteger),[]);
vtInt64 : Result := StringReplace(Result,'?',IntToStr(aWhereParams[i].VInt64^),[]);
vtExtended : Result := StringReplace(Result,'?',FloatToStr(aWhereParams[i].VExtended^),[]);
vtBoolean : Result := StringReplace(Result,'?',BoolToStr(aWhereParams[i].VBoolean),[]);
vtAnsiString : Result := StringReplace(Result,'?',string(aWhereParams[i].VAnsiString),[]);
vtWideString : Result := StringReplace(Result,'?',string(aWhereParams[i].VWideString^),[]);
{$IFNDEF NEXTGEN}
vtString : Result := StringReplace(Result,'?',aWhereParams[i].VString^,[]);
{$ENDIF}
vtChar : Result := StringReplace(Result,'?',aWhereParams[i].VChar,[]);
vtPChar : Result := StringReplace(Result,'?',string(aWhereParams[i].VPChar),[]);
else Result := StringReplace(Result,'?', DbQuotedStr(string(aWhereParams[i].VUnicodeString)),[]);
end;
end;
end;
function TLinqQuery<T>.Where(const aWhereClause: string; aWhereParams: array of const): ILinqQuery<T>;
begin
Result := Where(FormatParams(aWhereClause,aWhereParams));
end;
function TLinqQuery<T>.Where(const aWhereClause: string): ILinqQuery<T>;
begin
Result := Self;
try
fWhereClause := TExpressionParser.Parse(aWhereClause);
except
on E : Exception do raise ELinqNotValidExpression.Create(e.Message);
end;
end;
{$IFNDEF FPC}
function TLinqQuery<T>.Where(aPredicate: TPredicate<T>): ILinqQuery<T>;
begin
Result := Self;
fWhereClause := TLinqExpression<T>.Create(aPredicate);
end;
{$ENDIF}
{ TLinq }
{$IFNDEF FPC}
class function TLinq<T>.From(aObjectList: TObjectList<T>): ILinqQuery<T>;
begin
Result := TLinqQuery<T>.Create(aObjectList);
end;
{$ENDIF}
class function TLinq<T>.From(aArray: TArray<T>): ILinqQuery<T>;
begin
Result := TLinqQuery<T>.Create(aArray);
end;
class function TLinq<T>.From(aXArray : TXArray<T>) : ILinqQuery<T>;
begin
Result := TLinqQuery<T>.Create(aXArray);
end;
{ TLinqExpression<T> }
{$IFNDEF FPC}
constructor TLinqExpression<T>.Create(aPredicate: TPredicate<T>);
begin
fPredicate := aPredicate;
end;
function TLinqExpression<T>.Validate(const aValue : TValue) : Boolean;
begin
Result := fPredicate(aValue.AsType<T>);
end;
function TLinqExpression<T>.IsNull : Boolean;
begin
Result := not Assigned(fPredicate);
end;
{ TLinqArray<T> }
function TLinqArray<T>.Any : Boolean;
begin
Result := High(fArray) >= 0;
end;
function TLinqArray<T>.Any(const aMatchString: string; aUseRegEx: Boolean): Boolean;
begin
fMatchString := aMatchString;
fUseRegEx := aUseRegEx;
end;
function TLinqArray<T>.Count: Integer;
begin
Result := High(fArray) + 1;
end;
constructor TLinqArray<T>.Create(aArray: TArray<T>);
begin
fArray := aArray;
end;
function TLinqArray<T>.Delete: Integer;
var
i : Integer;
{$IFNDEF DELPHIXE7_UP}
n : Integer;
len : Integer;
{$ENDIF}
begin
Result := 0;
if fMatchString.IsEmpty then raise ELinqNotValidExpression.Create('Not valid expression defined!');
for i := High(fArray) downto Low(fArray) do
begin
if Validate(fArray[i]) then
begin
//TObject(fArray[i]).Free;
{$IFDEF DELPHIXE7_UP}
System.Delete(fArray,i,1);
{$ELSE}
len := Length(fArray);
if (len > 0) and (i < len) then
begin
for n := i + 1 to len - 1 do fArray[n - 1] := fArray[n];
SetLength(fArray, len - 1);
end;
{$ENDIF}
Inc(Result);
end;
end;
end;
function TLinqArray<T>.OrderAscending: ILinqArray<T>;
var
comparer : IComparer<T>;
begin
comparer := TLinqComparer.Create(True);
TArray.Sort<T>(fArray,comparer);
end;
function TLinqArray<T>.OrderDescending: ILinqArray<T>;
var
comparer : IComparer<T>;
begin
comparer := TLinqComparer.Create(False);
TArray.Sort<T>(fArray,comparer);
end;
function TLinqArray<T>.Select: TArray<T>;
var
value : T;
begin
//DoOrderBy(fList);
if fMatchString.IsEmpty then raise ELinqNotValidExpression.Create('Not valid expression defined!');
for value in fArray do
begin
if Validate(value) then Result := Result + [value];
end;
end;
function TLinqArray<T>.SelectFirst: T;
var
value : T;
begin
//DoOrderBy(fList);
if fMatchString.IsEmpty then raise ELinqNotValidExpression.Create('Not valid expression defined!');
for value in fArray do
begin
if Validate(value) then Exit(value);
end;
end;
function TLinqArray<T>.SelectLast: T;
var
value : T;
found : T;
begin
//DoOrderBy(fList);
if fMatchString.IsEmpty then raise ELinqNotValidExpression.Create('Not valid expression defined!');
for value in fArray do
begin
if Validate(value) then found := value;
end;
Result := found;
end;
function TLinqArray<T>.SelectTop(aLimit: Integer): TArray<T>;
var
i : Integer;
limit : Integer;
begin
if aLimit > High(fArray) then limit := High(fArray)
else limit := aLimit;
SetLength(Result,limit);
for i := Low(fArray) to limit do
begin
Result[i] := fArray[i];
end;
end;
function TLinqArray<T>.Update(const aNewValue: T): Integer;
var
i : Integer;
begin
for i := Low(fArray) to High(fArray) do
begin
if Validate(fArray[i]) then fArray[i] := aNewValue;
end;
end;
function TLinqArray<T>.Validate(aValue: T): Boolean;
var
regEx : TRegEx;
value : TValue;
begin
value := TValue.From<T>(aValue);
if fUseRegEx then
begin
regEx := TRegEx.Create(fMatchString,[roIgnoreCase,roMultiline]);
try
Result := regEx.IsMatch(value.AsString);
except
raise Exception.Create('TLinqArray not valid RegEx!');
end;
end
else
begin
Result := CompareText(fMatchString,value.AsString) = 0;
end;
end;
function TLinqArray<T>.Where(const aMatchString: string; aUseRegEx: Boolean): ILinqArray<T>;
begin
Result := Self;
fMatchString := aMatchString;
fUseRegEx := aUseRegEx;
end;
{ TLinqArray<T>.TLinqComparer }
constructor TLinqArray<T>.TLinqComparer.Create(aSortAscending : Boolean);
begin
fSortAscending := aSortAscending;
end;
function TLinqArray<T>.TLinqComparer.Compare(const L, R: T): Integer;
var
valueL : TValue;
valueR : TValue;
hr : Integer;
lr : Integer;
begin
Result := 0;
if fSortAscending then
begin
hr := 1;
lr := -1;
end
else
begin
hr := -1;
lr := 1;
end;
valueL := TValue.From<T>(L);
valueR := TValue.From<T>(R);
if (valueL.IsEmpty) and (not valueR.IsEmpty) then Exit(hr)
else if (not valueL.IsEmpty) and (valueR.IsEmpty) then Exit(lr);
case valueL.Kind of
{$IFNDEF FPC}
tkString,
{$ENDIF}
tkChar, tkWString, tkUString : Result := CompareText(valueL.AsString, valueR.AsString);
tkInteger, tkInt64 :
begin
if valueL.AsInteger > valueR.AsInteger then Result := hr
else if valueL.AsInteger < valueR.AsInteger then Result := lr;
end;
tkFloat :
begin
if valueL.AsExtended > valueR.AsExtended then Result := hr
else if valueL.AsExtended < valueR.AsExtended then Result := lr;
end;
end;
end;
{ TLinqArrayHelper }
function TLinqArrayHelper.Add(const aValue : string) : Integer;
begin
SetLength(Self,Length(Self)+1);
Self[High(Self)] := aValue;
Result := High(Self);
end;
function TLinqArrayHelper.AddIfNotExists(const aValue : string) : Integer;
var
i : Integer;
begin
for i := Low(Self) to High(Self) do
begin
if CompareText(Self[i],aValue) = 0 then Exit(i);
end;
//if not exists add it
Result := Self.Add(aValue);
end;
function TLinqArrayHelper.Remove(const aValue : string) : Boolean;
var
i : Integer;
begin
for i := Low(Self) to High(Self) do
begin
if CompareText(Self[i],aValue) = 0 then
begin
System.Delete(Self,i,1);
Exit(True);
end;
end;
Result := False;
end;
function TLinqArrayHelper.Any : Boolean;
begin
Result := High(Self) >= 0;
end;
function TLinqArrayHelper.Any(const aValue : string) : Boolean;
var
value : string;
begin
Result := False;
for value in Self do
begin
if CompareText(value,aValue) = 0 then Exit(True)
end;
end;
function TLinqArrayHelper.Any(const aMatchString : string; aUseRegEx : Boolean) : Boolean;
begin
Result := TLinqArray<string>.Create(Self).Any(aMatchString,aUseRegEx);
end;
function TLinqArrayHelper.Where(const aMatchString : string; aUseRegEx : Boolean) : ILinqArray<string>;
begin
Result := TLinqArray<string>.Create(Self).Where(aMatchString,aUseRegEx);
end;
{$ENDIF}
end.