Skip to content

Commit 6beb45b

Browse files
authored
Implementações (#13)
1 parent 32ca238 commit 6beb45b

File tree

5 files changed

+187
-21
lines changed

5 files changed

+187
-21
lines changed

MiniREST.SQL.Base.pas

Lines changed: 51 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,9 @@ TMiniRESTSQLConnectionBase = class;
1717
TMiniRESTSQLConnectionFactoryBase = class abstract(TInterfacedObject, IMiniRESTSQLConnectionFactory)
1818
strict private
1919
FConnectionsToNotifyFree: TList;
20+
private
21+
FDatabaseType: TMiniRESTSQLDatabaseType;
22+
FOnOpenQueryException: TMiniRESTOnOpenQueryException;
2023
protected
2124
FConnectionFactoryEventLogger: IMiniRESTSQLConnectionFactoryEventLogger;
2225
{$IFNDEF FPC}
@@ -38,6 +41,7 @@ TMiniRESTSQLConnectionFactoryBase = class abstract(TInterfacedObject, IMiniRES
3841
procedure ReleaseConnection(AConnection: IMiniRESTSQLConnection); virtual;
3942
function InternalGetconnection: IMiniRESTSQLConnection; virtual; abstract;
4043
procedure LogConnectionPoolEvent(const AMessage: string);
44+
function GetOnOpenQueryException: TMiniRESTOnOpenQueryException;
4145
public
4246
constructor Create(const AConnectionCount: Integer); overload;
4347
constructor Create(AParams: IMiniRESTSQLConnectionFactoryParams); overload;
@@ -49,6 +53,7 @@ TMiniRESTSQLConnectionFactoryBase = class abstract(TInterfacedObject, IMiniRES
4953
function GetConnection(const AIdentifier: string): IMiniRESTSQLConnection; overload;
5054
function GetSingletonConnection: IMiniRESTSQLConnection;
5155
procedure InvalidateConnections;
56+
function GetDatabaseType: TMiniRESTSQLDatabaseType;
5257
end;
5358

5459
{ TMiniRESTSQLConnectionBase }
@@ -58,6 +63,7 @@ TMiniRESTSQLConnectionBase = class abstract(TInterfacedObject, IMiniRESTSQLCon
5863
FOwner: TObject;
5964
FConnectionID: Integer;
6065
FValid: Boolean;
66+
FDatabaseType: TMiniRESTSQLDatabaseType;
6167
protected
6268
FName: string;
6369
FEstaNoPool: Boolean;
@@ -83,6 +89,7 @@ TMiniRESTSQLConnectionBase = class abstract(TInterfacedObject, IMiniRESTSQLCon
8389
function GetConnectionID: Integer;
8490
function IsValid: Boolean;
8591
procedure Invalidate; virtual; abstract;
92+
function GetDatabaseType: TMiniRESTSQLDatabaseType;
8693
end;
8794

8895
TMiniRESTSQLPrimaryKeyInfo = class(TInterfacedObject, IMiniRESTSQLPrimaryKeyInfo)
@@ -138,6 +145,8 @@ TMiniRESTSQLConnectionFactoryParams = class(TInterfacedObject, IMiniRESTSQLCon
138145
FConnectionCount: Integer;
139146
FConnectionFactoryEventLogger: IMiniRESTSQLConnectionFactoryEventLogger;
140147
FCharSet: string;
148+
FDatabaseType: TMiniRESTSQLDatabaseType;
149+
FOnOpenQueryException: TMiniRESTOnOpenQueryException;
141150
public
142151
function GetConnectionsCount: Integer;
143152
procedure SetConnectionsCount(const ACount: Integer);
@@ -146,6 +155,10 @@ TMiniRESTSQLConnectionFactoryParams = class(TInterfacedObject, IMiniRESTSQLCon
146155
procedure SetConnectionFactoryEventLogger(ALogger: IMiniRESTSQLConnectionFactoryEventLogger);
147156
function GetCharSet: string;
148157
procedure SetCharSet(const ACharSet: string);
158+
function GetDatabaseType: TMiniRESTSQLDatabaseType;
159+
procedure SetDatabseType(const ADatabaseType: TMiniRESTSQLDatabaseType);
160+
function GetOnOpenQueryException: TMiniRESTOnOpenQueryException;
161+
procedure SetOnOpenQueryException(AValue: TMiniRESTOnOpenQueryException);
149162
end;
150163

151164
implementation
@@ -434,7 +447,7 @@ constructor TMiniRESTSQLConnectionBase.Create(AParams: IMiniRESTSQLConnectionPar
434447
FOwner := nil;
435448
FOwner := AParams.GetConnectionFactory.GetObject;
436449
FConnectionID := AParams.GetConnectionID;
437-
450+
FDatabaseType := AParams.GetConnectionFactory.GetDatabaseType;
438451
TMiniRESTSQLConnectionFactoryBase(FOwner).AddConnectionToNotifyFree(Self);
439452
end;
440453

@@ -470,6 +483,8 @@ constructor TMiniRESTSQLConnectionFactoryBase.Create(AParams: IMiniRESTSQLConnec
470483
FCriticalSection := TCriticalSection.Create;
471484
FConnectionsToNotifyFree := TList.Create;
472485
FConnectionFactoryEventLogger := AParams.GetConnectionFactoryEventLogger;
486+
FDatabaseType := AParams.GetDatabaseType;
487+
FOnOpenQueryException := AParams.GetOnOpenQueryException;
473488
end;
474489

475490
function TMiniRESTSQLConnectionFactoryParams.GetObject: TObject;
@@ -528,6 +543,41 @@ procedure TMiniRESTSQLConnectionFactoryParams.SetCharSet(const ACharSet: string)
528543
FCharSet := ACharSet;
529544
end;
530545

546+
function TMiniRESTSQLConnectionBase.GetDatabaseType: TMiniRESTSQLDatabaseType;
547+
begin
548+
Result := FDatabaseType;
549+
end;
550+
551+
function TMiniRESTSQLConnectionFactoryBase.GetDatabaseType: TMiniRESTSQLDatabaseType;
552+
begin
553+
Result := FDatabaseType;
554+
end;
555+
556+
function TMiniRESTSQLConnectionFactoryParams.GetDatabaseType: TMiniRESTSQLDatabaseType;
557+
begin
558+
Result := FDatabaseType;
559+
end;
560+
561+
procedure TMiniRESTSQLConnectionFactoryParams.SetDatabseType(const ADatabaseType: TMiniRESTSQLDatabaseType);
562+
begin
563+
FDatabaseType := ADatabaseType;
564+
end;
565+
566+
function TMiniRESTSQLConnectionFactoryParams.GetOnOpenQueryException: TMiniRESTOnOpenQueryException;
567+
begin
568+
Result := FOnOpenQueryException;
569+
end;
570+
571+
procedure TMiniRESTSQLConnectionFactoryParams.SetOnOpenQueryException(AValue: TMiniRESTOnOpenQueryException);
572+
begin
573+
FOnOpenQueryException := AValue;
574+
end;
575+
576+
function TMiniRESTSQLConnectionFactoryBase.GetOnOpenQueryException: TMiniRESTOnOpenQueryException;
577+
begin
578+
Result := FOnOpenQueryException;
579+
end;
580+
531581
initialization
532582
gConnectionIDCounter := 0;
533583

MiniREST.SQL.Intf.pas

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@ interface
99

1010
type
1111
//TLoggerMethod = procedure (const ALog: string) of object;
12+
IMiniRESTSQLQuery = interface;
13+
IMiniRESTSQLConnection = interface;
14+
TMiniRESTOnOpenQueryException = procedure (AConnection: IMiniRESTSQLConnection; AQuery: IMiniRESTSQLQuery; AException: Exception; var ARaiseException: Boolean) of object;
1215

1316
IMiniRESTSQLDatabaseInfo = interface;
1417

@@ -57,6 +60,7 @@ interface
5760
function GetDatabaseInfo: IMiniRESTSQLDatabaseInfo;
5861
function GetConnectionID: Integer;
5962
function IsValid: Boolean;
63+
function GetDatabaseType: TMiniRESTSQLDatabaseType;
6064
procedure Invalidate;
6165
end;
6266

@@ -70,6 +74,7 @@ interface
7074
function GetConnectionsCount: Integer;
7175
function GetQueueCount: Integer;
7276
procedure InvalidateConnections;
77+
function GetDatabaseType: TMiniRESTSQLDatabaseType;
7378
property ConnectionsCount: Integer read GetConnectionsCount;
7479
property QueueCount: Integer read GetQueueCount;
7580
end;
@@ -141,7 +146,11 @@ interface
141146
function GetConnectionsCount: Integer;
142147
procedure SetConnectionsCount(const ACount: Integer);
143148
function GetCharSet: string;
144-
procedure SetCharSet(const ACharSet: string);
149+
procedure SetCharSet(const ACharSet: string);
150+
function GetDatabaseType: TMiniRESTSQLDatabaseType;
151+
procedure SetDatabseType(const ADatabaseType: TMiniRESTSQLDatabaseType);
152+
function GetOnOpenQueryException: TMiniRESTOnOpenQueryException;
153+
procedure SetOnOpenQueryException(AValue: TMiniRESTOnOpenQueryException);
145154
function GetObject: TObject;
146155
end;
147156

MiniREST.SQL.SQLDb.pas

Lines changed: 29 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,7 @@ interface
1515
function GetUserName: string;
1616
procedure SetUserName(const AUserName: string);
1717
function GetPassword: string;
18-
procedure SetPassword(const APassword: string);
19-
function GetDatabaseType: TMiniRESTSQLDatabaseType;
20-
procedure SetDatabseType(const ADatabaseType: TMiniRESTSQLDatabaseType);
18+
procedure SetPassword(const APassword: string);
2119
function GetDatabaseName: string;
2220
procedure SetDatabaseName(const ADatabaseName: string);
2321
function GetLogEvent: TLogEvent;
@@ -45,8 +43,6 @@ TMiniRESTSQLConnectionParamsSQLDb = class(TMiniRESTSQLConnectionFactoryParams,
4543
procedure SetUserName(const AUserName: string);
4644
function GetPassword: string;
4745
procedure SetPassword(const APassword: string);
48-
function GetDatabaseType: TMiniRESTSQLDatabaseType;
49-
procedure SetDatabseType(const ADatabaseType: TMiniRESTSQLDatabaseType);
5046
function GetDatabaseName: string;
5147
procedure SetDatabaseName(const ADatabaseName: string);
5248
function GetLogEvent: TLogEvent;
@@ -73,6 +69,7 @@ TMiniRESTSQLConnectionFactorySQLDb = class(TMiniRESTSQLConnectionFactoryBase)
7369

7470
TMiniRESTSQLConnectionSQLDb = class(TMiniRESTSQLConnectionBase)
7571
private
72+
FOnOpenQueryException: TMiniRESTOnOpenQueryException;
7673
function GetConnectorType(const ADatabaseType: TMiniRESTSQLDatabaseType): String;
7774
procedure OnBeforeConnect(Sender: TObject);
7875
protected
@@ -115,6 +112,7 @@ TMiniRESTSQLQuerySQLDb = class(TInterfacedObject, IMiniRESTSQLQuery)
115112
FSQL: string;
116113
//FParams: TFPGInterfacedObjectList<string, IMiniRESTSQLParam>;
117114
FParams: TFPGInterfacedObjectList<IMiniRESTSQLParam>;
115+
FOnOpenQueryException: TMiniRESTOnOpenQueryException;
118116
procedure BeforeOpenMiniRESTDataSet(DataSet: TDataSet);
119117
public
120118
constructor Create(AConnection: IMiniRESTSQLConnection);
@@ -186,19 +184,13 @@ procedure TMiniRESTSQLConnectionParamsSQLDb.SetPassword(const APassword: string)
186184
FPassword := APassword;
187185
end;
188186

189-
function TMiniRESTSQLConnectionParamsSQLDb.GetDatabaseType: TMiniRESTSQLDatabaseType;
190-
begin
191-
Result := FDatabaseType;
192-
end;
193-
194-
procedure TMiniRESTSQLConnectionParamsSQLDb.SetDatabseType(const ADatabaseType: TMiniRESTSQLDatabaseType);
195-
begin
196-
FDatabaseType := ADatabaseType;
197-
end;
198-
199187
function TMiniRESTSQLConnectionFactorySQLDb.InternalGetconnection: IMiniRESTSQLConnection;
200-
begin
201-
Result := TMiniRESTSQLConnectionSQLDb.Create(Self, FConnectionParams);
188+
var
189+
LConn: TMiniRESTSQLConnectionSQLDb;
190+
begin
191+
LConn := TMiniRESTSQLConnectionSQLDb.Create(Self, FConnectionParams);
192+
LConn.FOnOpenQueryException := GetOnOpenQueryException;
193+
Result := LConn;
202194
end;
203195

204196
constructor TMiniRESTSQLConnectionFactorySQLDb.Create(AParams: IMiniRESTSQLConnectionFactoryParamsSQLDb);
@@ -266,9 +258,13 @@ procedure TMiniRESTSQLConnectionSQLDb.Rollback;
266258
end;
267259

268260
function TMiniRESTSQLConnectionSQLDb.GetQuery: IMiniRESTSQLQuery;
261+
var
262+
LQry: TMiniRESTSQLQuerySQLDb;
269263
begin
270264
CheckConnectionIsValid;
271-
Result := TMiniRESTSQLQuerySQLDb.Create(Self);
265+
LQry := TMiniRESTSQLQuerySQLDb.Create(Self);
266+
LQry.FOnOpenQueryException := FOnOpenQueryException;
267+
Result := LQry;
272268
end;
273269

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

356352
procedure TMiniRESTSQLQuerySQLDb.Open;
353+
var
354+
LRaiseException: Boolean;
357355
begin
358-
FQry.Open;
356+
LRaiseException := True;
357+
try
358+
FQry.Open;
359+
except
360+
on E: Exception do
361+
begin
362+
if not Assigned(FOnOpenQueryException) then
363+
raise;
364+
FOnOpenQueryException(FConnection, Self, E, LRaiseException);
365+
if LRaiseException then
366+
raise;
367+
end;
368+
end;
359369
end;
360370

361371
procedure TMiniRESTSQLQuerySQLDb.Close;

unittest/SQL/Test.SQL.Default.pas

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@ TMiniRESTSQLTest = class({$IFNDEF FPC}TObject{$ELSE}TTestCase{$IFEND})
1212
FServerHostName: string;
1313
FServerPort: Integer;
1414
procedure MethodThatRaiseException;
15+
procedure MethodThatRaiseExceptionOnOpenQuery;
16+
procedure OnOpenQueryExceptionRaiseException(AConnection: IMiniRESTSQLConnection; AQuery: IMiniRESTSQLQuery; AException: Exception; var ARaiseException: Boolean);
17+
procedure OnOpenQueryExceptionNoRaiseException(AConnection: IMiniRESTSQLConnection; AQuery: IMiniRESTSQLQuery; AException: Exception; var ARaiseException: Boolean);
1518
protected
1619
FConnectionCount: Integer;
1720
FConnectionFactory: IMiniRESTSQLConnectionFactory;
@@ -137,6 +140,22 @@ TMiniRESTSQLTest = class({$IFNDEF FPC}TObject{$ELSE}TTestCase{$IFEND})
137140
[Test]
138141
{$IFEND}
139142
procedure TestCharSet;
143+
{$IFNDEF FPC}
144+
[Test]
145+
{$IFEND}
146+
procedure TestGetDatabaseTypeFromConnection;
147+
{$IFNDEF FPC}
148+
[Test]
149+
{$IFEND}
150+
procedure TestGetDatabaseTypeFromConnectionFactory;
151+
{$IFNDEF FPC}
152+
[Test]
153+
{$IFEND}
154+
procedure TestOnOpenQueryException1;
155+
{$IFNDEF FPC}
156+
[Test]
157+
{$IFEND}
158+
procedure TestOnOpenQueryException2;
140159
(* {$IFNDEF FPC}
141160
[Test]
142161
{$IFEND}
@@ -995,4 +1014,82 @@ procedure TMiniRESTSQLTest.TestCharSetPostgreSQL;
9951014
{$IFEND}
9961015
end;
9971016

1017+
procedure TMiniRESTSQLTest.TestGetDatabaseTypeFromConnection;
1018+
var
1019+
LConn: IMiniRESTSQLConnection;
1020+
begin
1021+
LConn := FConnectionFactory.GetConnection;
1022+
{$IFNDEF FPC}
1023+
Assert.AreTrue(LConn.GetDatabaseType = GetDatabaseType);
1024+
{$ELSE}
1025+
CheckTrue(LConn.GetDatabaseType = GetDatabaseType);
1026+
{$IFEND}
1027+
end;
1028+
1029+
procedure TMiniRESTSQLTest.TestGetDatabaseTypeFromConnectionFactory;
1030+
begin
1031+
{$IFNDEF FPC}
1032+
Assert.AreTrue(FConnectionFactory.GetDatabaseType = GetDatabaseType);
1033+
{$ELSE}
1034+
CheckTrue(FConnectionFactory.GetDatabaseType = GetDatabaseType);
1035+
{$IFEND}
1036+
end;
1037+
1038+
procedure TMiniRESTSQLTest.TestOnOpenQueryException1;
1039+
begin
1040+
// Deve lançar exceção
1041+
CheckException(@MethodThatRaiseExceptionOnOpenQuery, Exception);
1042+
end;
1043+
1044+
procedure TMiniRESTSQLTest.TestOnOpenQueryException2;
1045+
var
1046+
LConnectionFactory: IMiniRESTSQLConnectionFactory;
1047+
LConnectionFactoryParams: IMiniRESTSQLConnectionFactoryParams;
1048+
LQry: IMiniRESTSQLQuery;
1049+
LConn: IMiniRESTSQLConnection;
1050+
begin
1051+
// Não deve lançar exceção
1052+
LConnectionFactoryParams := GetConnectionFactoryParams;
1053+
LConnectionFactoryParams.SetOnOpenQueryException(@OnOpenQueryExceptionNoRaiseException);
1054+
LConnectionFactory := GetConnectionFactory(LConnectionFactoryParams);
1055+
LConn := LConnectionFactory.GetConnection;
1056+
LQry := LConn.GetQuery('HUE');
1057+
LQry.Open;
1058+
CheckTrue(True); // é assim mesmo, só pra ver se chegou aqui
1059+
end;
1060+
1061+
procedure TMiniRESTSQLTest.OnOpenQueryExceptionRaiseException(AConnection: IMiniRESTSQLConnection;
1062+
AQuery: IMiniRESTSQLQuery; AException: Exception; var ARaiseException: Boolean);
1063+
begin
1064+
CheckTrue(ARaiseException);
1065+
CheckTrue(AConnection <> nil);
1066+
CheckTrue(AQuery <> nil);
1067+
CheckTrue(AException <> nil);
1068+
ARaiseException := True;
1069+
end;
1070+
1071+
procedure TMiniRESTSQLTest.OnOpenQueryExceptionNoRaiseException(AConnection: IMiniRESTSQLConnection; AQuery: IMiniRESTSQLQuery; AException: Exception; var ARaiseException: Boolean);
1072+
begin
1073+
CheckTrue(ARaiseException);
1074+
CheckTrue(AConnection <> nil);
1075+
CheckTrue(AQuery <> nil);
1076+
CheckTrue(AException <> nil);
1077+
ARaiseException := False;
1078+
end;
1079+
1080+
procedure TMiniRESTSQLTest.MethodThatRaiseExceptionOnOpenQuery;
1081+
var
1082+
LConnectionFactory: IMiniRESTSQLConnectionFactory;
1083+
LConnectionFactoryParams: IMiniRESTSQLConnectionFactoryParams;
1084+
LQry: IMiniRESTSQLQuery;
1085+
LConn: IMiniRESTSQLConnection;
1086+
begin
1087+
LConnectionFactoryParams := GetConnectionFactoryParams;
1088+
LConnectionFactoryParams.SetOnOpenQueryException(@OnOpenQueryExceptionRaiseException);
1089+
LConnectionFactory := GetConnectionFactory(LConnectionFactoryParams);
1090+
LConn := LConnectionFactory.GetConnection;
1091+
LQry := LConn.GetQuery('HUE');
1092+
LQry.Open;
1093+
end;
1094+
9981095
end.

unittest/TEST.FDB

0 Bytes
Binary file not shown.

0 commit comments

Comments
 (0)