Skip to content

Commit

Permalink
Consistent references to scalar values vs Parameter objects
Browse files Browse the repository at this point in the history
  • Loading branch information
jhoeller committed Aug 16, 2023
1 parent 08bc7ed commit 1e75041
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -163,10 +163,9 @@ interface Builder {
interface GenericExecuteSpec {

/**
* Bind a non-{@code null} value to a parameter identified by its
* {@code index}. {@code value} can be either a scalar value or {@link io.r2dbc.spi.Parameter}.
* Bind a non-{@code null} value to a parameter identified by its {@code index}.
* @param index zero based index to bind the parameter to
* @param value either a scalar value or {@link io.r2dbc.spi.Parameter}
* @param value either a scalar value or a {@link io.r2dbc.spi.Parameter}
*/
GenericExecuteSpec bind(int index, Object value);

Expand All @@ -180,7 +179,7 @@ interface GenericExecuteSpec {
/**
* Bind a non-{@code null} value to a parameter identified by its {@code name}.
* @param name the name of the parameter
* @param value the value to bind
* @param value either a scalar value or a {@link io.r2dbc.spi.Parameter}
*/
GenericExecuteSpec bind(String name, Object value);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -245,23 +245,27 @@ class DefaultGenericExecuteSpec implements GenericExecuteSpec {
}

@SuppressWarnings("deprecation")
private Parameter resolveParameter(Object value) {
if (value instanceof Parameter param) {
return param;
}
else if (value instanceof org.springframework.r2dbc.core.Parameter param) {
Object paramValue = param.getValue();
return (paramValue != null ? Parameters.in(paramValue) : Parameters.in(param.getType()));
}
else {
return Parameters.in(value);
}
}

@Override
public DefaultGenericExecuteSpec bind(int index, Object value) {
assertNotPreparedOperation();
Assert.notNull(value, () -> String.format(
"Value at index %d must not be null. Use bindNull(…) instead.", index));

Map<Integer, Parameter> byIndex = new LinkedHashMap<>(this.byIndex);
if (value instanceof Parameter param) {
byIndex.put(index, param);
}
else if (value instanceof org.springframework.r2dbc.core.Parameter param) {
Object pv = param.getValue();
byIndex.put(index, (pv != null ? Parameters.in(pv) : Parameters.in(param.getType())));
}
else {
byIndex.put(index, Parameters.in(value));
}
byIndex.put(index, resolveParameter(value));

return new DefaultGenericExecuteSpec(byIndex, this.byName, this.sqlSupplier, this.filterFunction);
}
Expand All @@ -276,7 +280,6 @@ public DefaultGenericExecuteSpec bindNull(int index, Class<?> type) {
return new DefaultGenericExecuteSpec(byIndex, this.byName, this.sqlSupplier, this.filterFunction);
}

@SuppressWarnings("deprecation")
@Override
public DefaultGenericExecuteSpec bind(String name, Object value) {
assertNotPreparedOperation();
Expand All @@ -286,15 +289,7 @@ public DefaultGenericExecuteSpec bind(String name, Object value) {
"Value for parameter %s must not be null. Use bindNull(…) instead.", name));

Map<String, Parameter> byName = new LinkedHashMap<>(this.byName);
if (value instanceof Parameter p) {
byName.put(name, p);
}
else if (value instanceof org.springframework.r2dbc.core.Parameter p) {
byName.put(name, p.hasValue() ? Parameters.in(p.getValue()) : Parameters.in(p.getType()));
}
else {
byName.put(name, Parameters.in(value));
}
byName.put(name, resolveParameter(value));

return new DefaultGenericExecuteSpec(this.byIndex, byName, this.sqlSupplier, this.filterFunction);
}
Expand Down

0 comments on commit 1e75041

Please sign in to comment.