Cloud Datastore Client - Class GqlQuery (1.26.0)

Reference documentation and code samples for the Cloud Datastore Client class GqlQuery.

Query Google Cloud Datastore using GQL.

By default, parameters MUST be bound using named or positional bindings. Literals are disabled by default, and must be enabled by setting $options['allowLiterals'] to true. As with any SQL dialect, using parameter binding is highly recommended.

Idiomatic usage is via Google\Cloud\Datastore\DatastoreClient::gqlQuery().

Example:

use Google\Cloud\Datastore\DatastoreClient;

$datastore = new DatastoreClient();

$query = $datastore->gqlQuery('SELECT * FROM Companies');
$res = $datastore->runQuery($query);

foreach ($res as $company) {
    echo $company['companyName'] . PHP_EOL;
}
// Literals must be provided as bound parameters by default:
$query = $datastore->gqlQuery('SELECT * FROM Companies WHERE companyName = @companyName', [
    'bindings' => [
        'companyName' => 'Google'
    ]
]);
// Positional binding is also supported:
$query = $datastore->gqlQuery('SELECT * FROM Companies WHERE companyName = @1 LIMIT 1', [
    'bindings' => [
        'Google'
    ]
]);
// While not recommended, you can use literals in your query string:
$query = $datastore->gqlQuery('SELECT * FROM Companies WHERE companyName = \'Google\'', [
    'allowLiterals' => true
]);
// Using cursors as query bindings:
$cursor = $datastore->cursor($cursorValue);

$query = $datastore->gqlQuery('SELECT * FROM Companies OFFSET @offset', [
    'bindings' => [
        'offset' => $cursor
    ]
]);

Namespace

Google \ Cloud \ Datastore \ Query

Methods

__construct

Parameters
NameDescription
entityMapper Google\Cloud\Datastore\EntityMapper

An instance of EntityMapper

query string

The GQL Query string.

options array

Configuration Options

↳ allowLiterals bool

Whether literal values will be allowed in the query string. Parameter binding is strongly encouraged over literals. Defaults to false.

↳ bindings array

An array of values to bind to the query string. Queries using Named Bindings should provide a key/value set, while queries using Positional Bindings must provide a simple array. Applications with no need for multitenancy should not set this value.

queryObject

Format the query for use in the API.

Returns
TypeDescription
array

queryKey

Return the query_type union field name.

Returns
TypeDescription
string

aggregation

canPaginate

Indicate that this type does not support automatic pagination.

Returns
TypeDescription
bool

start

Fulfill the interface, but cursors are handled inside the query string.

Parameter
NameDescription
cursor string
Returns
TypeDescription
void

jsonSerialize

Define the json representation of the object.

Returns
TypeDescription
array

Constants

BINDING_NAMED

Value: 'namedBindings'

BINDING_POSITIONAL

Value: 'positionalBindings'