Skip to content

Commit

Permalink
Remove TransactionType ReadWrite because this function is not availbl…
Browse files Browse the repository at this point in the history
…e in the Firestore Rest API

Rename method BeginTransactionSynchronous to BeginReadOnlyTransactionSynchronous
Add RollBackTransactionSynchronous method
  • Loading branch information
SchneiderInfosystems committed Apr 17, 2020
1 parent 8267742 commit c71a7e6
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 160 deletions.
31 changes: 4 additions & 27 deletions Samples/Intro/FB4D.DemoFmx.fmx
Original file line number Diff line number Diff line change
Expand Up @@ -1267,43 +1267,20 @@ object fmxFirebaseDemo: TfmxFirebaseDemo
Size.Width = 161.000000000000000000
Size.Height = 22.000000000000000000
Size.PlatformDefault = False
TabOrder = 18
TabOrder = 16
Text = 'Start Transaction Read Only'
OnClick = btnStartTransReadOnlyClick
end
object btnStartTransReadWrite: TButton
Enabled = False
Position.X = 680.000000000000000000
Position.Y = 60.000000000000000000
Size.Width = 161.000000000000000000
Size.Height = 22.000000000000000000
Size.PlatformDefault = False
TabOrder = 16
Text = 'Start Transaction Read/Write'
OnClick = btnStartTransReadWriteClick
end
object btnCommitTrans: TButton
object btnStopTrans: TButton
Position.X = 512.000000000000000000
Position.Y = 60.000000000000000000
Size.Width = 161.000000000000000000
Size.Height = 22.000000000000000000
Size.PlatformDefault = False
TabOrder = 17
Text = 'Commit Transaction'
Visible = False
OnClick = btnCommitTransClick
end
object btnRollBackTrans: TButton
Enabled = False
Position.X = 680.000000000000000000
Position.Y = 60.000000000000000000
Size.Width = 161.000000000000000000
Size.Height = 22.000000000000000000
Size.PlatformDefault = False
TabOrder = 15
Text = 'Roll Back Transaction'
Text = 'Stop Transaction'
Visible = False
OnClick = btnRollBackTransClick
OnClick = btnStopTransClick
end
end
object tabStorage: TTabItem
Expand Down
65 changes: 6 additions & 59 deletions Samples/Intro/FB4D.DemoFmx.pas
Original file line number Diff line number Diff line change
Expand Up @@ -176,9 +176,7 @@ TfmxFirebaseDemo = class(TForm)
lblMinTestInt: TLabel;
btnLinkEMailPwd: TButton;
btnStartTransReadOnly: TButton;
btnStartTransReadWrite: TButton;
btnCommitTrans: TButton;
btnRollBackTrans: TButton;
btnStopTrans: TButton;
procedure btnLoginClick(Sender: TObject);
procedure btnRefreshClick(Sender: TObject);
procedure timRefreshTimer(Sender: TObject);
Expand Down Expand Up @@ -231,9 +229,7 @@ TfmxFirebaseDemo = class(TForm)
procedure trbMinTestIntChange(Sender: TObject);
procedure btnLinkEMailPwdClick(Sender: TObject);
procedure btnStartTransReadOnlyClick(Sender: TObject);
procedure btnStartTransReadWriteClick(Sender: TObject);
procedure btnRollBackTransClick(Sender: TObject);
procedure btnCommitTransClick(Sender: TObject);
procedure btnStopTransClick(Sender: TObject);
private
fAuth: IFirebaseAuthentication;
fStorageObject: IStorageObject;
Expand Down Expand Up @@ -1373,69 +1369,20 @@ procedure TfmxFirebaseDemo.btnStartTransReadOnlyClick(Sender: TObject);
if not CheckAndCreateFirestoreDBClass(memFirestore) then
exit;
try
fTransaction := fDatabase.BeginTransactionSynchronous(ttReadOnly);
fTransaction := fDatabase.BeginReadOnlyTransactionSynchronous;
memFirestore.Lines.Add('Read only transaction started');
except
on e: EFirebaseResponse do
memFirestore.Lines.Add('Transaction failed: ' + e.Message);
end;
btnStartTransReadWrite.Visible := false;
btnStartTransReadOnly.Visible := false;
btnCommitTrans.Visible := true;
btnRollBackTrans.Visible := true;
btnStopTrans.Visible := true;
end;

procedure TfmxFirebaseDemo.btnStartTransReadWriteClick(Sender: TObject);
procedure TfmxFirebaseDemo.btnStopTransClick(Sender: TObject);
begin
if not CheckAndCreateFirestoreDBClass(memFirestore) then
exit;
try
fTransaction := fDatabase.BeginTransactionSynchronous(ttReadOnly);
memFirestore.Lines.Add('Read/Write transaction started');
except
on e: EFirebaseResponse do
memFirestore.Lines.Add('Transaction failed: ' + e.Message);
end;
btnStartTransReadWrite.Visible := false;
btnStartTransReadOnly.Visible := false;
btnCommitTrans.Visible := true;
btnRollBackTrans.Visible := true;
end;

procedure TfmxFirebaseDemo.btnRollBackTransClick(Sender: TObject);
begin
try
fDatabase.RollBackTransactionSynchronous(fTransaction);
except
on e: EFirebaseResponse do
memFirestore.Lines.Add('Roll back failed: ' + e.Message);
end;
fTransaction := '';
btnCommitTrans.Visible := false;
btnRollBackTrans.Visible := false;
btnStartTransReadWrite.Visible := true;
btnStartTransReadOnly.Visible := true;
end;

procedure TfmxFirebaseDemo.btnCommitTransClick(Sender: TObject);
var
dt: TDateTime;
begin
try
dt := fDatabase.CommitTransactionSynchronous(fTransaction);
if dt > 0 then
memFirestore.Lines.Add('Transaction commited at ' +
DateTimeToStr(TFirebaseHelpers.ConvertToLocalDateTime(dt)))
else
memFirestore.Lines.Add('Transaction: nothing to commit');
except
on e: EFirebaseResponse do
memFirestore.Lines.Add('Commit failed: ' + e.Message);
end;
fTransaction := '';
btnCommitTrans.Visible := false;
btnRollBackTrans.Visible := false;
btnStartTransReadWrite.Visible := true;
btnStopTrans.Visible := false;
btnStartTransReadOnly.Visible := true;
end;
{$ENDREGION}
Expand Down
112 changes: 45 additions & 67 deletions Source/FB4D.Firestore.pas
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@ TFirestoreDatabase = class(TInterfacedObject, IFirestoreDatabase)
fOnPatchError, fOnDeleteError: TOnRequestError;
fOnCreateDocument, fOnInsertUpdateDocument, fOnPatchDocument: TOnDocument;
fOnDelete: TOnResponse;
fOnTransaction: TOnBeginTransaction;
fOnTransactionError: TOnRequestError;
function BaseURI: string;
procedure OnQueryResponse(const RequestID: string;
Response: IFirebaseResponse);
Expand All @@ -60,6 +62,8 @@ TFirestoreDatabase = class(TInterfacedObject, IFirestoreDatabase)
Response: IFirebaseResponse);
procedure OnDeleteResponse(const RequestID: string;
Response: IFirebaseResponse);
procedure BeginReadOnlyTransactionRespose(const RequestID: string;
Response: IFirebaseResponse);
public
constructor Create(const ProjectID: string; Auth: IFirebaseAuthentication;
const DatabaseID: string = DefaultDatabaseID);
Expand Down Expand Up @@ -101,13 +105,9 @@ TFirestoreDatabase = class(TInterfacedObject, IFirestoreDatabase)
OnResponse: TOnResponse; OnRequestError: TOnRequestError);
function DeleteSynchronous(Params: TRequestResourceParam;
QueryParams: TQueryParams = nil): IFirebaseResponse;
function BeginTransactionSynchronous(
TransactionType: TTransactionType): TTransaction;
function CommitTransactionSynchronous(const Transaction: TTransaction;
Writes: TJSONArray = nil): TDateTime;
procedure RollBackTransactionSynchronous(const Transaction: TTransaction);
// Async transaction methods are not yet implemented

procedure BeginReadOnlyTransaction(OnBeginTransaction: TOnBeginTransaction;
OnRequestError: TOnRequestError);
function BeginReadOnlyTransactionSynchronous: TTransaction;
property ProjectID: string read fProjectID;
property DatabaseID: string read fDatabaseID;
end;
Expand Down Expand Up @@ -611,70 +611,67 @@ function TFirestoreDatabase.DeleteSynchronous(Params: TRequestResourceParam;
result := Request.SendRequestSynchronous(Params, rmDELETE, nil, QueryParams);
end;

function TFirestoreDatabase.BeginTransactionSynchronous(
TransactionType: TTransactionType): TTransaction;
procedure TFirestoreDatabase.BeginReadOnlyTransaction(
OnBeginTransaction: TOnBeginTransaction; OnRequestError: TOnRequestError);
var
Request: IFirebaseRequest;
Response: IFirebaseResponse;
Data, Options, Res: TJSONObject;
Data: TJSONObject;
begin
fOnTransaction := OnBeginTransaction;
fOnTransactionError := OnRequestError;
Assert(assigned(fAuth), 'Authentication is required');
result := '';
Request := TFirebaseRequest.Create(BaseURI + METHODE_BEGINTRANS,
rsBeginTrans, fAuth);
case TransactionType of
ttReadOnly:
Options := TJSONObject.Create(
TJSONPair.Create('readOnly', TJSONObject.Create));
ttReadWrite:
Options := TJSONObject.Create(
TJSONPair.Create('readWrite', TJSONObject.Create));
else
Options := TJSONObject.Create;
end;
Data := TJSONObject.Create(TJSONPair.Create('options', Options));
Data := TJSONObject.Create(TJSONPair.Create('options', TJSONObject.Create(
TJSONPair.Create('readOnly', TJSONObject.Create))));
Request.SendRequest(nil, rmPOST, Data, nil, tmBearer,
BeginReadOnlyTransactionRespose, OnRequestError);
end;

procedure TFirestoreDatabase.BeginReadOnlyTransactionRespose(
const RequestID: string; Response: IFirebaseResponse);
var
Res: TJSONObject;
begin
try
Response := Request.SendRequestSynchronous(nil, rmPOST, Data, nil);
if Response.StatusOk then
Response.CheckForJSONObj;
Res := Response.GetContentAsJSONObj;
try
if assigned(fOnTransaction) then
fOnTransaction(Res.GetValue<string>('transaction'));
finally
Res.Free;
end;
except
on e: Exception do
begin
Res := Response.GetContentAsJSONObj;
try
result := Res.GetValue<string>('transaction');
finally
Res.Free;
end;
end else
raise EFirebaseResponse.Create(Response.ErrorMsgOrStatusText);
finally
Data.Free;
if assigned(fOnTransactionError) then
fOnTransactionError(RequestID, e.Message)
else
TFirebaseHelpers.Log(Format(rsFBFailureIn, [RequestID, e.Message]));
end;
end;
end;

function TFirestoreDatabase.CommitTransactionSynchronous(
const Transaction: TTransaction; Writes: TJSONArray): TDateTime;
function TFirestoreDatabase.BeginReadOnlyTransactionSynchronous: TTransaction;
var
Request: IFirebaseRequest;
Response: IFirebaseResponse;
Data, Res: TJSONObject;
begin
Assert(assigned(fAuth), 'Authentication is required');
Request := TFirebaseRequest.Create(BaseURI + METHODE_COMMITTRANS,
rsCommitTrans, fAuth);
if assigned(Writes) then
Data := TJSONObject.Create(TJSONPair.Create('writes', Writes))
else
Data := TJSONObject.Create;
result := '';
Request := TFirebaseRequest.Create(BaseURI + METHODE_BEGINTRANS,
rsBeginTrans, fAuth);
Data := TJSONObject.Create(TJSONPair.Create('options', TJSONObject.Create(
TJSONPair.Create('readOnly', TJSONObject.Create))));
try
Data.AddPair(TJSONPair.Create('transaction', Transaction));
Response := Request.SendRequestSynchronous(nil, rmPOST, Data, nil);
if Response.StatusOk then
begin
Res := Response.GetContentAsJSONObj;
try
if Res.GetValue('commitTime') <> nil then
result := Res.GetValue<TDateTime>('commitTime')
else
result := 0; // nothing to commit
result := Res.GetValue<string>('transaction');
finally
Res.Free;
end;
Expand All @@ -685,25 +682,6 @@ function TFirestoreDatabase.CommitTransactionSynchronous(
end;
end;

procedure TFirestoreDatabase.RollBackTransactionSynchronous(
const Transaction: TTransaction);
var
Request: IFirebaseRequest;
Response: IFirebaseResponse;
Data: TJSONObject;
begin
Assert(assigned(fAuth), 'Authentication is required');
Request := TFirebaseRequest.Create(BaseURI + METHODE_ROLLBACK,
rsRollBackTrans, fAuth);
Data := TJSONObject.Create(TJSONPair.Create('transaction', Transaction));
try
Response := Request.SendRequestSynchronous(nil, rmPOST, Data, nil);
if not Response.StatusOk then
raise EFirebaseResponse.Create(Response.ErrorMsgOrStatusText);
finally
Data.Free;
end;
end;

{ TStructuredQuery }

Expand Down
11 changes: 4 additions & 7 deletions Source/FB4D.Interfaces.pas
Original file line number Diff line number Diff line change
Expand Up @@ -243,8 +243,8 @@ EFirestoreDocument = class(Exception);
Documents: IFirestoreDocuments) of object;
TOnDocument = procedure(const Info: string;
Document: IFirestoreDocument) of object;
TTransactionType = (ttReadOnly, ttReadWrite);
TTransaction = string; // A base64 encoded ID
TOnBeginTransaction = procedure(Transaction: TTransaction) of object;
IFirestoreDatabase = interface(IInterface)
procedure RunQuery(StructuredQuery: IStructuredQuery;
OnDocuments: TOnDocuments; OnRequestError: TOnRequestError;
Expand Down Expand Up @@ -284,12 +284,9 @@ EFirestoreDocument = class(Exception);
OnResponse: TOnResponse; OnRequestError: TOnRequestError);
function DeleteSynchronous(Params: TRequestResourceParam;
QueryParams: TQueryParams = nil): IFirebaseResponse;
function BeginTransactionSynchronous(
TransactionType: TTransactionType): TTransaction;
function CommitTransactionSynchronous(const Transaction: TTransaction;
Writes: TJSONArray = nil): TDateTime;
procedure RollBackTransactionSynchronous(const Transaction: TTransaction);
// Async transaction methods are not yet implemented
procedure BeginReadOnlyTransaction(OnBeginTransaction: TOnBeginTransaction;
OnRequestError: TOnRequestError);
function BeginReadOnlyTransactionSynchronous: TTransaction;
end;

{$IFDEF TOKENJWT}
Expand Down

0 comments on commit c71a7e6

Please sign in to comment.