public fromBigQuery ( array $value, array $schema ) | ||
$value | array | The value to map. |
$schema | array | The schema describing the value. |
public function fromBigQuery(array $value, array $schema)
{
$value = $value['v'];
if (isset($schema['mode'])) {
if ($schema['mode'] === 'REPEATED') {
return $this->repeatedValueFromBigQuery($value, $schema);
}
if ($schema['mode'] === 'NULLABLE' && $value === null) {
return $value;
}
}
switch ($schema['type']) {
case self::TYPE_BOOLEAN:
return $value === 'true' ? true : false;
case self::TYPE_INTEGER:
return $this->returnInt64AsObject ? new Int64($value) : (int) $value;
case self::TYPE_FLOAT:
return (double) $value;
case self::TYPE_STRING:
return (string) $value;
case self::TYPE_BYTES:
return new Bytes(base64_decode($value));
case self::TYPE_DATE:
return new Date(new \DateTime($value));
case self::TYPE_DATETIME:
return new \DateTime($value);
case self::TYPE_TIME:
return new Time(new \DateTime($value));
case self::TYPE_TIMESTAMP:
$timestamp = new \DateTime();
$timestamp->setTimestamp((double) $value);
return new Timestamp($timestamp);
case self::TYPE_RECORD:
return $this->recordFromBigQuery($value, $schema['fields']);
default:
throw new \InvalidArgumentException(sprintf('Unrecognized value type %s. Please ensure you are using the latest version of google/cloud.', $schema['type']));
break;
}
}
/** * 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); } }