Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Controle de Transaction Firedac #76

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions SimpleInterface.pas
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@ interface
function DataSet : TDataSet;
function Open(aSQL : String) : iSimpleQuery; overload;
function Open : iSimpleQuery; overload;
function &EndTransaction: iSimpleQuery;
end;


Expand Down
58 changes: 41 additions & 17 deletions SimpleQueryFiredac.pas
Original file line number Diff line number Diff line change
Expand Up @@ -9,20 +9,22 @@ interface

Type
TSimpleQueryFiredac = class(TInterfacedObject, iSimpleQuery)
private
FConnection : TFDConnection;
FQuery : TFDQuery;
FParams : TParams;
public
constructor Create(aConnection : TFDConnection);
destructor Destroy; override;
class function New(aConnection : TFDConnection) : iSimpleQuery;
function SQL : TStrings;
function Params : TParams;
function ExecSQL : iSimpleQuery;
function DataSet : TDataSet;
function Open(aSQL : String) : iSimpleQuery; overload;
function Open : iSimpleQuery; overload;
private
FConnection: TFDConnection;
FTransaction: TFDTransaction;
FQuery: TFDQuery;
FParams: TParams;
public
constructor Create(aConnection: TFDConnection);
destructor Destroy; override;
class function New(aConnection: TFDConnection): iSimpleQuery;
function SQL: TStrings;
function Params: TParams;
function ExecSQL: iSimpleQuery;
function DataSet: TDataSet;
function Open(aSQL: String): iSimpleQuery; overload;
function Open: iSimpleQuery; overload;
function &EndTransaction: iSimpleQuery;
end;

implementation
Expand All @@ -32,11 +34,20 @@ implementation

{ TSimpleQuery<T> }

constructor TSimpleQueryFiredac.Create(aConnection : TFDConnection);
constructor TSimpleQueryFiredac.Create(aConnection: TFDConnection);
begin
FQuery := TFDQuery.Create(nil);
FConnection := aConnection;
FQuery.Connection := FConnection;

FTransaction := TFDTransaction.Create(nil);
FTransaction.Connection := FConnection;

FQuery.Transaction := FTransaction;

if FTransaction.Active then
FTransaction.Commit;
FTransaction.StartTransaction;
end;

function TSimpleQueryFiredac.DataSet: TDataSet;
Expand All @@ -46,26 +57,39 @@ function TSimpleQueryFiredac.DataSet: TDataSet;

destructor TSimpleQueryFiredac.Destroy;
begin
FreeAndNil(FTransaction);
FreeAndNil(FQuery);
if Assigned(FParams) then
FreeAndNil(FParams);
inherited;
end;

function TSimpleQueryFiredac.EndTransaction: iSimpleQuery;
begin
if FTransaction.Active then
FTransaction.Commit;
end;

function TSimpleQueryFiredac.ExecSQL: iSimpleQuery;
begin
Result := Self;
if Assigned(FParams) then
FQuery.Params.Assign(FParams);

FQuery.Prepare;
FQuery.ExecSQL;

try
FQuery.ExecSQL;
except
FTransaction.Rollback;
end;

if Assigned(FParams) then
FreeAndNil(FParams);
end;

class function TSimpleQueryFiredac.New(aConnection : TFDConnection): iSimpleQuery;
class function TSimpleQueryFiredac.New(aConnection: TFDConnection)
: iSimpleQuery;
begin
Result := Self.Create(aConnection);
end;
Expand Down