From 6beb45b137f5d273d57883512aa193d79002e290 Mon Sep 17 00:00:00 2001 From: Glaucos Ginez Date: Mon, 29 Jun 2020 13:10:31 -0300 Subject: [PATCH] =?UTF-8?q?Implementa=C3=A7=C3=B5es=20(#13)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- MiniREST.SQL.Base.pas | 52 +++++++++++++++- MiniREST.SQL.Intf.pas | 11 +++- MiniREST.SQL.SQLDb.pas | 48 +++++++++------ unittest/SQL/Test.SQL.Default.pas | 97 ++++++++++++++++++++++++++++++ unittest/TEST.FDB | Bin 1589248 -> 1589248 bytes 5 files changed, 187 insertions(+), 21 deletions(-) diff --git a/MiniREST.SQL.Base.pas b/MiniREST.SQL.Base.pas index a4c3d36..db7a495 100644 --- a/MiniREST.SQL.Base.pas +++ b/MiniREST.SQL.Base.pas @@ -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} @@ -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; @@ -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 } @@ -58,6 +63,7 @@ TMiniRESTSQLConnectionBase = class abstract(TInterfacedObject, IMiniRESTSQLCon FOwner: TObject; FConnectionID: Integer; FValid: Boolean; + FDatabaseType: TMiniRESTSQLDatabaseType; protected FName: string; FEstaNoPool: Boolean; @@ -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) @@ -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); @@ -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 @@ -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; @@ -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; @@ -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; diff --git a/MiniREST.SQL.Intf.pas b/MiniREST.SQL.Intf.pas index 543e9af..665f2b7 100644 --- a/MiniREST.SQL.Intf.pas +++ b/MiniREST.SQL.Intf.pas @@ -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; @@ -57,6 +60,7 @@ interface function GetDatabaseInfo: IMiniRESTSQLDatabaseInfo; function GetConnectionID: Integer; function IsValid: Boolean; + function GetDatabaseType: TMiniRESTSQLDatabaseType; procedure Invalidate; end; @@ -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; @@ -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; diff --git a/MiniREST.SQL.SQLDb.pas b/MiniREST.SQL.SQLDb.pas index 739bafe..e5f7cb0 100644 --- a/MiniREST.SQL.SQLDb.pas +++ b/MiniREST.SQL.SQLDb.pas @@ -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; @@ -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; @@ -73,6 +69,7 @@ TMiniRESTSQLConnectionFactorySQLDb = class(TMiniRESTSQLConnectionFactoryBase) TMiniRESTSQLConnectionSQLDb = class(TMiniRESTSQLConnectionBase) private + FOnOpenQueryException: TMiniRESTOnOpenQueryException; function GetConnectorType(const ADatabaseType: TMiniRESTSQLDatabaseType): String; procedure OnBeforeConnect(Sender: TObject); protected @@ -115,6 +112,7 @@ TMiniRESTSQLQuerySQLDb = class(TInterfacedObject, IMiniRESTSQLQuery) FSQL: string; //FParams: TFPGInterfacedObjectList; FParams: TFPGInterfacedObjectList; + FOnOpenQueryException: TMiniRESTOnOpenQueryException; procedure BeforeOpenMiniRESTDataSet(DataSet: TDataSet); public constructor Create(AConnection: IMiniRESTSQLConnection); @@ -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); @@ -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; @@ -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; diff --git a/unittest/SQL/Test.SQL.Default.pas b/unittest/SQL/Test.SQL.Default.pas index ea457a5..60af327 100644 --- a/unittest/SQL/Test.SQL.Default.pas +++ b/unittest/SQL/Test.SQL.Default.pas @@ -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; @@ -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} @@ -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. diff --git a/unittest/TEST.FDB b/unittest/TEST.FDB index 2d2b8c70dd9ae21f7b0bb2c99dea6c9b7ce0ac51..f1b47ec763fa25d3bb253934e2d745cac8d79f9a 100644 GIT binary patch delta 1960 zcmb`Gzi-n(6vyA)IZkXR@g+@aS}GI-Lxm#22K@slY}5l24DF6o{s5%2gyc+>;x1Xs zf*3$@6bVZ#lqFp$6Y7XMF;qpWL@5dIOnzDM!O@7bzLuUo-@EtT`PuU}Ja6M3ft4Gp zuNaIMx4cpG=@Em>vw;98Sv~~#a6rhF<$;?6#b^C@K=6&;PU&&m-sW22#nQm@1VJ}& zFFvjFMx7HBMKP3y%Aj;q7L|+Y-1xZ3=p7ok3$2=Gtg*#C+IF|>*G)q^rtdBVZY5gu z%+J?Zf0YDowY3zQj?iztuK`Od$98J72)DgF$38JrlD{Q5UjbsaF8^Br<@7bTE^E$% zsHi#nq;b?Sqoa;>@J94@7<6$eEFch%*eEuqVq+@KO~u)%SRck`c>Z8fVC1w9296JA zx_kfjK2Tf0mlc4tX(4I)gXh9OcpQc2T3pe*!W4U|d(rV`ek~uYYvOR+6A(svNGqcg zcH>^td``j33SLTbAxXfK1_`$nyr|%X1Q)Un^9h5DTMBL}xG~|FU(#W2VvzEzg6j&N z8S^;5qyTMfkaMQsRKdxJi(N^A9waUxJ%B&E!=FyJ7YF!KR76o&77ZX4T4d~uVSG%dJLpoB>6NY%N5je6+B(l8^Hg$?N}Avvm4P{IO%K2p^k`&p(;`b29S8hacU)JJr;V_f6}k-_r3Q{=Xp)fYu=}1eXab8 zk*)g&nek(&N7QlB{WEQxX21NFR7t(+=dBxo&)qW5bRr`px zP6m2-GBA!miZ}3-4&hZ8gp%lqC0-x%+L+Ic`I#|4J@l{P{CFvZ(VxGG2G0j7-P_(g zB8n-GPjUSJ?dx$2}PNkz3_B5zm*sJqk#xz^cSET$?pfzpp*PM z!j}<#EzO0bz}0j>!j};4ApA;-3)zI1Qvn%YMEC;2Yv&y5OD3E@4@h|x;g=9@k9bsH z(!ks(AmJi?8bZ)|5Zpd$xpz1Eq? wg9@M`$OLgvDLfMvcM44_y4v9iOKszm+IQ=QA6R}c>jz~&sQAI$+rV!B0XIceLjV8(