-
Notifications
You must be signed in to change notification settings - Fork 27
Expand file tree
/
Copy pathGpSQLBuilder.pas
More file actions
1279 lines (1140 loc) · 44.2 KB
/
GpSQLBuilder.pas
File metadata and controls
1279 lines (1140 loc) · 44.2 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
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
///<summary>SQL query builder.</summary>
///<author>Primoz Gabrijelcic</author>
///<remarks><para>
///Copyright (c) 2016, Primoz Gabrijelcic
///All rights reserved.
///
///Redistribution and use in source and binary forms, with or without
///modification, are permitted provided that the following conditions are met:
///
///* Redistributions of source code must retain the above copyright notice, this
/// list of conditions and the following disclaimer.
///
///* Redistributions in binary form must reproduce the above copyright notice,
/// this list of conditions and the following disclaimer in the documentation
/// and/or other materials provided with the distribution.
///
///* Neither the name of GpSQLBuilder nor the names of its
/// contributors may be used to endorse or promote products derived from
/// this software without specific prior written permission.
///
///THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
///AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
///IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
///DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
///FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
///DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
///SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
///CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
///OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
///OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
///
/// Author : Primoz Gabrijelcic
/// Creation date : 2010-11-24
/// Last modification : 2016-09-08
/// Version : 3.06
///</para><para>
/// History:
/// 3.06: 2016-09-08
/// - Added support for Insert columns.
/// - Added overloads for IGpSQLBuilder.From(IGpSQLBuilder) and
/// .From(IGpSQLBuilderExpression).
/// - Added SQL.&Not and SQL.Concat.
/// 3.05: 2015-07-12
/// - [leledumbo] Added .Insert and .Into methods.
/// - Sorted TGpSQLBuilder implementation.
/// - &Set(column, value: string) overload automatically adds quotes around the
/// `value` parameter when converted to string. If you don't want that to
/// happen, you can use the other overload: &Set(column, [value]).
/// 3.04a: 2015-06-30
/// - Fixed a bug when Where.&Or was called before Where.&And.
/// 3.04: 2015-06-18
/// - Following methods now accept `expression: IGpSQLBuilderExpression` parameter:
/// IGpSQLBuilderCase.When, IGpSQLBuilder.&Case, IGpSQLBuilder.Having,
/// IGpSQLBuilder.Where.
/// - TGpSQLBuilderExpression.&Or can be used on an empty expression (it is
/// silently converted to &And). This simplifies writing `for` loops which
/// add conditions with &Or.
/// - Added helper class SQL.
/// 3.03: 2015-06-17
/// - Added .Update, .&Set, and .Delete methods.
/// 3.02: 2015-05-05
/// - IGpSQLColums was renamed to IGpSQLNames.
/// - IGpSQLBuilder.From adds a new name on each call to accomodate multiple tables
/// in the From part.
/// 3.01: 2015-04-30
/// - Added .Distinct method.
/// 3.0: 2015-04-29
/// - Internal redesign: SQL is generated as an abstract syntax tree and only
/// converted to text when AsString is called. This allows implementing the
/// 'pretty print' function and makes the code less ugly.
/// - Added other Join types.
/// - Case expression can be used in the OrderBy section.
/// 2.02a: 2015-04-20
/// - Corrected SQL generation for LeftJoin().&As() construct.
/// 2.02: 2015-04-05
/// - Reimplemented old .Subquery mechanism as .Expression: IGpSQLBuilderExpression.
/// - Added &And and &Or overloads accepting IGpSQLBuilderExpression.
/// 2.01: 2015-04-05
/// - Added integer-accepting overloads for IGpSQLBuilderCase.&Then and .&Else.
/// 2.0: 2015-04-04
/// - Removed AndE and OrE aliases.
/// - Removed Column overload which accepted 'alias' parameter.
/// - Removed 'dbAlias' parameter from From and LeftJoin methods.
/// - Removed 'subquery' concept as it was not really useful.
/// - Renamed AllColumns to All.
/// - Renamed AsAlias to &As.
/// - IGpSQLBuilderCase.&Then and .&Else values are not automatically quoted.
/// 1.08: 2015-04-03
/// - &And and &Or aliases for AndE and OrE.
/// 1.07: 2015-04-02
/// - IGpSQLBuilder
/// - Added new overloads for Select, Where, OrderBy, GroupBy, and Having.
/// - Added property ActiveSection.
/// - Added methods &On and &Case.
/// - Added case-implementing interface IGpSQLBuilderCase.
/// 1.06: 2015-03-16
/// - Exposed Sections[].
/// - Added parameter dbAlias to the Column method.
/// - Added overloaded Column acception array of const.
/// - Added parameter dbAlias to the LeftJoin method.
/// - Fixed IGpSQLBuilderSection.Clear.
/// 1.05: 2015-03-13
/// - Added parameter dbAlias to the From method.
/// 1.04: 2013-03-06
/// - Added function AllColumns.
/// 1.03: 2013-03-04
/// - Supports multiple left joins.
/// 1.02: 2012-01-10
/// - Supports multiple 'from' databases.
/// 1.01: 2010-12-02
/// - Added 'OR' expression builder.
/// 1.0b: 2010-11-30
/// - Fixed memory leak in TGpSQLBuilder.Destroy.
/// 1.0a: 2010-11-25
/// - AsAlias did not insert 'AS' token.
/// - Clear and ClearAll did not return result.
/// 1.0: 2010-11-24
/// - Released.
///</para></remarks>
unit GpSQLBuilder;
{$IFDEF FPC}
{$MODE DELPHI}
{$ENDIF}
interface
uses
GpSQLBuilder.AST;
type
IGpSQLBuilder = interface;
IGpSQLBuilderExpression = interface ['{CC7ED7A2-3B39-4341-9CBB-EE1C7851BBA9}']
function GetAsString: string;
function GetExpression: IGpSQLExpression;
//
function &And(const expression: array of const): IGpSQLBuilderExpression; overload;
function &And(const expression: string): IGpSQLBuilderExpression; overload;
function &And(const expression: IGpSQLExpression): IGpSQLBuilderExpression; overload;
function &Or(const expression: array of const): IGpSQLBuilderExpression; overload;
function &Or(const expression: string): IGpSQLBuilderExpression; overload;
function &Or(const expression: IGpSQLExpression): IGpSQLBuilderExpression; overload;
property AsString: string read GetAsString;
property Expression: IGpSQLExpression read GetExpression;
end; { IGpSQLBuilderExpression }
IGpSQLBuilderCase = interface ['{1E379718-0959-455A-80AA-63BDA7C92F8C}']
function GetAsString: string;
function GetCase: IGpSQLCase;
//
function &And(const expression: array of const): IGpSQLBuilderCase; overload;
function &And(const expression: string): IGpSQLBuilderCase; overload;
function &And(const expression: IGpSQLBuilderExpression): IGpSQLBuilderCase; overload;
function &Else(const value: string): IGpSQLBuilderCase; overload;
function &Else(const value: int64): IGpSQLBuilderCase; overload;
function &End: IGpSQLBuilderCase;
function &Or(const expression: array of const): IGpSQLBuilderCase; overload;
function &Or(const expression: string): IGpSQLBuilderCase; overload;
function &Or(const expression: IGpSQLBuilderExpression): IGpSQLBuilderCase; overload;
function &Then(const value: string): IGpSQLBuilderCase; overload;
function &Then(const value: int64): IGpSQLBuilderCase; overload;
function When(const condition: string): IGpSQLBuilderCase; overload;
function When(const condition: array of const): IGpSQLBuilderCase; overload;
function When(const condition: IGpSQLBuilderExpression): IGpSQLBuilderCase; overload;
property &Case: IGpSQLCase read GetCase;
property AsString: string read GetAsString;
end; { IGpSQLBuilderCase }
IGpSQLBuilder = interface ['{43EA3E34-A8DB-4257-A19F-030F404646E7}']
function &And(const expression: string): IGpSQLBuilder; overload;
//
function &And(const expression: array of const): IGpSQLBuilder; overload;
function &And(const expression: IGpSQLBuilderExpression): IGpSQLBuilder; overload;
function &As(const alias: string): IGpSQLBuilder;
function &Case(const expression: string = ''): IGpSQLBuilderCase; overload;
function &Case(const expression: array of const): IGpSQLBuilderCase; overload;
function &Case(const expression: IGpSQLBuilderExpression): IGpSQLBuilderCase; overload;
function &On(const expression: string): IGpSQLBuilder; overload;
function &On(const expression: array of const): IGpSQLBuilder; overload;
function &Or(const expression: string): IGpSQLBuilder; overload;
function &Or(const expression: array of const): IGpSQLBuilder; overload;
function &Or(const expression: IGpSQLBuilderExpression): IGpSQLBuilder; overload;
function All: IGpSQLBuilder;
//
function Clear: IGpSQLBuilder;
function ClearAll: IGpSQLBuilder;
function Column(const colName: string): IGpSQLBuilder; overload;
function Column(const dbName, colName: string): IGpSQLBuilder; overload;
function Column(const colName: array of const): IGpSQLBuilder; overload;
function Column(const caseExpr: IGpSQLBuilderCase): IGpSQLBuilder; overload;
function Delete: IGpSQLBuilder;
function Desc: IGpSQLBuilder;
function Distinct: IGpSQLBuilder;
function Expression(const term: string = ''): IGpSQLBuilderExpression; overload;
function Expression(const term: array of const): IGpSQLBuilderExpression; overload;
function First(num: integer): IGpSQLBuilder;
function From(const expression: IGpSQLBuilderExpression): IGpSQLBuilder; overload;
function From(const query: IGpSQLBuilder): IGpSQLBuilder; overload;
function From(const dbName: string): IGpSQLBuilder; overload;
function FullJoin(const dbName: string): IGpSQLBuilder;
function GetAsString: string;
function GetAST: IGpSQLAST;
function GroupBy(const colName: string = ''): IGpSQLBuilder;
function Having(const expression: string = ''): IGpSQLBuilder; overload;
function Having(const expression: array of const): IGpSQLBuilder; overload;
function Having(const expression: IGpSQLBuilderExpression): IGpSQLBuilder; overload;
function InnerJoin(const dbName: string): IGpSQLBuilder;
function Insert: IGpSQLBuilder;
function Into(const tableName: string): IGpSQLBuilder;
function IsEmpty: boolean;
function LeftJoin(const dbName: string): IGpSQLBuilder;
function OrderBy(const colName: string = ''): IGpSQLBuilder; overload;
function OrderBy(const caseExpr: IGpSQLBuilderCase): IGpSQLBuilder; overload;
function RightJoin(const dbName: string): IGpSQLBuilder;
function Select(const colName: string = ''): IGpSQLBuilder; overload;
function Select(const caseExpr: IGpSQLBuilderCase): IGpSQLBuilder; overload;
function &Set(const colName, colValue: string): IGpSQLBuilder; overload;
function &Set(const colName: string; const colValue: array of const): IGpSQLBuilder; overload;
function Skip(num: integer): IGpSQLBuilder;
function Update(const tableName: string): IGpSQLBuilder;
function Where(const expression: string = ''): IGpSQLBuilder; overload;
function Where(const expression: array of const): IGpSQLBuilder; overload;
function Where(const expression: IGpSQLBuilderExpression): IGpSQLBuilder; overload;
property AsString: string read GetAsString;
property AST: IGpSQLAST read GetAST;
end; { IGpSQLBuilder }
SQL = class
class function Concat(const q: array of IGpSQLBuilder): string;
class function Count(const s: string): string; overload;
class function Count(const s: IGpSQLBuilder): string; overload;
class function Count(const s: IGpSQLBuilderExpression): string; overload;
class function Exists(const s: string): string; overload;
class function Exists(const s: IGpSQLBuilder): string; overload;
class function Exists(const s: IGpSQLBuilderExpression): string; overload;
class function Lower(const s: string): string; overload;
class function Lower(const s: IGpSQLBuilder): string; overload;
class function Lower(const s: IGpSQLBuilderExpression): string; overload;
class function Min(const s: string): string; overload;
class function Min(const s: IGpSQLBuilder): string; overload;
class function Min(const s: IGpSQLBuilderExpression): string; overload;
class function Max(const s: string): string; overload;
class function Max(const s: IGpSQLBuilder): string; overload;
class function Max(const s: IGpSQLBuilderExpression): string; overload;
class function &Not(const s: string): string; overload;
class function &Not(const s: IGpSQLBuilder): string; overload;
class function &Not(const s: IGpSQLBuilderExpression): string; overload;
class function Upper(const s: string): string; overload;
class function Upper(const s: IGpSQLBuilder): string; overload;
class function Upper(const s: IGpSQLBuilderExpression): string; overload;
class function Q(const s: string): string; overload;
class function Q(const s: IGpSQLBuilder): string; overload;
class function Q(const s: IGpSQLBuilderExpression): string; overload;
end; { SQL }
function CreateGpSQLBuilder: IGpSQLBuilder;
implementation
uses
{$IFNDEF FPC}System.SysUtils{$ELSE}SysUtils{$ENDIF},
GpSQLBuilder.Serialize;
type
TGpSQLBuilderExpression = class(TInterfacedObject, IGpSQLBuilderExpression)
strict private
FExpression: IGpSQLExpression;
FLastAnd : IGpSQLExpression;
strict protected
function FindRightmostAnd(const expression: IGpSQLExpression): IGpSQLExpression;
function GetAsString: string;
function GetExpression: IGpSQLExpression;
public
constructor Create(const expression: string = ''); overload;
constructor Create(const expression: IGpSQLExpression); overload;
function &And(const expression: array of const): IGpSQLBuilderExpression; overload;
function &And(const expression: string): IGpSQLBuilderExpression; overload;
function &And(const expression: IGpSQLExpression): IGpSQLBuilderExpression; overload;
function &Or(const expression: array of const): IGpSQLBuilderExpression; overload;
function &Or(const expression: string): IGpSQLBuilderExpression; overload;
function &Or(const expression: IGpSQLExpression): IGpSQLBuilderExpression; overload;
property AsString: string read GetAsString;
property Expression: IGpSQLExpression read GetExpression;
end; { TGpSQLBuilderExpression }
TGpSQLBuilderCase = class(TInterfacedObject, IGpSQLBuilderCase)
strict private
FCase : IGpSQLCase;
FLastExpr: IGpSQLBuilderExpression;
strict protected
function GetAsString: string;
function GetCase: IGpSQLCase;
public
constructor Create(const expression: string);
function &And(const expression: array of const): IGpSQLBuilderCase; overload;
function &And(const expression: string): IGpSQLBuilderCase; overload;
function &And(const expression: IGpSQLBuilderExpression): IGpSQLBuilderCase; overload;
function &Else(const value: string): IGpSQLBuilderCase; overload;
function &Else(const value: int64): IGpSQLBuilderCase; overload;
function &End: IGpSQLBuilderCase;
function Expression: IGpSQLBuilderExpression;
function &Or(const expression: array of const): IGpSQLBuilderCase; overload;
function &Or(const expression: string): IGpSQLBuilderCase; overload;
function &Or(const expression: IGpSQLBuilderExpression): IGpSQLBuilderCase; overload;
function &Then(const value: string): IGpSQLBuilderCase; overload;
function &Then(const value: int64): IGpSQLBuilderCase; overload;
function When(const condition: string): IGpSQLBuilderCase; overload;
function When(const condition: array of const): IGpSQLBuilderCase; overload;
function When(const condition: IGpSQLBuilderExpression): IGpSQLBuilderCase; overload;
property &Case: IGpSQLCase read GetCase;
property AsString: string read GetAsString;
end; { TGpSQLBuilderCase }
TGpSQLBuilder = class(TInterfacedObject, IGpSQLBuilder)
strict private
type
TGpSQLSection = (secSelect, secDelete, secInsert, secUpdate, secJoin, secWhere, secGroupBy, secHaving, secOrderBy);
TGpSQLSections = set of TGpSQLSection;
var
FActiveSection: TGpSQLSection;
FActiveExpr : IGpSQLBuilderExpression;
FActiveValues : IGpSQLNameValuePairs;
FAST : IGpSQLAST;
FASTColumns : IGpSQLNames;
FASTSection : IGpSQLSection;
FASTName : IGpSQLName;
FTableNames : IGpSQLNames;
strict protected
function AutoQuote(const s: string): string;
procedure AssertHaveName;
procedure AssertSection(sections: TGpSQLSections);
function CreateJoin(joinType: TGpSQLJoinType; const dbName: string): IGpSQLBuilder;
function GetAsString: string;
function GetAST: IGpSQLAST;
function InternalSet(const colName, colValue: string): IGpSQLBuilder;
procedure SelectSection(section: TGpSQLSection);
public
constructor Create;
function &And(const expression: array of const): IGpSQLBuilder; overload;
function &And(const expression: string): IGpSQLBuilder; overload;
function &And(const expression: IGpSQLBuilderExpression): IGpSQLBuilder; overload;
function All: IGpSQLBuilder;
function &As(const alias: string): IGpSQLBuilder;
function &Case(const expression: string = ''): IGpSQLBuilderCase; overload;
function &Case(const expression: array of const): IGpSQLBuilderCase; overload;
function &Case(const expression: IGpSQLBuilderExpression): IGpSQLBuilderCase; overload;
function Clear: IGpSQLBuilder;
function ClearAll: IGpSQLBuilder;
function Column(const colName: string): IGpSQLBuilder; overload;
function Column(const dbName, colName: string): IGpSQLBuilder; overload;
function Column(const colName: array of const): IGpSQLBuilder; overload;
function Column(const caseExpr: IGpSQLBuilderCase): IGpSQLBuilder; overload;
function Delete: IGpSQLBuilder;
function Desc: IGpSQLBuilder;
function Distinct: IGpSQLBuilder;
function Expression(const term: string = ''): IGpSQLBuilderExpression; overload;
function Expression(const term: array of const): IGpSQLBuilderExpression; overload;
function First(num: integer): IGpSQLBuilder;
function From(const expression: IGpSQLBuilderExpression): IGpSQLBuilder; overload;
function From(const query: IGpSQLBuilder): IGpSQLBuilder; overload;
function From(const dbName: string): IGpSQLBuilder; overload;
function FullJoin(const dbName: string): IGpSQLBuilder;
function GroupBy(const colName: string = ''): IGpSQLBuilder;
function Having(const expression: string = ''): IGpSQLBuilder; overload;
function Having(const expression: array of const): IGpSQLBuilder; overload;
function Having(const expression: IGpSQLBuilderExpression): IGpSQLBuilder; overload;
function Insert: IGpSQLBuilder;
function Into(const tableName: string): IGpSQLBuilder;
function InnerJoin(const dbName: string): IGpSQLBuilder;
function IsEmpty: boolean;
function LeftJoin(const dbName: string): IGpSQLBuilder;
function &On(const expression: string): IGpSQLBuilder; overload;
function &On(const expression: array of const): IGpSQLBuilder; overload;
function &Or(const expression: array of const): IGpSQLBuilder; overload;
function &Or(const expression: string): IGpSQLBuilder; overload;
function &Or(const expression: IGpSQLBuilderExpression): IGpSQLBuilder; overload;
function OrderBy(const colName: string = ''): IGpSQLBuilder; overload;
function OrderBy(const caseExpr: IGpSQLBuilderCase): IGpSQLBuilder; overload;
function RightJoin(const dbName: string): IGpSQLBuilder;
function Select(const colName: string = ''): IGpSQLBuilder; overload;
function Select(const caseExpr: IGpSQLBuilderCase): IGpSQLBuilder; overload;
function &Set(const colName, colValue: string): IGpSQLBuilder; overload;
function &Set(const colName: string; const colValue: array of const): IGpSQLBuilder; overload;
function Skip(num: integer): IGpSQLBuilder;
function Update(const tableName: string): IGpSQLBuilder;
function Where(const expression: string = ''): IGpSQLBuilder; overload;
function Where(const expression: array of const): IGpSQLBuilder; overload;
function Where(const expression: IGpSQLBuilderExpression): IGpSQLBuilder; overload;
property AsString: string read GetAsString;
property AST: IGpSQLAST read GetAST;
end; { TGpSQLBuilder }
{ exports }
function CreateGpSQLBuilder: IGpSQLBuilder;
begin
Result := TGpSQLBuilder.Create;
end; { CreateGpSQLBuilder }
{ globals }
function VarRecToString(const vr: TVarRec): string;
const
BoolChars: array [boolean] of string = ('F', 'T');
{$ifndef fpc}
type
PtrUInt = Integer;
{$endif}
begin
case vr.VType of
vtInteger: Result := IntToStr(vr.VInteger);
vtBoolean: Result := BoolChars[vr.VBoolean];
vtChar: Result := char(vr.VChar);
vtExtended: Result := FloatToStr(vr.VExtended^);
vtString: Result := string(vr.VString^);
vtPointer: Result := IntToHex(PtrUInt(vr.VPointer),8);
vtPChar: Result := string(vr.VPChar^);
vtObject: Result := vr.VObject.ClassName;
vtClass: Result := vr.VClass.ClassName;
vtWideChar: Result := string(vr.VWideChar);
vtPWideChar: Result := string(vr.VPWideChar^);
vtAnsiString: Result := string(vr.VAnsiString);
vtCurrency: Result := CurrToStr(vr.VCurrency^);
vtVariant: Result := string(vr.VVariant^);
vtWideString: Result := string(WideString(vr.VWideString));
vtInt64: Result := IntToStr(vr.VInt64^);
{$IFDEF Unicode}
vtUnicodeString: Result := string(vr.VUnicodeString);
{$ENDIF}
else raise Exception.Create('VarRecToString: Unsupported parameter type');
end;
end; { VarRecToString }
function SqlParamsToStr(const params: array of const): string;
var
iParam: integer;
lastCh: char;
sParam: string;
begin
Result := '';
for iParam := Low(params) to High(params) do begin
sParam := VarRecToString(params[iparam]);
if Result = '' then
lastCh := ' '
else
lastCh := Result[Length(Result)];
if (lastCh <> '.') and (lastCh <> '(') and (lastCh <> ' ') and (lastCh <> ':') and
(sParam <> ',') and (sParam <> '.') and (sParam <> ')')
then
Result := Result + ' ';
Result := Result + sParam;
end;
end; { SqlParamsToStr }
{ TGpSQLBuilderCase }
constructor TGpSQLBuilderCase.Create(const expression: string);
begin
inherited Create;
FCase := CreateSQLCase;
if expression <> '' then
FCase.CaseExpression.Term := expression;
end; { TGpSQLBuilderCase.Create }
function TGpSQLBuilderCase.&And(const expression: array of const): IGpSQLBuilderCase;
begin
FLastExpr.&And(expression);
Result := Self;
end; { TGpSQLBuilder.&And }
function TGpSQLBuilderCase.&And(const expression: string): IGpSQLBuilderCase;
begin
FLastExpr.&And(expression);
Result := Self;
end; { TGpSQLBuilder.&And }
function TGpSQLBuilderCase.&And(const expression: IGpSQLBuilderExpression):
IGpSQLBuilderCase;
begin
FLastExpr.&And(expression.Expression);
Result := Self;
end; { TGpSQLBuilderCase.&And }
function TGpSQLBuilderCase.&Else(const value: string): IGpSQLBuilderCase;
begin
FLastExpr := TGpSQLBuilderExpression.Create(value);
FCase.ElseExpression := FLastExpr.Expression;
Result := Self;
end; { TGpSQLBuilderCase.&Else }
function TGpSQLBuilderCase.&Else(const value: int64): IGpSQLBuilderCase;
begin
Result := &Else(IntToStr(value));
end; { TGpSQLBuilderCase.&Else }
function TGpSQLBuilderCase.&End: IGpSQLBuilderCase;
begin
Result := Self;
end; { TGpSQLBuilderCase.&End }
function TGpSQLBuilderCase.GetAsString: string;
begin
Result := CreateSQLSerializer(FCase).AsString;
end; { TGpSQLBuilderCase.GetAsString }
function TGpSQLBuilderCase.&Or(const expression: array of const): IGpSQLBuilderCase;
begin
FLastExpr.&Or(expression);
Result := Self;
end; { TGpSQLBuilder.&Or}
function TGpSQLBuilderCase.&Or(const expression: string): IGpSQLBuilderCase;
begin
FLastExpr.&Or(expression);
Result := Self;
end; { TGpSQLBuilder.&Or }
function TGpSQLBuilderCase.&Or(const expression: IGpSQLBuilderExpression):
IGpSQLBuilderCase;
begin
FLastExpr.&Or(expression.Expression);
Result := Self;
end; { TGpSQLBuilderCase.&Or }
function TGpSQLBuilderCase.&Then(const value: string): IGpSQLBuilderCase;
begin
Assert(FCase.WhenList.Count > 0, 'TGpSQLBuilderCase.&Then: Missing When');
FLastExpr := TGpSQLBuilderExpression.Create(value);
FCase.WhenList[FCase.WhenList.Count-1].ThenExpression := FLastExpr.Expression;
Result := Self;
end; { TGpSQLBuilderCase.&Then }
function TGpSQLBuilderCase.&Then(const value: int64): IGpSQLBuilderCase;
begin
Result := &Then(IntToStr(value));
end; { TGpSQLBuilderCase.&Then }
function TGpSQLBuilderCase.Expression: IGpSQLBuilderExpression;
begin
Result := TGpSQLBuilderExpression.Create;
end; { TGpSQLBuilderCase.Expression }
function TGpSQLBuilderCase.GetCase: IGpSQLCase;
begin
Result := FCase;
end; { TGpSQLBuilderCase.GetCase }
function TGpSQLBuilderCase.When(const condition: array of const): IGpSQLBuilderCase;
begin
Result := When(SqlParamsToStr(condition));
end; { TGpSQLBuilderCase.When }
function TGpSQLBuilderCase.When(const condition: string): IGpSQLBuilderCase;
begin
Result := When(TGpSQLBuilderExpression.Create(condition));
end; { TGpSQLBuilderCase.When }
function TGpSQLBuilderCase.When(const condition: IGpSQLBuilderExpression):
IGpSQLBuilderCase;
var
wt: IGpSQLCaseWhenThen;
begin
FLastExpr := condition;
wt := FCase.WhenList.Add;
wt.WhenExpression := FLastExpr.Expression;
Result := Self;
end; { TGpSQLBuilderCase.When }
{ TGpSQLBuilderExpression }
constructor TGpSQLBuilderExpression.Create(const expression: string);
begin
inherited Create;
FExpression := CreateSQLExpression;
if expression <> '' then
&And(expression);
end; { TGpSQLBuilderExpression.Create }
constructor TGpSQLBuilderExpression.Create(const expression: IGpSQLExpression);
begin
inherited Create;
FExpression := expression;
FLastAnd := FindRightmostAnd(expression);
end; { TGpSQLBuilderExpression.Create }
function TGpSQLBuilderExpression.&And(const expression: string): IGpSQLBuilderExpression;
var
node: IGpSQLExpression;
begin
node := CreateSQLExpression;
node.Term := expression;
Result := &And(node);
end; { TGpSQLBuilderExpression.&And }
function TGpSQLBuilderExpression.&And(
const expression: array of const): IGpSQLBuilderExpression;
begin
Result := &And(SqlParamsToStr(expression));
end; { TGpSQLBuilderExpression.&And }
function TGpSQLBuilderExpression.&And(const expression: IGpSQLExpression):
IGpSQLBuilderExpression;
var
node: IGpSQLExpression;
root: IGpSQLExpression;
begin
root := FExpression;
if root.IsEmpty then begin
root.Assign(expression);
FLastAnd := root;
end
else begin
node := CreateSQLExpression;
node.Assign(root);
root.Left := node;
root.Operation := opAnd;
root.Right := expression;
FLastAnd := root.Right;
end;
Result := Self;
end; { TGpSQLBuilderExpression.&And }
function TGpSQLBuilderExpression.FindRightmostAnd(const expression: IGpSQLExpression):
IGpSQLExpression;
begin
if expression.Operation = opNone then
Result := expression
else if expression.Operation = opOr then
Result := expression
else
Result := FindRightmostAnd(expression.Right);
end; { TGpSQLBuilderExpression.FindRightmostAnd }
function TGpSQLBuilderExpression.&Or(const expression: string): IGpSQLBuilderExpression;
var
node: IGpSQLExpression;
begin
node := CreateSQLExpression;
node.Term := expression;
Result := &Or(node);
end; { TGpSQLBuilderExpression.&Or }
function TGpSQLBuilderExpression.&Or(const expression: array of const): IGpSQLBuilderExpression;
begin
Result := &Or(SqlParamsToStr(expression));
end; { TGpSQLBuilderExpression.&Or }
function TGpSQLBuilderExpression.&Or(const expression: IGpSQLExpression): IGpSQLBuilderExpression;
var
node: IGpSQLExpression;
begin
if (not assigned(FLastAnd)) or FLastAnd.IsEmpty then
Result := &And(expression)
else begin
node := CreateSQLExpression;
node.Assign(FLastAnd);
FLastAnd.Left := node;
FLastAnd.Operation := opOr;
FLastAnd.Right := expression;
end;
Result := Self;
end; { TGpSQLBuilderExpression.&Or }
function TGpSQLBuilderExpression.GetAsString: string;
begin
Result := CreateSQLSerializer(Expression).AsString;
end; { TGpSQLBuilderExpression.GetAsString }
function TGpSQLBuilderExpression.GetExpression: IGpSQLExpression;
begin
Result := FExpression;
end; { TGpSQLBuilderExpression.GetExpression }
{ TGpSQLBuilder }
constructor TGpSQLBuilder.Create;
begin
inherited;
FAST := CreateSQLAST;
end; { TGpSQLBuilder.Create }
function TGpSQLBuilder.All: IGpSQLBuilder;
begin
Result := Column('*');
end; { TGpSQLBuilder.All }
function TGpSQLBuilder.&And(const expression: array of const): IGpSQLBuilder;
begin
Result := &And(SqlParamsToStr(expression));
end; { TGpSQLBuilder.&And }
function TGpSQLBuilder.&And(const expression: string): IGpSQLBuilder;
begin
FActiveExpr.&And(expression);
Result := Self;
end; { TGpSQLBuilder.&And }
function TGpSQLBuilder.&And(const expression: IGpSQLBuilderExpression): IGpSQLBuilder;
begin
FActiveExpr.&And(expression.Expression);
Result := Self;
end; { TGpSQLBuilder.&And }
function TGpSQLBuilder.&As(const alias: string): IGpSQLBuilder;
begin
AssertSection([secSelect, secDelete, secJoin]);
AssertHaveName;
FASTName.Alias := alias;
Result := Self;
end; { TGpSQLBuilder.&As }
procedure TGpSQLBuilder.AssertHaveName;
begin
if not assigned(FASTName) then
raise Exception.Create('TGpSQLBuilder: Curernt name is not set');
end; { TGpSQLBuilder.AssertHaveName }
procedure TGpSQLBuilder.AssertSection(sections: TGpSQLSections);
begin
if not (FActiveSection in sections) then
raise Exception.Create('TGpSQLBuilder: Not supported in this section');
end; { TGpSQLBuilder.AssertSection }
function TGpSQLBuilder.AutoQuote(const s: string): string;
begin
if (s <> '') and (s[1] = '''') and (s[Length(s)] = '''') then
Result := s
else
Result := '''' + s + '''';
end; { TGpSQLBuilder.AutoQuote }
function TGpSQLBuilder.&Case(const expression: string = ''): IGpSQLBuilderCase;
begin
Result := TGpSQLBuilderCase.Create(expression);
end; { TGpSQLBuilder.&Case }
function TGpSQLBuilder.&Case(const expression: array of const): IGpSQLBuilderCase;
begin
Result := &Case(SqlParamsToStr(expression));
end; { TGpSQLBuilder.&Case }
function TGpSQLBuilder.&Case(const expression: IGpSQLBuilderExpression):
IGpSQLBuilderCase;
begin
Result := TGpSQLBuilderCase.Create('');
Result.&And(expression);
end; { TGpSQLBuilder }
function TGpSQLBuilder.Clear: IGpSQLBuilder;
begin
FASTSection.Clear;
Result := Self;
end; { TGpSQLBuilder.Clear }
function TGpSQLBuilder.ClearAll: IGpSQLBuilder;
begin
FAST.Clear;
Result := Self;
end; { TGpSQLBuilder.ClearAll }
function TGpSQLBuilder.Column(const colName: string): IGpSQLBuilder;
begin
if assigned(FASTColumns) then begin
FASTName := FASTColumns.Add;
FASTName.Name := colName;
end
else
raise Exception.CreateFmt('Current section [%s] does not support COLUMN.',
[FASTSection.Name]);
Result := Self;
end; { TGpSQLBuilder.Column }
function TGpSQLBuilder.Column(const dbName, colName: string): IGpSQLBuilder;
begin
Result := Column(dbName + '.' + colName);
end; { TGpSQLBuilder.Column }
function TGpSQLBuilder.Column(const colName: array of const): IGpSQLBuilder;
begin
Result := Column(SqlParamsToStr(colName));
end; { TGpSQLBuilder.Column }
function TGpSQLBuilder.Column(const caseExpr: IGpSQLBuilderCase): IGpSQLBuilder;
begin
if assigned(FASTColumns) then begin
FASTName := FASTColumns.Add;
FASTName.&Case := caseExpr.&Case;
end
else
raise Exception.CreateFmt('Current section [%s] does not support COLUMN.',
[FASTSection.Name]);
Result := Self;
end; { TGpSQLBuilder.Column }
function TGpSQLBuilder.CreateJoin(joinType: TGpSQLJoinType; const dbName: string):
IGpSQLBuilder;
var
join: IGpSQLJoin;
begin
FActiveSection := secJoin;
join := FAST.Joins.Add;
join.JoinType := joinType;
FASTName := join.JoinedTable;
FASTName.Name := dbName;
FASTSection := join;
FASTColumns := nil;
FActiveExpr := TGpSQLBuilderExpression.Create(join.Condition);
Result := Self;
end; { TGpSQLBuilder.CreateJoin }
function TGpSQLBuilder.Delete: IGpSQLBuilder;
begin
SelectSection(secDelete);
Result := Self;
end; { TGpSQLBuilder.Delete }
function TGpSQLBuilder.Desc: IGpSQLBuilder;
begin
AssertSection([secOrderBy]);
Assert(FASTColumns.Count > 0, 'TGpSQLBuilder.Desc: No columns set up yet');
(FASTColumns[FASTColumns.Count - 1] as IGpSQLOrderByColumn).Direction := dirDescending;
Result := Self;
end; { TGpSQLBuilder.Desc }
function TGpSQLBuilder.Distinct: IGpSQLBuilder;
var
qual: IGpSQLSelectQualifier;
begin
AssertSection([secSelect]);
qual := (FASTSection as IGpSQLSelect).Qualifiers.Add;
qual.Qualifier := sqDistinct;
Result := Self;
end; { TGpSQLBuilder.Distinct }
function TGpSQLBuilder.Expression(const term: string): IGpSQLBuilderExpression;
begin
Result := TGpSQLBuilderExpression.Create(term);
end; { TGpSQLBuilder.Expression }
function TGpSQLBuilder.Expression(const term: array of const): IGpSQLBuilderExpression;
begin
Result := Expression(SqlParamsToStr(term));
end; { TGpSQLBuilder.Expression }
function TGpSQLBuilder.First(num: integer): IGpSQLBuilder;
var
qual: IGpSQLSelectQualifier;
begin
AssertSection([secSelect]);
qual := (FASTSection as IGpSQLSelect).Qualifiers.Add;
qual.Qualifier := sqFirst;
qual.Value := num;
Result := Self;
end; { TGpSQLBuilder.First }
function TGpSQLBuilder.From(const dbName: string): IGpSQLBuilder;
begin
AssertSection([secSelect, secDelete]);
FASTName := FTableNames.Add;
FASTName.Name := dbName;
Result := Self;
end; { TGpSQLBuilder.From }
function TGpSQLBuilder.FullJoin(const dbName: string): IGpSQLBuilder;
begin
Result := CreateJoin(jtFull, dbName);
end; { TGpSQLBuilder.FullJoin }
function TGpSQLBuilder.GetAsString: string;
begin
Result := CreateSQLSerializer(AST).AsString;
end; { TGpSQLBuilder.GetAsString }
function TGpSQLBuilder.GetAST: IGpSQLAST;
begin
Result := FAST;
end; { TGpSQLBuilder.GetAST }
function TGpSQLBuilder.GroupBy(const colName: string): IGpSQLBuilder;
begin
SelectSection(secGroupBy);
if colName = '' then
Result := Self
else
Result := Column(colName);
end; { TGpSQLBuilder.GroupBy }
function TGpSQLBuilder.Having(const expression: string): IGpSQLBuilder;
begin
SelectSection(secHaving);
if expression = '' then
Result := Self
else
Result := &And(expression);
end; { TGpSQLBuilder.Having }
function TGpSQLBuilder.Having(const expression: array of const): IGpSQLBuilder;
begin
Result := Having(SqlParamsToStr(expression));
end; { TGpSQLBuilder.Having }
function TGpSQLBuilder.Having(const expression: IGpSQLBuilderExpression): IGpSQLBuilder;
begin
SelectSection(secHaving);
Result := &And(expression);
end; { TGpSQLBuilder.Having }
function TGpSQLBuilder.InnerJoin(const dbName: string): IGpSQLBuilder;
begin
Result := CreateJoin(jtInner, dbName);
end; { TGpSQLBuilder.InnerJoin }
function TGpSQLBuilder.Insert: IGpSQLBuilder;
begin
SelectSection(secInsert);
Result := Self;
end; { TGpSQLBuilder.Insert }
function TGpSQLBuilder.InternalSet(const colName, colValue: string): IGpSQLBuilder;
var
pair: IGpSQLNameValue;
begin
AssertSection([secInsert, secUpdate]);
pair := FActiveValues.Add;
pair.Name := colName;
pair.Value := colValue;
Result := Self;
end; { TGpSQLBuilder.InternalSet }
function TGpSQLBuilder.Into(const tableName: string): IGpSQLBuilder;
begin
AssertSection([secInsert]);
(FASTSection as IGpSQLInsert).TableName := tableName;
Result := Self;
end; { TGpSQLBuilder.Into }
function TGpSQLBuilder.IsEmpty: boolean;
begin
Result := FASTSection.IsEmpty;
end; { TGpSQLBuilder.IsEmpty }
function TGpSQLBuilder.LeftJoin(const dbName: string): IGpSQLBuilder;
begin
Result := CreateJoin(jtLeft, dbName);
end; { TGpSQLBuilder.LeftJoin }
function TGpSQLBuilder.&On(const expression: string): IGpSQLBuilder;
begin
Result := &And(expression);
end; { TGpSQLBuilder.&On }
function TGpSQLBuilder.&On(const expression: array of const): IGpSQLBuilder;
begin
Result := &On(SqlParamsToStr(expression));
end; { TGpSQLBuilder.&On }
function TGpSQLBuilder.OrderBy(const colName: string): IGpSQLBuilder;
begin
SelectSection(secOrderBy);
if colName = '' then
Result := Self
else
Result := Column(colName);
end; { TGpSQLBuilder.OrderBy }
function TGpSQLBuilder.OrderBy(const caseExpr: IGpSQLBuilderCase): IGpSQLBuilder;
begin
SelectSection(secOrderBy);
Result := Column(caseExpr);
end; { TGpSQLBuilder.OrderBy }
function TGpSQLBuilder.RightJoin(const dbName: string): IGpSQLBuilder;
begin
Result := CreateJoin(jtRight, dbName);
end; { TGpSQLBuilder.RightJoin }
function TGpSQLBuilder.&Or(const expression: array of const): IGpSQLBuilder;
begin
Result := &Or(SqlParamsToStr(expression));
end; { TGpSQLBuilder.&Or }
function TGpSQLBuilder.&Or(const expression: string): IGpSQLBuilder;
begin
FActiveExpr.&Or(expression);
Result := Self;
end; { TGpSQLBuilder.&Or }
function TGpSQLBuilder.&Or(const expression: IGpSQLBuilderExpression): IGpSQLBuilder;
begin
FActiveExpr.&Or(expression.Expression);
Result := Self;
end; { TGpSQLBuilder.&Or }
function TGpSQLBuilder.Select(const colName: string): IGpSQLBuilder;
begin
SelectSection(secSelect);
if colName = '' then