db-ojb-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From arm...@apache.org
Subject cvs commit: jakarta-ojb/src/java/org/apache/ojb/broker/accesslayer/sql SqlCacheKey.java SqlDeleteByQuery.java SqlGeneratorDefaultImpl.java SqlGeneratorFactory.java SqlQueryStatement.java SqlSelectStatement.java
Date Tue, 24 Dec 2002 12:57:51 GMT
arminw      2002/12/24 04:57:51

  Modified:    src/java/org/apache/ojb/broker/accesslayer/sql
                        SqlCacheKey.java SqlDeleteByQuery.java
                        SqlGeneratorDefaultImpl.java
                        SqlGeneratorFactory.java SqlQueryStatement.java
                        SqlSelectStatement.java
  Log:
  improve metadata handling:
  separate the object stuff (class descriptor, field descriptor, ...)
  from the connection stuff (jdbc descriptor).
  DescriptorRepository now handles only object
  specific properties.
  
  Do some performance improvements
  
  Revision  Changes    Path
  1.5       +47 -39    jakarta-ojb/src/java/org/apache/ojb/broker/accesslayer/sql/SqlCacheKey.java
  
  Index: SqlCacheKey.java
  ===================================================================
  RCS file: /home/cvs/jakarta-ojb/src/java/org/apache/ojb/broker/accesslayer/sql/SqlCacheKey.java,v
  retrieving revision 1.4
  retrieving revision 1.5
  diff -u -r1.4 -r1.5
  --- SqlCacheKey.java	21 Nov 2002 09:52:19 -0000	1.4
  +++ SqlCacheKey.java	24 Dec 2002 12:57:51 -0000	1.5
  @@ -59,51 +59,29 @@
   
   /**
    * Helper key class to store generated sql statements
  + *
    * @author Matthew Baird
    * @version $Id$
    */
  -
  -/*
  - * Created by IntelliJ IDEA.
  - * User: Matthew Baird
  - * Date: Oct 29, 2002
  - * Time: 4:19:14 PM
  - * To change this template use Options | File Templates.
  - */
  -
   class SqlCacheKey
   {
  -	private Query m_query;
  -
  -	public Query getQuery()
  -	{
  -		return m_query;
  -	}
  -
  -	public void setQuery(Query query)
  -	{
  -		m_query = query;
  -	}
  -
  -	public ClassDescriptor getCld()
  -	{
  -		return m_cld;
  -	}
  +	public static final int TYPE_SELECT = 1;
  +    public static final int TYPE_INSERT = 2;
  +    public static final int TYPE_UPDATE = 3;
  +    public static final int TYPE_DELETE = 4;
  +
  +    private final Query m_query;
  +    private final ClassDescriptor m_cld;
  +    private final int type;
   
  -	public void setCld(ClassDescriptor cld)
  -	{
  -		m_cld = cld;
  -	}
  -
  -	private ClassDescriptor m_cld;
  -
  -	public SqlCacheKey(Query query, ClassDescriptor cld)
  +    public SqlCacheKey(final Query query, final ClassDescriptor cld, final int type)
   	{
   		m_query = query;
   		m_cld = cld;
  +        this.type = type;
   	}
   
  -	public int hashCode()
  +    public int hashCode()
   	{
   		int retval = 0;
   		if (getCld() != null)
  @@ -114,7 +92,7 @@
   		{
   			retval += getQuery().hashCode();
   		}
  -		return retval;
  +		return retval+type;
   	}
   
   	public boolean equals(Object other)
  @@ -126,18 +104,48 @@
   		if (other instanceof SqlCacheKey)
   		{
   			SqlCacheKey temp = (SqlCacheKey) other;
  -			if (temp.getCld().equals(getCld()) && (temp.getQuery().equals(getQuery())))
  +			if (temp.getCld().equals(getCld()) && (temp.getQuery().equals(getQuery())) && (temp.getType() == this.getType()))
   			{
   				return true;
   			}
   			else
               {
   				return false;
  -            }    
  +            }
   		}
   		else
           {
   			return false;
  -        }    
  +        }
   	}
  +
  +	public Query getQuery()
  +	{
  +		return m_query;
  +	}
  +
  +	public ClassDescriptor getCld()
  +	{
  +		return m_cld;
  +	}
  +
  +    public int getType()
  +    {
  +        return type;
  +    }
  +    /*
  +    arminw:
  +    When we declare cld and query final, we have to eliminate setters.
  +    */
  +
  +//	public void setCld(ClassDescriptor cld)
  +//	{
  +//		m_cld = cld;
  +//	}
  +//
  +//    public void setQuery(Query query)
  +//    {
  +//        m_query = query;
  +//    }
  +
   }
  
  
  
  1.5       +5 -4      jakarta-ojb/src/java/org/apache/ojb/broker/accesslayer/sql/SqlDeleteByQuery.java
  
  Index: SqlDeleteByQuery.java
  ===================================================================
  RCS file: /home/cvs/jakarta-ojb/src/java/org/apache/ojb/broker/accesslayer/sql/SqlDeleteByQuery.java,v
  retrieving revision 1.4
  retrieving revision 1.5
  diff -u -r1.4 -r1.5
  --- SqlDeleteByQuery.java	12 Oct 2002 16:24:58 -0000	1.4
  +++ SqlDeleteByQuery.java	24 Dec 2002 12:57:51 -0000	1.5
  @@ -55,6 +55,7 @@
    */
   
   import org.apache.ojb.broker.PersistenceBrokerException;
  +import org.apache.ojb.broker.platforms.Platform;
   import org.apache.ojb.broker.metadata.ClassDescriptor;
   import org.apache.ojb.broker.metadata.FieldDescriptor;
   import org.apache.ojb.broker.metadata.ObjectReferenceDescriptor;
  @@ -64,7 +65,7 @@
   
   /**
    * Model a DELETE by Query Statement
  - * 
  + *
    * @author Thomas Mahler
    * @version $Id$
    */
  @@ -75,9 +76,9 @@
   	 * Constructor for SqlDeleteByQuery.
   	 * @param cld
   	 */
  -	public SqlDeleteByQuery(ClassDescriptor cld, Query query, Logger logger)
  +	public SqlDeleteByQuery(Platform pf, ClassDescriptor cld, Query query, Logger logger)
   	{
  -		super(cld, null, query, logger);
  +		super(pf, cld, null, query, logger);
   	}
   
   	/**
  
  
  
  1.12      +388 -375  jakarta-ojb/src/java/org/apache/ojb/broker/accesslayer/sql/SqlGeneratorDefaultImpl.java
  
  Index: SqlGeneratorDefaultImpl.java
  ===================================================================
  RCS file: /home/cvs/jakarta-ojb/src/java/org/apache/ojb/broker/accesslayer/sql/SqlGeneratorDefaultImpl.java,v
  retrieving revision 1.11
  retrieving revision 1.12
  diff -u -r1.11 -r1.12
  --- SqlGeneratorDefaultImpl.java	8 Dec 2002 13:28:17 -0000	1.11
  +++ SqlGeneratorDefaultImpl.java	24 Dec 2002 12:57:51 -0000	1.12
  @@ -54,18 +54,31 @@
    * <http://www.apache.org/>.
    */
   
  +import org.apache.ojb.broker.metadata.ClassDescriptor;
  +import org.apache.ojb.broker.metadata.JdbcConnectionDescriptor;
  +import org.apache.ojb.broker.platforms.Platform;
  +import org.apache.ojb.broker.platforms.PlatformFactory;
  +import org.apache.ojb.broker.query.BetweenCriteria;
  +import org.apache.ojb.broker.query.ColumnCriteria;
  +import org.apache.ojb.broker.query.Criteria;
  +import org.apache.ojb.broker.query.ExistsCriteria;
  +import org.apache.ojb.broker.query.FieldCriteria;
  +import org.apache.ojb.broker.query.InCriteria;
  +import org.apache.ojb.broker.query.NullCriteria;
  +import org.apache.ojb.broker.query.Query;
  +import org.apache.ojb.broker.query.ReportQuery;
  +import org.apache.ojb.broker.query.SelectionCriteria;
  +import org.apache.ojb.broker.query.SqlCriteria;
  +import org.apache.ojb.broker.util.logging.Logger;
  +import org.apache.ojb.broker.util.logging.LoggerFactory;
  +
   import java.util.Collection;
   import java.util.Enumeration;
   import java.util.Map;
   import java.util.WeakHashMap;
   
  -import org.apache.ojb.broker.metadata.ClassDescriptor;
  -import org.apache.ojb.broker.query.*;
  -import org.apache.ojb.broker.util.logging.Logger;
  -import org.apache.ojb.broker.util.logging.LoggerFactory;
  -
   /**
  - * This Class is responsible for building sql statements 
  + * This Class is responsible for building sql statements
    * Objects fields and their repective values are accessed by Java reflection
    *
    * @author <a href="mailto:thma@apache.org">Thomas Mahler<a>
  @@ -73,80 +86,79 @@
    */
   public class SqlGeneratorDefaultImpl implements SqlGenerator
   {
  +    private Logger logger = LoggerFactory.getLogger(SqlGeneratorDefaultImpl.class);
  +
       private Map m_sqlCacheMap = new WeakHashMap();
  +    private Platform pf;
  +
  +    /**
  +     * Constructor is protected, use getInstance() to retrieve the singleton
  +     * instance of this class.
  +     */
  +    public SqlGeneratorDefaultImpl(Platform pf)
  +    {
  +        this.pf = pf;
  +    }
  +
  +    /**
  +     * generate a prepared DELETE-Statement for the Class
  +     * described by cld.
  +     * @param cld the ClassDescriptor
  +     */
  +    public String getPreparedDeleteStatement(ClassDescriptor cld)
  +    {
  +        SqlStatement sql;
  +        String result;
  +
  +        sql = new SqlDeleteByPkStatement(cld, logger);
  +        result = sql.getStatement();
   
  -	/**
  -	 * Constructor is protected, use getInstance() to retrieve the singleton
  -	 * instance of this class.
  -	 */
  -	public SqlGeneratorDefaultImpl()
  -	{
  -		logger = LoggerFactory.getLogger(this.getClass());
  -	}
  -
  -
  -	private Logger logger;
  -
  -
  -	/**
  -	 * generate a prepared DELETE-Statement for the Class
  -	 * described by cld.
  -	 * @param cld the ClassDescriptor
  -	 */
  -	public String getPreparedDeleteStatement(ClassDescriptor cld)
  -	{
  -		SqlStatement sql;
  -		String result;
  -
  -		sql = new SqlDeleteByPkStatement(cld, logger);
  -		result = sql.getStatement();
  -
  -		logger.debug("SQL: " + result);
  -		return result;
  -	}
  -
  -	/**
  -	 * generate a prepared INSERT-Statement for the Class
  -	 * described by cld.
  -	 * @param cld the ClassDescriptor
  -	 */
  -	public String getPreparedInsertStatement(ClassDescriptor cld)
  -	{
  -		SqlStatement sql;
  -		String result;
  -
  -		sql = new SqlInsertStatement(cld, logger);
  -		result = sql.getStatement();
  -
  -		logger.debug("SQL: " + result);
  -		return result;
  -	}
  -
  -	/**
  -	 * generate a prepared SELECT-Statement for the Class
  -	 * described by cld
  -	 * @param cld the ClassDescriptor
  -	 */
  -	public String getPreparedSelectByPkStatement(ClassDescriptor cld)
  -	{
  -		SqlStatement sql;
  -		String result;
  -
  -		sql = new SqlSelectByPkStatement(cld, logger);
  -		result = sql.getStatement();
  -
  -		logger.debug("SQL: " + result);
  -		return result;
  -	}
  -
  -	/**
  -	 * generate a select-Statement according to query
  -	 * @param query the Query
  -	 * @param cld the ClassDescriptor
  -	 */
  -	public String getPreparedSelectStatement(Query query, ClassDescriptor cld)
  -	{
  -        SqlCacheKey key = new SqlCacheKey(query,cld);
  +        logger.debug("SQL: " + result);
  +        return result;
  +    }
  +
  +    /**
  +     * generate a prepared INSERT-Statement for the Class
  +     * described by cld.
  +     * @param cld the ClassDescriptor
  +     */
  +    public String getPreparedInsertStatement(ClassDescriptor cld)
  +    {
  +        SqlStatement sql;
  +        String result;
  +
  +        sql = new SqlInsertStatement(cld, logger);
  +        result = sql.getStatement();
  +
  +        logger.debug("SQL: " + result);
  +        return result;
  +    }
  +
  +    /**
  +     * generate a prepared SELECT-Statement for the Class
  +     * described by cld
  +     * @param cld the ClassDescriptor
  +     */
  +    public String getPreparedSelectByPkStatement(ClassDescriptor cld)
  +    {
  +        SqlStatement sql;
  +        String result;
  +
  +        sql = new SqlSelectByPkStatement(cld, logger);
  +        result = sql.getStatement();
  +
  +        logger.debug("SQL: " + result);
  +        return result;
  +    }
  +
  +    /**
  +     * generate a select-Statement according to query
  +     * @param query the Query
  +     * @param cld the ClassDescriptor
  +     */
  +    public String getPreparedSelectStatement(Query query, ClassDescriptor cld)
  +    {
  +        SqlCacheKey key = new SqlCacheKey(query, cld, SqlCacheKey.TYPE_SELECT);
           String result = (String) m_sqlCacheMap.get(key);
           if (result == null)
           {
  @@ -156,45 +168,45 @@
               {
                   columns = ((ReportQuery) query).getColumns();
               }
  -            sql = new SqlSelectStatement(cld, columns, query, logger);
  +            sql = new SqlSelectStatement(pf, cld, columns, query, logger);
               result = sql.getStatement();
               logger.debug("SQL: " + result);
               m_sqlCacheMap.put(key, result);
           }
           return result;
  -	}
  +    }
  +
  +    /**
  +     * generate a prepared UPDATE-Statement for the Class
  +     * described by cld
  +     * @param cld the ClassDescriptor
  +     */
  +    public String getPreparedUpdateStatement(ClassDescriptor cld)
  +    {
  +        SqlStatement sql;
  +        String result;
  +
  +        sql = new SqlUpdateStatement(cld, logger);
  +        result = sql.getStatement();
  +
  +        logger.debug("SQL: " + result);
  +        return result;
  +    }
   
  -	/**
  -	 * generate a prepared UPDATE-Statement for the Class
  -	 * described by cld
  -	 * @param cld the ClassDescriptor
  -	 */
  -	public String getPreparedUpdateStatement(ClassDescriptor cld)
  -	{
  -		SqlStatement sql;
  -		String result;
  -
  -		sql = new SqlUpdateStatement(cld,logger);
  -		result = sql.getStatement();
  -
  -		logger.debug("SQL: " + result);
  -		return result;
  -	}
  -
  -	/**
  -	 * generate an INSERT-Statement for M:N indirection table
  -     * 
  +    /**
  +     * generate an INSERT-Statement for M:N indirection table
  +     *
        * @param table
        * @param pkColumns1
        * @param pkValues1
        * @param pkColumns2
        * @param pkValues2
  -	 */
  -	public String getInsertStatement(String table, Object[] pkColumns1, Object[] pkValues1, Object[] pkColumns2, Object[] pkValues2)
  -	{
  +     */
  +    public String getInsertStatement(String table, Object[] pkColumns1, Object[] pkValues1, Object[] pkColumns2, Object[] pkValues2)
  +    {
           SqlStatement sql;
           String result;
  - 
  +
           String[] cols = new String[pkColumns1.length + pkColumns2.length];
           Object[] values = new Object[pkColumns1.length + pkColumns2.length];
   
  @@ -204,16 +216,16 @@
           System.arraycopy(pkColumns2, 0, cols, pkColumns1.length, pkColumns2.length);
           System.arraycopy(pkValues2, 0, values, pkValues1.length, pkValues2.length);
   
  -		sql = new SqlInsertMNStatement(table, cols, values, logger);
  -		result = sql.getStatement();
  +        sql = new SqlInsertMNStatement(table, cols, values, logger);
  +        result = sql.getStatement();
   
  -		logger.debug("SQL: " + result);
  -		return result;
  -	}
  +        logger.debug("SQL: " + result);
  +        return result;
  +    }
   
       /**
        * generate a SELECT-Statement for M:N indirection table
  -     * 
  +     *
        * @param table the indirection table
        * @param selectColumns selected columns
        * @param columns for where
  @@ -235,19 +247,19 @@
           return result;
       }
   
  -	/**
  -	 * generate a DELETE-Statement for M:N indirection table
  -     * 
  -     * @param table 
  +    /**
  +     * generate a DELETE-Statement for M:N indirection table
  +     *
  +     * @param table
        * @param pkColumns1
        * @param pkValues1
        * @param pkColumns2
        * @param pkValues2
  -	 */
  -	public String getDeleteStatement(String table, Object[] pkColumns1, Object[] pkValues1, Object[] pkColumns2, Object[] pkValues2)
  -	{
  -		SqlStatement sql;
  -		String result;
  +     */
  +    public String getDeleteStatement(String table, Object[] pkColumns1, Object[] pkValues1, Object[] pkColumns2, Object[] pkValues2)
  +    {
  +        SqlStatement sql;
  +        String result;
           int paramCount;
   
           paramCount = pkColumns1.length;
  @@ -258,10 +270,10 @@
   
           String[] cols = new String[paramCount];
           Object[] values = new Object[paramCount];
  -        
  -		System.arraycopy(pkColumns1, 0, cols, 0, pkColumns1.length);
  +
  +        System.arraycopy(pkColumns1, 0, cols, 0, pkColumns1.length);
           System.arraycopy(pkValues1, 0, values, 0, pkValues1.length);
  -       
  +
           if (pkValues2 != null)
           {
               System.arraycopy(pkColumns2, 0, cols, pkColumns1.length, pkColumns2.length);
  @@ -269,20 +281,20 @@
           }
   
           sql = new SqlDeleteMNStatement(table, cols, values, logger);
  -		result = sql.getStatement();
  +        result = sql.getStatement();
  +
  +        logger.debug("SQL: " + result);
  +        return result;
  +    }
   
  -		logger.debug("SQL: " + result);
  -		return result;
  -	}
  -
  -	/**
  -	 * generate a select-Statement according to query
  -	 * @param query the Query
  -	 * @param cld the ClassDescriptor
  -	 */
  -	public String getSelectStatementDep(Query query, ClassDescriptor cld)
  -	{
  -        SqlCacheKey key = new SqlCacheKey(query,cld);
  +    /**
  +     * generate a select-Statement according to query
  +     * @param query the Query
  +     * @param cld the ClassDescriptor
  +     */
  +    public String getSelectStatementDep(Query query, ClassDescriptor cld)
  +    {
  +        SqlCacheKey key = new SqlCacheKey(query, cld, SqlCacheKey.TYPE_SELECT);
           String result = (String) m_sqlCacheMap.get(key);
           if (result == null)
           {
  @@ -292,260 +304,261 @@
               {
                   columns = ((ReportQuery) query).getColumns();
               }
  -            sql = new SqlSelectStatement(cld, columns, query, logger);
  +            sql = new SqlSelectStatement(pf, cld, columns, query, logger);
               result = sql.getStatement();
               logger.debug("SQL: " + result);
               m_sqlCacheMap.put(key, result);
           }
           return result;
   
  -	}
  +    }
  +
  +    /**
  +     * @param criteria Selection criteria
  +     *
  +     * 26/06/99 Change statement to a StringBuffer for efficiency
  +     */
  +    public String asSQLStatement(Criteria crit, ClassDescriptor cld)
  +    {
  +        Enumeration e = crit.getElements();
  +        StringBuffer statement = new StringBuffer();
  +        while (e.hasMoreElements())
  +        {
  +            Object o = e.nextElement();
  +            if (o instanceof Criteria)
  +            {
  +                String addAtStart;
  +                String addAtEnd;
  +                Criteria pc = (Criteria) o;
  +                // need to add parenthesises?
  +                if (pc.isEmbraced())
  +                {
  +                    addAtStart = " (";
  +                    addAtEnd = ") ";
  +                }
  +                else
  +                {
  +                    addAtStart = "";
  +                    addAtEnd = "";
  +                }
  +
  +                switch (pc.getType())
  +                {
  +                    case (Criteria.OR):
  +                        {
  +                            statement.append(" OR " + addAtStart);
  +                            statement.append(asSQLStatement(pc, cld));
  +                            statement.append(addAtEnd);
  +                            break;
  +                        }
  +                    case (Criteria.AND):
  +                        {
  +                            statement.insert(0, "( ");
  +                            statement.append(") ");
  +                            statement.append(" AND " + addAtStart);
  +                            statement.append(asSQLStatement(pc, cld));
  +                            statement.append(addAtEnd);
  +                            break;
  +                        }
  +                }
  +            }
  +            else
  +            {
  +                SelectionCriteria c = (SelectionCriteria) o;
  +                if (statement.length() == 0)
  +                {
  +                    statement.append(asSQLClause(c, cld));
  +                }
  +                else
  +                {
  +                    statement.insert(0, "(");
  +                    statement.append(") ");
  +                    statement.append(" AND ");
  +                    statement.append(asSQLClause(c, cld));
  +                }
  +            }
  +        } // while
  +        if (statement.length() == 0)
  +        {
  +            return null;
  +        }
  +        return statement.toString();
  +    }
   
  -	/**
  -	 * @param criteria Selection criteria
  -	 *
  -	 * 26/06/99 Change statement to a StringBuffer for efficiency
  -	 */
  -	public String asSQLStatement(Criteria crit, ClassDescriptor cld)
  -	{
  -		Enumeration e = crit.getElements();
  -		StringBuffer statement = new StringBuffer();
  -		while (e.hasMoreElements())
  -		{
  -			Object o = e.nextElement();
  -			if (o instanceof Criteria)
  -			{
  -				String addAtStart;
  -				String addAtEnd;
  -				Criteria pc = (Criteria) o;
  -				// need to add parenthesises?
  -				if (pc.isEmbraced())
  -				{
  -					addAtStart = " (";
  -					addAtEnd = ") ";
  -				}
  -				else
  -				{
  -					addAtStart = "";
  -					addAtEnd = "";
  -				}
  -
  -				switch (pc.getType())
  -				{
  -					case (Criteria.OR) :
  -						{
  -							statement.append(" OR " + addAtStart);
  -							statement.append(asSQLStatement(pc, cld));
  -							statement.append(addAtEnd);
  -							break;
  -						}
  -					case (Criteria.AND) :
  -						{
  -							statement.insert(0, "( ");
  -							statement.append(") ");
  -							statement.append(" AND " + addAtStart);
  -							statement.append(asSQLStatement(pc, cld));
  -							statement.append(addAtEnd);
  -							break;
  -						}
  -				}
  -			}
  -			else
  -			{
  -				SelectionCriteria c = (SelectionCriteria) o;
  -				if (statement.length() == 0)
  -				{
  -					statement.append(asSQLClause(c, cld));
  -				}
  -				else
  -				{
  -					statement.insert(0, "(");
  -					statement.append(") ");
  -					statement.append(" AND ");
  -					statement.append(asSQLClause(c, cld));
  -				}
  -			}
  -		} // while
  -		if (statement.length() == 0)
  -		{
  -			return null;
  -		}
  -		return statement.toString();
  -	}
  -	/**
  -	 * Answer the SQL-Clause for a SelectionCriteria
  -	 * @param c SelectionCriteria
  -	 * @param cld ClassDescriptor
  -	 */
  -	protected String asSQLClause(SelectionCriteria c, ClassDescriptor cld)
  -	{
  -		if (c instanceof FieldCriteria)
  -			return toSQLClause((FieldCriteria) c, cld);
  -
  -		if (c instanceof ColumnCriteria)
  -			return toSQLClause((ColumnCriteria) c, cld);
  -
  -		if (c instanceof NullCriteria)
  -			return toSQLClause((NullCriteria) c, cld);
  -
  -		if (c instanceof BetweenCriteria)
  -			return toSQLClause((BetweenCriteria) c, cld);
  -
  -		if (c instanceof InCriteria)
  -			return toSQLClause((InCriteria) c, cld);
  -
  -		if (c instanceof SqlCriteria)
  -			return toSQLClause((SqlCriteria) c, cld);
  -
  -		if (c instanceof ExistsCriteria)
  -			return toSQLClause((ExistsCriteria) c, cld);
  -
  -		return toSQLClause(c, cld);
  -	}
  -
  -	/**
  -	 * Answer the SQL-Clause for a NullCriteria
  -	 *
  -	 * @param c NullCriteria
  -	 * @param cld ClassDescriptor
  -	 */
  -	private String toSQLClause(NullCriteria c, ClassDescriptor cld)
  -	{
  -		String colName = c.getAttribute();
  -		return colName + c.getClause();
  -	}
  -
  -	/**
  -	 * Answer the SQL-Clause for a FieldCriteria
  -	 *
  -	 * @param c FieldCriteria
  -	 * @param cld ClassDescriptor
  -	 */
  -	private String toSQLClause(FieldCriteria c, ClassDescriptor cld)
  -	{
  -		String colName = c.getAttribute();
  -		return colName + c.getClause() + c.getValue();
  -	}
  -
  -	/**
  -	 * Answer the SQL-Clause for a ColumnCriteria
  -	 *
  -	 * @param c ColumnCriteria
  -	 * @param cld ClassDescriptor
  -	 */
  -	private String toSQLClause(ColumnCriteria c, ClassDescriptor cld)
  -	{
  -		String colName = c.getAttribute();
  -		return colName + c.getClause() + c.getValue();
  -	}
  -
  -	/**
  -	 * Answer the SQL-Clause for a BetweenCriteria
  -	 *
  -	 * @param c BetweenCriteria
  -	 * @param cld ClassDescriptor
  -	 */
  -	private String toSQLClause(BetweenCriteria c, ClassDescriptor cld)
  -	{
  -		String colName = c.getAttribute();
  -		return colName + c.getClause() + " ? AND ? ";
  -	}
  -
  -	/**
  -	 * Answer the SQL-Clause for an InCriteria
  -	 *
  -	 * @param c SelectionCriteria
  -	 * @param cld ClassDescriptor
  -	 */
  -	private String toSQLClause(InCriteria c, ClassDescriptor cld)
  -	{
  -		StringBuffer buf = new StringBuffer();
  -		Collection values = (Collection) c.getValue();
  -		int size = values.size();
  -
  -		buf.append(c.getAttribute());
  -		buf.append(c.getClause());
  -		buf.append("(");
  -		for (int i = 0; i < size - 1; i++)
  -		{
  -			buf.append("?,");
  -		}
  -		buf.append("?)");
  -		return buf.toString();
  -	}
  -
  -	/**
  -	 * Answer the SQL-Clause for a SelectionCriteria
  -	 *
  -	 * @param c SelectionCriteria
  -	 * @param cld ClassDescriptor
  -	 */
  -	private String toSQLClause(SelectionCriteria c, ClassDescriptor cld)
  -	{
  -		String colName = c.getAttribute();
  -		return colName + c.getClause() + " ? ";
  -	}
  -
  -	/**
  -	 * Answer the SQL-Clause for a SqlCriteria
  -	 *
  -	 * @param c SqlCriteria
  -	 * @param cld ClassDescriptor
  -	 */
  -	private String toSQLClause(SqlCriteria c, ClassDescriptor cld)
  -	{
  -		return c.getClause();
  -	}
  -
  -	/**
  -	 * Answer the SQL-Clause for an ExistsCriteria
  -	 *
  -	 * @param c ExistsCriteria
  -	 * @param cld ClassDescriptor
  -	 */
  -	private String toSQLClause(ExistsCriteria c, ClassDescriptor cld)
  -	{
  -		StringBuffer buf = new StringBuffer();
  -		Query subQuery = (Query) c.getValue();
  -
  -		buf.append(c.getClause());
  -		buf.append(" (");
  -
  -		// If it's a proper call
  -		if (cld != null)
  -		{
  -			buf.append(
  -				getPreparedSelectStatement(subQuery, cld.getRepository().getDescriptorFor(subQuery.getSearchClass())));
  -
  -			// Otherwise it's most likely a call to toString()
  -		}
  -		else
  -		{
  -			buf.append(subQuery);
  -		}
  -
  -		buf.append(")");
  -		return buf.toString();
  -	}
  -
  -	/**
  -	 * generate a prepared DELETE-Statement according to query
  -	 * @param query the Query
  -	 * @param cld the ClassDescriptor
  -	 */
  -	public String getPreparedDeleteStatement(Query query, ClassDescriptor cld)
  -	{
  -        SqlCacheKey key = new SqlCacheKey(query,cld);
  +    /**
  +     * Answer the SQL-Clause for a SelectionCriteria
  +     * @param c SelectionCriteria
  +     * @param cld ClassDescriptor
  +     */
  +    protected String asSQLClause(SelectionCriteria c, ClassDescriptor cld)
  +    {
  +        if (c instanceof FieldCriteria)
  +            return toSQLClause((FieldCriteria) c, cld);
  +
  +        if (c instanceof ColumnCriteria)
  +            return toSQLClause((ColumnCriteria) c, cld);
  +
  +        if (c instanceof NullCriteria)
  +            return toSQLClause((NullCriteria) c, cld);
  +
  +        if (c instanceof BetweenCriteria)
  +            return toSQLClause((BetweenCriteria) c, cld);
  +
  +        if (c instanceof InCriteria)
  +            return toSQLClause((InCriteria) c, cld);
  +
  +        if (c instanceof SqlCriteria)
  +            return toSQLClause((SqlCriteria) c, cld);
  +
  +        if (c instanceof ExistsCriteria)
  +            return toSQLClause((ExistsCriteria) c, cld);
  +
  +        return toSQLClause(c, cld);
  +    }
  +
  +    /**
  +     * Answer the SQL-Clause for a NullCriteria
  +     *
  +     * @param c NullCriteria
  +     * @param cld ClassDescriptor
  +     */
  +    private String toSQLClause(NullCriteria c, ClassDescriptor cld)
  +    {
  +        String colName = c.getAttribute();
  +        return colName + c.getClause();
  +    }
  +
  +    /**
  +     * Answer the SQL-Clause for a FieldCriteria
  +     *
  +     * @param c FieldCriteria
  +     * @param cld ClassDescriptor
  +     */
  +    private String toSQLClause(FieldCriteria c, ClassDescriptor cld)
  +    {
  +        String colName = c.getAttribute();
  +        return colName + c.getClause() + c.getValue();
  +    }
  +
  +    /**
  +     * Answer the SQL-Clause for a ColumnCriteria
  +     *
  +     * @param c ColumnCriteria
  +     * @param cld ClassDescriptor
  +     */
  +    private String toSQLClause(ColumnCriteria c, ClassDescriptor cld)
  +    {
  +        String colName = c.getAttribute();
  +        return colName + c.getClause() + c.getValue();
  +    }
  +
  +    /**
  +     * Answer the SQL-Clause for a BetweenCriteria
  +     *
  +     * @param c BetweenCriteria
  +     * @param cld ClassDescriptor
  +     */
  +    private String toSQLClause(BetweenCriteria c, ClassDescriptor cld)
  +    {
  +        String colName = c.getAttribute();
  +        return colName + c.getClause() + " ? AND ? ";
  +    }
  +
  +    /**
  +     * Answer the SQL-Clause for an InCriteria
  +     *
  +     * @param c SelectionCriteria
  +     * @param cld ClassDescriptor
  +     */
  +    private String toSQLClause(InCriteria c, ClassDescriptor cld)
  +    {
  +        StringBuffer buf = new StringBuffer();
  +        Collection values = (Collection) c.getValue();
  +        int size = values.size();
  +
  +        buf.append(c.getAttribute());
  +        buf.append(c.getClause());
  +        buf.append("(");
  +        for (int i = 0; i < size - 1; i++)
  +        {
  +            buf.append("?,");
  +        }
  +        buf.append("?)");
  +        return buf.toString();
  +    }
  +
  +    /**
  +     * Answer the SQL-Clause for a SelectionCriteria
  +     *
  +     * @param c SelectionCriteria
  +     * @param cld ClassDescriptor
  +     */
  +    private String toSQLClause(SelectionCriteria c, ClassDescriptor cld)
  +    {
  +        String colName = c.getAttribute();
  +        return colName + c.getClause() + " ? ";
  +    }
  +
  +    /**
  +     * Answer the SQL-Clause for a SqlCriteria
  +     *
  +     * @param c SqlCriteria
  +     * @param cld ClassDescriptor
  +     */
  +    private String toSQLClause(SqlCriteria c, ClassDescriptor cld)
  +    {
  +        return c.getClause();
  +    }
  +
  +    /**
  +     * Answer the SQL-Clause for an ExistsCriteria
  +     *
  +     * @param c ExistsCriteria
  +     * @param cld ClassDescriptor
  +     */
  +    private String toSQLClause(ExistsCriteria c, ClassDescriptor cld)
  +    {
  +        StringBuffer buf = new StringBuffer();
  +        Query subQuery = (Query) c.getValue();
  +
  +        buf.append(c.getClause());
  +        buf.append(" (");
  +
  +        // If it's a proper call
  +        if (cld != null)
  +        {
  +            buf.append(
  +                    getPreparedSelectStatement(subQuery, cld.getRepository().getDescriptorFor(subQuery.getSearchClass())));
  +
  +            // Otherwise it's most likely a call to toString()
  +        }
  +        else
  +        {
  +            buf.append(subQuery);
  +        }
  +
  +        buf.append(")");
  +        return buf.toString();
  +    }
  +
  +    /**
  +     * generate a prepared DELETE-Statement according to query
  +     * @param query the Query
  +     * @param cld the ClassDescriptor
  +     */
  +    public String getPreparedDeleteStatement(Query query, ClassDescriptor cld)
  +    {
  +        SqlCacheKey key = new SqlCacheKey(query, cld, SqlCacheKey.TYPE_DELETE);
           String result = (String) m_sqlCacheMap.get(key);
           if (result == null)
           {
               SqlStatement sql;
  -            sql = new SqlDeleteByQuery(cld, query, logger);
  +            sql = new SqlDeleteByQuery(pf, cld, query, logger);
               result = sql.getStatement();
               m_sqlCacheMap.put(key, result);
               logger.debug("SQL: " + result);
           }
           return result;
  -	}
  +    }
   
   }
  
  
  
  1.4       +19 -8     jakarta-ojb/src/java/org/apache/ojb/broker/accesslayer/sql/SqlGeneratorFactory.java
  
  Index: SqlGeneratorFactory.java
  ===================================================================
  RCS file: /home/cvs/jakarta-ojb/src/java/org/apache/ojb/broker/accesslayer/sql/SqlGeneratorFactory.java,v
  retrieving revision 1.3
  retrieving revision 1.4
  diff -u -r1.3 -r1.4
  --- SqlGeneratorFactory.java	5 Oct 2002 16:45:36 -0000	1.3
  +++ SqlGeneratorFactory.java	24 Dec 2002 12:57:51 -0000	1.4
  @@ -55,16 +55,22 @@
    */
   
   import org.apache.ojb.broker.util.factory.ConfigurableFactory;
  +import org.apache.ojb.broker.platforms.Platform;
  +
  +import java.util.Map;
  +import java.util.HashMap;
   
   /**
    * This factory creates SqlGenerator instances. it is a configurable factory and
    * can be used to generate user defined implementations too.
  - * @author Thomas Mahler 
  + * @author Thomas Mahler
    */
   public class SqlGeneratorFactory extends ConfigurableFactory
   {
   	private static SqlGeneratorFactory instance = null;
   
  +    private Map generatorMap = new HashMap();
  +
   	/**
   	 * @see org.apache.ojb.broker.util.factory.ConfigurableFactory#getConfigurationKey()
   	 */
  @@ -72,19 +78,24 @@
   	{
   		return "SqlGeneratorClass";
   	}
  -	
  -	public SqlGenerator createSqlGenerator()
  +
  +	public SqlGenerator createSqlGenerator(Platform pf)
   	{
  -		return (SqlGenerator) this.createNewInstance();	
  +		SqlGenerator gen = (SqlGenerator) generatorMap.get(pf.getClass().getName());
  +        if(gen == null)
  +        {
  +            gen = (SqlGenerator) this.createNewInstance(Platform.class, pf);
  +            generatorMap.put(pf.getClass(), gen);
  +        }
  +        return gen;
   	}
  -	
  +
   	public static SqlGeneratorFactory getInstance()
   	{
   		if (instance == null)
   		{
  -			instance = new SqlGeneratorFactory();	
  +			instance = new SqlGeneratorFactory();
   		}
  -		return instance;	
  +		return instance;
   	}
  -
   }
  
  
  
  1.15      +1062 -1056jakarta-ojb/src/java/org/apache/ojb/broker/accesslayer/sql/SqlQueryStatement.java
  
  Index: SqlQueryStatement.java
  ===================================================================
  RCS file: /home/cvs/jakarta-ojb/src/java/org/apache/ojb/broker/accesslayer/sql/SqlQueryStatement.java,v
  retrieving revision 1.14
  retrieving revision 1.15
  diff -u -r1.14 -r1.15
  --- SqlQueryStatement.java	16 Dec 2002 18:23:19 -0000	1.14
  +++ SqlQueryStatement.java	24 Dec 2002 12:57:51 -0000	1.15
  @@ -54,600 +54,614 @@
    * <http://www.apache.org/>.
    */
   
  -import java.util.ArrayList;
  -import java.util.Collection;
  -import java.util.Enumeration;
  -import java.util.HashMap;
  -import java.util.HashSet;
  -import java.util.Iterator;
  -import java.util.List;
  -
   import org.apache.ojb.broker.accesslayer.JoinSyntaxTypes;
   import org.apache.ojb.broker.metadata.ClassDescriptor;
   import org.apache.ojb.broker.metadata.CollectionDescriptor;
   import org.apache.ojb.broker.metadata.FieldDescriptor;
  -import org.apache.ojb.broker.metadata.FieldHelper;
   import org.apache.ojb.broker.metadata.ObjectReferenceDescriptor;
  +import org.apache.ojb.broker.metadata.FieldHelper;
   import org.apache.ojb.broker.platforms.Platform;
  -import org.apache.ojb.broker.platforms.PlatformFactory;
  -import org.apache.ojb.broker.query.*;
  +import org.apache.ojb.broker.query.BetweenCriteria;
  +import org.apache.ojb.broker.query.ColumnCriteria;
  +import org.apache.ojb.broker.query.Criteria;
  +import org.apache.ojb.broker.query.ExistsCriteria;
  +import org.apache.ojb.broker.query.FieldCriteria;
  +import org.apache.ojb.broker.query.InCriteria;
  +import org.apache.ojb.broker.query.MtoNQuery;
  +import org.apache.ojb.broker.query.NullCriteria;
  +import org.apache.ojb.broker.query.Query;
  +import org.apache.ojb.broker.query.QueryByCriteria;
  +import org.apache.ojb.broker.query.SelectionCriteria;
  +import org.apache.ojb.broker.query.SqlCriteria;
  +import org.apache.ojb.broker.util.SqlHelper;
   import org.apache.ojb.broker.util.logging.Logger;
   
  +import java.util.ArrayList;
  +import java.util.Collection;
  +import java.util.Enumeration;
  +import java.util.HashMap;
  +import java.util.HashSet;
  +import java.util.Iterator;
  +import java.util.List;
  +
   /**
  - * Model a Statement based on Query 
  - * 
  + * Model a Statement based on Query.
  + *
    * @author <a href="mailto:jbraeuchi@hotmail.com">Jakob Braeuchi</a>
    * @version $Id$
    */
   public abstract class SqlQueryStatement implements SqlStatement, JoinSyntaxTypes
   {
  -	/** the target table of the query */
  -	private TableAlias m_root;	
  -	/** the query */
  -	private Query m_query;
  -	/** the columns to select (null means all) */
  -	private String[] m_columns;
  -	/** the mapping of paths to TableAliases */
  -	private HashMap m_pathToAlias = new HashMap();
  -	/** maps trees of joins to criteria */
  -	private HashMap m_joinTreeToCriteria = new HashMap();
  -	/** the join syntax type, one of the constants from */
  -	private byte m_joinSyntaxType;
  -	/** the logger */
  -	private Logger m_logger;
  -	private ClassDescriptor m_classDescriptor;
  -
  -	/**
  -	 * Constructor for SqlCriteriaStatement.
  -	 */
  -	public SqlQueryStatement(ClassDescriptor cld, String[] columns, Query query, Logger logger)
  -	{
  -		Platform pf = PlatformFactory.getPlatformFor(cld.getConnectionDescriptor());
  -
  -		this.m_classDescriptor = cld;
  -		this.m_logger = logger;
  -		this.m_root = createTableAlias(cld, "");
  -		this.m_query = query;
  -		this.m_columns = columns;
  -		this.m_joinSyntaxType = pf.getJoinSyntaxType();
  -
  -		// In some cases it is necessary to split the query criteria
  -		// and then to generate UNION of several SELECTs
  -		// We build the joinTreeToCriteria mapping,
  -		if (query != null)
  -		{
  -			splitCriteria();
  -		}
  -	}
  +    /** the logger */
  +    private Logger m_logger;
  +    /** the target table of the query */
  +    private TableAlias m_root;
  +    /** the query */
  +    private Query m_query;
  +    /** the columns to select (null means all) */
  +    private String[] m_columns;
  +    /** the mapping of paths to TableAliases */
  +    private HashMap m_pathToAlias = new HashMap();
  +    /** maps trees of joins to criteria */
  +    private HashMap m_joinTreeToCriteria = new HashMap();
   
  -	/**
  -	 * Answer the appropriate ColumnName <br>
  -	 * if a FIELDDESCRIPTOR is found for the Criteria the colName is taken from
  -	 * there otherwise its taken from Criteria. <br>
  -	 * field names in functions (ie: sum(name) ) are tried to resolve
  -	 * ie: name from FIELDDESCRIPTOR , UPPER(name_test) from Criteria<br>
  -	 * also resolve pathExpression adress.city or owner.konti.saldo
  -	 */
  -	protected void appendColName(String attr, boolean useOuterJoins, StringBuffer buf)
  -	{
  -		FieldDescriptor fld = null;
  -		int braceBegin;
  -		int braceEnd;
  -		String functionName;
  -		String colName;
  -		int sp;
  -		TableAlias alias = null;
  -
  -		braceBegin = attr.indexOf("(");
  -		braceEnd = attr.indexOf(")");
  -		if (braceBegin >= 0 && braceEnd >= 0)
  -		{
  -			functionName = attr.substring(0, braceBegin);
  -			colName = attr.substring(braceBegin + 1, braceEnd).trim();
  -		}
  -		else
  -		{
  -			functionName = null;
  -			colName = attr;
  -		}
  +    private Platform pf;
  +    private ClassDescriptor m_cld;
   
  -		sp = colName.lastIndexOf(".");
  +    /**
  +     * Constructor for SqlCriteriaStatement.
  +     */
  +    public SqlQueryStatement(Platform pf, ClassDescriptor cld, String[] columns, Query query, Logger logger)
  +    {
  +        this.m_logger = logger;
  +        this.m_root = createTableAlias(cld, "");
  +        this.m_query = query;
  +        this.m_columns = columns;
  +        this.pf = pf;
  +        this.m_cld = cld;
  +
  +        // In some cases it is necessary to split the query criteria
  +        // and then to generate UNION of several SELECTs
  +        // We build the joinTreeToCriteria mapping,
  +        if (query != null)
  +        {
  +            splitCriteria();
  +        }
  +    }
   
  -		if (sp == -1)
  -		{
  -			fld = getRoot().cld.getFieldDescriptorByName(colName);
  -			if (fld == null)
  -			{
  -				/**
  -				 * MBAIRD
  -				 * possible we've referred to an object not by key, ie is_undefined(obj) instead of
  -				 * is_undefined(obj.pkField)
  -				 * I don't know how to join on multiple FK fields, so check the first one.
  -				 */
  -				ObjectReferenceDescriptor ord = getRoot().cld.getObjectReferenceDescriptorByName(colName);
  -				if (ord != null)
  -				{
  -					if (ord.getForeignKeyFields().size() > 0)
  -					{
  -						fld =
  -							getRoot().cld.getFieldDescriptorByIndex(
  -								((Integer) ord.getForeignKeyFields().get(0)).intValue());
  -					}
  -				}
  -			}
  -			alias = getRoot();
  -		}
  -		else
  -		{
  -			String fieldName = colName.substring(sp + 1);
  -			String pathName = colName.substring(0, sp);
  -			alias = getTableAlias(pathName, useOuterJoins);
  -			if (alias != null)
  -			{
  -				fld = alias.cld.getFieldDescriptorByName(fieldName);
  -				if (fld == null)
  -				{
  -					/**
  -					 * MBAIRD
  -					 * potentially people are referring to objects, not to the object's primary key, and then we need to take the
  -					 * primary key attribute of the referenced object to help them out.
  -					 */
  -					ObjectReferenceDescriptor ord = alias.cld.getObjectReferenceDescriptorByName(fieldName);
  -					if (ord != null)
  -					{
  -						ClassDescriptor cld = alias.cld.getRepository().getDescriptorFor(ord.getItemClass());
  -						if (cld != null)
  -						{
  -							/**
  -							 * MBAIRD
  -							 * just take the first one as no way to specify two values for primary key?
  -							 */
  -							fld =
  -								alias.cld.getFieldDescriptorByName(cld.getPkFields()[0].getPersistentField().getName());
  -						}
  -					}
  -				}
  -			}
  -		}
  +    protected ClassDescriptor getClassDescriptor()
  +    {
  +        return m_cld;
  +    }
   
  -		if (functionName != null) // rebuild function
  -		{
  -			buf.append(functionName);
  -			buf.append("(");
  -		}
  +    /**
  +     * Answer the appropriate ColumnName <br>
  +     * if a FIELDDESCRIPTOR is found for the Criteria the colName is taken from
  +     * there otherwise its taken from Criteria. <br>
  +     * field names in functions (ie: sum(name) ) are tried to resolve
  +     * ie: name from FIELDDESCRIPTOR , UPPER(name_test) from Criteria<br>
  +     * also resolve pathExpression adress.city or owner.konti.saldo
  +     */
  +    protected void appendColName(String attr, boolean useOuterJoins, StringBuffer buf)
  +    {
  +        FieldDescriptor fld = null;
  +        int braceBegin;
  +        int braceEnd;
  +        String functionName;
  +        String colName;
  +        int sp;
  +        TableAlias alias = null;
  +
  +        braceBegin = attr.indexOf("(");
  +        braceEnd = attr.indexOf(")");
  +        if (braceBegin >= 0 && braceEnd >= 0)
  +        {
  +            functionName = attr.substring(0, braceBegin);
  +            colName = attr.substring(braceBegin + 1, braceEnd).trim();
  +        }
  +        else
  +        {
  +            functionName = null;
  +            colName = attr;
  +        }
  +
  +        sp = colName.lastIndexOf(".");
  +
  +        if (sp == -1)
  +        {
  +            fld = getRoot().cld.getFieldDescriptorByName(colName);
  +            if (fld == null)
  +            {
  +                /**
  +                 * MBAIRD
  +                 * possible we've referred to an object not by key, ie is_undefined(obj) instead of
  +                 * is_undefined(obj.pkField)
  +                 * I don't know how to join on multiple FK fields, so check the first one.
  +                 */
  +                ObjectReferenceDescriptor ord = getRoot().cld.getObjectReferenceDescriptorByName(colName);
  +                if (ord != null)
  +                {
  +                    if (ord.getForeignKeyFields().size() > 0)
  +                    {
  +                        fld =
  +                                getRoot().cld.getFieldDescriptorByIndex(
  +                                        ((Integer) ord.getForeignKeyFields().get(0)).intValue());
  +                    }
  +                }
  +            }
  +            alias = getRoot();
  +        }
  +        else
  +        {
  +            String fieldName = colName.substring(sp + 1);
  +            String pathName = colName.substring(0, sp);
  +            alias = getTableAlias(pathName, useOuterJoins);
  +            if (alias != null)
  +            {
  +                fld = alias.cld.getFieldDescriptorByName(fieldName);
  +                if (fld == null)
  +                {
  +                    /**
  +                     * MBAIRD
  +                     * potentially people are referring to objects, not to the object's primary key, and then we need to take the
  +                     * primary key attribute of the referenced object to help them out.
  +                     */
  +                    ObjectReferenceDescriptor ord = alias.cld.getObjectReferenceDescriptorByName(fieldName);
  +                    if (ord != null)
  +                    {
  +                        ClassDescriptor cld = alias.cld.getRepository().getDescriptorFor(ord.getItemClass());
  +                        if (cld != null)
  +                        {
  +                            /**
  +                             * MBAIRD
  +                             * just take the first one as no way to specify two values for primary key?
  +                             */
  +                            fld =
  +                                    alias.cld.getFieldDescriptorByName(cld.getPkFields()[0].getPersistentField().getName());
  +                        }
  +                    }
  +                }
  +            }
  +        }
  +
  +        if (functionName != null) // rebuild function
  +        {
  +            buf.append(functionName);
  +            buf.append("(");
  +        }
  +
  +        if (fld != null)
  +        {
  +            buf.append(alias.alias);
  +            buf.append(".");
  +            buf.append(fld.getColumnName());
  +        }
  +        else
  +        {
  +            buf.append(colName);
  +        }
  +
  +        if (functionName != null) // rebuild function
  +        {
  +            buf.append(")");
  +        }
  +    }
   
  -		if (fld != null)
  -		{
  -			buf.append(alias.alias);
  -			buf.append(".");
  -			buf.append(fld.getColumnName());
  -		}
  -		else
  -		{
  -			buf.append(colName);
  -		}
  +    /**
  +     * appends a WHERE-clause to the Statement
  +     */
  +    protected void appendWhereClause(StringBuffer where, Criteria crit, StringBuffer stmt)
  +    {
  +        if (where.length() == 0)
  +        {
  +            where = null;
  +        }
  +
  +        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 ");
  +
  +            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(")");
  +                }
  +            }
  +        }
  +    }
   
  -		if (functionName != null) // rebuild function
  -		{
  -			buf.append(")");
  -		}
  -	}
  +    /**
  +     * @param criteria Selection criteria
  +     */
  +    private String asSQLStatement(Criteria crit)
  +    {
  +        Enumeration e = crit.getElements();
  +        StringBuffer statement = new StringBuffer();
  +        while (e.hasMoreElements())
  +        {
  +            Object o = e.nextElement();
  +            if (o instanceof Criteria)
  +            {
  +                String addAtStart;
  +                String addAtEnd;
  +                Criteria pc = (Criteria) o;
  +                // need to add parenthesises?
  +                if (pc.isEmbraced())
  +                {
  +                    addAtStart = " (";
  +                    addAtEnd = ")";
  +                }
  +                else
  +                {
  +                    addAtStart = "";
  +                    addAtEnd = "";
  +                }
  +
  +                switch (pc.getType())
  +                {
  +                    case (Criteria.OR):
  +                        {
  +                            statement.append(" OR ");
  +                            statement.append(addAtStart);
  +                            statement.append(asSQLStatement(pc));
  +                            statement.append(addAtEnd);
  +                            break;
  +                        }
  +                    case (Criteria.AND):
  +                        {
  +                            statement.insert(0, "( ");
  +                            statement.append(") AND ");
  +                            statement.append(addAtStart);
  +                            statement.append(asSQLStatement(pc));
  +                            statement.append(addAtEnd);
  +                            break;
  +                        }
  +                }
  +            }
  +            else
  +            {
  +                SelectionCriteria c = (SelectionCriteria) o;
  +                if (statement.length() == 0)
  +                {
  +                    appendSQLClause(c, statement);
  +                }
  +                else
  +                {
  +                    statement.insert(0, "(");
  +                    statement.append(") AND ");
  +                    appendSQLClause(c, statement);
  +                }
  +            }
  +        } // while
   
  -	/**
  -	 * appends a WHERE-clause to the Statement
  -	 */
  -	protected void appendWhereClause(StringBuffer where, Criteria crit, StringBuffer stmt)
  -	{
  -		if (where.length() == 0)
  -		{
  -			where = null;
  -		}
  +        return (statement.length() == 0 ? null : statement.toString());
  +    }
   
  -		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 ");
  +    /**
  +     * Answer the SQL-Clause for a BetweenCriteria
  +     *
  +     * @param c BetweenCriteria
  +     */
  +    private void appendSQLClause(BetweenCriteria c, StringBuffer buf)
  +    {
  +        appendColName(c.getAttribute(), false, buf);
  +        buf.append(c.getClause());
  +        appendParameter(c.getValue(), buf);
  +        buf.append("AND");
  +        appendParameter(c.getValue2(), buf);
  +    }
   
  -			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(")");
  -				}
  -			}
  -		}
  -	}
  +    /**
  +     * Answer the SQL-Clause for a ColumnCriteria
  +     *
  +     * @param c ColumnCriteria
  +     */
  +    private void appendSQLClause(ColumnCriteria c, StringBuffer buf)
  +    {
  +        appendColName(c.getAttribute(), false, buf);
  +        buf.append(c.getClause());
  +        appendColName((String) c.getValue(), false, buf);
  +    }
   
  -	/**
  -	 * @param criteria Selection criteria
  -	 */
  -	private String asSQLStatement(Criteria crit)
  -	{
  -		Enumeration e = crit.getElements();
  -		StringBuffer statement = new StringBuffer();
  -		while (e.hasMoreElements())
  -		{
  -			Object o = e.nextElement();
  -			if (o instanceof Criteria)
  -			{
  -				String addAtStart;
  -				String addAtEnd;
  -				Criteria pc = (Criteria) o;
  -				// need to add parenthesises?
  -				if (pc.isEmbraced())
  -				{
  -					addAtStart = " (";
  -					addAtEnd = ")";
  -				}
  -				else
  -				{
  -					addAtStart = "";
  -					addAtEnd = "";
  -				}
  -
  -				switch (pc.getType())
  -				{
  -					case (Criteria.OR) :
  -						{
  -							statement.append(" OR ");
  -							statement.append(addAtStart);
  -							statement.append(asSQLStatement(pc));
  -							statement.append(addAtEnd);
  -							break;
  -						}
  -					case (Criteria.AND) :
  -						{
  -							statement.insert(0, "( ");
  -							statement.append(") AND ");
  -							statement.append(addAtStart);
  -							statement.append(asSQLStatement(pc));
  -							statement.append(addAtEnd);
  -							break;
  -						}
  -				}
  -			}
  -			else
  -			{
  -				SelectionCriteria c = (SelectionCriteria) o;
  -				if (statement.length() == 0)
  -				{
  -					appendSQLClause(c, statement);
  -				}
  -				else
  -				{
  -					statement.insert(0, "(");
  -					statement.append(") AND ");
  -					appendSQLClause(c, statement);
  -				}
  -			}
  -		} // while
  +    /**
  +     * Answer the SQL-Clause for an ExistsCriteria
  +     * @param c ExistsCriteria
  +     */
  +    private void appendSQLClause(ExistsCriteria c, StringBuffer buf)
  +    {
  +        Query subQuery = (Query) c.getValue();
   
  -		return (statement.length() == 0 ? null : statement.toString());
  -	}
  +        buf.append(c.getClause());
  +        appendSubQuery(subQuery, buf);
  +    }
   
  -	/**
  -	 * Answer the SQL-Clause for a BetweenCriteria
  -	 *
  -	 * @param c BetweenCriteria
  -	 */
  -	private void appendSQLClause(BetweenCriteria c, StringBuffer buf)
  -	{
  -		appendColName(c.getAttribute(), false, buf);
  -		buf.append(c.getClause());
  -		appendParameter(c.getValue(), buf);
  -		buf.append("AND");
  -		appendParameter(c.getValue2(), buf);
  -	}
  -
  -	/**
  -	 * Answer the SQL-Clause for a ColumnCriteria
  -	 *
  -	 * @param c ColumnCriteria
  -	 */
  -	private void appendSQLClause(ColumnCriteria c, StringBuffer buf)
  -	{
  -		appendColName(c.getAttribute(), false, buf);
  -		buf.append(c.getClause());
  -		appendColName((String) c.getValue(), false, buf);
  -	}
  -
  -	/**
  -	 * Answer the SQL-Clause for an ExistsCriteria
  -	 * @param c ExistsCriteria
  -	 */
  -	private void appendSQLClause(ExistsCriteria c, StringBuffer buf)
  -	{
  -		Query subQuery = (Query) c.getValue();
  -
  -		buf.append(c.getClause());
  -		appendSubQuery(subQuery, buf);
  -	}
  -
  -	/**
  -	 * Answer the SQL-Clause for a FieldCriteria
  -	 *
  -	 * @param c FieldCriteria
  -	 */
  -	private void appendSQLClause(FieldCriteria c, StringBuffer buf)
  -	{
  -		appendColName(c.getAttribute(), false, buf);
  -		buf.append(c.getClause());
  -		appendColName((String) c.getValue(), false, buf);
  -	}
  -
  -	/**
  -	 * Answer the SQL-Clause for an InCriteria
  -	 *
  -	 * @param c SelectionCriteria
  -	 */
  -	private void appendSQLClause(InCriteria c, StringBuffer buf)
  -	{
  -		appendColName(c.getAttribute(), false, buf);
  -		buf.append(c.getClause());
  -		
  -		if (c.getValue() instanceof Collection)
  -		{
  -			Object[] values = ((Collection) c.getValue()).toArray();
  -			int size = ((Collection) c.getValue()).size();
  +    /**
  +     * Answer the SQL-Clause for a FieldCriteria
  +     *
  +     * @param c FieldCriteria
  +     */
  +    private void appendSQLClause(FieldCriteria c, StringBuffer buf)
  +    {
  +        appendColName(c.getAttribute(), false, buf);
  +        buf.append(c.getClause());
  +        appendColName((String) c.getValue(), false, buf);
  +    }
   
  -			buf.append("(");
  -			for (int i = 0; i < size - 1; i++)
  -			{
  -				appendParameter(values[i], buf);
  -				buf.append(",");
  -			}
  -			appendParameter(values[size - 1], buf);
  -			buf.append(")");
  -		}
  -		else
  -		{
  -			appendParameter(c.getValue(), buf);
  -		}	
  -	}
  -
  -	/**
  -	 * Answer the SQL-Clause for a NullCriteria
  -	 *
  -	 * @param c NullCriteria
  -	 */
  -	private void appendSQLClause(NullCriteria c, StringBuffer buf)
  -	{
  -		appendColName(c.getAttribute(), false, buf);
  -		buf.append(c.getClause());
  -	}
  -
  -	/**
  -	 * Answer the SQL-Clause for a SelectionCriteria
  -	 * @param c SelectionCriteria
  -	 */
  -	protected void appendSQLClause(SelectionCriteria c, StringBuffer buf)
  -	{
  -		if (c instanceof FieldCriteria)
  -		{
  -			appendSQLClause((FieldCriteria) c, buf);
  -		}
  -		else if (c instanceof ColumnCriteria)
  -		{
  -			appendSQLClause((ColumnCriteria) c, buf);
  -		}
  -		else if (c instanceof NullCriteria)
  -		{
  -			appendSQLClause((NullCriteria) c, buf);
  -		}
  -		else if (c instanceof BetweenCriteria)
  -		{
  -			appendSQLClause((BetweenCriteria) c, buf);
  -		}
  -		else if (c instanceof InCriteria)
  -		{
  -			appendSQLClause((InCriteria) c, buf);
  -		}
  -		else if (c instanceof SqlCriteria)
  -		{
  -			appendSQLClause((SqlCriteria) c, buf);
  -		}
  -		else if (c instanceof ExistsCriteria)
  -		{
  -			appendSQLClause((ExistsCriteria) c, buf);
  -		}
  -		else
  -		{
  -			appendColName(c.getAttribute(), false, buf);
  -			buf.append(c.getClause());
  -			appendParameter(c.getValue(), buf);
  -		}
  -	}
  +    /**
  +     * Answer the SQL-Clause for an InCriteria
  +     *
  +     * @param c SelectionCriteria
  +     */
  +    private void appendSQLClause(InCriteria c, StringBuffer buf)
  +    {
  +        appendColName(c.getAttribute(), false, buf);
  +        buf.append(c.getClause());
   
  -	/**
  -	 * Answer the SQL-Clause for a SqlCriteria
  -	 *
  -	 * @param c SqlCriteria
  -	 */
  -	private void appendSQLClause(SqlCriteria c, StringBuffer buf)
  -	{
  -		buf.append(c.getClause());
  -	}
  -
  -	/**
  -	 * Append the Parameter 
  -	 * Add the place holder ? or the SubQuery
  -	 * @param value the value of the criteria
  -	 */
  -	private void appendParameter(Object value, StringBuffer buf)
  -	{
  -		if (value instanceof Query)
  -		{
  -			appendSubQuery((Query) value, buf);
  -		}
  -		else
  -		{
  -			buf.append(" ? ");
  -		}
  -	}
  +        if (c.getValue() instanceof Collection)
  +        {
  +            Object[] values = ((Collection) c.getValue()).toArray();
  +            int size = ((Collection) c.getValue()).size();
  +
  +            buf.append("(");
  +            for (int i = 0; i < size - 1; i++)
  +            {
  +                appendParameter(values[i], buf);
  +                buf.append(",");
  +            }
  +            appendParameter(values[size - 1], buf);
  +            buf.append(")");
  +        }
  +        else
  +        {
  +            appendParameter(c.getValue(), buf);
  +        }
  +    }
   
  -	/**
  -	 * Append a SubQuery the SQL-Clause
  -	 * @param subQuery the subQuery value of SelectionCriteria
  -	 */
  -	private void appendSubQuery(Query subQuery, StringBuffer buf)
  -	{
  -		ClassDescriptor cld = getRoot().cld.getRepository().getDescriptorFor(subQuery.getSearchClass());
  -		String subSql = SqlGeneratorFactory.getInstance().createSqlGenerator().getPreparedSelectStatement(subQuery, cld);
  -
  -		buf.append(" (");
  -		buf.append(subSql);
  -		buf.append(") ");
  -	}
  -
  -	/**
  -	 * Get TableAlias by the path from the target table of the query.
  -	 * @param path the path from the target table of the query to this TableAlias.
  -	 * @param useOuterJoins use outer join to join this table with the previous
  -	 * table in the path.
  -	 */
  -	private TableAlias getTableAlias(String path, boolean useOuterJoins)
  -	{
  -		TableAlias curr, prev, indirect;
  -		String attr, attrPath = null;
  -		ObjectReferenceDescriptor ord;
  -		CollectionDescriptor cod;
  -		ClassDescriptor cld = null;
  -		Object[] prevKeys = null;
  -		Object[] keys = null;
  -		Join join;
  -		ArrayList descriptors;
  +    /**
  +     * Answer the SQL-Clause for a NullCriteria
  +     *
  +     * @param c NullCriteria
  +     */
  +    private void appendSQLClause(NullCriteria c, StringBuffer buf)
  +    {
  +        appendColName(c.getAttribute(), false, buf);
  +        buf.append(c.getClause());
  +    }
   
  -		curr = (TableAlias) m_pathToAlias.get(path);
  -		if (curr == null)
  -		{
  -			descriptors = getRoot().cld.getAttributeDescriptorsForPath(path);
  -			prev = getRoot();
  +    /**
  +     * Answer the SQL-Clause for a SelectionCriteria
  +     * @param c SelectionCriteria
  +     */
  +    protected void appendSQLClause(SelectionCriteria c, StringBuffer buf)
  +    {
  +        if (c instanceof FieldCriteria)
  +        {
  +            appendSQLClause((FieldCriteria) c, buf);
  +        }
  +        else if (c instanceof ColumnCriteria)
  +        {
  +            appendSQLClause((ColumnCriteria) c, buf);
  +        }
  +        else if (c instanceof NullCriteria)
  +        {
  +            appendSQLClause((NullCriteria) c, buf);
  +        }
  +        else if (c instanceof BetweenCriteria)
  +        {
  +            appendSQLClause((BetweenCriteria) c, buf);
  +        }
  +        else if (c instanceof InCriteria)
  +        {
  +            appendSQLClause((InCriteria) c, buf);
  +        }
  +        else if (c instanceof SqlCriteria)
  +        {
  +            appendSQLClause((SqlCriteria) c, buf);
  +        }
  +        else if (c instanceof ExistsCriteria)
  +        {
  +            appendSQLClause((ExistsCriteria) c, buf);
  +        }
  +        else
  +        {
  +            appendColName(c.getAttribute(), false, buf);
  +            buf.append(c.getClause());
  +            appendParameter(c.getValue(), buf);
  +        }
  +    }
   
  -			for (int i = 0; i < descriptors.size(); i++)
  -			{
  -				if (!(descriptors.get(i) instanceof ObjectReferenceDescriptor))
  -				{
  -					// only use Collection- and ObjectReferenceDescriptor
  -					continue;
  -				}
  -
  -				ord = (ObjectReferenceDescriptor) descriptors.get(i);
  -				attr = ord.getAttributeName();
  -				if (attrPath == null)
  -				{
  -					attrPath = attr;
  -				}
  -				else
  -				{
  -					attrPath = attrPath + "." + attr;
  -				}
  -
  -				// look for 1:n or m:n   
  -				if (ord instanceof CollectionDescriptor)
  -				{
  -					cod = (CollectionDescriptor) ord;
  -					cld = getItemClassDescriptor(cod, attr);
  -
  -					if (!cod.isMtoNRelation())
  -					{
  -						prevKeys = prev.cld.getPkFields();
  -						keys = cod.getForeignKeyFieldDescriptors(cld);
  -					}
  -					else
  -					{
  -						String mnAttrPath = attrPath + "*";
  -						indirect = getTableAlias(mnAttrPath);
  -						if (indirect == null)
  -						{	
  -							indirect = createTableAlias(cod.getIndirectionTable(), mnAttrPath);
  -						}
  -							
  -						// we need two Joins for m:n
  -						// 1.) prev class to indirectionTable
  -						prevKeys = prev.cld.getPkFields();
  -						join = new Join(prev, prevKeys, indirect, cod.getFksToThisClass(), useOuterJoins, attr + "*");
  -						prev.addJoin(join);
  -
  -						// 2.) indirectionTable to the current Class
  -						prev = indirect;
  -						prevKeys = cod.getFksToItemClass();
  -						keys = cld.getPkFields();
  -					}
  -				}
  -				else
  -				{
  -					// must be n:1 or 1:1
  -					cld = getItemClassDescriptor(ord, attr);
  -
  -					prevKeys = ord.getForeignKeyFieldDescriptors(prev.cld);
  -					keys = cld.getPkFields();
  -				}
  -
  -				curr = getTableAlias(attrPath);
  -				if (curr == null)
  -				{
  -					curr = createTableAlias(cld, attrPath);
  -					join = new Join(prev, prevKeys, curr, keys, useOuterJoins, attr);
  -					prev.addJoin(join);
  -				}
  +    /**
  +     * Answer the SQL-Clause for a SqlCriteria
  +     *
  +     * @param c SqlCriteria
  +     */
  +    private void appendSQLClause(SqlCriteria c, StringBuffer buf)
  +    {
  +        buf.append(c.getClause());
  +    }
   
  -				prev = curr;
  -			}
  -		}
  +    /**
  +     * Append the Parameter
  +     * Add the place holder ? or the SubQuery
  +     * @param value the value of the criteria
  +     */
  +    private void appendParameter(Object value, StringBuffer buf)
  +    {
  +        if (value instanceof Query)
  +        {
  +            appendSubQuery((Query) value, buf);
  +        }
  +        else
  +        {
  +            buf.append(" ? ");
  +        }
  +    }
  +
  +    /**
  +     * Append a SubQuery the SQL-Clause
  +     * @param subQuery the subQuery value of SelectionCriteria
  +     */
  +    private void appendSubQuery(Query subQuery, StringBuffer buf)
  +    {
  +        ClassDescriptor cld = getRoot().cld.getRepository().getDescriptorFor(subQuery.getSearchClass());
  +        String subSql = SqlGeneratorFactory.getInstance().createSqlGenerator(pf).getPreparedSelectStatement(subQuery, cld);
   
  -		return curr;
  -	}
  +        buf.append(" (");
  +        buf.append(subSql);
  +        buf.append(") ");
  +    }
   
  -	/**
  -	 * Create new TableAlias for path
  -	 * @param cld the class descriptor for the TableAlias
  -	 * @param path the path from the target table of the query to this TableAlias.
  -	 */
  -	private TableAlias createTableAlias(ClassDescriptor cld, String path)
  -	{
  -		TableAlias alias;
  +    /**
  +     * Get TableAlias by the path from the target table of the query.
  +     * @param path the path from the target table of the query to this TableAlias.
  +     * @param useOuterJoins use outer join to join this table with the previous
  +     * table in the path.
  +     */
  +    private TableAlias getTableAlias(String path, boolean useOuterJoins)
  +    {
  +        TableAlias curr, prev, indirect;
  +        String attr, attrPath = null;
  +        ObjectReferenceDescriptor ord;
  +        CollectionDescriptor cod;
  +        ClassDescriptor cld = null;
  +        Object[] prevKeys = null;
  +        Object[] keys = null;
  +        Join join;
  +        ArrayList descriptors;
  +
  +        curr = (TableAlias) m_pathToAlias.get(path);
  +        if (curr == null)
  +        {
  +            descriptors = getRoot().cld.getAttributeDescriptorsForPath(path);
  +            prev = getRoot();
  +
  +            for (int i = 0; i < descriptors.size(); i++)
  +            {
  +                if (!(descriptors.get(i) instanceof ObjectReferenceDescriptor))
  +                {
  +                    // only use Collection- and ObjectReferenceDescriptor
  +                    continue;
  +                }
  +
  +                ord = (ObjectReferenceDescriptor) descriptors.get(i);
  +                attr = ord.getAttributeName();
  +                if (attrPath == null)
  +                {
  +                    attrPath = attr;
  +                }
  +                else
  +                {
  +                    attrPath = attrPath + "." + attr;
  +                }
  +
  +                // look for 1:n or m:n
  +                if (ord instanceof CollectionDescriptor)
  +                {
  +                    cod = (CollectionDescriptor) ord;
  +                    cld = getItemClassDescriptor(cod, attr);
  +
  +                    if (!cod.isMtoNRelation())
  +                    {
  +                        prevKeys = prev.cld.getPkFields();
  +                        keys = cod.getForeignKeyFieldDescriptors(cld);
  +                    }
  +                    else
  +                    {
  +                        String mnAttrPath = attrPath + "*";
  +                        indirect = getTableAlias(mnAttrPath);
  +                        if (indirect == null)
  +                        {
  +                            indirect = createTableAlias(cod.getIndirectionTable(), mnAttrPath);
  +                        }
  +
  +                        // we need two Joins for m:n
  +                        // 1.) prev class to indirectionTable
  +                        prevKeys = prev.cld.getPkFields();
  +                        join = new Join(prev, prevKeys, indirect, cod.getFksToThisClass(), useOuterJoins, attr + "*");
  +                        prev.addJoin(join);
  +
  +                        // 2.) indirectionTable to the current Class
  +                        prev = indirect;
  +                        prevKeys = cod.getFksToItemClass();
  +                        keys = cld.getPkFields();
  +                    }
  +                }
  +                else
  +                {
  +                    // must be n:1 or 1:1
  +                    cld = getItemClassDescriptor(ord, attr);
  +
  +                    prevKeys = ord.getForeignKeyFieldDescriptors(prev.cld);
  +                    keys = cld.getPkFields();
  +                }
  +
  +                curr = getTableAlias(attrPath);
  +                if (curr == null)
  +                {
  +                    curr = createTableAlias(cld, attrPath);
  +                    join = new Join(prev, prevKeys, curr, keys, useOuterJoins, attr);
  +                    prev.addJoin(join);
  +                }
  +
  +                prev = curr;
  +            }
  +        }
   
  -		if (cld.isAbstract())
  -		{
  -			getLogger().warn("Creating TableAlias for abstract ClassDescriptor: " + cld.getClassNameOfObject());
  -		}	
  -		
  -		alias = new TableAlias(cld, "A" + m_pathToAlias.size());
  -		m_pathToAlias.put(path, alias);
  -			
  -		return alias;
  -	}
  -
  -	/**
  -	 * Create new TableAlias for path
  -	 * @param table the table name
  -	 * @param path the path from the target table of the query to this TableAlias.
  -	 */
  -	private TableAlias createTableAlias(String table, String path)
  -	{
  -		TableAlias alias;
  -
  -		alias = new TableAlias(table, "A" + m_pathToAlias.size());
  -		m_pathToAlias.put(path, alias);
  -		
  -		return alias;
  -	}
  +        return curr;
  +    }
  +
  +    /**
  +     * Create new TableAlias for path
  +     * @param cld the class descriptor for the TableAlias
  +     * @param path the path from the target table of the query to this TableAlias.
  +     */
  +    private TableAlias createTableAlias(ClassDescriptor cld, String path)
  +    {
  +        TableAlias alias;
  +
  +        if (cld.isAbstract())
  +        {
  +            getLogger().warn("Creating TableAlias for abstract ClassDescriptor: " + cld.getClassNameOfObject());
  +        }
  +
  +        alias = new TableAlias(cld, "A" + m_pathToAlias.size());
  +        m_pathToAlias.put(path, alias);
  +
  +        return alias;
  +    }
  +
  +    /**
  +     * Create new TableAlias for path
  +     * @param table the table name
  +     * @param path the path from the target table of the query to this TableAlias.
  +     */
  +    private TableAlias createTableAlias(String table, String path)
  +    {
  +        TableAlias alias;
  +
  +        alias = new TableAlias(table, "A" + m_pathToAlias.size());
  +        m_pathToAlias.put(path, alias);
  +
  +        return alias;
  +    }
   
       /**
        * Answer the TableAlias for aPath
  @@ -656,37 +670,37 @@
        */
       private TableAlias getTableAlias(String aPath)
       {
  -    	return (TableAlias)m_pathToAlias.get(aPath);
  +        return (TableAlias) m_pathToAlias.get(aPath);
       }
   
  -	/**
  -	 * answer the ClassDescriptor for itemClass for an ObjectReferenceDescriptor
  -	 * check optional hint;
  -	 */
  -	private ClassDescriptor getItemClassDescriptor(ObjectReferenceDescriptor ord, String attr)
  -	{
  -		Class itemClass = null;
  -		Query q = getQuery();
  -
  -		// BRJ look for hint
  -		if (q instanceof QueryByCriteria)
  -		{
  -			itemClass = ((QueryByCriteria) q).getClassForPath(attr);
  -		}
  +    /**
  +     * answer the ClassDescriptor for itemClass for an ObjectReferenceDescriptor
  +     * check optional hint;
  +     */
  +    private ClassDescriptor getItemClassDescriptor(ObjectReferenceDescriptor ord, String attr)
  +    {
  +        Class itemClass = null;
  +        Query q = getQuery();
   
  -		if (itemClass == null)
  -		{
  -			itemClass = ord.getItemClass();
  -		}
  -		return ord.getClassDescriptor().getRepository().getDescriptorFor(itemClass);
  -	}
  +        // BRJ look for hint
  +        if (q instanceof QueryByCriteria)
  +        {
  +            itemClass = ((QueryByCriteria) q).getClassForPath(attr);
  +        }
  +
  +        if (itemClass == null)
  +        {
  +            itemClass = ord.getItemClass();
  +        }
  +        return ord.getClassDescriptor().getRepository().getDescriptorFor(itemClass);
  +    }
   
  -	/**
  -	 * Appends to the statement the ORDER BY clause for the Query
  -	 */
  -	protected void appendOrderByClause(List orderByFields, int[] orderByColumnNumbers, StringBuffer buf)
  -	{
  -		FieldHelper cf;
  +    /**
  +     * Appends to the statement the ORDER BY clause for the Query
  +     */
  +    protected void appendOrderByClause(List orderByFields, int[] orderByColumnNumbers, StringBuffer buf)
  +    {
  +        FieldHelper cf;
   
   		if (orderByColumnNumbers == null)
   		{
  @@ -707,14 +721,14 @@
   				buf.append(" DESC");
   			}
   		}
  -	}
  +    }
   
  -	/**
  -	 * Appends to the statement the GROUP BY clause for the Query
  -	 */
  -	protected void appendGroupByClause(List groupByFields, StringBuffer buf)
  -	{
  -		FieldHelper cf;
  +    /**
  +     * Appends to the statement the GROUP BY clause for the Query
  +     */
  +    protected void appendGroupByClause(List groupByFields, StringBuffer buf)
  +    {
  +        FieldHelper cf;
   
   		if (groupByFields == null || groupByFields.size() == 0)
   		{
  @@ -731,136 +745,136 @@
   			}
   			appendColName(cf.name, true, buf);
   		}
  -	}
  -	
  -	/**
  -	 * Appends to the statement table and all tables joined to it.
  -	 * @param table the table
  -	 * @param where append conditions for WHERE clause here
  -	 */
  -	protected void appendTableWithJoins(TableAlias alias, StringBuffer where, StringBuffer buf)
  -	{
  -	    int stmtFromPos = 0;  
  -        byte joinSyntax = getJoinSyntaxType();
  -
  -		if (joinSyntax == SQL92_JOIN_SYNTAX)
  -		{
  -			stmtFromPos = buf.length();  // store position of join (by: Terry Dexter)
  -		}
  -
  -		if (!(joinSyntax == SQL92_NOPAREN_JOIN_SYNTAX && alias != getRoot()))
  -		{
  -			buf.append(alias.table);
  -			buf.append(" ");
  -			buf.append(alias.alias);
  +    }
   
  -			if (getQuery() instanceof MtoNQuery)
  -			{
  -				buf.append(",");
  -				buf.append(((MtoNQuery) getQuery()).getIndirectionTable());
  -			}
  -		}
  +    /**
  +     * Appends to the statement table and all tables joined to it.
  +     * @param table the table
  +     * @param where append conditions for WHERE clause here
  +     */
  +    protected void appendTableWithJoins(TableAlias alias, StringBuffer where, StringBuffer buf)
  +    {
  +        int stmtFromPos = 0;
  +        byte joinSyntax = getJoinSyntaxType();
   
  -		if (!alias.hasJoins())
  -		{
  -			return;
  -		}
  +        if (joinSyntax == SQL92_JOIN_SYNTAX)
  +        {
  +            stmtFromPos = buf.length();  // store position of join (by: Terry Dexter)
  +        }
  +
  +        if (!(joinSyntax == SQL92_NOPAREN_JOIN_SYNTAX && alias != getRoot()))
  +        {
  +            buf.append(alias.table);
  +            buf.append(" ");
  +            buf.append(alias.alias);
  +
  +            if (getQuery() instanceof MtoNQuery)
  +            {
  +                buf.append(",");
  +                buf.append(((MtoNQuery) getQuery()).getIndirectionTable());
  +            }
  +        }
  +
  +        if (!alias.hasJoins())
  +        {
  +            return;
  +        }
  +
  +        for (Iterator it = alias.iterateJoins(); it.hasNext();)
  +        {
  +            Join join = (Join) it.next();
  +
  +            if (joinSyntax == SQL92_JOIN_SYNTAX)
  +            {
  +                appendJoinSQL92(join, where, buf);
  +                if (it.hasNext())
  +                {
  +                    buf.insert(stmtFromPos, "(");
  +                    buf.append(")");
  +                }
  +            }
  +            else if (joinSyntax == SQL92_NOPAREN_JOIN_SYNTAX)
  +            {
  +                appendJoinSQL92NoParen(join, where, buf);
  +            }
  +            else
  +            {
  +                appendJoin(where, buf, join);
  +            }
   
  -		for (Iterator it = alias.iterateJoins(); it.hasNext();)
  -		{
  -			Join join = (Join) it.next();
  -            
  -			if (joinSyntax == SQL92_JOIN_SYNTAX)
  -			{
  -				appendJoinSQL92(join, where, buf);
  -				if (it.hasNext())
  -				{
  -					buf.insert(stmtFromPos, "(");
  -					buf.append(")");
  -				}
  -			}
  -			else if (joinSyntax == SQL92_NOPAREN_JOIN_SYNTAX)
  -			{
  -				appendJoinSQL92NoParen(join, where, buf);
  -			}
  -			else
  -			{
  -				appendJoin(where, buf, join);
  -			}
  -            
  -		}
  -	}
  +        }
  +    }
   
       /**
        * Append Join for non SQL92 Syntax
  -     */ 
  -	private void appendJoin(StringBuffer where, StringBuffer buf, Join join)
  -	{
  -		buf.append(",");
  -		appendTableWithJoins(join.right, where, buf);
  -		if (where.length() > 0)
  -		{
  -			where.append(" AND ");
  -		}
  -		join.appendJoinEqualities(where);
  -	}
  +     */
  +    private void appendJoin(StringBuffer where, StringBuffer buf, Join join)
  +    {
  +        buf.append(",");
  +        appendTableWithJoins(join.right, where, buf);
  +        if (where.length() > 0)
  +        {
  +            where.append(" AND ");
  +        }
  +        join.appendJoinEqualities(where);
  +    }
   
       /**
        * Append Join for SQL92 Syntax
  -     */ 
  -	private void appendJoinSQL92(Join join, StringBuffer where, StringBuffer buf)
  -	{
  -		if (join.isOuter)
  -		{
  -			buf.append(" LEFT OUTER JOIN ");
  -		}
  -		else
  -		{
  -			buf.append(" INNER JOIN ");
  -		}
  -		if (join.right.hasJoins())
  -		{
  -			buf.append("(");
  -			appendTableWithJoins(join.right, where, buf);
  -			buf.append(")");
  -		}
  -		else
  -		{
  -			appendTableWithJoins(join.right, where, buf);
  -		}
  -		buf.append(" ON ");
  -		join.appendJoinEqualities(buf);
  -	}
  +     */
  +    private void appendJoinSQL92(Join join, StringBuffer where, StringBuffer buf)
  +    {
  +        if (join.isOuter)
  +        {
  +            buf.append(" LEFT OUTER JOIN ");
  +        }
  +        else
  +        {
  +            buf.append(" INNER JOIN ");
  +        }
  +        if (join.right.hasJoins())
  +        {
  +            buf.append("(");
  +            appendTableWithJoins(join.right, where, buf);
  +            buf.append(")");
  +        }
  +        else
  +        {
  +            appendTableWithJoins(join.right, where, buf);
  +        }
  +        buf.append(" ON ");
  +        join.appendJoinEqualities(buf);
  +    }
   
       /**
        * Append Join for SQL92 Syntax without parentheses
  -     */ 
  -	private void appendJoinSQL92NoParen(Join join, StringBuffer where, StringBuffer buf)
  -	{
  -		if (join.isOuter)
  -		{
  -			buf.append(" LEFT OUTER JOIN ");
  -		}
  -		else
  -		{
  -			buf.append(" INNER JOIN ");
  -		}
  -		buf.append(join.right.table);
  -		buf.append(" ");
  -		buf.append(join.right.alias);
  -		buf.append(" ON ");
  -		join.appendJoinEqualities(buf);
  -		appendTableWithJoins(join.right, where, buf);
  -	}
  -
  -	/**
  -	 * Appends to the statement columns if they are not found among the existingColumns.
  -	 * @param columns the list of columns represented by Criteria.Field to ensure
  -	 * @param existingColumns the list of column names (String) that are already appended
  -	 * @return the array of column numbers (base 1)
  -	 */
  -	protected int[] ensureColumns(List columns, List existingColumns, StringBuffer buf)
  -	{
  +     */
  +    private void appendJoinSQL92NoParen(Join join, StringBuffer where, StringBuffer buf)
  +    {
  +        if (join.isOuter)
  +        {
  +            buf.append(" LEFT OUTER JOIN ");
  +        }
  +        else
  +        {
  +            buf.append(" INNER JOIN ");
  +        }
  +        buf.append(join.right.table);
  +        buf.append(" ");
  +        buf.append(join.right.alias);
  +        buf.append(" ON ");
  +        join.appendJoinEqualities(buf);
  +        appendTableWithJoins(join.right, where, buf);
  +    }
  +
  +    /**
  +     * Appends to the statement columns if they are not found among the existingColumns.
  +     * @param columns the list of columns represented by Criteria.Field to ensure
  +     * @param existingColumns the list of column names (String) that are already appended
  +     * @return the array of column numbers (base 1)
  +     */
  +    protected int[] ensureColumns(List columns, List existingColumns, StringBuffer buf)
  +    {
           FieldHelper cf;
   		int[] columnNumbers = new int[columns.size()];
   
  @@ -878,86 +892,86 @@
   			columnNumbers[i]++; // columns numbers have base 1
   		}
   		return columnNumbers;
  -	}
  +    }
   
  -	/**
  -	* Build the tree of joins for the given criteria
  -	*/
  -	private void buildJoinTree(Criteria crit)
  -	{
  -		Enumeration e = crit.getElements();
  +    /**
  +     * Build the tree of joins for the given criteria
  +     */
  +    private void buildJoinTree(Criteria crit)
  +    {
  +        Enumeration e = crit.getElements();
   
  -		while (e.hasMoreElements())
  -		{
  -			Object o = e.nextElement();
  -			if (o instanceof Criteria)
  -			{
  -				buildJoinTree((Criteria) o);
  -			}
  -			else
  -			{
  -				SelectionCriteria c = (SelectionCriteria) o;
  -				if (c.getAttribute() != null)
  -				{
  -					buildJoinTreeForColumn(c.getAttribute());
  -				}
  -				if (c instanceof FieldCriteria)
  -				{
  -					FieldCriteria cc = (FieldCriteria) c;
  -					buildJoinTreeForColumn((String) cc.getValue());
  -				}
  -				else if (c instanceof ColumnCriteria)
  -				{
  -					ColumnCriteria cc = (ColumnCriteria) c;
  -					buildJoinTreeForColumn((String) cc.getValue());
  -				}
  -			}
  -		} // while
  -	}
  +        while (e.hasMoreElements())
  +        {
  +            Object o = e.nextElement();
  +            if (o instanceof Criteria)
  +            {
  +                buildJoinTree((Criteria) o);
  +            }
  +            else
  +            {
  +                SelectionCriteria c = (SelectionCriteria) o;
  +                if (c.getAttribute() != null)
  +                {
  +                    buildJoinTreeForColumn(c.getAttribute());
  +                }
  +                if (c instanceof FieldCriteria)
  +                {
  +                    FieldCriteria cc = (FieldCriteria) c;
  +                    buildJoinTreeForColumn((String) cc.getValue());
  +                }
  +                else if (c instanceof ColumnCriteria)
  +                {
  +                    ColumnCriteria cc = (ColumnCriteria) c;
  +                    buildJoinTreeForColumn((String) cc.getValue());
  +                }
  +            }
  +        } // while
  +    }
   
  -	/**
  -	 * build the Join-Information for name
  -	 * functions and the last segment are removed
  -	 * ie: avg(accounts.amount) -> accounts 
  -	 */
  -	private void buildJoinTreeForColumn(String name)
  -	{
  -		int sepPos = name.lastIndexOf(".");
  -		String pathName;
  +    /**
  +     * build the Join-Information for name
  +     * functions and the last segment are removed
  +     * ie: avg(accounts.amount) -> accounts
  +     */
  +    private void buildJoinTreeForColumn(String name)
  +    {
  +        int sepPos = name.lastIndexOf(".");
  +        String pathName;
   
  -		if (sepPos >= 0)
  -		{
  -			pathName = ClassDescriptor.cleanPath(name);
  -			getTableAlias(pathName.substring(0, sepPos), false);
  -		}
  +        if (sepPos >= 0)
  +        {
  +            pathName = SqlHelper.cleanPath(name);
  +            getTableAlias(pathName.substring(0, sepPos), false);
  +        }
   
  -	}
  +    }
   
  -	/**
  -	 * First reduce the Criteria to the normal disjunctive form, then
  -	 * calculate the necessary tree of joined tables for each item, then group
  -	 * items with the same tree of joined tables.
  -	 */
  -	protected void splitCriteria()
  -	{
  -		Criteria crit = getQuery().getCriteria();
  -		/*
  +    /**
  +     * First reduce the Criteria to the normal disjunctive form, then
  +     * calculate the necessary tree of joined tables for each item, then group
  +     * items with the same tree of joined tables.
  +     */
  +    protected void splitCriteria()
  +    {
  +        Criteria crit = getQuery().getCriteria();
  +        /*
   		Criteria crit1;
   		Criteria crit2;
   		List dnf;
   		*/
   
  -		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;
  +        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(); )
   		{
  @@ -975,298 +989,290 @@
   		    }
   		}
   		*/
  -	}
  -
  -	/**
  -	 * Gets the query.
  -	 * @return Returns a Query
  -	 */
  -	protected Query getQuery()
  -	{
  -		return m_query;
  -	}
  -
  -	/**
  -	 * Gets the root.
  -	 * @return Returns a TableAlias
  -	 */
  -	protected TableAlias getRoot()
  -	{
  -		return m_root;
  -	}
  -
  -	/**
  -	 * Sets the root.
  -	 * @param root The root to set
  -	 */
  -	protected void setRoot(TableAlias root)
  -	{
  -		this.m_root = root;
  -	}
  -
  -	/**
  -	 * Gets the joinTreeToCriteria.
  -	 * @return Returns a HashMap
  -	 */
  -	protected HashMap getJoinTreeToCriteria()
  -	{
  -		return m_joinTreeToCriteria;
  -	}
  -
  -	/**
  -	 * Gets the columns.
  -	 * @return Returns a String[]
  -	 */
  -	protected String[] getColumns()
  -	{
  -		return m_columns;
  -	}
  +    }
   
       /**
  -     * Returns the joinSyntaxType.
  -     * @return byte
  +     * Gets the query.
  +     * @return Returns a Query
        */
  -    protected byte getJoinSyntaxType()
  +    protected Query getQuery()
       {
  -        return m_joinSyntaxType;
  +        return m_query;
       }
   
  -	/**
  -	 * Returns the logger.
  -	 * @return Logger
  -	 */
  -	protected Logger getLogger()
  -	{
  -		return m_logger;
  -	}    
  -
  -	/**
  -	 * Returns the classDescriptor.
  -	 * @return ClassDescriptor
  -	 */
  -	protected ClassDescriptor getClassDescriptor()
  -	{
  -		return m_classDescriptor;
  -	}    	
  -	// ------------------- Inner classes ------------------------------
  -
  -	/**
  -	 * This class represents one table (possibly with alias) in the SQL query
  -	 */
  -	static class TableAlias
  -	{
  -		final ClassDescriptor cld; // Is null for indirection table of M:N relation
  -		final String table;
  -		final String alias;
  -		private HashSet joins;
  -
  -		TableAlias(String table, String alias)
  -		{
  -			this.cld = null;
  -			this.table = table;
  -			this.alias = alias;
  -		}
  -
  -		TableAlias(ClassDescriptor cld, String alias)
  -		{
  -			this.cld = cld;
  -			this.table = cld.getFullTableName();
  -			this.alias = alias;
  -		}
  -
  -		void addJoin(Join join)
  -		{
  -			if (joins == null)
  -			{
  -				joins = new HashSet();
  -			}
  -			joins.add(join);
  -		}
  -
  -		public Iterator iterateJoins()
  -		{
  -			return joins.iterator();
  -		}
  -
  -		public boolean hasJoins()
  -		{
  -			return (joins != null);
  -		}
  -
  -		public void removeAllJoins()
  -		{
  -			if (joins != null)
  -			{
  -				joins.clear();
  -			}
  -		}
  -
  -		public String toString()
  -		{
  -			StringBuffer sb = new StringBuffer(1024);
  -			boolean first = true;
  -
  -			sb.append(table);
  -			sb.append(" ");
  -			sb.append(alias);
  -			if (joins != null)
  -			{
  -				sb.append(" [");
  -				for (Iterator it = joins.iterator(); it.hasNext();)
  -				{
  -					Join join = (Join) it.next();
  -
  -					if (first)
  -					{
  -						first = false;
  -					}
  -					else
  -					{
  -						sb.append(", ");
  -					}
  -					sb.append("-(");
  -					sb.append(join.name);
  -					sb.append(")->");
  -					sb.append(join.right);
  -				}
  -				sb.append("]");
  -			}
  -			return sb.toString();
  -		}
  -
  -		public boolean equals(Object obj)
  -		{
  -			TableAlias t = (TableAlias) obj;
  -
  -			if (!alias.equals(t.alias))
  -			{
  -				return false;
  -			}
  -
  -			if (joins == null)
  -			{
  -				return (t.joins != null);
  -			}
  +    /**
  +     * Gets the root.
  +     * @return Returns a TableAlias
  +     */
  +    protected TableAlias getRoot()
  +    {
  +        return m_root;
  +    }
   
  -			return joins.equals(t.joins);
  -		}
  +    /**
  +     * Sets the root.
  +     * @param root The root to set
  +     */
  +    protected void setRoot(TableAlias root)
  +    {
  +        this.m_root = root;
  +    }
   
  -		public int hashCode()
  -		{
  -			return alias.hashCode();
  -		}
  +    /**
  +     * Gets the joinTreeToCriteria.
  +     * @return Returns a HashMap
  +     */
  +    protected HashMap getJoinTreeToCriteria()
  +    {
  +        return m_joinTreeToCriteria;
  +    }
   
  -		public Object clone()
  -		{
  -			TableAlias clone;
  +    /**
  +     * Gets the columns.
  +     * @return Returns a String[]
  +     */
  +    protected String[] getColumns()
  +    {
  +        return m_columns;
  +    }
   
  -			if (cld == null)
  -			{
  -				clone = new TableAlias(table, alias);
  -			}
  -			else
  -			{
  -				clone = new TableAlias(cld, alias);
  -			}
  +    /**
  +     * Returns the joinSyntaxType.
  +     * @return byte
  +     */
  +    protected byte getJoinSyntaxType()
  +    {
  +        return pf.getJoinSyntaxType();
  +    }
   
  -			clone.joins = joins;
  -			return clone;
  -		}
  -	}
  +    /**
  +     * Returns the logger.
  +     * @return Logger
  +     */
  +    protected Logger getLogger()
  +    {
  +        return m_logger;
  +    }
   
  -	/**
  -	 * This class represents join between two TableAliases
  -	 */
  -	class Join
  -	{
  -		final TableAlias left;
  -		final String[] leftKeys;
  -		final TableAlias right;
  -		final String[] rightKeys;
  -		final boolean isOuter;
  -		/** This is the name of the field corresponding to this join */
  -		final String name;
  -
  -		/**
  -		 * leftKeys and rightKeys should be either FieldDescriptor[] or String[]
  -		 */
  -		Join(TableAlias left, Object[] leftKeys, TableAlias right, Object[] rightKeys, boolean isOuter, String name)
  -		{
  -			this.left = left;
  -			this.leftKeys = getColumns(leftKeys);
  -			this.right = right;
  -			this.rightKeys = getColumns(rightKeys);
  -			this.isOuter = isOuter;
  -			this.name = name;
  -		}
  +    //-----------------------------------------------------------------
  +    // ------------------- Inner classes ------------------------------
  +    //-----------------------------------------------------------------
   
  -		private String[] getColumns(Object[] keys)
  -		{
  -			String[] columns = new String[keys.length];
  +    /**
  +     * This class represents one table (possibly with alias) in the SQL query
  +     */
  +    static class TableAlias
  +    {
  +        final ClassDescriptor cld; // Is null for indirection table of M:N relation
  +        final String table;
  +        final String alias;
  +        private HashSet joins;
  +
  +        TableAlias(String table, String alias)
  +        {
  +            this.cld = null;
  +            this.table = table;
  +            this.alias = alias;
  +        }
  +
  +        TableAlias(ClassDescriptor cld, String alias)
  +        {
  +            this.cld = cld;
  +            this.table = cld.getFullTableName();
  +            this.alias = alias;
  +        }
  +
  +        void addJoin(Join join)
  +        {
  +            if (joins == null)
  +            {
  +                joins = new HashSet();
  +            }
  +            joins.add(join);
  +        }
  +
  +        public Iterator iterateJoins()
  +        {
  +            return joins.iterator();
  +        }
  +
  +        public boolean hasJoins()
  +        {
  +            return (joins != null);
  +        }
  +
  +        public void removeAllJoins()
  +        {
  +            if (joins != null)
  +            {
  +                joins.clear();
  +            }
  +        }
  +
  +        public String toString()
  +        {
  +            StringBuffer sb = new StringBuffer(1024);
  +            boolean first = true;
  +
  +            sb.append(table);
  +            sb.append(" ");
  +            sb.append(alias);
  +            if (joins != null)
  +            {
  +                sb.append(" [");
  +                for (Iterator it = joins.iterator(); it.hasNext();)
  +                {
  +                    Join join = (Join) it.next();
  +
  +                    if (first)
  +                    {
  +                        first = false;
  +                    }
  +                    else
  +                    {
  +                        sb.append(", ");
  +                    }
  +                    sb.append("-(");
  +                    sb.append(join.name);
  +                    sb.append(")->");
  +                    sb.append(join.right);
  +                }
  +                sb.append("]");
  +            }
  +            return sb.toString();
  +        }
  +
  +        public boolean equals(Object obj)
  +        {
  +            TableAlias t = (TableAlias) obj;
  +
  +            if (!alias.equals(t.alias))
  +            {
  +                return false;
  +            }
  +
  +            if (joins == null)
  +            {
  +                return (t.joins != null);
  +            }
  +
  +            return joins.equals(t.joins);
  +        }
  +
  +        public int hashCode()
  +        {
  +            return alias.hashCode();
  +        }
  +
  +        public Object clone()
  +        {
  +            TableAlias clone;
  +
  +            if (cld == null)
  +            {
  +                clone = new TableAlias(table, alias);
  +            }
  +            else
  +            {
  +                clone = new TableAlias(cld, alias);
  +            }
  +
  +            clone.joins = joins;
  +            return clone;
  +        }
  +    }
   
  -			if (keys instanceof FieldDescriptor[])
  -			{
  -				FieldDescriptor[] kd = (FieldDescriptor[]) keys;
  -				for (int i = 0; i < columns.length; i++)
  -				{
  -					columns[i] = kd[i].getColumnName();
  -				}
  -			}
  -			else
  -			{
  -				for (int i = 0; i < columns.length; i++)
  -				{
  -					columns[i] = keys[i].toString();
  -				}
  -			}
  -			return columns;
  -		}
  +    /**
  +     * This class represents join between two TableAliases
  +     */
  +    class Join
  +    {
  +        final TableAlias left;
  +        final String[] leftKeys;
  +        final TableAlias right;
  +        final String[] rightKeys;
  +        final boolean isOuter;
  +        /** This is the name of the field corresponding to this join */
  +        final String name;
  +
  +        /**
  +         * leftKeys and rightKeys should be either FieldDescriptor[] or String[]
  +         */
  +        Join(TableAlias left, Object[] leftKeys, TableAlias right, Object[] rightKeys, boolean isOuter, String name)
  +        {
  +            this.left = left;
  +            this.leftKeys = getColumns(leftKeys);
  +            this.right = right;
  +            this.rightKeys = getColumns(rightKeys);
  +            this.isOuter = isOuter;
  +            this.name = name;
  +        }
  +
  +        private String[] getColumns(Object[] keys)
  +        {
  +            String[] columns = new String[keys.length];
  +
  +            if (keys instanceof FieldDescriptor[])
  +            {
  +                FieldDescriptor[] kd = (FieldDescriptor[]) keys;
  +                for (int i = 0; i < columns.length; i++)
  +                {
  +                    columns[i] = kd[i].getColumnName();
  +                }
  +            }
  +            else
  +            {
  +                for (int i = 0; i < columns.length; i++)
  +                {
  +                    columns[i] = keys[i].toString();
  +                }
  +            }
  +            return columns;
  +        }
   
  -		void appendJoinEqualities(StringBuffer buf)
  -		{
  +        void appendJoinEqualities(StringBuffer buf)
  +        {
               byte joinSyntax = getJoinSyntaxType();
  -            
  -			for (int i = 0; i < leftKeys.length; i++)
  -			{
  -				if (i > 0)
  -				{
  -					buf.append(" AND ");
  -				}
  -				buf.append(left.alias);
  -				buf.append(".");
  -				buf.append(leftKeys[i]);
  -
  -				if (isOuter && joinSyntax == SYBASE_JOIN_SYNTAX)
  -				{
  -					buf.append("*=");
  -				}
  -				else
  -				{
  -					buf.append("=");
  -				}
  -
  -				buf.append(right.alias);
  -				buf.append(".");
  -				buf.append(rightKeys[i]);
  -
  -				if (isOuter && joinSyntax == ORACLE_JOIN_SYNTAX)
  -				{
  -					buf.append("(+)");
  -				}
  -			}
  -		}
  -
  -		public boolean equals(Object obj)
  -		{
  -			Join j = (Join) obj;
  -			return name.equals(j.name) && (isOuter == j.isOuter) && right.equals(j.right);
  -		}
  -
  -		public int hashCode()
  -		{
  -			return name.hashCode();
  -		}
  -	}
  -
   
  +            for (int i = 0; i < leftKeys.length; i++)
  +            {
  +                if (i > 0)
  +                {
  +                    buf.append(" AND ");
  +                }
  +                buf.append(left.alias);
  +                buf.append(".");
  +                buf.append(leftKeys[i]);
  +
  +                if (isOuter && joinSyntax == SYBASE_JOIN_SYNTAX)
  +                {
  +                    buf.append("*=");
  +                }
  +                else
  +                {
  +                    buf.append("=");
  +                }
  +
  +                buf.append(right.alias);
  +                buf.append(".");
  +                buf.append(rightKeys[i]);
  +
  +                if (isOuter && joinSyntax == ORACLE_JOIN_SYNTAX)
  +                {
  +                    buf.append("(+)");
  +                }
  +            }
  +        }
  +
  +        public boolean equals(Object obj)
  +        {
  +            Join j = (Join) obj;
  +            return name.equals(j.name) && (isOuter == j.isOuter) && right.equals(j.right);
  +        }
  +
  +        public int hashCode()
  +        {
  +            return name.hashCode();
  +        }
  +    }
   }
  
  
  
  1.8       +10 -9     jakarta-ojb/src/java/org/apache/ojb/broker/accesslayer/sql/SqlSelectStatement.java
  
  Index: SqlSelectStatement.java
  ===================================================================
  RCS file: /home/cvs/jakarta-ojb/src/java/org/apache/ojb/broker/accesslayer/sql/SqlSelectStatement.java,v
  retrieving revision 1.7
  retrieving revision 1.8
  diff -u -r1.7 -r1.8
  --- SqlSelectStatement.java	27 Nov 2002 05:48:15 -0000	1.7
  +++ SqlSelectStatement.java	24 Dec 2002 12:57:51 -0000	1.8
  @@ -64,25 +64,26 @@
   import org.apache.ojb.broker.query.Criteria;
   import org.apache.ojb.broker.query.Query;
   import org.apache.ojb.broker.util.logging.Logger;
  +import org.apache.ojb.broker.platforms.Platform;
   
   /**
    * Model a SELECT Statement
  - * 
  + *
    * @author <a href="mailto:jbraeuchi@hotmail.com">Jakob Braeuchi</a>
    * @version $Id$
    */
   public class SqlSelectStatement extends SqlQueryStatement
   {
  -	
  +
   	/**
   	 * Constructor for SqlSelectStatement.
   	 * @param cld
   	 * @param columns
   	 * @param query
   	 */
  -	public SqlSelectStatement(ClassDescriptor cld, String[] columns, Query query, Logger logger)
  +	public SqlSelectStatement(Platform pf, ClassDescriptor cld, String[] columns, Query query, Logger logger)
   	{
  -		super(cld, columns, query, logger);
  +		super(pf, cld, columns, query, logger);
   	}
   
       /**
  @@ -148,7 +149,7 @@
           return columnList;
       }
   
  -	
  +
   	/**
        * Answer the SELECT-Sql for the Statement
        */
  @@ -161,7 +162,7 @@
           /** the column numbers used in ORDER BY clause  */
           int[] orderByColumnNumbers = null;
           String[] columns = getColumns();
  -        
  +
           for (Iterator it = getJoinTreeToCriteria().entrySet().iterator(); it.hasNext();)
           {
               Map.Entry entry = (Map.Entry) it.next();
  @@ -223,7 +224,7 @@
                   }
               }
   
  -                /**
  +            /**
                * treeder
                * going to map superclass tables here, not sure about the columns, just using all columns for now
                *
  @@ -239,7 +240,7 @@
               }
   
               stmt.append(" FROM ");
  -            
  +
               appendTableWithJoins(getRoot(), where, stmt);
               if(cld.getSuperClass() != null){
                   appendSuperClassJoin(cld, cldSuper, stmt, where);
  
  
  

Mime
View raw message