Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 35 additions & 1 deletion packages/google-cloud-spanner/samples/samples/pg_snippets.py
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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__
)
Expand Down Expand Up @@ -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":
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
34 changes: 34 additions & 0 deletions packages/google-cloud-spanner/samples/samples/snippets.py
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down Expand Up @@ -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__
)
Expand Down Expand Up @@ -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":
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
Loading