Skip to content

Commit ee09805

Browse files
sgorse123sgorse12
andauthored
feat: Add last statement option samples (#16499)
This adds a sample for the last statement option. Support for the option was added in #1313 by @olavloite. --------- Co-authored-by: Shirdon Gorse <shirdon@google.com>
1 parent 7bb29e5 commit ee09805

4 files changed

Lines changed: 85 additions & 3 deletions

File tree

packages/google-cloud-spanner/samples/samples/pg_snippets.py

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1080,6 +1080,35 @@ def update_albums(transaction):
10801080
# [END spanner_postgresql_dml_batch_update]
10811081

10821082

1083+
def dml_last_statement_option(instance_id, database_id):
1084+
"""Inserts and updates using DML where the update sets the last statement option."""
1085+
# [START spanner_postgresql_dml_last_statement]
1086+
# instance_id = "your-spanner-instance"
1087+
# database_id = "your-spanner-db-id"
1088+
1089+
spanner_client = spanner.Client()
1090+
instance = spanner_client.instance(instance_id)
1091+
database = instance.database(database_id)
1092+
1093+
def insert_and_update_singers(transaction):
1094+
insert_row_ct = transaction.execute_update(
1095+
"INSERT INTO Singers (SingerId, FirstName, LastName) "
1096+
"VALUES (54214, 'John', 'Do')"
1097+
)
1098+
1099+
print("{} record(s) inserted.".format(insert_row_ct))
1100+
1101+
update_row_ct = transaction.execute_update(
1102+
"UPDATE Singers SET LastName = 'Doe' WHERE SingerId = 54214",
1103+
last_statement=True,
1104+
)
1105+
1106+
print("{} record(s) updated.".format(update_row_ct))
1107+
1108+
database.run_in_transaction(insert_and_update_singers)
1109+
# [END spanner_postgresql_dml_last_statement]
1110+
1111+
10831112
def create_table_with_datatypes(instance_id, database_id):
10841113
"""Creates a table with supported datatypes."""
10851114
# [START spanner_postgresql_create_table_with_datatypes]
@@ -1768,7 +1797,7 @@ def drop_sequence(instance_id, database_id):
17681797
subparsers.add_parser("insert_data_with_dml", help=insert_data_with_dml.__doc__)
17691798
subparsers.add_parser("update_data_with_dml", help=update_data_with_dml.__doc__)
17701799
subparsers.add_parser(
1771-
"update_data_with_dml", help=update_data_with_dml_returning.__doc__
1800+
"update_data_with_dml_returning", help=update_data_with_dml_returning.__doc__
17721801
)
17731802
subparsers.add_parser("delete_data_with_dml", help=delete_data_with_dml.__doc__)
17741803
subparsers.add_parser(
@@ -1796,6 +1825,9 @@ def drop_sequence(instance_id, database_id):
17961825
help=delete_data_with_partitioned_dml.__doc__,
17971826
)
17981827
subparsers.add_parser("update_with_batch_dml", help=update_with_batch_dml.__doc__)
1828+
subparsers.add_parser(
1829+
"dml_last_statement_option", help=dml_last_statement_option.__doc__
1830+
)
17991831
subparsers.add_parser(
18001832
"create_table_with_datatypes", help=create_table_with_datatypes.__doc__
18011833
)
@@ -1898,6 +1930,8 @@ def drop_sequence(instance_id, database_id):
18981930
delete_data_with_partitioned_dml(args.instance_id, args.database_id)
18991931
elif args.command == "update_with_batch_dml":
19001932
update_with_batch_dml(args.instance_id, args.database_id)
1933+
elif args.command == "dml_last_statement_option":
1934+
dml_last_statement_option(args.instance_id, args.database_id)
19011935
elif args.command == "create_table_with_datatypes":
19021936
create_table_with_datatypes(args.instance_id, args.database_id)
19031937
elif args.command == "insert_datatypes_data":

packages/google-cloud-spanner/samples/samples/pg_snippets_test.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -359,7 +359,14 @@ def test_delete_data_with_partitioned_dml(capsys, instance_id, sample_database):
359359
def test_update_with_batch_dml(capsys, instance_id, sample_database):
360360
snippets.update_with_batch_dml(instance_id, sample_database.database_id)
361361
out, _ = capsys.readouterr()
362-
assert "Executed 2 SQL statements using Batch DML" in out
362+
assert "Executed 2 SQL statements using Batch DML." in out
363+
364+
365+
def test_dml_last_statement_option(capsys, instance_id, sample_database):
366+
snippets.dml_last_statement_option(instance_id, sample_database.database_id)
367+
out, _ = capsys.readouterr()
368+
assert "1 record(s) inserted." in out
369+
assert "1 record(s) updated." in out
363370

364371

365372
@pytest.mark.dependency(name="create_table_with_datatypes")

packages/google-cloud-spanner/samples/samples/snippets.py

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2046,6 +2046,35 @@ def update_albums(transaction):
20462046
# [END spanner_dml_batch_update]
20472047

20482048

2049+
def dml_last_statement_option(instance_id, database_id):
2050+
"""Inserts and updates using DML where the update sets the last statement option."""
2051+
# [START spanner_dml_last_statement]
2052+
# instance_id = "your-spanner-instance"
2053+
# database_id = "your-spanner-db-id"
2054+
2055+
spanner_client = spanner.Client()
2056+
instance = spanner_client.instance(instance_id)
2057+
database = instance.database(database_id)
2058+
2059+
def insert_and_update_singers(transaction):
2060+
insert_row_ct = transaction.execute_update(
2061+
"INSERT INTO Singers (SingerId, FirstName, LastName) "
2062+
"VALUES (54213, 'John', 'Do')"
2063+
)
2064+
2065+
print("{} record(s) inserted.".format(insert_row_ct))
2066+
2067+
update_row_ct = transaction.execute_update(
2068+
"UPDATE Singers SET LastName = 'Doe' WHERE SingerId = 54213",
2069+
last_statement=True,
2070+
)
2071+
2072+
print("{} record(s) updated.".format(update_row_ct))
2073+
2074+
database.run_in_transaction(insert_and_update_singers)
2075+
# [END spanner_dml_last_statement]
2076+
2077+
20492078
def create_table_with_datatypes(instance_id, database_id):
20502079
"""Creates a table with supported datatypes."""
20512080
# [START spanner_create_table_with_datatypes]
@@ -3865,6 +3894,9 @@ def add_split_points(instance_id, database_id):
38653894
help=delete_data_with_partitioned_dml.__doc__,
38663895
)
38673896
subparsers.add_parser("update_with_batch_dml", help=update_with_batch_dml.__doc__)
3897+
subparsers.add_parser(
3898+
"dml_last_statement_option", help=dml_last_statement_option.__doc__
3899+
)
38683900
subparsers.add_parser(
38693901
"create_table_with_datatypes", help=create_table_with_datatypes.__doc__
38703902
)
@@ -4033,6 +4065,8 @@ def add_split_points(instance_id, database_id):
40334065
delete_data_with_partitioned_dml(args.instance_id, args.database_id)
40344066
elif args.command == "update_with_batch_dml":
40354067
update_with_batch_dml(args.instance_id, args.database_id)
4068+
elif args.command == "dml_last_statement_option":
4069+
dml_last_statement_option(args.instance_id, args.database_id)
40364070
elif args.command == "create_table_with_datatypes":
40374071
create_table_with_datatypes(args.instance_id, args.database_id)
40384072
elif args.command == "insert_datatypes_data":

packages/google-cloud-spanner/samples/samples/snippets_test.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -705,7 +705,14 @@ def test_delete_data_with_partitioned_dml(capsys, instance_id, sample_database):
705705
def test_update_with_batch_dml(capsys, instance_id, sample_database):
706706
snippets.update_with_batch_dml(instance_id, sample_database.database_id)
707707
out, _ = capsys.readouterr()
708-
assert "Executed 2 SQL statements using Batch DML" in out
708+
assert "Executed 2 SQL statements using Batch DML." in out
709+
710+
711+
def test_dml_last_statement_option(capsys, instance_id, sample_database):
712+
snippets.dml_last_statement_option(instance_id, sample_database.database_id)
713+
out, _ = capsys.readouterr()
714+
assert "1 record(s) inserted." in out
715+
assert "1 record(s) updated." in out
709716

710717

711718
@pytest.mark.dependency(name="create_table_with_datatypes")

0 commit comments

Comments
 (0)