-
Notifications
You must be signed in to change notification settings - Fork 438
Change test case terminology and create sample for snapshot isolation #15895
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
Closed
prathmeshcc
wants to merge
1
commit into
googleapis:main
from
prathmeshcc:fix/isolation-level-terminology
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -22,6 +22,7 @@ | |
| #include "google/cloud/spanner/backoff_policy.h" | ||
| #include "google/cloud/spanner/backup.h" | ||
| #include "google/cloud/spanner/create_instance_request_builder.h" | ||
| #include "google/cloud/spanner/options.h" | ||
| #include "google/cloud/spanner/row.h" | ||
| #include "google/cloud/spanner/testing/debug_log.h" // TODO(#4758): remove | ||
| #include "google/cloud/spanner/testing/instance_location.h" | ||
|
|
@@ -3472,6 +3473,63 @@ void ReadWriteTransaction(google::cloud::spanner::Client client) { | |
| } | ||
| //! [END spanner_read_write_transaction] | ||
|
|
||
| //! [START spanner_isolation_level] [spanner_isolation_level] | ||
| void IsolationLevelSetting(std::string const& project_id, | ||
| std::string const& instance_id, | ||
| std::string const& database_id) { | ||
| namespace spanner = ::google::cloud::spanner; | ||
| using ::google::cloud::Options; | ||
| using ::google::cloud::StatusOr; | ||
|
|
||
| auto db = spanner::Database(project_id, instance_id, database_id); | ||
|
|
||
| // The isolation level specified at the client-level will be applied | ||
| // to all RW transactions. | ||
| auto options = Options{}.set<spanner::TransactionIsolationLevelOption>( | ||
| spanner::Transaction::IsolationLevel::kSerializable); | ||
| auto client = spanner::Client(spanner::MakeConnection(db, options)); | ||
|
|
||
| auto commit = client.Commit( | ||
| [&client](spanner::Transaction const& txn) -> StatusOr<spanner::Mutations> { | ||
| // Read an AlbumTitle. | ||
| auto sql = spanner::SqlStatement( | ||
| "SELECT AlbumTitle from Albums WHERE SingerId = 1 and AlbumId = 1"); | ||
| auto rows = client.ExecuteQuery(txn, std::move(sql)); | ||
| for (auto const& row : spanner::StreamOf<std::tuple<std::string>>(rows)) { | ||
| if (!row) return row.status(); | ||
| std::cout << "Current album title: " << std::get<0>(*row) << "\n"; | ||
| } | ||
|
|
||
| // Update the title. | ||
| auto update_sql = spanner::SqlStatement( | ||
| "UPDATE Albums " | ||
| "SET AlbumTitle = 'New Album Title' " | ||
| "WHERE SingerId = 1 and AlbumId = 1"); | ||
|
Comment on lines
+3504
to
+3507
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Similar to the SELECT statement, it's recommended to use parameters for DML statements to prevent SQL injection and improve code clarity. auto update_sql = spanner::SqlStatement(
"UPDATE Albums "
"SET AlbumTitle = @AlbumTitle "
"WHERE SingerId = @SingerId and AlbumId = @AlbumId",
{{"AlbumTitle", spanner::Value("New Album Title")},
{"SingerId", spanner::Value(1)},
{"AlbumId", spanner::Value(1)}}); |
||
| auto result = client.ExecuteDml(txn, std::move(update_sql)); | ||
| if (!result) return result.status(); | ||
| std::cout << result->RowsModified() << " record updated.\n"; | ||
|
|
||
| return spanner::Mutations{}; | ||
| }, | ||
| // The isolation level specified at the transaction-level takes | ||
| // precedence over the isolation level configured at the client-level. | ||
| // kRepeatableRead is used here to demonstrate overriding the client-level setting. | ||
| Options{}.set<spanner::TransactionIsolationLevelOption>( | ||
| spanner::Transaction::IsolationLevel::kRepeatableRead)); | ||
|
|
||
| if (!commit) throw std::move(commit).status(); | ||
| std::cout << "Update was successful [spanner_isolation_level]\n"; | ||
| } | ||
| //! [END spanner_isolation_level] [spanner_isolation_level] | ||
|
|
||
| void IsolationLevelSettingCommand(std::vector<std::string> argv) { | ||
| if (argv.size() != 3) { | ||
| throw std::runtime_error( | ||
| "isolation-level-setting <project-id> <instance-id> <database-id>"); | ||
| } | ||
| IsolationLevelSetting(argv[0], argv[1], argv[2]); | ||
| } | ||
|
|
||
| //! [START spanner_get_commit_stats] | ||
| void GetCommitStatistics(google::cloud::spanner::Client client) { | ||
| namespace spanner = ::google::cloud::spanner; | ||
|
|
@@ -5190,6 +5248,7 @@ int RunOneCommand(std::vector<std::string> argv) { | |
| make_command_entry("read-data-with-storing-index", | ||
| ReadDataWithStoringIndex), | ||
| make_command_entry("read-write-transaction", ReadWriteTransaction), | ||
| {"isolation-level-setting", IsolationLevelSettingCommand}, | ||
| make_command_entry("get-commit-stats", GetCommitStatistics), | ||
| make_command_entry("dml-standard-insert", DmlStandardInsert), | ||
| make_command_entry("dml-standard-update", DmlStandardUpdate), | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For improved security and to prevent SQL injection vulnerabilities, it's a best practice to use query parameters instead of embedding literal values directly into the SQL string. This also improves readability and maintainability.