You are viewing a plain text version of this content. The canonical link for it is here.
Posted to ojb-dev@db.apache.org by br...@apache.org on 2003/12/30 20:15:32 UTC

cvs commit: db-ojb/src/java/org/apache/ojb/broker/accesslayer ReportQueryRsIterator.java

brj         2003/12/30 11:15:32

  Modified:    src/test/org/apache/ojb/broker PBRollbackTest.java
                        QueryTest.java
               src/java/org/apache/ojb/broker/accesslayer/sql
                        SqlQueryStatement.java
               src/java/org/apache/ojb/broker/accesslayer
                        ReportQueryRsIterator.java
  Log:
  - fixed problem with additional columns in ReposrtQueries. 
  - columns added by ensureColumn use a column alias
  - additional testcases for added columns
  
  Revision  Changes    Path
  1.7       +1 -0      db-ojb/src/test/org/apache/ojb/broker/PBRollbackTest.java
  
  Index: PBRollbackTest.java
  ===================================================================
  RCS file: /home/cvs/db-ojb/src/test/org/apache/ojb/broker/PBRollbackTest.java,v
  retrieving revision 1.6
  retrieving revision 1.7
  diff -u -r1.6 -r1.7
  --- PBRollbackTest.java	3 Sep 2003 18:41:09 -0000	1.6
  +++ PBRollbackTest.java	30 Dec 2003 19:15:31 -0000	1.7
  @@ -311,6 +311,7 @@
               badProject.setTitle("Bad project!");
               badProject.setPersons(projects);
   
  +            System.err.println("!! The follwing SQLException is part of the Test !!");
               storeObjects(broker, projects);
   
               fail("Test should throw a exception in place");
  
  
  
  1.47      +45 -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.46
  retrieving revision 1.47
  diff -u -r1.46 -r1.47
  --- QueryTest.java	13 Dec 2003 20:47:10 -0000	1.46
  +++ QueryTest.java	30 Dec 2003 19:15:31 -0000	1.47
  @@ -438,6 +438,29 @@
       }
   
       /**
  +     * ReportQuery returning rows with some "Liquor" data ordered by productGroup.groupId
  +     */
  +    public void testReportQueryOrderByNonSelectColumn()
  +    {
  +
  +        Criteria crit = new Criteria();
  +        Collection results = new Vector();
  +        crit.addEqualTo("productGroup.groupName", "Liquors");
  +        ReportQueryByCriteria q = QueryFactory.newReportQuery(Article.class, crit);
  +        q.setColumns(new String[] { "articleId", "articleName", "price" });
  +        q.addOrderByAscending("productGroup.groupId");
  +
  +        Iterator iter = broker.getReportQueryIteratorByQuery(q);
  +        assertNotNull(iter);
  +        while (iter.hasNext())
  +        {
  +            results.add(iter.next());
  +        }
  +        assertTrue(results.size() > 0);
  +
  +    }
  +    
  +    /**
   	 * ReportQuery returning rows with summed stock and price per article group
   	 */
       public void testReportQueryGroupBy()
  @@ -792,6 +815,28 @@
   
       }
   
  +    /**
  +     * ReportQuery returning rows with some "Liquor" data ordered by productGroup.groupId
  +     */
  +    public void testQueryOrderByNonSelectColumn()
  +    {
  +
  +        Criteria crit = new Criteria();
  +        Collection results = new Vector();
  +        crit.addEqualTo("productGroup.groupName", "Liquors");
  +        QueryByCriteria q = QueryFactory.newQuery(Article.class, crit);
  +        q.addOrderByAscending("productGroup.groupId");
  +
  +        Iterator iter = broker.getIteratorByQuery(q);
  +        assertNotNull(iter);
  +        while (iter.hasNext())
  +        {
  +            results.add(iter.next());
  +        }
  +        assertTrue(results.size() > 0);
  +
  +    }
  +    
       /**
   	 * test PathExpression pointing to abstract class (InterfaceArticle)
   	 */
  
  
  
  1.61      +61 -21    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.60
  retrieving revision 1.61
  diff -u -r1.60 -r1.61
  --- SqlQueryStatement.java	12 Dec 2003 19:36:48 -0000	1.60
  +++ SqlQueryStatement.java	30 Dec 2003 19:15:31 -0000	1.61
  @@ -307,7 +307,7 @@
        *
        * @param aTableAlias
        * @param aPathInfo
  -     * @param translate
  +     * @param translate, flag to indicate translation of pathInfo 
        * @param buf
        * @return true if appended
        */
  @@ -435,12 +435,18 @@
       }
   
       /**
  -     * Answer the appropriate ColumnName <br>
  +     * Append the appropriate ColumnName to the buffer<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
  +     * 
  +     * @param attr
  +     * @param useOuterJoins
  +     * @param aUserAlias
  +     * @param buf
  +     * @return
        */
       protected boolean appendColName(String attr, boolean useOuterJoins, String aUserAlias, StringBuffer buf)
       {
  @@ -449,8 +455,41 @@
   
           return appendColName(tableAlias, attrInfo.pathInfo, true, buf);
       }
  -
  -
  +  
  +    /**
  +     * Append the appropriate ColumnName to the buffer<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
  +     * 
  +     * @param attr
  +     * @param attrAlias, column alias 
  +     * @param useOuterJoins
  +     * @param aUserAlias
  +     * @param buf
  +     * @return
  +     */
  +    protected boolean appendColName(String attr, String attrAlias, boolean useOuterJoins, String aUserAlias, StringBuffer buf)
  +    {
  +        AttributeInfo attrInfo = getAttributeInfo(attr, useOuterJoins, aUserAlias);
  +        TableAlias tableAlias = attrInfo.tableAlias;
  +        PathInfo pi = attrInfo.pathInfo;   
  +        
  +        if (pi.suffix != null)
  +        {
  +            pi.suffix = pi.suffix + " as " + attrAlias;
  +        }
  +        else
  +        {
  +            pi.suffix = " as " + attrAlias;
  +        }
  +        
  +        
  +        return appendColName(tableAlias, pi, true, buf);
  +    }
  +    
       /**
        * appends a WHERE-clause to the Statement
        * @param where
  @@ -1446,6 +1485,7 @@
   
       /**
        * Appends to the statement columns if they are not found among the existingColumns.
  +     * Columns added here use a column-alias "ojb_col_x", x being the number of existing columns
        * @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)
  @@ -1453,24 +1493,24 @@
       protected int[] ensureColumns(List columns, List existingColumns, StringBuffer buf)
       {
           FieldHelper cf;
  -		int[] columnNumbers = new int[columns.size()];
  +        int[] columnNumbers = new int[columns.size()];
   
  -		for (int i = 0; i < columnNumbers.length; i++)
  -		{
  -			cf = (FieldHelper) columns.get(i);
  -			columnNumbers[i] = existingColumns.indexOf(cf.name);
  -			if (columnNumbers[i] == -1)
  -			{
  -				buf.append(",");
  -				appendColName(cf.name, false, null, buf);
  -				columnNumbers[i] = existingColumns.size();
  -				existingColumns.add(cf.name);
  -			}
  -			columnNumbers[i]++; // columns numbers have base 1
  -		}
  -		return columnNumbers;
  +        for (int i = 0; i < columnNumbers.length; i++)
  +        {
  +            cf = (FieldHelper) columns.get(i);
  +            columnNumbers[i] = existingColumns.indexOf(cf.name);
  +            if (columnNumbers[i] == -1)
  +            {
  +                columnNumbers[i] = existingColumns.size();
  +                existingColumns.add(cf.name);
  +                buf.append(",");
  +                appendColName(cf.name, "ojb_col_" + columnNumbers[i], false, null, buf);
  +            }
  +            columnNumbers[i]++; // columns numbers have base 1
  +        }
  +        return columnNumbers;
       }
  -
  +    
       /**
        * Build the tree of joins for the given criteria
        */
  
  
  
  1.9       +7 -2      db-ojb/src/java/org/apache/ojb/broker/accesslayer/ReportQueryRsIterator.java
  
  Index: ReportQueryRsIterator.java
  ===================================================================
  RCS file: /home/cvs/db-ojb/src/java/org/apache/ojb/broker/accesslayer/ReportQueryRsIterator.java,v
  retrieving revision 1.8
  retrieving revision 1.9
  diff -u -r1.8 -r1.9
  --- ReportQueryRsIterator.java	9 Dec 2003 17:19:59 -0000	1.8
  +++ ReportQueryRsIterator.java	30 Dec 2003 19:15:32 -0000	1.9
  @@ -59,6 +59,7 @@
   
   import org.apache.ojb.broker.PersistenceBrokerException;
   import org.apache.ojb.broker.core.PersistenceBrokerImpl;
  +import org.apache.ojb.broker.query.ReportQuery;
   import org.apache.ojb.broker.util.JdbcTypesHelper;
   
   /**
  @@ -82,7 +83,11 @@
           try
           {
               rsMetaData = getRsAndStmt().m_rs.getMetaData();
  -            columnCount = rsMetaData.getColumnCount();
  +            
  +            // BRJ: use only explicit columns ! 
  +            // ignore those columns automatically added for ordering or grouping 
  +            ReportQuery q = (ReportQuery)queryObject.getQuery();
  +            columnCount = q.getColumns().length;
           }
           catch (SQLException e)
           {
  
  
  

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