db-ojb-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From b..@apache.org
Subject cvs commit: db-ojb/src/test/org/apache/ojb/broker QueryTest.java
Date Fri, 15 Aug 2003 14:13:07 GMT
brj         2003/08/15 07:13:07

  Modified:    src/java/org/apache/ojb/broker/accesslayer/sql
                        SqlQueryStatement.java SqlSelectStatement.java
               src/test/org/apache/ojb/broker QueryTest.java
  Log:
  subqueries referencing attribute of enclosing query
  
  Revision  Changes    Path
  1.49      +59 -20    db-ojb/src/java/org/apache/ojb/broker/accesslayer/sql/SqlQueryStatement.java
  
  Index: SqlQueryStatement.java
  ===================================================================
  RCS file: /home/cvs/db-ojb/src/java/org/apache/ojb/broker/accesslayer/sql/SqlQueryStatement.java,v
  retrieving revision 1.48
  retrieving revision 1.49
  diff -u -r1.48 -r1.49
  --- SqlQueryStatement.java	2 Aug 2003 09:24:00 -0000	1.48
  +++ SqlQueryStatement.java	15 Aug 2003 14:13:07 -0000	1.49
  @@ -80,11 +80,12 @@
   /**
    * Model a Statement based on Query.
    *
  - * @author <a href="mailto:jbraeuchi@hotmail.com">Jakob Braeuchi</a>
  + * @author <a href="mailto:jbraeuchi@gmx.ch">Jakob Braeuchi</a>
    * @version $Id$
    */
   public abstract class SqlQueryStatement implements SqlStatement, JoinSyntaxTypes
   {
  +    private SqlQueryStatement m_parentStatement;
       /** the logger */
       Logger m_logger = LoggerFactory.getLogger(SqlQueryStatement.class);
       /** the target table of the query */
  @@ -110,12 +111,20 @@
        */
       public SqlQueryStatement(Platform pf, ClassDescriptor cld, String[] columns, Query
query, Logger logger)
       {
  +        this (null, pf, cld, columns, query, logger);
  +    }
  +
  +    /**
  +     * Constructor for SqlCriteriaStatement.
  +     */
  +    public SqlQueryStatement(SqlQueryStatement parent, Platform pf, ClassDescriptor cld,
String[] columns, Query query, Logger logger)
  +    {
  +        this.m_parentStatement = parent;
           this.m_query = (QueryByCriteria) query;
           this.m_columns = columns;
           this.pf = pf;
           this.m_searchCld = cld;
  -        if ((m_query == null) 
  -                || (m_query.getBaseClass() == m_query.getSearchClass()))
  +        if ((m_query == null) || (m_query.getBaseClass() == m_query.getSearchClass()))
           {
               this.m_baseCld = m_searchCld;
           }
  @@ -123,15 +132,14 @@
           {
               this.m_baseCld = cld.getRepository().getDescriptorFor(query.getBaseClass());
           }
  -        this.m_root = createTableAlias(m_baseCld, null,"");
  +        this.m_root = createTableAlias(m_baseCld, null, "");
           if (m_searchCld == m_baseCld)
           {
               this.m_search = m_root;
           }
           else
           {
  -            this.m_search = getTableAlias(m_query.getObjectProjectionAttribute(),
  -                                          false, null, null);
  +            this.m_search = getTableAlias(m_query.getObjectProjectionAttribute(), false,
null, null);
           }
   
           // In some cases it is necessary to split the query criteria
  @@ -166,19 +174,18 @@
       protected AttributeInfo getAttributeInfo(String attr, boolean useOuterJoins, String
aUserAlias)
       {
           AttributeInfo result = new AttributeInfo();
  -        TableAlias tableAlias;
  +        TableAlias tableAlias = null;
           SqlHelper.PathInfo pathInfo = SqlHelper.splitPath(attr);
           String colName = pathInfo.column;
           int sp;
   
  -
           sp = colName.lastIndexOf(".");
   
           if (sp == -1)
           {
  -            // BRJ : ignore alias for simple attributes ??
               tableAlias = getRoot();
  -/*
  +            // BRJ : ignore alias for simple attributes ??
  +            /*
               if (aUserAlias == null)
               {
                   tableAlias = getRoot();
  @@ -191,16 +198,20 @@
                       tableAlias = getRoot();
                   }
               }
  -*/
  +            */
           }
           else
           {
               String pathName = colName.substring(0, sp);
  -            String[] fieldNameRef = {colName.substring(sp + 1)};
  +            String[] fieldNameRef = { colName.substring(sp + 1)};
   
  -            tableAlias = getTableAlias(pathName, useOuterJoins, aUserAlias,
  -                                       fieldNameRef);
  +            // check if we refer to an attribute in the parent query 
  +            if (pathName.equals("super") && m_parentStatement != null)
  +            {
  +                return m_parentStatement.getAttributeInfo(fieldNameRef[0], useOuterJoins,
aUserAlias);
  +            }
   
  +            tableAlias = getTableAlias(pathName, useOuterJoins, aUserAlias, fieldNameRef);
               /**
                * if we have not found an alias by the pathName or
                * aliasName (if given), try again because pathName
  @@ -208,7 +219,7 @@
                * a path, which means there may be no path separators (,)
                * in the pathName.
                */
  -            if ((tableAlias==null) && (colName.lastIndexOf(".")==-1))
  +            if ((tableAlias == null) && (colName.lastIndexOf(".") == -1))
               {
                   /**
                    * pathName might be an alias, so check this first
  @@ -740,10 +751,24 @@
       private void appendSubQuery(Query subQuery, StringBuffer buf)
       {
           ClassDescriptor cld = getRoot().cld.getRepository().getDescriptorFor(subQuery.getSearchClass());
  -        String subSql = SqlGeneratorFactory.getInstance().createSqlGenerator(pf).getPreparedSelectStatement(subQuery,
cld);
  +        String sql;
   
  +        if (subQuery instanceof QueryBySQL)
  +        {
  +            sql = ((QueryBySQL) subQuery).getSql();
  +        }
  +        else
  +        {
  +            String[] columns = null;
  +            if (subQuery instanceof ReportQuery)
  +            {
  +                columns = ((ReportQuery) subQuery).getColumns();
  +            }
  +            sql = new SqlSelectStatement(this ,pf, cld, columns, subQuery, m_logger).getStatement();
  +        }
  +        
           buf.append(" (");
  -        buf.append(subSql);
  +        buf.append(sql);
           buf.append(") ");
       }
   
  @@ -944,6 +969,18 @@
           
       }  
       
  +    private char getAliasChar()
  +    {
  +        char result = 'A';
  +        
  +        if (m_parentStatement != null)
  +        {
  +            result = (char)(m_parentStatement.getAliasChar() + 1);
  +        }
  +        
  +        return result;
  +    }
  +    
       /**
        * Create a TableAlias for path or userAlias
        * @param aCld
  @@ -982,7 +1019,8 @@
               lookForExtents = true;
           } 
           
  -        alias = new TableAlias(cld, "A" + m_pathToAlias.size(), lookForExtents,hints);
  +        String aliasName = String.valueOf(getAliasChar()) + m_pathToAlias.size();
  +        alias = new TableAlias(cld, aliasName, lookForExtents,hints);
           m_pathToAlias.put(path, alias);
           return alias;
       }
  @@ -1021,7 +1059,8 @@
               getLogger().warn("Creating TableAlias without table for path: " + path);
           }
   
  -        alias = new TableAlias(table, "A" + m_pathToAlias.size());
  +        String aliasName = String.valueOf(getAliasChar()) + m_pathToAlias.size();
  +        alias = new TableAlias(table, aliasName);
           m_pathToAlias.put(path, alias);
   
           return alias;
  
  
  
  1.15      +25 -7     db-ojb/src/java/org/apache/ojb/broker/accesslayer/sql/SqlSelectStatement.java
  
  Index: SqlSelectStatement.java
  ===================================================================
  RCS file: /home/cvs/db-ojb/src/java/org/apache/ojb/broker/accesslayer/sql/SqlSelectStatement.java,v
  retrieving revision 1.14
  retrieving revision 1.15
  diff -u -r1.14 -r1.15
  --- SqlSelectStatement.java	2 Aug 2003 09:24:00 -0000	1.14
  +++ SqlSelectStatement.java	15 Aug 2003 14:13:07 -0000	1.15
  @@ -75,16 +75,34 @@
   public class SqlSelectStatement extends SqlQueryStatement
   {
   
  -	/**
  -	 * Constructor for SqlSelectStatement.
  -	 * @param cld
  -	 * @param columns
  -	 * @param query
  -	 */
  +    /**
  +     * Constructor for SqlSelectStatement.
  +     * 
  +     * @param pf
  +     * @param cld
  +     * @param columns
  +     * @param query
  +     * @param logger
  +     */
   	public SqlSelectStatement(Platform pf, ClassDescriptor cld, String[] columns, Query query,
Logger logger)
   	{
   		super(pf, cld, columns, query, logger);
   	}
  +
  +    /**
  +     * Constructor for SqlSelectStatement.
  +     *
  +     * @param parent
  +     * @param pf
  +     * @param cld
  +     * @param columns
  +     * @param query
  +     * @param logger
  +     */
  +    public SqlSelectStatement(SqlQueryStatement parent, Platform pf, ClassDescriptor cld,
String[] columns, Query query, Logger logger)
  +    {
  +        super(parent, pf, cld, columns, query, logger);
  +    }
   
       /**
        * Appends to the statement a comma separated list of column names.
  
  
  
  1.34      +25 -0     db-ojb/src/test/org/apache/ojb/broker/QueryTest.java
  
  Index: QueryTest.java
  ===================================================================
  RCS file: /home/cvs/db-ojb/src/test/org/apache/ojb/broker/QueryTest.java,v
  retrieving revision 1.33
  retrieving revision 1.34
  diff -u -r1.33 -r1.34
  --- QueryTest.java	23 May 2003 16:45:40 -0000	1.33
  +++ QueryTest.java	15 Aug 2003 14:13:07 -0000	1.34
  @@ -146,6 +146,31 @@
   
   	}
   
  +    /**
  +     * test Subquery
  +     * get all product groups with more than 10 articles
  +     *
  +     * test may fail if db does not support sub queries
  +     */
  +    public void testSubQuery3()
  +    {
  +
  +        ReportQueryByCriteria subQuery;
  +        Criteria subCrit = new Criteria();
  +        Criteria crit = new Criteria();
  +
  +        subCrit.addEqualToField("productGroupId","super.groupId");
  +        subQuery = QueryFactory.newReportQuery(Article.class, subCrit);
  +        subQuery.setColumns(new String[]{"count(productGroupId)"});
  +
  +        crit.addLessThan("10", subQuery);    // MORE than 10 articles
  +        Query q = QueryFactory.newQuery(ProductGroup.class, crit);
  +
  +        Collection results = broker.getCollectionByQuery(q);
  +        assertNotNull(results);
  +        assertTrue(results.size() == 4);
  +
  +    }
   	/**
   	 * test Like Criteria
   	 */
  
  
  

---------------------------------------------------------------------
To unsubscribe, e-mail: ojb-dev-unsubscribe@db.apache.org
For additional commands, e-mail: ojb-dev-help@db.apache.org


Mime
View raw message