Cloud Datastore Client - Class GqlQuery (1.29.2)

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
Name Description
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
Type Description
array

queryKey

Return the query_type union field name.

Returns
Type Description
string

aggregation

canPaginate

Indicate that this type does not support automatic pagination.

Returns
Type Description
bool

start

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

Parameter
Name Description
cursor string
Returns
Type Description
void

jsonSerialize

Define the json representation of the object.

Returns
Type Description
array

Constants

BINDING_NAMED

Value: 'namedBindings'

BINDING_POSITIONAL

Value: 'positionalBindings'