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
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
| ///////////////////////////////////////////////////////////////////////////
// //
// Copyright (c) 2015 by Charta Software B.V. //
// All rights reserved //
// //
// Version: 1.7.0.83525 //
// Web site: https://pascal.chartasoftware.com/ //
// //
// This code and information are provided "as is" without warranty of //
// any kind. Dissemination of this information or reproduction of //
// this material is strictly forbidden unless prior written permission //
// is obtained from Charta Software B.V.. //
// //
///////////////////////////////////////////////////////////////////////////
unit Sql.Statement.DataManipulation;
interface
uses
Byte.Block,
Collection.List,
Integer._32,
Sql.Expression,
Sql.Expression.Row,
Sql.Operator_.Assign,
Sql.Statement,
Sql.Statement.ResultSet.Select,
Sql.Table,
Sql.Table.Column,
Sql.Table.Expression,
Text;
type
TSqlInsertStatement = class(TSqlStatement)
public
Table: TSqlTable;
Columns: TList<TSqlTableColumn>;
constructor Create(Table: TSqlTable); reintroduce; virtual;
destructor Destroy(); override;
function CopyStatement(): TSqlStatement; override;
function CopyInsertStatement(): TSqlInsertStatement; virtual; abstract;
end;
TSqlInsertValuesStatement = class(TSqlInsertStatement)
public
Rows: TList<TSqlRow>;
constructor Create(Table: TSqlTable); override;
destructor Destroy(); override;
function CopyInsertStatement(): TSqlInsertStatement; override;
function CopyInsertValuesStatement(): TSqlInsertValuesStatement; virtual; abstract;
end;
TSqlInsertSelectStatement = class(TSqlInsertStatement)
private
FSelect: TSqlSelect;
procedure SetSelect(const Value: TSqlSelect);
public
constructor Create(Table: TSqlTable; Select: TSqlSelect); reintroduce; virtual;
destructor Destroy(); override;
function CopyInsertStatement(): TSqlInsertStatement; override;
function CopyInsertSelectStatement(): TSqlInsertSelectStatement; virtual; abstract;
property Select: TSqlSelect read FSelect write SetSelect;
end;
TSqlInsertValues = class(TSqlInsertValuesStatement)
public
function CopyInsertValuesStatement(): TSqlInsertValuesStatement; override;
function CopyInsertValues(): TSqlInsertValues;
end;
TSqlInsertSelect = class(TSqlInsertSelectStatement)
public
function CopyInsertSelectStatement(): TSqlInsertSelectStatement; override;
function CopyInsertSelect(): TSqlInsertSelect;
end;
TSqlReplaceValues = class(TSqlInsertValuesStatement)
public
function CopyInsertValuesStatement(): TSqlInsertValuesStatement; override;
function CopyReplaceValues(): TSqlReplaceValues;
end;
TSqlDelete = class(TSqlStatement)
public
TargetTables: TList<TSqlTable>;
ReferenceTables: TList<TSqlTableExpression>;
Restrictions: TList<TSqlExpression>;
constructor Create(); override;
destructor Destroy(); override;
function CopyStatement(): TSqlStatement; override;
function CopyDelete(): TSqlDelete;
end;
TSqlTruncate = class(TSqlStatement)
public
Table: TSqlTable;
constructor Create(Table: TSqlTable); reintroduce; virtual;
function CopyStatement(): TSqlStatement; override;
function CopyTruncate(): TSqlTruncate;
end;
TSqlUpdate = class(TSqlStatement)
public
Tables: TList<TSqlTableExpression>;
Assignments: TList<TSqlAssign>;
Restrictions: TList<TSqlExpression>;
constructor Create(); override;
destructor Destroy(); override;
function CopyStatement(): TSqlStatement; override;
function CopyUpdate(): TSqlUpdate;
function GetColumns(): TList<TSqlTableColumn>;
end;
TSqlLoadDataInfile = class(TSqlStatement)
public
FileName: TText;
Table: TSqlTable;
Data: TByteBlock;
Columns: TList<TSqlTableColumn>;
IgnoreLineCount: TInteger32;
FieldTerminator: TText;
FieldEnclosure: TText;
LineTerminator: TText;
Assignments: TList<TSqlAssign>;
constructor Create(FileName: TText; Table: TSqlTable); reintroduce; virtual;
destructor Destroy(); override;
function CopyStatement(): TSqlStatement; override;
function CopyLoadDataInfile(): TSqlLoadDataInfile;
end;
implementation
uses
Collection.List.Array_,
Object_.List,
Object_.Reference.Counter,
Reference.User;
{ TSqlInsertStatement }
function TSqlInsertStatement.CopyStatement(): TSqlStatement;
begin
Result := CopyInsertStatement();
end;
constructor TSqlInsertStatement.Create(Table: TSqlTable);
begin
inherited Create();
Self.Table := Table;
Columns := TObjectList<TSqlTableColumn>.Create();
Columns.ElementManager := TReferenceCounter<TSqlTableColumn>.Create();
end;
destructor TSqlInsertStatement.Destroy();
begin
Columns.Free();
inherited Destroy();
end;
{ TSqlInsertValuesStatement }
function TSqlInsertValuesStatement.CopyInsertStatement(): TSqlInsertStatement;
begin
Result := CopyInsertValuesStatement();
end;
constructor TSqlInsertValuesStatement.Create(Table: TSqlTable);
begin
inherited Create(Table);
Rows := TObjectList<TSqlRow>.Create();
Rows.ElementManager := TReferenceCounter<TSqlRow>.Create();
end;
destructor TSqlInsertValuesStatement.Destroy();
begin
Rows.Free();
inherited Destroy();
end;
{ TSqlInsertSelectStatement }
function TSqlInsertSelectStatement.CopyInsertStatement(): TSqlInsertStatement;
begin
Result := CopyInsertSelectStatement();
end;
constructor TSqlInsertSelectStatement.Create(Table: TSqlTable; Select: TSqlSelect);
begin
inherited Create(Table);
Self.Select := Select;
end;
destructor TSqlInsertSelectStatement.Destroy();
begin
Select.ReleaseReference();
inherited Destroy();
end;
procedure TSqlInsertSelectStatement.SetSelect(const Value: TSqlSelect);
begin
if Value <> Select then
begin
Value.AddReference();
Select.ReleaseReference();
FSelect := Value;
end;
end;
{ TSqlInsertValues }
function TSqlInsertValues.CopyInsertValues(): TSqlInsertValues;
var
Row: TSqlRow;
begin
Result := TSqlInsertValues.Create(Table);
Result.Columns.AddAll(Columns);
for Row in Rows do
Result.Rows.Add(Row.CopyRow());
end;
function TSqlInsertValues.CopyInsertValuesStatement: TSqlInsertValuesStatement;
begin
Result := CopyInsertValues();
end;
{ TSqlInsertSelect }
function TSqlInsertSelect.CopyInsertSelect(): TSqlInsertSelect;
begin
Result := TSqlInsertSelect.Create(Table, Select.CopySelect());
Result.Columns.AddAll(Columns);
end;
function TSqlInsertSelect.CopyInsertSelectStatement(): TSqlInsertSelectStatement;
begin
Result := CopyInsertSelect();
end;
{ TSqlDelete }
function TSqlDelete.CopyDelete(): TSqlDelete;
var
TableExpression: TSqlTableExpression;
Restriction: TSqlExpression;
begin
Result := TSqlDelete.Create();
Result.TargetTables.AddAll(TargetTables);
for TableExpression in ReferenceTables do
Result.ReferenceTables.Add(TableExpression.CopyTableExpression());
for Restriction in Restrictions do
Result.Restrictions.Add(Restriction.Copy());
end;
function TSqlDelete.CopyStatement(): TSqlStatement;
begin
Result := CopyDelete();
end;
constructor TSqlDelete.Create();
begin
inherited Create();
TargetTables := TObjectList<TSqlTable>.Create();
TargetTables.ElementManager := TReferenceUser<TSqlTable>.Create();
ReferenceTables := TArrayList<TSqlTableExpression>.Create();
ReferenceTables.ElementManager := TReferenceCounter<TSqlTableExpression>.Create();
Restrictions := TObjectList<TSqlExpression>.Create();
Restrictions.ElementManager := TReferenceCounter<TSqlExpression>.Create();
end;
destructor TSqlDelete.Destroy();
begin
TargetTables.Free();
ReferenceTables.Free();
Restrictions.Free();
inherited Destroy();
end;
{ TSqlUpdate }
function TSqlUpdate.CopyStatement(): TSqlStatement;
begin
Result := CopyUpdate();
end;
function TSqlUpdate.CopyUpdate(): TSqlUpdate;
var
Assignment: TSqlAssign;
Table: TSqlTableExpression;
Restriction: TSqlExpression;
begin
Result := TSqlUpdate.Create();
for Table in Tables do
Result.Tables.Add(Table.CopyTableExpression());
for Assignment in Assignments do
Result.Assignments.Add(Assignment.CopyAssign());
for Restriction in Restrictions do
Result.Restrictions.Add(Restriction.Copy());
end;
constructor TSqlUpdate.Create();
begin
inherited Create();
Tables := TArrayList<TSqlTableExpression>.Create();
Tables.ElementManager := TReferenceCounter<TSqlTableExpression>.Create();
Assignments := TObjectList<TSqlAssign>.Create();
Assignments.ElementManager := TReferenceCounter<TSqlAssign>.Create();
Restrictions := TObjectList<TSqlExpression>.Create();
Restrictions.ElementManager := TReferenceCounter<TSqlExpression>.Create()
end;
destructor TSqlUpdate.Destroy();
begin
Tables.Free();
Assignments.Free();
Restrictions.Free();
inherited Destroy();
end;
function TSqlUpdate.GetColumns(): TList<TSqlTableColumn>;
var
Assignment: TSqlAssign;
begin
Result := TObjectList<TSqlTableColumn>.Create();
Result.ElementManager := TReferenceCounter<TSqlTableColumn>.Create();
for Assignment in Assignments do
Result.Add(Assignment.Column);
end;
{ TSqlLoadDataInfile }
function TSqlLoadDataInfile.CopyLoadDataInfile(): TSqlLoadDataInfile;
var
Assignment: TSqlAssign;
begin
Result := TSqlLoadDataInfile.Create(FileName, Table);
Result.Columns.AddAll(Columns);
for Assignment in Assignments do
Result.Assignments.Add(Assignment.CopyAssign());
Result.IgnoreLineCount := IgnoreLineCount;
Result.FieldTerminator := FieldTerminator;
Result.FieldEnclosure := FieldEnclosure;
Result.LineTerminator := LineTerminator;
end;
function TSqlLoadDataInfile.CopyStatement(): TSqlStatement;
begin
Result := CopyLoadDataInfile();
end;
constructor TSqlLoadDataInfile.Create(FileName: TText; Table: TSqlTable);
begin
inherited Create();
Self.FileName := FileName;
Self.Table := Table;
Columns := TObjectList<TSqlTableColumn>.Create();
Columns.ElementManager := TReferenceCounter<TSqlTableColumn>.Create();
Assignments := TObjectList<TSqlAssign>.Create();
Assignments.ElementManager := TReferenceCounter<TSqlAssign>.Create();
end;
destructor TSqlLoadDataInfile.Destroy();
begin
Columns.Free();
Assignments.Free();
inherited Destroy();
end;
{ TSqlTruncate }
function TSqlTruncate.CopyStatement(): TSqlStatement;
begin
Result := CopyTruncate();
end;
function TSqlTruncate.CopyTruncate(): TSqlTruncate;
begin
Result := TSqlTruncate.Create(Table);
end;
constructor TSqlTruncate.Create(Table: TSqlTable);
begin
inherited Create();
Self.Table := Table;
end;
{ TSqlReplaceValues }
function TSqlReplaceValues.CopyInsertValuesStatement(): TSqlInsertValuesStatement;
begin
Result := CopyReplaceValues();
end;
function TSqlReplaceValues.CopyReplaceValues(): TSqlReplaceValues;
var
Row: TSqlRow;
begin
Result := TSqlReplaceValues.Create(Table);
Result.Columns.AddAll(Columns);
for Row in Rows do
Result.Rows.Add(Row.CopyRow());
end;
end.
|