Skip to content

Commit

Permalink
Invalidate connections (#7)
Browse files Browse the repository at this point in the history
  • Loading branch information
glprog authored Feb 19, 2020
1 parent f521b78 commit 301ad9b
Show file tree
Hide file tree
Showing 5 changed files with 97 additions and 1 deletion.
31 changes: 31 additions & 0 deletions MiniREST.SQL.Base.pas
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ TMiniRESTSQLConnectionFactoryBase = class abstract(TInterfacedObject, IMiniRES
function GetQueueCount: Integer; virtual; abstract;
function GetConnection(const AIdentifier: string): IMiniRESTSQLConnection; overload;
function GetSingletonConnection: IMiniRESTSQLConnection;
procedure InvalidateConnections;
end;

{ TMiniRESTSQLConnectionBase }
Expand All @@ -56,9 +57,11 @@ TMiniRESTSQLConnectionBase = class abstract(TInterfacedObject, IMiniRESTSQLCon
strict private
FOwner: TObject;
FConnectionID: Integer;
FValid: Boolean;
protected
FName: string;
FEstaNoPool: Boolean;
procedure SetValid(const AValid: Boolean);
function _Release: Integer; {$IFNDEF WINDOWS}cdecl{$ELSE}stdcall{$ENDIF};
function GetObject: TObject; virtual; abstract;
procedure SetOwner(AOwner: Pointer);
Expand All @@ -78,6 +81,8 @@ TMiniRESTSQLConnectionBase = class abstract(TInterfacedObject, IMiniRESTSQLCon
function GetDatabaseInfo: IMiniRESTSQLDatabaseInfo; virtual; abstract;
function InTransaction: Boolean; virtual; abstract;
function GetConnectionID: Integer;
function IsValid: Boolean;
procedure Invalidate; virtual; abstract;
end;

TMiniRESTSQLPrimaryKeyInfo = class(TInterfacedObject, IMiniRESTSQLPrimaryKeyInfo)
Expand Down Expand Up @@ -354,6 +359,7 @@ function TMiniRESTSQLConnectionFactoryBase.GetConnection(const AIdentifier: stri
LConnection := InternalGetconnection.SetName('Connection' + IntToStr(FConnectionCounter));
Inc(FConnectionCounter);
Result := LConnection;
TMiniRESTSQLConnectionBase(Result).SetValid(True);
end
else
begin
Expand All @@ -379,6 +385,7 @@ function TMiniRESTSQLConnectionFactoryBase.GetConnection(const AIdentifier: stri
LConnection := InternalGetconnection.SetName('Connection' + IntToStr(FConnectionCounter) + ' ' + AIdentifier);
Inc(FConnectionCounter);
Result := LConnection;
TMiniRESTSQLConnectionBase(Result.GetObject).SetValid(True);
end
else
begin
Expand Down Expand Up @@ -484,6 +491,30 @@ function TMiniRESTSQLConnectionFactoryBase.GetSingletonConnection: IMiniRESTSQLC
Result := FSingletonConnection;
end;

procedure TMiniRESTSQLConnectionFactoryBase.InvalidateConnections;
var
I: Integer;
LConnectionObj: TMiniRESTSQLConnectionBase;
LConnection: IMiniRESTSQLConnection;
begin
for I := 0 to (FConnectionsToNotifyFree.Count - 1) do
begin
LConnectionObj := FConnectionsToNotifyFree.Items[I];
if LConnectionObj.GetInterface(IMiniRESTSQLConnection, LConnection) then
LConnection.Invalidate;
end;
end;

function TMiniRESTSQLConnectionBase.IsValid: Boolean;
begin
Result := FValid;
end;

procedure TMiniRESTSQLConnectionBase.SetValid(const AValid: Boolean);
begin
FValid := AValid;
end;

initialization
gConnectionIDCounter := 0;

Expand Down
3 changes: 3 additions & 0 deletions MiniREST.SQL.Intf.pas
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,8 @@ interface
function SetName(const AName: string): IMiniRESTSQLConnection;
function GetDatabaseInfo: IMiniRESTSQLDatabaseInfo;
function GetConnectionID: Integer;
function IsValid: Boolean;
procedure Invalidate;
end;

IMiniRESTSQLConnectionFactory = interface
Expand All @@ -66,6 +68,7 @@ interface
function GetObject: TObject;
function GetConnectionsCount: Integer;
function GetQueueCount: Integer;
procedure InvalidateConnections;
property ConnectionsCount: Integer read GetConnectionsCount;
property QueueCount: Integer read GetQueueCount;
end;
Expand Down
23 changes: 23 additions & 0 deletions MiniREST.SQL.SQLDb.pas
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ TMiniRESTSQLConnectionSQLDb = class(TMiniRESTSQLConnectionBase)
function GetDriverName(const ADatabaseType: TMiniRESTSQLDatabaseType): string;
procedure Log(Sender : TSQLConnection; EventType : TDBEventType; Const Msg : String);
procedure SetMiniRESTSQLParamToSQLParam(AMiniRESTSQLParam: IMiniRESTSQLParam; ASQLParam: TParam);
procedure CheckConnectionIsValid;
public
constructor Create(AOwner: IMiniRESTSQLConnectionFactory; AParams: IMiniRESTSQLConnectionFactoryParamsSQLDb);
destructor Destroy; override;
Expand All @@ -89,6 +90,7 @@ TMiniRESTSQLConnectionSQLDb = class(TMiniRESTSQLConnectionBase)
function Execute(const ACommand: string; AParams: array of IMiniRESTSQLParam): Integer; override;
function GetDatabaseInfo: IMiniRESTSQLDatabaseInfo; override;
function InTransaction: Boolean; override;
procedure Invalidate; override;
end;

{ TMiniRESTSQLQuerySQLDb }
Expand Down Expand Up @@ -224,6 +226,7 @@ procedure TMiniRESTSQLConnectionSQLDb.Connect;
LName: string;
I: Integer;
begin
CheckConnectionIsValid;
LStringList := TStringList.Create;
try
if FSQLConnection.Connected then
Expand All @@ -247,6 +250,7 @@ procedure TMiniRESTSQLConnectionSQLDb.Connect;

procedure TMiniRESTSQLConnectionSQLDb.StartTransaction;
begin
CheckConnectionIsValid;
if FTransaction.Active then
FTransaction.Rollback;
FTransaction.StartTransaction;
Expand All @@ -255,18 +259,21 @@ procedure TMiniRESTSQLConnectionSQLDb.StartTransaction;

procedure TMiniRESTSQLConnectionSQLDb.Commit;
begin
CheckConnectionIsValid;
FTransaction.Commit;
FInExplicitTransaction := False;
end;

procedure TMiniRESTSQLConnectionSQLDb.Rollback;
begin
CheckConnectionIsValid;
FTransaction.Rollback;
FInExplicitTransaction := False;
end;

function TMiniRESTSQLConnectionSQLDb.GetQuery: IMiniRESTSQLQuery;
begin
CheckConnectionIsValid;
Result := TMiniRESTSQLQuerySQLDb.Create(Self);
end;

Expand All @@ -293,6 +300,7 @@ function TMiniRESTSQLConnectionSQLDb.Execute(const ACommand: string; AParams: ar
LParam: TParam;
LMiniRESTSQLParam: IMiniRESTSQLParam;
begin
CheckConnectionIsValid;
Self.Connect;
LQry := TSQLQuery.Create(nil);
try
Expand Down Expand Up @@ -321,6 +329,7 @@ function TMiniRESTSQLConnectionSQLDb.Execute(const ACommand: string; AParams: ar

function TMiniRESTSQLConnectionSQLDb.GetDatabaseInfo: IMiniRESTSQLDatabaseInfo;
begin
CheckConnectionIsValid;
Result := nil;
case FConnectionParams.GetDatabaseType of
dbtFirebird: Result := TMiniRESTSQLDatabaseInfoFirebird.Create(Self);
Expand Down Expand Up @@ -523,6 +532,7 @@ procedure TMiniRESTSQLConnectionSQLDb.SetMiniRESTSQLParamToSQLParam(AMiniRESTSQL

function TMiniRESTSQLConnectionSQLDb.InTransaction: Boolean;
begin
CheckConnectionIsValid;
Result := FSQLConnection.Transaction.Active;
end;

Expand All @@ -538,4 +548,17 @@ function TMiniRESTSQLConnectionFactorySQLDb.GetQueueCount: Integer;
Result := 0;
end;

procedure TMiniRESTSQLConnectionSQLDb.Invalidate;
begin
SetValid(False);
FreeAndNil(FSQLConnection);
FreeAndNil(FTransaction);
end;

procedure TMiniRESTSQLConnectionSQLDb.CheckConnectionIsValid;
begin
if not IsValid then
raise Exception.Create('A conexão foi invalidada.');
end;

end.
41 changes: 40 additions & 1 deletion unittest/SQL/Test.SQL.Default.pas
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,14 @@ TMiniRESTSQLTest = class({$IFNDEF FPC}TObject{$ELSE}TTestCase{$IFEND})
[Test]
{$IFEND}
procedure TestGetSingletonConnection;
{$IFNDEF FPC}
[Test]
{$IFEND}
procedure TestConnectionIsValid;
{$IFNDEF FPC}
[Test]
{$IFEND}
procedure TestConnectionIsNotValid;
(* {$IFNDEF FPC}
[Test]
{$IFEND}
Expand Down Expand Up @@ -150,8 +158,9 @@ procedure TMiniRESTSQLTest.TearDown;
var
LConnection: IMiniRESTSQLConnection;
begin
LConnection := FConnectionFactory.GetConnection;
(* LConnection := FConnectionFactory.GetConnection;
LConnection.Execute('DELETE FROM CUSTOMER', []);
*)
FConnectionPoolEvents.Free;
end;

Expand Down Expand Up @@ -713,4 +722,34 @@ procedure TMiniRESTSQLTest.TestGetSingletonConnection;
{$IFEND}
end;

procedure TMiniRESTSQLTest.TestConnectionIsValid;
var
LConn1: IMiniRESTSQLConnection;
begin
LConn1 := FConnectionFactory.GetConnection;
{$IFNDEF FPC}
Assert.IsTrue(LConn1.IsValid, 'LCon1 não está válida.');
{$ELSE}
CheckTrue(LConn1.IsValid, 'LCon1 não está válida.');
{$IFEND}
end;

procedure TMiniRESTSQLTest.TestConnectionIsNotValid;
var
LConn1: IMiniRESTSQLConnection;
begin
LConn1 := FConnectionFactory.GetConnection;
{$IFNDEF FPC}
Assert.IsTrue(LConn1.IsValid, 'LCon1 não está válida.');
{$ELSE}
CheckTrue(LConn1.IsValid, 'LCon1 não está válida.');
{$IFEND}
FConnectionFactory.InvalidateConnections;
{$IFNDEF FPC}
Assert.IsFalse(LConn1.IsValid, 'LCon1 está válida.');
{$ELSE}
CheckFalse(LConn1.IsValid, 'LCon1 está válida.');
{$IFEND}
end;

end.
Binary file modified unittest/TEST.FDB
Binary file not shown.

0 comments on commit 301ad9b

Please sign in to comment.