Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

docs: Added additional docs for executeUpdate #2269

Merged
merged 14 commits into from
Aug 29, 2019
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
28 changes: 27 additions & 1 deletion Spanner/src/Transaction.php
Original file line number Diff line number Diff line change
Expand Up @@ -312,7 +312,9 @@ public function delete($table, KeySet $keySet)
*
* Data Manipulation Language (DML) allows you to execute statements which
* modify the state of the database (i.e. inserting, updating or deleting
* rows).
* rows). DML supports INSERT, UPDATE and DELETE statements. For
* more on DML syntax, visit the
* [DML syntax guide](https://cloud.google.com/spanner/docs/dml-syntax).
*
* To execute a SQL query (such as a SELECT), use
* {@see Google\Cloud\Spanner\Transaction::execute()}.
Expand All @@ -333,8 +335,32 @@ public function delete($table, KeySet $keySet)
* ]);
* ```
*
* ```
* // Example of executeUpdate while using DML Structs
* $statement = "UPDATE Posts SET title = 'Updated Title' " .
* "WHERE STRUCT<Title STRING, Content STRING>(Title, Content) = @post";
*
* $postValue = new StructValue();
* $postValue->add('Title', 'Updated Title')
* ->add('Content', 'Sample Content');
*
* $postType = new StructType();
* $postType->add('Title', Database::TYPE_STRING)
* ->add('Content', Database::TYPE_STRING);
*
* $modifiedRowCount = $transaction->executeUpdate($statement, [
* 'parameters' => [
* 'post' => $postValue
* ],
* 'types' => [
* 'post' => $postType
* ]
* ]);
* ```
*
* @codingStandardsIgnoreStart
* @see https://cloud.google.com/spanner/reference/rpc/google.spanner.v1#google.spanner.v1.ExecuteSqlRequest ExecuteSqlRequest
* @see https://cloud.google.com/spanner/docs/dml-syntax DML Syntax Guide
* @codingStandardsIgnoreEnd
*
* @param string $sql The query string to execute.
Expand Down
57 changes: 57 additions & 0 deletions Spanner/tests/Snippet/TransactionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@
use Google\Cloud\Spanner\Operation;
use Google\Cloud\Spanner\Result;
use Google\Cloud\Spanner\Session\Session;
use Google\Cloud\Spanner\StructType;
use Google\Cloud\Spanner\StructValue;
use Google\Cloud\Spanner\Tests\OperationRefreshTrait;
use Google\Cloud\Spanner\Tests\ResultGeneratorTrait;
use Google\Cloud\Spanner\Timestamp;
Expand Down Expand Up @@ -116,6 +118,61 @@ public function testExecuteUpdate()
$this->assertEquals(1, $res->returnVal());
}

public function testExecuteUpdateWithStruct()
{
$expectedSql = "UPDATE Posts SET title = 'Updated Title' WHERE " .
"STRUCT<Title STRING, Content STRING>(Title, Content) = @post";

$expectedParams = [
'post' => ["Updated Title", "Sample Content"]
];
$expectedStructData = [
[
"name" => "Title",
"type" => [
"code" => Database::TYPE_STRING
]
],
[
"name" => "Content",
"type" => [
"code" => Database::TYPE_STRING
]
]
];

$this->connection->executeStreamingSql(
Argument::allOf(
Argument::withEntry('sql', $expectedSql),
Argument::withEntry('params', $expectedParams),
Argument::withEntry(
'paramTypes',
Argument::withEntry(
'post',
Argument::withEntry(
'structType',
Argument::withEntry('fields', Argument::is($expectedStructData))
)
)
)
)
)
->shouldBeCalled()
->willReturn($this->resultGenerator(true));

$this->refreshOperation($this->transaction, $this->connection->reveal());

$snippet = $this->snippetFromMethod(Transaction::class, 'executeUpdate', 1);
$snippet->addUse(Database::class);
$snippet->addUse(StructType::class);
$snippet->addUse(StructValue::class);

$snippet->addLocal('transaction', $this->transaction);
$res = $snippet->invoke('modifiedRowCount');

$this->assertEquals(1, $res->returnVal());
}

public function testExecuteUpdateBatch()
{
$this->connection->executeBatchDml(Argument::any())
Expand Down