-
Notifications
You must be signed in to change notification settings - Fork 29
Expand file tree
/
Copy pathData.QueryIntf.pas
More file actions
896 lines (769 loc) · 21.8 KB
/
Data.QueryIntf.pas
File metadata and controls
896 lines (769 loc) · 21.8 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
{ *************************************************************************** }
{ }
{ }
{ Copyright (C) Amarildo Lacerda }
{ }
{ https://github.com/amarildolacerda }
{ }
{ }
{ *************************************************************************** }
{ }
{ 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. }
{ }
{ *************************************************************************** }
{
Alterações:
25/03/16 - Primeira versão publicada - Construção
25/07/16 - refactoring na interface para acoplar o dataset.. Connector
}
unit Data.QueryIntf;
interface
uses
System.SysUtils, System.Classes, FireDAC.Stan.Intf, FireDAC.Stan.Option,
System.Rtti, FireDAC.Stan.Param,
FireDAC.Stan.Error, FireDAC.UI.Intf, FireDAC.Phys.Intf, FireDAC.Stan.Def,
FireDAC.Stan.Pool, FireDAC.Stan.Async, FireDAC.Phys, FireDAC.Stan.ExprFuncs,
{FireDAC.Phys.SQLiteDef,} FireDAC.Comp.UI,
FireDAC.Phys.SQLite, Data.DB, FireDAC.Comp.Client;
type
TDatasetClass = class of TDataset;
IQuery = interface;
// TQueryIntf = class;
TDataStorageRec = record
class function NewQuery(const Conn: TFDCustomConnection): IQuery;
overload; static;
class function NewQuery(): IQuery; overload; static;
class function NewQuery(const ATable: string; const AFields: String = '*';
const AWhere: String = ''; const AGroup: string = '';
const AOrderBy: string = ''; const AJoin: String = ''): IQuery;
overload; static;
class function SqlBuilder(const ATable: string; const AFields: String = '*';
const AWhere: String = ''; const AGroup: String = '';
const AOrderBy: string = ''; const AJoin: String = ''): string;
overload; static;
end;
IJsonDataset = interface
['{535F0BD0-6535-452B-9E84-6A9BFCFCEB91}']
function ToJson: string;
end;
IDataset = interface(IJsonDataset)
procedure Next;
procedure Prior;
procedure Last;
procedure First;
function eof: boolean;
function bof: boolean;
procedure Insert;
procedure Append;
procedure post;
procedure edit;
procedure delete;
function GetActiveQuery: boolean;
procedure SetActiveQuery(const Value: boolean);
property Active: boolean read GetActiveQuery Write SetActiveQuery;
function GetFields: TFields;
property Fields: TFields read GetFields;
function RowsAffected: integer;
function GetCommand: string;
procedure SetCommand(const ACommand: string);
property Command: string read GetCommand write SetCommand;
end;
IDatasetIntf = interface(IDataset)
function Open: IQuery;
function Close: IQuery;
function StartTransaction: IQuery;
function Commit: IQuery;
function Rollback: IQuery;
function RecordCountInt: integer;
property RecordCount: integer read RecordCountInt;
end;
IQuery = Interface(IDatasetIntf)
['{4C9E016E-41A2-42D1-899E-D820FE4FA9C4}']
function ExecSql(const AScript: string): IQuery;
function GetParams: TFDParams;
property Params: TFDParams read GetParams;
// Enumerator
function GetEnumerator: IQuery;
function GetCurrent: TFields;
property Current: TFields read GetCurrent;
function MoveNext: boolean;
procedure Reset;
// Open
function Open(const AProc: TProc<TFDParams>): IQuery; overload;
function DoLoop(const AProc: TProc<TDataset>): IQuery;
function DoQuery(const AProc: TProc<TDataset>): IQuery;
function Clone: IQuery;
// Filters
function ConnectName(texto: String): IQuery;
function Where(const texto: string): IQuery;
function Table(const texto: string): IQuery;
function Join(const texto: string): IQuery;
function GroupBy(const texto: string): IQuery;
function OrderBy(const texto: string): IQuery;
function AndWhere(const texto: string): IQuery;
function OrWhere(const texto: string): IQuery;
function FilterResult(const texto: string): IQuery;
function FieldNames(const texto: string): IQuery;
function ParamValue(const nome: string; valor: variant): IQuery;
function FieldValue(const nome: string; valor: variant): IQuery;
function GetCmdFields: string;
function GetCmdJoin: string;
function GetCmdOrderBy: string;
function GetCmdTable: string;
function GetCmdWhere: string;
procedure SetCmdWhere(const Value: string);
procedure SetCmdFiedls(const Value: string);
procedure SetCmdJoin(const Value: string);
procedure SetCmdOrderBy(const Value: string);
procedure SetCmdTable(const Value: string);
function GetCmdGroup: String;
procedure SetCmdGroup(const Value: String);
property CmdWhere: string read GetCmdWhere write SetCmdWhere;
property CmdTable: string read GetCmdTable write SetCmdTable;
property CmdFields: string read GetCmdFields write SetCmdFiedls;
property CmdJoin: string read GetCmdJoin write SetCmdJoin;
property CmdOrderBy: string read GetCmdOrderBy write SetCmdOrderBy;
property CmdGroup: string read GetCmdGroup write SetCmdGroup;
function GetDataset: TFDQuery;
procedure SetDataset(const Value: TFDQuery);
property Dataset: TFDQuery read GetDataset write SetDataset;
End;
(* IDatabase = interface
['{09A79486-12B6-4944-9F4C-8CF841D901E5}']
function Open:IDatabase;
function Close:IDatabase;
function ParamValue(sParam:string;value:variant):IDatabase;
function Driver(ADriver:string):IDatabase;
function ConnectName(ADBname:string):IDatabase;
function LoginParam(AUser:string;APass:String):IDatabase;
end;
TDatabaseIntf = class(TComponent,IDatabase)
private
FDatabase:TFDConnection;
public
class function new:IDatabase;
function Open:IDatabase;virtual;
function Close:IDatabase;virtual;
function ParamValue(sParam:string;value:variant):IDatabase;
function Driver(ADriver:string):IDatabase;
function ConnectName(ADBname:string):IDatabase;
function LoginParam(AUser:string;APass:String):IDatabase;
end;
*)
TQueryIntfBase = TFDQuery;
TQueryIntf<T:TFDQuery> = class(TComponent, IQuery)
private
FFreeOnDestroy: boolean;
FScrollRow: integer;
FDataset: TFDQuery;
FWhere: string;
FGroup: string;
FJoin: string;
FTable: string;
FFieldNames: string;
FOrderBy: string;
procedure SetFieldByName(const AField: string; const AValue: variant);
function GetActiveQuery: boolean;
procedure SetActiveQuery(const Value: boolean);
function GetCmdFields: string;
function GetCmdJoin: string;
function GetCmdOrderBy: string;
function GetCmdTable: string;
function GetCmdWhere: string;
procedure SetCmdWhere(const Value: string);
procedure SetCmdFiedls(const Value: string);
procedure SetCmdJoin(const Value: string);
procedure SetCmdOrderBy(const Value: string);
procedure SetCmdTable(const Value: string);
procedure Next;
procedure Prior;
procedure Last;
procedure First;
procedure Insert;
procedure Append;
procedure Reset;
function Clone: IQuery;
function Commit: IQuery;
function DoLoop(const AProc: TProc<TDataset>): IQuery;
function DoQuery(const AProc: TProc<TDataset>): IQuery;
function ExecSql(const AScript: string): IQuery;
function Open(const AProc: TProc<TFDParams>): IQuery; overload;
function Open: IQuery; overload;
function Rollback: IQuery;
function StartTransaction: IQuery;
function MoveBy(Distance: integer): integer;
procedure post;
procedure edit;
procedure delete;
function GetCmdGroup: String;
procedure SetCmdGroup(const Value: String);
procedure SetDataset(Const ADataset: TFDQuery);
function GetDataset: TFDQuery;
procedure SetFreeOnDestroy(const Value: boolean);
protected
public
property FreeOnDestroy:boolean read FFreeOnDestroy write SetFreeOnDestroy;
function GetQuery: T;
property Dataset: TFDQuery read GetDataset Write SetDataset;
class function New: IQuery; overload; static;
class function New(ADataset: T): IQuery; overload; static;
class function New(const AConnection: TFDCustomConnection): IQuery;
overload;
function Close: IQuery; virtual;
function ConnectName(texto: String): IQuery; virtual;
function RowsAffected: integer;
function eof: boolean;
function bof: boolean;
function GetCurrent: TFields;
function MoveNext: boolean;
function GetCommand: string;
procedure SetCommand(const ACommand: string);
procedure SetConnectionIntf(const AConn: TFDCustomConnection);
function GetConnectionIntf: TFDCustomConnection;
function GetFields: TFields;
function GetEnumerator: IQuery;
function GetParams: TFDParams;
function ParamValue(const nome: string; valor: variant): IQuery;
function FieldValue(const nome: string; valor: variant): IQuery;
function NewQuery(ATable, AFields, AWhere, AGroup, AOrderBy,
AJoin: String): IQuery;
constructor create(AOwner: TComponent); overload; override;
constructor create(); overload;
destructor Destroy; override;
function RebuildSql: IQuery;
function GroupBy(const texto: string): IQuery;
function Where(const texto: string): IQuery; virtual;
function Table(const texto: string): IQuery; virtual;
function FieldNames(const texto: string): IQuery; virtual;
function Join(const texto: string): IQuery; virtual;
function OrderBy(const texto: string): IQuery; virtual;
function AndWhere(const texto: string): IQuery; virtual;
function OrWhere(const texto: string): IQuery; virtual;
function FilterResult(const texto: string): IQuery;
function RecordCountInt: integer;
function ToJson: string;
property CmdFields: string read GetCmdFields write SetCmdFiedls;
property CmdTable: string read GetCmdTable write SetCmdTable;
property CmdWhere: string read GetCmdWhere write SetCmdWhere;
property CmdJoin: string read GetCmdJoin write SetCmdJoin;
property CmdGroup: String read GetCmdGroup write SetCmdGroup;
property CmdOrderBy: string read GetCmdOrderBy write SetCmdOrderBy;
end;
implementation
{ TQueyIntf }
function TQueryIntf<T>.Rollback: IQuery;
begin
TQueryIntfBase(FDataset) .Connection.Rollback;
result := self;
end;
function TQueryIntf<T>.RowsAffected: integer;
begin
result := TQueryIntfBase(FDataset).RowsAffected;
end;
function TQueryIntf<T>.AndWhere(const texto: string): IQuery;
begin
result := self;
if FWhere <> '' then
FWhere := FWhere + ' and ';
FWhere := FWhere + texto;
RebuildSql;
end;
procedure TQueryIntf<T>.Append;
begin
TQueryIntfBase(FDataset).Append;
end;
function TQueryIntf<T>.bof: boolean;
begin
result := TQueryIntfBase(FDataset).bof;
end;
function TQueryIntf<T>.Clone: IQuery;
begin
result := TQueryIntf<T>.create(nil) as IQuery;
if result.CmdTable <> '' then
begin
result.CmdTable := self.CmdTable;
result.CmdWhere := self.CmdWhere;
result.CmdJoin := self.CmdJoin;
result.CmdFields := self.CmdFields;
result.CmdOrderBy := self.CmdOrderBy;
result.CmdGroup := self.CmdGroup;
end
else
result.Dataset.sql.assign(TQueryIntfBase(FDataset).sql);
result.Dataset.Connection := self.Dataset.Connection;
end;
function TQueryIntf<T>.Close: IQuery;
begin
result := self;
TQueryIntfBase(FDataset).Close;
end;
function TQueryIntf<T>.Commit: IQuery;
begin
result := self;
Dataset.Connection.Commit;
end;
function TQueryIntf<T>.ConnectName(texto: String): IQuery;
begin
result := self;
Dataset.ConnectionName := texto;
end;
constructor TQueryIntf<T>.create();
begin
inherited create(nil);
FFreeOnDestroy := true;
FDataset := T.Create(nil);
if not supports(FDataset, IQuery) then
begin
FreeAndNil(FDataset);
raise exception.create('Não suporta interface IQuery');
end;
end;
constructor TQueryIntf<T>.create(AOwner: TComponent);
begin
inherited;
FFreeOnDestroy := false;
if supports(AOwner, IJsonDataset) then
FDataset := T(AOwner)
else
begin
FDataset := T.create(self);
if not supports(FDataset, IQuery) then
begin
FreeAndNil(FDataset);
raise exception.create('Não suporta interface IQuery');
end;
FFreeOnDestroy := true;
end;
FFieldNames := '*';
FScrollRow := -1;
end;
procedure TQueryIntf<T>.delete;
begin
FDataset.delete;
end;
destructor TQueryIntf<T>.Destroy;
begin
if FFreeOnDestroy then
FreeAndNil(FDataset);
inherited;
end;
function TQueryIntf<T>.DoLoop(const AProc: TProc<TDataset>): IQuery;
var
book: TBookmark;
begin
result := self;
FDataset.DisableControls;
try
book := FDataset.GetBookmark;
try
First;
while not eof do
begin
try
AProc(self.FDataset);
Next;
except
break;
end;
end;
finally
FDataset.GotoBookmark(book);
FDataset.FreeBookmark(book);
end;
finally
FDataset.EnableControls;
end;
end;
function TQueryIntf<T>.DoQuery(const AProc: TProc<TDataset>): IQuery;
begin
result := self;
AProc(self.FDataset);
end;
procedure TQueryIntf<T>.edit;
begin
FDataset.edit;
end;
function TQueryIntf<T>.eof: boolean;
begin
result := FDataset.eof;
end;
function TQueryIntf<T>.ExecSql(const AScript: string): IQuery;
begin
result := self;
FDataset.sql.Text := AScript;
FDataset.Execute();
end;
function TQueryIntf<T>.FieldNames(const texto: string): IQuery;
begin
result := self;
FFieldNames := texto;
RebuildSql;
end;
function TQueryIntf<T>.FieldValue(const nome: string; valor: variant): IQuery;
var
fld: TField;
begin
result := self;
fld := FDataset.FindField(nome);
if fld <> nil then
fld.Value := valor;
end;
function TQueryIntf<T>.FilterResult(const texto: string): IQuery;
begin
result := self;
FDataset.Filter := texto;
FDataset.Filtered := texto <> '';
end;
procedure TQueryIntf<T>.First;
begin
FDataset.First;
end;
function TQueryIntf<T>.Join(const texto: string): IQuery;
begin
result := self;
FJoin := texto;
RebuildSql;
end;
procedure TQueryIntf<T>.Last;
begin
FDataset.Last;
end;
function TQueryIntf<T>.GetActiveQuery: boolean;
begin
result := FDataset.Active;
end;
function TQueryIntf<T>.GetCommand: string;
begin
result := FDataset.sql.Text;
end;
function TQueryIntf<T>.GetConnectionIntf: TFDCustomConnection;
begin
result := FDataset.Connection;
end;
function TQueryIntf<T>.GetCurrent: TFields;
begin
result := FDataset.Fields;
end;
function TQueryIntf<T>.GetDataset: TFDQuery;
begin
result := T(FDataset);
end;
function TQueryIntf<T>.GetEnumerator: IQuery;
begin
result := self as IQuery;
end;
function TQueryIntf<T>.GetFields: TFields;
begin
result := FDataset.Fields;
end;
function TQueryIntf<T>.GetParams: TFDParams;
begin
result := FDataset.Params;
end;
function TQueryIntf<T>.GetQuery: T;
begin
result := T(FDataset);
end;
function TQueryIntf<T>.GroupBy(const texto: string): IQuery;
begin
result := self;
FGroup := texto;
RebuildSql;
end;
procedure TQueryIntf<T>.Insert;
begin
FDataset.Insert;
end;
function TQueryIntf<T>.GetCmdFields: string;
begin
result := FFieldNames;
end;
function TQueryIntf<T>.GetCmdGroup: String;
begin
result := FGroup;
end;
function TQueryIntf<T>.GetCmdJoin: string;
begin
result := FJoin;
end;
function TQueryIntf<T>.GetCmdOrderBy: string;
begin
result := FOrderBy;
end;
function TQueryIntf<T>.GetCmdTable: string;
begin
result := FTable;
end;
function TQueryIntf<T>.GetCmdWhere: string;
begin
result := FWhere;
end;
function TQueryIntf<T>.MoveBy(Distance: integer): integer;
begin
result := FDataset.MoveBy(Distance);
if eof then
FScrollRow := -1;
end;
function TQueryIntf<T>.MoveNext: boolean;
begin
if FScrollRow >= 0 then
Next;
inc(FScrollRow);
result := not eof;
end;
class function TQueryIntf<T>.New: IQuery;
begin
result := TQueryIntf<T>.create(nil) as IQuery;
end;
class function TQueryIntf<T>.New(ADataset: T): IQuery;
begin
result := TQueryIntf<T>.create(ADataset) as IQuery;
end;
class function TQueryIntf<T>.New(const AConnection
: TFDCustomConnection): IQuery;
begin
result := New() as IQuery;
result.Dataset.Connection := AConnection;
end;
function TQueryIntf<T>.NewQuery(ATable, AFields, AWhere, AGroup, AOrderBy,
AJoin: String): IQuery;
begin
result := New;
// TDataStorageRec.NewQuery(ATable, AFields, AWhere,AGroup, AOrderBy, AJoin);
result.CmdTable := ATable;
result.CmdWhere := AWhere;
result.CmdFields := AFields;
result.CmdJoin := AJoin;
result.CmdGroup := AGroup;
result.CmdOrderBy := AOrderBy;
result.Dataset.Connection := self.Dataset.Connection;
end;
procedure TQueryIntf<T>.Next;
begin
FDataset.Next;
end;
function TQueryIntf<T>.Open(const AProc: TProc<TFDParams>): IQuery;
begin
AProc(GetParams);
result := self;
FDataset.Open;
end;
function TQueryIntf<T>.OrderBy(const texto: string): IQuery;
begin
result := self;
FOrderBy := texto;
RebuildSql;
end;
function TQueryIntf<T>.OrWhere(const texto: string): IQuery;
begin
result := self;
if FWhere <> '' then
FWhere := FWhere + ' or ';
FWhere := FWhere + texto;
RebuildSql;
end;
function TQueryIntf<T>.ParamValue(const nome: string; valor: variant): IQuery;
var
prm: TFDParam;
begin
result := self;
prm := FDataset.FindParam(nome);
if prm <> nil then
prm.Value := valor;
end;
procedure TQueryIntf<T>.post;
begin
FDataset.post;
end;
procedure TQueryIntf<T>.Prior;
begin
FDataset.Prior;
end;
function TQueryIntf<T>.RebuildSql: IQuery;
begin
result := self;
SetCommand(TDataStorageRec.SqlBuilder(FTable, FFieldNames, FWhere, FGroup,
FOrderBy, FJoin));
end;
function TQueryIntf<T>.RecordCountInt: integer;
begin
result := FDataset.RecordCount;
end;
procedure TQueryIntf<T>.Reset;
begin
end;
function TQueryIntf<T>.Open: IQuery;
begin
FDataset.Open;
FScrollRow := -1;
result := self as IQuery;
end;
procedure TQueryIntf<T>.SetActiveQuery(const Value: boolean);
begin
FDataset.Active := Value;
end;
procedure TQueryIntf<T>.SetCommand(const ACommand: string);
begin
FDataset.sql.Text := ACommand;
end;
procedure TQueryIntf<T>.SetConnectionIntf(const AConn: TFDCustomConnection);
begin
FDataset.Connection := AConn;
end;
procedure TQueryIntf<T>.SetDataset(Const ADataset: TFDQuery);
begin
if FFreeOnDestroy and (not ADataset.Equals(FDataset)) then
FreeAndNil(FDataset);
FDataset := T(ADataset);
FFreeOnDestroy := false;
end;
procedure TQueryIntf<T>.SetFieldByName(const AField: string;
const AValue: variant);
begin
FDataset.FieldByName(AField).Value := AValue;
end;
procedure TQueryIntf<T>.SetFreeOnDestroy(const Value: boolean);
begin
FFreeOnDestroy := Value;
end;
procedure TQueryIntf<T>.SetCmdWhere(const Value: string);
begin
FWhere := Value;
RebuildSql;
end;
procedure TQueryIntf<T>.SetCmdFiedls(const Value: string);
begin
FFieldNames := Value;
RebuildSql;
end;
procedure TQueryIntf<T>.SetCmdGroup(const Value: String);
begin
FGroup := Value;
RebuildSql;
end;
procedure TQueryIntf<T>.SetCmdJoin(const Value: string);
begin
FJoin := Value;
RebuildSql;
end;
procedure TQueryIntf<T>.SetCmdOrderBy(const Value: string);
begin
FOrderBy := Value;
RebuildSql;
end;
procedure TQueryIntf<T>.SetCmdTable(const Value: string);
begin
FTable := Value;
RebuildSql;
end;
function TQueryIntf<T>.StartTransaction: IQuery;
begin
result := self;
FDataset.Connection.StartTransaction;
end;
function TQueryIntf<T>.Table(const texto: string): IQuery;
begin
result := self;
FTable := texto;
RebuildSql;
end;
function TQueryIntf<T>.ToJson: string;
var
Intf: IJsonDataset;
begin
if supports(FDataset, IJsonDataset, Intf) then
result := (Intf).ToJson
else
result := '{ }';
end;
function TQueryIntf<T>.Where(const texto: string): IQuery;
begin
result := self;
FWhere := texto;
RebuildSql;
end;
{ TStoreDataStorage }
class function TDataStorageRec.NewQuery: IQuery;
begin
result := TQueryIntf<TFDQuery>.create(nil) as IQuery;
end;
class function TDataStorageRec.NewQuery(const Conn
: TFDCustomConnection): IQuery;
begin
result := TDataStorageRec.NewQuery();
result.Dataset.Connection := Conn;
end;
class function TDataStorageRec.NewQuery(const ATable, AFields, AWhere, AGroup,
AOrderBy, AJoin: String): IQuery;
begin
result := NewQuery();
result.Table(ATable).Where(AWhere).OrderBy(AOrderBy).Join(AJoin)
.FieldNames(AFields);
result.GroupBy(AGroup);
end;
class function TDataStorageRec.SqlBuilder(const ATable, AFields, AWhere, AGroup,
AOrderBy, AJoin: String): string;
var
sql: TStringBuilder;
begin
sql := TStringBuilder.create;
try
if ATable <> '' then
sql.Append('select ' + AFields + ' from ' + ATable);
if AJoin <> '' then
sql.Append(' ' + AJoin);
if AWhere <> '' then
sql.Append(' where ' + AWhere);
if AGroup <> '' then
sql.Append(' group by ' + AGroup);
if AOrderBy <> '' then
sql.Append(' order by ' + AOrderBy);
result := sql.ToString;
finally
sql.Free;
end;
end;
{ TDatabaseIntf }
(*
function TDatabaseIntf.ConnectName(ADBname: string): IDatabase;
begin
result := self;
FDatabase.ConnectionName:=ADBname;
end;
function TDatabaseIntf.Driver(ADriver: string): IDatabase;
begin
result := self;
inherited driverName := ADriver;
end;
function TDatabaseIntf.LoginParam(AUser, APass: String): IDatabase;
begin
result := self
.ParamValue('USER_NAME',AUser)
.ParamValue('Password',APass);
end;
class function TDatabaseIntf.new: IDatabase;
begin
result := TDatabaseIntf.Create(nil) as IDatabase;
end;
function TDatabaseIntf.IOpen: IDatabase;
begin
result := self;
inherited Open;
end;
function TDatabaseIntf.ParamValue(sParam: string; value: variant): IDatabase;
begin
result := self;
Params.Values[sParam] := value;
end;
*)
end.