-
Notifications
You must be signed in to change notification settings - Fork 32
/
Quick.ORM.Engine.pas
337 lines (272 loc) · 9.74 KB
/
Quick.ORM.Engine.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
{ ***************************************************************************
Copyright (c) 2016-2019 Kike Pérez
Unit : Quick.ORM.Engine
Description : Rest ORM Engine
Author : Kike Pérez
Version : 1.5
Created : 02/06/2017
Modified : 08/05/2019
This file is part of QuickORM: https://github.com/exilon/QuickORM
Uses Synopse mORMot framework. Copyright (C) 2017 Arnaud Bouchez
Synopse Informatique - https://synopse.info
***************************************************************************
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.ORM.Engine;
{$i QuickORM.inc}
{$INCLUDE synopse.inc}
interface
uses
Classes,
SysUtils,
{$IFDEF ANDROID}
SynCrossPlatformJSON,
SynCrossPlatformREST,
{$ELSE}
SynCommons,
mORMot,
{$ENDIF}
Quick.Commons;
const
DEF_CONNECTION_TIMEOUT = 20000;
DEF_ENCRYPTIONKEY = '234CC84D8A32D7477A7B1633615499AB';
DEF_NAMEDPIPE = 'defaultpipe';
DEF_ROOTURI = 'root';
SQLITE_MEMORY_DATABASE_NAME = ':memory:';
LOG_ALL : TSynLogInfos = [succ(sllNone)..high(TSynLogInfo)];
LOG_ONLYERRORS: TSynLogInfos = [sllLastError,sllError,sllException,sllExceptionOS,sllDDDError];
LOG_NONE: TSynLogInfos = [];
{$IFDEF DELPHIXE7_UP}
ProviderName : array of string = ['Microsoft.Jet.OLEDB.4.0',
'Microsoft.ACE.OLEDB.12.0',
'SQL Server',
'SQL Server Native Client 10.0',
'SQL Server Native Client 11.0',
'MySQL ODBC 5.2 UNICODE Driver',
'PostgreSQL Unicode'];
{$ELSE}
ProviderName : array[0..6] of string = ('Microsoft.Jet.OLEDB.4.0',
'Microsoft.ACE.OLEDB.12.0',
'SQL Server',
'SQL Server Native Client 10.0',
'SQL Server Native Client 11.0',
'MySQL ODBC 5.2 UNICODE Driver',
'PostgreSQL Unicode');
{$ENDIF}
type
{$IFNDEF MSWINDOWS}
EORMException = class(Exception);
{$ENDIF}
EORMServer = class(EORMException);
TDBType = (dtSQLite, dtMSSQL);
TSQLiteLockMode = (lmNormal, lmExclusive);
TDBProvider = (dbMSAccess2000, dbMSAccess2007, dbMSSQL, dbMSSQLnc10, dbMSSQLnc11, dbMySQL, dbPostgreSQL);
TSQLConnection = class
private
fProvider : TDBProvider;
fServerName : RawUTF8;
fServerPort : Integer;
fDataBase : RawUTF8;
fUsername : RawUTF8;
fUserPass : RawUTF8;
public
constructor Create;
property Provider : TDBProvider read fProvider write fProvider;
property ServerName : RawUTF8 read fServerName write fServerName;
property DataBase : RawUTF8 read fDataBase write fDataBase;
property UserName : RawUTF8 read fUsername write fUsername;
property UserPass : RawUTF8 read fUserPass write fUserPass;
function GetConnectionString: string;
end;
//TORMServer = class(TSQLRestServer)
//published
// procedure Sum(Ctxt: TSQLRestServerURIContext);
//end;
TORMClient = TSQLRestClientURI;
{$IFNDEF NEXTGEN}
TSrvProtocol = (spHTTP_Socket, spHTTPsys, spHTTPsys_SSL, spHTTPsys_AES, spHTTP_WebSocket, spWebSocketBidir_JSON, spWebSocketBidir_Binary, spWebSocketBidir_BinaryAES{$IFDEF MSWINDOWS}, spNamedPipe{$ENDIF});
{$ELSE}
TSrvProtocol = (spHTTP_Socket, spHTTP_SSL, spHTTP_AES, spHTTP_WebSocket, spWebSocketBidir_JSON, spWebSocketBidir_Binary, spWebSocketBidir_BinaryAES);
{$ENDIF}
TAuthMode = (amNoAuthentication, amSimple, amDefault, amHttpBasic {$IFDEF MSWINDOWS}, amSSPI {$ENDIF});
TServiceAuthorizationPolicy = (saAllowAll, saDenyAll);
TSecurityAppliedEvent = procedure of object;
TConnectionSuccessEvent = procedure of object;
TConnectionFailedEvent = procedure of object;
TIPRestrictedTryEvent = procedure(const ip : string) of object;
TApiKeyBeforeAccessEvent = procedure(const apikey : string; var allow : Boolean ) of object;
TApiKeyAfterAccessEvent = procedure(const apikey : string; allowed : Boolean) of object;
TLoadConfigEvent = procedure of object;
TIPBinding = class
IP : RawUTF8;
Port : Integer;
end;
TArrayOfRawUTF8 = array of RawUTF8;
{$IFNDEF ANDROID}
TArrayOfRawUTF8Helper = record helper for TArrayOfRawUTF8
public
procedure Add(const value : RawUTF8; AllowDuplicates : Boolean = False);
procedure Remove(const value : RawUTF8);
function Exists(const value : RawUTF8) : Boolean;
procedure Clear;
function Count : Integer;
end;
{$ENDIF}
TDBIndex = record
SQLRecordClass : TSQLRecordClass;
FieldNames : TArrayOfRawUTF8;
Unique : Boolean;
end;
TDBIndexArray = TArray<TDBIndex>;
TDBIndexArrayHelper = record helper for TDBIndexArray
public
procedure Add(aSQLRecordClass : TSQLRecordClass; aIndexFieldName : RawUTF8; aUnique : Boolean = False); overload;
procedure Add(aSQLRecordClass : TSQLRecordClass; aMultiIndexFieldNames : TArrayOfRawUTF8; aUnique : Boolean = False); overload;
end;
TDBMappingField = record
SQLRecordClass : TSQLRecordClass;
InternalFieldName : string;
ExternalFieldName : string;
end;
TDBMappingArray = TArray<TDBMappingField>;
TDBMappingArrayHelper = record helper for TDBMappingArray
public
procedure Map(aSQLRecordClass : TSQLRecordClass; const aInternalFieldName, aExternalFieldName : RawUTF8);
end;
TSQLRecordClassArray = array of TSQLRecordClass;
TFilterParams = record
Field : RawUTF8;
Value : Variant;
end;
TSQLRecordTimeStamped = class(TSQLRecord)
private
fCreationDate: TCreateTime;
fModifiedDate: TModTime;
published
property CreationDate: TCreateTime read fCreationDate write fCreationDate;
property ModifiedDate: TModTime read fModifiedDate write fModifiedDate;
end;
function GetQueryParam(const aParams : string; const QueryParam : string) : string;
implementation
function GetQueryParam(const aParams : string; const QueryParam : string) : string;
var
param : string;
pair : string;
value : TArray<string>;
begin
Result := '';
if aParams.Contains('?') then
begin
param := Copy(aParams,Pos('?',aParams)+1,aParams.Length);
for pair in param.Split(['&']) do
begin
value := pair.Split(['=']);
if value[0].ToLower = QueryParam.ToLower then
begin
Result := value[1];
Break;
end;
end;
end;
end;
{$IFNDEF ANDROID}
{ TArrayOfRawUTF8Helper }
procedure TArrayOfRawUTF8Helper.Add(const value: RawUTF8; AllowDuplicates : Boolean = False);
var
arr : TDynArray;
begin
arr.Init(TypeInfo(TArrayOfRawUTF8),Self);
if AllowDuplicates then
begin
if arr.IndexOf(value) = -1 then arr.Add(value);
end
else arr.Add(value);
end;
procedure TArrayOfRawUTF8Helper.Remove(const value: RawUTF8);
var
idx : Integer;
arr : TDynArray;
begin
arr.Init(TypeInfo(TArrayOfRawUTF8),Self);
idx := arr.IndexOf(value);
if idx <> -1 then arr.Delete(idx);
end;
function TArrayOfRawUTF8Helper.Exists(const value : RawUTF8) : Boolean;
var
idx : Integer;
arr : TDynArray;
begin
arr.Init(TypeInfo(TArrayOfRawUTF8),Self);
idx := arr.IndexOf(value);
if idx <> -1 then Result := True
else Result := False;
end;
procedure TArrayOfRawUTF8Helper.Clear;
begin
Self := [];
end;
function TArrayOfRawUTF8Helper.Count : Integer;
begin
Result := High(Self) + 1;
end;
{$ENDIF}
{ TDBIndexArrayHelper }
procedure TDBIndexArrayHelper.Add(aSQLRecordClass : TSQLRecordClass; aIndexFieldName : RawUTF8; aUnique : Boolean = False);
var
dbindex : TDBIndex;
begin
dbindex.SQLRecordClass := aSQLRecordClass;
dbindex.FieldNames := [aIndexFieldName];
dbindex.Unique := aUnique;
Self := Self + [dbindex];
end;
procedure TDBIndexArrayHelper.Add(aSQLRecordClass : TSQLRecordClass; aMultiIndexFieldNames : TArrayOfRawUTF8; aUnique : Boolean = False);
var
dbindex : TDBIndex;
begin
dbindex.SQLRecordClass := aSQLRecordClass;
dbindex.FieldNames := aMultiIndexFieldNames;
dbindex.Unique := aUnique;
Self := Self + [dbindex];
end;
{ TDBMappingArrayHelper }
procedure TDBMappingArrayHelper.Map(aSQLRecordClass: TSQLRecordClass; const aInternalFieldName, aExternalFieldName: RawUTF8);
var
dbMappingField : TDBMappingField;
begin
dbMappingField.SQLRecordClass := aSQLRecordClass;
dbMappingField.InternalFieldName := aInternalFieldName;
dbMappingField.ExternalFieldName := aExternalFieldName;
Self := Self + [dbMappingField];
end;
{ TSQLConnection }
constructor TSQLConnection.Create;
begin
fServerName := 'localhost';
fServerPort := 0;
end;
function TSQLConnection.GetConnectionString: string;
begin
if fServerPort = 0 then
case fProvider of
dbMSSQL, dbMSSQLnc10, dbMSSQLnc11 : fServerPort := 1433;
dbMySQL : fServerPort := 3306;
dbPostgreSQL : fServerPort := 5432;
end;
Result := Format('Driver={%s};Persist Security Info=False;UID=%s;Pwd=%s;Database=%s;Server=%s;Port=%d;MARS_Connection=yes',[
ProviderName[Integer(fProvider)],
fUserName,
fUserPass,
fDatabase,
fServerName,
fServerPort]);
end;
end.