Skip to content

Commit

Permalink
Implementações (#13)
Browse files Browse the repository at this point in the history
  • Loading branch information
glprog authored Jun 29, 2020
1 parent 32ca238 commit 6beb45b
Show file tree
Hide file tree
Showing 5 changed files with 187 additions and 21 deletions.
52 changes: 51 additions & 1 deletion MiniREST.SQL.Base.pas
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ TMiniRESTSQLConnectionBase = class;
TMiniRESTSQLConnectionFactoryBase = class abstract(TInterfacedObject, IMiniRESTSQLConnectionFactory)
strict private
FConnectionsToNotifyFree: TList;
private
FDatabaseType: TMiniRESTSQLDatabaseType;
FOnOpenQueryException: TMiniRESTOnOpenQueryException;
protected
FConnectionFactoryEventLogger: IMiniRESTSQLConnectionFactoryEventLogger;
{$IFNDEF FPC}
Expand All @@ -38,6 +41,7 @@ TMiniRESTSQLConnectionFactoryBase = class abstract(TInterfacedObject, IMiniRES
procedure ReleaseConnection(AConnection: IMiniRESTSQLConnection); virtual;
function InternalGetconnection: IMiniRESTSQLConnection; virtual; abstract;
procedure LogConnectionPoolEvent(const AMessage: string);
function GetOnOpenQueryException: TMiniRESTOnOpenQueryException;
public
constructor Create(const AConnectionCount: Integer); overload;
constructor Create(AParams: IMiniRESTSQLConnectionFactoryParams); overload;
Expand All @@ -49,6 +53,7 @@ TMiniRESTSQLConnectionFactoryBase = class abstract(TInterfacedObject, IMiniRES
function GetConnection(const AIdentifier: string): IMiniRESTSQLConnection; overload;
function GetSingletonConnection: IMiniRESTSQLConnection;
procedure InvalidateConnections;
function GetDatabaseType: TMiniRESTSQLDatabaseType;
end;

{ TMiniRESTSQLConnectionBase }
Expand All @@ -58,6 +63,7 @@ TMiniRESTSQLConnectionBase = class abstract(TInterfacedObject, IMiniRESTSQLCon
FOwner: TObject;
FConnectionID: Integer;
FValid: Boolean;
FDatabaseType: TMiniRESTSQLDatabaseType;
protected
FName: string;
FEstaNoPool: Boolean;
Expand All @@ -83,6 +89,7 @@ TMiniRESTSQLConnectionBase = class abstract(TInterfacedObject, IMiniRESTSQLCon
function GetConnectionID: Integer;
function IsValid: Boolean;
procedure Invalidate; virtual; abstract;
function GetDatabaseType: TMiniRESTSQLDatabaseType;
end;

TMiniRESTSQLPrimaryKeyInfo = class(TInterfacedObject, IMiniRESTSQLPrimaryKeyInfo)
Expand Down Expand Up @@ -138,6 +145,8 @@ TMiniRESTSQLConnectionFactoryParams = class(TInterfacedObject, IMiniRESTSQLCon
FConnectionCount: Integer;
FConnectionFactoryEventLogger: IMiniRESTSQLConnectionFactoryEventLogger;
FCharSet: string;
FDatabaseType: TMiniRESTSQLDatabaseType;
FOnOpenQueryException: TMiniRESTOnOpenQueryException;
public
function GetConnectionsCount: Integer;
procedure SetConnectionsCount(const ACount: Integer);
Expand All @@ -146,6 +155,10 @@ TMiniRESTSQLConnectionFactoryParams = class(TInterfacedObject, IMiniRESTSQLCon
procedure SetConnectionFactoryEventLogger(ALogger: IMiniRESTSQLConnectionFactoryEventLogger);
function GetCharSet: string;
procedure SetCharSet(const ACharSet: string);
function GetDatabaseType: TMiniRESTSQLDatabaseType;
procedure SetDatabseType(const ADatabaseType: TMiniRESTSQLDatabaseType);
function GetOnOpenQueryException: TMiniRESTOnOpenQueryException;
procedure SetOnOpenQueryException(AValue: TMiniRESTOnOpenQueryException);
end;

implementation
Expand Down Expand Up @@ -434,7 +447,7 @@ constructor TMiniRESTSQLConnectionBase.Create(AParams: IMiniRESTSQLConnectionPar
FOwner := nil;
FOwner := AParams.GetConnectionFactory.GetObject;
FConnectionID := AParams.GetConnectionID;

FDatabaseType := AParams.GetConnectionFactory.GetDatabaseType;
TMiniRESTSQLConnectionFactoryBase(FOwner).AddConnectionToNotifyFree(Self);
end;

Expand Down Expand Up @@ -470,6 +483,8 @@ constructor TMiniRESTSQLConnectionFactoryBase.Create(AParams: IMiniRESTSQLConnec
FCriticalSection := TCriticalSection.Create;
FConnectionsToNotifyFree := TList.Create;
FConnectionFactoryEventLogger := AParams.GetConnectionFactoryEventLogger;
FDatabaseType := AParams.GetDatabaseType;
FOnOpenQueryException := AParams.GetOnOpenQueryException;
end;

function TMiniRESTSQLConnectionFactoryParams.GetObject: TObject;
Expand Down Expand Up @@ -528,6 +543,41 @@ procedure TMiniRESTSQLConnectionFactoryParams.SetCharSet(const ACharSet: string)
FCharSet := ACharSet;
end;

function TMiniRESTSQLConnectionBase.GetDatabaseType: TMiniRESTSQLDatabaseType;
begin
Result := FDatabaseType;
end;

function TMiniRESTSQLConnectionFactoryBase.GetDatabaseType: TMiniRESTSQLDatabaseType;
begin
Result := FDatabaseType;
end;

function TMiniRESTSQLConnectionFactoryParams.GetDatabaseType: TMiniRESTSQLDatabaseType;
begin
Result := FDatabaseType;
end;

procedure TMiniRESTSQLConnectionFactoryParams.SetDatabseType(const ADatabaseType: TMiniRESTSQLDatabaseType);
begin
FDatabaseType := ADatabaseType;
end;

function TMiniRESTSQLConnectionFactoryParams.GetOnOpenQueryException: TMiniRESTOnOpenQueryException;
begin
Result := FOnOpenQueryException;
end;

procedure TMiniRESTSQLConnectionFactoryParams.SetOnOpenQueryException(AValue: TMiniRESTOnOpenQueryException);
begin
FOnOpenQueryException := AValue;
end;

function TMiniRESTSQLConnectionFactoryBase.GetOnOpenQueryException: TMiniRESTOnOpenQueryException;
begin
Result := FOnOpenQueryException;
end;

initialization
gConnectionIDCounter := 0;

Expand Down
11 changes: 10 additions & 1 deletion MiniREST.SQL.Intf.pas
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ interface

type
//TLoggerMethod = procedure (const ALog: string) of object;
IMiniRESTSQLQuery = interface;
IMiniRESTSQLConnection = interface;
TMiniRESTOnOpenQueryException = procedure (AConnection: IMiniRESTSQLConnection; AQuery: IMiniRESTSQLQuery; AException: Exception; var ARaiseException: Boolean) of object;

IMiniRESTSQLDatabaseInfo = interface;

Expand Down Expand Up @@ -57,6 +60,7 @@ interface
function GetDatabaseInfo: IMiniRESTSQLDatabaseInfo;
function GetConnectionID: Integer;
function IsValid: Boolean;
function GetDatabaseType: TMiniRESTSQLDatabaseType;
procedure Invalidate;
end;

Expand All @@ -70,6 +74,7 @@ interface
function GetConnectionsCount: Integer;
function GetQueueCount: Integer;
procedure InvalidateConnections;
function GetDatabaseType: TMiniRESTSQLDatabaseType;
property ConnectionsCount: Integer read GetConnectionsCount;
property QueueCount: Integer read GetQueueCount;
end;
Expand Down Expand Up @@ -141,7 +146,11 @@ interface
function GetConnectionsCount: Integer;
procedure SetConnectionsCount(const ACount: Integer);
function GetCharSet: string;
procedure SetCharSet(const ACharSet: string);
procedure SetCharSet(const ACharSet: string);
function GetDatabaseType: TMiniRESTSQLDatabaseType;
procedure SetDatabseType(const ADatabaseType: TMiniRESTSQLDatabaseType);
function GetOnOpenQueryException: TMiniRESTOnOpenQueryException;
procedure SetOnOpenQueryException(AValue: TMiniRESTOnOpenQueryException);
function GetObject: TObject;
end;

Expand Down
48 changes: 29 additions & 19 deletions MiniREST.SQL.SQLDb.pas
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,7 @@ interface
function GetUserName: string;
procedure SetUserName(const AUserName: string);
function GetPassword: string;
procedure SetPassword(const APassword: string);
function GetDatabaseType: TMiniRESTSQLDatabaseType;
procedure SetDatabseType(const ADatabaseType: TMiniRESTSQLDatabaseType);
procedure SetPassword(const APassword: string);
function GetDatabaseName: string;
procedure SetDatabaseName(const ADatabaseName: string);
function GetLogEvent: TLogEvent;
Expand Down Expand Up @@ -45,8 +43,6 @@ TMiniRESTSQLConnectionParamsSQLDb = class(TMiniRESTSQLConnectionFactoryParams,
procedure SetUserName(const AUserName: string);
function GetPassword: string;
procedure SetPassword(const APassword: string);
function GetDatabaseType: TMiniRESTSQLDatabaseType;
procedure SetDatabseType(const ADatabaseType: TMiniRESTSQLDatabaseType);
function GetDatabaseName: string;
procedure SetDatabaseName(const ADatabaseName: string);
function GetLogEvent: TLogEvent;
Expand All @@ -73,6 +69,7 @@ TMiniRESTSQLConnectionFactorySQLDb = class(TMiniRESTSQLConnectionFactoryBase)

TMiniRESTSQLConnectionSQLDb = class(TMiniRESTSQLConnectionBase)
private
FOnOpenQueryException: TMiniRESTOnOpenQueryException;
function GetConnectorType(const ADatabaseType: TMiniRESTSQLDatabaseType): String;
procedure OnBeforeConnect(Sender: TObject);
protected
Expand Down Expand Up @@ -115,6 +112,7 @@ TMiniRESTSQLQuerySQLDb = class(TInterfacedObject, IMiniRESTSQLQuery)
FSQL: string;
//FParams: TFPGInterfacedObjectList<string, IMiniRESTSQLParam>;
FParams: TFPGInterfacedObjectList<IMiniRESTSQLParam>;
FOnOpenQueryException: TMiniRESTOnOpenQueryException;
procedure BeforeOpenMiniRESTDataSet(DataSet: TDataSet);
public
constructor Create(AConnection: IMiniRESTSQLConnection);
Expand Down Expand Up @@ -186,19 +184,13 @@ procedure TMiniRESTSQLConnectionParamsSQLDb.SetPassword(const APassword: string)
FPassword := APassword;
end;

function TMiniRESTSQLConnectionParamsSQLDb.GetDatabaseType: TMiniRESTSQLDatabaseType;
begin
Result := FDatabaseType;
end;

procedure TMiniRESTSQLConnectionParamsSQLDb.SetDatabseType(const ADatabaseType: TMiniRESTSQLDatabaseType);
begin
FDatabaseType := ADatabaseType;
end;

function TMiniRESTSQLConnectionFactorySQLDb.InternalGetconnection: IMiniRESTSQLConnection;
begin
Result := TMiniRESTSQLConnectionSQLDb.Create(Self, FConnectionParams);
var
LConn: TMiniRESTSQLConnectionSQLDb;
begin
LConn := TMiniRESTSQLConnectionSQLDb.Create(Self, FConnectionParams);
LConn.FOnOpenQueryException := GetOnOpenQueryException;
Result := LConn;
end;

constructor TMiniRESTSQLConnectionFactorySQLDb.Create(AParams: IMiniRESTSQLConnectionFactoryParamsSQLDb);
Expand Down Expand Up @@ -266,9 +258,13 @@ procedure TMiniRESTSQLConnectionSQLDb.Rollback;
end;

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

function TMiniRESTSQLConnectionSQLDb.GetQuery(const ASQL: string; AParams: array of IMiniRESTSQLParam): IMiniRESTSQLQuery;
Expand Down Expand Up @@ -354,8 +350,22 @@ procedure TMiniRESTSQLConnectionSQLDb.Log(Sender: TSQLConnection;
end;

procedure TMiniRESTSQLQuerySQLDb.Open;
var
LRaiseException: Boolean;
begin
FQry.Open;
LRaiseException := True;
try
FQry.Open;
except
on E: Exception do
begin
if not Assigned(FOnOpenQueryException) then
raise;
FOnOpenQueryException(FConnection, Self, E, LRaiseException);
if LRaiseException then
raise;
end;
end;
end;

procedure TMiniRESTSQLQuerySQLDb.Close;
Expand Down
97 changes: 97 additions & 0 deletions unittest/SQL/Test.SQL.Default.pas
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ TMiniRESTSQLTest = class({$IFNDEF FPC}TObject{$ELSE}TTestCase{$IFEND})
FServerHostName: string;
FServerPort: Integer;
procedure MethodThatRaiseException;
procedure MethodThatRaiseExceptionOnOpenQuery;
procedure OnOpenQueryExceptionRaiseException(AConnection: IMiniRESTSQLConnection; AQuery: IMiniRESTSQLQuery; AException: Exception; var ARaiseException: Boolean);
procedure OnOpenQueryExceptionNoRaiseException(AConnection: IMiniRESTSQLConnection; AQuery: IMiniRESTSQLQuery; AException: Exception; var ARaiseException: Boolean);
protected
FConnectionCount: Integer;
FConnectionFactory: IMiniRESTSQLConnectionFactory;
Expand Down Expand Up @@ -137,6 +140,22 @@ TMiniRESTSQLTest = class({$IFNDEF FPC}TObject{$ELSE}TTestCase{$IFEND})
[Test]
{$IFEND}
procedure TestCharSet;
{$IFNDEF FPC}
[Test]
{$IFEND}
procedure TestGetDatabaseTypeFromConnection;
{$IFNDEF FPC}
[Test]
{$IFEND}
procedure TestGetDatabaseTypeFromConnectionFactory;
{$IFNDEF FPC}
[Test]
{$IFEND}
procedure TestOnOpenQueryException1;
{$IFNDEF FPC}
[Test]
{$IFEND}
procedure TestOnOpenQueryException2;
(* {$IFNDEF FPC}
[Test]
{$IFEND}
Expand Down Expand Up @@ -995,4 +1014,82 @@ procedure TMiniRESTSQLTest.TestCharSetPostgreSQL;
{$IFEND}
end;

procedure TMiniRESTSQLTest.TestGetDatabaseTypeFromConnection;
var
LConn: IMiniRESTSQLConnection;
begin
LConn := FConnectionFactory.GetConnection;
{$IFNDEF FPC}
Assert.AreTrue(LConn.GetDatabaseType = GetDatabaseType);
{$ELSE}
CheckTrue(LConn.GetDatabaseType = GetDatabaseType);
{$IFEND}
end;

procedure TMiniRESTSQLTest.TestGetDatabaseTypeFromConnectionFactory;
begin
{$IFNDEF FPC}
Assert.AreTrue(FConnectionFactory.GetDatabaseType = GetDatabaseType);
{$ELSE}
CheckTrue(FConnectionFactory.GetDatabaseType = GetDatabaseType);
{$IFEND}
end;

procedure TMiniRESTSQLTest.TestOnOpenQueryException1;
begin
// Deve lançar exceção
CheckException(@MethodThatRaiseExceptionOnOpenQuery, Exception);
end;

procedure TMiniRESTSQLTest.TestOnOpenQueryException2;
var
LConnectionFactory: IMiniRESTSQLConnectionFactory;
LConnectionFactoryParams: IMiniRESTSQLConnectionFactoryParams;
LQry: IMiniRESTSQLQuery;
LConn: IMiniRESTSQLConnection;
begin
// Não deve lançar exceção
LConnectionFactoryParams := GetConnectionFactoryParams;
LConnectionFactoryParams.SetOnOpenQueryException(@OnOpenQueryExceptionNoRaiseException);
LConnectionFactory := GetConnectionFactory(LConnectionFactoryParams);
LConn := LConnectionFactory.GetConnection;
LQry := LConn.GetQuery('HUE');
LQry.Open;
CheckTrue(True); // é assim mesmo, só pra ver se chegou aqui
end;

procedure TMiniRESTSQLTest.OnOpenQueryExceptionRaiseException(AConnection: IMiniRESTSQLConnection;
AQuery: IMiniRESTSQLQuery; AException: Exception; var ARaiseException: Boolean);
begin
CheckTrue(ARaiseException);
CheckTrue(AConnection <> nil);
CheckTrue(AQuery <> nil);
CheckTrue(AException <> nil);
ARaiseException := True;
end;

procedure TMiniRESTSQLTest.OnOpenQueryExceptionNoRaiseException(AConnection: IMiniRESTSQLConnection; AQuery: IMiniRESTSQLQuery; AException: Exception; var ARaiseException: Boolean);
begin
CheckTrue(ARaiseException);
CheckTrue(AConnection <> nil);
CheckTrue(AQuery <> nil);
CheckTrue(AException <> nil);
ARaiseException := False;
end;

procedure TMiniRESTSQLTest.MethodThatRaiseExceptionOnOpenQuery;
var
LConnectionFactory: IMiniRESTSQLConnectionFactory;
LConnectionFactoryParams: IMiniRESTSQLConnectionFactoryParams;
LQry: IMiniRESTSQLQuery;
LConn: IMiniRESTSQLConnection;
begin
LConnectionFactoryParams := GetConnectionFactoryParams;
LConnectionFactoryParams.SetOnOpenQueryException(@OnOpenQueryExceptionRaiseException);
LConnectionFactory := GetConnectionFactory(LConnectionFactoryParams);
LConn := LConnectionFactory.GetConnection;
LQry := LConn.GetQuery('HUE');
LQry.Open;
end;

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

0 comments on commit 6beb45b

Please sign in to comment.