-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathHGM.SQLang.pas
1307 lines (1128 loc) · 36.6 KB
/
HGM.SQLang.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
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
unit HGM.SQLang;
interface
uses
System.Classes, System.SysUtils, System.Generics.Collections;
type
TFieldType = (ftInteger, ftString, ftFloat, ftDateTime, ftBlob, ftBoolean, ftParam);
TDIType = (diInc, diDec, diDiv, diMul);
TWhereUnion = (wuAnd, wuOr, wuNotAnd, wuNotOr);
TField = record
private
function GetSQL: string;
public
FieldType: TFieldType;
FieldName: string;
PrimaryKey: Boolean;
AutoInc: Boolean;
NotNull: Boolean;
property SQL: string read GetSQL;
end;
TFields = TList<TField>;
TFieldNames = TStringList;
TUnionWhere = TStringList;
TInsertValue = record
public
FieldType: TFieldType;
FieldName: string;
Value: Variant;
end;
TInsertValues = TList<TInsertValue>;
TDIValue = record
public
FieldType: TFieldType;
DIType: TDIType;
FieldName: string;
Value: Variant;
end;
TDIValues = TList<TDIValue>;
TTable = class;
TInsertInto = class;
TUpdate = class;
TSelect = class;
TDelete = class;
TDropTable = class;
TUpdateBlob = class;
BaseSQL = class
private
FName: string;
procedure SetName(const Value: string);
public
property TableName: string read FName write SetName;
end;
SQL = class(BaseSQL)
private
FWhereStr: string;
FUWheres: TUnionWhere;
function GetWhere: string;
function InsertUnion(const Union: TWhereUnion): string;
public
function AsSubquery: string; virtual;
function GetSQL(AutoFree: Boolean = False): string; virtual; abstract;
procedure EndCreate; virtual;
procedure Clear; virtual;
procedure WhereParenthesesOpen(Union: TWhereUnion = wuAnd);
procedure WhereParenthesesClose;
procedure WhereFieldLike(const FieldName: string; const Value: string; const Union: TWhereUnion = wuAnd); overload;
procedure WhereFieldBetween(const FieldName: string; const ValueLeft, ValueRight: TDateTime; const Union: TWhereUnion = wuAnd); overload;
procedure WhereFieldBetween(const FieldName: string; const ValueLeft, ValueRight: Extended; const Union: TWhereUnion = wuAnd); overload;
procedure WhereFieldBetween(const FieldName: string; const ValueLeft, ValueRight: Integer; const Union: TWhereUnion = wuAnd); overload;
procedure WhereFieldIsNull(const FieldName: string; const Union: TWhereUnion = wuAnd);
procedure WhereFieldIsNotNull(const FieldName: string; const Union: TWhereUnion = wuAnd);
procedure WhereField(const FieldName, Oper: string; const FieldValue: string; const Union: TWhereUnion = wuAnd); overload;
procedure WhereField(const FieldName, Oper: string; const FieldValue: Extended; const Union: TWhereUnion = wuAnd); overload;
procedure WhereField(const FieldName, Oper: string; const FieldValue: Integer; const Union: TWhereUnion = wuAnd); overload;
procedure WhereField(const FieldName, Oper: string; const FieldValue: TDateTime; const Union: TWhereUnion = wuAnd); overload;
procedure WhereField(const FieldName, Oper: string; const FieldValue: Boolean; const Union: TWhereUnion = wuAnd); overload;
procedure WhereFieldWOQ(const FieldName, Oper: string; const FieldValue: string; const Union: TWhereUnion = wuAnd); //Áåç êîâû÷åê
procedure WhereFieldIN(const FieldName: string; const FieldValues: array of string; const Union: TWhereUnion = wuAnd); overload;
procedure WhereFieldIN(const FieldName: string; const FieldValues: array of Extended; const Union: TWhereUnion = wuAnd); overload;
procedure WhereFieldIN(const FieldName: string; const FieldValues: array of Integer; const Union: TWhereUnion = wuAnd); overload;
procedure WhereFieldIN(const FieldName: string; const FieldValues: array of TDateTime; const Union: TWhereUnion = wuAnd); overload;
procedure WhereFieldIN(const FieldName: string; const FieldValues: array of Boolean; const Union: TWhereUnion = wuAnd); overload;
procedure WhereFieldEqual(const FieldName: string; const FieldValue: string; const Union: TWhereUnion = wuAnd); overload;
procedure WhereFieldEqual(const FieldName: string; const FieldValue: Extended; const Union: TWhereUnion = wuAnd); overload;
procedure WhereFieldEqual(const FieldName: string; const FieldValue: Integer; const Union: TWhereUnion = wuAnd); overload;
procedure WhereFieldEqual(const FieldName: string; const FieldValue: TDateTime; const Union: TWhereUnion = wuAnd); overload;
procedure WhereFieldEqual(const FieldName: string; const FieldValue: Boolean; const Union: TWhereUnion = wuAnd); overload;
procedure WhereNotFieldEqual(const FieldName: string; const FieldValue: string; const Union: TWhereUnion = wuAnd); overload;
procedure WhereNotFieldEqual(const FieldName: string; const FieldValue: Extended; const Union: TWhereUnion = wuAnd); overload;
procedure WhereNotFieldEqual(const FieldName: string; const FieldValue: Integer; const Union: TWhereUnion = wuAnd); overload;
procedure WhereNotFieldEqual(const FieldName: string; const FieldValue: TDateTime; const Union: TWhereUnion = wuAnd); overload;
procedure WhereNotFieldEqual(const FieldName: string; const FieldValue: Boolean; const Union: TWhereUnion = wuAnd); overload;
procedure WhereExists(const Select: string; const Union: TWhereUnion = wuAnd);
procedure WhereStr(const Value: string);
constructor Create; virtual;
destructor Destroy; override;
property Where: string read GetWhere;
public
class function CreateField: TField;
class function CreateTable(TableName: string = ''; IfNotExists: Boolean = True): TTable; overload;
class function InsertInto: TInsertInto; overload;
class function InsertInto(TableName: string): TInsertInto; overload;
class function InsertOrReplace: TInsertInto; overload;
class function InsertOrReplace(TableName: string): TInsertInto; overload;
class function Delete: TDelete; overload;
class function Delete(TableName: string): TDelete; overload;
class function DropTable: TDropTable; overload;
class function DropTable(TableName: string): TDropTable; overload;
class function Update: TUpdate; overload;
class function Update(TableName: string): TUpdate; overload;
class function Select: TSelect; overload;
class function Select(TableName: string; Field: string = ''): TSelect; overload;
class function Select(TableName: string; Fields: TArray<string>): TSelect; overload;
class function UpdateBlob: TUpdateBlob; overload;
class function UpdateBlob(TableName: string): TUpdateBlob; overload;
class function UpdateBlob(TableName, FieldName: string): TUpdateBlob; overload;
//
class function PRAGMA(Key, Value: string): string;
class function SelectLastInsertID: string;
//
end;
TTable = class(BaseSQL)
private
FIfNotExists: Boolean;
function GetField(Index: Integer): TField;
procedure SetField(Index: Integer; const Value: TField);
protected
FFields: TFields;
public
function GetSQL(AutoFree: Boolean = False): string; virtual;
procedure AddField(Name: string; FieldType: TFieldType; PrimaryKey: Boolean = False; NotNull: Boolean = False; AutoInc: Boolean = False); virtual;
procedure EndCreate; virtual;
procedure Clear; virtual;
constructor Create(ATableName: string = ''; AIfNotExists: Boolean = True); overload; virtual;
destructor Destroy; override;
property Fields[Index: Integer]: TField read GetField write SetField;
property TableName;
property IfNotExists: Boolean read FIfNotExists write FIfNotExists;
end;
TInsertInto = class(BaseSQL)
private
FDoubleDoubleQuote: Boolean;
FOrReplace: Boolean;
procedure SetDoubleDoubleQuote(const Value: Boolean);
protected
FFieldValues: TInsertValues;
public
function GetSQL(AutoFree: Boolean = False): string;
procedure EndCreate;
procedure Clear;
procedure AddValue(FieldName: string; FieldValue: Integer); overload;
procedure AddValue(FieldName: string; FieldValue: string); overload;
procedure AddValue(FieldName: string; FieldValue: Extended); overload;
procedure AddValue(FieldName: string; FieldValue: TDateTime); overload;
procedure AddValue(FieldName: string; FieldValue: Boolean); overload;
procedure AddValueAsParam(FieldName: string; ParamChar: string = ':'; CharOnly: Boolean = False);
constructor Create; virtual;
destructor Destroy; override;
property TableName;
property OrReplace: Boolean read FOrReplace write FOrReplace;
property DoubleDoubleQuote: Boolean read FDoubleDoubleQuote write SetDoubleDoubleQuote default False;
end;
TUpdate = class(SQL)
private
FDoubleDoubleQuote: Boolean;
procedure SetDoubleDoubleQuote(const Value: Boolean);
protected
FFieldValues: TInsertValues;
FDIValues: TDIValues;
public
function GetSQL(AutoFree: Boolean = False): string; override;
procedure Clear; override;
procedure AddValue(FieldName: string; FieldValue: Integer); overload;
procedure AddValue(FieldName: string; FieldValue: string); overload;
procedure AddValue(FieldName: string; FieldValue: Extended); overload;
procedure AddValue(FieldName: string; FieldValue: TDateTime); overload;
procedure AddValue(FieldName: string; FieldValue: Boolean); overload;
procedure IncValue(FieldName: string; Value: Integer); overload;
procedure IncValue(FieldName: string; Value: Extended); overload;
procedure DecValue(FieldName: string; Value: Integer); overload;
procedure DecValue(FieldName: string; Value: Extended); overload;
procedure MulValue(FieldName: string; Value: Integer); overload;
procedure MulValue(FieldName: string; Value: Extended); overload;
procedure DivValue(FieldName: string; Value: Integer); overload;
procedure DivValue(FieldName: string; Value: Extended); overload;
procedure AddValueAsParam(FieldName: string; ParamChar: string = ':'; CharOnly: Boolean = False);
constructor Create; override;
destructor Destroy; override;
property TableName;
property DoubleDoubleQuote: Boolean read FDoubleDoubleQuote write SetDoubleDoubleQuote;
end;
TSelect = class(SQL)
private
FOrderBy: TStringList;
FJoins: TStringList;
FLimitInt: Integer;
FDistinct: Boolean;
function GetField(Index: Integer): string;
procedure SetField(Index: Integer; const Value: string);
function GetOrderBy: string;
protected
FFields: TFieldNames;
public
function GetSQL(AutoFree: Boolean = False): string; override;
procedure AddField(Name: string; IFNULL: string = '');
procedure AddAsField(Value: string; Alias: string = '');
procedure AddFieldCount(Name: string; Alias: string = '');
procedure InnerJoin(JoinTable, BaseField, JoinField: string);
procedure LeftJoin(JoinTable, BaseField, JoinField: string; AndWhere: string = '');
procedure RightJoin(JoinTable, BaseField, JoinField: string);
procedure OrderBy(FieldName: string; DESC: Boolean = False);
procedure Clear; override;
constructor Create; override;
destructor Destroy; override;
property Fields[Index: Integer]: string read GetField write SetField;
property Distinct: Boolean read FDistinct write FDistinct;
property Limit: Integer read FLimitInt write FLimitInt;
property OrderByStr: string read GetOrderBy;
property TableName;
end;
TDelete = class(SQL)
public
function GetSQL(AutoFree: Boolean = False): string; override;
procedure Clear; override;
constructor Create; override;
destructor Destroy; override;
property Where;
property TableName;
end;
TDropTable = class(BaseSQL)
public
function GetSQL(AutoFree: Boolean = False): string;
procedure EndCreate;
procedure Clear;
constructor Create; virtual;
property TableName;
end;
TUpdateBlob = class(SQL)
private
FBlobField: string;
public
function GetSQL(AutoFree: Boolean = False): string; override;
property BlobField: string read FBlobField write FBlobField;
property Where;
property TableName;
end;
function Field(Name: string; FieldType: TFieldType; PrimaryKey, NotNull, AutoInc: Boolean): TField;
function InsertValue(Name: string; FieldType: TFieldType; Value: Variant): TInsertValue;
function FloatToSQLStr(Value: Extended): string;
function FieldTypeToStr(Value: TFieldType): string;
function FieldTypeToString(Value: TFieldType): string;
implementation
uses
System.StrUtils;
function QuotedStr(const S: string): string;
var
I: Integer;
begin
Result := S;
if Result = '?' then
Exit;
for I := Result.Length - 1 downto 0 do
if Result.Chars[I] = '''' then
Result := Result.Insert(I, '''');
Result := '''' + Result + '''';
end;
function FloatToSQLStr(Value: Extended): string;
begin
Result := FloatToStr(Value);
Result := StringReplace(Result, ',', '.', [rfReplaceAll]);
end;
function BoolToSQLStr(Value: Boolean): string;
begin
if Value then
Exit('1')
else
Exit('0');
end;
function FieldTypeToStr(Value: TFieldType): string;
begin
case Value of
ftInteger:
Result := 'INTEGER';
ftString:
Result := 'TEXT';
ftFloat:
Result := 'REAL';
ftDateTime:
Result := 'REAL';
ftBlob:
Result := 'BLOB';
ftBoolean:
Result := 'INTEGER';
end;
end;
function FieldTypeToString(Value: TFieldType): string;
begin
case Value of
ftInteger:
Result := 'ftInteger';
ftString:
Result := 'ftString';
ftFloat:
Result := 'ftFloat';
ftDateTime:
Result := 'ftDateTime';
ftBlob:
Result := 'ftBlob';
ftBoolean:
Result := 'ftBoolean';
end;
end;
function Field;
begin
Result.FieldType := FieldType;
Result.FieldName := Name;
Result.PrimaryKey := PrimaryKey;
Result.AutoInc := AutoInc;
Result.NotNull := NotNull;
end;
function InsertValue(Name: string; FieldType: TFieldType; Value: Variant): TInsertValue; overload;
begin
Result.FieldName := Name;
Result.FieldType := FieldType;
Result.Value := Value;
end;
function DIValue(Name: string; FieldType: TFieldType; DIType: TDIType; Value: Variant): TDIValue; overload;
begin
Result.FieldName := Name;
Result.FieldType := FieldType;
Result.DIType := DIType;
Result.Value := Value;
end;
{ SQL }
class function SQL.CreateField: TField;
begin
Result := Field('', ftInteger, False, False, False);
end;
class function SQL.Delete: TDelete;
begin
Result := TDelete.Create;
end;
class function SQL.CreateTable(TableName: string; IfNotExists: Boolean): TTable;
begin
Result := TTable.Create;
Result.IfNotExists := IfNotExists;
Result.TableName := TableName;
end;
class function SQL.Delete(TableName: string): TDelete;
begin
Result := TDelete.Create;
Result.TableName := TableName;
end;
destructor SQL.Destroy;
begin
FUWheres.Free;
inherited;
end;
class function SQL.DropTable(TableName: string): TDropTable;
begin
Result := TDropTable.Create;
Result.TableName := TableName;
end;
class function SQL.DropTable: TDropTable;
begin
Result := TDropTable.Create;
end;
class function SQL.InsertInto(TableName: string): TInsertInto;
begin
Result := TInsertInto.Create;
Result.TableName := TableName;
end;
class function SQL.InsertInto: TInsertInto;
begin
Result := TInsertInto.Create;
end;
class function SQL.InsertOrReplace(TableName: string): TInsertInto;
begin
Result := InsertOrReplace;
Result.TableName := TableName;
end;
class function SQL.InsertOrReplace: TInsertInto;
begin
Result := TInsertInto.Create;
Result.OrReplace := True;
end;
class function SQL.PRAGMA(Key, Value: string): string;
begin
Result := 'PRAGMA ' + Key + ' = "' + Value + '"';
end;
class function SQL.Select(TableName: string; Fields: TArray<string>): TSelect;
var
i: Integer;
begin
Result := TSelect.Create;
Result.TableName := TableName;
if Length(Fields) > 0 then
begin
for i := Low(Fields) to High(Fields) do
Result.AddField(Fields[i]);
end;
end;
class function SQL.Select(TableName, Field: string): TSelect;
var
FFields: TArray<string>;
begin
if not Field.IsEmpty then
begin
SetLength(FFields, 1);
FFields[0] := Field;
end;
Result := SQL.Select(TableName, FFields);
end;
class function SQL.SelectLastInsertID: string;
begin
Result := 'SELECT LAST_INSERT_ID();';
end;
class function SQL.Select: TSelect;
begin
Result := TSelect.Create;
end;
class function SQL.Update: TUpdate;
begin
Result := TUpdate.Create;
end;
class function SQL.Update(TableName: string): TUpdate;
begin
Result := TUpdate.Create;
Result.TableName := TableName;
end;
class function SQL.UpdateBlob: TUpdateBlob;
begin
Result := TUpdateBlob.Create;
end;
class function SQL.UpdateBlob(TableName: string): TUpdateBlob;
begin
Result := TUpdateBlob.Create;
Result.TableName := TableName;
end;
class function SQL.UpdateBlob(TableName, FieldName: string): TUpdateBlob;
begin
Result := TUpdateBlob.Create;
Result.TableName := TableName;
Result.BlobField := FieldName;
end;
{ TTable }
constructor TTable.Create(ATableName: string; AIfNotExists: Boolean);
begin
inherited Create;
FFields := TFields.Create;
if not ATableName.IsEmpty then
TableName := ATableName;
IfNotExists := AIfNotExists;
end;
destructor TTable.Destroy;
begin
FFields.Free;
inherited;
end;
function TTable.GetField(Index: Integer): TField;
begin
Result := FFields[Index];
end;
function TTable.GetSQL(AutoFree: Boolean = False): string;
var
i: Integer;
begin
Result := 'CREATE TABLE';
if FIfNotExists then
Result := Result + ' IF NOT EXISTS';
Result := Result + ' ' + TableName + ' (';
for i := 0 to FFields.Count - 1 do
begin
Result := Result + FFields[i].GetSQL;
if i <> FFields.Count - 1 then
Result := Result + ', ';
end;
Result := Result + ')';
if AutoFree then
EndCreate;
end;
procedure TTable.AddField;
begin
FFields.Add(Field(Name, FieldType, PrimaryKey, NotNull, AutoInc));
end;
procedure TTable.Clear;
begin
TableName := '';
FFields.Clear;
end;
procedure TTable.EndCreate;
begin
Clear;
Free;
end;
procedure TTable.SetField(Index: Integer; const Value: TField);
begin
FFields[Index] := Value;
end;
{ TField }
function TField.GetSQL: string;
begin
Result := FieldName + ' ' + FieldTypeToStr(FieldType);
if PrimaryKey then
Result := Result + ' PRIMARY KEY';
if NotNull then
Result := Result + ' NOT NULL';
if AutoInc then
Result := Result + ' AUTOINCREMENT';
end;
{ TInsertInto }
constructor TInsertInto.Create;
begin
inherited;
FDoubleDoubleQuote := False;
FFieldValues := TInsertValues.Create;
end;
destructor TInsertInto.Destroy;
begin
FFieldValues.Free;
inherited;
end;
function TInsertInto.GetSQL(AutoFree: Boolean = False): string;
var
i: Integer;
begin
Result := 'INSERT ' + IfThen(FOrReplace, 'OR REPLACE') + ' INTO ' + TableName + ' (';
for i := 0 to FFieldValues.Count - 1 do
begin
Result := Result + FFieldValues[i].FieldName;
if i <> FFieldValues.Count - 1 then
Result := Result + ', ';
end;
Result := Result + ') VALUES (';
for i := 0 to FFieldValues.Count - 1 do
begin
case FFieldValues[i].FieldType of
ftInteger:
Result := Result + QuotedStr(IntToStr(FFieldValues[i].Value));
ftString:
Result := Result + QuotedStr(FFieldValues[i].Value);
ftFloat:
Result := Result + QuotedStr(FloatToSQLStr(FFieldValues[i].Value));
ftDateTime:
Result := Result + QuotedStr(FloatToSQLStr(FFieldValues[i].Value));
ftBoolean:
Result := Result + QuotedStr(BoolToSQLStr(FFieldValues[i].Value));
ftParam:
Result := Result + FFieldValues[i].Value;
end;
if i <> FFieldValues.Count - 1 then
Result := Result + ', ';
end;
Result := Result + ')';
if AutoFree then
EndCreate;
end;
procedure TInsertInto.SetDoubleDoubleQuote(const Value: Boolean);
begin
FDoubleDoubleQuote := Value;
end;
procedure TInsertInto.AddValue(FieldName: string; FieldValue: Extended);
begin
FFieldValues.Add(InsertValue(FieldName, ftFloat, FieldValue));
end;
procedure TInsertInto.AddValue(FieldName, FieldValue: string);
begin
if FDoubleDoubleQuote then
FieldValue := StringReplace(FieldValue, '"', '""', [rfReplaceAll]);
FFieldValues.Add(InsertValue(FieldName, ftString, FieldValue));
end;
procedure TInsertInto.AddValue(FieldName: string; FieldValue: Integer);
begin
FFieldValues.Add(InsertValue(FieldName, ftInteger, FieldValue));
end;
procedure TInsertInto.AddValue(FieldName: string; FieldValue: Boolean);
begin
FFieldValues.Add(InsertValue(FieldName, ftBoolean, FieldValue));
end;
procedure TInsertInto.AddValueAsParam(FieldName: string; ParamChar: string = ':'; CharOnly: Boolean = False);
begin
if CharOnly then
FFieldValues.Add(InsertValue(FieldName, ftParam, ParamChar))
else
FFieldValues.Add(InsertValue(FieldName, ftParam, ParamChar + FieldName));
end;
procedure TInsertInto.AddValue(FieldName: string; FieldValue: TDateTime);
begin
FFieldValues.Add(InsertValue(FieldName, ftDateTime, FieldValue));
end;
procedure TInsertInto.Clear;
begin
TableName := '';
FOrReplace := False;
FDoubleDoubleQuote := False;
FFieldValues.Clear;
end;
procedure TInsertInto.EndCreate;
begin
Clear;
Free;
end;
{ TSelect }
procedure TSelect.AddAsField(Value, Alias: string);
begin
if Alias <> '' then
Value := Value + ' as ' + Alias;
FFields.Add(Value);
end;
procedure TSelect.AddField(Name: string; IFNULL: string);
begin
if IFNULL.IsEmpty then
FFields.Add(Name)
else
FFields.Add('IFNULL(' + Name + ', ' + IFNULL + ')');
end;
procedure TSelect.AddFieldCount(Name, Alias: string);
begin
Name := 'COUNT(' + Name + ')';
if Alias <> '' then
Name := Name + ' as ' + Alias;
FFields.Add(Name);
end;
procedure TSelect.Clear;
begin
FName := '';
FFields.Clear;
FJoins.Clear;
FOrderBy.Clear;
end;
constructor TSelect.Create;
begin
inherited;
FFields := TFieldNames.Create;
FJoins := TStringList.Create;
FOrderBy := TStringList.Create;
FLimitInt := 0;
end;
destructor TSelect.Destroy;
begin
FFields.Free;
FJoins.Free;
FOrderBy.Free;
inherited;
end;
function TSelect.GetField(Index: Integer): string;
begin
Result := FFields[Index];
end;
function TSelect.GetOrderBy: string;
var
i: Integer;
begin
if FOrderBy.Count <= 0 then
Exit('');
Result := ' ORDER BY ';
for i := 0 to FOrderBy.Count - 1 do
begin
Result := Result + FOrderBy[i];
if i <> FOrderBy.Count - 1 then
Result := Result + ', ';
end;
end;
function TSelect.GetSQL(AutoFree: Boolean = False): string;
var
i: Integer;
FieldsStr, ALimit, AJoins: string;
begin
if FLimitInt > 0 then
ALimit := ' LIMIT ' + IntToStr(FLimitInt);
if FDistinct then
Result := 'SELECT DISTINCT '
else
Result := 'SELECT ';
FieldsStr := '';
for i := 0 to FFields.Count - 1 do
begin
FieldsStr := FieldsStr + FFields[i];
if i <> FFields.Count - 1 then
FieldsStr := FieldsStr + ', ';
end;
AJoins := '';
for i := 0 to FJoins.Count - 1 do
AJoins := AJoins + FJoins[i] + ' ';
if AJoins <> '' then
AJoins := ' ' + AJoins;
Result := Result + FieldsStr;
if not TableName.IsEmpty then
Result := Result + ' FROM ' + TableName;
Result := Result + AJoins + Where + OrderByStr + ALimit;
if AutoFree then
EndCreate;
end;
procedure TSelect.InnerJoin(JoinTable, BaseField, JoinField: string);
begin
FJoins.Add('INNER JOIN ' + JoinTable + ' ON ' + FName + '.' + BaseField + '=' + JoinTable + '.' + JoinField);
end;
procedure TSelect.LeftJoin(JoinTable, BaseField, JoinField: string; AndWhere: string = '');
begin
if AndWhere.Length > 0 then
AndWhere := ' and ' + AndWhere;
FJoins.Add('LEFT JOIN ' + JoinTable + ' ON ' + FName + '.' + BaseField + '=' + JoinTable + '.' + JoinField + AndWhere);
end;
procedure TSelect.OrderBy(FieldName: string; DESC: Boolean);
begin
if DESC then
FieldName := FieldName + ' DESC';
FOrderBy.Add(FieldName);
end;
procedure TSelect.RightJoin(JoinTable, BaseField, JoinField: string);
begin
FJoins.Add('RIGHT JOIN ' + JoinTable + ' ON ' + FName + '.' + BaseField + '=' + JoinTable + '.' + JoinField);
end;
procedure TSelect.SetField(Index: Integer; const Value: string);
begin
FFields[Index] := Value;
end;
{ TDelete }
procedure TDelete.Clear;
begin
FName := '';
end;
constructor TDelete.Create;
begin
inherited;
end;
destructor TDelete.Destroy;
begin
//
inherited;
end;
function TDelete.GetSQL(AutoFree: Boolean = False): string;
begin
Result := 'DELETE FROM ' + FName + Where;
if AutoFree then
EndCreate;
end;
{ TUpdate }
procedure TUpdate.AddValue(FieldName: string; FieldValue: Extended);
begin
FFieldValues.Add(InsertValue(FieldName, ftFloat, FieldValue));
end;
procedure TUpdate.AddValue(FieldName, FieldValue: string);
begin
if FDoubleDoubleQuote then
FieldValue := StringReplace(FieldValue, '"', '""', [rfReplaceAll]);
FFieldValues.Add(InsertValue(FieldName, ftString, FieldValue));
end;
procedure TUpdate.AddValue(FieldName: string; FieldValue: Integer);
begin
FFieldValues.Add(InsertValue(FieldName, ftInteger, FieldValue));
end;
procedure TUpdate.AddValue(FieldName: string; FieldValue: Boolean);
begin
FFieldValues.Add(InsertValue(FieldName, ftBoolean, FieldValue));
end;
procedure TUpdate.AddValueAsParam(FieldName: string; ParamChar: string = ':'; CharOnly: Boolean = False);
begin
if CharOnly then
FFieldValues.Add(InsertValue(FieldName, ftParam, ParamChar))
else
FFieldValues.Add(InsertValue(FieldName, ftParam, ParamChar + FieldName));
end;
procedure TUpdate.AddValue(FieldName: string; FieldValue: TDateTime);
begin
FFieldValues.Add(InsertValue(FieldName, ftDateTime, FieldValue));
end;
procedure TUpdate.Clear;
begin
TableName := '';
FFieldValues.Clear;
end;
constructor TUpdate.Create;
begin
inherited;
FDoubleDoubleQuote := False;
FFieldValues := TInsertValues.Create;
FDIValues := TDIValues.Create;
end;
procedure TUpdate.DecValue(FieldName: string; Value: Extended);
begin
FDIValues.Add(DIValue(FieldName, ftFloat, diDec, Value));
end;
destructor TUpdate.Destroy;
begin
FFieldValues.Free;
FDIValues.Free;
inherited;
end;
procedure TUpdate.DivValue(FieldName: string; Value: Integer);
begin
FDIValues.Add(DIValue(FieldName, ftInteger, diDiv, Value));
end;
procedure TUpdate.DivValue(FieldName: string; Value: Extended);
begin
FDIValues.Add(DIValue(FieldName, ftFloat, diDiv, Value));
end;
procedure TUpdate.DecValue(FieldName: string; Value: Integer);
begin
FDIValues.Add(DIValue(FieldName, ftInteger, diDec, Value));
end;
procedure TUpdate.IncValue(FieldName: string; Value: Extended);
begin
FDIValues.Add(DIValue(FieldName, ftFloat, diInc, Value));
end;
procedure TUpdate.IncValue(FieldName: string; Value: Integer);
begin
FDIValues.Add(DIValue(FieldName, ftInteger, diInc, Value));
end;
procedure TUpdate.MulValue(FieldName: string; Value: Integer);
begin
FDIValues.Add(DIValue(FieldName, ftInteger, diMul, Value));
end;
procedure TUpdate.MulValue(FieldName: string; Value: Extended);
begin
FDIValues.Add(DIValue(FieldName, ftFloat, diMul, Value));
end;
procedure TUpdate.SetDoubleDoubleQuote(const Value: Boolean);
begin
FDoubleDoubleQuote := Value;
end;
function TUpdate.GetSQL(AutoFree: Boolean = False): string;
var
i: Integer;
str: string;
begin
Result := 'UPDATE ' + TableName + ' SET ';
for i := 0 to FFieldValues.Count - 1 do
begin
str := '';
case FFieldValues[i].FieldType of
ftInteger:
str := QuotedStr(IntToStr(FFieldValues[i].Value));
ftString:
str := QuotedStr(FFieldValues[i].Value);
ftFloat:
str := QuotedStr(FloatToSQLStr(FFieldValues[i].Value));
ftDateTime:
str := QuotedStr(FloatToSQLStr(FFieldValues[i].Value));
ftBoolean:
str := QuotedStr(BoolToSQLStr(FFieldValues[i].Value));
ftParam:
str := FFieldValues[i].Value;
end;
Result := Result + FFieldValues[i].FieldName + ' = ' + str;
if i <> FFieldValues.Count - 1 then
Result := Result + ', ';
end;
for i := 0 to FDIValues.Count - 1 do
begin
str := '';
case FDIValues[i].FieldType of
ftInteger:
str := QuotedStr(IntToStr(FDIValues[i].Value));
ftFloat:
str := QuotedStr(FloatToSQLStr(FDIValues[i].Value));
end;
case FDIValues[i].DIType of
diInc:
Result := Result + FDIValues[i].FieldName + ' = ' + FDIValues[i].FieldName + ' + ' + str;
diDec:
Result := Result + FDIValues[i].FieldName + ' = ' + FDIValues[i].FieldName + ' - ' + str;
diMul:
Result := Result + FDIValues[i].FieldName + ' = ' + FDIValues[i].FieldName + ' * ' + str;
diDiv:
Result := Result + FDIValues[i].FieldName + ' = ' + FDIValues[i].FieldName + ' / ' + str;
end;
if i <> FDIValues.Count - 1 then
Result := Result + ', ';
end;