/** * Retrieves the rows associated with the query and merges them together * with the table's schema. It is recommended to check the completeness of * the query before attempting to access rows. * * Refer to the table below for a guide on how BigQuery types are mapped as * they come back from the API. * * | **PHP Type** | **BigQuery Data Type** | * |--------------------------------------------|--------------------------------------| * | `\DateTimeInterface` | `DATETIME` | * | {@see Google\Cloud\BigQuery\Bytes} | `BYTES` | * | {@see Google\Cloud\BigQuery\Date} | `DATE` | * | {@see Google\Cloud\Int64} | `INTEGER` | * | {@see Google\Cloud\BigQuery\Time} | `TIME` | * | {@see Google\Cloud\BigQuery\Timestamp} | `TIMESTAMP` | * | Associative Array | `RECORD` | * | Non-Associative Array | `RECORD` (Repeated) | * | `float` | `FLOAT` | * | `int` | `INTEGER` | * | `string` | `STRING` | * | `bool` | `BOOLEAN` | * * Example: * ``` * $isComplete = $queryResults->isComplete(); * * if ($isComplete) { * $rows = $queryResults->rows(); * * foreach ($rows as $row) { * echo $row['name'] . PHP_EOL; * } * } * ``` * * @param array $options [optional] Configuration options. * @return array * @throws GoogleException Thrown if the query has not yet completed. */ public function rows(array $options = []) { if (!$this->isComplete()) { throw new GoogleException('The query has not completed yet.'); } if (!isset($this->info['rows'])) { return; } $schema = $this->info['schema']['fields']; while (true) { $options['pageToken'] = isset($this->info['pageToken']) ? $this->info['pageToken'] : null; foreach ($this->info['rows'] as $row) { $mergedRow = []; if ($row === null) { continue; } if (!array_key_exists('f', $row)) { throw new GoogleException('Bad response - missing key "f" for a row.'); } foreach ($row['f'] as $key => $value) { $fieldSchema = $schema[$key]; $mergedRow[$fieldSchema['name']] = $this->mapper->fromBigQuery($value, $fieldSchema); } (yield $mergedRow); } if (!$options['pageToken']) { return; } $this->info = $this->connection->getQueryResults($options + $this->identity); } }
/** * Formats query parameters for the API. * * @param array $parameters The parameters to format. * @return array */ private function formatQueryParameters(array $parameters) { $options = ['parameterMode' => $this->isAssoc($parameters) ? 'named' : 'positional', 'useLegacySql' => false]; foreach ($parameters as $name => $value) { $param = $this->mapper->toParameter($value); if ($options['parameterMode'] === 'named') { $param += ['name' => $name]; } $options['queryParameters'][] = $param; } return $options; }
/** * @dataProvider parameterValueProvider */ public function testMapsToParameter($value, $expected) { if (is_resource($value)) { rewind($value); } $mapper = new ValueMapper(false); $actual = $mapper->toParameter($value); $this->assertEquals($expected, $actual); }