forked from academiadocodigo/SimpleORM
-
Notifications
You must be signed in to change notification settings - Fork 0
/
SimpleQueryFiredac.pas
110 lines (89 loc) · 2.24 KB
/
SimpleQueryFiredac.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
unit SimpleQueryFiredac;
interface
uses
SimpleInterface, FireDAC.Comp.Client, System.Classes, Data.DB,
FireDAC.Stan.Param, FireDAC.DatS,
FireDAC.DApt.Intf, FireDAC.DApt, FireDAC.Comp.DataSet;
Type
TSimpleQueryFiredac = class(TInterfacedObject, iSimpleQuery)
private
FConnection : TFDConnection;
FQuery : TFDQuery;
FParams : TParams;
public
constructor Create(aConnection : TFDConnection);
destructor Destroy; override;
class function New(aConnection : TFDConnection) : iSimpleQuery;
function SQL : TStrings;
function Params : TParams;
function ExecSQL : iSimpleQuery;
function DataSet : TDataSet;
function Open(aSQL : String) : iSimpleQuery; overload;
function Open : iSimpleQuery; overload;
end;
implementation
uses
System.SysUtils;
{ TSimpleQuery<T> }
constructor TSimpleQueryFiredac.Create(aConnection : TFDConnection);
begin
FQuery := TFDQuery.Create(nil);
FConnection := aConnection;
FQuery.Connection := FConnection;
end;
function TSimpleQueryFiredac.DataSet: TDataSet;
begin
Result := TDataSet(FQuery);
end;
destructor TSimpleQueryFiredac.Destroy;
begin
FreeAndNil(FQuery);
if Assigned(FParams) then
FreeAndNil(FParams);
inherited;
end;
function TSimpleQueryFiredac.ExecSQL: iSimpleQuery;
begin
Result := Self;
if Assigned(FParams) then
FQuery.Params.Assign(FParams);
FQuery.Prepare;
FQuery.ExecSQL;
if Assigned(FParams) then
FreeAndNil(FParams);
end;
class function TSimpleQueryFiredac.New(aConnection : TFDConnection): iSimpleQuery;
begin
Result := Self.Create(aConnection);
end;
function TSimpleQueryFiredac.Open: iSimpleQuery;
begin
Result := Self;
FQuery.Close;
if Assigned(FParams) then
FQuery.Params.Assign(FParams);
FQuery.Prepare;
FQuery.Open;
if Assigned(FParams) then
FreeAndNil(FParams);
end;
function TSimpleQueryFiredac.Open(aSQL: String): iSimpleQuery;
begin
Result := Self;
FQuery.Close;
FQuery.Open(aSQL);
end;
function TSimpleQueryFiredac.Params: TParams;
begin
if not Assigned(FParams) then
begin
FParams := TParams.Create(nil);
FParams.Assign(FQuery.Params);
end;
Result := FParams;
end;
function TSimpleQueryFiredac.SQL: TStrings;
begin
Result := FQuery.SQL;
end;
end.