-
Notifications
You must be signed in to change notification settings - Fork 35
/
Quick.Core.Entity.QueryGenerator.SQLite.pas
314 lines (275 loc) · 10.3 KB
/
Quick.Core.Entity.QueryGenerator.SQLite.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
{ ***************************************************************************
Copyright (c) 2016-2020 Kike Pérez
Unit : Quick.DAO.QueryGenerator.SQLite
Description : Core Entity SQLite 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.SQLite;
{$i QuickCore.inc}
interface
uses
Classes,
SysUtils,
Quick.Commons,
Quick.Core.Entity.DAO;
type
TSQLiteQueryGenerator = class(TEntityQueryGenerator,IEntityQueryGenerator)
private
fFormatSettings : TFormatSettings;
public
function Name : string;
constructor Create;
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 QuotedStr(const aValue: string): string; override;
function DBFieldToGUID(const aValue : string) : TGUID;
function GUIDToDBField(aGuid : TGUID) : string;
end;
implementation
const
{$IFNDEF FPC}
DBDATATYPES : array of string = ['varchar(%d)','text','char(%d)','integer','integer','bigint','decimal(%d,%d)','bit','date','time','datetime','datetime','datetime','varchar(38)'];
{$ELSE}
DBDATATYPES : array[0..10] of string = ('varchar(%d)','text','char(%d)','integer','integer','bigint','decimal(%d,%d)','bit','date','time','datetime','datetime','datetime','varchar(38)');
{$ENDIF}
{ TSQLiteQueryGenerator }
function TSQLiteQueryGenerator.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;
constructor TSQLiteQueryGenerator.Create;
begin
{$IFDEF DELPHIRX103_UP}
fFormatSettings := TFormatSettings.Create;
{$ELSE}
GetLocaleFormatSettings(0,fFormatSettings);
{$ENDIF}
fFormatSettings.ShortDateFormat := 'YYYY-MM-DD';
fFormatSettings.ShortTimeFormat := 'hh:nn:ss';
fFormatSettings.DateSeparator := '-';
fFormatSettings.TimeSeparator := ':';
end;
function TSQLiteQueryGenerator.CreateIndex(aModel: TEntityModel; aIndex: TEntityIndex) : string;
begin
Result := Format('CREATE INDEX IF NOT EXISTS PK_%s ON %s (%s)',[aIndex.FieldNames[0],aModel.TableName,aIndex.FieldNames[0]]);
end;
function TSQLiteQueryGenerator.CreateTable(const aTable: TEntityModel) : string;
var
field : TEntityField;
datatype : string;
querytext : TStringList;
begin
querytext := TStringList.Create;
try
querytext.Add(Format('CREATE TABLE IF NOT EXISTS [%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.IsPrimaryKey then
begin
if field.DataType = dtAutoID then querytext.Add(Format('[%s] %s PRIMARY KEY AUTOINCREMENT,',[field.Name,datatype]))
else querytext.Add(Format('[%s] %s PRIMARY KEY,',[field.Name,datatype]));
end
else
begin
if field.DataType = dtCreationDate then querytext.Add(Format('[%s] %s DEFAULT CURRENT_TIMESTAMP,',[field.Name,datatype]))
else querytext.Add(Format('[%s] %s,',[field.Name,datatype]));
end;
end;
querytext[querytext.Count-1] := Copy(querytext[querytext.Count-1],1,querytext[querytext.Count-1].Length-1);
querytext.Add(')');
Result := querytext.Text;
finally
querytext.Free;
end;
end;
function TSQLiteQueryGenerator.ExistsColumn(aModel : TEntityModel; const aFieldName : string) : string;
begin
Result := Format('PRAGMA table_info(%s)',[aModel.TableName]);
end;
function TSQLiteQueryGenerator.ExistsTable(aModel: TEntityModel): string;
begin
Result := Format('SELECT name FROM sqlite_master WHERE type = ''table'' AND name = ''%s''',[aModel.TableName]);
end;
function TSQLiteQueryGenerator.Name: string;
begin
Result := 'SQLITE';
end;
function TSQLiteQueryGenerator.QuotedStr(const aValue: string): string;
begin
Result := '"' + aValue + '"';
end;
function TSQLiteQueryGenerator.SetPrimaryKey(aModel: TEntityModel) : string;
begin
Result := '';
Exit;
end;
function TSQLiteQueryGenerator.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 TSQLiteQueryGenerator.Select(const aTableName, aFieldNames : string; aLimit: Integer;
const aWhere: string; aOrderFields: string; aOrderAsc: Boolean) : string;
var
orderdir : string;
querytext : TStringList;
begin
querytext := TStringList.Create;
try
//define select-where clauses
if aFieldNames.IsEmpty then querytext.Add(Format('SELECT * FROM [%s] WHERE %s',[aTableName,aWhere]))
else querytext.Add(Format('SELECT %s FROM [%s] WHERE %s',[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;
//define limited query clause
if aLimit > 0 then querytext.Add('LIMIT ' + aLimit.ToString);
Result := querytext.Text;
finally
querytext.Free;
end;
end;
function TSQLiteQueryGenerator.Add(const aTableName: string; const 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 TSQLiteQueryGenerator.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 TSQLiteQueryGenerator.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 TSQLiteQueryGenerator.Count(const aTableName : string; const aWhere : string) : string;
begin
Result := Format('SELECT COUNT(*) AS cnt FROM [%s] WHERE %s',[aTableName,aWhere]);
end;
function TSQLiteQueryGenerator.DateTimeToDBField(aDateTime: TDateTime): string;
begin
if fISO8601DateTime then Result := FormatDateTime('YYYY-MM-DD"T"HH:NN:SS',aDateTime)
else Result := FormatDateTime('YYYY-MM-DD hh:nn:ss',aDateTime);
end;
function TSQLiteQueryGenerator.DBFieldToDateTime(const aValue: string): TDateTime;
var
fmtdate : TFormatSettings;
begin
if fISO8601DateTime then
begin
fmtdate := fFormatSettings;
fmtdate.LongDateFormat := 'YYYY-MM-DDThh:nn:ss';
Result := StrToDateTime(aValue,fmtdate);
end
else Result := StrToDateTime(aValue,fFormatSettings);
end;
function TSQLiteQueryGenerator.Delete(const aTableName, aWhere: string) : string;
begin
Result := Format('DELETE FROM [%s] WHERE %s',[aTableName,aWhere]);
end;
function TSQLiteQueryGenerator.DBFieldToGUID(const aValue: string): TGUID;
begin
//Result := StringToGUID(aValue);
if aValue.Contains('{') then Result := StringToGUID(aValue)
else Result := StringToGUID('{' + aValue + '}');
end;
function TSQLiteQueryGenerator.GUIDToDBField(aGuid: TGUID): string;
begin
Result := aGuid.ToString;
end;
end.