#1645 - ENH: Add expression rawOrEmpty() ... as a convenience for conditionally added a raw expression

This commit is contained in:
rob bygrave
2019-03-05 22:13:38 +13:00
parent 86d0216159
commit e195f20981
5 changed files with 162 additions and 5 deletions
@@ -6,6 +6,7 @@ import io.ebean.Expr;
import io.ebean.Query;
import io.ebean.annotation.ForPlatform;
import io.ebean.annotation.Platform;
import org.ebeantest.LoggedSqlCollector;
import org.junit.Test;
import org.tests.model.basic.Customer;
import org.tests.model.basic.Order;
@@ -13,6 +14,7 @@ import org.tests.model.basic.OrderDetail;
import org.tests.model.basic.ResetBasicData;
import java.sql.Timestamp;
import java.util.Collections;
import java.util.List;
import static java.util.Arrays.asList;
@@ -65,6 +67,54 @@ public class TestWhereRawClause extends BaseTestCase {
assertThat(sqlOf(query)).contains(" t0.id in (select c.id from o_customer c where c.name in (?,?,?))");
}
@Test
public void testRawOrEmpty_when_notEmpty() {
ResetBasicData.reset();
Query<Customer> query = Ebean.find(Customer.class)
.select("name")
.where()
.rawOrEmpty("id in (select c.id from o_customer c where c.name in (?1))", asList("Rob", "Fiona", "Jack"))
.query();
List<Customer> list = query.findList();
assertThat(list).isNotEmpty();
assertThat(sqlOf(query)).contains(" t0.id in (select c.id from o_customer c where c.name in (?,?,?))");
}
@Test
public void testRawOrEmpty_when_null() {
ResetBasicData.reset();
Query<Customer> query = Ebean.find(Customer.class)
.select("name")
.where()
.rawOrEmpty("id in (select c.id from o_customer c where c.name in (?1))", null)
.query();
List<Customer> list = query.findList();
assertThat(list).isNotEmpty();
assertThat(sqlOf(query)).isEqualTo("select t0.id, t0.name from o_customer t0");
}
@Test
public void testRawOrEmpty_when_empty() {
ResetBasicData.reset();
Query<Customer> query = Ebean.find(Customer.class)
.select("name")
.where()
.rawOrEmpty("id in (select c.id from o_customer c where c.name in (?1))", Collections.emptySet())
.query();
List<Customer> list = query.findList();
assertThat(list).isNotEmpty();
assertThat(sqlOf(query)).isEqualTo("select t0.id, t0.name from o_customer t0");
}
@Test
public void testRaw_bindExpansion() {
@@ -94,6 +144,33 @@ public class TestWhereRawClause extends BaseTestCase {
assertThat(list).isNotEmpty();
}
@Test
@ForPlatform(Platform.POSTGRES)
public void testRawOrEmpty_PostgresArray() {
ResetBasicData.reset();
LoggedSqlCollector.start();
Ebean.find(Customer.class)
.select("name").where().rawOrEmpty("name = any(?)", asList("Rob", "Fiona", "Jack"))
.findList();
Ebean.find(Customer.class)
.select("name").where().rawOrEmpty("name = any(?)", asList())
.findList();
Ebean.find(Customer.class)
.select("name").where().rawOrEmpty("name = any(?)", null)
.findList();
List<String> sql = LoggedSqlCollector.stop();
assertThat(sql.get(0)).isEqualTo("select t0.id, t0.name from o_customer t0 where t0.name = any(?); --bind(Array[3]={Rob,Fiona,Jack})");
assertThat(sql.get(1)).isEqualTo("select t0.id, t0.name from o_customer t0; --bind()");
assertThat(sql.get(2)).isEqualTo("select t0.id, t0.name from o_customer t0; --bind()");
}
@Test
public void testRawWithBindParams() {