package com.avaje.ebeaninternal.server.expression; import java.sql.Timestamp; import java.util.Collection; import java.util.List; import java.util.Map; import java.util.Set; import com.avaje.ebean.*; import com.avaje.ebean.event.BeanQueryRequest; import com.avaje.ebean.text.PathProperties; import com.avaje.ebeaninternal.api.HashQueryPlanBuilder; import com.avaje.ebeaninternal.api.ManyWhereJoins; import com.avaje.ebeaninternal.api.SpiExpression; import com.avaje.ebeaninternal.api.SpiExpressionRequest; import com.avaje.ebeaninternal.server.deploy.BeanDescriptor; import com.avaje.ebeaninternal.util.DefaultExpressionList; /** * Junction implementation. */ abstract class JunctionExpression implements Junction, SpiExpression, ExpressionList { private static final long serialVersionUID = -7422204102750462676L; private static final String OR = " or "; private static final String AND = " and "; static class Conjunction extends JunctionExpression { private static final long serialVersionUID = -645619859900030678L; Conjunction(com.avaje.ebean.Query query, ExpressionList parent) { super(false, AND, query, parent); } } static class Disjunction extends JunctionExpression { private static final long serialVersionUID = -8464470066692221413L; Disjunction(com.avaje.ebean.Query query, ExpressionList parent) { super(true, OR, query, parent); } } private final DefaultExpressionList exprList; private final String joinType; /** * If true then a disjunction which means outer joins are required. */ private final boolean disjunction; JunctionExpression(boolean disjunction, String joinType, com.avaje.ebean.Query query, ExpressionList parent) { this.disjunction = disjunction; this.joinType = joinType; this.exprList = new DefaultExpressionList(query, parent); } @Override public void containsMany(BeanDescriptor desc, ManyWhereJoins manyWhereJoin) { List list = exprList.internalList(); // get the current state for 'require outer joins' boolean parentOuterJoins = manyWhereJoin.isRequireOuterJoins(); if (disjunction) { // turn on outer joins required for disjunction expressions manyWhereJoin.setRequireOuterJoins(true); } for (int i = 0; i < list.size(); i++) { list.get(i).containsMany(desc, manyWhereJoin); } if (disjunction && !parentOuterJoins) { // restore state to not forcing outer joins manyWhereJoin.setRequireOuterJoins(false); } } @Override public Junction add(Expression item) { SpiExpression i = (SpiExpression) item; exprList.add(i); return this; } @Override public Junction addAll(ExpressionList addList) { exprList.addAll(addList); return this; } @Override public void addBindValues(SpiExpressionRequest request) { List list = exprList.internalList(); for (int i = 0; i < list.size(); i++) { SpiExpression item = list.get(i); item.addBindValues(request); } } @Override public void addSql(SpiExpressionRequest request) { List list = exprList.internalList(); if (!list.isEmpty()) { request.append("("); for (int i = 0; i < list.size(); i++) { SpiExpression item = list.get(i); if (i > 0) { request.append(joinType); } item.addSql(request); } request.append(") "); } } /** * Based on Junction type and all the expression contained. */ @Override public void queryAutoFetchHash(HashQueryPlanBuilder builder) { builder.add(JunctionExpression.class).add(joinType); List list = exprList.internalList(); for (int i = 0; i < list.size(); i++) { list.get(i).queryAutoFetchHash(builder); } } @Override public void queryPlanHash(BeanQueryRequest request, HashQueryPlanBuilder builder) { builder.add(JunctionExpression.class).add(joinType); List list = exprList.internalList(); for (int i = 0; i < list.size(); i++) { list.get(i).queryPlanHash(request, builder); } } @Override public int queryBindHash() { int hc = JunctionExpression.class.getName().hashCode(); List list = exprList.internalList(); for (int i = 0; i < list.size(); i++) { hc = hc * 31 + list.get(i).queryBindHash(); } return hc; } @Override public ExpressionList endJunction() { return exprList.endJunction(); } @Override public ExpressionList allEq(Map propertyMap) { return exprList.allEq(propertyMap); } @Override public ExpressionList and(Expression expOne, Expression expTwo) { return exprList.and(expOne, expTwo); } @Override public ExpressionList between(String propertyName, Object value1, Object value2) { return exprList.between(propertyName, value1, value2); } @Override public ExpressionList betweenProperties(String lowProperty, String highProperty, Object value) { return exprList.betweenProperties(lowProperty, highProperty, value); } @Override public Junction conjunction() { return exprList.conjunction(); } @Override public ExpressionList contains(String propertyName, String value) { return exprList.contains(propertyName, value); } @Override public Junction disjunction() { return exprList.disjunction(); } @Override public ExpressionList endsWith(String propertyName, String value) { return exprList.endsWith(propertyName, value); } @Override public ExpressionList eq(String propertyName, Object value) { return exprList.eq(propertyName, value); } @Override public ExpressionList exampleLike(Object example) { return exprList.exampleLike(example); } @Override public ExpressionList filterMany(String prop) { throw new RuntimeException("filterMany not allowed on Junction expression list"); } @Override public Query asOf(Timestamp asOf) { return exprList.asOf(asOf); } @Override public List> findVersions() { return exprList.findVersions(); } @Override public List> findVersionsBetween(Timestamp start, Timestamp end) { return exprList.findVersionsBetween(start, end); } @Override public Query apply(PathProperties pathProperties) { return exprList.apply(pathProperties); } @Override public FutureIds findFutureIds() { return exprList.findFutureIds(); } @Override public FutureList findFutureList() { return exprList.findFutureList(); } @Override public FutureRowCount findFutureRowCount() { return exprList.findFutureRowCount(); } @Override public List findIds() { return exprList.findIds(); } @Override public void findEach(QueryEachConsumer consumer) { exprList.findEach(consumer); } @Override public void findEachWhile(QueryEachWhileConsumer consumer) { exprList.findEachWhile(consumer); } @Override public QueryIterator findIterate() { return exprList.findIterate(); } @Override public List findList() { return exprList.findList(); } @Override public Map findMap() { return exprList.findMap(); } @Override public Map findMap(String keyProperty, Class keyType) { return exprList.findMap(keyProperty, keyType); } @Override public PagedList findPagedList(int pageIndex, int pageSize) { return exprList.findPagedList(pageIndex, pageSize); } @Override public int findRowCount() { return exprList.findRowCount(); } @Override public Set findSet() { return exprList.findSet(); } @Override public T findUnique() { return exprList.findUnique(); } @Override public ExpressionList ge(String propertyName, Object value) { return exprList.ge(propertyName, value); } @Override public ExpressionList gt(String propertyName, Object value) { return exprList.gt(propertyName, value); } @Override public ExpressionList having() { throw new RuntimeException("having() not allowed on Junction expression list"); } @Override public ExpressionList icontains(String propertyName, String value) { return exprList.icontains(propertyName, value); } @Override public ExpressionList idEq(Object value) { return exprList.idEq(value); } @Override public ExpressionList idIn(List idValues) { return exprList.idIn(idValues); } @Override public ExpressionList iendsWith(String propertyName, String value) { return exprList.iendsWith(propertyName, value); } @Override public ExpressionList ieq(String propertyName, String value) { return exprList.ieq(propertyName, value); } @Override public ExpressionList iexampleLike(Object example) { return exprList.iexampleLike(example); } @Override public ExpressionList ilike(String propertyName, String value) { return exprList.ilike(propertyName, value); } @Override public ExpressionList in(String propertyName, Collection values) { return exprList.in(propertyName, values); } @Override public ExpressionList in(String propertyName, Object... values) { return exprList.in(propertyName, values); } @Override public ExpressionList in(String propertyName, com.avaje.ebean.Query subQuery) { return exprList.in(propertyName, subQuery); } @Override public ExpressionList notIn(String propertyName, Collection values) { return exprList.notIn(propertyName, values); } @Override public ExpressionList notIn(String propertyName, Object... values) { return exprList.notIn(propertyName, values); } @Override public ExpressionList notIn(String propertyName, com.avaje.ebean.Query subQuery) { return exprList.notIn(propertyName, subQuery); } @Override public ExpressionList exists(Query subQuery) { return exprList.exists(subQuery); } @Override public ExpressionList notExists(Query subQuery) { return exprList.exists(subQuery); } @Override public ExpressionList isNotNull(String propertyName) { return exprList.isNotNull(propertyName); } @Override public ExpressionList isNull(String propertyName) { return exprList.isNull(propertyName); } @Override public ExpressionList istartsWith(String propertyName, String value) { return exprList.istartsWith(propertyName, value); } @Override public ExpressionList le(String propertyName, Object value) { return exprList.le(propertyName, value); } @Override public ExpressionList like(String propertyName, String value) { return exprList.like(propertyName, value); } @Override public ExpressionList lt(String propertyName, Object value) { return exprList.lt(propertyName, value); } @Override public ExpressionList ne(String propertyName, Object value) { return exprList.ne(propertyName, value); } @Override public ExpressionList not(Expression exp) { return exprList.not(exp); } @Override public ExpressionList or(Expression expOne, Expression expTwo) { return exprList.or(expOne, expTwo); } @Override public OrderBy order() { return exprList.order(); } @Override public com.avaje.ebean.Query order(String orderByClause) { return exprList.order(orderByClause); } @Override public OrderBy orderBy() { return exprList.orderBy(); } @Override public com.avaje.ebean.Query orderBy(String orderBy) { return exprList.orderBy(orderBy); } @Override public com.avaje.ebean.Query query() { return exprList.query(); } @Override public ExpressionList raw(String raw, Object value) { return exprList.raw(raw, value); } @Override public ExpressionList raw(String raw, Object[] values) { return exprList.raw(raw, values); } @Override public ExpressionList raw(String raw) { return exprList.raw(raw); } @Override public com.avaje.ebean.Query select(String properties) { return exprList.select(properties); } @Override public Query setDistinct(boolean distinct) { return exprList.setDistinct(distinct); } @Override public com.avaje.ebean.Query setFirstRow(int firstRow) { return exprList.setFirstRow(firstRow); } @Override public com.avaje.ebean.Query setMapKey(String mapKey) { return exprList.setMapKey(mapKey); } @Override public com.avaje.ebean.Query setMaxRows(int maxRows) { return exprList.setMaxRows(maxRows); } @Override public com.avaje.ebean.Query setOrderBy(String orderBy) { return exprList.setOrderBy(orderBy); } @Override public com.avaje.ebean.Query setUseCache(boolean useCache) { return exprList.setUseCache(useCache); } @Override public ExpressionList startsWith(String propertyName, String value) { return exprList.startsWith(propertyName, value); } @Override public ExpressionList where() { return exprList.where(); } }