db-ojb-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From b..@apache.org
Subject cvs commit: db-ojb/src/java/org/apache/ojb/broker/accesslayer/sql SqlQueryStatement.java SqlSelectStatement.java
Date Mon, 10 Mar 2003 20:38:10 GMT
brj         2003/03/10 12:38:10

  Modified:    src/java/org/apache/ojb/broker/accesslayer
                        StatementManager.java JdbcAccessImpl.java
                        StatementManagerIF.java
               src/java/org/apache/ojb/broker/query QueryBySQL.java
                        QueryByCriteria.java Query.java
                        QueryByIdentity.java
               src/java/org/apache/ojb/broker/accesslayer/sql
                        SqlQueryStatement.java SqlSelectStatement.java
  Log:
  first try to support HAVING
  
  Revision  Changes    Path
  1.25      +15 -2     db-ojb/src/java/org/apache/ojb/broker/accesslayer/StatementManager.java
  
  Index: StatementManager.java
  ===================================================================
  RCS file: /home/cvs/db-ojb/src/java/org/apache/ojb/broker/accesslayer/StatementManager.java,v
  retrieving revision 1.24
  retrieving revision 1.25
  diff -u -r1.24 -r1.25
  --- StatementManager.java	9 Mar 2003 15:38:46 -0000	1.24
  +++ StatementManager.java	10 Mar 2003 20:38:09 -0000	1.25
  @@ -413,10 +413,23 @@
   		}
   	}
   
  +    /**
  +     * bind a Query based Select Statement
  +     */
  +    public int bindStatement(PreparedStatement stmt, Query query, ClassDescriptor cld,
int param) throws SQLException
  +    {
  +        int result;
  +        
  +        result = bindStatement(stmt, query.getCriteria(), cld,  param);
  +        result = bindStatement(stmt, query.getHavingCriteria(), cld,  result);
  +        
  +        return result;
  +    }       
  +
   	/**
   	 * bind a Query based Select Statement
   	 */
  -	public int bindStatement(PreparedStatement stmt, Criteria crit, ClassDescriptor cld, int
param) throws SQLException
  +	protected int bindStatement(PreparedStatement stmt, Criteria crit, ClassDescriptor cld,
int param) throws SQLException
   	{
   		if (crit != null)
   		{
  
  
  
  1.7       +3 -3      db-ojb/src/java/org/apache/ojb/broker/accesslayer/JdbcAccessImpl.java
  
  Index: JdbcAccessImpl.java
  ===================================================================
  RCS file: /home/cvs/db-ojb/src/java/org/apache/ojb/broker/accesslayer/JdbcAccessImpl.java,v
  retrieving revision 1.6
  retrieving revision 1.7
  diff -u -r1.6 -r1.7
  --- JdbcAccessImpl.java	5 Feb 2003 04:38:21 -0000	1.6
  +++ JdbcAccessImpl.java	10 Mar 2003 20:38:09 -0000	1.7
  @@ -163,7 +163,7 @@
   		{
   			String sql = this.broker.serviceSqlGenerator().getPreparedDeleteStatement(query, cld);
   			stmt = broker.serviceStatementManager().getPreparedStatement(cld, sql, false);
  -			broker.serviceStatementManager().bindStatement(stmt, query.getCriteria(), cld, 1);
  +			broker.serviceStatementManager().bindStatement(stmt, query, cld, 1);
   			stmt.executeUpdate();
   		}
   		catch (SQLException e)
  @@ -255,7 +255,7 @@
               {
                   String sql = this.broker.serviceSqlGenerator().getPreparedSelectStatement(query,
cld);
                   PreparedStatement stmt = broker.serviceStatementManager().getPreparedStatement(cld,
sql, scrollable);
  -                broker.serviceStatementManager().bindStatement(stmt, query.getCriteria(),
cld, 1);
  +                broker.serviceStatementManager().bindStatement(stmt, query, cld, 1);
                   ResultSet rs = stmt.executeQuery();
                   // as we return the resultset for further operations, we cannot release
the statement yet.
                   // that has to be done by the JdbcAccess-clients (i.e. RsIterator, ProxyRsIterator
and PkEnumeration.)
  
  
  
  1.7       +11 -9     db-ojb/src/java/org/apache/ojb/broker/accesslayer/StatementManagerIF.java
  
  Index: StatementManagerIF.java
  ===================================================================
  RCS file: /home/cvs/db-ojb/src/java/org/apache/ojb/broker/accesslayer/StatementManagerIF.java,v
  retrieving revision 1.6
  retrieving revision 1.7
  diff -u -r1.6 -r1.7
  --- StatementManagerIF.java	19 Jan 2003 17:20:16 -0000	1.6
  +++ StatementManagerIF.java	10 Mar 2003 20:38:09 -0000	1.7
  @@ -54,16 +54,16 @@
    * <http://www.apache.org/>.
    */
   
  -import org.apache.ojb.broker.Identity;
  -import org.apache.ojb.broker.PersistenceBrokerSQLException;
  -import org.apache.ojb.broker.PersistenceBrokerException;
  -import org.apache.ojb.broker.query.Criteria;
  -import org.apache.ojb.broker.metadata.ClassDescriptor;
  -
   import java.sql.PreparedStatement;
  +import java.sql.ResultSet;
   import java.sql.SQLException;
   import java.sql.Statement;
  -import java.sql.ResultSet;
  +
  +import org.apache.ojb.broker.Identity;
  +import org.apache.ojb.broker.PersistenceBrokerException;
  +import org.apache.ojb.broker.PersistenceBrokerSQLException;
  +import org.apache.ojb.broker.metadata.ClassDescriptor;
  +import org.apache.ojb.broker.query.Query;
   
   /**
    * @version $Id$
  @@ -82,11 +82,13 @@
        */
       void bindDelete(PreparedStatement stmt, ClassDescriptor cld, Object obj)
           throws java.sql.SQLException;
  +
       /**
        * bind a Query based Select Statement
        */
  -    int bindStatement(PreparedStatement stmt, Criteria crit, ClassDescriptor cld, int param)
  +    int bindStatement(PreparedStatement stmt, Query query, ClassDescriptor cld, int param)
           throws SQLException;
  +
       /**
        * binds the values of the object obj to the statements parameters
        */
  
  
  
  1.3       +83 -74    db-ojb/src/java/org/apache/ojb/broker/query/QueryBySQL.java
  
  Index: QueryBySQL.java
  ===================================================================
  RCS file: /home/cvs/db-ojb/src/java/org/apache/ojb/broker/query/QueryBySQL.java,v
  retrieving revision 1.2
  retrieving revision 1.3
  diff -u -r1.2 -r1.3
  --- QueryBySQL.java	17 Jun 2002 19:34:12 -0000	1.2
  +++ QueryBySQL.java	10 Mar 2003 20:38:09 -0000	1.3
  @@ -1,4 +1,4 @@
  -package org.apache.ojb.broker.query;
  +package org.apache.ojb.broker.query;
   
   /* ====================================================================
    * The Apache Software License, Version 1.1
  @@ -53,76 +53,85 @@
    * information on the Apache Software Foundation, please see
    * <http://www.apache.org/>.
    */
  -
  -/**
  - * @version 	1.0
  - * @author 	Jakob Braeuchi
  - */
  -public class QueryBySQL extends AbstractQueryImpl implements Query
  -{
  -	private String sql;
  -	private Class clazz;
  -
  -	/**
  -	 * Gets the sql.
  -	 * @return Returns a String
  -	 */
  -
  -	public QueryBySQL(Class targetClass, String anSQLStatement)
  -	{
  -		this.clazz = targetClass;
  -		this.sql = anSQLStatement;
  -	}
  -
  -	public String getSql()
  -	{
  -		return sql;
  -	}
  -
  -	/**
  -	 * Sets the sql.
  -	 * @param sql The sql to set
  -	 */
  -	public void setSql(String sql)
  -	{
  -		this.sql = sql;
  -	}
  -
  -	/*
  -	 * @see Query#getCriteria()
  -	 */
  -	public Criteria getCriteria()
  -	{
  -		return null;
  -	}
  -
  -	/*
  -	 * @see Query#getExampleObject()
  -	 */
  -	public Object getExampleObject()
  -	{
  -		return null;
  -	}
  -
  -	/*
  -	 * @see Query#getSearchClass()
  -	 */
  -	public Class getSearchClass()
  -	{
  -		return clazz;
  -	}
  -
  -	/*
  -	 * @see Query#isDistinct()
  -	 */
  -	public boolean isDistinct()
  -	{
  -		return false;
  -	}
  -
  -    public String toString()
  -    {
  -        return "Query from " + clazz + " sql= " + sql;
  -    }
  -
  -}
  +
  +/**
  + * @version 	1.0
  + * @author 	Jakob Braeuchi
  + */
  +public class QueryBySQL extends AbstractQueryImpl implements Query
  +{
  +	private String sql;
  +	private Class clazz;
  +
  +	/**
  +	 * Gets the sql.
  +	 * @return Returns a String
  +	 */
  +
  +	public QueryBySQL(Class targetClass, String anSQLStatement)
  +	{
  +		this.clazz = targetClass;
  +		this.sql = anSQLStatement;
  +	}
  +
  +	public String getSql()
  +	{
  +		return sql;
  +	}
  +
  +	/**
  +	 * Sets the sql.
  +	 * @param sql The sql to set
  +	 */
  +	public void setSql(String sql)
  +	{
  +		this.sql = sql;
  +	}
  +
  +	/*
  +	 * @see Query#getCriteria()
  +	 */
  +	public Criteria getCriteria()
  +	{
  +		return null;
  +	}
  +
  +    /* (non-Javadoc)
  +     * @see org.apache.ojb.broker.query.Query#getHavingCriteria()
  +     */
  +    public Criteria getHavingCriteria()
  +    {
  +        return null;
  +    }
  +
  +	/*
  +	 * @see Query#getExampleObject()
  +	 */
  +	public Object getExampleObject()
  +	{
  +		return null;
  +	}
  +
  +	/*
  +	 * @see Query#getSearchClass()
  +	 */
  +	public Class getSearchClass()
  +	{
  +		return clazz;
  +	}
  +
  +	/*
  +	 * @see Query#isDistinct()
  +	 */
  +	public boolean isDistinct()
  +	{
  +		return false;
  +	}
  +
  +    public String toString()
  +    {
  +        return "Query from " + clazz + " sql= " + sql;
  +    }
  +
  +
  +}
  
  
  
  1.7       +61 -14    db-ojb/src/java/org/apache/ojb/broker/query/QueryByCriteria.java
  
  Index: QueryByCriteria.java
  ===================================================================
  RCS file: /home/cvs/db-ojb/src/java/org/apache/ojb/broker/query/QueryByCriteria.java,v
  retrieving revision 1.6
  retrieving revision 1.7
  diff -u -r1.6 -r1.7
  --- QueryByCriteria.java	24 Dec 2002 13:17:10 -0000	1.6
  +++ QueryByCriteria.java	10 Mar 2003 20:38:09 -0000	1.7
  @@ -82,10 +82,34 @@
    */
   public class QueryByCriteria extends AbstractQueryImpl implements Query
   {
  -    private Class clazz;
  -    private Criteria criteria;
  -    private boolean distinct = false;
  -    private HashMap pathClasses;
  +    private Class m_clazz;
  +    private Criteria m_criteria;
  +    private boolean m_distinct = false;
  +    private HashMap m_pathClasses;
  +    private Criteria m_havingCriteria;
  +
  +    /**
  +     * Build a Query for class targetClass with criteria.
  +     * Criteriy may be null (will result in a query returning ALL objects from a table)
  +     */
  +    public QueryByCriteria(Class targetClass, Criteria whereCriteria, Criteria havingCriteria,
boolean distinct)
  +    {
  +        this.m_clazz = targetClass;
  +        this.m_criteria = whereCriteria;
  +        this.m_havingCriteria = havingCriteria;
  +        this.m_distinct = distinct;
  +        this.m_pathClasses = new HashMap();
  +    }
  +
  +    /**
  +     * Build a Query for class targetClass with criteria.
  +     * Criteriy may be null (will result in a query returning ALL objects from a table)
  +     */
  +    public QueryByCriteria(Class targetClass, Criteria whereCriteria, Criteria havingCriteria)
  +    {
  +        this(targetClass, whereCriteria, havingCriteria, false);
  +    }
  +
   
       /**
        * Build a Query for class targetClass with criteria.
  @@ -102,10 +126,7 @@
        */
       public QueryByCriteria(Class targetClass, Criteria criteria, boolean distinct)
       {
  -        this.clazz = targetClass;
  -        this.criteria = criteria;
  -        this.distinct = distinct;
  -        this.pathClasses = new HashMap();
  +        this(targetClass, criteria, null, distinct);
       }
   
       /**
  @@ -202,9 +223,17 @@
        */
       public Criteria getCriteria()
       {
  -        return criteria;
  +        return m_criteria;
       }
   
  +    /* (non-Javadoc)
  +     * @see org.apache.ojb.broker.query.Query#getHavingCriteria()
  +     */
  +    public Criteria getHavingCriteria()
  +    {
  +        return m_havingCriteria;
  +    }
  +    
       /**
        * Method declaration
        *
  @@ -228,7 +257,7 @@
        */
       public Class getSearchClass()
       {
  -        return clazz;
  +        return m_clazz;
       }
   
       /**
  @@ -238,7 +267,7 @@
        */
       public String toString()
       {
  -        return "Query from " + clazz + " where " + criteria;
  +        return "Query from " + m_clazz + " where " + m_criteria;
       }
   
       /**
  @@ -247,7 +276,7 @@
        */
       public boolean isDistinct()
       {
  -        return distinct;
  +        return m_distinct;
       }
   
       /**
  @@ -256,7 +285,7 @@
        */
       public void setDistinct(boolean distinct)
       {
  -        this.distinct = distinct;
  +        this.m_distinct = distinct;
       }
   
       /**
  @@ -265,7 +294,25 @@
        */
       protected HashMap getPathClasses()
       {
  -        return pathClasses;
  +        return m_pathClasses;
       }
  +
  +	/**
  +	 * Sets the criteria.
  +	 * @param criteria The criteria to set
  +	 */
  +	public void setCriteria(Criteria criteria)
  +	{
  +		m_criteria = criteria;
  +	}
  +
  +	/**
  +	 * Sets the havingCriteria.
  +	 * @param havingCriteria The havingCriteria to set
  +	 */
  +	public void setHavingCriteria(Criteria havingCriteria)
  +	{
  +		m_havingCriteria = havingCriteria;
  +	}
   
   }
  
  
  
  1.6       +5 -0      db-ojb/src/java/org/apache/ojb/broker/query/Query.java
  
  Index: Query.java
  ===================================================================
  RCS file: /home/cvs/db-ojb/src/java/org/apache/ojb/broker/query/Query.java,v
  retrieving revision 1.5
  retrieving revision 1.6
  diff -u -r1.5 -r1.6
  --- Query.java	5 Jan 2003 14:19:55 -0000	1.5
  +++ Query.java	10 Mar 2003 20:38:09 -0000	1.6
  @@ -91,6 +91,11 @@
       public abstract Criteria getCriteria();
   
       /**
  +     * return the criteria of the query if present or null.
  +     */
  +    public abstract Criteria getHavingCriteria();
  +
  +    /**
        * return the template Object if present or null
        */
       public abstract Object getExampleObject();
  
  
  
  1.4       +10 -1     db-ojb/src/java/org/apache/ojb/broker/query/QueryByIdentity.java
  
  Index: QueryByIdentity.java
  ===================================================================
  RCS file: /home/cvs/db-ojb/src/java/org/apache/ojb/broker/query/QueryByIdentity.java,v
  retrieving revision 1.3
  retrieving revision 1.4
  diff -u -r1.3 -r1.4
  --- QueryByIdentity.java	5 Oct 2002 15:50:58 -0000	1.3
  +++ QueryByIdentity.java	10 Mar 2003 20:38:09 -0000	1.4
  @@ -99,6 +99,14 @@
   		return null;
   	}
   
  +    /* (non-Javadoc)
  +     * @see org.apache.ojb.broker.query.Query#getHavingCriteria()
  +     */
  +    public Criteria getHavingCriteria()
  +    {
  +        return null;
  +    }
  +
   	/**
   	 * Answer the example Object
   	 * @return the example Object or an Identity
  @@ -135,5 +143,6 @@
   	{
   		return false;
   	}
  +
   
   }
  
  
  
  1.26      +69 -57    db-ojb/src/java/org/apache/ojb/broker/accesslayer/sql/SqlQueryStatement.java
  
  Index: SqlQueryStatement.java
  ===================================================================
  RCS file: /home/cvs/db-ojb/src/java/org/apache/ojb/broker/accesslayer/sql/SqlQueryStatement.java,v
  retrieving revision 1.25
  retrieving revision 1.26
  diff -u -r1.25 -r1.26
  --- SqlQueryStatement.java	9 Mar 2003 08:59:39 -0000	1.25
  +++ SqlQueryStatement.java	10 Mar 2003 20:38:10 -0000	1.26
  @@ -309,36 +309,61 @@
   
           if (where != null || crit != null)
           {
  -            /**
  -             * MBAIRD
  -             * when generating the "WHERE" clause we need to append the criteria for multi-mapped
  -             * tables. We only need to do this for the root classdescriptor and not for
joined tables
  -             * because we assume you cannot make a relation of the wrong type upon insertion.
Of course,
  -             * you COULD mess the data up manually and this would cause a problem.
  -             */
               stmt.append(" WHERE ");
  +            appendClause(where, crit, stmt);
  +        }
  +    }
   
  -            if (where != null)
  -            {
  -                stmt.append(where.toString());
  -            }
  -            if (crit != null)
  -            {
  -                if (where == null)
  -                {
  -                    stmt.append(asSQLStatement(crit));
  -                }
  -                else
  -                {
  -                    stmt.append(" AND (");
  -                    stmt.append(asSQLStatement(crit));
  -                    stmt.append(")");
  -                }
  -            }
  +    /**
  +     * appends a HAVING-clause to the Statement
  +     */
  +    protected void appendHavingClause(StringBuffer having, Criteria crit, StringBuffer
stmt)
  +    {
  +        if (having.length() == 0)
  +        {
  +            having = null;
  +        }
  +
  +        if (having != null || crit != null)
  +        {
  +            stmt.append(" HAVING ");
  +            appendClause(having, crit, stmt);
           }
       }
   
       /**
  +     * appends a WHERE/HAVING-clause to the Statement
  +     */
  +	protected void appendClause(StringBuffer clause, Criteria crit, StringBuffer stmt)
  +	{
  +		/**
  +		 * MBAIRD
  +		 * when generating the "WHERE/HAVING" clause we need to append the criteria for multi-mapped
  +		 * tables. We only need to do this for the root classdescriptor and not for joined tables
  +		 * because we assume you cannot make a relation of the wrong type upon insertion. Of
course,
  +		 * you COULD mess the data up manually and this would cause a problem.
  +		 */
  +
  +		if (clause != null)
  +		{
  +			stmt.append(clause.toString());
  +		}
  +		if (crit != null)
  +		{
  +			if (clause == null)
  +			{
  +				stmt.append(asSQLStatement(crit));
  +			}
  +			else
  +			{
  +				stmt.append(" AND (");
  +				stmt.append(asSQLStatement(crit));
  +				stmt.append(")");
  +			}
  +		}
  +	}
  +
  +    /**
        * @param criteria Selection criteria
        */
       private String asSQLStatement(Criteria crit)
  @@ -1153,41 +1178,28 @@
        */
       protected void splitCriteria()
       {
  -        Criteria crit = getQuery().getCriteria();
  -        /*
  -		Criteria crit1;
  -		Criteria crit2;
  -		List dnf;
  -		*/
  +		Criteria whereCrit = getQuery().getCriteria();
  +		Criteria havingCrit = getQuery().getHavingCriteria();
   
  -        if (crit == null || crit.isEmpty())
  -        {
  -            getJoinTreeToCriteria().put(getRoot(), null);
  -            return;
  -        }
  -        // TODO: parameters list shold be modified when the form is reduced to DNF.
  -        buildJoinTree(crit);
  -        m_joinTreeToCriteria.put(getRoot(), crit);
  -        return;
  -
  -        /*
  -		dnf = CriteriaUtils.getDNF(crit);
  -		for (Iterator it = dnf.iterator(); it.hasNext(); )
  +		if (whereCrit == null || whereCrit.isEmpty())
  +		{
  +			getJoinTreeToCriteria().put(getRoot(), null);
  +		}
  +		else
   		{
  -		    crit1 = (Criteria) it.next();
  -		    clearJoinTree();
  -		    buildJoinTree(crit1);
  -		    crit2 = (Criteria) joinTreeToCriteria.get(root);
  -		    if (crit2 == null)
  -		    {
  -		        joinTreeToCriteria.put(root.clone(), crit1);
  -		    }
  -		    else
  -		    {
  -		        crit2.addOrCriteria(crit1);
  -		    }
  +			// TODO: parameters list shold be modified when the form is reduced to DNF.
  +            getJoinTreeToCriteria().put(getRoot(), whereCrit);
  +			buildJoinTree(whereCrit);
   		}
  -		*/
  +
  +        if (havingCrit == null || havingCrit.isEmpty())
  +        {
  +        }
  +        else
  +        {
  +            buildJoinTree(havingCrit);
  +        }
  +
       }
   
       /**
  
  
  
  1.10      +16 -6     db-ojb/src/java/org/apache/ojb/broker/accesslayer/sql/SqlSelectStatement.java
  
  Index: SqlSelectStatement.java
  ===================================================================
  RCS file: /home/cvs/db-ojb/src/java/org/apache/ojb/broker/accesslayer/sql/SqlSelectStatement.java,v
  retrieving revision 1.9
  retrieving revision 1.10
  diff -u -r1.9 -r1.10
  --- SqlSelectStatement.java	1 Mar 2003 20:19:56 -0000	1.9
  +++ SqlSelectStatement.java	10 Mar 2003 20:38:10 -0000	1.10
  @@ -162,21 +162,29 @@
           /** the column numbers used in ORDER BY clause  */
           int[] orderByColumnNumbers = null;
           String[] columns = getColumns();
  +        Iterator it = getJoinTreeToCriteria().entrySet().iterator();
   
  -        for (Iterator it = getJoinTreeToCriteria().entrySet().iterator(); it.hasNext();)
  +        while (it.hasNext())
           {
               Map.Entry entry = (Map.Entry) it.next();
  -            Criteria crit = (Criteria) entry.getValue();
  +            Criteria whereCrit = (Criteria) entry.getValue();
  +            Criteria havingCrit = query.getHavingCriteria();
               List columnList;
               StringBuffer where = new StringBuffer();
  +            StringBuffer having = new StringBuffer();
               List groupByFields = null;
   
               // Set correct tree of joins for the current criteria
               setRoot((TableAlias) entry.getKey());
   
  -            if (crit != null && crit.isEmpty())
  +            if (whereCrit != null && whereCrit.isEmpty())
               {
  -                crit = null;
  +                whereCrit = null;
  +            }
  +
  +            if (havingCrit != null && havingCrit.isEmpty())
  +            {
  +                havingCrit = null;
               }
   
               if (first)
  @@ -245,9 +253,11 @@
               if(cld.getSuperClass() != null){
                   appendSuperClassJoin(cld, cldSuper, stmt, where);
               }
  -            appendWhereClause(where,crit, stmt);
  +            appendWhereClause(where,whereCrit, stmt);
               appendGroupByClause(groupByFields, stmt);
  +            appendHavingClause(having,havingCrit, stmt);
           }
  +        
           appendOrderByClause(orderByFields, orderByColumnNumbers, stmt);
   
           return stmt.toString();
  
  
  

Mime
View raw message