Skip to content

Commit a350ed2

Browse files
authored
Add Query system tests, fix value mapping bugs (#520)
1 parent b4e9ca0 commit a350ed2

File tree

2 files changed

+35
-2
lines changed

2 files changed

+35
-2
lines changed

Database.php

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1086,6 +1086,35 @@ public function delete($table, KeySet $keySet, array $options = [])
10861086
* $transaction = $result->transaction();
10871087
* ```
10881088
*
1089+
* ```
1090+
* // Parameters which may be null must include an expected parameter type.
1091+
* $result = $database->execute('SELECT * FROM Posts WHERE lastModifiedTime = @timestamp', [
1092+
* 'parameters' => [
1093+
* 'timestamp' => $timestamp
1094+
* ],
1095+
* 'types' => [
1096+
* 'timestamp' => ValueMapper::TYPE_TIMESTAMP
1097+
* ]
1098+
* ]);
1099+
*
1100+
* $neverEditedPosts = $result->rows();
1101+
* ```
1102+
*
1103+
* ```
1104+
* // Array parameters which may be null or empty must include the array value type.
1105+
* $result = $database->execute('SELECT @emptyArrayOfIntegers as numbers', [
1106+
* 'parameters' => [
1107+
* 'emptyArrayOfIntegers' => []
1108+
* ],
1109+
* 'types' => [
1110+
* 'emptyArrayOfIntegers' => [ValueMapper::TYPE_ARRAY, ValueMapper::TYPE_INT64]
1111+
* ]
1112+
* ]);
1113+
*
1114+
* $row = $result->rows()->current();
1115+
* $emptyArray = $row['numbers'];
1116+
* ```
1117+
*
10891118
* @codingStandardsIgnoreStart
10901119
* @see https://cloud.google.com/spanner/reference/rpc/google.spanner.v1#google.spanner.v1.ExecuteSqlRequest ExecuteSqlRequest
10911120
* @codingStandardsIgnoreEnd

ValueMapper.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -242,7 +242,11 @@ private function decodeValue($value, array $type)
242242
break;
243243

244244
case self::TYPE_STRUCT:
245-
$value = $this->decodeValues($type['structType']['fields'], $value, Result::RETURN_ASSOCIATIVE);
245+
$fields = isset($type['structType']['fields'])
246+
? $type['structType']['fields']
247+
: [];
248+
249+
$value = $this->decodeValues($fields, $value, Result::RETURN_ASSOCIATIVE);
246250
break;
247251

248252
case self::TYPE_FLOAT64:
@@ -354,7 +358,7 @@ private function paramType($value, $givenType = null, $arrayType = null)
354358

355359
$type = $this->typeObject(
356360
self::TYPE_ARRAY,
357-
$this->typeObject((isset($types[0])) ? $types[0] : null),
361+
$this->typeObject((isset($types[0])) ? $types[0] : $arrayType),
358362
'arrayElementType'
359363
);
360364

0 commit comments

Comments
 (0)