Skip to content

Commit

Permalink
Implementação
Browse files Browse the repository at this point in the history
  • Loading branch information
glprog committed Mar 4, 2019
1 parent 64acb77 commit c448ff2
Show file tree
Hide file tree
Showing 6 changed files with 150 additions and 129 deletions.
23 changes: 9 additions & 14 deletions MiniREST.SQL.Base.pas
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@ TMiniRESTSQLConnectionFactoryBase = class abstract(TInterfacedObject, IMiniRES
FConnectionCounter: Integer;
FConnectionsCount: Integer;
procedure AddConnectionToNotifyFree(AConnection: IMiniRESTSQLConnection);
procedure ReleaseConnection(AConnection: IMiniRESTSQLConnection);
procedure RemoveConnectionToNotifyFree(AConnection: IMiniRESTSQLConnection);
procedure ReleaseConnection(AConnection: IMiniRESTSQLConnection); virtual;
function InternalGetconnection: IMiniRESTSQLConnection; virtual; abstract;
constructor Create(const AConnectionCount: Integer);
public
Expand All @@ -48,7 +49,7 @@ TMiniRESTSQLConnectionBase = class abstract(TInterfacedObject, IMiniRESTSQLCon
function _Release: Integer; stdcall;
function GetObject: TObject; virtual; abstract;
procedure SetOwner(AOwner: Pointer);
constructor Create(AOwner : IMiniRESTSQLConnectionFactory);
constructor Create(AOwner : IMiniRESTSQLConnectionFactory);
public
procedure StartTransaction; virtual; abstract;
procedure Commit; virtual; abstract;
Expand Down Expand Up @@ -213,18 +214,7 @@ procedure TMiniRESTSQLConnectionFactoryBase.ReleaseConnection(
finally
FCriticalSection.Leave;
end;
{$ELSE}
RTLeventWaitFor(FConnectionGetEvent);
try
FQueue.Add(AConnection);
TMiniRESTSQLConnectionBase(AConnection.GetObject).FEstaNoPool := True;
FConnectionsToNotifyFree.Remove(AConnection.GetObject);
Inc(FAvailableConnections);
finally
RTLeventSetEvent(FConnectionReleaseEvent);
RTLeventSetEvent(FConnectionGetEvent);
end;
{$IFEND}
{$IFEND}
end;

{ TMiniRESTSQLConnectionBase }
Expand Down Expand Up @@ -347,4 +337,9 @@ procedure TMiniRESTSQLConnectionFactoryBase.AddConnectionToNotifyFree(AConnectio
FConnectionsToNotifyFree.Add(AConnection.GetObject);
end;

procedure TMiniRESTSQLConnectionFactoryBase.RemoveConnectionToNotifyFree(AConnection: IMiniRESTSQLConnection);
begin
FConnectionsToNotifyFree.Remove(AConnection.GetObject);
end;

end.
46 changes: 38 additions & 8 deletions MiniREST.SQL.SQLDb.pas
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ TMiniRESTSQLConnectionFactorySQLDb = class(TMiniRESTSQLConnectionFactoryBase)
protected
FConnectionString: string;
FConnectionParams: IMiniRESTSQLConnectionParamsSQLDb;
procedure ReleaseConnection(AConnection: IMiniRESTSQLConnection); override;
function InternalGetconnection: IMiniRESTSQLConnection; override;
public
constructor Create(AParams: IMiniRESTSQLConnectionParamsSQLDb); overload;
Expand All @@ -63,6 +64,7 @@ TMiniRESTSQLConnectionSQLDb = class(TMiniRESTSQLConnectionBase)
//FTransaction: TDBXTransaction;
FConnectionParams: IMiniRESTSQLConnectionParamsSQLDb;
FTransaction: TSQLTransaction;
FInExplicitTransaction: Boolean;
function GetObject: TObject; override;
function GetDriverName(const ADatabaseType: TMiniRESTSQLDatabaseType): string;
public
Expand Down Expand Up @@ -104,6 +106,9 @@ TMiniRESTSQLQuerySQLDb = class(TInterfacedObject, IMiniRESTSQLQuery)

implementation

type
TMiniRESTSQLConnectionBaseCrack = class(TMiniRESTSQLConnectionBase);

class function TMiniRESTSQLConnectionParamsSQLDb.New: IMiniRESTSQLConnectionParamsSQLDb;
begin
Result := Create;
Expand Down Expand Up @@ -179,10 +184,12 @@ constructor TMiniRESTSQLConnectionFactorySQLDb.Create(AParams: IMiniRESTSQLConne
constructor TMiniRESTSQLConnectionSQLDb.Create(AOwner: IMiniRESTSQLConnectionFactory; AParams: IMiniRESTSQLConnectionParamsSQLDb);
begin
FSQLConnection := TSQLConnector.Create(nil);
FTransaction := TSQLTransaction.Create(nil);
FTransaction := TSQLTransaction.Create(nil);
//FTransaction.Options := [stoUseImplicit];
FSQLConnection.Transaction := FTransaction;
FTransaction.Action := caCommit;
FConnectionParams := AParams;
FInExplicitTransaction := False;
inherited Create(AOwner);
end;

Expand Down Expand Up @@ -224,18 +231,21 @@ procedure TMiniRESTSQLConnectionSQLDb.Connect;
procedure TMiniRESTSQLConnectionSQLDb.StartTransaction;
begin
if FTransaction.Active then
FTransaction.Rollback;
FTransaction.Rollback;
FTransaction.StartTransaction;
FInExplicitTransaction := True;
end;

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

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

function TMiniRESTSQLConnectionSQLDb.GetQuery: IMiniRESTSQLQuery;
Expand Down Expand Up @@ -310,9 +320,13 @@ function TMiniRESTSQLQuerySQLDb.AddParam(AParam: IMiniRESTSQLParam): IMiniRESTSQ
end;

function TMiniRESTSQLQuerySQLDb.ApplyUpdates(const AMaxErrors: Integer): Integer;
begin
var
LConnectionInExplicitTransaction: Boolean;
begin
LConnectionInExplicitTransaction := TMiniRESTSQLConnectionSQLDb(FConnection.GetObject).FInExplicitTransaction;
FQry.ApplyUpdates;
FTransaction.Commit;
if not LConnectionInExplicitTransaction then
FConnection.Commit;
Result := 0;
{TODO: Tratar o retorno. Transformar em procedure?}
end;
Expand All @@ -331,17 +345,19 @@ constructor TMiniRESTSQLQuerySQLDb.Create(AConnection: IMiniRESTSQLConnection);
begin
FConnection := AConnection;
FQry := TSQLQuery.Create(nil);
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 @@ -364,4 +380,18 @@ function TMiniRESTSQLConnectionParamsSQLDb.SetDatabaseName(const ADatabaseName:
FDatabaseName := ADatabaseName;
end;

procedure TMiniRESTSQLConnectionFactorySQLDb.ReleaseConnection(AConnection: IMiniRESTSQLConnection);
begin
RTLeventWaitFor(FConnectionGetEvent);
try
FQueue.Add(AConnection);
TMiniRESTSQLConnectionBaseCrack(AConnection.GetObject).FEstaNoPool := True;
RemoveConnectionToNotifyFree(AConnection);
Inc(FAvailableConnections);
finally
RTLeventSetEvent(FConnectionReleaseEvent);
RTLeventSetEvent(FConnectionGetEvent);
end;
end;

end.
Loading

0 comments on commit c448ff2

Please sign in to comment.