Skip to content

Commit 870f2cf

Browse files
committed
Add PG last statement samples
1 parent 5bb406d commit 870f2cf

File tree

2 files changed

+149
-0
lines changed

2 files changed

+149
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
/*
2+
* Copyright 2025 Google LLC
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.example.spanner;
18+
19+
import com.google.cloud.spanner.DatabaseClient;
20+
import com.google.cloud.spanner.DatabaseId;
21+
import com.google.cloud.spanner.Options;
22+
import com.google.cloud.spanner.Spanner;
23+
import com.google.cloud.spanner.SpannerOptions;
24+
import com.google.cloud.spanner.Statement;
25+
26+
/**
27+
* Sample showing how to set the last statement option when a DML statement is the last statement in a transaction.
28+
*/
29+
public class PgLastStatementSample {
30+
31+
static void insertAndUpdateUsingLastStatement() {
32+
// TODO(developer): Replace these variables before running the sample.
33+
final String projectId = "my-project";
34+
final String instanceId = "my-instance";
35+
final String databaseId = "my-database";
36+
37+
try (Spanner spanner =
38+
SpannerOptions.newBuilder().setProjectId(projectId).build().getService()) {
39+
final DatabaseClient databaseClient =
40+
spanner.getDatabaseClient(DatabaseId.of(projectId, instanceId, databaseId));
41+
insertAndUpdateUsingLastStatement(databaseClient);
42+
}
43+
}
44+
45+
// [START spanner_last_statement]
46+
static void insertAndUpdateUsingLastStatement(DatabaseClient client) {
47+
client
48+
.readWriteTransaction()
49+
.run(
50+
transaction -> {
51+
transaction.executeUpdate(
52+
Statement.of(
53+
"INSERT INTO Singers (SingerId, FirstName, LastName) "
54+
+ "VALUES (54214, 'John', 'Do')"));
55+
System.out.println("New singer inserted.");
56+
57+
// Pass in the `lastStatement` option to the last DML statement of the transaction.
58+
transaction.executeUpdate(
59+
Statement.of(
60+
"UPDATE Singers SET LastName = 'Doe' WHERE SingerId = 54213\n"),
61+
Options.lastStatement());
62+
System.out.println("Singer last name updated.");
63+
64+
return null;
65+
});
66+
}
67+
// [END spanner_last_statement]
68+
69+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
/*
2+
* Copyright 2025 Google LLC
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.example.spanner;
18+
19+
import static com.example.spanner.SampleRunner.runSample;
20+
import static com.google.common.truth.Truth.assertThat;
21+
22+
import com.google.api.gax.longrunning.OperationFuture;
23+
import com.google.cloud.spanner.DatabaseClient;
24+
import com.google.cloud.spanner.DatabaseId;
25+
import com.google.cloud.spanner.Dialect;
26+
import com.google.cloud.spanner.KeySet;
27+
import com.google.cloud.spanner.Mutation;
28+
import com.google.common.collect.ImmutableList;
29+
import com.google.spanner.admin.database.v1.UpdateDatabaseDdlMetadata;
30+
import java.util.Arrays;
31+
import java.util.Collections;
32+
import java.util.concurrent.TimeUnit;
33+
import org.junit.BeforeClass;
34+
import org.junit.Test;
35+
import org.junit.runner.RunWith;
36+
import org.junit.runners.JUnit4;
37+
38+
/** Integration tests for {@link PgLastStatementSample} */
39+
@RunWith(JUnit4.class)
40+
public class PgLastStatementSampleIT extends SampleTestBase {
41+
42+
private static DatabaseId databaseId;
43+
44+
@BeforeClass
45+
public static void createTestDatabase() throws Exception {
46+
final String database = idGenerator.generateDatabaseId();
47+
databaseAdminClient
48+
.createDatabase(
49+
databaseAdminClient
50+
.newDatabaseBuilder(DatabaseId.of(projectId, instanceId, database))
51+
.setDialect(Dialect.POSTGRESQL)
52+
.build(),
53+
Collections.emptyList())
54+
.get(10, TimeUnit.MINUTES);
55+
final OperationFuture<Void, UpdateDatabaseDdlMetadata> updateOperation =
56+
databaseAdminClient.updateDatabaseDdl(
57+
instanceId,
58+
database,
59+
ImmutableList.of(
60+
"CREATE TABLE Singers ("
61+
+ " SingerId bigint NOT NULL,"
62+
+ " FirstName character varying(1024),"
63+
+ " LastName character varying(1024),"
64+
+ " PRIMARY KEY (SingerId)"
65+
+ ")"),
66+
null);
67+
updateOperation.get(10, TimeUnit.MINUTES);
68+
databaseId = DatabaseId.of(projectId, instanceId, database);
69+
}
70+
71+
@Test
72+
public void testSetLastStatementOptionSample() throws Exception {
73+
final DatabaseClient client = spanner.getDatabaseClient(databaseId);
74+
String out =
75+
runSample(
76+
() -> PgLastStatementSample.insertAndUpdateUsingLastStatement(client));
77+
assertThat(out).contains("New singer inserted.");
78+
assertThat(out).contains("Singer last name updated.");
79+
}
80+
}

0 commit comments

Comments
 (0)