-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathuPersistentObject.pas
More file actions
266 lines (242 loc) · 6.58 KB
/
Copy pathuPersistentObject.pas
File metadata and controls
266 lines (242 loc) · 6.58 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
unit uPersistentObject;
interface
uses
Rtti,StrUtils,Variants,Classes,FireDAC.Comp.Client,
FireDAC.VCLUI.Wait, FireDAC.DApt, uConnection, uAtrib,SysUtils;
type
TPersintentObject = class
private
FSQL: WideString;
function GetValue(const ARTP: TRttiProperty; const AFK: Boolean): String;
procedure SetValue(P: TRttiProperty; S: Variant);
public
property CustomSQL: WideString read FSQL write FSQL;
function Insert: Boolean;
function Update: Boolean;
function Delete: Boolean;
procedure Load(const AValue: Integer); overload; virtual; abstract;
function Load: Boolean; overload;
end;
implementation
{ TPersintentObject }
function TPersintentObject.Delete: Boolean;
begin
end;
function TPersintentObject.GetValue(const ARTP: TRttiProperty;
const AFK: Boolean): String;
begin
case ARTP.PropertyType.TypeKind of
tkUnknown, tkInteger,
tkInt64: Result := ARTP.GetValue(Self).ToString;
tkEnumeration: Result := IntToStr(Integer(ARTP.GetValue(Self).AsBoolean));
tkChar, tkString,
tkWChar, tkLString,
tkWString, tkUString: Result := QuotedStr(ARTP.GetValue(Self).ToString);
tkFloat: Result := StringReplace(FormatFloat('0.00',ARTP.GetValue(Self).AsCurrency)
,FormatSettings.DecimalSeparator,'.',[rfReplaceAll,rfIgnoreCase]);
end;
if (AFK) and (Result = '0') then
Result := 'null';
end;
function TPersintentObject.Insert: Boolean;
var
Ctx: TRttiContext;
RTT: TRttiType;
RTP: TRttiProperty;
Att: TCustomAttribute;
SQL,
Field,
Value,
FieldID,
NomeTabela,Error: String;
Qry: TFDQuery;
begin
Field := '';
Value := '';
TConnection.GetInstance.BeginTrans;
Ctx := TRttiContext.Create;
try
try
RTT := CTX.GetType(ClassType);
for Att in RTT.GetAttributes do
begin
if Att is TableName then
begin
SQL := 'INSERT INTO ' + TableName(ATT).Name;
NomeTabela := TableName(ATT).Name;
end;
end;
for RTP in RTT.GetProperties do
begin
for Att in RTP.GetAttributes do
begin
if Att is FieldName then
begin
if not (FieldName(ATT).AutoInc) then {Auto incremento não pode entrar no insert}
begin
Field := Field + FieldName(ATT).Name + ',';
Value := Value + GetValue(RTP,FieldName(ATT).FK) + ',';
end
else
FieldID := FieldName(ATT).Name;
end;
end;
end;
Field := Copy(Field,1,Length(Field)-1);
Value := Copy(Value,1,Length(Value)-1);
SQL := SQL + ' (' + Field + ') VALUES (' + Value + ')';
if Trim(CustomSQL) <> '' then
SQL := CustomSQL;
Result := TConnection.GetInstance.Execute(SQL,Error);
SQL := 'SELECT ' + FieldID + ' FROM ' + NomeTabela + ' ORDER BY ' + FieldID + ' DESC';
Qry := TConnection.GetInstance.ExecuteQuery(SQL);
//Qry.Next;
for RTP in RTT.GetProperties do
begin
for Att in RTP.GetAttributes do
begin
if (Att is FieldName) and (FieldName(ATT).AutoInc) then
begin
RTP.SetValue(Self,TValue.FromVariant(qry.Fields[0].AsInteger));
end;
end;
end;
finally
CustomSQL := '';
TConnection.GetInstance.Commit;
CTX.Free;
end;
except
TConnection.GetInstance.Rollback;
raise;
end;
end;
function TPersintentObject.Load: Boolean;
var
Ctx: TRttiContext;
RTT: TRttiType;
RTP: TRttiProperty;
Att: TCustomAttribute;
SQL,
Where: String;
Reader: TFDQuery;
begin
Result := True;
Ctx := TRttiContext.Create;
try
RTT := CTX.GetType(ClassType);
for Att in RTT.GetAttributes do
begin
if Att is TableName then
SQL := 'SELECT * FROM ' + TableName(ATT).Name;
end;
for RTP in RTT.GetProperties do
begin
for Att in RTP.GetAttributes do
begin
if Att is FieldName then
begin
if (FieldName(ATT).PK) then
Where := Where + Ifthen(Trim(where)='','',' AND ') + FieldName(ATT).Name + ' = ' + GetValue(RTP,FieldName(ATT).FK);
end;
end;
end;
SQL := SQL + ' WHERE ' + Where;
if Trim(CustomSQL) <> '' then
SQL := CustomSQL;
Reader := TConnection.GetInstance.ExecuteQuery(SQL);
if (Assigned(Reader)) and (Reader.RecordCount > 0) then
begin
with Reader do
begin
First;
while not EOF do
begin
for RTP in RTT.GetProperties do
begin
for Att in RTP.GetAttributes do
begin
if Att is FieldName then
begin
if Assigned(FindField(FieldName(ATT).Name)) then
SetValue(RTP,FieldByName(FieldName(ATT).Name).Value);
end;
end;
end;
Next;
end;
end;
end
else
Result := False;
finally
CustomSQL := '';
CTX.Free;
end;
end;
procedure TPersintentObject.SetValue(P: TRttiProperty; S: Variant);
var
V: TValue;
w: Word;
begin
w := VarType(S);
case w of
271: v := StrToFloat(S); {smallmoney}
272: v := StrToDateTime(S); {smalldatetime}
3: v := StrToInt(S);
else
begin
P.SetValue(Self,TValue.FromVariant(S));
exit;
end;
end;
p.SetValue(Self,v);
end;
function TPersintentObject.Update: Boolean;
var
Ctx: TRttiContext;
RTT: TRttiType;
RTP: TRttiProperty;
Att: TCustomAttribute;
SQL,
Field,
Where,
Error: String;
begin
Field := '';
Ctx := TRttiContext.Create;
try
RTT := CTX.GetType(ClassType);
for Att in RTT.GetAttributes do
begin
if Att is TableName then
SQL := 'UPDATE ' + TableName(ATT).Name + ' SET';
end;
for RTP in RTT.GetProperties do
begin
for Att in RTP.GetAttributes do
begin
if Att is FieldName then
begin
if (not (FieldName(ATT).AutoInc)) and (not (FieldName(ATT).PK)) then {Auto incremento não pode entrar no update}
begin
Field := Field + FieldName(ATT).Name + ' = ' + GetValue(RTP,FieldName(ATT).FK) + ',';
end
else if (FieldName(ATT).PK) then
Where := Where + Ifthen(Trim(where)='','',' AND ') + FieldName(ATT).Name + ' = ' + GetValue(RTP,FieldName(ATT).FK);
end;
end;
end;
Field := Copy(Field,1,Length(Field)-1);
SQL := SQL + ' ' + Field + ' WHERE ' + Where;
if Trim(CustomSQL) <> '' then
SQL := CustomSQL;
Result := TConnection.GetInstance.Execute(SQL,Error);
if not Result then
raise Exception.Create(Error);
finally
CustomSQL := '';
CTX.Free;
end;
end;
end.