You are viewing a plain text version of this content. The canonical link for it is here.
Posted to ojb-user@db.apache.org by "Norman C. Jarvis" <nj...@learningoptics.com> on 2003/05/15 18:45:02 UTC

Code for PersistenceBrokerImp.getCount

Could you please change the getCount method of
PersistenceBrokerImpl.java to support count queries for fields other
than primary keys.  Here is the diff for how we are doing it compared
to revision 1.7 of PersistenceBrokerImpl.java:


2219c2219
<         String[] columns = new String[pkFields.length];
---
>         String[] columns;
2221a2222,2235
>         if(query instanceof ReportQueryByCriteria &&
>         ((ReportQueryByCriteria)query).getColumns()!=null){
>           columns = ((ReportQueryByCriteria)query).getColumns();
>         }
>         else
>         {
>           // BRJ: add a column for each pkField
>           columns = new String[pkFields.length];
>           for(int i=0; i < pkFields.length; i++)
>           {
>             columns[i] = pkFields[i].getAttributeName();
>           }
>         }
>
2229c2243
<         // BRJ: add a column for each pkField, make it distinct if
query is distinct
---
>         // BRJ: make column distinct if query is distinct
2231c2245
<         for (int i = 0; i < pkFields.length; i++)
---
>         for (int i = 0; i < columns.length; i++)
2235c2249
<                 columns[i] = "count(distinct " +
pkFields[i].getAttributeName() + ")";
---
>                 columns[i] = "count(distinct " + columns[i] + ")";
2239c2253
<                 columns[i] = "count(" + pkFields[i].getAttributeName()
+ ")";
---
>                 columns[i] = "count(" + columns[i] + ")";



Here is the full method as we are using it:

    public int getCount(Query query) throws PersistenceBrokerException
    {
        ReportQueryByCriteria reportQuery;
        Criteria reportCrit = null;
        Iterator iter;
        FieldDescriptor[] pkFields =
getClassDescriptor(query.getSearchClass()).getPkFields();
        String[] columns;
        int result = 0;

        if(query instanceof ReportQueryByCriteria &&
((ReportQueryByCriteria)query).getColumns()!=null)
        {
          columns = ((ReportQueryByCriteria)query).getColumns();
        }
        else
        {
          // BRJ: add a column for each pkField
          columns = new String[pkFields.length];
          for(int i=0; i < pkFields.length; i++)
          {
            columns[i] = pkFields[i].getAttributeName();
          }
        }

        // build a ReportQuery based on query
        // orderby needs to be cleared
        if (query.getCriteria() != null)
        {
            reportCrit = query.getCriteria().copy(false, false, false);
        }

        // BRJ: make column distinct if query is distinct
        // tbd check if it really works for multiple keys ?
        for (int i = 0; i < columns.length; i++)
        {
            if (query.isDistinct())
            {
                columns[i] = "count(distinct " + columns[i] + ")";
            }
            else
            {
                columns[i] = "count(" + columns[i] + ")";
            }
        }

        // BRJ: we have to preserve indirection table !
        if (query instanceof MtoNQuery)
        {
            reportQuery = new
ReportQueryByMtoNCriteria(query.getSearchClass(), columns, reportCrit);
            ((ReportQueryByMtoNCriteria)
reportQuery).setIndirectionTable(((MtoNQuery)
query).getIndirectionTable());
        }
        else
        {
            reportQuery = new
ReportQueryByCriteria(query.getSearchClass(), columns, reportCrit);
        }

        if (logger.isDebugEnabled()) logger.debug("getCount " +
reportQuery.getSearchClass() + ", " + reportQuery);

        iter = getReportQueryIteratorByQuery(reportQuery);
        try
        {
            while (iter.hasNext())
            {
                Object[] row = (Object[]) iter.next();
                result += ((Number) row[0]).intValue();
            }
        }
        finally
        {
            if (iter instanceof OJBIterator)
            {
                ((OJBIterator) iter).releaseDbResources();
            }
        }

        return result;
    }


-- 
Norman Jarvis
njarvis@learningoptics.com