Skip to content

Commit fe18c61

Browse files
committed
Adds last statement sample
1 parent 5b9e4f8 commit fe18c61

File tree

2 files changed

+140
-0
lines changed

2 files changed

+140
-0
lines changed
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
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 LastStatementSample {
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+
final String projectId = "data-placement-test";
37+
final String instanceId = "shirdon-instance";
38+
final String databaseId = "db";
39+
40+
try (Spanner spanner =
41+
SpannerOptions.newBuilder().setProjectId(projectId).build().getService()) {
42+
final DatabaseClient databaseClient =
43+
spanner.getDatabaseClient(DatabaseId.of(projectId, instanceId, databaseId));
44+
insertAndUpdateUsingLastStatement(databaseClient);
45+
}
46+
}
47+
48+
// [START spanner_last_statement]
49+
static void insertAndUpdateUsingLastStatement(DatabaseClient client) {
50+
client
51+
.readWriteTransaction()
52+
.run(
53+
transaction -> {
54+
transaction.executeUpdate(
55+
Statement.of(
56+
"INSERT Singers (SingerId, FirstName, LastName)\n"
57+
+ "VALUES (54213, 'John', 'Do')"));
58+
System.out.println("New singer inserted.");
59+
60+
// Pass in the `lastStatement` option to the last DML statement of the transaction.
61+
transaction.executeUpdate(
62+
Statement.of(
63+
"UPDATE Singers SET Singers.LastName = 'Doe' WHERE SingerId = 54213\n"), Options.lastStatement());
64+
System.out.println("Singer last name updated.");
65+
66+
return null;
67+
});
68+
}
69+
// [END spanner_last_statement]
70+
71+
}
Lines changed: 69 additions & 0 deletions
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 static com.example.spanner.SampleRunner.runSample;
20+
import static com.google.common.truth.Truth.assertThat;
21+
22+
import com.google.cloud.spanner.DatabaseClient;
23+
import com.google.cloud.spanner.DatabaseId;
24+
import com.google.cloud.spanner.KeySet;
25+
import com.google.cloud.spanner.Mutation;
26+
import com.google.common.collect.ImmutableList;
27+
import java.util.Arrays;
28+
import java.util.Collections;
29+
import org.junit.After;
30+
import org.junit.Before;
31+
import org.junit.BeforeClass;
32+
import org.junit.Test;
33+
import org.junit.runner.RunWith;
34+
import org.junit.runners.JUnit4;
35+
36+
/** Integration tests for {@link LastStatementSample} */
37+
@RunWith(JUnit4.class)
38+
public class LastStatementSampleIT extends SampleTestBase {
39+
40+
private static DatabaseId databaseId;
41+
42+
@BeforeClass
43+
public static void createTestDatabase() throws Exception {
44+
final String database = idGenerator.generateDatabaseId();
45+
databaseAdminClient
46+
.createDatabase(
47+
instanceId,
48+
database,
49+
ImmutableList.of(
50+
"CREATE TABLE Singers ("
51+
+ " SingerId INT64 NOT NULL,"
52+
+ " FirstName STRING(1024),"
53+
+ " LastName STRING(1024),"
54+
+ " SingerInfo BYTES(MAX)"
55+
+ ") PRIMARY KEY (SingerId)"))
56+
.get();
57+
databaseId = DatabaseId.of(projectId, instanceId, database);
58+
}
59+
60+
@Test
61+
public void testSetLastStatementOptionSample() throws Exception {
62+
final DatabaseClient client = spanner.getDatabaseClient(databaseId);
63+
String out =
64+
runSample(
65+
() -> LastStatementSample.insertAndUpdateUsingLastStatement(client));
66+
assertThat(out).contains("New singer inserted.");
67+
assertThat(out).contains("Singer last name updated.");
68+
}
69+
}

0 commit comments

Comments
 (0)