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

com.avaje.ebeaninternal.util.DefaultExpressionList Maven / Gradle / Ivy

There is a newer version: 2.8.1
Show newest version
package com.avaje.ebeaninternal.util;

import java.util.ArrayList;
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.ExpressionFactory;
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.Query;
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.SpiExpressionList;
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;

/**
 * Default implementation of ExpressionList.
 */
public class DefaultExpressionList implements SpiExpressionList {

    private static final long serialVersionUID = -6992345500247035947L;

    private final ArrayList list = new ArrayList();

    private final Query query;

    private final ExpressionList parentExprList;

    private transient ExpressionFactory expr;
    
    private final String exprLang;
    private final String listAndStart;
    private final String listAndEnd;
    private final String listAndJoin;

    public DefaultExpressionList(Query query, ExpressionList parentExprList) {
        this(query, query.getExpressionFactory(), parentExprList);
    }
    
    public DefaultExpressionList(Query query, ExpressionFactory expr, ExpressionList parentExprList) {
        this.query = query;
        this.expr = expr;
        this.exprLang = expr.getLang();
        this.parentExprList = parentExprList;
        
        if ("ldap".equals(exprLang)){
            // Language is LDAP
            listAndStart = "(&";
            listAndEnd = ")";
            listAndJoin = "";
            
        } else {
            listAndStart = "";
            listAndEnd = "";
            listAndJoin = " and ";
        }
    }
    
    public void trimPath(int prefixTrim) {
        throw new RuntimeException("Only allowed on FilterExpressionList");
    }
    
    public List internalList() {
        return list;
    }
    
    public boolean isLuceneResolvable(LuceneResolvableRequest req) {
        for (int i = 0; i < list.size(); i++) {
            if (!list.get(i).isLuceneResolvable(req)) {
                return false;
            }
        }
        return true;
    }

    public SpiLuceneExpr createLuceneExpr(SpiExpressionRequest request, SpiLuceneExpr.ExprOccur occur) {
        
        LuceneQueryList queryList = new LuceneQueryList(occur);
        for (int i = 0; i < list.size(); i++) {
            SpiLuceneExpr query = list.get(i).createLuceneExpr(request);
            queryList.add(query);
        }
        return queryList;       
    }

    
    /**
     * Set the ExpressionFactory.
     * 

* After deserialisation so that it can be further modified. *

*/ public void setExpressionFactory(ExpressionFactory expr) { this.expr = expr; } /** * Return a copy of the expression list. *

* Each of the expressions are expected to be immutable and safe to * reference. *

*/ public DefaultExpressionList copy(Query query) { DefaultExpressionList copy = new DefaultExpressionList(query, expr, null); copy.list.addAll(list); return copy; } /** * Return true if one of the expressions is related to a Many property. */ public void containsMany(BeanDescriptor desc, ManyWhereJoins whereManyJoins) { for (int i = 0; i < list.size(); i++) { list.get(i).containsMany(desc, whereManyJoins); } } public ExpressionList endJunction() { return parentExprList == null ? this : parentExprList; } public Query query() { return query; } public ExpressionList where() { return query.where(); } public OrderBy order() { return query.order(); } public OrderBy orderBy() { return query.order(); } public Query order(String orderByClause) { return query.order(orderByClause); } public Query orderBy(String orderBy) { return query.order(orderBy); } public Query setOrderBy(String orderBy) { return query.order(orderBy); } public FutureIds findFutureIds() { return query.findFutureIds(); } public FutureRowCount findFutureRowCount() { return query.findFutureRowCount(); } public FutureList findFutureList() { return query.findFutureList(); } public PagingList findPagingList(int pageSize) { return query.findPagingList(pageSize); } public int findRowCount() { return query.findRowCount(); } public List findIds() { return query.findIds(); } public void findVisit(QueryResultVisitor visitor) { query.findVisit(visitor); } public QueryIterator findIterate() { return query.findIterate(); } public List findList() { return query.findList(); } public Set findSet() { return query.findSet(); } public Map findMap() { return query.findMap(); } public Map findMap(String keyProperty, Class keyType) { return query.findMap(keyProperty, keyType); } public T findUnique() { return query.findUnique(); } public ExpressionList filterMany(String prop) { return query.filterMany(prop); } public Query select(String fetchProperties) { return query.select(fetchProperties); } public Query join(String assocProperties) { return query.fetch(assocProperties); } public Query join(String assocProperty, String assocProperties) { return query.fetch(assocProperty, assocProperties); } public Query setFirstRow(int firstRow) { return query.setFirstRow(firstRow); } public Query setMaxRows(int maxRows) { return query.setMaxRows(maxRows); } public Query setBackgroundFetchAfter(int backgroundFetchAfter) { return query.setBackgroundFetchAfter(backgroundFetchAfter); } public Query setMapKey(String mapKey) { return query.setMapKey(mapKey); } public Query setListener(QueryListener queryListener) { return query.setListener(queryListener); } public Query setUseCache(boolean useCache) { return query.setUseCache(useCache); } public ExpressionList having() { return query.having(); } public ExpressionList add(Expression expr) { list.add((SpiExpression) expr); return this; } public boolean isEmpty() { return list.isEmpty(); } public String buildSql(SpiExpressionRequest request) { request.append(listAndStart); for (int i = 0, size = list.size(); i < size; i++) { SpiExpression expression = list.get(i); if (i > 0) { request.append(listAndJoin); } expression.addSql(request); } request.append(listAndEnd); return request.getSql(); } public ArrayList buildBindValues(SpiExpressionRequest request) { for (int i = 0, size = list.size(); i < size; i++) { SpiExpression expression = list.get(i); expression.addBindValues(request); } return request.getBindValues(); } /** * Calculate a hash based on the expressions but excluding the actual bind * values. */ public int queryAutoFetchHash() { int hash = DefaultExpressionList.class.getName().hashCode(); for (int i = 0, size = list.size(); i < size; i++) { SpiExpression expression = list.get(i); hash = hash * 31 + expression.queryAutoFetchHash(); } return hash; } /** * Calculate a hash based on the expressions but excluding the actual bind * values. */ public int queryPlanHash(BeanQueryRequest request) { int hash = DefaultExpressionList.class.getName().hashCode(); for (int i = 0, size = list.size(); i < size; i++) { SpiExpression expression = list.get(i); hash = hash * 31 + expression.queryPlanHash(request); } return hash; } /** * Calculate a hash based on the expressions. */ public int queryBindHash() { int hash = DefaultExpressionList.class.getName().hashCode(); for (int i = 0, size = list.size(); i < size; i++) { SpiExpression expression = list.get(i); hash = hash * 31 + expression.queryBindHash(); } return hash; } public ExpressionList eq(String propertyName, Object value) { add(expr.eq(propertyName, value)); return this; } public ExpressionList ieq(String propertyName, String value) { add(expr.ieq(propertyName, value)); return this; } public ExpressionList ne(String propertyName, Object value) { add(expr.ne(propertyName, value)); return this; } public ExpressionList allEq(Map propertyMap) { add(expr.allEq(propertyMap)); return this; } public ExpressionList and(Expression expOne, Expression expTwo) { add(expr.and(expOne, expTwo)); return this; } public ExpressionList between(String propertyName, Object value1, Object value2) { add(expr.between(propertyName, value1, value2)); return this; } public ExpressionList betweenProperties(String lowProperty, String highProperty, Object value) { add(expr.betweenProperties(lowProperty, highProperty, value)); return this; } public Junction conjunction() { Junction conjunction = expr.conjunction(query, this); add(conjunction); return conjunction; } public ExpressionList contains(String propertyName, String value) { add(expr.contains(propertyName, value)); return this; } public ExpressionList lucene(String propertyName, String value) { add(expr.lucene(propertyName, value)); return this; } public Junction disjunction() { Junction disjunction = expr.disjunction(query, this); add(disjunction); return disjunction; } public ExpressionList endsWith(String propertyName, String value) { add(expr.endsWith(propertyName, value)); return this; } public ExpressionList ge(String propertyName, Object value) { add(expr.ge(propertyName, value)); return this; } public ExpressionList gt(String propertyName, Object value) { add(expr.gt(propertyName, value)); return this; } public ExpressionList icontains(String propertyName, String value) { add(expr.icontains(propertyName, value)); return this; } public ExpressionList idIn(List idList) { add(expr.idIn(idList)); return this; } public ExpressionList idEq(Object value) { add(expr.idEq(value)); return this; } public ExpressionList iendsWith(String propertyName, String value) { add(expr.iendsWith(propertyName, value)); return this; } public ExpressionList ilike(String propertyName, String value) { add(expr.ilike(propertyName, value)); return this; } public ExpressionList in(String propertyName, Query subQuery) { add(expr.in(propertyName, subQuery)); return this; } public ExpressionList in(String propertyName, Collection values) { add(expr.in(propertyName, values)); return this; } public ExpressionList in(String propertyName, Object... values) { add(expr.in(propertyName, values)); return this; } public ExpressionList isNotNull(String propertyName) { add(expr.isNotNull(propertyName)); return this; } public ExpressionList isNull(String propertyName) { add(expr.isNull(propertyName)); return this; } public ExpressionList istartsWith(String propertyName, String value) { add(expr.istartsWith(propertyName, value)); return this; } public ExpressionList le(String propertyName, Object value) { add(expr.le(propertyName, value)); return this; } public ExpressionList exampleLike(Object example) { add(expr.exampleLike(example)); return this; } public ExpressionList iexampleLike(Object example) { add(expr.iexampleLike(example)); return this; } public ExpressionList like(String propertyName, String value) { add(expr.like(propertyName, value)); return this; } public ExpressionList lt(String propertyName, Object value) { add(expr.lt(propertyName, value)); return this; } public ExpressionList not(Expression exp) { add(expr.not(exp)); return this; } public ExpressionList or(Expression expOne, Expression expTwo) { add(expr.or(expOne, expTwo)); return this; } public ExpressionList raw(String raw, Object value) { add(expr.raw(raw, value)); return this; } public ExpressionList raw(String raw, Object[] values) { add(expr.raw(raw, values)); return this; } public ExpressionList raw(String raw) { add(expr.raw(raw)); return this; } public ExpressionList startsWith(String propertyName, String value) { add(expr.startsWith(propertyName, value)); return this; } }