@@ -40,9 +40,13 @@ interface
40
40
41
41
TMVCActiveRecordMiddleware = class (TInterfacedObject, IMVCMiddleware)
42
42
private
43
- fConnectionDefName : string;
43
+ fDefaultConnectionDefName : string;
44
44
fConnectionDefFileName: string;
45
45
fConnectionLoaded: Boolean;
46
+ fAdditionalARConnectionNames: TArray<String>;
47
+ fAdditionalConnectionDefNames: TArray<String>;
48
+ fAdditionalARConnectionNamesCount: Integer;
49
+ fAdditionalConnectionDefNamesCount: Integer;
46
50
protected
47
51
procedure EnsureConnection ;
48
52
procedure OnBeforeRouting (
@@ -68,8 +72,13 @@ TMVCActiveRecordMiddleware = class(TInterfacedObject, IMVCMiddleware)
68
72
const AHandled: Boolean);
69
73
public
70
74
constructor Create(
71
- const ConnectionDefName: string;
72
- const ConnectionDefFileName: string = ' FDConnectionDefs.ini' ); virtual ;
75
+ const DefaultConnectionDefName: string;
76
+ const ConnectionDefFileName: string{ = 'FDConnectionDefs.ini'} ); overload; virtual ;
77
+ constructor Create(
78
+ const DefaultConnectionDefName: string;
79
+ const AdditionalARConnectionNames: TArray<String>;
80
+ const AdditionalConnectionDefNames: TArray<String>;
81
+ const ConnectionDefFileName: string{ = 'FDConnectionDefs.ini'} ); overload; virtual ;
73
82
end ;
74
83
75
84
implementation
@@ -84,16 +93,30 @@ implementation
84
93
85
94
{ TMVCActiveRecordMiddleware }
86
95
87
- constructor TMVCActiveRecordMiddleware.Create(const ConnectionDefName : string;
96
+ constructor TMVCActiveRecordMiddleware.Create(const DefaultConnectionDefName : string;
88
97
const ConnectionDefFileName: string);
89
98
begin
90
99
inherited Create;
91
100
fConnectionLoaded := False;
92
- fConnectionDefName := ConnectionDefName;
101
+ fDefaultConnectionDefName := DefaultConnectionDefName;
102
+ fConnectionDefFileName := ConnectionDefFileName;
103
+ end ;
104
+
105
+ constructor TMVCActiveRecordMiddleware.Create(
106
+ const DefaultConnectionDefName: string;
107
+ const AdditionalARConnectionNames, AdditionalConnectionDefNames: TArray<String>;
108
+ const ConnectionDefFileName: string);
109
+ begin
110
+ fConnectionLoaded := False;
111
+ fDefaultConnectionDefName := DefaultConnectionDefName;
93
112
fConnectionDefFileName := ConnectionDefFileName;
113
+ fAdditionalARConnectionNames := AdditionalARConnectionNames;
114
+ fAdditionalConnectionDefNames := AdditionalConnectionDefNames;
94
115
end ;
95
116
96
117
procedure TMVCActiveRecordMiddleware.EnsureConnection ;
118
+ var
119
+ I: Integer;
97
120
begin
98
121
if fConnectionLoaded then
99
122
begin
@@ -116,10 +139,34 @@ procedure TMVCActiveRecordMiddleware.EnsureConnection;
116
139
begin
117
140
FDManager.LoadConnectionDefFile;
118
141
end ;
119
- if not FDManager.IsConnectionDef(fConnectionDefName) then
142
+ end ;
143
+
144
+ // loading default connection
145
+ if not fDefaultConnectionDefName.IsEmpty then
146
+ begin
147
+ if not FDManager.IsConnectionDef(fDefaultConnectionDefName) then
148
+ begin
149
+ raise EMVCConfigException.CreateFmt(' ConnectionDefName "%s" not found in config file "%s" - or config file not present' ,
150
+ [fDefaultConnectionDefName, FDManager.ActualConnectionDefFileName]);
151
+ end ;
152
+ end ;
153
+
154
+ // loading additional connections
155
+ fAdditionalARConnectionNamesCount := Length(fAdditionalARConnectionNames);
156
+ fAdditionalConnectionDefNamesCount := Length(fAdditionalConnectionDefNames);
157
+ if (fAdditionalARConnectionNamesCount > 0 ) or (fAdditionalConnectionDefNamesCount > 0 ) then
158
+ begin
159
+ if fAdditionalARConnectionNamesCount <> fAdditionalConnectionDefNamesCount then
160
+ begin
161
+ raise EMVCConfigException.Create(' AdditionalARConnectionNames must have the same length of AdditionalConnectionDefNames' );
162
+ end ;
163
+ for I := 0 to fAdditionalConnectionDefNamesCount - 1 do
120
164
begin
121
- raise EMVCConfigException.CreateFmt(' ConnectionDefName "%s" not found in config file "%s"' ,
122
- [fConnectionDefName, FDManager.ActualConnectionDefFileName]);
165
+ if not FDManager.IsConnectionDef(fAdditionalConnectionDefNames[I]) then
166
+ begin
167
+ raise EMVCConfigException.CreateFmt(' ConnectionDefName "%s" not found in config file "%s"' ,
168
+ [fAdditionalConnectionDefNames[I], FDManager.ActualConnectionDefFileName]);
169
+ end ;
123
170
end ;
124
171
end ;
125
172
end ;
@@ -139,8 +186,17 @@ procedure TMVCActiveRecordMiddleware.OnAfterControllerAction(
139
186
end ;
140
187
141
188
procedure TMVCActiveRecordMiddleware.OnAfterRouting (AContext: TWebContext; const AHandled: Boolean);
189
+ var
190
+ I: Integer;
142
191
begin
143
- ActiveRecordConnectionsRegistry.RemoveDefaultConnection(False);
192
+ if not fDefaultConnectionDefName.IsEmpty then
193
+ begin
194
+ ActiveRecordConnectionsRegistry.RemoveDefaultConnection(False);
195
+ end ;
196
+ for I := 0 to fAdditionalConnectionDefNamesCount - 1 do
197
+ begin
198
+ ActiveRecordConnectionsRegistry.RemoveConnection(fAdditionalARConnectionNames[I], False);
199
+ end ;
144
200
end ;
145
201
146
202
procedure TMVCActiveRecordMiddleware.OnBeforeControllerAction (
@@ -152,9 +208,18 @@ procedure TMVCActiveRecordMiddleware.OnBeforeControllerAction(
152
208
end ;
153
209
154
210
procedure TMVCActiveRecordMiddleware.OnBeforeRouting (AContext: TWebContext; var AHandled: Boolean);
211
+ var
212
+ I: Integer;
155
213
begin
156
214
EnsureConnection;
157
- ActiveRecordConnectionsRegistry.AddDefaultConnection(fConnectionDefName);
215
+ if not fDefaultConnectionDefName.IsEmpty then
216
+ begin
217
+ ActiveRecordConnectionsRegistry.AddDefaultConnection(fDefaultConnectionDefName);
218
+ end ;
219
+ for I := 0 to fAdditionalConnectionDefNamesCount - 1 do
220
+ begin
221
+ ActiveRecordConnectionsRegistry.AddConnection(fAdditionalARConnectionNames[I], fAdditionalConnectionDefNames[I]);
222
+ end ;
158
223
AHandled := False;
159
224
end ;
160
225
0 commit comments