-
Notifications
You must be signed in to change notification settings - Fork 32
/
Quick.ORM.RestDBFull.pas
150 lines (125 loc) · 4.56 KB
/
Quick.ORM.RestDBFull.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
{ ***************************************************************************
Copyright (c) 2016-2019 Kike Pérez
Unit : Quick.ORM.RestDBFull
Description : Rest ORM access local/remote db only
Author : Kike Pérez
Version : 1.6
Created : 12/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.RestDBFull;
{$i QuickORM.inc}
{$INCLUDE synopse.inc}
interface
uses
Classes,
SysUtils,
SynCommons,
SynDBODBC,
mORMot,
mORMotDB,
mORMotSQLite3,
SynSQLite3Static,
Quick.ORM.Engine,
Quick.ORM.Server.Base,
Quick.ORM.DataBase,
Quick.ORM.Security,
Quick.ORM.Server.Config;
type
//Server with direct DB access
TORMRestDBFull = class(TORMBaseServer)
private
fConfigFile : TORMRestDBFullConfig;
procedure ConfigFileLoaded;
public
ORM : TSQLRestServer;
constructor Create(cFullMemoryMode : Boolean = False); override;
destructor Destroy; override;
function Connect : Boolean; overload; override;
function Connect(DoCustomDB : TProc) : Boolean; overload;
end;
implementation
{TORMRestDB Class}
constructor TORMRestDBFull.Create(cFullMemoryMode : Boolean = False);
begin
inherited Create(cFullMemoryMode);
fConfigFile := TORMRestDBFullConfig.Create;
fConfigFile.OnLoadConfig := Self.ConfigFileLoaded;
//fConfigFile.Load; loads on connect
end;
destructor TORMRestDBFull.Destroy;
begin
if Assigned(ORM) then ORM.Free;
if Assigned(fConfigFile) then fConfigFile.Free;
inherited;
end;
function TORMRestDBFull.Connect : Boolean;
begin
Result := Connect(nil);
end;
function TORMRestDBFull.Connect(DoCustomDB : TProc) : Boolean;
var
DBIndex : TDBIndex;
begin
Result := False;
//load config file
if UseConfigFile then fConfigFile.Load;
if Assigned(DataBase.Model) then DataBase.Model.Free;
DataBase.Model := TSQLModel.Create(DataBase.IncludedClasses, DataBase.aRootURI);
if DataBase.FullMemoryMode then
begin
if DataBase.IncludedClasses = nil then ORM := TSQLRestServerFullMemory.CreateWithOwnModel([])
else ORM := TSQLRestServerFullMemory.Create(DataBase.Model,False);
end
else
begin
if not Assigned(DoCustomDb) then
begin
case DataBase.DBType of
dtSQLite : ORM := TSQLRestServerDB.Create(DataBase.Model,DataBase.DBFileName,Security.Enabled);
dtMSSQL :
begin {SQL Server Native Client 10.0}
//fDataBase.Model := TSQLModel.Create(fDataBase.IncludedClasses);
DataBase.SQLProperties := //TOleDBMSSQL2008ConnectionProperties.Create(fDataBase.SQLConnection.ServerName,fDataBase.SQLConnection.DataBase,fDataBase.SQLConnection.Username,fDataBase.SQLConnection.UserPass);
TODBCConnectionProperties.Create('','Driver={SQL Server} ;Database='+DataBase.SQLConnection.DataBase+';'+
'Server='+DataBase.SQLConnection.ServerName+';UID='+DataBase.SQLConnection.Username+';Pwd='+DataBase.SQLConnection.UserPass,'','');
VirtualTableExternalRegisterAll(DataBase.Model,DataBase.SQLProperties);
ORM := TSQLRestServerDB.Create(DataBase.Model,SQLITE_MEMORY_DATABASE_NAME,Security.Enabled,'');
end;
end;
end
else DoCustomDB;
end;
//create tables
ORM.CreateMissingTables;
//create indexes
for DBIndex in DataBase.DBIndexes do
begin
if DBIndex.FieldNames.Count > 1 then ORM.CreateSQLMultiIndex(DBIndex.SQLRecordClass,DBIndex.FieldNames,DBIndex.Unique)
else ORM.CreateSQLIndex(DBIndex.SQLRecordClass,DBIndex.FieldNames,DBIndex.Unique);
end;
//assigns ORM to security class
Security.SetORMServer(ORM);
//checks if default security needs to apply
Security.SetDefaultSecurity;
Result := True;
end;
procedure TORMRestDBFull.ConfigFileLoaded;
begin
//read base config file fields of Base class
ReadBaseConfigFile(fConfigFile);
end;
end.