-
Notifications
You must be signed in to change notification settings - Fork 27
/
Quick.DAO.Database.pas
342 lines (298 loc) · 10.2 KB
/
Quick.DAO.Database.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
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
{ ***************************************************************************
Copyright (c) 2016-2020 Kike Pérez
Unit : Quick.DAO.Database
Description : DAO Database
Author : Kike Pérez
Version : 1.2
Created : 22/06/2018
Modified : 14/04/2020
This file is part of QuickDAO: https://github.com/exilon/QuickDAO
***************************************************************************
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.DAO.Database;
{$i QuickDAO.inc}
interface
uses
SysUtils,
Classes,
Rtti,
TypInfo,
{$IFDEF FPC}
rttiutils,
fpjson,
jsonparser,
strUtils,
//jsonreader,
//fpjsonrtti,
Quick.Json.fpc.Compatibility,
{$ELSE}
{$IFDEF DELPHIXE7_UP}
System.Json,
{$ENDIF}
{$ENDIF}
Quick.Json.Serializer,
Quick.DAO,
Quick.DAO.Factory.QueryGenerator;
type
IDBConnectionSettings = interface
['{B4AE214B-432F-409C-8A15-AEEEE39CBAB5}']
function GetProvider : TDBProvider;
function GetServer : string;
function GetDatabase : string;
function GetUserName : string;
function GetPassword : string;
property Provider : TDBProvider read GetProvider;
property Server : string read GetServer;
property Database : string read GetDatabase;
property UserName : string read GetUserName;
property Password : string read GetPassword;
function IsCustomConnectionString : Boolean;
procedure FromConnectionString(aDBProviderID : Integer; const aConnectionString: string);
function GetCustomConnectionString : string;
end;
TDBConnectionSettings = class(TInterfacedObject,IDBConnectionSettings)
private
fDBProvider : TDBProvider;
fServer : string;
fDatabase : string;
fUserName : string;
fPassword : string;
fCustomConnectionString : string;
fIsCustomConnectionString : Boolean;
function GetProvider : TDBProvider; virtual;
function GetServer : string;
function GetDatabase : string;
function GetUserName : string;
function GetPassword : string;
public
constructor Create;
property Provider : TDBProvider read GetProvider write fDBProvider;
property Server : string read fServer write fServer;
property Database : string read fDatabase write fDatabase;
property UserName : string read fUserName write fUserName;
property Password : string read fPassword write fPassword;
function IsCustomConnectionString : Boolean;
procedure FromConnectionString(aDBProviderID : Integer; const aConnectionString: string);
function GetCustomConnectionString : string;
function Clone : TDBConnectionSettings;
end;
TDAODataBase = class
private
fDBConnection : TDBConnectionSettings;
fOwnsConnection : Boolean;
fQueryGenerator : IDAOQueryGenerator;
fModels : TDAOModels;
fIndexes : TDAOIndexes;
protected
property OwnsConnection : Boolean read fOwnsConnection write fOwnsConnection;
function CreateConnectionString : string; virtual; abstract;
procedure ExecuteSQLQuery(const aQueryText : string); virtual; abstract;
procedure OpenSQLQuery(const aQueryText: string); virtual; abstract;
function ExistsTable(aModel : TDAOModel) : Boolean; virtual; abstract;
function CreateTable(const aModel : TDAOModel): Boolean; virtual;
function ExistsColumn(aModel: TDAOModel; const aFieldName: string): Boolean; virtual; abstract;
procedure AddColumnToTable(aModel : TDAOModel; aField : TDAOField); virtual;
procedure CreateTables; virtual;
procedure SetPrimaryKey(aModel : TDAOModel); virtual;
procedure CreateIndexes; virtual;
procedure CreateIndex(aModel : TDAOModel; aIndex : TDAOIndex); virtual;
public
constructor Create; virtual;
destructor Destroy; override;
function QueryGenerator : IDAOQueryGenerator;
property Connection : TDBConnectionSettings read fDBConnection write fDBConnection;
property Models : TDAOModels read fModels write fModels;
property Indexes : TDAOIndexes read fIndexes write fIndexes;
function CreateQuery(aModel : TDAOModel) : IDAOQuery<TDAORecord>; virtual; abstract;
function GetTableNames : TArray<string>; virtual; abstract;
function GetFieldNames(const aTableName : string) : TArray<string>; virtual; abstract;
function Connect : Boolean; virtual;
procedure Disconnect; virtual; abstract;
function IsConnected : Boolean; virtual; abstract;
function AddOrUpdate(aDAORecord : TDAORecord) : Boolean; virtual;
function Add(aDAORecord : TDAORecord) : Boolean; virtual;
function Update(aDAORecord : TDAORecord) : Boolean; virtual;
function Delete(aDAORecord : TDAORecord) : Boolean; overload; virtual;
function Clone : TDAODatabase; virtual; abstract;
end;
implementation
{ TDAODataBase }
constructor TDAODataBase.Create;
begin
fDBConnection := TDBConnectionSettings.Create;
fOwnsConnection := True;
fModels := TDAOModels.Create;
fIndexes := TDAOIndexes.Create;
end;
destructor TDAODataBase.Destroy;
begin
fDBConnection.Free;
fModels.Free;
fIndexes.Free;
inherited;
end;
procedure TDAODataBase.CreateIndexes;
var
daoindex : TDAOIndex;
daomodel : TDAOModel;
begin
for daoindex in Indexes.List do
begin
if (Models.List.TryGetValue(daoindex.Table,daomodel)) and (daomodel.HasPrimaryKey) then CreateIndex(daomodel,daoindex);
end;
end;
procedure TDAODataBase.CreateTables;
var
daomodel : TDAOModel;
field : TDAOField;
begin
for daomodel in Models.List.Values do
begin
if not ExistsTable(daomodel) then CreateTable(daomodel)
else
begin
//add new fields
for field in daomodel.Fields do
begin
if not ExistsColumn(daomodel,field.Name) then AddColumnToTable(daomodel,field);
end;
end;
SetPrimaryKey(daomodel);
end;
end;
function TDAODataBase.CreateTable(const aModel : TDAOModel): Boolean;
begin
try
ExecuteSQLQuery(QueryGenerator.CreateTable(aModel));
Result := True;
except
on E : Exception do raise EDAOCreationError.CreateFmt('Error creating table "%s" : %s!',[aModel.TableName,e.Message])
end;
end;
function TDAODataBase.Connect: Boolean;
begin
Result := False;
fQueryGenerator := TDAOQueryGeneratorFactory.Create(fDBConnection.Provider);
end;
procedure TDAODataBase.AddColumnToTable(aModel : TDAOModel; aField : TDAOField);
begin
try
ExecuteSQLQuery(QueryGenerator.AddColumn(aModel,aField));
except
on E : Exception do raise EDAOCreationError.CreateFmt('Error creating table "%s" fields',[aModel.TableName]);
end;
end;
procedure TDAODataBase.SetPrimaryKey(aModel : TDAOModel);
var
query : string;
begin
try
query := QueryGenerator.SetPrimaryKey(aModel);
if not query.IsEmpty then ExecuteSQLQuery(query);
except
on E : Exception do raise EDAOCreationError.Create('Error modifying primary key field');
end;
if (fDBConnection.Provider = daoSQLite) and (aModel.HasPrimaryKey) then Indexes.Add(aModel.Table,[aModel.PrimaryKey.Name],TDAOIndexOrder.orAscending);
end;
procedure TDAODataBase.CreateIndex(aModel : TDAOModel; aIndex : TDAOIndex);
var
query : string;
begin
try
query := QueryGenerator.CreateIndex(aModel,aIndex);
if query.IsEmpty then Exit;
ExecuteSQLQuery(query);
except
on E : Exception do raise EDAOCreationError.CreateFmt('Error creating index "%s" on table "%s"',[aIndex.FieldNames[0],aModel.TableName]);
end;
end;
function TDAODataBase.Add(aDAORecord : TDAORecord) : Boolean;
begin
Result := CreateQuery(fModels.Get(aDAORecord)).Add(aDAORecord);
end;
function TDAODataBase.AddOrUpdate(aDAORecord : TDAORecord) : Boolean;
begin
Result := CreateQuery(fModels.Get(aDAORecord)).AddOrUpdate(aDAORecord);
end;
function TDAODataBase.Delete(aDAORecord : TDAORecord) : Boolean;
begin
Result := CreateQuery(fModels.Get(aDAORecord)).Delete(aDAORecord);
end;
function TDAODataBase.Update(aDAORecord : TDAORecord) : Boolean;
begin
Result := CreateQuery(fModels.Get(aDAORecord)).Update(aDAORecord);
end;
function TDAODataBase.QueryGenerator: IDAOQueryGenerator;
begin
Result := fQueryGenerator;
end;
{ TDBConnectionSettings }
function TDBConnectionSettings.Clone: TDBConnectionSettings;
begin
Result := TDBConnectionSettings.Create;
Result.Provider := fDBProvider;
Result.Server := fServer;
Result.Database := fDatabase;
Result.UserName := fUserName;
Result.Password := fPassword;
Result.fIsCustomConnectionString := fIsCustomConnectionString;
Result.fCustomConnectionString := fCustomConnectionString;
end;
constructor TDBConnectionSettings.Create;
begin
fCustomConnectionString := '';
fIsCustomConnectionString := False;
end;
procedure TDBConnectionSettings.FromConnectionString(aDBProviderID : Integer; const aConnectionString: string);
begin
if aConnectionString.IsEmpty then fIsCustomConnectionString := False
else
begin
fCustomConnectionString := aConnectionString;
fIsCustomConnectionString := True;
end;
//get provider from connectionstring
if aDBProviderID <> 0 then fDBProvider := TDBProvider(aDBProviderID)
else
begin
if fCustomConnectionString.ToUpper.Contains('DRIVERID=SQLITE') then fDBProvider := TDBProvider.daoSQLite;
end;
end;
function TDBConnectionSettings.GetCustomConnectionString: string;
begin
Result := fCustomConnectionString;
end;
function TDBConnectionSettings.GetDatabase: string;
begin
Result := fDatabase;
end;
function TDBConnectionSettings.GetProvider: TDBProvider;
begin
Result := fDBProvider;
end;
function TDBConnectionSettings.GetServer: string;
begin
Result := fServer;
end;
function TDBConnectionSettings.GetUserName: string;
begin
Result := fUserName;
end;
function TDBConnectionSettings.IsCustomConnectionString: Boolean;
begin
Result := fIsCustomConnectionString;
end;
function TDBConnectionSettings.GetPassword: string;
begin
Result := fPassword;
end;
end.