#2619 - Lack of setNullParameter() functionality on io.ebean.SqlQuery interface

This commit is contained in:
Rob Bygrave committed 2022-03-29 22:41:36 +13:00
1 parent aa89df5f00
commit 4cad515110
4 files changed
+73 -2

No files matched your search

@@ -246,6 +246,16 @@ public interface SqlQuery extends Serializable, CancelableQuery {
*/ */
SqlQuery setParameter(Object value); SqlQuery setParameter(Object value);
/**
* Set a null parameter by position.
*/
SqlQuery setNullParameter(int position, int jdbcType);
/**
* Set a null parameter by name.
*/
SqlQuery setNullParameter(String name, int jdbcType);
/** /**
* Bind the parameter by its index position (1 based like JDBC). * Bind the parameter by its index position (1 based like JDBC).
* <p> * <p>
@@ -167,7 +167,7 @@ public final class BindParams implements Serializable {
*/ */
@SuppressWarnings("rawtypes") @SuppressWarnings("rawtypes")
public void setParameter(int position, Object value) { public void setParameter(int position, Object value) {
assert value != null : "use setNullParameter"; //TODO: Review - assert value != null : "use setNullParameter";
Param p = getParam(position); Param p = getParam(position);
if (value instanceof Collection) { if (value instanceof Collection) {
// use of postgres ANY with positioned parameter // use of postgres ANY with positioned parameter
@@ -219,7 +219,7 @@ public final class BindParams implements Serializable {
* Set a named In parameter that is not null. * Set a named In parameter that is not null.
*/ */
public Param setParameter(String name, Object value) { public Param setParameter(String name, Object value) {
assert value != null : "use setNullParameter"; // TODO: Review - assert value != null : "use setNullParameter";
Param p = getParam(name); Param p = getParam(name);
p.setInValue(value); p.setInValue(value);
return p; return p;
@@ -119,6 +119,18 @@ public final class DefaultRelationalQuery extends AbstractQuery implements SpiSq
return this; return this;
} }
@Override
public SqlQuery setNullParameter(int position, int jdbcType) {
bindParams.setNullParameter(position, jdbcType);
return this;
}
@Override
public SqlQuery setNullParameter(String name, int jdbcType) {
bindParams.setNullParameter(name, jdbcType);
return this;
}
@Override @Override
public DefaultRelationalQuery setParameter(int position, Object value) { public DefaultRelationalQuery setParameter(int position, Object value) {
bindParams.setParameter(position, value); bindParams.setParameter(position, value);
@@ -13,6 +13,7 @@ import org.tests.model.basic.ResetBasicData;
import java.math.BigDecimal; import java.math.BigDecimal;
import java.sql.ResultSet; import java.sql.ResultSet;
import java.sql.SQLException; import java.sql.SQLException;
import java.sql.Types;
import java.time.OffsetDateTime; import java.time.OffsetDateTime;
import java.util.List; import java.util.List;
import java.util.concurrent.atomic.AtomicInteger; import java.util.concurrent.atomic.AtomicInteger;
@@ -23,6 +24,54 @@ import static org.junit.jupiter.api.Assertions.assertEquals;
public class SqlQueryTests extends BaseTestCase { public class SqlQueryTests extends BaseTestCase {
@ForPlatform(Platform.H2)
@Test
public void selectBindNull_byPos() {
String sql = "select nvl(cast(? as int), 42)";
final Long val = DB.sqlQuery(sql)
.setNullParameter(1, Types.INTEGER)
.mapToScalar(Long.class)
.findOne();
assertThat(val).isEqualTo(42);
}
@ForPlatform(Platform.H2)
@Test
public void selectBindNull_byName() {
String sql = "select nvl(cast(:val as int), 42)";
final Long val = DB.sqlQuery(sql)
.setNullParameter("val", Types.INTEGER)
.mapToScalar(Long.class)
.findOne();
assertThat(val).isEqualTo(42);
}
@ForPlatform(Platform.H2)
@Test
public void selectBindNull_usingSetParameter_ByPosition() {
String sql = "select nvl(cast(? as int), 42)";
final Long val = DB.sqlQuery(sql)
.setParameter(1, null)
.mapToScalar(Long.class)
.findOne();
assertThat(val).isEqualTo(42);
}
@ForPlatform(Platform.H2)
@Test
public void selectBindNull_usingSetParameter_byName() {
String sql = "select nvl(cast(:val as int), 42)";
final Long val = DB.sqlQuery(sql)
.setParameter("val", null)
.mapToScalar(Long.class)
.findOne();
assertThat(val).isEqualTo(42);
}
@ForPlatform(Platform.H2) @ForPlatform(Platform.H2)
@Test @Test
public void selectFunction() { public void selectFunction() {