From 3ede440f9d9f9021994110c695367f51d4e7b834 Mon Sep 17 00:00:00 2001 From: Shirdon Gorse Date: Wed, 23 Apr 2025 15:42:14 +0000 Subject: [PATCH 1/3] Add last statement option samples --- samples/samples/pg_snippets.py | 34 ++++++++++++++++++++++++++++- samples/samples/pg_snippets_test.py | 6 +++++ samples/samples/requirements.txt | 2 +- samples/samples/snippets.py | 31 +++++++++++++++++++++++++- samples/samples/snippets_test.py | 6 +++++ 5 files changed, 76 insertions(+), 3 deletions(-) diff --git a/samples/samples/pg_snippets.py b/samples/samples/pg_snippets.py index ad8744794a..6a6fd12985 100644 --- a/samples/samples/pg_snippets.py +++ b/samples/samples/pg_snippets.py @@ -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 @@ -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( @@ -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() @@ -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) diff --git a/samples/samples/pg_snippets_test.py b/samples/samples/pg_snippets_test.py index 1b5d2971c1..56c8e113e0 100644 --- a/samples/samples/pg_snippets_test.py +++ b/samples/samples/pg_snippets_test.py @@ -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 \ No newline at end of file diff --git a/samples/samples/requirements.txt b/samples/samples/requirements.txt index 4009a0a00b..e15b0f908f 100644 --- a/samples/samples/requirements.txt +++ b/samples/samples/requirements.txt @@ -1,2 +1,2 @@ -google-cloud-spanner==3.50.0 +google-cloud-spanner==3.53.0 futures==3.4.0; python_version < "3" diff --git a/samples/samples/snippets.py b/samples/samples/snippets.py index 6650ebe88d..0f09fafcc5 100644 --- a/samples/samples/snippets.py +++ b/samples/samples/snippets.py @@ -33,6 +33,7 @@ from google.cloud.spanner_v1 import DirectedReadOptions, param_types from google.cloud.spanner_v1.data_types import JsonObject from google.protobuf import field_mask_pb2 # type: ignore +from google.cloud.spanner_v1.transaction import Transaction from testdata import singer_pb2 @@ -1577,7 +1578,6 @@ def insert_singers(transaction): database.run_in_transaction(insert_singers) # [END spanner_dml_standard_insert] - # [START spanner_get_commit_stats] def log_commit_stats(instance_id, database_id): """Inserts sample data using DML and displays the commit statistics.""" @@ -3508,6 +3508,32 @@ def query_data_with_proto_types_parameter(instance_id, database_id): ) # [END spanner_query_with_proto_types_parameter] +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( @@ -3666,6 +3692,7 @@ def query_data_with_proto_types_parameter(instance_id, database_id): "query_data_with_proto_types_parameter", help=query_data_with_proto_types_parameter.__doc__, ) + subparsers.add_parser("dml_last_statement_option", help=dml_last_statement_option.__doc__) args = parser.parse_args() @@ -3815,3 +3842,5 @@ def query_data_with_proto_types_parameter(instance_id, database_id): update_data_with_proto_types_with_dml(args.instance_id, args.database_id) elif args.command == "query_data_with_proto_types_parameter": query_data_with_proto_types_parameter(args.instance_id, args.database_id) + elif args.command == "dml_last_statement_option": + dml_last_statement_option(args.instance_id, args.database_id) diff --git a/samples/samples/snippets_test.py b/samples/samples/snippets_test.py index 87fa7a43a2..b667715dd8 100644 --- a/samples/samples/snippets_test.py +++ b/samples/samples/snippets_test.py @@ -1009,3 +1009,9 @@ def test_query_data_with_proto_types_parameter( ) out, _ = capsys.readouterr() assert "SingerId: 2, SingerInfo: singer_id: 2" 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 \ No newline at end of file From 7f9b2dc6bc50e64d8fcc4fbb892252eda9a3d2dd Mon Sep 17 00:00:00 2001 From: Shirdon Gorse Date: Wed, 23 Apr 2025 17:38:31 +0000 Subject: [PATCH 2/3] Fix formatting --- samples/samples/pg_snippets_test.py | 2 +- samples/samples/snippets.py | 1 + samples/samples/snippets_test.py | 3 ++- 3 files changed, 4 insertions(+), 2 deletions(-) diff --git a/samples/samples/pg_snippets_test.py b/samples/samples/pg_snippets_test.py index 56c8e113e0..9a5703ed47 100644 --- a/samples/samples/pg_snippets_test.py +++ b/samples/samples/pg_snippets_test.py @@ -517,4 +517,4 @@ 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 \ No newline at end of file + assert "1 record(s) updated." in out diff --git a/samples/samples/snippets.py b/samples/samples/snippets.py index a38be87dc6..836c24c4fe 100644 --- a/samples/samples/snippets.py +++ b/samples/samples/snippets.py @@ -1578,6 +1578,7 @@ def insert_singers(transaction): database.run_in_transaction(insert_singers) # [END spanner_dml_standard_insert] + # [START spanner_get_commit_stats] def log_commit_stats(instance_id, database_id): """Inserts sample data using DML and displays the commit statistics.""" diff --git a/samples/samples/snippets_test.py b/samples/samples/snippets_test.py index 48322e6cff..f0378bdf9c 100644 --- a/samples/samples/snippets_test.py +++ b/samples/samples/snippets_test.py @@ -1017,8 +1017,9 @@ def test_add_split_points(capsys, instance_id, sample_database): 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 \ No newline at end of file + assert "1 record(s) updated." in out From 9ae22789733db7829013f7762ff50ec410b92887 Mon Sep 17 00:00:00 2001 From: sgorse123 <72293954+sgorse123@users.noreply.github.com> Date: Mon, 5 May 2025 10:07:09 -0700 Subject: [PATCH 3/3] Update pg_snippets.py with correct tags --- samples/samples/pg_snippets.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/samples/samples/pg_snippets.py b/samples/samples/pg_snippets.py index 6a6fd12985..37967555f1 100644 --- a/samples/samples/pg_snippets.py +++ b/samples/samples/pg_snippets.py @@ -1732,7 +1732,7 @@ def drop_sequence(instance_id, database_id): # [END spanner_postgresql_drop_sequence] -# [START spanner_postgres_dml_last_statement] +# [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" @@ -1759,7 +1759,7 @@ def insert_and_update_singers(transaction): database.run_in_transaction(insert_and_update_singers) -# [END spanner_postgres_dml_last_statement] +# [END spanner_postgresql_dml_last_statement] if __name__ == "__main__": # noqa: C901 parser = argparse.ArgumentParser(