Skip to content

Commit 1886d20

Browse files
committed
Revert "Temporarily remove NamedQuery from the public API (#452)"
This reverts commit 8598f29.
1 parent 0d36667 commit 1886d20

File tree

5 files changed

+94
-0
lines changed

5 files changed

+94
-0
lines changed

firestore/integration_test_internal/src/bundle_test.cc

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,25 @@ class BundleTest : public FirestoreIntegrationTest {
9494
MapFieldValue{{"k", FieldValue::String("b")},
9595
{"bar", FieldValue::Integer(2)}}));
9696
}
97+
98+
{
99+
Query limit = AwaitResult(db->NamedQuery(kLimitQueryName));
100+
auto limit_snapshot = AwaitResult(limit.Get(Source::kCache));
101+
EXPECT_THAT(
102+
QuerySnapshotToValues(limit_snapshot),
103+
testing::ElementsAre(MapFieldValue{{"k", FieldValue::String("b")},
104+
{"bar", FieldValue::Integer(2)}}));
105+
}
106+
107+
{
108+
Query limit_to_last = AwaitResult(db->NamedQuery(kLimitToLastQueryName));
109+
auto limit_to_last_snapshot =
110+
AwaitResult(limit_to_last.Get(Source::kCache));
111+
EXPECT_THAT(
112+
QuerySnapshotToValues(limit_to_last_snapshot),
113+
testing::ElementsAre(MapFieldValue{{"k", FieldValue::String("a")},
114+
{"bar", FieldValue::Integer(1)}}));
115+
}
97116
}
98117
};
99118

@@ -240,6 +259,21 @@ TEST_F(BundleTest, LoadBundleWithDocumentsAlreadyPulledFromBackend) {
240259
testing::ElementsAre(
241260
MapFieldValue{{"bar", FieldValue::String("newValueA")}},
242261
MapFieldValue{{"bar", FieldValue::String("newValueB")}}));
262+
263+
{
264+
Query limit = AwaitResult(db->NamedQuery(kLimitQueryName));
265+
EXPECT_THAT(QuerySnapshotToValues(AwaitResult(limit.Get(Source::kCache))),
266+
testing::ElementsAre(
267+
MapFieldValue{{"bar", FieldValue::String("newValueB")}}));
268+
}
269+
270+
{
271+
Query limit_to_last = AwaitResult(db->NamedQuery(kLimitToLastQueryName));
272+
EXPECT_THAT(
273+
QuerySnapshotToValues(AwaitResult(limit_to_last.Get(Source::kCache))),
274+
testing::ElementsAre(
275+
MapFieldValue{{"bar", FieldValue::String("newValueA")}}));
276+
}
243277
}
244278

245279
TEST_F(BundleTest, LoadedDocumentsShouldNotBeGarbageCollectedRightAway) {
@@ -282,6 +316,28 @@ TEST_F(BundleTest, LoadDocumentsFromOtherProjectsShouldFail) {
282316
VerifyErrorProgress(progresses[1]);
283317
}
284318

319+
TEST_F(BundleTest, GetInvalidNamedQuery) {
320+
Firestore* db = TestFirestore();
321+
{
322+
auto future = db->NamedQuery("DOES_NOT_EXIST");
323+
Await(future);
324+
EXPECT_EQ(future.status(), FutureStatus::kFutureStatusComplete);
325+
EXPECT_EQ(future.error(), Error::kErrorNotFound);
326+
}
327+
{
328+
auto future = db->NamedQuery("");
329+
Await(future);
330+
EXPECT_EQ(future.status(), FutureStatus::kFutureStatusComplete);
331+
EXPECT_EQ(future.error(), Error::kErrorNotFound);
332+
}
333+
{
334+
auto future = db->NamedQuery("\xc3\x28");
335+
Await(future);
336+
EXPECT_EQ(future.status(), FutureStatus::kFutureStatusComplete);
337+
EXPECT_EQ(future.error(), Error::kErrorNotFound);
338+
}
339+
}
340+
285341
} // namespace
286342
} // namespace firestore
287343
} // namespace firebase

firestore/src/common/firestore.cc

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -343,5 +343,10 @@ Future<LoadBundleTaskProgress> Firestore::LoadBundle(
343343
return internal_->LoadBundle(bundle, std::move(progress_callback));
344344
}
345345

346+
Future<Query> Firestore::NamedQuery(const std::string& query_name) {
347+
if (!internal_) return FailedFuture<Query>();
348+
return internal_->NamedQuery(query_name);
349+
}
350+
346351
} // namespace firestore
347352
} // namespace firebase

firestore/src/include/firebase/firestore.h

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -429,6 +429,21 @@ class Firestore {
429429
const std::string& bundle,
430430
std::function<void(const LoadBundleTaskProgress&)> progress_callback);
431431

432+
/**
433+
* Reads a Firestore `Query` from the local cache, identified by the given
434+
* name.
435+
*
436+
* Named queries are packaged into bundles on the server side (along with the
437+
* resulting documents) and loaded into local cache using `LoadBundle`. Once
438+
* in the local cache, you can use this method to extract a query by name.
439+
*
440+
* If a query cannot be found, the returned future will complete with its
441+
* `error()` set to a non-zero error code.
442+
*
443+
* @param query_name The name of the query to read from saved bundles.
444+
*/
445+
virtual Future<Query> NamedQuery(const std::string& query_name);
446+
432447
protected:
433448
/**
434449
* Default constructor, to be used only for mocking `Firestore`.

firestore/src/main/firestore_main.cc

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -378,5 +378,22 @@ Future<LoadBundleTaskProgress> FirestoreInternal::LoadBundle(
378378
return promise.future();
379379
}
380380

381+
Future<Query> FirestoreInternal::NamedQuery(const std::string& query_name) {
382+
auto promise = promise_factory_.CreatePromise<Query>(AsyncApi::kNamedQuery);
383+
firestore_core_->GetNamedQuery(
384+
query_name,
385+
[this, promise](const absl::optional<core::Query>& query) mutable {
386+
if (query.has_value()) {
387+
promise.SetValue(
388+
MakePublic(api::Query(query.value(), firestore_core_)));
389+
} else {
390+
promise.SetError(
391+
Status(Error::kErrorNotFound, "Named query cannot be found"));
392+
}
393+
});
394+
395+
return promise.future();
396+
}
397+
381398
} // namespace firestore
382399
} // namespace firebase

firestore/src/main/firestore_main.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,7 @@ class FirestoreInternal {
100100
Future<LoadBundleTaskProgress> LoadBundle(
101101
const std::string& bundle,
102102
std::function<void(const LoadBundleTaskProgress&)> progress_callback);
103+
Future<Query> NamedQuery(const std::string& query_name);
103104

104105
// Manages the ListenerRegistrationInternal objects.
105106
void RegisterListenerRegistration(ListenerRegistrationInternal* registration);

0 commit comments

Comments
 (0)