Keys are considered literals in Cloud Datastore GQL and require special handling.
If users will be providing values at runtime, we recommend using argument binding. This helps prevent malicious behavior such as injection attacks.
There are two ways to do this; both start with a key value:
$key_path_element = new Google_Service_Datastore_KeyPathElement();
$key_path_element->setKind('notification');
$key_path_element->setId(1410611039);
$key = new Google_Service_Datastore_Key();
$key.setPath([$key_path_element]);
$key_value = new Google_Service_Datastore_Value();
$key_value->setKeyValue($key);
$key_value
can then be used either as a named argument:
$gql_query = new Google_Service_Datastore_GqlQuery();
$gql_query->setQueryString("SELECT * FROM notification WHERE __key__ = $theKey");
$name_arg = new Google_Service_Datastore_GqlQueryArg();
$name_arg->setName("theKey");
$name_arg->setValue($key_value);
$gql_query->setNameArgs([$name_arg]);
or as a positional argument:
$gql_query = new Google_Service_Datastore_GqlQuery();
$gql_query->setQueryString("SELECT * FROM notification WHERE __key__ = @1");
$number_arg = new Google_Service_Datastore_GqlQueryArg();
$number_arg->setValue($key_value);
$gql_query->setNumberArgs([$number_arg]);
If no user-provided input is being added to the query, another option is to explicitly allow literals in the request:
$gql_query = new Google_Service_Datastore_GqlQuery();
$gql_query->setQueryString("SELECT * FROM notification WHERE __key__ = KEY('notification', 1410611039)");
$gql_query->setAllowLiteral(true);
Here are some additional details on argument binding and here is the full GQL reference.
notification
without the quotes? – Daniel Rosemannotification
without the quotes, but still the same error. Unfortunately, there is no guide for Datastore API on how to make a Gql-string. I thinkKEY('kind', 'name/id')
is a method for making a Datastore key, but it's not working via the API. – Behnam Rasooli