⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 persistentcriteria.java

📁 根据Scott W. Ambler在1998年写的关于ORM Persistence Layer的详细设计论文的设计思路,Artem Rudoy 开发了一个开源的ORM实现 -- PL(Persist
💻 JAVA
字号:
package pl.criteria;

import java.util.*;



/**
 * 查询条件类
 */
public abstract class PersistentCriteria
{
    private pl.map.ClassMap classMap = null;
    private HashSet tables = new HashSet();
//    private HashSet associations = new HashSet();
    private CriteriaCondition whereCondition = null;

    /**
     * Creates PersistentCriteria.
     *
     * @param cl Class target class for this criteria
     */
    public PersistentCriteria(Class cl)
    {
        this(cl.getName());
    }

    /**
     * Creates PersistentCriteria.
     *
     * @param className String name of the target criteria
     */
    public PersistentCriteria(String className)
    {
        this(pl.PersistenceBroker.getInstance().getClassMap(className));
    }

    /**
     * Creates copy of persistent criteria.
     */
    public PersistentCriteria(PersistentCriteria c)
    {
        super();

//        this.associations = c.associations;
        this.classMap = c.classMap;
        this.tables = c.tables;
        this.whereCondition = c.whereCondition;
    }

    /**
     * Creates PersistentCriteria.
     *
     * @param classMap class map for this criteria
     */
    public PersistentCriteria(pl.map.ClassMap classMap)
    {
        this.classMap = classMap;

        // Fill tables HashSet with values
        pl.map.ClassMap cm = classMap;
        do
        {
            tables.addAll(cm.getTables());
            cm = cm.getSuperClass();
        }
        while(cm != null);

        // Create condition for the WHERE part of this criteria
        whereCondition = getNewCondition();
    }

    /**
     * 补充完整Where条件子句
     *
     * @param statement pl.sql.SqlStatement
     */
    protected void fillStatementWithWhere(pl.sql.SqlStatement statement, Iterator parameters) throws pl.PlException
    {
        // Build 'WHERE' part of SqlStatement for this criteria

        if(whereCondition.getSize() > 0 )
        {
            // Add 'WHERE key=?' to the statement
            statement.addSqlClause(" " + classMap.getRelationalDatabase().getClauseStringWhere() + " ");

            // Add criteria
            whereCondition.fillSqlStatement(statement, parameters);
        }
    }

    /**
     * Returns AttributeMap by the given name.
     *
     * @return pl.map.AttributeMap
     * @param attributeName java.lang.String
     */
    private pl.map.AttributeMap getAttributeMap(String attributeName) throws pl.PlException
    {
        pl.map.AttributeMap map = null;

        // Split name into tokens using "." delimiter and put tokens
        // into nameSequence Vector
        StringTokenizer st = new StringTokenizer(attributeName, ".", false);
        Vector nameSequence = new Vector();
        while(st.hasMoreTokens())
        {
            nameSequence.add(st.nextToken());
        }

        // Find attribute map for the given name
        pl.map.ClassMap cm = classMap;


        if(cm != null)
        {
            map = cm.getAttributeMap((String)nameSequence.get(nameSequence.size() - 1), true);
        }

        if(map == null)
            throw new pl.PlException("Bad attribute name '" + attributeName + "' in criteria");
        else
            return map;
    }

    /**
     * Returns class map for this criteria.
     *
     * @return pl.map.ClassMap
     */
    public pl.map.ClassMap getClassMap()
    {
        return classMap;
    }

    /**
     * Returns new empty condition for this criteria.
     *
     * @return pl.criteria.CriteriaCondition
     */
    public CriteriaCondition getNewCondition()
    {
        return new CriteriaCondition(classMap);
    }

    /**
     * Returns tables involved in this criteria.
     *
     * @return java.util.HashSet
     */
    protected java.util.HashSet getTables()
    {
        return tables;
    }

    /**
     * Returns condition for the WHERE part of this criteria.
     *
     * @return pl.criteria.CriteriaCondition
     */
    public CriteriaCondition getWhereCondition()
    {
        return whereCondition;
    }

    public EqualToCriteria getEqualToCriteria(String attributeName) throws pl.PlException
    {
        return new EqualToCriteria(classMap, getAttributeMap(attributeName));
    }

}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -