Skip to content

Commit ce07a14

Browse files
authored
feat(Datastore): add samples for multiple inequality (#2034)
1 parent d9fcf08 commit ce07a14

File tree

2 files changed

+93
-0
lines changed

2 files changed

+93
-0
lines changed
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
<?php
2+
/**
3+
* Copyright 2024 Google Inc.
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
/**
19+
* For instructions on how to run the full sample:
20+
*
21+
* @see https://github.com/GoogleCloudPlatform/php-docs-samples/tree/main/datastore/api/README.md
22+
*/
23+
24+
namespace Google\Cloud\Samples\Datastore;
25+
26+
use Google\Cloud\Datastore\DatastoreClient;
27+
use DateTime;
28+
29+
/**
30+
* Example of a query with range and inequality filters on multiple fields.
31+
* @see https://cloud.google.com/datastore/docs/multiple-range-fields
32+
*
33+
* @param string $namespaceId
34+
*/
35+
function query_filter_compound_multi_ineq(string $namespaceId = null): void
36+
{
37+
$datastore = new DatastoreClient(['namespaceId' => $namespaceId]);
38+
// [START datastore_query_filter_compound_multi_ineq]
39+
$query = $datastore->query()
40+
->kind('Task')
41+
->filter('priority', '>', 3)
42+
->filter('created', '>', new DateTime('1990-01-01T00:00:00z'));
43+
// [END datastore_query_filter_compound_multi_ineq]
44+
$result = $datastore->runQuery($query);
45+
$found = false;
46+
foreach ($result as $entity) {
47+
$found = true;
48+
printf(
49+
'Document %s returned by priority > 3 and created > 1990' . PHP_EOL,
50+
$entity->key()
51+
);
52+
}
53+
54+
if (!$found) {
55+
print("No records found.\n");
56+
}
57+
}
58+
59+
// The following 2 lines are only needed to run the samples
60+
require_once __DIR__ . '/../../../testing/sample_helpers.php';
61+
\Google\Cloud\Samples\execute_sample(__FILE__, __NAMESPACE__, $argv);

datastore/api/test/ConceptsTest.php

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -909,6 +909,38 @@ public function testPropertyFilteringRunQuery()
909909
});
910910
}
911911

912+
public function testChainedInequalityQuery()
913+
{
914+
// This will show in the query
915+
$key1 = self::$datastore->key('Task', $this->generateRandomString());
916+
$entity1 = self::$datastore->entity($key1);
917+
$entity1['priority'] = 4;
918+
$entity1['created'] = new \DateTime();
919+
920+
// These will not show in the query
921+
$key2 = self::$datastore->key('Task', $this->generateRandomString());
922+
$entity2 = self::$datastore->entity($key2);
923+
$entity2['priority'] = 2;
924+
$entity2['created'] = new \DateTime();
925+
926+
$key3 = self::$datastore->key('Task', $this->generateRandomString());
927+
$entity3 = self::$datastore->entity($key3);
928+
$entity3['priority'] = 4;
929+
$entity3['created'] = new \DateTime('1989');
930+
931+
self::$keys = [$key1, $key2, $key3];
932+
self::$datastore->upsertBatch([$entity1, $entity2, $entity3]);
933+
934+
$output = $this->runFunctionSnippet('query_filter_compound_multi_ineq', [self::$namespaceId]);
935+
$this->assertStringContainsString(sprintf(
936+
'Document %s returned by priority > 3 and created > 1990',
937+
$key1
938+
), $output);
939+
940+
$this->assertStringNotContainsString((string) $key2, $output);
941+
$this->assertStringNotContainsString((string) $key3, $output);
942+
}
943+
912944
public function tearDown(): void
913945
{
914946
if (! empty(self::$keys)) {

0 commit comments

Comments
 (0)