All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.avaje.ebeaninternal.server.expression.JunctionExpression Maven / Gradle / Ivy

package com.avaje.ebeaninternal.server.expression;

import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.Set;

import com.avaje.ebean.Expression;
import com.avaje.ebean.ExpressionList;
import com.avaje.ebean.FutureIds;
import com.avaje.ebean.FutureList;
import com.avaje.ebean.FutureRowCount;
import com.avaje.ebean.Junction;
import com.avaje.ebean.OrderBy;
import com.avaje.ebean.PagingList;
import com.avaje.ebean.QueryIterator;
import com.avaje.ebean.QueryListener;
import com.avaje.ebean.QueryResultVisitor;
import com.avaje.ebean.event.BeanQueryRequest;
import com.avaje.ebeaninternal.api.ManyWhereJoins;
import com.avaje.ebeaninternal.api.SpiExpression;
import com.avaje.ebeaninternal.api.SpiExpressionRequest;
import com.avaje.ebeaninternal.api.SpiLuceneExpr;
import com.avaje.ebeaninternal.server.deploy.BeanDescriptor;
import com.avaje.ebeaninternal.server.query.LuceneResolvableRequest;
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(AND, query, parent);
		}
	}
	
	static class Disjunction extends JunctionExpression {
		
        private static final long serialVersionUID = -8464470066692221413L;

		Disjunction(com.avaje.ebean.Query query, ExpressionList parent){
			super(OR, query, parent);
		}
	}
	
	//private final ArrayList list = new ArrayList();
	private final DefaultExpressionList exprList;
	
	private final String joinType;

	
	JunctionExpression(String joinType, com.avaje.ebean.Query query, ExpressionList parent) {
		this.joinType = joinType;
		this.exprList = new DefaultExpressionList(query, parent);
	}
	
    public boolean isLuceneResolvable(LuceneResolvableRequest req) {
        
        List list = exprList.internalList();
        
        for (int i = 0; i < list.size(); i++) {
            if (!list.get(i).isLuceneResolvable(req)){
                return false;
            }
        }
        return true;
    }

    public SpiLuceneExpr createLuceneExpr(SpiExpressionRequest request) {

        boolean disjunction = OR.equals(joinType);
        return new JunctionExpressionLucene().createLuceneExpr(request,  exprList.internalList(), disjunction);
    }
    
	public void containsMany(BeanDescriptor desc, ManyWhereJoins manyWhereJoin) {
		
        List list = exprList.internalList();

		for (int i = 0; i < list.size(); i++) {
			list.get(i).containsMany(desc, manyWhereJoin);
		}
	}

	public Junction add(Expression item){
		SpiExpression i = (SpiExpression)item;
		exprList.add(i);
		return this;
	}
	
	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);
		}
	}
	
	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.
	 */
	public int queryAutoFetchHash() {
		int hc = JunctionExpression.class.getName().hashCode();
		hc = hc * 31 + joinType.hashCode();
		
        List list = exprList.internalList();
		for (int i = 0; i < list.size(); i++) {
			hc = hc * 31 + list.get(i).queryAutoFetchHash();
		}
		
		return hc;
	}
	
	public int queryPlanHash(BeanQueryRequest request) {
		int hc = JunctionExpression.class.getName().hashCode();
		hc = hc * 31 + joinType.hashCode();
		
        List list = exprList.internalList();
		for (int i = 0; i < list.size(); i++) {
			hc = hc * 31 + list.get(i).queryPlanHash(request);
		}
		
		return hc;
	}

	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;
	}

		
    public ExpressionList endJunction() {
        return exprList.endJunction();
    }

    public ExpressionList allEq(Map propertyMap) {
        return exprList.allEq(propertyMap);
    }

    public ExpressionList and(Expression expOne, Expression expTwo) {
        return exprList.and(expOne, expTwo);
    }

    public ExpressionList between(String propertyName, Object value1, Object value2) {
        return exprList.between(propertyName, value1, value2);
    }

    public ExpressionList betweenProperties(String lowProperty, String highProperty, Object value) {
        return exprList.betweenProperties(lowProperty, highProperty, value);
    }

    public Junction conjunction() {
        return exprList.conjunction();
    }

    public ExpressionList contains(String propertyName, String value) {
        return exprList.contains(propertyName, value);
    }

    public Junction disjunction() {
        return exprList.disjunction();
    }

    public ExpressionList endsWith(String propertyName, String value) {
        return exprList.endsWith(propertyName, value);
    }

    public ExpressionList eq(String propertyName, Object value) {
        return exprList.eq(propertyName, value);
    }

    public ExpressionList exampleLike(Object example) {
        return exprList.exampleLike(example);
    }

    public ExpressionList filterMany(String prop) {
        throw new RuntimeException("filterMany not allowed on Junction expression list");
    }

    public FutureIds findFutureIds() {
        return exprList.findFutureIds();
    }

    public FutureList findFutureList() {
        return exprList.findFutureList();
    }

    public FutureRowCount findFutureRowCount() {
        return exprList.findFutureRowCount();
    }
    
    public List findIds() {
        return exprList.findIds();
    }

    public void findVisit(QueryResultVisitor visitor) {
        exprList.findVisit(visitor);
    }

    public QueryIterator findIterate() {
        return exprList.findIterate();
    }
    
    public List findList() {
        return exprList.findList();
    }

    public Map findMap() {
        return exprList.findMap();
    }
    
    public  Map findMap(String keyProperty, Class keyType) {
        return exprList.findMap(keyProperty, keyType);
    }

    public PagingList findPagingList(int pageSize) {
        return exprList.findPagingList(pageSize);
    }

    public int findRowCount() {
        return exprList.findRowCount();
    }

    public Set findSet() {
        return exprList.findSet();
    }

    public T findUnique() {
        return exprList.findUnique();
    }

    public ExpressionList ge(String propertyName, Object value) {
        return exprList.ge(propertyName, value);
    }

    public ExpressionList gt(String propertyName, Object value) {
        return exprList.gt(propertyName, value);
    }

    public ExpressionList having() {
        throw new RuntimeException("having() not allowed on Junction expression list");
    }

    public ExpressionList icontains(String propertyName, String value) {
        return exprList.icontains(propertyName, value);
    }

    public ExpressionList idEq(Object value) {
        return exprList.idEq(value);
    }

    public ExpressionList idIn(List idValues) {
        return exprList.idIn(idValues);
    }

    public ExpressionList iendsWith(String propertyName, String value) {
        return exprList.iendsWith(propertyName, value);
    }

    public ExpressionList ieq(String propertyName, String value) {
        return exprList.ieq(propertyName, value);
    }

    public ExpressionList iexampleLike(Object example) {
        return exprList.iexampleLike(example);
    }

    public ExpressionList ilike(String propertyName, String value) {
        return exprList.ilike(propertyName, value);
    }

    public ExpressionList in(String propertyName, Collection values) {
        return exprList.in(propertyName, values);
    }

    public ExpressionList in(String propertyName, Object... values) {
        return exprList.in(propertyName, values);
    }

    public ExpressionList in(String propertyName, com.avaje.ebean.Query subQuery) {
        return exprList.in(propertyName, subQuery);
    }

    public ExpressionList isNotNull(String propertyName) {
        return exprList.isNotNull(propertyName);
    }

    public ExpressionList isNull(String propertyName) {
        return exprList.isNull(propertyName);
    }

    public ExpressionList istartsWith(String propertyName, String value) {
        return exprList.istartsWith(propertyName, value);
    }

    public com.avaje.ebean.Query join(String assocProperty, String assocProperties) {
        return exprList.join(assocProperty, assocProperties);
    }

    public com.avaje.ebean.Query join(String assocProperties) {
        return exprList.join(assocProperties);
    }

    public ExpressionList le(String propertyName, Object value) {
        return exprList.le(propertyName, value);
    }

    public ExpressionList like(String propertyName, String value) {
        return exprList.like(propertyName, value);
    }

    public ExpressionList lt(String propertyName, Object value) {
        return exprList.lt(propertyName, value);
    }

    public ExpressionList lucene(String propertyName, String value) {
        return exprList.lucene(propertyName, value);
    }

    public ExpressionList ne(String propertyName, Object value) {
        return exprList.ne(propertyName, value);
    }

    public ExpressionList not(Expression exp) {
        return exprList.not(exp);
    }

    public ExpressionList or(Expression expOne, Expression expTwo) {
        return exprList.or(expOne, expTwo);
    }

    public OrderBy order() {
        return exprList.order();
    }

    public com.avaje.ebean.Query order(String orderByClause) {
        return exprList.order(orderByClause);
    }

    public OrderBy orderBy() {
        return exprList.orderBy();
    }

    public com.avaje.ebean.Query orderBy(String orderBy) {
        return exprList.orderBy(orderBy);
    }

    public com.avaje.ebean.Query query() {
        return exprList.query();
    }

    public ExpressionList raw(String raw, Object value) {
        return exprList.raw(raw, value);
    }

    public ExpressionList raw(String raw, Object[] values) {
        return exprList.raw(raw, values);
    }

    public ExpressionList raw(String raw) {
        return exprList.raw(raw);
    }

    public com.avaje.ebean.Query select(String properties) {
        return exprList.select(properties);
    }

    public com.avaje.ebean.Query setBackgroundFetchAfter(int backgroundFetchAfter) {
        return exprList.setBackgroundFetchAfter(backgroundFetchAfter);
    }

    public com.avaje.ebean.Query setFirstRow(int firstRow) {
        return exprList.setFirstRow(firstRow);
    }

    public com.avaje.ebean.Query setListener(QueryListener queryListener) {
        return exprList.setListener(queryListener);
    }

    public com.avaje.ebean.Query setMapKey(String mapKey) {
        return exprList.setMapKey(mapKey);
    }

    public com.avaje.ebean.Query setMaxRows(int maxRows) {
        return exprList.setMaxRows(maxRows);
    }

    public com.avaje.ebean.Query setOrderBy(String orderBy) {
        return exprList.setOrderBy(orderBy);
    }

    public com.avaje.ebean.Query setUseCache(boolean useCache) {
        return exprList.setUseCache(useCache);
    }

    public ExpressionList startsWith(String propertyName, String value) {
        return exprList.startsWith(propertyName, value);
    }

    public ExpressionList where() {
        return exprList.where();
    }
	
	
	
}