Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 29 additions & 0 deletions src/Spanner/Database.php
Original file line number Diff line number Diff line change
Expand Up @@ -1086,6 +1086,35 @@ public function delete($table, KeySet $keySet, array $options = [])
* $transaction = $result->transaction();
* ```
*
* ```
* // Parameters which may be null must include an expected parameter type.
* $result = $database->execute('SELECT * FROM Posts WHERE lastModifiedTime = @timestamp', [
* 'parameters' => [
* 'timestamp' => $timestamp
* ],
* 'types' => [
* 'timestamp' => ValueMapper::TYPE_TIMESTAMP
* ]
* ]);
*
* $neverEditedPosts = $result->rows();
* ```
*
* ```
* // Array parameters which may be null or empty must include the array value type.
* $result = $database->execute('SELECT @emptyArrayOfIntegers as numbers', [
* 'parameters' => [
* 'emptyArrayOfIntegers' => []
* ],
* 'types' => [
* 'emptyArrayOfIntegers' => [ValueMapper::TYPE_ARRAY, ValueMapper::TYPE_INT64]
* ]
* ]);
*
* $row = $result->rows()->current();
* $emptyArray = $row['numbers'];
* ```
*
* @codingStandardsIgnoreStart
* @see https://cloud.google.com/spanner/reference/rpc/google.spanner.v1#google.spanner.v1.ExecuteSqlRequest ExecuteSqlRequest
* @codingStandardsIgnoreEnd
Expand Down
8 changes: 6 additions & 2 deletions src/Spanner/ValueMapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -242,7 +242,11 @@ private function decodeValue($value, array $type)
break;

case self::TYPE_STRUCT:
$value = $this->decodeValues($type['structType']['fields'], $value, Result::RETURN_ASSOCIATIVE);
$fields = isset($type['structType']['fields'])
? $type['structType']['fields']
: [];

$value = $this->decodeValues($fields, $value, Result::RETURN_ASSOCIATIVE);
break;

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

$type = $this->typeObject(
self::TYPE_ARRAY,
$this->typeObject((isset($types[0])) ? $types[0] : null),
$this->typeObject((isset($types[0])) ? $types[0] : $arrayType),
'arrayElementType'
);

Expand Down
73 changes: 73 additions & 0 deletions tests/snippets/Spanner/DatabaseTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -659,6 +659,79 @@ public function testExecuteBeginTransaction()
$this->assertInstanceOf(Transaction::class, $res->returnVal()->transaction());
}

public function testExecuteWithParameterType()
{
$this->connection->executeStreamingSql(Argument::that(function ($arg) {
if (!isset($arg['params'])) return false;
if (!isset($arg['paramTypes'])) return false;
if ($arg['paramTypes']['timestamp']['code'] !== ValueMapper::TYPE_TIMESTAMP) return false;

return true;
}))->shouldBeCalled()->willReturn($this->resultGenerator([
'metadata' => [
'rowType' => [
'fields' => [
[
'name' => 'lastModifiedTime',
'type' => [
'code' => ValueMapper::TYPE_TIMESTAMP
]
]
]
]
],
'values' => [null]
]));

$this->stubOperation();

$snippet = $this->snippetFromMethod(Database::class, 'execute', 3);
$snippet->addLocal('database', $this->database);
$snippet->addLocal('timestamp', null);
$snippet->addUse(ValueMapper::class);

$res = $snippet->invoke('neverEditedPosts');
$this->assertNull($res->returnVal()->current()['lastModifiedTime']);
}

public function testExecuteWithEmptyArray()
{
$this->connection->executeStreamingSql(Argument::that(function ($arg) {
if (!isset($arg['params'])) return false;
if (!isset($arg['paramTypes'])) return false;
if ($arg['paramTypes']['emptyArrayOfIntegers']['code'] !== ValueMapper::TYPE_ARRAY) return false;
if ($arg['paramTypes']['emptyArrayOfIntegers']['arrayElementType']['code'] !== ValueMapper::TYPE_INT64) return false;

return true;
}))->shouldBeCalled()->willReturn($this->resultGenerator([
'metadata' => [
'rowType' => [
'fields' => [
[
'name' => 'numbers',
'type' => [
'code' => ValueMapper::TYPE_ARRAY,
'arrayElementType' => [
'code' => ValueMapper::TYPE_INT64
]
]
]
]
]
],
'values' => [[]]
]));

$this->stubOperation();

$snippet = $this->snippetFromMethod(Database::class, 'execute', 4);
$snippet->addLocal('database', $this->database);
$snippet->addUse(ValueMapper::class);

$res = $snippet->invoke('emptyArray');
$this->assertEmpty($res->returnVal());
}

public function testRead()
{
$this->connection->streamingRead(Argument::any())
Expand Down
Loading