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/java/org/apache/ojb/broker/util BrokerHelper.java
Date Sat, 22 Nov 2003 14:12:04 GMT
brj         2003/11/22 06:12:04

  Modified:    src/java/org/apache/ojb/broker/core
                        PersistenceBrokerImpl.java
               src/java/org/apache/ojb/broker/util BrokerHelper.java
  Log:
  moved creation of count-query to BrokerHelper
  
  Revision  Changes    Path
  1.55      +4 -44     db-ojb/src/java/org/apache/ojb/broker/core/PersistenceBrokerImpl.java
  
  Index: PersistenceBrokerImpl.java
  ===================================================================
  RCS file: /home/cvs/db-ojb/src/java/org/apache/ojb/broker/core/PersistenceBrokerImpl.java,v
  retrieving revision 1.54
  retrieving revision 1.55
  diff -u -r1.54 -r1.55
  --- PersistenceBrokerImpl.java	19 Nov 2003 21:16:55 -0000	1.54
  +++ PersistenceBrokerImpl.java	22 Nov 2003 14:12:03 -0000	1.55
  @@ -81,14 +81,11 @@
   import org.apache.ojb.broker.metadata.ObjectReferenceDescriptor;
   import org.apache.ojb.broker.metadata.fieldaccess.PersistentField;
   import org.apache.ojb.broker.query.Criteria;
  -import org.apache.ojb.broker.query.MtoNQuery;
   import org.apache.ojb.broker.query.Query;
   import org.apache.ojb.broker.query.QueryByCriteria;
   import org.apache.ojb.broker.query.QueryByIdentity;
   import org.apache.ojb.broker.query.QueryBySQL;
   import org.apache.ojb.broker.query.QueryFactory;
  -import org.apache.ojb.broker.query.ReportQueryByCriteria;
  -import org.apache.ojb.broker.query.ReportQueryByMtoNCriteria;
   import org.apache.ojb.broker.util.ArrayIterator;
   import org.apache.ojb.broker.util.BrokerHelper;
   import org.apache.ojb.broker.util.ObjectModification;
  @@ -129,7 +126,6 @@
       private Logger logger = LoggerFactory.getLogger(PersistenceBrokerImpl.class);
   
       private final boolean batchRetrieval = true;
  -
       private final boolean prefetchProxies = true;
   
       protected PersistenceBrokerFactoryIF pbf;
  @@ -169,7 +165,6 @@
       private List markedForDelete;
   
       private Class classToPrefetch;
  -
       private ArrayList prefetchingListeners;
   
       private HashMap m_retrievalTasks;
  @@ -2351,48 +2346,13 @@
        */
       public int getCount(Query query) throws PersistenceBrokerException
       {
  -        ReportQueryByCriteria reportQuery;
  -        Criteria reportCrit = null;
  +        Query countQuery = serviceBrokerHelper().getCountQuery(query);
           Iterator iter;
  -        FieldDescriptor[] pkFields = getClassDescriptor(query.getSearchClass()).getPkFields();
  -        String[] columns = new String[pkFields.length];
           int result = 0;
   
  -        // build a ReportQuery based on query
  -        // orderby needs to be cleared
  -        if (query.getCriteria() != null)
  -        {
  -            reportCrit = query.getCriteria().copy(false, false, false);
  -        }
  -
  -        // BRJ: add a column for each pkField, make it distinct if query is distinct
  -        // tbd check if it really works for multiple keys ?
  -        for (int i = 0; i < pkFields.length; i++)
  -        {
  -            if (query.isDistinct())
  -            {
  -                columns[i] = "count(distinct " + pkFields[i].getAttributeName() + ")";
  -            }
  -            else
  -            {
  -                columns[i] = "count(" + pkFields[i].getAttributeName() + ")";
  -            }
  -        }
  -
  -        // 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);
  +        if (logger.isDebugEnabled()) logger.debug("getCount " + countQuery.getSearchClass()
+ ", " + countQuery);
   
  -        iter = getReportQueryIteratorByQuery(reportQuery);
  +        iter = getReportQueryIteratorByQuery(countQuery);
           try
           {
               while (iter.hasNext())
  
  
  
  1.29      +105 -4    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.28
  retrieving revision 1.29
  diff -u -r1.28 -r1.29
  --- BrokerHelper.java	6 Nov 2003 01:41:03 -0000	1.28
  +++ BrokerHelper.java	22 Nov 2003 14:12:04 -0000	1.29
  @@ -69,6 +69,13 @@
   import org.apache.ojb.broker.metadata.MetadataException;
   import org.apache.ojb.broker.metadata.MetadataManager;
   import org.apache.ojb.broker.metadata.fieldaccess.PersistentField;
  +import org.apache.ojb.broker.query.Criteria;
  +import org.apache.ojb.broker.query.MtoNQuery;
  +import org.apache.ojb.broker.query.Query;
  +import org.apache.ojb.broker.query.QueryByCriteria;
  +import org.apache.ojb.broker.query.QueryBySQL;
  +import org.apache.ojb.broker.query.ReportQueryByCriteria;
  +import org.apache.ojb.broker.query.ReportQueryByMtoNCriteria;
   import org.apache.ojb.broker.util.sequence.SequenceManagerException;
   //#ifdef JDK13
   import java.lang.reflect.Proxy;
  @@ -86,11 +93,11 @@
   public class BrokerHelper
   {
   	public static final String REPOSITORY_NAME_SEPARATOR = "#";
  -	private PersistenceBroker broker;
  +	private PersistenceBroker m_broker;
   
   	public BrokerHelper(PersistenceBroker broker)
   	{
  -		this.broker = broker;
  +		m_broker = broker;
   	}
   
   	/**
  @@ -315,7 +322,7 @@
               {
                   // lookup SeqMan for a value matching db column an
   				// fieldconversion
  -                Object result = broker.serviceSequenceManager().getUniqueValue(fd);
  +                Object result = m_broker.serviceSequenceManager().getUniqueValue(fd);
                   // reflect autoincrement value back into object
                   f.set(obj, result);
                   return result;
  @@ -395,6 +402,100 @@
   		return getValuesForObject(realCld.getAllRwFields(), obj, true);
   	}
   
  +   
  +    /**
  +     * Build a Count-Query based on aQuery
  +     * @param aQuery
  +     * @return
  +     */
  +    public Query getCountQuery(Query aQuery)
  +    {
  +        if (aQuery instanceof QueryBySQL)
  +        {
  +            return getCountQuery((QueryBySQL)aQuery);
  +        }
  +        else
  +        {
  +            return getCountQuery((QueryByCriteria)aQuery);            
  +        }
  +    }
  +    
  +    /**
  +     * Create a Count-Query for QueryBySQL
  +     * @param aQuery
  +     * @return
  +     */
  +    private Query getCountQuery(QueryBySQL aQuery)
  +    {
  +        String countSql = aQuery.getSql();
  +
  +        int fromPos = countSql.toUpperCase().indexOf(" FROM ");
  +        if (fromPos >= 0)
  +        {
  +            countSql = "select count(*)" + countSql.substring(fromPos);
  +        }
  +
  +        int orderPos = countSql.toUpperCase().indexOf(" ORDER BY ");
  +        if (orderPos >= 0)
  +        {
  +            countSql = countSql.substring(0, orderPos);
  +        }
  +        
  +        return new QueryBySQL(aQuery.getSearchClass(), countSql);        
  +    }
  +
  +    
  +    /**
  +     * Create a Count-Query for QueryByCriteria
  +     * @param aQuery
  +     * @return
  +     */
  +    private Query getCountQuery(QueryByCriteria aQuery)
  +    {
  +        Class searchClass = aQuery.getSearchClass();
  +        ReportQueryByCriteria countQuery;
  +        Criteria countCrit = null;
  +        FieldDescriptor[] pkFields = m_broker.getClassDescriptor(searchClass).getPkFields();
  +        String[] columns = new String[pkFields.length];
  +
  +        // build a ReportQuery based on query orderby needs to be cleared
  +        if (aQuery.getCriteria() != null)
  +        {
  +            countCrit = aQuery.getCriteria().copy(false, false, false);
  +        }
  +
  +        // BRJ: add a column for each pkField, make it distinct if query is distinct
  +        // TBD check if it really works for multiple keys ?
  +        for (int i = 0; i < pkFields.length; i++)
  +        {
  +            if (aQuery.isDistinct())
  +            {
  +                columns[i] = "count(distinct " + pkFields[i].getAttributeName() + ")";
  +            }
  +            else
  +            {
  +                columns[i] = "count(" + pkFields[i].getAttributeName() + ")";
  +            }
  +        }
  +
  +        // 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);
  +        }
  +        
  +        return countQuery;
  +    }
  +    
  +    
   	public static String buildMessageString(Object obj, Object value, Field field)
   	{
   		String eol = SystemUtils.LINE_SEPARATOR;
  
  
  

---------------------------------------------------------------------
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