Skip to content

chore: util method for creating statement with params #3817

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 14, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,11 @@ public static Statement of(String sql) {
return new Statement(sql, ImmutableMap.of(), /*queryOptions=*/ null);
}

/** Creates a {@link Statement} with the given SQL text and parameters. */
public static Statement of(String sql, ImmutableMap<String, Value> parameters) {
return new Statement(sql, parameters, /*queryOptions=*/ null);
}

/** Creates a new statement builder with the SQL text {@code sql}. */
public static Builder newBuilder(String sql) {
return new Builder(sql);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@

import static com.google.common.testing.SerializableTester.reserializeAndAssert;
import static com.google.common.truth.Truth.assertThat;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertThrows;

Expand All @@ -42,6 +44,17 @@ public void basic() {
reserializeAndAssert(stmt);
}

@Test
public void basicWithParameters() {
String sql = "SELECT @name";
Statement stmt = Statement.of(sql, ImmutableMap.of("name", Value.string("hello")));
assertEquals(sql, stmt.getSql());
assertFalse(stmt.getParameters().isEmpty());
assertEquals(Value.string("hello"), stmt.getParameters().get("name"));
assertEquals(sql + " {name: hello}", stmt.toString());
reserializeAndAssert(stmt);
}

@Test
public void serialization() {
Statement stmt =
Expand Down
Loading