-
Notifications
You must be signed in to change notification settings - Fork 7
/
SQLG.Condition.pas
180 lines (163 loc) · 4.3 KB
/
SQLG.Condition.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
unit SQLG.Condition;
interface
uses
System.SysUtils, System.Rtti, SQLG.Params;
type
TSQLConditionOperation = (
// AND
coAnd,
// OR
coOr,
// NOT
coNot,
// XOR
coXOr,
// =
coEqual,
// <>
coNotEqual,
// <
coLessThan,
// <=
coLessThanOrEqual,
// >
coGreaterThan,
// >=
coGreaterThanOrEqual,
// IN
coIn,
// +
coAdd);
TSQLConditionOperationHelper = record helper for TSQLConditionOperation
function ToString: string;
end;
TSQLCondition = record
Op: TSQLConditionOperation;
Left, Right: TValue;
class operator LogicalAnd(Left, Right: TSQLCondition): TSQLCondition;
class operator LogicalOr(Left, Right: TSQLCondition): TSQLCondition;
class operator LogicalNot(Right: TSQLCondition): TSQLCondition;
function Build(var Params: TSQLParams; const Level: Integer = -1): string;
end;
implementation
uses
SQLG.Select;
function TSQLCondition.Build(var Params: TSQLParams; const Level: Integer): string;
begin
Result := '';
if Left.IsType<TSQLParam>(False) then
begin
var Param := Left.AsType<TSQLParam>;
Param.Key := 'p' + Length(Params).ToString;
Params := Params + [Param];
Result := Result + ':' + Param.Key;
end
else if Left.IsType<TArray<TSQLParam>>(False) then
begin
var AParam := Left.AsType<TArray<TSQLParam>>;
var InVal: TArray<string> := [];
for var LParam in AParam do
begin
var Param := LParam;
Param.Key := 'p' + Length(Params).ToString;
Params := Params + [Param];
InVal := InVal + [':' + Param.Key];
end;
if Length(InVal) > 0 then
Result := Result + '(' + string.Join(', ', InVal) + ')';
end
else if Left.IsType<TSQLCondition>(False) then
begin
var Cond := Left.AsType<TSQLCondition>;
Result := Result + '(' + Cond.Build(Params, Level + 1) + ')';
end
else if Left.IsType<TSQLSelect>(False) then
begin
var Select := Left.AsType<TSQLSelect>;
Result := Result + '(' + Select.Build(Params, '', Level + 1) + ')';
end
else if Left.IsType<string>(False) then
Result := Result + Left.AsString;
if Result.IsEmpty then
Result := Op.ToString + ' '
else
Result := Result + ' ' + Op.ToString + ' ';
if Right.IsType<TSQLParam>(False) then
begin
var Param := Right.AsType<TSQLParam>;
Param.Key := 'p' + Length(Params).ToString;
Params := Params + [Param];
Result := Result + ':' + Param.Key;
end
else if Right.IsType<TArray<TSQLParam>>(False) then
begin
var AParam := Right.AsType<TArray<TSQLParam>>;
var InVal: TArray<string> := [];
for var LParam in AParam do
begin
var Param := LParam;
Param.Key := 'p' + Length(Params).ToString;
Params := Params + [Param];
InVal := InVal + [':' + Param.Key];
end;
if Length(InVal) > 0 then
Result := Result + '(' + string.Join(', ', InVal) + ')';
end
else if Right.IsType<TSQLCondition>(False) then
begin
var Cond := Right.AsType<TSQLCondition>;
Result := Result + '(' + Cond.Build(Params, Level + 1) + ')';
end
else if Right.IsType<TSQLSelect>(False) then
begin
var Select := Right.AsType<TSQLSelect>;
Result := Result + '(' + Select.Build(Params, '', Level + 1) + ')';
end
else if Right.IsType<string>(False) then
Result := Result + Right.AsString;
end;
class operator TSQLCondition.LogicalAnd(Left, Right: TSQLCondition): TSQLCondition;
begin
Result.Op := coAnd;
Result.Left := TValue.From(Left);
Result.Right := TValue.From(Right);
end;
class operator TSQLCondition.LogicalNot(Right: TSQLCondition): TSQLCondition;
begin
Result.Op := coNot;
Result.Left := TValue.Empty;
Result.Right := TValue.From(Right);
end;
class operator TSQLCondition.LogicalOr(Left, Right: TSQLCondition): TSQLCondition;
begin
Result.Op := coOr;
Result.Left := TValue.From(Left);
Result.Right := TValue.From(Right);
end;
{ TSQLConditionOperationHelper }
function TSQLConditionOperationHelper.ToString: string;
begin
case Self of
coAnd:
Exit('AND');
coOr:
Exit('OR');
coNot:
Exit('NOT');
coEqual:
Exit('=');
coNotEqual:
Exit('<>');
coLessThan:
Exit('<');
coLessThanOrEqual:
Exit('<=');
coGreaterThan:
Exit('>');
coGreaterThanOrEqual:
Exit('>=');
coIn:
Exit('in');
end;
end;
end.