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..9a5703ed47 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 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 4b4d7b5a2e..836c24c4fe 100644 --- a/samples/samples/snippets.py +++ b/samples/samples/snippets.py @@ -3594,6 +3594,32 @@ def add_split_points(instance_id, database_id): # [END spanner_database_add_split_points] +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( @@ -3756,6 +3782,7 @@ def add_split_points(instance_id, database_id): "add_split_points", help=add_split_points.__doc__, ) + subparsers.add_parser("dml_last_statement_option", help=dml_last_statement_option.__doc__) args = parser.parse_args() @@ -3907,3 +3934,5 @@ def add_split_points(instance_id, database_id): query_data_with_proto_types_parameter(args.instance_id, args.database_id) elif args.command == "add_split_points": add_split_points(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 eb61e8bd1f..f0378bdf9c 100644 --- a/samples/samples/snippets_test.py +++ b/samples/samples/snippets_test.py @@ -1016,3 +1016,10 @@ def test_add_split_points(capsys, instance_id, sample_database): snippets.add_split_points(instance_id, sample_database.database_id) 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