Skip to content

Commit 065084b

Browse files
committed
feat: Add last statement option samples
Support for the option was added in #1313 by @olavloite.
1 parent 14abfd5 commit 065084b

File tree

4 files changed

+87
-3
lines changed

4 files changed

+87
-3
lines changed

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

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

10831083

1084+
# [START spanner_postgresql_dml_last_statement]
1085+
def dml_last_statement_option(instance_id, database_id):
1086+
"""Inserts and updates using DML where the update set the last statement option."""
1087+
# instance_id = "your-spanner-instance"
1088+
# database_id = "your-spanner-db-id"
1089+
1090+
spanner_client = spanner.Client()
1091+
instance = spanner_client.instance(instance_id)
1092+
database = instance.database(database_id)
1093+
1094+
def insert_and_update_singers(transaction):
1095+
insert_row_ct = transaction.execute_update(
1096+
"INSERT INTO Singers (SingerId, FirstName, LastName) "
1097+
" VALUES (54214, 'John', 'Do')"
1098+
)
1099+
1100+
print("{} record(s) inserted.".format(insert_row_ct))
1101+
1102+
update_row_ct = transaction.execute_update(
1103+
"UPDATE Singers SET LastName = 'Doe' WHERE SingerId = 54214",
1104+
last_statement=True,
1105+
)
1106+
1107+
print("{} record(s) updated.".format(update_row_ct))
1108+
1109+
database.run_in_transaction(insert_and_update_singers)
1110+
1111+
1112+
# [END spanner_postgresql_dml_last_statement]
1113+
1114+
10841115
def create_table_with_datatypes(instance_id, database_id):
10851116
"""Creates a table with supported datatypes."""
10861117
# [START spanner_postgresql_create_table_with_datatypes]
@@ -1769,7 +1800,7 @@ def drop_sequence(instance_id, database_id):
17691800
subparsers.add_parser("insert_data_with_dml", help=insert_data_with_dml.__doc__)
17701801
subparsers.add_parser("update_data_with_dml", help=update_data_with_dml.__doc__)
17711802
subparsers.add_parser(
1772-
"update_data_with_dml", help=update_data_with_dml_returning.__doc__
1803+
"update_data_with_dml_returning", help=update_data_with_dml_returning.__doc__
17731804
)
17741805
subparsers.add_parser("delete_data_with_dml", help=delete_data_with_dml.__doc__)
17751806
subparsers.add_parser(
@@ -1797,6 +1828,9 @@ def drop_sequence(instance_id, database_id):
17971828
help=delete_data_with_partitioned_dml.__doc__,
17981829
)
17991830
subparsers.add_parser("update_with_batch_dml", help=update_with_batch_dml.__doc__)
1831+
subparsers.add_parser(
1832+
"dml_last_statement_option", help=dml_last_statement_option.__doc__
1833+
)
18001834
subparsers.add_parser(
18011835
"create_table_with_datatypes", help=create_table_with_datatypes.__doc__
18021836
)
@@ -1899,6 +1933,8 @@ def drop_sequence(instance_id, database_id):
18991933
delete_data_with_partitioned_dml(args.instance_id, args.database_id)
19001934
elif args.command == "update_with_batch_dml":
19011935
update_with_batch_dml(args.instance_id, args.database_id)
1936+
elif args.command == "dml_last_statement_option":
1937+
dml_last_statement_option(args.instance_id, args.database_id)
19021938
elif args.command == "create_table_with_datatypes":
19031939
create_table_with_datatypes(args.instance_id, args.database_id)
19041940
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
@@ -2045,6 +2045,35 @@ def update_albums(transaction):
20452045
# [END spanner_dml_batch_update]
20462046

20472047

2048+
def dml_last_statement_option(instance_id, database_id):
2049+
"""Inserts and updates using DML where the update set the last statement option."""
2050+
# [START spanner_dml_last_statement]
2051+
# instance_id = "your-spanner-instance"
2052+
# database_id = "your-spanner-db-id"
2053+
2054+
spanner_client = spanner.Client()
2055+
instance = spanner_client.instance(instance_id)
2056+
database = instance.database(database_id)
2057+
2058+
def insert_and_update_singers(transaction):
2059+
insert_row_ct = transaction.execute_update(
2060+
"INSERT INTO Singers (SingerId, FirstName, LastName) "
2061+
" VALUES (54213, 'John', 'Do')"
2062+
)
2063+
2064+
print("{} record(s) inserted.".format(insert_row_ct))
2065+
2066+
update_row_ct = transaction.execute_update(
2067+
"UPDATE Singers SET LastName = 'Doe' WHERE SingerId = 54213",
2068+
last_statement=True,
2069+
)
2070+
2071+
print("{} record(s) updated.".format(update_row_ct))
2072+
2073+
database.run_in_transaction(insert_and_update_singers)
2074+
# [END spanner_dml_last_statement]
2075+
2076+
20482077
def create_table_with_datatypes(instance_id, database_id):
20492078
"""Creates a table with supported datatypes."""
20502079
# [START spanner_create_table_with_datatypes]
@@ -3862,6 +3891,9 @@ def add_split_points(instance_id, database_id):
38623891
help=delete_data_with_partitioned_dml.__doc__,
38633892
)
38643893
subparsers.add_parser("update_with_batch_dml", help=update_with_batch_dml.__doc__)
3894+
subparsers.add_parser(
3895+
"dml_last_statement_option", help=dml_last_statement_option.__doc__
3896+
)
38653897
subparsers.add_parser(
38663898
"create_table_with_datatypes", help=create_table_with_datatypes.__doc__
38673899
)
@@ -4032,6 +4064,8 @@ def add_split_points(instance_id, database_id):
40324064
delete_data_with_partitioned_dml(args.instance_id, args.database_id)
40334065
elif args.command == "update_with_batch_dml":
40344066
update_with_batch_dml(args.instance_id, args.database_id)
4067+
elif args.command == "dml_last_statement_option":
4068+
dml_last_statement_option(args.instance_id, args.database_id)
40354069
elif args.command == "create_table_with_datatypes":
40364070
create_table_with_datatypes(args.instance_id, args.database_id)
40374071
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)