Skip to content

Commit 2f62816

Browse files
authored
feat: Last statement sample (#3830)
Support of the last statement option was added in https://togithub.com/googleapis/java-spanner/pull/3647 by @olavloite. This change adds a sample of how to use the option in the last DML statement of a transaction.
1 parent f385698 commit 2f62816

File tree

4 files changed

+275
-0
lines changed

4 files changed

+275
-0
lines changed
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
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
28+
* a transaction.
29+
*/
30+
public class LastStatementSample {
31+
32+
static void insertAndUpdateUsingLastStatement() {
33+
// TODO(developer): Replace these variables before running the sample.
34+
final String projectId = "my-project";
35+
final String instanceId = "my-instance";
36+
final String databaseId = "my-database";
37+
38+
try (Spanner spanner =
39+
SpannerOptions.newBuilder().setProjectId(projectId).build().getService()) {
40+
final DatabaseClient databaseClient =
41+
spanner.getDatabaseClient(DatabaseId.of(projectId, instanceId, databaseId));
42+
insertAndUpdateUsingLastStatement(databaseClient);
43+
}
44+
}
45+
46+
// [START spanner_dml_last_statement]
47+
static void insertAndUpdateUsingLastStatement(DatabaseClient client) {
48+
client
49+
.readWriteTransaction()
50+
.run(
51+
transaction -> {
52+
transaction.executeUpdate(
53+
Statement.of(
54+
"INSERT Singers (SingerId, FirstName, LastName)\n"
55+
+ "VALUES (54213, 'John', 'Do')"));
56+
System.out.println("New singer inserted.");
57+
58+
// Pass in the `lastStatement` option to the last DML statement of the transaction.
59+
transaction.executeUpdate(
60+
Statement.of(
61+
"UPDATE Singers SET Singers.LastName = 'Doe' WHERE SingerId = 54213\n"),
62+
Options.lastStatement());
63+
System.out.println("Singer last name updated.");
64+
65+
return null;
66+
});
67+
}
68+
// [END spanner_dml_last_statement]
69+
70+
}
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 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
28+
* a transaction.
29+
*/
30+
public class PgLastStatementSample {
31+
32+
static void insertAndUpdateUsingLastStatement() {
33+
// TODO(developer): Replace these variables before running the sample.
34+
final String projectId = "my-project";
35+
final String instanceId = "my-instance";
36+
final String databaseId = "my-database";
37+
38+
try (Spanner spanner =
39+
SpannerOptions.newBuilder().setProjectId(projectId).build().getService()) {
40+
final DatabaseClient databaseClient =
41+
spanner.getDatabaseClient(DatabaseId.of(projectId, instanceId, databaseId));
42+
insertAndUpdateUsingLastStatement(databaseClient);
43+
}
44+
}
45+
46+
// [START spanner_postgresql_dml_last_statement]
47+
static void insertAndUpdateUsingLastStatement(DatabaseClient client) {
48+
client
49+
.readWriteTransaction()
50+
.run(
51+
transaction -> {
52+
transaction.executeUpdate(
53+
Statement.of(
54+
"INSERT INTO Singers (SingerId, FirstName, LastName) "
55+
+ "VALUES (54214, 'John', 'Do')"));
56+
System.out.println("New singer inserted.");
57+
58+
// Pass in the `lastStatement` option to the last DML statement of the transaction.
59+
transaction.executeUpdate(
60+
Statement.of("UPDATE Singers SET LastName = 'Doe' WHERE SingerId = 54214\n"),
61+
Options.lastStatement());
62+
System.out.println("Singer last name updated.");
63+
64+
return null;
65+
});
66+
}
67+
// [END spanner_postgresql_dml_last_statement]
68+
69+
}
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
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.common.collect.ImmutableList;
25+
import org.junit.BeforeClass;
26+
import org.junit.Test;
27+
import org.junit.runner.RunWith;
28+
import org.junit.runners.JUnit4;
29+
30+
/** Integration tests for {@link LastStatementSample} */
31+
@RunWith(JUnit4.class)
32+
public class LastStatementSampleIT extends SampleTestBase {
33+
34+
private static DatabaseId databaseId;
35+
36+
@BeforeClass
37+
public static void createTestDatabase() throws Exception {
38+
final String database = idGenerator.generateDatabaseId();
39+
databaseAdminClient
40+
.createDatabase(
41+
instanceId,
42+
database,
43+
ImmutableList.of(
44+
"CREATE TABLE Singers ("
45+
+ " SingerId INT64 NOT NULL,"
46+
+ " FirstName STRING(1024),"
47+
+ " LastName STRING(1024),"
48+
+ " SingerInfo BYTES(MAX)"
49+
+ ") PRIMARY KEY (SingerId)"))
50+
.get();
51+
databaseId = DatabaseId.of(projectId, instanceId, database);
52+
}
53+
54+
@Test
55+
public void testSetLastStatementOptionSample() throws Exception {
56+
final DatabaseClient client = spanner.getDatabaseClient(databaseId);
57+
String out = runSample(() -> LastStatementSample.insertAndUpdateUsingLastStatement(client));
58+
assertThat(out).contains("New singer inserted.");
59+
assertThat(out).contains("Singer last name updated.");
60+
}
61+
}
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
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.common.collect.ImmutableList;
27+
import com.google.spanner.admin.database.v1.UpdateDatabaseDdlMetadata;
28+
import java.util.Collections;
29+
import java.util.concurrent.TimeUnit;
30+
import org.junit.BeforeClass;
31+
import org.junit.Test;
32+
import org.junit.runner.RunWith;
33+
import org.junit.runners.JUnit4;
34+
35+
/** Integration tests for {@link PgLastStatementSample} */
36+
@RunWith(JUnit4.class)
37+
public class PgLastStatementSampleIT extends SampleTestBase {
38+
39+
private static DatabaseId databaseId;
40+
41+
@BeforeClass
42+
public static void createTestDatabase() throws Exception {
43+
final String database = idGenerator.generateDatabaseId();
44+
databaseAdminClient
45+
.createDatabase(
46+
databaseAdminClient
47+
.newDatabaseBuilder(DatabaseId.of(projectId, instanceId, database))
48+
.setDialect(Dialect.POSTGRESQL)
49+
.build(),
50+
Collections.emptyList())
51+
.get(10, TimeUnit.MINUTES);
52+
final OperationFuture<Void, UpdateDatabaseDdlMetadata> updateOperation =
53+
databaseAdminClient.updateDatabaseDdl(
54+
instanceId,
55+
database,
56+
ImmutableList.of(
57+
"CREATE TABLE Singers ("
58+
+ " SingerId bigint NOT NULL,"
59+
+ " FirstName character varying(1024),"
60+
+ " LastName character varying(1024),"
61+
+ " PRIMARY KEY (SingerId)"
62+
+ ")"),
63+
null);
64+
updateOperation.get(10, TimeUnit.MINUTES);
65+
databaseId = DatabaseId.of(projectId, instanceId, database);
66+
}
67+
68+
@Test
69+
public void testSetLastStatementOptionSample() throws Exception {
70+
final DatabaseClient client = spanner.getDatabaseClient(databaseId);
71+
String out = runSample(() -> PgLastStatementSample.insertAndUpdateUsingLastStatement(client));
72+
assertThat(out).contains("New singer inserted.");
73+
assertThat(out).contains("Singer last name updated.");
74+
}
75+
}

0 commit comments

Comments
 (0)