forked from academiadocodigo/SimpleORM
-
Notifications
You must be signed in to change notification settings - Fork 0
/
SimpleDAO.pas
363 lines (333 loc) · 9.61 KB
/
SimpleDAO.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
unit SimpleDAO;
interface
uses
SimpleInterface,
System.RTTI,
System.Generics.Collections,
System.Classes,
Data.DB,
{$IFNDEF CONSOLE}
{$IFDEF FMX}
FMX.Forms,
{$ELSE}
Vcl.Forms,
{$ENDIF}
{$ENDIF}
SimpleDAOSQLAttribute,
System.Threading;
Type
TSimpleDAO<T: class, constructor> = class(TInterfacedObject, iSimpleDAO<T>)
private
FQuery: iSimpleQuery;
FDataSource: TDataSource;
FSQLAttribute: iSimpleDAOSQLAttribute<T>;
{$IFNDEF CONSOLE}
FForm: TForm;
{$ENDIF}
FList: TObjectList<T>;
function FillParameter(aInstance: T): iSimpleDAO<T>; overload;
function FillParameter(aInstance: T; aId: Variant)
: iSimpleDAO<T>; overload;
procedure OnDataChange(Sender: TObject; Field: TField);
public
constructor Create(aQuery: iSimpleQuery);
destructor Destroy; override;
class function New(aQuery: iSimpleQuery): iSimpleDAO<T>; overload;
function DataSource(aDataSource: TDataSource): iSimpleDAO<T>;
function Insert(aValue: T): iSimpleDAO<T>; overload;
function Update(aValue: T): iSimpleDAO<T>; overload;
function Delete(aValue: T): iSimpleDAO<T>; overload;
function Delete(aField: String; aValue: String): iSimpleDAO<T>;
overload;
function LastID: iSimpleDAO<T>;
function LastRecord: iSimpleDAO<T>;
{$IFNDEF CONSOLE}
function Insert: iSimpleDAO<T>; overload;
function Update: iSimpleDAO<T>; overload;
function Delete: iSimpleDAO<T>; overload;
{$ENDIF}
function Find(aBindList: Boolean = True): iSimpleDAO<T>; overload;
function Find(var aList: TObjectList<T>): iSimpleDAO<T>; overload;
function Find(aId: Integer): T; overload;
function Find(aKey: String; aValue: Variant): iSimpleDAO<T>; overload;
function SQL: iSimpleDAOSQLAttribute<T>;
{$IFNDEF CONSOLE}
function BindForm(aForm: TForm): iSimpleDAO<T>;
{$ENDIF}
end;
implementation
uses
System.SysUtils,
SimpleAttributes,
System.TypInfo,
SimpleRTTI,
SimpleSQL,
Variants;
{ TGenericDAO }
{$IFNDEF CONSOLE}
function TSimpleDAO<T>.BindForm(aForm: TForm): iSimpleDAO<T>;
begin
Result := Self;
FForm := aForm;
end;
{$ENDIF}
constructor TSimpleDAO<T>.Create(aQuery: iSimpleQuery);
begin
FQuery := aQuery;
FSQLAttribute := TSimpleDAOSQLAttribute<T>.New(Self);
FList := TObjectList<T>.Create;
end;
function TSimpleDAO<T>.DataSource(aDataSource: TDataSource): iSimpleDAO<T>;
begin
Result := Self;
FDataSource := aDataSource;
FDataSource.DataSet := FQuery.DataSet;
FDataSource.OnDataChange := OnDataChange;
end;
function TSimpleDAO<T>.Delete(aValue: T): iSimpleDAO<T>;
var
aSQL: String;
begin
Result := Self;
TSimpleSQL<T>.New(aValue).Delete(aSQL);
FQuery.SQL.Clear;
FQuery.SQL.Add(aSQL);
Self.FillParameter(aValue);
FQuery.ExecSQL;
end;
{$IFNDEF CONSOLE}
function TSimpleDAO<T>.Delete: iSimpleDAO<T>;
var
aSQL: String;
Entity: T;
begin
Result := Self;
Entity := T.Create;
try
TSimpleSQL<T>.New(Entity).Delete(aSQL);
FQuery.SQL.Clear;
FQuery.SQL.Add(aSQL);
TSimpleRTTI<T>.New(nil).BindFormToClass(FForm, Entity);
Self.FillParameter(Entity);
FQuery.ExecSQL;
finally
FreeAndNil(Entity);
end;
end;
{$ENDIF}
function TSimpleDAO<T>.Delete(aField, aValue: String): iSimpleDAO<T>;
var
aSQL: String;
Entity: T;
aTableName: string;
begin
Result := Self;
Entity := T.Create;
try
TSimpleSQL<T>.New(Entity).Delete(aSQL);
TSimpleRTTI<T>.New(Entity).TableName(aTableName);
aSQL := 'DELETE FROM ' + aTableName + ' WHERE ' + aField +
' = ' + aValue;
FQuery.SQL.Clear;
FQuery.SQL.Add(aSQL);
FQuery.ExecSQL;
finally
FreeAndNil(Entity);
end;
end;
destructor TSimpleDAO<T>.Destroy;
begin
FreeAndNil(FList);
inherited;
end;
function TSimpleDAO<T>.Find(aBindList: Boolean = True): iSimpleDAO<T>;
var
aSQL: String;
begin
Result := Self;
TSimpleSQL<T>.New(nil).Fields(FSQLAttribute.Fields).Join(FSQLAttribute.Join)
.Where(FSQLAttribute.Where).GroupBy(FSQLAttribute.GroupBy)
.OrderBy(FSQLAttribute.OrderBy).Select(aSQL);
FQuery.DataSet.DisableControls;
FQuery.Open(aSQL);
if aBindList then
TSimpleRTTI<T>.New(nil).DataSetToEntityList(FQuery.DataSet, FList);
FSQLAttribute.Clear;
FQuery.DataSet.EnableControls;
end;
function TSimpleDAO<T>.Find(aId: Integer): T;
var
aSQL: String;
begin
Result := T.Create;
TSimpleSQL<T>.New(nil).SelectId(aSQL);
FQuery.SQL.Clear;
FQuery.SQL.Add(aSQL);
Self.FillParameter(Result, aId);
FQuery.Open;
TSimpleRTTI<T>.New(nil).DataSetToEntity(FQuery.DataSet, Result);
end;
{$IFNDEF CONSOLE}
function TSimpleDAO<T>.Insert: iSimpleDAO<T>;
var
aSQL: String;
Entity: T;
begin
Result := Self;
Entity := T.Create;
try
TSimpleSQL<T>.New(Entity).Insert(aSQL);
FQuery.SQL.Clear;
FQuery.SQL.Add(aSQL);
TSimpleRTTI<T>.New(nil).BindFormToClass(FForm, Entity);
Self.FillParameter(Entity);
FQuery.ExecSQL;
finally
FreeAndNil(Entity);
end;
end;
{$ENDIF}
function TSimpleDAO<T>.LastID: iSimpleDAO<T>;
var
aSQL: String;
begin
Result := Self;
TSimpleSQL<T>.New(nil).LastID(aSQL);
FQuery.Open(aSQL);
end;
function TSimpleDAO<T>.LastRecord: iSimpleDAO<T>;
var
aSQL: String;
begin
Result := Self;
TSimpleSQL<T>.New(nil).LastRecord(aSQL);
FQuery.Open(aSQL);
end;
function TSimpleDAO<T>.Find(var aList: TObjectList<T>): iSimpleDAO<T>;
var
aSQL: String;
begin
Result := Self;
TSimpleSQL<T>.New(nil).Fields(FSQLAttribute.Fields).Join(FSQLAttribute.Join)
.Where(FSQLAttribute.Where).GroupBy(FSQLAttribute.GroupBy)
.OrderBy(FSQLAttribute.OrderBy).Select(aSQL);
FQuery.Open(aSQL);
TSimpleRTTI<T>.New(nil).DataSetToEntityList(FQuery.DataSet, aList);
FSQLAttribute.Clear;
end;
function TSimpleDAO<T>.Insert(aValue: T): iSimpleDAO<T>;
var
aSQL: String;
begin
Result := Self;
TSimpleSQL<T>.New(aValue).Insert(aSQL);
FQuery.SQL.Clear;
FQuery.SQL.Add(aSQL);
Self.FillParameter(aValue);
FQuery.ExecSQL;
end;
class function TSimpleDAO<T>.New(aQuery: iSimpleQuery): iSimpleDAO<T>;
begin
Result := Self.Create(aQuery);
end;
procedure TSimpleDAO<T>.OnDataChange(Sender: TObject; Field: TField);
begin
if (FList.Count > 0) and (FDataSource.DataSet.RecNo - 1 <= FList.Count) then
begin
{$IFNDEF CONSOLE}
if Assigned(FForm) then
TSimpleRTTI<T>.New(nil).BindClassToForm(FForm,
FList[FDataSource.DataSet.RecNo - 1]);
{$ENDIF}
end;
end;
function TSimpleDAO<T>.SQL: iSimpleDAOSQLAttribute<T>;
begin
Result := FSQLAttribute;
end;
{$IFNDEF CONSOLE}
function TSimpleDAO<T>.Update: iSimpleDAO<T>;
var
aSQL: String;
Entity: T;
begin
Result := Self;
Entity := T.Create;
try
TSimpleSQL<T>.New(Entity).Update(aSQL);
FQuery.SQL.Clear;
FQuery.SQL.Add(aSQL);
TSimpleRTTI<T>.New(nil).BindFormToClass(FForm, Entity);
Self.FillParameter(Entity);
FQuery.ExecSQL;
finally
FreeAndNil(Entity)
end;
end;
{$ENDIF}
function TSimpleDAO<T>.Update(aValue: T): iSimpleDAO<T>;
var
aSQL: String;
begin
Result := Self;
TSimpleSQL<T>.New(aValue).Update(aSQL);
FQuery.SQL.Clear;
FQuery.SQL.Add(aSQL);
Self.FillParameter(aValue);
FQuery.ExecSQL;
end;
function TSimpleDAO<T>.FillParameter(aInstance: T): iSimpleDAO<T>;
var
Key: String;
DictionaryFields: TDictionary<String, Variant>;
DictionaryTypeFields: TDictionary<String, TFieldType>;
FieldType: TFieldType;
begin
DictionaryFields := TDictionary<String, Variant>.Create;
DictionaryTypeFields := TDictionary<String, TFieldType>.Create;
TSimpleRTTI<T>.New(aInstance).DictionaryFields(DictionaryFields);
TSimpleRTTI<T>.New(aInstance).DictionaryTypeFields(DictionaryTypeFields);
try
for Key in DictionaryFields.Keys do
begin
if FQuery.Params.FindParam(Key) <> nil then
begin
if DictionaryTypeFields.TryGetValue(Key, FieldType ) then
FQuery.Params.ParamByName(Key).DataType := FieldType;
FQuery.Params.ParamByName(Key).Value := DictionaryFields.Items[Key];
end;
end;
finally
FreeAndNil(DictionaryFields);
FreeAndNil(DictionaryTypeFields);
end;
end;
function TSimpleDAO<T>.FillParameter(aInstance: T; aId: Variant): iSimpleDAO<T>;
var
I: Integer;
ListFields: TList<String>;
begin
ListFields := TList<String>.Create;
TSimpleRTTI<T>.New(aInstance).ListFields(ListFields);
try
for I := 0 to Pred(ListFields.Count) do
begin
if FQuery.Params.FindParam(ListFields[I]) <> nil then
FQuery.Params.ParamByName(ListFields[I]).Value := aId;
end;
finally
FreeAndNil(ListFields);
end;
end;
function TSimpleDAO<T>.Find(aKey: String; aValue: Variant): iSimpleDAO<T>;
var
aSQL: String;
begin
Result := Self;
TSimpleSQL<T>.New(nil).Where(aKey + ' = :' + aKey).Select(aSQL);
FQuery.SQL.Clear;
FQuery.SQL.Add(aSQL);
FQuery.Params.ParamByName(aKey).Value := aValue;
FQuery.Open;
end;
end.