From 1a3e4e974f7d1eca631adee0dc91a64f9bd1aac3 Mon Sep 17 00:00:00 2001 From: SchneiderInfosystems Date: Thu, 8 Aug 2024 09:07:42 +0200 Subject: [PATCH] Improve in Firestore RunQuery --- Samples/Intro/FB4D.DemoFmx.fmx | 9 +++++-- Samples/Intro/FB4D.FirestoreFra.fmx | 38 +++++++++++++++++++------- Samples/Intro/FB4D.FirestoreFra.pas | 41 ++++++++++++++++++----------- 3 files changed, 60 insertions(+), 28 deletions(-) diff --git a/Samples/Intro/FB4D.DemoFmx.fmx b/Samples/Intro/FB4D.DemoFmx.fmx index 2ad96b9..65a8bfc 100644 --- a/Samples/Intro/FB4D.DemoFmx.fmx +++ b/Samples/Intro/FB4D.DemoFmx.fmx @@ -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 @@ -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 @@ -627,7 +633,6 @@ object fmxFirebaseDemo: TfmxFirebaseDemo OnClick = lblOpenFBConsoleClick end object lblOpenFBConsoleForProject: TLabel - Anchors = [akTop, akRight] AutoSize = True Cursor = crHandPoint StyledSettings = [Family, Size, FontColor] @@ -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 diff --git a/Samples/Intro/FB4D.FirestoreFra.fmx b/Samples/Intro/FB4D.FirestoreFra.fmx index 489f206..6e0077e 100644 --- a/Samples/Intro/FB4D.FirestoreFra.fmx +++ b/Samples/Intro/FB4D.FirestoreFra.fmx @@ -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 = ( @@ -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 @@ -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 @@ -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 @@ -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 @@ -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 @@ -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] @@ -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] @@ -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 @@ -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 diff --git a/Samples/Intro/FB4D.FirestoreFra.pas b/Samples/Intro/FB4D.FirestoreFra.pas index 7a71886..3883140 100644 --- a/Samples/Intro/FB4D.FirestoreFra.pas +++ b/Samples/Intro/FB4D.FirestoreFra.pas @@ -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); @@ -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; @@ -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', ''); @@ -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); @@ -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; @@ -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 @@ -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'} @@ -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 @@ -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). @@ -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);