-
Notifications
You must be signed in to change notification settings - Fork 32
/
Quick.ORM.RestDB.pas
155 lines (130 loc) · 5.07 KB
/
Quick.ORM.RestDB.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
{ ***************************************************************************
Copyright (c) 2016-2019 Kike Pérez
Unit : Quick.ORM.RestDB
Description : Rest ORM access SQLite db only
Author : Kike Pérez
Version : 1.6
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.RestDB;
{$i QuickORM.inc}
{$INCLUDE synopse.inc}
interface
uses
Classes,
SysUtils,
{$IFDEF NEXTGEN}
Not compatible with firemonkey android/ios
{$ENDIF}
SynCommons,
SynDBODBC,
mORMot,
mORMotDB,
mORMotSQLite3,
SynSQLite3,
SynSQLite3Static,
Quick.ORM.Engine,
Quick.ORM.DataBase,
Quick.ORM.Security,
Quick.ORM.Server.Config;
type
TIDDynArray = mORMot.TIDDynArray;
//Client with direct SQLite DB access
TORMRestDB = class
private
fDataBase : TORMDataBase;
fSecurity : TORMSecurity;
fConfigFile : TORMRestDBConfig;
fUseConfigFile : Boolean;
public
ORM : TSQLRestClientDB;
property DataBase : TORMDataBase read fDataBase write fDataBase;
property Security : TORMSecurity read fSecurity write fSecurity;
constructor Create;
destructor Destroy; override;
function Connect : Boolean; overload;
function Connect(DoCustomDB : TProc) : Boolean; overload;
end;
implementation
{TORMRestDB Class}
constructor TORMRestDB.Create;
begin
inherited;
fDataBase := TORMDataBase.Create;
fSecurity := TORMSecurity.Create;
end;
destructor TORMRestDB.Destroy;
begin
if Assigned(ORM) then ORM.Free;
if Assigned(fDataBase) then fDataBase.Free;
if Assigned(fSecurity) then fSecurity.Free;
inherited;
end;
function TORMRestDB.Connect : Boolean;
begin
Result := Connect(nil);
end;
function TORMRestDB.Connect(DoCustomDB : TProc) : Boolean;
var
DBIndex : TDBIndex;
begin
Result := False;
if Assigned(fDataBase.Model) then fDataBase.Model.Free;
fDataBase.Model := TSQLModel.Create(fDataBase.IncludedClasses, fDataBase.aRootURI);
if fDataBase.FullMemoryMode then
begin
ORM := TSQLRestClientDB.Create(fDataBase.Model,nil,SQLITE_MEMORY_DATABASE_NAME,TSQLRestServerDB,fSecurity.Enabled,'');
end
else
begin
if not Assigned(DoCustomDb) then
begin
case fDataBase.DBType of
dtSQLite : ORM := TSQLRestClientDB.Create(fDataBase.Model, nil, fDataBase.DBFileName, TSQLRestServerDB);
dtMSSQL :
begin {SQL Server Native Client 10.0} {Microsoft OLE DB Provider for SQL Server}
//fDataBase.Model := TSQLModel.Create(fDataBase.IncludedClasses);
fDataBase.SQLProperties := //TOleDBMSSQL2008ConnectionProperties.Create(fDataBase.SQLConnection.ServerName,fDataBase.SQLConnection.DataBase,fDataBase.SQLConnection.Username,fDataBase.SQLConnection.UserPass);
//TODBCConnectionProperties.Create('','Driver={SQL Server Native Client 10.0} ;Database='+fDataBase.SQLConnection.DataBase+';'+
// 'Server='+fDataBase.SQLConnection.ServerName+';UID='+fDataBase.SQLConnection.Username+';Pwd='+fDataBase.SQLConnection.UserPass+';MARS_Connection=yes','','');
TODBCConnectionProperties.Create('',DataBase.SQLConnection.GetConnectionString,'','');
VirtualTableExternalRegisterAll(fDataBase.Model,fDataBase.SQLProperties);
//fDataBase.Model.VirtualTableRegister(fDataBase.IncludedClasses[0],TSQLVirtualTableBinary);
ORM := TSQLRestClientDB.Create(fDataBase.Model,nil,SQLITE_MEMORY_DATABASE_NAME,TSQLRestServerDB,fSecurity.Enabled,'');
end;
end;
end
else DoCustomDB;
end;
//exclusive mode speeds up sqlite performance, but db can't be accessible from outside processes
if fDataBase.LockMode = TSQLiteLockMode.lmExclusive then ORM.Server.DB.LockingMode := TSQLLockingMode.lmExclusive
else ORM.Server.DB.LockingMode := TSQLLockingMode.lmNormal;
//creates tables if not exists
ORM.Server.CreateMissingTables;
//create indexes
for DBIndex in DataBase.DBIndexes do
begin
if DBIndex.FieldNames.Count > 1 then ORM.Server.CreateSQLMultiIndex(DBIndex.SQLRecordClass,DBIndex.FieldNames,DBIndex.Unique)
else ORM.Server.CreateSQLIndex(DBIndex.SQLRecordClass,DBIndex.FieldNames,DBIndex.Unique);
end;
//assigns ORM to security class
fSecurity.SetORMServer(ORM);
//checks if default security needs to apply
fSecurity.SetDefaultSecurity;
Result := True;
end;
end.