-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathORM.DataTypes.pas
More file actions
370 lines (312 loc) · 8.65 KB
/
Copy pathORM.DataTypes.pas
File metadata and controls
370 lines (312 loc) · 8.65 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
unit ORM.DataTypes;
interface
uses RTTI, Classes, Generics.Collections;
type
TSimpleAttribute=class(TCustomAttribute)
private
fName:string;
public
constructor Create(Name:String);
property Name:String read fName;
end;
Table=class(TSimpleAttribute);
View=class(TSimpleAttribute);
TColumnType=(ctPrimary,ctCommon);
Column = class(TCustomAttribute)
private
fName:string;
fCType:TColumnType;
fLen:Integer;
public
constructor Create(FieldName:String; ColumnType:TColumnType=ctCommon); overload;
constructor Create(FieldName:String; Len:Integer); overload;
property Name:String read fName;
property CType:TColumnType read fCType;
end;
AutoGeneration = class(TCustomAttribute);
TEntity=class;
TEntityClass=class of TEntity;
TColumn=record
Name:string;
CType:TColumnType;
Len:Integer;
AutoGeneration:Boolean;
IsPrimary:Boolean;
IsForeign:Boolean;
FKClass:TEntityClass;
FKColumn:String;
procedure Load(Col:Column);
end;
TEntityInfo=record
private
fEntityClass:Pointer;
fColCount:Integer;
fColumns:TArray<TColumn>;
fFieldNames:TArray<String>;
function GetColumnType(Column:String):String;
public
constructor Create(EntityClass:Pointer);
property ColCount:Integer read fColCount;
property Columns:TArray<TColumn> read fColumns;
property FieldNames:TArray<String> read fFieldNames;
property ColumnType[Column:String]:String read GetColumnType;
end;
TEntity=class
private
class var fEntitiesName:TDictionary<String, String>;
class var fEntitiesInfo:TDictionary<String, TEntityInfo>;
public
PrimaryKeys:TArray<TValue>;
class function EntityName:String;
class function ViewName:String;
class function GetEntityInfo:TEntityInfo;
constructor Create;
procedure SetAsNew;
procedure Init; virtual; abstract;
end;
ForeignKey=class(TCustomAttribute)
private
fEntityClass:TEntityClass;
fColumnName:String;
public
constructor Create(EntityClass:TEntityClass; ColumnName:String);
property EntityClass:TEntityClass read fEntityClass;
property ColumnName:string read fColumnName;
end;
TFieldType=(ftNone,ftALL);
TField=record
private
FieldType:TFieldType;
public
EntityClass:TEntityClass;
Name:String;
constructor Create(EntityClass:TEntityClass; Name:String);
constructor ALL(EntityClass:TEntityClass);
function ToString:String;
end;
function EntityToStr(Entity:TEntity):String;
implementation
uses SysUtils;
constructor TEntity.Create;
begin
Init;
end;
procedure TEntity.SetAsNew;
begin
SetLength(PrimaryKeys,0);
end;
procedure TColumn.Load(Col:Column);
begin
Self.Name:=Col.Name;
Self.CType:=Col.CType;
Self.Len:=Col.fLen;
end;
constructor TSimpleAttribute.Create(Name:String);
begin
fName:=Name;
end;
constructor Column.Create(FieldName:String; ColumnType:TColumnType=ctCommon);
begin
fName:=FieldName;
fCType:=ColumnType;
fLen:=-1;
end;
constructor Column.Create(FieldName:String; Len:Integer);
begin
fName:=FieldName;
fCType:=ctCommon;
fLen:=Len;
end;
constructor ForeignKey.Create(EntityClass:TEntityClass; ColumnName:String);
begin
fEntityClass:=EntityClass;
fColumnName:=ColumnName;
end;
constructor TEntityInfo.Create(EntityClass:Pointer);
var arr:TArray<TRttiField>;
c : TRttiContext;
t : TRttiType;
a : TCustomAttribute;
i:integer;
ag,fk:boolean;
fkclass:TEntityClass;
fkcol:String;
begin
fEntityClass:=EntityClass;
fColCount:=0;
c:=TRttiContext.Create;
t:=c.GetType(EntityClass);
arr:=t.GetFields;
for i := 0 to High(arr) do begin
ag:=false;
fk:=false;
for a in arr[i].GetAttributes do begin
if a is AutoGeneration then begin
ag:=true;
end;
if a is ForeignKey then begin
fk:=true;
fkclass:=ForeignKey(a).EntityClass;
fkcol:=ForeignKey(a).ColumnName;
end;
end;
for a in arr[i].GetAttributes do begin
if a is Column then begin
SetLength(fFieldNames,fColCount+1);
SetLength(fColumns,fColCount+1);
fFieldNames[fColCount]:=arr[i].Name;
fColumns[fColCount].Load(Column(a));
fColumns[fColCount].AutoGeneration:=ag;
fColumns[fColCount].IsPrimary:=(Column(a).fCType=ctPrimary);
fColumns[fColCount].IsForeign:=fk;
fColumns[fColCount].FKClass:=fkclass;
fColumns[fColCount].FKColumn:=fkcol;
Inc(fColCount);
end;
end;
end;
end;
function TEntityInfo.GetColumnType(Column:String):String;
var c:TRttiContext;
t:TRttiType;
i:integer;
begin
Result:='Unknown';
try
for i := 0 to High(fColumns) do begin
if fColumns[i].Name=Column then begin
c:=TRttiContext.Create;
t:=c.GetType(fEntityClass);
Result:= t.GetField(fFieldNames[i]).FieldType.Name;
break;
end;
end;
//t.GetField()
finally
FreeAndNil(t);
end;
end;
class function TEntity.EntityName:String;
var c : TRttiContext;
t : TRttiType;
a : TCustomAttribute;
begin
if fEntitiesName=nil then begin
fEntitiesName:=TDictionary<String,String>.Create;
end;
if fEntitiesName.ContainsKey(ClassName) then begin
Result:=fEntitiesName.Items[ClassName];
end else begin
Result:=ClassName;
c:=TRttiContext.Create;
t:=c.GetType(ClassInfo);
for a in t.GetAttributes do begin
if a is Table then begin
if Length(Table(a).Name)>0 then begin
Result:=Table(a).Name;
end;
end;
end;
fEntitiesName.Add(ClassName,Result);
end;
end;
class function TEntity.ViewName:String;
var c : TRttiContext;
t : TRttiType;
a : TCustomAttribute;
begin
Result:=ClassName;
c:=TRttiContext.Create;
t:=c.GetType(ClassInfo);
for a in t.GetAttributes do begin
if a is Table then begin
if Length(Table(a).Name)>0 then begin
Result:=Table(a).Name;
end;
end;
if a is View then begin
if Length(View(a).Name)>0 then begin
Result:=View(a).Name;
break;
end;
end;
end;
end;
class function TEntity.GetEntityInfo:TEntityInfo;
begin
if fEntitiesInfo=nil then begin
fEntitiesInfo:=TDictionary<String,TEntityInfo>.Create;
end;
if fEntitiesInfo.ContainsKey(EntityName) then begin
Result:=fEntitiesInfo.Items[EntityName];
end else begin
Result:=TEntityInfo.Create(ClassInfo);
fEntitiesInfo.Add(EntityName,Result);
end;
Result:=TEntityInfo.Create(ClassInfo);
end;
constructor TField.Create(EntityClass:TEntityClass; Name:String);
begin
Self.EntityClass:=EntityClass;
Self.Name:=Name;
FieldType:=ftNone;
end;
constructor TField.ALL(EntityClass:TEntityClass);
begin
Self.EntityClass:=EntityClass;
FieldType:=ftALL;
end;
function TField.ToString:String;
begin
Result:='';
case FieldType of
ftNone: begin
if EntityClass<>nil then begin
Result:=EntityClass.EntityName+'.';
end;
Result:=Result+Name;
end;
ftALL: begin
Result:=Format('%s.*',[EntityClass.EntityName]);
end;
end;
end;
function EntityToStr(Entity:TEntity):String;
procedure AddParam(var S:String; Name:String; Value:String);
begin
if Length(S)>0 then
S:=S+#9;
S:=S+Name+'='+Value;
end;
var rt : TRttiType;
EntityInfo:TEntityInfo;
i,n:integer;
s:string;
begin
Result:='';
EntityInfo:=Entity.GetEntityInfo;
n:=EntityInfo.ColCount;
rt:=TRttiContext.Create.GetType(Entity.ClassInfo);
for i := 0 to n - 1 do begin
try
if rt.GetField(EntityInfo.FieldNames[i]).FieldType.Name='Boolean' then begin
if rt.GetField(EntityInfo.FieldNames[i]).GetValue(TObject(Entity)).AsBoolean then begin
AddParam(Result,EntityInfo.FieldNames[i],'TRUE');
end else begin
{ TODO -oGROM : bool or int??? }
AddParam(Result,EntityInfo.FieldNames[i],'FALSE');
end;
end else begin
if rt.GetField(EntityInfo.FieldNames[i]).FieldType.Name='TMemoryStream' then begin
end else begin
AddParam(Result,EntityInfo.FieldNames[i], rt.GetField(EntityInfo.FieldNames[i]).GetValue(TObject(Entity)).ToString);
end;
end;
except
on E:Exception do begin
raise Exception.Create('EntityToStr: [Field='+EntityInfo.Columns[i].Name+'] '+E.Message);
end;
end;
end;
end;
end.