2020-04-30 17:43:49 +02:00
|
|
|
// ***************************************************************************
|
|
|
|
//
|
|
|
|
// Delphi MVC Framework
|
|
|
|
//
|
2022-01-04 15:44:47 +01:00
|
|
|
// Copyright (c) 2010-2022 Daniele Teti and the DMVCFramework Team
|
2020-04-30 17:43:49 +02:00
|
|
|
//
|
|
|
|
// https://github.com/danieleteti/delphimvcframework
|
|
|
|
//
|
|
|
|
// ***************************************************************************
|
|
|
|
//
|
|
|
|
// 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 MVCFramework.Middleware.ActiveRecord;
|
|
|
|
|
|
|
|
{$I dmvcframework.inc}
|
|
|
|
|
|
|
|
interface
|
|
|
|
|
|
|
|
uses
|
|
|
|
System.SysUtils,
|
|
|
|
System.StrUtils,
|
|
|
|
System.Generics.Collections,
|
|
|
|
MVCFramework,
|
|
|
|
MVCFramework.Commons,
|
|
|
|
MVCFramework.Serializer.Commons;
|
|
|
|
|
|
|
|
type
|
|
|
|
|
|
|
|
TMVCActiveRecordMiddleware = class(TInterfacedObject, IMVCMiddleware)
|
|
|
|
private
|
|
|
|
fConnectionDefName: string;
|
2020-06-29 18:07:25 +02:00
|
|
|
fConnectionDefFileName: string;
|
|
|
|
fConnectionLoaded: Boolean;
|
2020-04-30 17:43:49 +02:00
|
|
|
protected
|
2020-06-29 18:07:25 +02:00
|
|
|
procedure EnsureConnection;
|
2020-04-30 17:43:49 +02:00
|
|
|
procedure OnBeforeRouting(
|
|
|
|
AContext: TWebContext;
|
|
|
|
var AHandled: Boolean);
|
|
|
|
|
|
|
|
procedure OnBeforeControllerAction(
|
|
|
|
AContext: TWebContext;
|
|
|
|
const AControllerQualifiedClassName: string;
|
|
|
|
const AActionName: string;
|
|
|
|
var AHandled: Boolean
|
|
|
|
);
|
|
|
|
|
|
|
|
procedure OnAfterControllerAction(
|
|
|
|
AContext: TWebContext;
|
2022-08-13 15:01:15 +02:00
|
|
|
const AControllerQualifiedClassName: string;
|
2020-04-30 17:43:49 +02:00
|
|
|
const AActionName: string;
|
|
|
|
const AHandled: Boolean
|
|
|
|
);
|
|
|
|
|
|
|
|
procedure OnAfterRouting(
|
|
|
|
AContext: TWebContext;
|
|
|
|
const AHandled: Boolean);
|
|
|
|
public
|
|
|
|
constructor Create(
|
|
|
|
const ConnectionDefName: string;
|
|
|
|
const ConnectionDefFileName: string = 'FDConnectionDefs.ini'); virtual;
|
|
|
|
end;
|
|
|
|
|
|
|
|
implementation
|
|
|
|
|
|
|
|
uses
|
|
|
|
MVCFramework.ActiveRecord,
|
2022-08-13 15:01:15 +02:00
|
|
|
System.SyncObjs,
|
2020-04-30 17:43:49 +02:00
|
|
|
FireDAC.Comp.Client;
|
|
|
|
|
2022-08-13 15:01:15 +02:00
|
|
|
var
|
|
|
|
gCONNECTION_DEF_FILE_LOADED: Integer = 0;
|
|
|
|
|
2020-04-30 17:43:49 +02:00
|
|
|
{ TMVCActiveRecordMiddleware }
|
|
|
|
|
|
|
|
constructor TMVCActiveRecordMiddleware.Create(const ConnectionDefName: string;
|
|
|
|
const ConnectionDefFileName: string);
|
|
|
|
begin
|
|
|
|
inherited Create;
|
2020-06-29 18:07:25 +02:00
|
|
|
fConnectionLoaded := False;
|
|
|
|
fConnectionDefName := ConnectionDefName;
|
|
|
|
fConnectionDefFileName := ConnectionDefFileName;
|
|
|
|
end;
|
|
|
|
|
|
|
|
procedure TMVCActiveRecordMiddleware.EnsureConnection;
|
|
|
|
begin
|
|
|
|
if fConnectionLoaded then
|
2020-04-30 17:43:49 +02:00
|
|
|
begin
|
2020-06-29 18:07:25 +02:00
|
|
|
Exit;
|
2020-04-30 17:43:49 +02:00
|
|
|
end;
|
2020-06-29 18:07:25 +02:00
|
|
|
|
2022-08-13 15:01:15 +02:00
|
|
|
TMonitor.Enter(Self);
|
|
|
|
try
|
|
|
|
if fConnectionLoaded then
|
|
|
|
begin
|
|
|
|
Exit;
|
|
|
|
end;
|
|
|
|
if TInterlocked.CompareExchange(gCONNECTION_DEF_FILE_LOADED, 1, 0) = 0 then
|
|
|
|
begin
|
2022-08-28 13:06:16 +02:00
|
|
|
if not fConnectionDefFileName.IsEmpty then
|
2022-08-13 15:01:15 +02:00
|
|
|
begin
|
2022-08-28 13:06:16 +02:00
|
|
|
FDManager.ConnectionDefFileAutoLoad := False;
|
|
|
|
FDManager.ConnectionDefFileName := fConnectionDefFileName;
|
2022-10-09 15:45:59 +02:00
|
|
|
if not FDManager.ConnectionDefFileLoaded then
|
|
|
|
begin
|
|
|
|
FDManager.LoadConnectionDefFile;
|
|
|
|
end;
|
2022-08-28 13:06:16 +02:00
|
|
|
if not FDManager.IsConnectionDef(fConnectionDefName) then
|
|
|
|
begin
|
|
|
|
raise EMVCConfigException.CreateFmt('ConnectionDefName "%s" not found in config file "%s"',
|
|
|
|
[fConnectionDefName, FDManager.ActualConnectionDefFileName]);
|
|
|
|
end;
|
2022-08-13 15:01:15 +02:00
|
|
|
end;
|
|
|
|
end;
|
2020-06-29 18:07:25 +02:00
|
|
|
fConnectionLoaded := True;
|
2022-08-13 15:01:15 +02:00
|
|
|
finally
|
|
|
|
TMonitor.Exit(Self);
|
2020-04-30 17:43:49 +02:00
|
|
|
end;
|
|
|
|
end;
|
|
|
|
|
|
|
|
procedure TMVCActiveRecordMiddleware.OnAfterControllerAction(
|
|
|
|
AContext: TWebContext;
|
2022-08-13 15:01:15 +02:00
|
|
|
const AControllerQualifiedClassName: string;
|
2020-04-30 17:43:49 +02:00
|
|
|
const AActionName: string;
|
|
|
|
const AHandled: Boolean);
|
|
|
|
begin
|
|
|
|
// Implement as needed
|
|
|
|
end;
|
|
|
|
|
|
|
|
procedure TMVCActiveRecordMiddleware.OnAfterRouting(AContext: TWebContext; const AHandled: Boolean);
|
|
|
|
begin
|
2022-10-21 18:32:33 +02:00
|
|
|
ActiveRecordConnectionsRegistry.RemoveDefaultConnection(False);
|
2020-04-30 17:43:49 +02:00
|
|
|
end;
|
|
|
|
|
|
|
|
procedure TMVCActiveRecordMiddleware.OnBeforeControllerAction(
|
|
|
|
AContext: TWebContext;
|
|
|
|
const AControllerQualifiedClassName, AActionName: string;
|
|
|
|
var AHandled: Boolean);
|
|
|
|
begin
|
|
|
|
// do nothing
|
|
|
|
end;
|
|
|
|
|
|
|
|
procedure TMVCActiveRecordMiddleware.OnBeforeRouting(AContext: TWebContext; var AHandled: Boolean);
|
|
|
|
begin
|
2020-06-29 18:07:25 +02:00
|
|
|
EnsureConnection;
|
2022-08-13 15:01:15 +02:00
|
|
|
ActiveRecordConnectionsRegistry.AddDefaultConnection(fConnectionDefName);
|
2020-04-30 17:43:49 +02:00
|
|
|
AHandled := False;
|
|
|
|
end;
|
|
|
|
|
|
|
|
end.
|