#1212 - ENH: Add query findSingleAttribute()

This commit is contained in:
Rob Bygrave
2017-11-18 22:36:08 +13:00
parent d5555ed9a9
commit fcff571664
4 changed files with 132 additions and 65 deletions
@@ -75,12 +75,40 @@ public class TestQuerySingleAttribute extends BaseTestCase {
Query<Customer> query = Ebean.find(Customer.class).select("name");
List<String> names = query.findSingleAttributeList();//String.class);
List<String> names = query.findSingleAttributeList();
assertThat(sqlOf(query)).contains("select t0.name from o_customer t0");
assertThat(names).isNotNull();
}
@Test
public void findSingleAttribute() {
ResetBasicData.reset();
Query<Customer> query = Ebean.find(Customer.class)
.select("name")
.where().eq("id", 1).query();
String name = query.findSingleAttribute();
assertThat(sqlOf(query)).contains("select t0.name from o_customer t0");
assertThat(name).isNotNull();
}
@Test
public void findSingleAttribute_viaExpression() {
ResetBasicData.reset();
String name = Ebean.find(Customer.class)
.select("name")
.where().eq("id", 1)
.findSingleAttribute();
assertThat(name).isNotNull();
}
@Test
public void distinctAndWhere() {
@@ -321,89 +349,89 @@ public class TestQuerySingleAttribute extends BaseTestCase {
assertThat(sqlOf(query)).contains("select distinct t1.billing_address_id from contact t0 join o_customer t1 on t1.id = t0.customer_id order by t1.billing_address_id desc");
}
@Test
public void distinctWithOrderByPkAndQuery() {
ResetBasicData.reset();
Query<Contact> query = Ebean.find(Contact.class)
.setDistinct(true)
.fetch("customer","billingAddress")
@Test
public void distinctWithOrderByPkAndQuery() {
ResetBasicData.reset();
Query<Contact> query = Ebean.find(Contact.class)
.setDistinct(true)
.fetch("customer","billingAddress")
.where().eq("customer.billingAddress.city", "Auckland")
.orderBy().desc("customer.billingAddress.id");
List<Integer> ids = query.findSingleAttributeList();
.orderBy().desc("customer.billingAddress.id");
List<Integer> ids = query.findSingleAttributeList();
assertThat(ids).isNotEmpty();
assertThat(sqlOf(query)).contains("select distinct t1.billing_address_id from contact t0 "
+ "join o_customer t1 on t1.id = t0.customer_id " // two spaces!
+ "left join o_address t2 on t2.id = t1.billing_address_id "
+ "where t2.city = ? "
+ "order by t1.billing_address_id desc");
}
@Test
public void distinctWithCascadedFetchOrderByPkAndQuery() {
ResetBasicData.reset();
Query<Contact> query = Ebean.find(Contact.class)
.setDistinct(true)
.fetch("customer","billingAddress")
.where().eq("customer.billingAddress.city", "Auckland")
.orderBy().desc("customer.billingAddress.id");
List<Short> ids = query.findSingleAttributeList();
assertThat(ids).isNotEmpty();
assertThat(sqlOf(query)).contains("select distinct t1.billing_address_id from contact t0 "
+ "join o_customer t1 on t1.id = t0.customer_id "
+ "left join o_address t2 on t2.id = t1.billing_address_id "
+ "where t2.city = ? "
+ "order by t1.billing_address_id desc");
+ "order by t1.billing_address_id desc");
}
@Test
public void distinctWithCascadedFetchOrderByPkAndQuery3() {
ResetBasicData.reset();
Query<Contact> query = Ebean.find(Contact.class)
.setDistinct(true)
@Test
public void distinctWithCascadedFetchOrderByPkAndQuery() {
ResetBasicData.reset();
Query<Contact> query = Ebean.find(Contact.class)
.setDistinct(true)
.fetch("customer","billingAddress")
.where().eq("customer.billingAddress.city", "Auckland")
.orderBy().desc("customer.billingAddress.id");
List<Integer> ids = query.findSingleAttributeList();
.orderBy().desc("customer.billingAddress.id");
List<Short> ids = query.findSingleAttributeList();
assertThat(ids).isNotEmpty();
assertThat(sqlOf(query)).contains("select distinct t1.billing_address_id from contact t0 "
+ "join o_customer t1 on t1.id = t0.customer_id "
+ "join o_customer t1 on t1.id = t0.customer_id "
+ "left join o_address t2 on t2.id = t1.billing_address_id "
+ "where t2.city = ? "
+ "order by t1.billing_address");
+ "order by t1.billing_address_id desc");
}
@Test
public void distinctWithCascadedFetchOrderByPkAndQuery2() {
ResetBasicData.reset();
Query<Contact> query = Ebean.find(Contact.class)
.setDistinct(true)
.fetch("customer","billingAddress")
.where().eq("customer.shippingAddress.city", "Auckland") // query on shippingAddress
.orderBy().desc("customer.billingAddress.id");
List<Short> ids = query.findSingleAttributeList();
@Test
public void distinctWithCascadedFetchOrderByPkAndQuery3() {
ResetBasicData.reset();
Query<Contact> query = Ebean.find(Contact.class)
.setDistinct(true)
.fetch("customer","billingAddress")
.where().eq("customer.billingAddress.city", "Auckland")
.orderBy().desc("customer.billingAddress.id");
List<Integer> ids = query.findSingleAttributeList();
assertThat(ids).isNotEmpty();
assertThat(sqlOf(query)).contains("select distinct t1.billing_address_id from contact t0 "
+ "join o_customer t1 on t1.id = t0.customer_id "
+ "join o_customer t1 on t1.id = t0.customer_id "
+ "left join o_address t2 on t2.id = t1.billing_address_id "
+ "where t2.city = ? "
+ "order by t1.billing_address");
}
@Test
public void distinctWithCascadedFetchOrderByPkAndQuery2() {
ResetBasicData.reset();
Query<Contact> query = Ebean.find(Contact.class)
.setDistinct(true)
.fetch("customer","billingAddress")
.where().eq("customer.shippingAddress.city", "Auckland") // query on shippingAddress
.orderBy().desc("customer.billingAddress.id");
List<Short> ids = query.findSingleAttributeList();
assertThat(ids).isNotEmpty();
assertThat(sqlOf(query)).contains("select distinct t1.billing_address_id from contact t0 "
+ "join o_customer t1 on t1.id = t0.customer_id "
+ "left join o_address t2 on t2.id = t1.shipping_address_id "
+ "where t2.city = ? "
+ "order by t1.billing_address_id desc");
+ "order by t1.billing_address_id desc");
}
}