Maps a value coming from BigQuery to the expected format for use in the
library.
public fromBigQuery ( array $value, array $schema ) | ||
$value | array | The value to map. |
$schema | array | The schema describing the value. |
/** * 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); } }
public function testMapsBytesFromBigQuery() { $mapper = new ValueMapper(false); $actual = $mapper->fromBigQuery(['v' => base64_encode('abcd')], ['type' => 'BYTES']); $this->assertEquals((string) new Bytes('abcd'), (string) $actual); }