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 2 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). For example, DML supports INSERT, UPDATE and DELETE statements. For
danielgsims marked this conversation as resolved.
Show resolved Hide resolved
* more on DML syntax, visit the DML syntax guide.
danielgsims marked this conversation as resolved.
Show resolved Hide resolved
* {@see https://cloud.google.com/spanner/docs/dml-syntax}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Move this down near line 363. URLs in the doc text should be formatted as markdown.

*
* To execute a SQL query (such as a SELECT), use
* {@see Google\Cloud\Spanner\Transaction::execute()}.
Expand All @@ -333,7 +335,31 @@ public function delete($table, KeySet $keySet)
* ]);
* ```
*
* ```
* //Example of executeUpdate while using DML Structs
danielgsims marked this conversation as resolved.
Show resolved Hide resolved
* $statement = "UPDATE Posts SET title = 'Updated Title' " .
* "WHERE STRUCT<Title STRING, Content STRING>(Title, Content) = @post";
danielgsims marked this conversation as resolved.
Show resolved Hide resolved
*
* $postValue = new StructValue;
danielgsims marked this conversation as resolved.
Show resolved Hide resolved
* $postValue->add('Title', 'Updated Title');
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This seems like a pretty nice opportunity to use the fluent interface.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For sure! How does this look? Would you rather go all-the-way with (new StructValue())->add()->add()?

* $postValue->add('Content', 'Sample Content');
*
* $postType = new StructType;
danielgsims marked this conversation as resolved.
Show resolved Hide resolved
* $postType->add('Title', Database::TYPE_STRING);
* $postType->add('Title', Database::TYPE_STRING);
*
* $modifiedRowCount = $transaction->executeUpdate($statement, [
* 'parameters' => [
* 'post' => $postValue
* ],
* 'types' => [
* 'post' => $postType
* ]
* ]);
* ```
*
* @codingStandardsIgnoreStart
*
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

remove empty line.

* @see https://cloud.google.com/spanner/reference/rpc/google.spanner.v1#google.spanner.v1.ExecuteSqlRequest ExecuteSqlRequest
* @codingStandardsIgnoreEnd
*
Expand Down
52 changes: 52 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,56 @@ 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";
danielgsims marked this conversation as resolved.
Show resolved Hide resolved

$expectedParams = [
'post' => ["Updated Title", "Sample Content"]
];
$expectedStructData = [
"fields" => [
[
"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', $expectedStructData))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you can assert on nested array data? cool! TIL.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah! I found an example someone in phpspec, I'll send it to you if I can find it again.

)
))
->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