diff --git a/packages/google-cloud-spanner/samples/samples/pg_snippets.py b/packages/google-cloud-spanner/samples/samples/pg_snippets.py index 432d68a8ce5f..e9f9a2ba627d 100644 --- a/packages/google-cloud-spanner/samples/samples/pg_snippets.py +++ b/packages/google-cloud-spanner/samples/samples/pg_snippets.py @@ -1081,6 +1081,35 @@ def update_albums(transaction): # [END spanner_postgresql_dml_batch_update] +def dml_last_statement_option(instance_id, database_id): + """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" + + 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 +1798,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 +1826,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 +1931,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..342a9f72666d 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 sets 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")