Skip to content

Commit

Permalink
Improve in Firestore RunQuery
Browse files Browse the repository at this point in the history
  • Loading branch information
SchneiderInfosystems committed Aug 8, 2024
1 parent eb4463c commit 1a3e4e9
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 28 deletions.
9 changes: 7 additions & 2 deletions Samples/Intro/FB4D.DemoFmx.fmx
Original file line number Diff line number Diff line change
Expand Up @@ -320,6 +320,9 @@ object fmxFirebaseDemo: TfmxFirebaseDemo
Size.Height = 26.000000000000000000
ExplicitSize.cx = 100.000000000000000000
ExplicitSize.cy = 26.000000000000000000
inherited cboDemoDocType: TComboBox
TabOrder = 18
end
end
inherited tabPatch: TTabItem
Size.Width = 50.000000000000000000
Expand Down Expand Up @@ -364,6 +367,9 @@ object fmxFirebaseDemo: TfmxFirebaseDemo
ExplicitSize.cy = 26.000000000000000000
inherited GroupBox1: TGroupBox
TabOrder = 4
inherited chbIncludeDescendants: TCheckBox
TabOrder = 38
end
end
inherited GroupBox2: TGroupBox
Size.Width = 242.000000000000000000
Expand Down Expand Up @@ -627,7 +633,6 @@ object fmxFirebaseDemo: TfmxFirebaseDemo
OnClick = lblOpenFBConsoleClick
end
object lblOpenFBConsoleForProject: TLabel
Anchors = [akTop, akRight]
AutoSize = True
Cursor = crHandPoint
StyledSettings = [Family, Size, FontColor]
Expand Down Expand Up @@ -684,7 +689,7 @@ object fmxFirebaseDemo: TfmxFirebaseDemo
Cursor = crHandPoint
StyledSettings = [Family, Size, FontColor]
HitTest = True
Position.X = 760.000000000000000000
Position.X = 750.000000000000000000
Position.Y = 4.000000000000000000
Size.Width = 200.000000000000000000
Size.Height = 16.000000000000000000
Expand Down
38 changes: 28 additions & 10 deletions Samples/Intro/FB4D.FirestoreFra.fmx
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ object FirestoreFra: TFirestoreFra
Size.Width = 1000.000000000000000000
Size.Height = 132.000000000000000000
Size.PlatformDefault = False
TabIndex = 0
TabIndex = 1
TabOrder = 11
TabPosition = PlatformDefault
Sizes = (
Expand Down Expand Up @@ -207,6 +207,25 @@ object FirestoreFra: TFirestoreFra
Size.Height = 22.000000000000000000
Size.PlatformDefault = False
TabOrder = 19
OnChange = cboDemoDocTypeChange
end
object edtTestInt: TEdit
Touch.InteractiveGestures = [LongTap, DoubleTap]
TabOrder = 20
KeyboardType = NumbersAndPunctuation
Position.X = 439.000000000000000000
Position.Y = 16.000000000000000000
Visible = False
object lblTestInt: TLabel
Position.X = -120.000000000000000000
Position.Y = 3.000000000000000000
Size.Width = 108.000000000000000000
Size.Height = 17.000000000000000000
Size.PlatformDefault = False
TextSettings.HorzAlign = Trailing
Text = 'testInt Value'
TabOrder = 0
end
end
end
object tabPatch: TTabItem
Expand Down Expand Up @@ -292,7 +311,7 @@ object FirestoreFra: TFirestoreFra
Size.Width = 145.000000000000000000
Size.Height = 19.000000000000000000
Size.PlatformDefault = False
TabOrder = 13
TabOrder = 12
Text = 'Get next page'
end
object chbLimitTo10Docs: TCheckBox
Expand Down Expand Up @@ -320,13 +339,12 @@ object FirestoreFra: TFirestoreFra
ExplicitSize.cx = 53.000000000000000000
ExplicitSize.cy = 26.000000000000000000
object btnRunQuery: TButton
Enabled = False
Position.X = 16.000000000000000000
Position.Y = 16.000000000000000000
Size.Width = 121.000000000000000000
Size.Height = 22.000000000000000000
Size.PlatformDefault = False
TabOrder = 16
TabOrder = 13
Text = 'Run Query'
TextSettings.Trimming = None
OnClick = btnRunQueryClick
Expand All @@ -336,7 +354,7 @@ object FirestoreFra: TFirestoreFra
Position.Y = 58.000000000000000000
TextSettings.Trimming = None
Text = 'Min testInt Val: 50'
TabOrder = 7
TabOrder = 5
end
object trbMinTestInt: TTrackBar
CanParentFocus = True
Expand All @@ -346,7 +364,7 @@ object FirestoreFra: TFirestoreFra
Size.Width = 121.000000000000000000
Size.Height = 19.000000000000000000
Size.PlatformDefault = False
TabOrder = 5
TabOrder = 4
Value = 50.000000000000000000
OnChange = trbMinTestIntChange
end
Expand Down Expand Up @@ -387,7 +405,7 @@ object FirestoreFra: TFirestoreFra
Size.Height = 57.000000000000000000
Size.PlatformDefault = False
Text = 'Target 1: Entire Collection'
TabOrder = 7
TabOrder = 5
object edtCollectionIDForFSListener: TEdit
Touch.InteractiveGestures = [LongTap, DoubleTap]
Anchors = [akLeft, akTop, akRight]
Expand Down Expand Up @@ -431,7 +449,7 @@ object FirestoreFra: TFirestoreFra
Size.Height = 57.000000000000000000
Size.PlatformDefault = False
Text = 'Target 2: Single Document'
TabOrder = 6
TabOrder = 4
object edtDocPathForFSListener: TEdit
Touch.InteractiveGestures = [LongTap, DoubleTap]
Anchors = [akLeft, akTop, akRight]
Expand Down Expand Up @@ -462,7 +480,7 @@ object FirestoreFra: TFirestoreFra
Size.Width = 121.000000000000000000
Size.Height = 22.000000000000000000
Size.PlatformDefault = False
TabOrder = 2
TabOrder = 1
Text = 'Stop Listener'
TextSettings.Trimming = None
OnClick = btnStopFSListenerClick
Expand Down Expand Up @@ -512,7 +530,7 @@ object FirestoreFra: TFirestoreFra
Size.Width = 104.000000000000000000
Size.Height = 22.000000000000000000
Size.PlatformDefault = False
TabOrder = 26
TabOrder = 25
Text = 'Read Transaction'
TextSettings.Trimming = None
OnClick = btnStartReadTransactionClick
Expand Down
41 changes: 25 additions & 16 deletions Samples/Intro/FB4D.FirestoreFra.pas
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,8 @@ TFirestoreFra = class(TFrame)
btnStopFSListener: TButton;
btnStartFSListener: TButton;
lblReadTransID: TLabel;
edtTestInt: TEdit;
lblTestInt: TLabel;
procedure edtDocumentChangeTracking(Sender: TObject);
procedure btnGetClick(Sender: TObject);
procedure btnCreateDocumentClick(Sender: TObject);
Expand All @@ -103,6 +105,7 @@ TFirestoreFra = class(TFrame)
procedure mniClearClick(Sender: TObject);
procedure btnStartFSListenerClick(Sender: TObject);
procedure btnStopFSListenerClick(Sender: TObject);
procedure cboDemoDocTypeChange(Sender: TObject);
private
fDatabase: IFirestoreDatabase;
fReadTransaction: TFirestoreReadTransaction;
Expand Down Expand Up @@ -188,7 +191,6 @@ procedure TFirestoreFra.LoadSettingsFromIniFile(IniFile: TIniFile);
edtChildCollection.Text := IniFile.ReadString('Firestore', 'ChildCol', '');
edtChildDocument.Text := IniFile.ReadString('Firestore', 'ChildDoc', '');
cboDemoDocType.ItemIndex := IniFile.ReadInteger('Firestore', 'DocType', 0);
btnRunQuery.Enabled := IniFile.ReadBool('Firestore', 'RunQueryEnabled', false);
edtCollectionIDForFSListener.Text := IniFile.ReadString('Firestore',
'ListenerColID', '');
edtDocPathForFSListener.Text := IniFile.ReadString('Firestore', 'DocPath', '');
Expand All @@ -204,7 +206,6 @@ procedure TFirestoreFra.SaveSettingsIntoIniFile(IniFile: TIniFile);
IniFile.WriteString('Firestore', 'ChildCol', edtChildCollection.Text);
IniFile.WriteString('Firestore', 'ChildDoc', edtChildDocument.Text);
IniFile.WriteInteger('Firestore', 'DocType', cboDemoDocType.ItemIndex);
IniFile.WriteBool('Firestore', 'RunQueryEnabled', btnRunQuery.Enabled);
IniFile.WriteString('Firestore', 'ListenerColID',
edtCollectionIDForFSListener.Text);
IniFile.WriteString('Firestore', 'DocPath', edtDocPathForFSListener.Text);
Expand Down Expand Up @@ -235,8 +236,6 @@ function TFirestoreFra.CheckFirestoreFields(InsUpdGetWF: boolean): boolean;

procedure TFirestoreFra.CheckDocument;
begin
chbLimitTo10Docs.Visible := edtDocument.Text.IsEmpty;
chbUseChildDoc.Visible := not chbLimitTo10Docs.Visible;
edtChildCollection.Visible := chbUseChildDoc.IsChecked;
edtChildDocument.Visible := chbUseChildDoc.IsChecked;
chbUsePageToken.Visible := false;
Expand Down Expand Up @@ -372,17 +371,12 @@ procedure TFirestoreFra.btnInsertOrUpdateDocumentClick(Sender: TObject);
else
Doc.AddOrUpdateField(TJSONObject.SetString('info',
'This demonstrates a simple child collection with random data.'));
if not btnRunQuery.Enabled then
Doc.AddOrUpdateField(TJSONObject.SetString('hint',
'For test Run Query create same record as this one and by press ' +
'Create Doc and Insert Doc repeatedly'))
else
Doc.AddOrUpdateField(TJSONObject.SetString('hint',
'Create at least 5 documents so you have some records for later ' +
'test of Run Query'));
Doc.AddOrUpdateField(TJSONObject.SetInteger('testInt', random(100)));
Doc.AddOrUpdateField(TJSONObject.SetString('hint',
'For test Run Query create same record as this one and by press ' +
'Create Doc and Insert Doc repeatedly, so you have at least 5 documents'));
Doc.AddOrUpdateField(TJSONObject.SetInteger('testInt', StrToInt(edtTestInt.Text)));
Doc.AddOrUpdateField(TJSONObject.SetTimeStamp('documentCreated', now));
btnRunQuery.Enabled := true;
edtTestInt.Text := IntToStr(random(100));
end;
end;
if assigned(fWriteTransaction) then
Expand Down Expand Up @@ -432,6 +426,13 @@ procedure TFirestoreFra.OnFirestoreInsertOrUpdate(const Info: string;
Doc := nil;
end;

procedure TFirestoreFra.cboDemoDocTypeChange(Sender: TObject);
begin
edtTestInt.visible := cboDemoDocType.ItemIndex = 2;
edtTestInt.Text := IntToStr(random(100));
end;


{$ENDREGION}

{$REGION 'Get Document'}
Expand Down Expand Up @@ -698,6 +699,10 @@ procedure TFirestoreFra.btnRunQueryClick(Sender: TObject);
// The following structured query expects documents that were created with the
// option 'Docs for Run Query'
if not chbUseChildDoc.IsChecked then
begin
memFirestore.Lines.Add(Format(
'Query documents: select * from %s where testIn >= %d order by testInt asc',
[edtCollection.Text, trunc(trbMinTestInt.Value)]));
fDatabase.RunQuery(
TStructuredQuery.CreateForCollection(edtCollection.Text).
// To fetch partial documents
Expand All @@ -706,8 +711,11 @@ procedure TFirestoreFra.btnRunQueryClick(Sender: TObject);
TQueryFilter.IntegerFieldFilter('testInt', woGreaterThan,
trunc(trbMinTestInt.Value))).
OrderBy('testInt', odAscending),
OnFirestoreGet, OnFirestoreError, Query)
else
OnFirestoreGet, OnFirestoreError, Query);
end else begin
memFirestore.Lines.Add(
Format('Query documents: select testInt, documentCreated, info from %s/%s/%s limit 10 where testIn >= %d order by testInt asc, documentCreated desc',
[edtCollection.Text, edtDocument.Text, edtChildCollection.Text, trunc(trbMinTestInt.Value)]));
fDatabase.RunQuery([edtCollection.Text, edtDocument.Text],
TStructuredQuery.CreateForSelect(['testInt', 'documentCreated', 'info']).
Collection(edtChildCollection.Text).
Expand All @@ -724,6 +732,7 @@ procedure TFirestoreFra.btnRunQueryClick(Sender: TObject);
// TJSONObject.SetInteger('testInt', 85)), false).
Limit(10).Offset(0), // returns only the first 10 documents!
OnFirestoreGet, OnFirestoreError, Query);
end;
end;

procedure TFirestoreFra.trbMinTestIntChange(Sender: TObject);
Expand Down

0 comments on commit 1a3e4e9

Please sign in to comment.