Skip to content
Open
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 @@ -58,10 +58,15 @@ public class BoundStatement {

private final PreparedStatementImpl preparedStatement;
private final Map<String, Value> params;
private final Map<String, Value> viewParameters;

private BoundStatement(PreparedStatementImpl preparedStatement, Map<String, Value> params) {
private BoundStatement(
PreparedStatementImpl preparedStatement,
Map<String, Value> params,
Map<String, Value> viewParameters) {
this.preparedStatement = preparedStatement;
this.params = params;
this.viewParameters = viewParameters;
}

/**
Expand All @@ -78,6 +83,7 @@ public static class Builder {
private final PreparedStatementImpl preparedStatement;
private final Map<String, SqlType<?>> paramTypes;
private final Map<String, Value> params;
private final Map<String, Value> viewParameters;

/**
* Creates a builder from a {@link PreparedStatement}
Expand All @@ -90,6 +96,7 @@ public Builder(PreparedStatementImpl preparedStatement, Map<String, SqlType<?>>
this.preparedStatement = preparedStatement;
this.paramTypes = paramTypes;
this.params = new HashMap<>();
this.viewParameters = new HashMap<>();
}

/** Builds a {@link BoundStatement} from the builder */
Expand All @@ -101,7 +108,28 @@ public BoundStatement build() {
"Attempting to build BoundStatement without binding parameter: " + paramName);
}
}
return new BoundStatement(preparedStatement, ImmutableMap.copyOf(params));
return new BoundStatement(
preparedStatement, ImmutableMap.copyOf(params), ImmutableMap.copyOf(viewParameters));
}

/** Sets a view parameter with the name {@code name} and the Value {@code value}. */
public Builder setViewParameter(String name, Value value) {
Preconditions.checkNotNull(name, "name cannot be null");
Preconditions.checkNotNull(value, "value cannot be null");
Preconditions.checkArgument(
value.getKindCase() == Value.KindCase.STRING_VALUE,
"Currently only String typed view parameters are supported");
viewParameters.put(name, value);
return this;
}

/** Sets view parameters from a map. */
public Builder setViewParameters(Map<String, Value> viewParameters) {
Preconditions.checkNotNull(viewParameters, "viewParameters cannot be null");
for (Map.Entry<String, Value> entry : viewParameters.entrySet()) {
setViewParameter(entry.getKey(), entry.getValue());
}
return this;
}

/**
Expand Down Expand Up @@ -372,7 +400,8 @@ public ExecuteQueryRequest toProto(
requestContext.getProjectId(), requestContext.getInstanceId()))
.setAppProfileId(requestContext.getAppProfileId())
.setPreparedQuery(preparedQuery)
.putAllParams(params);
.putAllParams(params)
.putAllViewParameters(viewParameters);

if (resumeToken != null) {
requestBuilder.setResumeToken(resumeToken);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -869,4 +869,76 @@ public void builderValidatesAllParamsAreSet() {
assertThat(e.getMessage())
.contains("Attempting to build BoundStatement without binding parameter: bytesParam");
}

@Test
public void statementWithViewParameters() {
Value stringVal = Value.newBuilder().setType(stringType()).setStringValue("alice").build();
Value locationVal = Value.newBuilder().setType(stringType()).setStringValue("us-east1").build();
HashMap<String, Value> viewParams = new HashMap<>();
viewParams.put("user_id", stringVal);
viewParams.put("location", locationVal);

BoundStatement s = boundStatementBuilder().setViewParameters(viewParams).build();

assertThat(s.toProto(EXPECTED_PREPARED_QUERY, REQUEST_CONTEXT, NO_RESUME_TOKEN))
.isEqualTo(
ExecuteQueryRequest.newBuilder()
.setPreparedQuery(EXPECTED_PREPARED_QUERY)
.setInstanceName(EXPECTED_INSTANCE_NAME)
.setAppProfileId(EXPECTED_APP_PROFILE)
.putViewParameters("user_id", stringVal)
.putViewParameters("location", locationVal)
.build());
}
Comment thread
ad548 marked this conversation as resolved.

@Test
public void setViewParameterRejectsNonStringValues() {
BoundStatement.Builder builder = boundStatementBuilder();
Value intVal = Value.newBuilder().setType(int64Type()).setIntValue(42).build();
Value boolVal = Value.newBuilder().setType(boolType()).setBoolValue(true).build();
Value unsetVal = Value.newBuilder().setType(stringType()).build();
Value arrayVal = arrayValue(stringValue("foo"), stringValue("bar"));

IllegalArgumentException eInt =
assertThrows(IllegalArgumentException.class, () -> builder.setViewParameter("age", intVal));
assertThat(eInt.getMessage())
.contains("Currently only String typed view parameters are supported");

IllegalArgumentException eBool =
assertThrows(
IllegalArgumentException.class, () -> builder.setViewParameter("active", boolVal));
assertThat(eBool.getMessage())
.contains("Currently only String typed view parameters are supported");

IllegalArgumentException eUnset =
assertThrows(
IllegalArgumentException.class, () -> builder.setViewParameter("unset", unsetVal));
assertThat(eUnset.getMessage())
.contains("Currently only String typed view parameters are supported");

IllegalArgumentException eArray =
assertThrows(
IllegalArgumentException.class, () -> builder.setViewParameter("tags", arrayVal));
assertThat(eArray.getMessage())
.contains("Currently only String typed view parameters are supported");

HashMap<String, Value> viewParams = new HashMap<>();
viewParams.put(
"user_id", Value.newBuilder().setType(stringType()).setStringValue("alice").build());
viewParams.put("age", intVal);

IllegalArgumentException eMap =
assertThrows(IllegalArgumentException.class, () -> builder.setViewParameters(viewParams));
assertThat(eMap.getMessage())
.contains("Currently only String typed view parameters are supported");

// Empty map should succeed without error
builder.setViewParameters(Collections.emptyMap());

// Null checks
assertThrows(
NullPointerException.class, () -> builder.setViewParameter(null, stringValue("alice")));
assertThrows(NullPointerException.class, () -> builder.setViewParameter("user_id", null));
assertThrows(NullPointerException.class, () -> builder.setViewParameters(null));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,7 @@ static BoundStatement toBoundStatement(
throw new IllegalArgumentException("Unexpected query param type in param: " + value);
}
}
boundStatementBuilder.setViewParameters(request.getRequest().getViewParametersMap());
return boundStatementBuilder.build();
}

Expand Down
Loading