Skip to content

Commit cb6f0f0

Browse files
committed
https://github.com/danieleteti/delphimvcframework/issues/581
1 parent 4a25c05 commit cb6f0f0

File tree

2 files changed

+87
-14
lines changed

2 files changed

+87
-14
lines changed

sources/MVCFramework.ActiveRecord.pas

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -541,9 +541,10 @@ TMVCEntitiesRegistry = class(TInterfacedObject, IMVCEntitiesRegistry)
541541

542542
IMVCActiveRecordConnections = interface
543543
['{7B87473C-1784-489F-A838-925E7DDD0DE2}']
544-
procedure AddConnection(const aName: string; const aConnection: TFDConnection; const Owns: Boolean = false);
544+
procedure AddConnection(const aName: string; const aConnection: TFDConnection; const Owns: Boolean = false); overload;
545545
procedure AddDefaultConnection(const aConnection: TFDConnection; const Owns: Boolean = false); overload;
546546
procedure AddDefaultConnection(const aConnectionDefName: String); overload;
547+
procedure AddConnection(const aName, aConnectionDefName: String); overload;
547548
procedure RemoveConnection(const aName: string; const RaiseExceptionIfNotAvailable: Boolean = True);
548549
procedure RemoveDefaultConnection(const RaiseExceptionIfNotAvailable: Boolean = True);
549550
procedure SetCurrent(const aName: string);
@@ -569,7 +570,8 @@ TConnHolder = class
569570
public
570571
constructor Create; virtual;
571572
destructor Destroy; override;
572-
procedure AddConnection(const aName: string; const aConnection: TFDConnection; const aOwns: Boolean = false);
573+
procedure AddConnection(const aName: string; const aConnection: TFDConnection; const aOwns: Boolean = false); overload;
574+
procedure AddConnection(const aName, aConnectionDefName: String); overload;
573575
procedure AddDefaultConnection(const aConnection: TFDConnection; const aOwns: Boolean = false); overload;
574576
procedure AddDefaultConnection(const aConnectionDefName: String); overload;
575577
procedure RemoveConnection(const aName: string; const RaiseExceptionIfNotAvailable: Boolean = True);
@@ -883,14 +885,15 @@ procedure TMVCConnectionsRepository.AddDefaultConnection(const aConnection: TFDC
883885
AddConnection('default', aConnection, aOwns);
884886
end;
885887

886-
procedure TMVCConnectionsRepository.AddDefaultConnection(const aConnectionDefName: String);
888+
procedure TMVCConnectionsRepository.AddConnection(const aName,
889+
aConnectionDefName: String);
887890
var
888891
lConn: TFDConnection;
889892
begin
890893
lConn := TFDConnection.Create(nil);
891894
try
892895
lConn.ConnectionDefName := aConnectionDefName;
893-
AddDefaultConnection(lConn, True);
896+
AddConnection(aName, lConn, True);
894897
except
895898
on E: Exception do
896899
begin
@@ -900,6 +903,11 @@ procedure TMVCConnectionsRepository.AddDefaultConnection(const aConnectionDefNam
900903
end;
901904
end;
902905

906+
procedure TMVCConnectionsRepository.AddDefaultConnection(const aConnectionDefName: String);
907+
begin
908+
AddConnection('default', aConnectionDefName);
909+
end;
910+
903911
constructor TMVCConnectionsRepository.Create;
904912
begin
905913
inherited;

sources/MVCFramework.Middleware.ActiveRecord.pas

Lines changed: 75 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -40,9 +40,13 @@ interface
4040

4141
TMVCActiveRecordMiddleware = class(TInterfacedObject, IMVCMiddleware)
4242
private
43-
fConnectionDefName: string;
43+
fDefaultConnectionDefName: string;
4444
fConnectionDefFileName: string;
4545
fConnectionLoaded: Boolean;
46+
fAdditionalARConnectionNames: TArray<String>;
47+
fAdditionalConnectionDefNames: TArray<String>;
48+
fAdditionalARConnectionNamesCount: Integer;
49+
fAdditionalConnectionDefNamesCount: Integer;
4650
protected
4751
procedure EnsureConnection;
4852
procedure OnBeforeRouting(
@@ -68,8 +72,13 @@ TMVCActiveRecordMiddleware = class(TInterfacedObject, IMVCMiddleware)
6872
const AHandled: Boolean);
6973
public
7074
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;
7382
end;
7483

7584
implementation
@@ -84,16 +93,30 @@ implementation
8493

8594
{ TMVCActiveRecordMiddleware }
8695

87-
constructor TMVCActiveRecordMiddleware.Create(const ConnectionDefName: string;
96+
constructor TMVCActiveRecordMiddleware.Create(const DefaultConnectionDefName: string;
8897
const ConnectionDefFileName: string);
8998
begin
9099
inherited Create;
91100
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;
93112
fConnectionDefFileName := ConnectionDefFileName;
113+
fAdditionalARConnectionNames := AdditionalARConnectionNames;
114+
fAdditionalConnectionDefNames := AdditionalConnectionDefNames;
94115
end;
95116

96117
procedure TMVCActiveRecordMiddleware.EnsureConnection;
118+
var
119+
I: Integer;
97120
begin
98121
if fConnectionLoaded then
99122
begin
@@ -116,10 +139,34 @@ procedure TMVCActiveRecordMiddleware.EnsureConnection;
116139
begin
117140
FDManager.LoadConnectionDefFile;
118141
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
120164
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;
123170
end;
124171
end;
125172
end;
@@ -139,8 +186,17 @@ procedure TMVCActiveRecordMiddleware.OnAfterControllerAction(
139186
end;
140187

141188
procedure TMVCActiveRecordMiddleware.OnAfterRouting(AContext: TWebContext; const AHandled: Boolean);
189+
var
190+
I: Integer;
142191
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;
144200
end;
145201

146202
procedure TMVCActiveRecordMiddleware.OnBeforeControllerAction(
@@ -152,9 +208,18 @@ procedure TMVCActiveRecordMiddleware.OnBeforeControllerAction(
152208
end;
153209

154210
procedure TMVCActiveRecordMiddleware.OnBeforeRouting(AContext: TWebContext; var AHandled: Boolean);
211+
var
212+
I: Integer;
155213
begin
156214
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;
158223
AHandled := False;
159224
end;
160225

0 commit comments

Comments
 (0)