-
Notifications
You must be signed in to change notification settings - Fork 35
/
Quick.Core.Entity.QueryGenerator.MSAccess.pas
325 lines (288 loc) · 11 KB
/
Quick.Core.Entity.QueryGenerator.MSAccess.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
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
{ ***************************************************************************
Copyright (c) 2016-2020 Kike Pérez
Unit : Quick.Core.Entity.QueryGenerator.MSAccess
Description : Core Entity MSAccess Query Generator
Author : Kike Pérez
Version : 1.0
Created : 22/11/2019
Modified : 11/09/2020
This file is part of QuickCore: https://github.com/exilon/QuickCore
***************************************************************************
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*************************************************************************** }
unit Quick.Core.Entity.QueryGenerator.MSAccess;
interface
{$i QuickCore.inc}
uses
Classes,
SysUtils,
Quick.Commons,
Quick.Core.Entity.DAO;
type
TMSAccessQueryGenerator = class(TEntityQueryGenerator,IEntityQueryGenerator)
public
function Name : string;
function CreateTable(const aTable : TEntityModel) : string;
function ExistsTable(aModel : TEntityModel) : string;
function ExistsColumn(aModel : TEntityModel; const aFieldName : string) : string;
function AddColumn(aModel : TEntityModel; aField : TEntityField) : string;
function SetPrimaryKey(aModel : TEntityModel) : string;
function CreateIndex(aModel : TEntityModel; aIndex : TEntityIndex) : string;
function Select(const aTableName, aFieldNames : string; aLimit : Integer; const aWhere : string; aOrderFields : string; aOrderAsc : Boolean) : string;
function Sum(const aTableName, aFieldName, aWhere: string): string;
function Count(const aTableName : string; const aWhere : string) : string;
function Add(const aTableName: string; const aFieldNames, aFieldValues : string) : string;
function AddOrUpdate(const aTableName: string; const aFieldNames, aFieldValues : string) : string;
function Update(const aTableName, aFieldPairs, aWhere : string) : string;
function Delete(const aTableName : string; const aWhere : string) : string;
function DateTimeToDBField(aDateTime : TDateTime) : string;
function DBFieldToDateTime(const aValue : string) : TDateTime;
function DBFieldToGUID(const aValue : string) : TGUID;
function GUIDToDBField(aGuid : TGUID) : string;
end;
implementation
const
{$IFNDEF FPC}
DBDATATYPES : array of string = ['text(%d)','text','char(%d)','int','integer','bigint','decimal(%d,%d)','bit','date','time','datetime','datetime','datetime','guid'];
{$ELSE}
DBDATATYPES : array[0..10] of string = ('text(%d)','text','char(%d)','int','integer','bigint','decimal(%d,%d)','bit','date','time','datetime','datetime','datetime','guid');
{$ENDIF}
{ TMSSQLQueryGenerator }
function TMSAccessQueryGenerator.Add(const aTableName, aFieldNames, aFieldValues: string): string;
var
querytext : TStringList;
begin
querytext := TStringList.Create;
try
querytext.Add(Format('INSERT INTO [%s]',[aTableName]));
querytext.Add(Format('(%s)',[aFieldNames]));
querytext.Add(Format('VALUES(%s)',[aFieldValues]));
Result := querytext.Text;
finally
querytext.Free;
end;
end;
function TMSAccessQueryGenerator.AddColumn(aModel: TEntityModel; aField: TEntityField) : string;
var
datatype : string;
querytext : TStringList;
begin
querytext := TStringList.Create;
try
querytext.Add(Format('ALTER TABLE [%s]',[aModel.TableName]));
if aField.DataType = dtFloat then
begin
datatype := Format(DBDATATYPES[Integer(aField.DataType)],[aField.DataSize,aField.Precision])
end
else
begin
if aField.DataSize > 0 then datatype := Format(DBDATATYPES[Integer(aField.DataType)],[aField.DataSize])
else datatype := DBDATATYPES[Integer(aField.DataType)];
end;
querytext.Add(Format('ADD [%s] %s',[aField.Name,datatype]));
Result := querytext.Text;
finally
querytext.Free;
end;
end;
function TMSAccessQueryGenerator.CreateIndex(aModel: TEntityModel; aIndex: TEntityIndex) : string;
var
querytext : TStringList;
begin
Exit;
querytext := TStringList.Create;
try
querytext.Add(Format('IF NOT EXISTS(SELECT * FROM sys.indexes WHERE name = ''PK_%s'' AND object_id = OBJECT_ID(''%s''))',[aIndex.FieldNames[0],aModel.TableName]));
querytext.Add('BEGIN');
querytext.Add(Format('CREATE INDEX PK_%s ON [%s] (%s);',[aIndex.FieldNames[0],aModel.TableName,aIndex.FieldNames[0]]));
querytext.Add('END');
Result := querytext.Text;
finally
querytext.Free;
end;
end;
function TMSAccessQueryGenerator.CreateTable(const aTable: TEntityModel) : string;
var
field : TEntityField;
datatype : string;
querytext : TStringList;
begin
querytext := TStringList.Create;
try
//querytext.Add(Format('IF NOT EXISTS (SELECT Count(MSysObjects.Id) AS CountOfId FROM MSysObjects WHERE MSysObjects.Type IN (1,4,6) AND MSysObjects.name = ''%s'')',[aTable.TableName]));
//querytext.Add('BEGIN');
querytext.Add(Format('CREATE TABLE [%s] (',[aTable.TableName]));
for field in aTable.Fields do
begin
if field.DataType = dtFloat then
begin
datatype := Format(DBDATATYPES[Integer(field.DataType)],[field.DataSize,field.Precision])
end
else
begin
if field.DataSize > 0 then datatype := Format(DBDATATYPES[Integer(field.DataType)],[field.DataSize])
else datatype := DBDATATYPES[Integer(field.DataType)];
end;
if field.DataType = dtAutoID then querytext.Add(Format('[%s] %s IDENTITY(1,1),',[field.Name,datatype]))
else querytext.Add(Format('[%s] %s,',[field.Name,datatype]))
end;
if not aTable.PrimaryKey.Name.IsEmpty then
begin
querytext.Add(Format('PRIMARY KEY(%s)',[aTable.PrimaryKey.Name]));
end
else querytext[querytext.Count-1] := Copy(querytext[querytext.Count-1],1,querytext[querytext.Count-1].Length-1);
//querytext.Add('END');
Result := querytext.Text;
finally
querytext.Free;
end;
end;
function TMSAccessQueryGenerator.ExistsColumn(aModel: TEntityModel; const aFieldName: string) : string;
var
querytext : TStringList;
begin
querytext := TStringList.Create;
try
querytext.Add('SELECT Name FROM sys.columns');
querytext.Add(Format('WHERE object_id = OBJECT_ID(''%s'')',[aModel.TableName]));
Result := querytext.Text;
finally
querytext.Free;
end;
end;
function TMSAccessQueryGenerator.ExistsTable(aModel: TEntityModel): string;
var
querytext : TStringList;
begin
querytext := TStringList.Create;
try
querytext.Add('GRANT SELECT ON MSysObjects TO Admin;');
//querytext.Add(Format('SELECT name FROM MSysObjects WHERE MSysObjects.Type IN (1,4,6) AND MSysObjects.name = ''%s''',[aModel.TableName]));
Result := querytext.Text;
finally
querytext.Free;
end;
end;
function TMSAccessQueryGenerator.Name: string;
begin
Result := 'MSACCESS';
end;
function TMSAccessQueryGenerator.SetPrimaryKey(aModel: TEntityModel) : string;
var
querytext : TStringList;
begin
querytext := TStringList.Create;
try
//querytext.Add('IF NOT EXISTS (SELECT * FROM INFORMATION_SCHEMA.TABLE_CONSTRAINTS');
//querytext.Add(Format('WHERE CONSTRAINT_TYPE = ''PRIMARY KEY'' AND TABLE_NAME = ''%s'' AND TABLE_SCHEMA =''dbo'')',[aModel.TableName]));
//querytext.Add('BEGIN');
//querytext.Add(Format('ALTER TABLE [%s] ADD CONSTRAINT PK_%s PRIMARY KEY (%s)',[aModel.TableName,aModel.PrimaryKey,aModel.PrimaryKey]));
//querytext.Add('END');
Result := querytext.Text;
finally
querytext.Free;
end;
end;
function TMSAccessQueryGenerator.Sum(const aTableName, aFieldName, aWhere: string): string;
var
querytext : TStringList;
begin
querytext := TStringList.Create;
try
querytext.Add(Format('SELECT SUM (%s) FROM [%s] WHERE %s',[aTableName,aFieldName,aWhere]));
Result := querytext.Text;
finally
querytext.Free;
end;
end;
function TMSAccessQueryGenerator.Select(const aTableName, aFieldNames: string; aLimit: Integer;
const aWhere: string; aOrderFields: string; aOrderAsc: Boolean) : string;
var
orderdir : string;
querytext : TStringList;
toplimit : string;
begin
querytext := TStringList.Create;
try
//define limited query clause
if aLimit > 0 then toplimit := Format('TOP %d ',[aLimit])
else toplimit := '';
//define select-where clauses
if aFieldNames.IsEmpty then querytext.Add(Format('SELECT %s* FROM [%s] WHERE %s',[toplimit,aTableName,aWhere]))
else querytext.Add(Format('SELECT %s%s FROM [%s] WHERE %s',[toplimit,aFieldNames,aTableName,aWhere]));
//define orderby clause
if not aOrderFields.IsEmpty then
begin
if aOrderAsc then orderdir := 'ASC'
else orderdir := 'DESC';
querytext.Add(Format('ORDER BY %s %s',[aOrderFields,orderdir]));
end;
Result := querytext.Text;
finally
querytext.Free;
end;
end;
function TMSAccessQueryGenerator.AddOrUpdate(const aTableName: string; const aFieldNames, aFieldValues : string) : string;
var
querytext : TStringList;
begin
querytext := TStringList.Create;
try
querytext.Add(Format('INSERT OR REPLACE INTO [%s]',[aTableName]));
querytext.Add(Format('(%s)',[aFieldNames]));
querytext.Add(Format('VALUES(%s)',[aFieldValues]));
Result := querytext.Text;
finally
querytext.Free;
end;
end;
function TMSAccessQueryGenerator.Update(const aTableName, aFieldPairs, aWhere : string) : string;
var
querytext : TStringList;
begin
querytext := TStringList.Create;
try
querytext.Add(Format('UPDATE [%s]',[aTableName]));
querytext.Add(Format('SET %s',[aFieldPairs]));
querytext.Add(Format('WHERE %s',[aWhere]));
Result := querytext.Text;
finally
querytext.Free;
end;
end;
function TMSAccessQueryGenerator.Count(const aTableName : string; const aWhere : string) : string;
begin
Result := Format('SELECT COUNT(*) AS cnt FROM [%s] WHERE %s',[aTableName,aWhere]);
end;
function TMSAccessQueryGenerator.DateTimeToDBField(aDateTime: TDateTime): string;
begin
Result := FormatDateTime('YYYY/MM/DD hh:nn:ss',aDateTime);
end;
function TMSAccessQueryGenerator.Delete(const aTableName, aWhere: string) : string;
begin
Result := Format('DELETE FROM [%s] WHERE %s',[aTableName,aWhere]);
end;
function TMSAccessQueryGenerator.DBFieldToDateTime(const aValue: string): TDateTime;
begin
Result := StrToDateTime(aValue);
end;
function TMSAccessQueryGenerator.DBFieldToGUID(const aValue: string): TGUID;
begin
Result := StringToGUID(aValue);
end;
function TMSAccessQueryGenerator.GUIDToDBField(aGuid: TGUID): string;
begin
SetLength(Result, 38);
StrLFmt(PChar(Result), 38,'%.8x-%.4x-%.4x-%.2x%.2x-%.2x%.2x%.2x%.2x%.2x%.2x', // do not localize
[aGuid.D1, aGuid.D2, aGuid.D3, aGuid.D4[0], aGuid.D4[1], aGuid.D4[2], aGuid.D4[3],
aGuid.D4[4], aGuid.D4[5], aGuid.D4[6], aGuid.D4[7]]);
end;
end.