Skip to content

Commit

Permalink
Implementação
Browse files Browse the repository at this point in the history
  • Loading branch information
glprog committed Mar 6, 2019
1 parent c448ff2 commit 3b24541
Show file tree
Hide file tree
Showing 9 changed files with 324 additions and 120 deletions.
4 changes: 3 additions & 1 deletion MiniREST.SQL.Base.pas
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ TMiniRESTSQLConnectionFactoryBase = class abstract(TInterfacedObject, IMiniRES
function GetObject: TObject;
end;

{ TMiniRESTSQLConnectionBase }

TMiniRESTSQLConnectionBase = class abstract(TInterfacedObject, IMiniRESTSQLConnection)
strict private
FOwner : TObject;
Expand Down Expand Up @@ -246,7 +248,7 @@ procedure TMiniRESTSQLConnectionBase.SetOwner(AOwner: Pointer);
FOwner := AOwner;
end;

function TMiniRESTSQLConnectionBase._Release: Integer;
function TMiniRESTSQLConnectionBase._Release: Integer; stdcall;
begin
if (FRefCount = 1) and (FOwner <> nil) and (not FEstaNoPool) then
TMiniRESTSQLConnectionFactoryBase(FOwner).ReleaseConnection(Self);
Expand Down
44 changes: 37 additions & 7 deletions MiniREST.SQL.SQLDb.pas
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ interface
sqldb, IBConnection;

type
TLogEvent = procedure (Sender : TSQLConnection; EventType : TDBEventType; Const Msg : String);
IMiniRESTSQLConnectionParamsSQLDb = interface
['{F2FB358A-6369-4FCE-AA9B-75FFECA18E88}']
function GetConnectionsCount: Integer;
Expand All @@ -20,6 +21,8 @@ interface
function SetDatabseType(const ADatabaseType: TMiniRESTSQLDatabaseType): IMiniRESTSQLConnectionParamsSQLDb;
function GetDatabaseName: string;
function SetDatabaseName(const ADatabaseName: string): IMiniRESTSQLConnectionParamsSQLDb;
function GetLogEvent: TLogEvent;
function SetLogEvent(const ALogEvent: TLogEvent): IMiniRESTSQLConnectionParamsSQLDb;
end;

TMiniRESTSQLConnectionParamsSQLDb = class(TInterfacedObject, IMiniRESTSQLConnectionParamsSQLDb)
Expand All @@ -30,6 +33,7 @@ TMiniRESTSQLConnectionParamsSQLDb = class(TInterfacedObject, IMiniRESTSQLConne
FPassword: string;
FDatabaseType: TMiniRESTSQLDatabaseType;
FDatabaseName: string;
FLogEvent: TLogEvent;
public
class function New: IMiniRESTSQLConnectionParamsSQLDb;
function GetConnectionsCount: Integer;
Expand All @@ -44,6 +48,8 @@ TMiniRESTSQLConnectionParamsSQLDb = class(TInterfacedObject, IMiniRESTSQLConne
function SetDatabseType(const ADatabaseType: TMiniRESTSQLDatabaseType): IMiniRESTSQLConnectionParamsSQLDb;
function GetDatabaseName: string;
function SetDatabaseName(const ADatabaseName: string): IMiniRESTSQLConnectionParamsSQLDb;
function GetLogEvent: TLogEvent;
function SetLogEvent(const ALogEvent: TLogEvent): IMiniRESTSQLConnectionParamsSQLDb;
end;

TMiniRESTSQLConnectionFactorySQLDb = class(TMiniRESTSQLConnectionFactoryBase)
Expand All @@ -56,6 +62,8 @@ TMiniRESTSQLConnectionFactorySQLDb = class(TMiniRESTSQLConnectionFactoryBase)
constructor Create(AParams: IMiniRESTSQLConnectionParamsSQLDb); overload;
end;

{ TMiniRESTSQLConnectionSQLDb }

TMiniRESTSQLConnectionSQLDb = class(TMiniRESTSQLConnectionBase)
private
function GetConnectorType(const ADatabaseType: TMiniRESTSQLDatabaseType): String;
Expand All @@ -65,8 +73,10 @@ TMiniRESTSQLConnectionSQLDb = class(TMiniRESTSQLConnectionBase)
FConnectionParams: IMiniRESTSQLConnectionParamsSQLDb;
FTransaction: TSQLTransaction;
FInExplicitTransaction: Boolean;
FLogEvent: TLogEvent;
function GetObject: TObject; override;
function GetDriverName(const ADatabaseType: TMiniRESTSQLDatabaseType): string;
procedure Log(Sender : TSQLConnection; EventType : TDBEventType; Const Msg : String);
public
constructor Create(AOwner: IMiniRESTSQLConnectionFactory; AParams: IMiniRESTSQLConnectionParamsSQLDb);
destructor Destroy; override;
Expand Down Expand Up @@ -187,9 +197,11 @@ constructor TMiniRESTSQLConnectionSQLDb.Create(AOwner: IMiniRESTSQLConnectionFac
FTransaction := TSQLTransaction.Create(nil);
//FTransaction.Options := [stoUseImplicit];
FSQLConnection.Transaction := FTransaction;
FSQLConnection.OnLog := @Log;
FTransaction.Action := caCommit;
FConnectionParams := AParams;
FInExplicitTransaction := False;
FLogEvent := AParams.GetLogEvent;
inherited Create(AOwner);
end;

Expand Down Expand Up @@ -287,6 +299,13 @@ function TMiniRESTSQLConnectionSQLDb.GetDriverName(const ADatabaseType: TMiniRES
raise Exception.Create('Not implemented');
end;

procedure TMiniRESTSQLConnectionSQLDb.Log(Sender: TSQLConnection;
EventType: TDBEventType; const Msg: String);
begin
if Assigned(FLogEvent) then
FLogEvent(Sender, EventType, Msg);
end;

procedure TMiniRESTSQLQuerySQLDb.Open;
begin
FConnection.Connect;
Expand Down Expand Up @@ -326,7 +345,8 @@ function TMiniRESTSQLQuerySQLDb.ApplyUpdates(const AMaxErrors: Integer): Integer
LConnectionInExplicitTransaction := TMiniRESTSQLConnectionSQLDb(FConnection.GetObject).FInExplicitTransaction;
FQry.ApplyUpdates;
if not LConnectionInExplicitTransaction then
FConnection.Commit;
FTransaction.Commit;
//FConnection.Commit;
Result := 0;
{TODO: Tratar o retorno. Transformar em procedure?}
end;
Expand All @@ -345,19 +365,19 @@ constructor TMiniRESTSQLQuerySQLDb.Create(AConnection: IMiniRESTSQLConnection);
begin
FConnection := AConnection;
FQry := TSQLQuery.Create(nil);
FQry.Options := [sqoKeepOpenOnCommit];
//FTransaction := TSQLTransaction.Create(nil);
//FQry.Options := [sqoKeepOpenOnCommit];
FTransaction := TSQLTransaction.Create(nil);
//FTransaction.Action := caNone;
//FTransaction.Options := [stoUseImplicit];
//FTransaction.Database := TMiniRESTSQLConnectionSQLDb(AConnection.GetObject).FSQLConnection;
//FQry.Transaction := FTransaction;
FTransaction.Database := TMiniRESTSQLConnectionSQLDb(AConnection.GetObject).FSQLConnection;
FQry.Transaction := FTransaction;
FQry.SQLConnection := TMiniRESTSQLConnectionSQLDb(AConnection.GetObject).FSQLConnection;
end;

destructor TMiniRESTSQLQuerySQLDb.Destroy;
begin
FQry.Free;
//FTransaction.Free;
FTransaction.Free;
inherited Destroy;
end;

Expand All @@ -384,7 +404,7 @@ procedure TMiniRESTSQLConnectionFactorySQLDb.ReleaseConnection(AConnection: IMin
begin
RTLeventWaitFor(FConnectionGetEvent);
try
FQueue.Add(AConnection);
FQueue.Add(AConnection);
TMiniRESTSQLConnectionBaseCrack(AConnection.GetObject).FEstaNoPool := True;
RemoveConnectionToNotifyFree(AConnection);
Inc(FAvailableConnections);
Expand All @@ -394,4 +414,14 @@ procedure TMiniRESTSQLConnectionFactorySQLDb.ReleaseConnection(AConnection: IMin
end;
end;

function TMiniRESTSQLConnectionParamsSQLDb.GetLogEvent: TLogEvent;
begin
Result := FLogEvent;
end;

function TMiniRESTSQLConnectionParamsSQLDb.SetLogEvent(const ALogEvent: TLogEvent): IMiniRESTSQLConnectionParamsSQLDb;
begin
FLogEvent := ALogEvent;
end;

end.
9 changes: 5 additions & 4 deletions unittest/SQL/MiniRESTSQLTest.lpi
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?xml version="1.0" encoding="UTF-8"?>
<CONFIG>
<ProjectOptions>
<Version Value="10"/>
<Version Value="11"/>
<PathDelim Value="\"/>
<General>
<SessionStorage Value="InProjectDir"/>
Expand All @@ -17,9 +17,10 @@
<Version Value="2"/>
</PublishOptions>
<RunParams>
<local>
<FormatVersion Value="1"/>
</local>
<FormatVersion Value="2"/>
<Modes Count="1">
<Mode0 Name="default"/>
</Modes>
</RunParams>
<RequiredPackages Count="3">
<Item1>
Expand Down
Loading

0 comments on commit 3b24541

Please sign in to comment.