forked from HemulGM/SQLGenerator
-
Notifications
You must be signed in to change notification settings - Fork 0
/
SQLG.Params.pas
155 lines (122 loc) · 3.68 KB
/
SQLG.Params.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
unit SQLG.Params;
interface
uses
System.Rtti, System.SysUtils;
type
TSQLParam = record
Key: string;
Value: TValue;
AsType: string;
function KeyWithCast: string;
constructor Create(const Key: string; const Value: TValue);
function ToString: string;
end;
TSQLParams = TArray<TSQLParam>;
TEmptyGUID = record
class function Create: TEmptyGUID; static;
end;
TParams = class
class function Params<T>(const Name: string; const Values: TArray<T>; AsType: string = ''): TArray<TSQLParam>;
end;
function Param(const Name: string; const Value: TValue; AsType: string = ''): TSQLParam; overload;
function Param(const Name: string; const Value: TGUID; AsType: string = ''): TSQLParam; overload;
function Params(const Name: string; const Values: TArray<Integer>; AsType: string = ''): TArray<TSQLParam>; overload;
function Params(const Name: string; const Values: TArray<string>; AsType: string = ''): TArray<TSQLParam>; overload;
function Params(const Name: string; const Values: TArray<TGUID>; AsType: string = ''): TArray<TSQLParam>; overload;
function ToJson: string;
function CreateGUID(const Value: TGUID): TValue; overload;
function CreateGUID(const Value: string): TValue; overload;
implementation
function CreateGUID(const Value: TGUID): TValue;
begin
Result := TValue.From<TGUID>(Value);
end;
function CreateGUID(const Value: string): TValue;
begin
if Value = '' then
Result := TValue.From<TEmptyGUID>(TEmptyGUID.Create)
else
Result := TValue.From<TGUID>(TGUID.Create(Value));
end;
function Param(const Name: string; const Value: TValue; AsType: string = ''): TSQLParam;
begin
Result := TSQLParam.Create(Name, Value);
Result.AsType := AsType;
end;
function Param(const Name: string; const Value: TGUID; AsType: string = ''): TSQLParam;
begin
Result := TSQLParam.Create(Name, CreateGUID(Value));
Result.AsType := AsType;
end;
function Params(const Name: string; const Values: TArray<Integer>; AsType: string = ''): TArray<TSQLParam>;
begin
for var Value in Values do
begin
var Param := TSQLParam.Create(Name, Value);
Param.AsType := AsType;
Result := Result + [Param];
end;
end;
function Params(const Name: string; const Values: TArray<string>; AsType: string = ''): TArray<TSQLParam>;
begin
for var Value in Values do
begin
var Param := TSQLParam.Create(Name, Value);
Param.AsType := AsType;
Result := Result + [Param];
end;
end;
function Params(const Name: string; const Values: TArray<TGUID>; AsType: string = ''): TArray<TSQLParam>;
begin
for var Value in Values do
begin
var Param := TSQLParam.Create(Name, CreateGUID(Value));
Param.AsType := AsType;
Result := Result + [Param];
end;
end;
function ToJson: string;
begin
Result := 'to_json';
end;
{ TSQLParam }
constructor TSQLParam.Create(const Key: string; const Value: TValue);
begin
Self.Key := Key;
Self.Value := Value;
end;
function TSQLParam.KeyWithCast: string;
begin
if AsType.IsEmpty then
Result := ':' + Key
else if AsType = ToJson then
Result := 'to_json(:' + Key + ')'
else
Result := 'CAST(:' + Key + ' as ' + AsType + ')';
end;
function TSQLParam.ToString: string;
begin
if Value.IsType<TGUID>(False) then
Exit(Value.AsType<TGUID>.ToString);
try
Result := Value.ToString;
except
Result := '?';
end;
end;
{ TEmptyGUID }
class function TEmptyGUID.Create: TEmptyGUID;
begin
// empty
end;
{ TParams }
class function TParams.Params<T>(const Name: string; const Values: TArray<T>; AsType: string): TArray<TSQLParam>;
begin
for var Value in Values do
begin
var Param := TSQLParam.Create(Name, TValue.From<T>(Value));
Param.AsType := AsType;
Result := Result + [Param];
end;
end;
end.