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 to...@apache.org on 2005/12/18 17:43:19 UTC

cvs commit: db-ojb/src/java/org/apache/ojb/broker/platforms PlatformDefaultImpl.java PlatformDerbyImpl.java Platform.java

tomdz       2005/12/18 08:43:19

  Modified:    src/java/org/apache/ojb/broker/util Tag: OJB_1_0_RELEASE
                        BrokerHelper.java
               src/java/org/apache/ojb/broker/platforms Tag:
                        OJB_1_0_RELEASE PlatformDefaultImpl.java
                        PlatformDerbyImpl.java Platform.java
  Log:
  Added first bits of support for COUNT DISTINCT workaround for databases that don't support it for multiple columns
  
  Revision  Changes    Path
  No                   revision
  No                   revision
  1.57.2.22 +50 -27    db-ojb/src/java/org/apache/ojb/broker/util/BrokerHelper.java
  
  Index: BrokerHelper.java
  ===================================================================
  RCS file: /home/cvs/db-ojb/src/java/org/apache/ojb/broker/util/BrokerHelper.java,v
  retrieving revision 1.57.2.21
  retrieving revision 1.57.2.22
  diff -u -r1.57.2.21 -r1.57.2.22
  --- BrokerHelper.java	13 Dec 2005 18:21:23 -0000	1.57.2.21
  +++ BrokerHelper.java	18 Dec 2005 16:43:19 -0000	1.57.2.22
  @@ -542,23 +542,25 @@
        */
       private Query getQueryByCriteriaCount(QueryByCriteria aQuery)
       {
  -        Class searchClass = aQuery.getSearchClass();
  -        ReportQueryByCriteria countQuery;
  -        Criteria countCrit = null;
  -        String[] columns = new String[1];
  +        Class                 searchClass = aQuery.getSearchClass();
  +        ReportQueryByCriteria countQuery  = null;
  +        Criteria              countCrit   = null;
  +        String[]              columns     = new String[1];
   
           // BRJ: copied Criteria without groupby, orderby, and prefetched relationships
           if (aQuery.getCriteria() != null)
           {
  -            countCrit = aQuery.getCriteria().copy(false,false,false);
  +            countCrit = aQuery.getCriteria().copy(false, false, false);
           }
   
  -        if(aQuery.isDistinct())
  +        if (aQuery.isDistinct())
           {
  -            //
               // BRJ: Count distinct is dbms dependent
               // hsql/sapdb: select count (distinct(person_id || project_id)) from person_project
               // mysql: select count (distinct person_id,project_id) from person_project
  +            // [tomdz]
  +            // Some databases have no support for multi-column count distinct (e.g. Derby)
  +            // Here we use a SELECT count(*) FROM (SELECT DISTINCT ...) instead 
               //
               // concatenation of pk-columns is a simple way to obtain a single column
               // but concatenation is also dbms dependent:
  @@ -566,26 +568,37 @@
               // SELECT count(distinct concat(row1, row2, row3)) mysql
               // SELECT count(distinct (row1 || row2 || row3)) ansi
               // SELECT count(distinct (row1 + row2 + row3)) ms sql-server
  -            //
  -            FieldDescriptor[] pkFields = m_broker.getClassDescriptor(searchClass).getPkFields();
  -            String[] keyColumns = new String[pkFields.length];
   
  -            if(pkFields.length > 1)
  +            FieldDescriptor[] pkFields   = m_broker.getClassDescriptor(searchClass).getPkFields();
  +            String[]          keyColumns = new String[pkFields.length];
  +
  +            if (pkFields.length > 1)
               {
                   // TODO: Use ColumnName. This is a temporary solution because
                   // we cannot yet resolve multiple columns in the same attribute.
  -                for(int i = 0; i < pkFields.length; i++)
  +                for (int idx = 0; idx < pkFields.length; idx++)
                   {
  -                    keyColumns[i] = pkFields[i].getColumnName();
  +                    keyColumns[idx] = pkFields[idx].getColumnName();
                   }
               }
               else
               {
  -                for(int i = 0; i < pkFields.length; i++)
  +                for (int idx = 0; idx < pkFields.length; idx++)
                   {
  -                    keyColumns[i] = pkFields[i].getAttributeName();
  +                    keyColumns[idx] = pkFields[idx].getAttributeName();
                   }
               }
  +            // [tomdz]
  +            // TODO: Add support for databases that do not support COUNT DISTINCT over multiple columns
  +//            if (getPlatform().supportsMultiColumnCountDistinct())
  +//            {
  +//                columns[0] = "count(distinct " + getPlatform().concatenate(keyColumns) + ")";
  +//            }
  +//            else
  +//            {
  +//                columns = keyColumns;
  +//            }
  +
               columns[0] = "count(distinct " + getPlatform().concatenate(keyColumns) + ")";
           }
           else
  @@ -594,13 +607,12 @@
           }
   
           // BRJ: we have to preserve indirection table !
  -        if(aQuery instanceof MtoNQuery)
  +        if (aQuery instanceof MtoNQuery)
           {
  -            MtoNQuery mnQuery = (MtoNQuery) aQuery;
  -            ReportQueryByMtoNCriteria mnReportQuery = new ReportQueryByMtoNCriteria(searchClass,
  -                    columns, countCrit);
  -            mnReportQuery.setIndirectionTable(mnQuery.getIndirectionTable());
  +            MtoNQuery                 mnQuery       = (MtoNQuery)aQuery;
  +            ReportQueryByMtoNCriteria mnReportQuery = new ReportQueryByMtoNCriteria(searchClass, columns, countCrit);
   
  +            mnReportQuery.setIndirectionTable(mnQuery.getIndirectionTable());
               countQuery = mnReportQuery;
           }
           else
  @@ -609,10 +621,10 @@
           }
   
           // BRJ: we have to preserve outer-join-settings (by Andr� Markwalder)
  -        Iterator outerJoinPath = aQuery.getOuterJoinPaths().iterator();
  -        while (outerJoinPath.hasNext())
  +        for (Iterator outerJoinPath = aQuery.getOuterJoinPaths().iterator(); outerJoinPath.hasNext();)
           {
               String path = (String) outerJoinPath.next();
  +
               if (aQuery.isPathOuterJoin(path))
               {
                   countQuery.setPathOuterJoin(path);
  @@ -621,17 +633,28 @@
   
           //BRJ: add orderBy Columns asJoinAttributes
           List orderBy = aQuery.getOrderBy();
  -        if (orderBy != null && !orderBy.isEmpty())
  +
  +        if ((orderBy != null) && !orderBy.isEmpty())
           {
               String[] joinAttributes = new String[orderBy.size()];
  -            for (int i=0; i<orderBy.size(); i++)
  +
  +            for (int idx = 0; idx < orderBy.size(); idx++)
               {
  -                joinAttributes[i] = ((FieldHelper)orderBy.get(i)).name;
  +                joinAttributes[idx] = ((FieldHelper)orderBy.get(idx)).name;
               }
  -
               countQuery.setJoinAttributes(joinAttributes);
           }
   
  +        // [tomdz]
  +        // TODO:
  +        // For those databases that do not support COUNT DISTINCT over multiple columns
  +        // we wrap the normal SELECT DISTINCT that we just created, into a SELECT count(*)
  +        // For this however we need a report query that gets its data from a sub query instead
  +        // of a table (target class)
  +//        if (aQuery.isDistinct() && !getPlatform().supportsMultiColumnCountDistinct())
  +//        {
  +//        }
  +
           return countQuery;
       }
   
  
  
  
  No                   revision
  No                   revision
  1.27.2.7  +9 -1      db-ojb/src/java/org/apache/ojb/broker/platforms/PlatformDefaultImpl.java
  
  Index: PlatformDefaultImpl.java
  ===================================================================
  RCS file: /home/cvs/db-ojb/src/java/org/apache/ojb/broker/platforms/PlatformDefaultImpl.java,v
  retrieving revision 1.27.2.6
  retrieving revision 1.27.2.7
  diff -u -r1.27.2.6 -r1.27.2.7
  --- PlatformDefaultImpl.java	5 Sep 2005 23:37:26 -0000	1.27.2.6
  +++ PlatformDefaultImpl.java	18 Dec 2005 16:43:19 -0000	1.27.2.7
  @@ -390,6 +390,14 @@
       }
   
       /**
  +     * {@inheritDoc}
  +     */
  +    public boolean supportsMultiColumnCountDistinct()
  +    {
  +        return true;
  +    }
  +
  +    /**
        * @see org.apache.ojb.broker.platforms.Platform#concatenate(java.lang.String[])
        */
       public String concatenate(String[] theColumns)
  
  
  
  1.1.2.4   +4 -2      db-ojb/src/java/org/apache/ojb/broker/platforms/Attic/PlatformDerbyImpl.java
  
  Index: PlatformDerbyImpl.java
  ===================================================================
  RCS file: /home/cvs/db-ojb/src/java/org/apache/ojb/broker/platforms/Attic/PlatformDerbyImpl.java,v
  retrieving revision 1.1.2.3
  retrieving revision 1.1.2.4
  diff -u -r1.1.2.3 -r1.1.2.4
  --- PlatformDerbyImpl.java	18 Dec 2005 11:44:03 -0000	1.1.2.3
  +++ PlatformDerbyImpl.java	18 Dec 2005 16:43:19 -0000	1.1.2.4
  @@ -36,9 +36,10 @@
       /**
        * {@inheritDoc}
        */
  -    protected String getConcatenationCharacter()
  +    public boolean supportsMultiColumnCountDistinct()
       {
  -        return ",";
  +        // Currently Derby supports COUNT DISTINCT only for one column
  +        return false;
       }
   
       /**
  @@ -52,6 +53,7 @@
               // [tomdz]
               // Currently, Derby doesn't like Character objects in the PreparedStatement
               // when using PreparedStatement#setObject(index, value, jdbcType) method
  +            // (see issue DERBY-773)
               // So we make a String object out of the Character object and use that instead
               super.setObjectForStatement(ps, index, value.toString(), jdbcType);
           }
  
  
  
  1.24.2.6  +8 -1      db-ojb/src/java/org/apache/ojb/broker/platforms/Platform.java
  
  Index: Platform.java
  ===================================================================
  RCS file: /home/cvs/db-ojb/src/java/org/apache/ojb/broker/platforms/Platform.java,v
  retrieving revision 1.24.2.5
  retrieving revision 1.24.2.6
  diff -u -r1.24.2.5 -r1.24.2.6
  --- Platform.java	5 Sep 2005 23:31:29 -0000	1.24.2.5
  +++ Platform.java	18 Dec 2005 16:43:19 -0000	1.24.2.6
  @@ -223,6 +223,13 @@
       int bindPagingParameters(PreparedStatement ps, int index, int startAt, int endAt) throws SQLException;
   
       /**
  +     * Whether the platform supports a COUNT DISTINCT across multiple columns.
  +     * 
  +     * @return <code>true</code> if it is supported
  +     */
  +    boolean supportsMultiColumnCountDistinct();
  +    
  +    /**
        * Concatenate the columns </br>
        * ie: col1 || col2 || col3 (ANSI)</br>
        * ie: col1 + col2 + col3 (MS SQL-Server)</br>
  
  
  

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