@@ -1080,6 +1080,35 @@ def update_albums(transaction):
10801080 # [END spanner_postgresql_dml_batch_update]
10811081
10821082
1083+ def dml_last_statement_option (instance_id , database_id ):
1084+ """Inserts and updates using DML where the update sets the last statement option."""
1085+ # [START spanner_postgresql_dml_last_statement]
1086+ # instance_id = "your-spanner-instance"
1087+ # database_id = "your-spanner-db-id"
1088+
1089+ spanner_client = spanner .Client ()
1090+ instance = spanner_client .instance (instance_id )
1091+ database = instance .database (database_id )
1092+
1093+ def insert_and_update_singers (transaction ):
1094+ insert_row_ct = transaction .execute_update (
1095+ "INSERT INTO Singers (SingerId, FirstName, LastName) "
1096+ "VALUES (54214, 'John', 'Do')"
1097+ )
1098+
1099+ print ("{} record(s) inserted." .format (insert_row_ct ))
1100+
1101+ update_row_ct = transaction .execute_update (
1102+ "UPDATE Singers SET LastName = 'Doe' WHERE SingerId = 54214" ,
1103+ last_statement = True ,
1104+ )
1105+
1106+ print ("{} record(s) updated." .format (update_row_ct ))
1107+
1108+ database .run_in_transaction (insert_and_update_singers )
1109+ # [END spanner_postgresql_dml_last_statement]
1110+
1111+
10831112def create_table_with_datatypes (instance_id , database_id ):
10841113 """Creates a table with supported datatypes."""
10851114 # [START spanner_postgresql_create_table_with_datatypes]
@@ -1768,7 +1797,7 @@ def drop_sequence(instance_id, database_id):
17681797 subparsers .add_parser ("insert_data_with_dml" , help = insert_data_with_dml .__doc__ )
17691798 subparsers .add_parser ("update_data_with_dml" , help = update_data_with_dml .__doc__ )
17701799 subparsers .add_parser (
1771- "update_data_with_dml " , help = update_data_with_dml_returning .__doc__
1800+ "update_data_with_dml_returning " , help = update_data_with_dml_returning .__doc__
17721801 )
17731802 subparsers .add_parser ("delete_data_with_dml" , help = delete_data_with_dml .__doc__ )
17741803 subparsers .add_parser (
@@ -1796,6 +1825,9 @@ def drop_sequence(instance_id, database_id):
17961825 help = delete_data_with_partitioned_dml .__doc__ ,
17971826 )
17981827 subparsers .add_parser ("update_with_batch_dml" , help = update_with_batch_dml .__doc__ )
1828+ subparsers .add_parser (
1829+ "dml_last_statement_option" , help = dml_last_statement_option .__doc__
1830+ )
17991831 subparsers .add_parser (
18001832 "create_table_with_datatypes" , help = create_table_with_datatypes .__doc__
18011833 )
@@ -1898,6 +1930,8 @@ def drop_sequence(instance_id, database_id):
18981930 delete_data_with_partitioned_dml (args .instance_id , args .database_id )
18991931 elif args .command == "update_with_batch_dml" :
19001932 update_with_batch_dml (args .instance_id , args .database_id )
1933+ elif args .command == "dml_last_statement_option" :
1934+ dml_last_statement_option (args .instance_id , args .database_id )
19011935 elif args .command == "create_table_with_datatypes" :
19021936 create_table_with_datatypes (args .instance_id , args .database_id )
19031937 elif args .command == "insert_datatypes_data" :
0 commit comments