/**
* Run a query and return entities
*
* @param QueryInterface $query The query object.
* @param array $options [optional] {
* Configuration Options
*
* @type string $transaction The transaction ID, if the query should be
* run in a transaction.
* @type string $className The name of the class to return results as.
* Must be a subclass of {@see Google\Cloud\Datastore\Entity}.
* If not set, {@see Google\Cloud\Datastore\Entity} will be used.
* @type string $readConsistency See
* [ReadConsistency](https://cloud.google.com/datastore/reference/rest/v1/ReadOptions#ReadConsistency).
* }
* @return \Generator<Google\Cloud\Datastore\Entity>
*/
public function runQuery(QueryInterface $query, array $options = [])
{
$options += ['className' => null, 'namespaceId' => $this->namespaceId];
$moreResults = true;
do {
$request = $options + $this->readOptions($options) + ['projectId' => $this->projectId, 'partitionId' => $this->partitionId($this->projectId, $options['namespaceId']), $query->queryKey() => $query->queryObject()];
$res = $this->connection->runQuery($request);
if (isset($res['batch']['entityResults']) && is_array($res['batch']['entityResults'])) {
$results = $this->mapEntityResult($res['batch']['entityResults'], $options['className']);
foreach ($results as $result) {
(yield $result);
}
if ($query->canPaginate() && $res['batch']['moreResults'] === 'NOT_FINISHED') {
$query->start($res['batch']['endCursor']);
} else {
$moreResults = false;
}
} else {
$moreResults = false;
}
} while ($moreResults);
}