Cloud Datastore Client - Class GqlQuery (1.23.0)

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

Query Google Cloud Datastore usingGQL.

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

Idiomatic usage is viaGoogle\Cloud\Datastore\Query\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
entityMapperGoogle\Cloud\Datastore\EntityMapper

An instance of EntityMapper

querystring

The GQL Query string.

optionsarray

Configuration Options

↳ allowLiteralsbool

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

↳ bindingsarray

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
cursorstring
Returns
TypeDescription
void

jsonSerialize

Define the json representation of the object.

Returns
TypeDescription
array

Constants

BINDING_NAMED

Value: 'namedBindings'

BINDING_POSITIONAL

Value: 'positionalBindings'

Except as otherwise noted, the content of this page is licensed under theCreative Commons Attribution 4.0 License, and code samples are licensed under theApache 2.0 License. For details, see theGoogle Developers Site Policies. Java is a registered trademark of Oracle and/or its affiliates.

Last updated 2026-01-24 UTC.