Skip to content
This repository was archived by the owner on Apr 7, 2026. It is now read-only.

Commit 5db02f6

Browse files
chore: Add samples for unnamed(or positional) parameters
1 parent 32b2373 commit 5db02f6

5 files changed

Lines changed: 168 additions & 2 deletions

File tree

.kokoro/presubmit/samples.cfg

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
# Format: //devtools/kokoro/config/proto/build.proto
2+
3+
# Configure the docker image for kokoro-trampoline.
4+
env_vars: {
5+
key: "TRAMPOLINE_IMAGE"
6+
value: "gcr.io/cloud-devrel-kokoro-resources/java8"
7+
}
8+
9+
env_vars: {
10+
key: "JOB_TYPE"
11+
value: "samples"
12+
}
13+
14+
# TODO: remove this after we've migrated all tests and scripts
15+
env_vars: {
16+
key: "GCLOUD_PROJECT"
17+
value: "gcloud-devel"
18+
}
19+
20+
env_vars: {
21+
key: "GOOGLE_CLOUD_PROJECT"
22+
value: "gcloud-devel"
23+
}
24+
25+
env_vars: {
26+
key: "GOOGLE_APPLICATION_CREDENTIALS"
27+
value: "secret_manager/java-it-service-account"
28+
}
29+
30+
env_vars: {
31+
key: "SECRET_MANAGER_KEYS"
32+
value: "java-it-service-account"
33+
}
34+
35+
env_vars: {
36+
key: "ENABLE_BUILD_COP"
37+
value: "true"
38+
}

samples/install-without-bom/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@
3333
<dependency>
3434
<groupId>com.google.cloud</groupId>
3535
<artifactId>google-cloud-spanner</artifactId>
36-
<version>6.89.0</version>
36+
<version>6.91.0</version>
3737
</dependency>
3838
<!-- [END spanner_install_without_bom] -->
3939

samples/snippets/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@
3434
<dependency>
3535
<groupId>com.google.cloud</groupId>
3636
<artifactId>libraries-bom</artifactId>
37-
<version>26.57.0</version>
37+
<version>26.58.0</version>
3838
<type>pom</type>
3939
<scope>import</scope>
4040
</dependency>
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
package com.example.spanner;
2+
3+
import com.google.cloud.Timestamp;
4+
import com.google.cloud.spanner.DatabaseClient;
5+
import com.google.cloud.spanner.DatabaseId;
6+
import com.google.cloud.spanner.ResultSet;
7+
import com.google.cloud.spanner.Spanner;
8+
import com.google.cloud.spanner.SpannerOptions;
9+
import com.google.cloud.spanner.Statement;
10+
import com.google.cloud.spanner.Statement.StatementFactory;
11+
import java.time.LocalDate;
12+
13+
public class UnnamedParametersExample {
14+
15+
static void executeQueryWithUnnamedParameters() {
16+
// TODO(developer): Replace these variables before running the sample.
17+
String projectId = "my-project";
18+
String instanceId = "my-instance";
19+
String databaseId = "my-database";
20+
21+
executeQueryWithUnnamedParameters(projectId, instanceId, databaseId);
22+
}
23+
24+
static void executeQueryWithUnnamedParameters(
25+
String projectId, String instanceId, String databaseId) {
26+
try (Spanner spanner =
27+
SpannerOptions.newBuilder().setProjectId(projectId).build().getService()) {
28+
29+
DatabaseClient client =
30+
spanner.getDatabaseClient(DatabaseId.of(projectId, instanceId, databaseId));
31+
StatementFactory statementFactory = client.getStatementFactory();
32+
33+
// Insert a row with unnamed parameters
34+
client
35+
.readWriteTransaction()
36+
.run(
37+
transaction -> {
38+
Statement statement =
39+
statementFactory.withUnnamedParameters(
40+
"INSERT INTO Students(StudentId, Name, IsNRI, AvgMarks, JoinedAt, PinCode, CreatedAt) VALUES(?, ?, ?, ?, ?, ?, ?)",
41+
1000001,
42+
"Google",
43+
false,
44+
(float) 34.5,
45+
LocalDate.of(2024, 3, 31),
46+
"123456",
47+
Timestamp.now());
48+
transaction.executeUpdate(statement);
49+
50+
return null;
51+
});
52+
System.out.println("Row is inserted.");
53+
54+
// Query the table with unnamed parameters
55+
try (ResultSet resultSet =
56+
client
57+
.singleUse()
58+
.executeQuery(
59+
statementFactory.withUnnamedParameters(
60+
"SELECT * FROM Students WHERE StudentId = ?", 1000001))) {
61+
while (resultSet.next()) {
62+
System.out.println(resultSet.getString("Name"));
63+
}
64+
}
65+
System.out.println("Row is fetched.");
66+
}
67+
}
68+
}
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
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 org.junit.Assert.assertTrue;
20+
21+
import com.google.cloud.spanner.DatabaseId;
22+
import com.google.common.collect.ImmutableList;
23+
import java.util.concurrent.ExecutionException;
24+
import org.junit.Before;
25+
import org.junit.Test;
26+
27+
public class UnnamedParametersIT extends SampleTestBase {
28+
private static String databaseId;
29+
30+
@Before
31+
public void setup() throws ExecutionException, InterruptedException {
32+
databaseId = idGenerator.generateDatabaseId();
33+
databaseAdminClient
34+
.createDatabase(
35+
databaseAdminClient
36+
.newDatabaseBuilder(DatabaseId.of(projectId, instanceId, databaseId))
37+
.build(),
38+
ImmutableList.of(
39+
"CREATE TABLE Students ("
40+
+ " StudentId INT64 NOT NULL PRIMARY KEY,"
41+
+ " Name STRING(1024) NOT NULL,"
42+
+ " IsNRI BOOL NOT NULL,"
43+
+ " AvgMarks FLOAT32 NOT NULL,"
44+
+ " JoinedAt DATE NOT NULL,"
45+
+ " PinCode INT64 NOT NULL,"
46+
+ " CreatedAt TIMESTAMP NOT NULL"
47+
+ ")"))
48+
.get();
49+
}
50+
51+
@Test
52+
public void testUnnamedParameters() throws Exception {
53+
final String out =
54+
SampleRunner.runSample(
55+
() -> UnnamedParametersExample.executeQueryWithUnnamedParameters(projectId, instanceId, databaseId));
56+
assertTrue(out.contains("Row is inserted."));
57+
assertTrue(out.contains("Google"));
58+
assertTrue(out.contains("Row is fetched."));
59+
}
60+
}

0 commit comments

Comments
 (0)