mirror of
https://github.com/ebean-orm/ebean.git
synced 2024-04-21 10:51:47 +00:00
#1212 - ENH: Add query findSingleAttribute()
This commit is contained in:
@@ -5,9 +5,9 @@ import io.ebean.search.MultiMatch;
|
||||
import io.ebean.search.TextCommonTerms;
|
||||
import io.ebean.search.TextQueryString;
|
||||
import io.ebean.search.TextSimple;
|
||||
import javax.annotation.Nullable;
|
||||
|
||||
import javax.annotation.Nonnull;
|
||||
import javax.annotation.Nullable;
|
||||
import javax.persistence.NonUniqueResultException;
|
||||
import java.sql.Timestamp;
|
||||
import java.util.Collection;
|
||||
@@ -261,6 +261,24 @@ public interface ExpressionList<T> {
|
||||
@Nonnull
|
||||
<A> List<A> findSingleAttributeList();
|
||||
|
||||
/**
|
||||
* Execute a query returning a single value of a single property/column.
|
||||
* <p>
|
||||
* <pre>{@code
|
||||
*
|
||||
* String name =
|
||||
* Ebean.find(Customer.class)
|
||||
* .select("name")
|
||||
* .where().eq("id", 42)
|
||||
* .findSingleAttribute();
|
||||
*
|
||||
* }</pre>
|
||||
*/
|
||||
default <A> A findSingleAttribute() {
|
||||
List<A> list = findSingleAttributeList();
|
||||
return !list.isEmpty() ? list.get(0) : null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Execute the query returning a single bean or null (if no matching
|
||||
* bean is found).
|
||||
|
||||
@@ -813,6 +813,21 @@ public interface Query<T> {
|
||||
@Nonnull
|
||||
<A> List<A> findSingleAttributeList();
|
||||
|
||||
/**
|
||||
* Execute a query returning a single value of a single property/column.
|
||||
* <p>
|
||||
* <pre>{@code
|
||||
*
|
||||
* String name =
|
||||
* Ebean.find(Customer.class)
|
||||
* .select("name")
|
||||
* .where().eq("id", 42)
|
||||
* .findSingleAttribute();
|
||||
*
|
||||
* }</pre>
|
||||
*/
|
||||
<A> A findSingleAttribute();
|
||||
|
||||
/**
|
||||
* Execute the query returning either a single bean or null (if no matching
|
||||
* bean is found).
|
||||
|
||||
@@ -1284,6 +1284,12 @@ public class DefaultOrmQuery<T> implements SpiQuery<T> {
|
||||
return (List<A>) server.findSingleAttributeList(this, null);
|
||||
}
|
||||
|
||||
@Override
|
||||
public <A> A findSingleAttribute() {
|
||||
List<A> list = findSingleAttributeList();
|
||||
return !list.isEmpty() ? list.get(0) : null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public T findOne() {
|
||||
return server.findOne(this, null);
|
||||
|
||||
@@ -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");
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user