Skip to content

feat: Add last statement option samples #1341

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

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
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
34 changes: 33 additions & 1 deletion samples/samples/pg_snippets.py
Original file line number Diff line number Diff line change
Expand Up @@ -1732,6 +1732,35 @@ def drop_sequence(instance_id, database_id):

# [END spanner_postgresql_drop_sequence]

# [START spanner_postgres_dml_last_statement]
def dml_last_statement_option(instance_id, database_id):
"""Inserts and updates using DML where the update set the last statement option."""
# instance_id = "your-spanner-instance"
# database_id = "your-spanner-db-id"

spanner_client = spanner.Client()
instance = spanner_client.instance(instance_id)
database = instance.database(database_id)

def insert_and_update_singers(transaction):
insert_row_ct = transaction.execute_update(
"INSERT INTO Singers (SingerId, FirstName, LastName) "
" VALUES (54214, 'John', 'Do')"
)

print("{} record(s) inserted.".format(insert_row_ct))

update_row_ct = transaction.execute_update(
"UPDATE Singers SET LastName = 'Doe' WHERE SingerId = 54214",
last_statement=True)

print("{} record(s) updated.".format(update_row_ct))

database.run_in_transaction(insert_and_update_singers)


# [END spanner_postgres_dml_last_statement]

if __name__ == "__main__": # noqa: C901
parser = argparse.ArgumentParser(
description=__doc__, formatter_class=argparse.RawDescriptionHelpFormatter
Expand Down Expand Up @@ -1778,7 +1807,7 @@ def drop_sequence(instance_id, database_id):
subparsers.add_parser("insert_data_with_dml", help=insert_data_with_dml.__doc__)
subparsers.add_parser("update_data_with_dml", help=update_data_with_dml.__doc__)
subparsers.add_parser(
"update_data_with_dml", help=update_data_with_dml_returning.__doc__
"update_data_with_dml_returning", help=update_data_with_dml_returning.__doc__
)
subparsers.add_parser("delete_data_with_dml", help=delete_data_with_dml.__doc__)
subparsers.add_parser(
Expand Down Expand Up @@ -1837,6 +1866,7 @@ def drop_sequence(instance_id, database_id):
subparsers.add_parser("create_sequence", help=create_sequence.__doc__)
subparsers.add_parser("alter_sequence", help=alter_sequence.__doc__)
subparsers.add_parser("drop_sequence", help=drop_sequence.__doc__)
subparsers.add_parser("dml_last_statement_option", help=dml_last_statement_option.__doc__)

args = parser.parse_args()

Expand Down Expand Up @@ -1932,3 +1962,5 @@ def drop_sequence(instance_id, database_id):
query_data_with_query_options(args.instance_id, args.database_id)
elif args.command == "create_client_with_query_options":
create_client_with_query_options(args.instance_id, args.database_id)
elif args.command == "dml_last_statement_option":
dml_last_statement_option(args.instance_id, args.database_id)
6 changes: 6 additions & 0 deletions samples/samples/pg_snippets_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -512,3 +512,9 @@ def test_drop_sequence(capsys, instance_id, bit_reverse_sequence_database):
"Altered Customers table to drop DEFAULT from CustomerId column and dropped the Seq sequence on database"
in out
)

def test_dml_last_statement_option(capsys, instance_id, sample_database):
snippets.dml_last_statement_option(instance_id, sample_database.database_id)
out, _ = capsys.readouterr()
assert "1 record(s) inserted." in out
assert "1 record(s) updated." in out
2 changes: 1 addition & 1 deletion samples/samples/requirements.txt
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
google-cloud-spanner==3.50.0
google-cloud-spanner==3.53.0
futures==3.4.0; python_version < "3"
29 changes: 29 additions & 0 deletions samples/samples/snippets.py
Original file line number Diff line number Diff line change
Expand Up @@ -3594,6 +3594,32 @@ def add_split_points(instance_id, database_id):

# [END spanner_database_add_split_points]

def dml_last_statement_option(instance_id, database_id):
"""Inserts and updates using DML where the update set the last statement option."""
# [START spanner_dml_last_statement]
# instance_id = "your-spanner-instance"
# database_id = "your-spanner-db-id"

spanner_client = spanner.Client()
instance = spanner_client.instance(instance_id)
database = instance.database(database_id)

def insert_and_update_singers(transaction):
insert_row_ct = transaction.execute_update(
"INSERT INTO Singers (SingerId, FirstName, LastName) "
" VALUES (54213, 'John', 'Do')"
)

print("{} record(s) inserted.".format(insert_row_ct))

update_row_ct = transaction.execute_update(
"UPDATE Singers SET LastName = 'Doe' WHERE SingerId = 54213",
last_statement=True)

print("{} record(s) updated.".format(update_row_ct))

database.run_in_transaction(insert_and_update_singers)
# [END spanner_dml_last_statement]

if __name__ == "__main__": # noqa: C901
parser = argparse.ArgumentParser(
Expand Down Expand Up @@ -3756,6 +3782,7 @@ def add_split_points(instance_id, database_id):
"add_split_points",
help=add_split_points.__doc__,
)
subparsers.add_parser("dml_last_statement_option", help=dml_last_statement_option.__doc__)

args = parser.parse_args()

Expand Down Expand Up @@ -3907,3 +3934,5 @@ def add_split_points(instance_id, database_id):
query_data_with_proto_types_parameter(args.instance_id, args.database_id)
elif args.command == "add_split_points":
add_split_points(args.instance_id, args.database_id)
elif args.command == "dml_last_statement_option":
dml_last_statement_option(args.instance_id, args.database_id)
7 changes: 7 additions & 0 deletions samples/samples/snippets_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -1016,3 +1016,10 @@ def test_add_split_points(capsys, instance_id, sample_database):
snippets.add_split_points(instance_id, sample_database.database_id)
out, _ = capsys.readouterr()
assert "Added split points." in out


def test_dml_last_statement_option(capsys, instance_id, sample_database):
snippets.dml_last_statement_option(instance_id, sample_database.database_id)
out, _ = capsys.readouterr()
assert "1 record(s) inserted." in out
assert "1 record(s) updated." in out