private Query getQueryByCriteriaCount(QueryByCriteria aQuery)
    {
        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);
        }

        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:
            //
            // 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)
            {
                // TODO: Use ColumnName. This is a temporary solution because
                // we cannot yet resolve multiple columns in the same attribute.
                for (int idx = 0; idx < pkFields.length; idx++)
                {
                    keyColumns[idx] = pkFields[idx].getColumnName();
                }
            }
            else
            {
                for (int idx = 0; idx < pkFields.length; idx++)
                {
                    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
        {
            columns[0] = "count(*)";
        }

        // BRJ: we have to preserve indirection table !
        if (aQuery instanceof MtoNQuery)
        {
            MtoNQuery                 mnQuery       = (MtoNQuery)aQuery;
            ReportQueryByMtoNCriteria mnReportQuery = new ReportQueryByMtoNCriteria(searchClass, columns, countCrit);

            mnReportQuery.setIndirectionTable(mnQuery.getIndirectionTable());
            countQuery = mnReportQuery;
        }
        else
        {
            countQuery = new ReportQueryByCriteria(searchClass, columns, countCrit);
        }

        // BRJ: we have to preserve outer-join-settings (by André Markwalder)
        for (Iterator outerJoinPath = aQuery.getOuterJoinPaths().iterator(); outerJoinPath.hasNext();)
        {
            String path = (String) outerJoinPath.next();

            if (aQuery.isPathOuterJoin(path))
            {
                countQuery.setPathOuterJoin(path);
            }
        }

        //BRJ: add orderBy Columns asJoinAttributes
        List orderBy = aQuery.getOrderBy();

        if ((orderBy != null) && !orderBy.isEmpty())
        {
            String[] joinAttributes = new String[orderBy.size()];

            for (int idx = 0; idx < orderBy.size(); idx++)
            {
                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;
    }
private Query getQueryByCriteriaCount ( QueryByCriteria aQuery ) { Class searchClass = aQuery . getSearchClass ( ) ; ReportQueryByCriteria countQuery = null ; Criteria countCrit = null ; String [ ] columns = new String [ 1 ] ; if ( aQuery . getCriteria ( ) != null ) { countCrit = aQuery . getCriteria ( ) . copy ( false , false , false ) ; } if ( aQuery . isDistinct ( ) ) { FieldDescriptor [ ] pkFields = m_broker . getClassDescriptor ( searchClass ) . getPkFields ( ) ; String [ ] keyColumns = new String [ pkFields . length ] ; if ( pkFields . length > 1 ) { for ( int idx = 0 ; idx < pkFields . length ; idx ++ ) { keyColumns [ idx ] = pkFields [ idx ] . getColumnName ( ) ; } } else { for ( int idx = 0 ; idx < pkFields . length ; idx ++ ) { keyColumns [ idx ] = pkFields [ idx ] . getAttributeName ( ) ; } } columns [ 0 ] = "count(distinct " + getPlatform ( ) . concatenate ( keyColumns ) + ")" ; } else { columns [ 0 ] = "count(*)" ; } if ( aQuery instanceof MtoNQuery ) { MtoNQuery mnQuery = ( MtoNQuery ) aQuery ; ReportQueryByMtoNCriteria mnReportQuery = new ReportQueryByMtoNCriteria ( searchClass , columns , countCrit ) ; mnReportQuery . setIndirectionTable ( mnQuery . getIndirectionTable ( ) ) ; countQuery = mnReportQuery ; } else { countQuery = new ReportQueryByCriteria ( searchClass , columns , countCrit ) ; } for ( Iterator outerJoinPath = aQuery . getOuterJoinPaths ( ) . iterator ( ) ; outerJoinPath . hasNext ( ) ; ) { String path = ( String ) outerJoinPath . next ( ) ; if ( aQuery . isPathOuterJoin ( path ) ) { countQuery . setPathOuterJoin ( path ) ; } } List orderBy = aQuery . getOrderBy ( ) ; if ( ( orderBy != null ) && ! orderBy . isEmpty ( ) ) { String [ ] joinAttributes = new String [ orderBy . size ( ) ] ; for ( int idx = 0 ; idx < orderBy . size ( ) ; idx ++ ) { joinAttributes [ idx ] = ( ( FieldHelper ) orderBy . get ( idx ) ) . name ; } countQuery . setJoinAttributes ( joinAttributes ) ; } return countQuery ; }
Create a Count-Query for QueryByCriteria