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

org.ibatis.persist.impl.CriteriaManipulation Maven / Gradle / Ivy

Go to download

The jBATIS persistence framework will help you to significantly reduce the amount of Java code that you normally need to access a relational database. iBATIS simply maps JavaBeans to SQL statements using a very simple XML descriptor.

The newest version!
package org.ibatis.persist.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;

import org.ibatis.persist.Parameter;
import org.ibatis.persist.criteria.CommonAbstractCriteria;
import org.ibatis.persist.criteria.Expression;
import org.ibatis.persist.criteria.Predicate;
import org.ibatis.persist.criteria.Root;
import org.ibatis.persist.criteria.Subquery;
import org.ibatis.persist.impl.path.RootImpl;
import org.ibatis.persist.meta.EntityType;

import com.ibatis.sqlmap.engine.impl.SqlMapExecutorDelegate;
import com.ibatis.sqlmap.engine.mapping.parameter.ParameterMap;
import com.ibatis.sqlmap.engine.mapping.parameter.ParameterMapping;

@SuppressWarnings("unchecked")
public abstract class CriteriaManipulation implements CriteriaStatement, CommonAbstractCriteria {
    private final CriteriaBuilderImpl criteriaBuilder;

    private RootImpl root;
    private Predicate restriction;
    // private List> subQueries;

    protected CriteriaManipulation(CriteriaBuilderImpl criteriaBuilder, Class entityClass) {
        this.criteriaBuilder = criteriaBuilder;
        EntityType entityType = criteriaBuilder.getEntityManager().initEntityClass(entityClass);
        if (entityType != null && !entityType.isFailed()) {
            from(entityType);
        }
    }

    protected CriteriaBuilderImpl criteriaBuilder() {
        return criteriaBuilder;
    }

    // Root ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

    public Root from(Class entityClass) {
        EntityType entityType = criteriaBuilder.getEntityManager().initEntityClass(entityClass);
        if (entityType == null || entityType.isFailed()) {
            throw new IllegalArgumentException(entityClass + " is not an entity");
        }
        return from(entityType);
    }

    public Root from(EntityType entityType) {
        if (entityType == null || entityType.isFailed()) {
            throw new IllegalArgumentException("null or bad entity");
        }
        root = new RootImpl(criteriaBuilder, entityType, null);
        return root;
    }

    public Root getRoot() {
        return root;
    }

    // Restriction ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

    protected void setRestriction(Expression restriction) {
        this.restriction = criteriaBuilder.wrap(restriction);
    }

    public void setRestriction(Predicate... restrictions) {
        this.restriction = criteriaBuilder.and(restrictions);
    }

    public Predicate getRestriction() {
        return restriction;
    }

    public  Subquery subquery(Class type) {
        return new CriteriaSubqueryImpl(criteriaBuilder(), type, this);
    }

    // compiling ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

    protected void validate() {
        if (root == null) {
            throw new IllegalStateException("UPDATE/DELETE criteria must name root entity");
        }
    }

    protected abstract void renderQuery(RenderingContext rc);

    protected void renderRoot(RenderingContext rc) {
        ((FromImplementor) root).renderFrom(rc);
    }

    protected void renderRestrictions(RenderingContext rc) {
        if (getRestriction() != null) {
            rc.append(" where ");
            ((Renderable) getRestriction()).render(rc);
        }
    }

    RenderingContext rc;
    @Override
    public synchronized void prepare() {
        if (rc == null) {
            validate();
            rc = new RenderingContext();
            rc.setQuery(false);
            renderQuery(rc);
        }
    }

    @Override
    public boolean isQuery() {
        return false;
    }

    @Override
    public String getSql() {
        prepare();
        return rc.getSql();
    }

    @Override
    public ParameterInfo[] getParameterInfos() {
        prepare();
        return rc.getParameterInfos();
    }

    public Set> getParameters() {
        prepare();
        return rc.getParameters();
    }

    public Parameter getParameter(String name) {
        for (Parameter p : getParameters()) {
            if (name.equals(p.getName())) {
                return p;
            }
        }
        throw new IllegalArgumentException(name);
    }

    public  Parameter getParameter(String name, Class type) {
        for (Parameter p : getParameters()) {
            if (name.equals(p.getName()) && type.isAssignableFrom(p.getParameterType())) {
                return (Parameter) p;
            }
        }
        throw new IllegalArgumentException(name);
    }

    public boolean isBound(Parameter param) {
        Object r = ((ParameterInfo) param).getParameterValue();
        return r != ParameterInfo.None;
    }

    public  R getParameterValue(Parameter param) {
        R r = ((ParameterInfo) param).getParameterValue();
        if (r == ParameterInfo.None) {
            return null;
        }
        return r;
    }

    public  R getParameterValue(String name) {
        return getParameterValue((Parameter) getParameter(name));
    }
    
    ParameterMap parameterMap;
    @Override
    public ParameterMap makeParameterMap(SqlMapExecutorDelegate delegate) {
        if (parameterMap == null) {
            parameterMap = new ParameterMap(delegate);
            List maps = new ArrayList();
            for (ParameterInfo pi : getParameterInfos()) {
                ParameterMapping map = new ParameterMapping();
                map.setMode("IN");
                map.setTypeHandler(delegate.getTypeHandlerFactory().getTypeHandler(pi.getParameterType()));
                map.setJavaType(pi.getParameterType());
                maps.add(map);
            }
            parameterMap.setParameterMappingList(maps);
        }
        return parameterMap;
    }

    @Override
    public void flushCache(SqlMapExecutorDelegate delegate) {
        if (root != null && root.getEntityType().isCacheable()) {
            delegate.getEntityManager().flushEntityCache(root.getEntityType().getJavaType());
        }
    }

    @Override
    public EntityType getQueryCacheType() {
        return null;
    }
}