-
Notifications
You must be signed in to change notification settings - Fork 7
/
SQLG.Select.pas
299 lines (258 loc) · 8.06 KB
/
SQLG.Select.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
unit SQLG.Select;
interface
uses
System.SysUtils, System.Rtti, SQLG.Params, SQLG.Table, SQLG.Condition;
type
TOrderField = record
Field: TValue;
OrderType: string;
class operator Implicit(Left: TOrderField): TValue;
end;
TSQLSelect = record
type
TSQLJoin = record
private
FParent: TValue;
FSelect: TValue;
FSelectName: string;
FOn: TSQLCondition;
FPredict: string;
FTable: TSQLTable;
public
function &On(const Condition: TSQLCondition): TSQLSelect;
function Build(var Params: TSQLParams; const Level: Integer = -1): string;
end;
private
FFields: TArray<TValue>;
FFroms: TArray<TValue>;
FWheres: TArray<TSQLCondition>;
FJoins: TArray<TSQLJoin>;
FOrderBy: TArray<TValue>;
FGroupBy: TArray<TValue>;
public
function Build(var Params: TSQLParams; const Alias: string = ''; const Level: Integer = -1): string;
function From(const Items: TArray<TValue>): TSQLSelect; overload;
function From(const Item: TValue): TSQLSelect; overload;
function Where(const Condition: TSQLCondition): TSQLSelect;
function Join(const Table: TSQLTable): TSQLJoin; overload;
function Join(const Select: TSQLSelect; const Alias: string): TSQLJoin; overload;
function LeftJoin(const Table: TSQLTable): TSQLJoin; overload;
function LeftJoin(const Select: TSQLSelect; const Alias: string): TSQLJoin; overload;
function RightJoin(const Table: TSQLTable): TSQLJoin; overload;
function RightJoin(const Select: TSQLSelect; const Alias: string): TSQLJoin; overload;
function OrderBy(const Fields: TArray<TValue>): TSQLSelect; overload;
function OrderBy(const Field: TValue): TSQLSelect; overload;
function GroupBy(const Fields: TArray<TValue>): TSQLSelect; overload;
function GroupBy(const Field: TValue): TSQLSelect; overload;
end;
function Select(Fields: TArray<TValue>): TSQLSelect; overload;
function Select(Fields: TValue): TSQLSelect; overload;
function ASC(Field: TValue): TOrderField;
function DESC(Field: TValue): TOrderField;
implementation
uses
SQLG.Field;
function Select(Fields: TArray<TValue>): TSQLSelect;
begin
Result.FFields := Fields;
end;
function Select(Fields: TValue): TSQLSelect;
begin
Result.FFields := [Fields];
end;
function ASC(Field: TValue): TOrderField;
begin
Result.Field := Field;
Result.OrderType := 'ASC';
end;
function DESC(Field: TValue): TOrderField;
begin
Result.Field := Field;
Result.OrderType := 'DESC';
end;
{ TSQLSelect }
function TSQLSelect.Build(var Params: TSQLParams; const Alias: string; const Level: Integer): string;
begin
// SELECT
var FieldList: TArray<string>;
for var Field in FFields do
begin
if Field.IsType<TSQLTable>(False) then
FieldList := FieldList + [Field.AsType<TSQLTable>(False).TableName + '.*']
else if IsSQLField(Field) then
FieldList := FieldList + [GetFieldName(Alias, Field)]
else if Field.IsType<string>(False) then
FieldList := FieldList + [Field.AsString];
end;
if Level >= 0 then
Result := Result + #13#10;
for var i := 0 to Level do
Result := Result + ' ';
Result := Result + 'SELECT ' + string.Join(', ', FieldList);
// FROM
if Length(FFroms) > 0 then
begin
Result := Result + #13#10;
for var i := 0 to Level do
Result := Result + ' ';
var FromList: TArray<string>;
for var From in FFroms do
begin
if From.IsType<TSQLTable>(False) then
begin
FromList := FromList + [From.AsType<TSQLTable>(False).TableName];
end;
end;
Result := Result + ' FROM ' + string.Join(', ', FromList);
end;
// JOIN
if Length(FJoins) > 0 then
begin
Result := Result + #13#10;
for var i := 0 to Level do
Result := Result + ' ';
var JoinList: TArray<string>;
for var Join in FJoins do
JoinList := JoinList + [Join.Build(Params, Level + 1)];
Result := Result + string.Join(#13#10, JoinList);
end;
// WHERE
if Length(FWheres) > 0 then
begin
Result := Result + #13#10;
for var i := 0 to Level do
Result := Result + ' ';
var WhereList: TArray<string>;
for var Where in FWheres do
WhereList := WhereList + [Where.Build(Params, Level + 1)];
Result := Result + ' WHERE ' + string.Join(' AND ', WhereList);
end;
// ORDER BY
if Length(FOrderBy) > 0 then
begin
Result := Result + #13#10;
for var i := 0 to Level do
Result := Result + ' ';
var OrderList: TArray<string>;
for var OrderBy in FOrderBy do
begin
if IsSQLField(OrderBy) then
OrderList := OrderList + [GetFieldName(Alias, OrderBy)]
else if OrderBy.IsType<TOrderField>(False) then
OrderList := OrderList + [GetFieldName(Alias, OrderBy.AsType<TOrderField>.Field) + ' ' + OrderBy.AsType<TOrderField>.OrderType]
end;
Result := Result + ' ORDER BY ' + string.Join(', ', OrderList);
end;
// GROUP BY
if Length(FGroupBy) > 0 then
begin
Result := Result + #13#10;
for var i := 0 to Level do
Result := Result + ' ';
var GroupList: TArray<string>;
for var GroupBy in FGroupBy do
begin
if IsSQLField(GroupBy) then
GroupList := GroupList + [GetFieldName(Alias, GroupBy)];
end;
Result := Result + ' GROUP BY ' + string.Join(', ', GroupList);
end;
end;
function TSQLSelect.From(const Items: TArray<TValue>): TSQLSelect;
begin
FFroms := Items;
Result := Self;
end;
function TSQLSelect.From(const Item: TValue): TSQLSelect;
begin
FFroms := FFroms + [Item];
Result := Self;
end;
function TSQLSelect.GroupBy(const Field: TValue): TSQLSelect;
begin
FGroupBy := FGroupBy + [Field];
Result := Self;
end;
function TSQLSelect.GroupBy(const Fields: TArray<TValue>): TSQLSelect;
begin
FGroupBy := FGroupBy + Fields;
Result := Self;
end;
function TSQLSelect.Join(const Select: TSQLSelect; const Alias: string): TSQLJoin;
begin
Result.FPredict := ' ';
Result.FTable := nil;
Result.FSelectName := Alias;
Result.FSelect := TValue.From(Select);
Result.FParent := TValue.From(Self);
end;
function TSQLSelect.LeftJoin(const Select: TSQLSelect; const Alias: string): TSQLJoin;
begin
Result.FPredict := ' LEFT ';
Result.FTable := nil;
Result.FSelectName := Alias;
Result.FSelect := TValue.From(Select);
Result.FParent := TValue.From(Self);
end;
function TSQLSelect.OrderBy(const Field: TValue): TSQLSelect;
begin
FOrderBy := FOrderBy + [Field];
Result := Self;
end;
function TSQLSelect.OrderBy(const Fields: TArray<TValue>): TSQLSelect;
begin
FOrderBy := FOrderBy + Fields;
Result := Self;
end;
function TSQLSelect.Join(const Table: TSQLTable): TSQLJoin;
begin
Result.FPredict := ' ';
Result.FTable := Table;
Result.FParent := TValue.From(Self);
end;
function TSQLSelect.LeftJoin(const Table: TSQLTable): TSQLJoin;
begin
Result.FPredict := ' LEFT ';
Result.FTable := Table;
Result.FParent := TValue.From(Self);
end;
function TSQLSelect.RightJoin(const Table: TSQLTable): TSQLJoin;
begin
Result.FPredict := ' RIGHT ';
Result.FTable := Table;
Result.FParent := TValue.From(Self);
end;
function TSQLSelect.Where(const Condition: TSQLCondition): TSQLSelect;
begin
FWheres := FWheres + [Condition];
Result := Self;
end;
function TSQLSelect.RightJoin(const Select: TSQLSelect; const Alias: string): TSQLJoin;
begin
Result.FPredict := ' RIGHT ';
Result.FTable := nil;
Result.FSelectName := Alias;
Result.FSelect := TValue.From(Select);
Result.FParent := TValue.From(Self);
end;
{ TSQLSelect.TSQLJoin }
function TSQLSelect.TSQLJoin.Build(var Params: TSQLParams; const Level: Integer): string;
begin
if FTable <> nil then
Result := FPredict + 'JOIN ' + FTable.TableName
else
Result := FPredict + 'JOIN (' + FSelect.AsType<TSQLSelect>.Build(Params, '', Level + 1) + ') ' + FSelectName;
Result := Result + ' ON ' + FOn.Build(Params);
end;
function TSQLSelect.TSQLJoin.&On(const Condition: TSQLCondition): TSQLSelect;
begin
FOn := Condition;
Result := FParent.AsType<TSQLSelect>;
Result.FJoins := Result.FJoins + [Self];
end;
{ TOrderField }
class operator TOrderField.Implicit(Left: TOrderField): TValue;
begin
Result := TValue.From(Left);
end;
end.