mirror of
https://github.com/ebean-orm/ebean.git
synced 2024-04-21 10:51:47 +00:00
Added SQL command "EXISTS" and "NOT EXISTS" to Ebean (ExpressionFactory,
ExpressionList).
This commit is contained in:
@@ -169,6 +169,16 @@ public interface ExpressionFactory {
|
||||
* In - property has a value in the collection of values.
|
||||
*/
|
||||
public Expression in(String propertyName, Collection<?> values);
|
||||
|
||||
/**
|
||||
* Exists expression
|
||||
*/
|
||||
public Expression exists(Query<?> subQuery);
|
||||
|
||||
/**
|
||||
* Not exists expression
|
||||
*/
|
||||
public Expression notExists(Query<?> subQuery);
|
||||
|
||||
/**
|
||||
* Id Equal to - ID property is equal to the value.
|
||||
|
||||
@@ -473,6 +473,16 @@ public interface ExpressionList<T> extends Serializable {
|
||||
* In - property has a value in the collection of values.
|
||||
*/
|
||||
public ExpressionList<T> in(String propertyName, Collection<?> values);
|
||||
|
||||
/**
|
||||
* Exists expression
|
||||
*/
|
||||
public ExpressionList<T> exists(Query<?> subQuery);
|
||||
|
||||
/**
|
||||
* Not exists expression
|
||||
*/
|
||||
public ExpressionList<T> notExists(Query<?> subQuery);
|
||||
|
||||
/**
|
||||
* Id IN a list of id values.
|
||||
|
||||
@@ -241,6 +241,23 @@ public class DefaultExpressionFactory implements SpiExpressionFactory {
|
||||
public Expression in(String propertyName, Collection<?> values) {
|
||||
return new InExpression(propertyName, values);
|
||||
}
|
||||
|
||||
/**
|
||||
* Exists subquery
|
||||
*/
|
||||
@Override
|
||||
public Expression exists(Query<?> subQuery) {
|
||||
return new ExistsExpression((SpiQuery<?>) subQuery, false);
|
||||
}
|
||||
|
||||
/**
|
||||
* Not exists subquery
|
||||
*/
|
||||
@Override
|
||||
public Expression notExists(Query<?> subQuery) {
|
||||
return new ExistsExpression((SpiQuery<?>) subQuery, true);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Id Equal to - ID property is equal to the value.
|
||||
|
||||
@@ -0,0 +1,87 @@
|
||||
package com.avaje.ebeaninternal.server.expression;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import com.avaje.ebean.event.BeanQueryRequest;
|
||||
import com.avaje.ebeaninternal.api.HashQueryPlanBuilder;
|
||||
import com.avaje.ebeaninternal.api.ManyWhereJoins;
|
||||
import com.avaje.ebeaninternal.api.SpiEbeanServer;
|
||||
import com.avaje.ebeaninternal.api.SpiExpression;
|
||||
import com.avaje.ebeaninternal.api.SpiExpressionRequest;
|
||||
import com.avaje.ebeaninternal.api.SpiQuery;
|
||||
import com.avaje.ebeaninternal.server.deploy.BeanDescriptor;
|
||||
import com.avaje.ebeaninternal.server.query.CQuery;
|
||||
|
||||
public class ExistsExpression implements SpiExpression {
|
||||
|
||||
private static final long serialVersionUID = 666990277309851644L;
|
||||
|
||||
private final boolean not;
|
||||
private final SpiQuery<?> subQuery;
|
||||
|
||||
private transient CQuery<?> compiledSubQuery;
|
||||
|
||||
public ExistsExpression(SpiQuery<?> subQuery, boolean not) {
|
||||
this.subQuery = subQuery;
|
||||
this.not=not;
|
||||
}
|
||||
|
||||
public void queryAutoFetchHash(HashQueryPlanBuilder builder) {
|
||||
builder.add(ExistsExpression.class).add(not);
|
||||
|
||||
subQuery.queryAutofetchHash(builder);
|
||||
}
|
||||
|
||||
public void queryPlanHash(BeanQueryRequest<?> request, HashQueryPlanBuilder builder) {
|
||||
|
||||
// queryPlanHash executes prior to addSql() or addBindValues()
|
||||
// ... so compiledQuery will exist
|
||||
compiledSubQuery = compileSubQuery(request);
|
||||
|
||||
queryAutoFetchHash(builder);
|
||||
}
|
||||
|
||||
/**
|
||||
* Compile/build the sub query.
|
||||
*/
|
||||
private CQuery<?> compileSubQuery(BeanQueryRequest<?> queryRequest) {
|
||||
|
||||
SpiEbeanServer ebeanServer = (SpiEbeanServer) queryRequest.getEbeanServer();
|
||||
return ebeanServer.compileQuery(subQuery, queryRequest.getTransaction());
|
||||
}
|
||||
|
||||
public int queryBindHash() {
|
||||
return subQuery.queryBindHash();
|
||||
}
|
||||
|
||||
public void addSql(SpiExpressionRequest request) {
|
||||
|
||||
String subSelect = compiledSubQuery.getGeneratedSql();
|
||||
subSelect = subSelect.replace('\n', ' ');
|
||||
|
||||
|
||||
if(not) request.append(" not");
|
||||
request.append(" exists (");
|
||||
request.append(subSelect);
|
||||
request.append(") ");
|
||||
}
|
||||
|
||||
public void addBindValues(SpiExpressionRequest request) {
|
||||
|
||||
List<Object> bindParams = compiledSubQuery.getPredicates().getWhereExprBindValues();
|
||||
|
||||
if (bindParams == null) {
|
||||
return;
|
||||
}
|
||||
|
||||
for (int i = 0; i < bindParams.size(); i++) {
|
||||
request.addBindValue(bindParams.get(i));
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void containsMany(BeanDescriptor<?> desc, ManyWhereJoins whereManyJoins) {
|
||||
// TODO Auto-generated method stub
|
||||
|
||||
}
|
||||
}
|
||||
@@ -361,6 +361,16 @@ abstract class JunctionExpression<T> implements Junction<T>, SpiExpression, Expr
|
||||
public ExpressionList<T> in(String propertyName, com.avaje.ebean.Query<?> subQuery) {
|
||||
return exprList.in(propertyName, subQuery);
|
||||
}
|
||||
|
||||
@Override
|
||||
public ExpressionList<T> exists(Query<?> subQuery) {
|
||||
return exprList.exists(subQuery);
|
||||
}
|
||||
|
||||
@Override
|
||||
public ExpressionList<T> notExists(Query<?> subQuery) {
|
||||
return exprList.exists(subQuery);
|
||||
}
|
||||
|
||||
@Override
|
||||
public ExpressionList<T> isNotNull(String propertyName) {
|
||||
|
||||
@@ -468,6 +468,19 @@ public class DefaultExpressionList<T> implements SpiExpressionList<T> {
|
||||
add(expr.in(propertyName, values));
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public ExpressionList<T> exists(Query<?> subQuery) {
|
||||
add(expr.exists(subQuery));
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public ExpressionList<T> notExists(Query<?> subQuery) {
|
||||
add(expr.notExists(subQuery));
|
||||
return this;
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public ExpressionList<T> isNotNull(String propertyName) {
|
||||
|
||||
@@ -0,0 +1,41 @@
|
||||
package com.avaje.tests.query;
|
||||
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
|
||||
import com.avaje.ebean.BaseTestCase;
|
||||
import com.avaje.ebean.Ebean;
|
||||
import com.avaje.ebean.Query;
|
||||
import com.avaje.tests.model.basic.Customer;
|
||||
import com.avaje.tests.model.basic.Order;
|
||||
import com.avaje.tests.model.basic.ResetBasicData;
|
||||
|
||||
public class TestQueryExists extends BaseTestCase {
|
||||
@Test
|
||||
public void testExists() {
|
||||
ResetBasicData.reset();
|
||||
|
||||
Query<Order> subQuery=Ebean.find(Order.class).alias("sq").select("id").where().raw("sq.kcustomer_id = qt.id")
|
||||
.query();
|
||||
Query<Customer> query=Ebean.find(Customer.class).alias("qt").where().exists(subQuery).query();
|
||||
|
||||
query.findList();
|
||||
String sql=query.getGeneratedSql();
|
||||
|
||||
Assert.assertTrue(sql.indexOf("exists (")>0);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testNotExists() {
|
||||
ResetBasicData.reset();
|
||||
|
||||
Query<Order> subQuery=Ebean.find(Order.class).alias("sq").select("id").where().raw("sq.kcustomer_id = qt.id")
|
||||
.query();
|
||||
Query<Customer> query=Ebean.find(Customer.class).alias("qt").where().notExists(subQuery).query();
|
||||
|
||||
query.findList();
|
||||
String sql=query.getGeneratedSql();
|
||||
|
||||
Assert.assertTrue(sql.indexOf("not exists (")>0);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user