Skip to content

Commit 92494d0

Browse files
authored
chore: util method for creating statement with params (#3817)
Currently, statements with parameters can only be created through a Statement.Builder. This has the disadvantage that Statement.Builder uses a StringBuilder internally, which means that each time Statement.newBuilder() is called, a new StringBuilder with the initial SQL statement is created. Later, when the statement is built, the contents of the StringBuilder are copied into a new string. This is efficient for statements that are built with multiple SQL fragements that are appended together. It is however inefficient for statements that are created with a fixed SQL string. This change therefore adds an additional util method to directly create a Statement from a string and an immutable map of parameters. Calling this method directly does not invoke any internal copy methods, and is more efficient for clients that have both the SQL string and the parameters readily available. This method will be used by PGAdapter, that does have this information available directly.
1 parent b6c9c6e commit 92494d0

File tree

2 files changed

+18
-0
lines changed

2 files changed

+18
-0
lines changed

google-cloud-spanner/src/main/java/com/google/cloud/spanner/Statement.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,11 @@ public static Statement of(String sql) {
144144
return new Statement(sql, ImmutableMap.of(), /*queryOptions=*/ null);
145145
}
146146

147+
/** Creates a {@link Statement} with the given SQL text and parameters. */
148+
public static Statement of(String sql, ImmutableMap<String, Value> parameters) {
149+
return new Statement(sql, parameters, /*queryOptions=*/ null);
150+
}
151+
147152
/** Creates a new statement builder with the SQL text {@code sql}. */
148153
public static Builder newBuilder(String sql) {
149154
return new Builder(sql);

google-cloud-spanner/src/test/java/com/google/cloud/spanner/StatementTest.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@
1818

1919
import static com.google.common.testing.SerializableTester.reserializeAndAssert;
2020
import static com.google.common.truth.Truth.assertThat;
21+
import static org.junit.Assert.assertEquals;
22+
import static org.junit.Assert.assertFalse;
2123
import static org.junit.Assert.assertNotNull;
2224
import static org.junit.Assert.assertThrows;
2325

@@ -42,6 +44,17 @@ public void basic() {
4244
reserializeAndAssert(stmt);
4345
}
4446

47+
@Test
48+
public void basicWithParameters() {
49+
String sql = "SELECT @name";
50+
Statement stmt = Statement.of(sql, ImmutableMap.of("name", Value.string("hello")));
51+
assertEquals(sql, stmt.getSql());
52+
assertFalse(stmt.getParameters().isEmpty());
53+
assertEquals(Value.string("hello"), stmt.getParameters().get("name"));
54+
assertEquals(sql + " {name: hello}", stmt.toString());
55+
reserializeAndAssert(stmt);
56+
}
57+
4558
@Test
4659
public void serialization() {
4760
Statement stmt =

0 commit comments

Comments
 (0)