From 065084b3930e20904936cdf2c70301baf199da80 Mon Sep 17 00:00:00 2001 From: Shirdon Gorse Date: Wed, 1 Apr 2026 02:07:39 +0000 Subject: [PATCH 1/2] feat: Add last statement option samples Support for the option was added in #1313 by @olavloite. --- .../samples/samples/pg_snippets.py | 38 ++++++++++++++++++- .../samples/samples/pg_snippets_test.py | 9 ++++- .../samples/samples/snippets.py | 34 +++++++++++++++++ .../samples/samples/snippets_test.py | 9 ++++- 4 files changed, 87 insertions(+), 3 deletions(-) diff --git a/packages/google-cloud-spanner/samples/samples/pg_snippets.py b/packages/google-cloud-spanner/samples/samples/pg_snippets.py index 432d68a8ce5f..ee30af79bfc1 100644 --- a/packages/google-cloud-spanner/samples/samples/pg_snippets.py +++ b/packages/google-cloud-spanner/samples/samples/pg_snippets.py @@ -1081,6 +1081,37 @@ def update_albums(transaction): # [END spanner_postgresql_dml_batch_update] +# [START spanner_postgresql_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_postgresql_dml_last_statement] + + def create_table_with_datatypes(instance_id, database_id): """Creates a table with supported datatypes.""" # [START spanner_postgresql_create_table_with_datatypes] @@ -1769,7 +1800,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( @@ -1797,6 +1828,9 @@ def drop_sequence(instance_id, database_id): help=delete_data_with_partitioned_dml.__doc__, ) subparsers.add_parser("update_with_batch_dml", help=update_with_batch_dml.__doc__) + subparsers.add_parser( + "dml_last_statement_option", help=dml_last_statement_option.__doc__ + ) subparsers.add_parser( "create_table_with_datatypes", help=create_table_with_datatypes.__doc__ ) @@ -1899,6 +1933,8 @@ def drop_sequence(instance_id, database_id): delete_data_with_partitioned_dml(args.instance_id, args.database_id) elif args.command == "update_with_batch_dml": update_with_batch_dml(args.instance_id, args.database_id) + elif args.command == "dml_last_statement_option": + dml_last_statement_option(args.instance_id, args.database_id) elif args.command == "create_table_with_datatypes": create_table_with_datatypes(args.instance_id, args.database_id) elif args.command == "insert_datatypes_data": diff --git a/packages/google-cloud-spanner/samples/samples/pg_snippets_test.py b/packages/google-cloud-spanner/samples/samples/pg_snippets_test.py index 1b5d2971c19e..b6ba349eda03 100644 --- a/packages/google-cloud-spanner/samples/samples/pg_snippets_test.py +++ b/packages/google-cloud-spanner/samples/samples/pg_snippets_test.py @@ -359,7 +359,14 @@ def test_delete_data_with_partitioned_dml(capsys, instance_id, sample_database): def test_update_with_batch_dml(capsys, instance_id, sample_database): snippets.update_with_batch_dml(instance_id, sample_database.database_id) out, _ = capsys.readouterr() - assert "Executed 2 SQL statements using Batch DML" in out + assert "Executed 2 SQL statements using Batch DML." 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 @pytest.mark.dependency(name="create_table_with_datatypes") diff --git a/packages/google-cloud-spanner/samples/samples/snippets.py b/packages/google-cloud-spanner/samples/samples/snippets.py index 96c00548525c..0346a74e06a0 100644 --- a/packages/google-cloud-spanner/samples/samples/snippets.py +++ b/packages/google-cloud-spanner/samples/samples/snippets.py @@ -2045,6 +2045,35 @@ def update_albums(transaction): # [END spanner_dml_batch_update] +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] + + def create_table_with_datatypes(instance_id, database_id): """Creates a table with supported datatypes.""" # [START spanner_create_table_with_datatypes] @@ -3862,6 +3891,9 @@ def add_split_points(instance_id, database_id): help=delete_data_with_partitioned_dml.__doc__, ) subparsers.add_parser("update_with_batch_dml", help=update_with_batch_dml.__doc__) + subparsers.add_parser( + "dml_last_statement_option", help=dml_last_statement_option.__doc__ + ) subparsers.add_parser( "create_table_with_datatypes", help=create_table_with_datatypes.__doc__ ) @@ -4032,6 +4064,8 @@ def add_split_points(instance_id, database_id): delete_data_with_partitioned_dml(args.instance_id, args.database_id) elif args.command == "update_with_batch_dml": update_with_batch_dml(args.instance_id, args.database_id) + elif args.command == "dml_last_statement_option": + dml_last_statement_option(args.instance_id, args.database_id) elif args.command == "create_table_with_datatypes": create_table_with_datatypes(args.instance_id, args.database_id) elif args.command == "insert_datatypes_data": diff --git a/packages/google-cloud-spanner/samples/samples/snippets_test.py b/packages/google-cloud-spanner/samples/samples/snippets_test.py index 3888bf012092..c4e37b3e64f3 100644 --- a/packages/google-cloud-spanner/samples/samples/snippets_test.py +++ b/packages/google-cloud-spanner/samples/samples/snippets_test.py @@ -705,7 +705,14 @@ def test_delete_data_with_partitioned_dml(capsys, instance_id, sample_database): def test_update_with_batch_dml(capsys, instance_id, sample_database): snippets.update_with_batch_dml(instance_id, sample_database.database_id) out, _ = capsys.readouterr() - assert "Executed 2 SQL statements using Batch DML" in out + assert "Executed 2 SQL statements using Batch DML." 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 @pytest.mark.dependency(name="create_table_with_datatypes") From a82a7b797e73febc4b3f3fe6c5dc93bd36a309bc Mon Sep 17 00:00:00 2001 From: Shirdon Gorse Date: Wed, 1 Apr 2026 02:07:39 +0000 Subject: [PATCH 2/2] feat: Add last statement option samples Support for the option was added in #1313 by @olavloite. --- .../samples/samples/pg_snippets.py | 10 ++++------ .../google-cloud-spanner/samples/samples/snippets.py | 4 ++-- 2 files changed, 6 insertions(+), 8 deletions(-) diff --git a/packages/google-cloud-spanner/samples/samples/pg_snippets.py b/packages/google-cloud-spanner/samples/samples/pg_snippets.py index ee30af79bfc1..e9f9a2ba627d 100644 --- a/packages/google-cloud-spanner/samples/samples/pg_snippets.py +++ b/packages/google-cloud-spanner/samples/samples/pg_snippets.py @@ -1081,9 +1081,9 @@ def update_albums(transaction): # [END spanner_postgresql_dml_batch_update] -# [START spanner_postgresql_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.""" + """Inserts and updates using DML where the update sets the last statement option.""" + # [START spanner_postgresql_dml_last_statement] # instance_id = "your-spanner-instance" # database_id = "your-spanner-db-id" @@ -1094,7 +1094,7 @@ def dml_last_statement_option(instance_id, database_id): def insert_and_update_singers(transaction): insert_row_ct = transaction.execute_update( "INSERT INTO Singers (SingerId, FirstName, LastName) " - " VALUES (54214, 'John', 'Do')" + "VALUES (54214, 'John', 'Do')" ) print("{} record(s) inserted.".format(insert_row_ct)) @@ -1107,9 +1107,7 @@ def insert_and_update_singers(transaction): print("{} record(s) updated.".format(update_row_ct)) database.run_in_transaction(insert_and_update_singers) - - -# [END spanner_postgresql_dml_last_statement] + # [END spanner_postgresql_dml_last_statement] def create_table_with_datatypes(instance_id, database_id): diff --git a/packages/google-cloud-spanner/samples/samples/snippets.py b/packages/google-cloud-spanner/samples/samples/snippets.py index 0346a74e06a0..342a9f72666d 100644 --- a/packages/google-cloud-spanner/samples/samples/snippets.py +++ b/packages/google-cloud-spanner/samples/samples/snippets.py @@ -2046,7 +2046,7 @@ def update_albums(transaction): def dml_last_statement_option(instance_id, database_id): - """Inserts and updates using DML where the update set the last statement option.""" + """Inserts and updates using DML where the update sets the last statement option.""" # [START spanner_dml_last_statement] # instance_id = "your-spanner-instance" # database_id = "your-spanner-db-id" @@ -2058,7 +2058,7 @@ def dml_last_statement_option(instance_id, database_id): def insert_and_update_singers(transaction): insert_row_ct = transaction.execute_update( "INSERT INTO Singers (SingerId, FirstName, LastName) " - " VALUES (54213, 'John', 'Do')" + "VALUES (54213, 'John', 'Do')" ) print("{} record(s) inserted.".format(insert_row_ct))