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 ar...@apache.org on 2003/11/14 19:16:46 UTC

cvs commit: db-ojb/src/java/org/apache/ojb/broker/accesslayer RsQueryObject.java SqlBasedRsIterator.java SqlBasedReportQueryRsIterator.java RsIterator.java ReportQueryRsIterator.java

arminw      2003/11/14 10:16:46

  Modified:    src/java/org/apache/ojb/broker/core
                        RsIteratorFactoryImpl.java RsIteratorFactory.java
                        ReportRsIteratorFactoryImpl.java
                        PersistenceBrokerImpl.java
               src/java/org/apache/ojb/broker/accesslayer
                        SqlBasedRsIterator.java
                        SqlBasedReportQueryRsIterator.java RsIterator.java
                        ReportQueryRsIterator.java
  Added:       src/java/org/apache/ojb/broker/accesslayer
                        RsQueryObject.java
  Log:
  - refactoring of RsIterator handling
  - prepare for sql-paging support
  
  Revision  Changes    Path
  1.2       +8 -5      db-ojb/src/java/org/apache/ojb/broker/core/RsIteratorFactoryImpl.java
  
  Index: RsIteratorFactoryImpl.java
  ===================================================================
  RCS file: /home/cvs/db-ojb/src/java/org/apache/ojb/broker/core/RsIteratorFactoryImpl.java,v
  retrieving revision 1.1
  retrieving revision 1.2
  diff -u -r1.1 -r1.2
  --- RsIteratorFactoryImpl.java	26 Apr 2003 23:18:25 -0000	1.1
  +++ RsIteratorFactoryImpl.java	14 Nov 2003 18:16:45 -0000	1.2
  @@ -56,8 +56,10 @@
   
   import org.apache.ojb.broker.accesslayer.RsIterator;
   import org.apache.ojb.broker.accesslayer.SqlBasedRsIterator;
  +import org.apache.ojb.broker.accesslayer.RsQueryObject;
   import org.apache.ojb.broker.metadata.ClassDescriptor;
   import org.apache.ojb.broker.query.Query;
  +import org.apache.ojb.broker.query.QueryBySQL;
   
   /**
    * Factory for RsIterator
  @@ -92,15 +94,16 @@
   	 */
   	public RsIterator createRsIterator(Query query, ClassDescriptor cld, PersistenceBrokerImpl broker)
   	{
  -		return new RsIterator(query, cld, broker);
  +		return new RsIterator(RsQueryObject.get(cld, query), broker);
   	}
   
   	/**
  -	 * @see org.apache.ojb.broker.core.RsIteratorFactory#createRsIterator(String, ClassDescriptor, PersistenceBrokerImpl)
  +	 * @see org.apache.ojb.broker.core.RsIteratorFactory#createRsIterator
  +     * (org.apache.ojb.broker.query.QueryBySQL, org.apache.ojb.broker.metadata.ClassDescriptor, org.apache.ojb.broker.core.PersistenceBrokerImpl))
   	 */
  -	public RsIterator createRsIterator(String sql, ClassDescriptor cld, PersistenceBrokerImpl broker)
  +	public RsIterator createRsIterator(QueryBySQL query, ClassDescriptor cld, PersistenceBrokerImpl broker)
   	{
  -		return new SqlBasedRsIterator(sql, cld, broker);
  +		return new SqlBasedRsIterator(RsQueryObject.get(cld, query), broker);
   	}
   
   }
  
  
  
  1.2       +3 -2      db-ojb/src/java/org/apache/ojb/broker/core/RsIteratorFactory.java
  
  Index: RsIteratorFactory.java
  ===================================================================
  RCS file: /home/cvs/db-ojb/src/java/org/apache/ojb/broker/core/RsIteratorFactory.java,v
  retrieving revision 1.1
  retrieving revision 1.2
  diff -u -r1.1 -r1.2
  --- RsIteratorFactory.java	26 Apr 2003 23:18:25 -0000	1.1
  +++ RsIteratorFactory.java	14 Nov 2003 18:16:45 -0000	1.2
  @@ -57,6 +57,7 @@
   import org.apache.ojb.broker.accesslayer.RsIterator;
   import org.apache.ojb.broker.metadata.ClassDescriptor;
   import org.apache.ojb.broker.query.Query;
  +import org.apache.ojb.broker.query.QueryBySQL;
   
   /**
    * Interface for RsIteratorFactory
  @@ -75,6 +76,6 @@
   	/**
   	 * Create a RsIterator based on a SQL-Statement
   	 */
  -	RsIterator createRsIterator(String sql, ClassDescriptor cld, PersistenceBrokerImpl broker);
  +	RsIterator createRsIterator(QueryBySQL query, ClassDescriptor cld, PersistenceBrokerImpl broker);
   
   }
  
  
  
  1.2       +7 -5      db-ojb/src/java/org/apache/ojb/broker/core/ReportRsIteratorFactoryImpl.java
  
  Index: ReportRsIteratorFactoryImpl.java
  ===================================================================
  RCS file: /home/cvs/db-ojb/src/java/org/apache/ojb/broker/core/ReportRsIteratorFactoryImpl.java,v
  retrieving revision 1.1
  retrieving revision 1.2
  diff -u -r1.1 -r1.2
  --- ReportRsIteratorFactoryImpl.java	26 Apr 2003 23:18:25 -0000	1.1
  +++ ReportRsIteratorFactoryImpl.java	14 Nov 2003 18:16:45 -0000	1.2
  @@ -56,9 +56,11 @@
   
   import org.apache.ojb.broker.accesslayer.ReportQueryRsIterator;
   import org.apache.ojb.broker.accesslayer.RsIterator;
  +import org.apache.ojb.broker.accesslayer.RsQueryObject;
   import org.apache.ojb.broker.accesslayer.SqlBasedReportQueryRsIterator;
   import org.apache.ojb.broker.metadata.ClassDescriptor;
   import org.apache.ojb.broker.query.Query;
  +import org.apache.ojb.broker.query.QueryBySQL;
   
   /**
    * Factory for ReportQueryRsIterator
  @@ -93,15 +95,15 @@
   	 */
   	public RsIterator createRsIterator(Query query, ClassDescriptor cld, PersistenceBrokerImpl broker)
   	{
  -		return new ReportQueryRsIterator(query, cld, broker);
  +		return new ReportQueryRsIterator(RsQueryObject.get(cld, query), broker);
   	}
   
   	/**
  -	 * @see org.apache.ojb.broker.core.RsIteratorFactory#createRsIterator(String, ClassDescriptor, PersistenceBrokerImpl)
  +	 * @see org.apache.ojb.broker.core.RsIteratorFactory#createRsIterator(org.apache.ojb.broker.query.QueryBySQL, ClassDescriptor, PersistenceBrokerImpl)
   	 */
  -	public RsIterator createRsIterator(String sql, ClassDescriptor cld, PersistenceBrokerImpl broker)
  +	public RsIterator createRsIterator(QueryBySQL query, ClassDescriptor cld, PersistenceBrokerImpl broker)
   	{
  -		return new SqlBasedReportQueryRsIterator(sql, cld, broker);
  +		return new SqlBasedReportQueryRsIterator(RsQueryObject.get(cld, query), broker);
   	}
   
   }
  
  
  
  1.52      +20 -22    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.51
  retrieving revision 1.52
  diff -u -r1.51 -r1.52
  --- PersistenceBrokerImpl.java	10 Nov 2003 19:41:04 -0000	1.51
  +++ PersistenceBrokerImpl.java	14 Nov 2003 18:16:45 -0000	1.52
  @@ -456,7 +456,7 @@
               // Invoke events on PersistenceBrokerAware instances and listeners
               AFTER_DELETE_EVENT.setTarget(obj);
               fireBrokerEvent(AFTER_DELETE_EVENT);
  -            
  +
               // let the connection manager to execute batch
               connectionManager.executeBatchIfNecessary();
           }
  @@ -668,7 +668,7 @@
                   // now store it:
               }
               store(obj, oid, cld, doInsert);
  -            
  +
               // let the connection manager to execute batch
               connectionManager.executeBatchIfNecessary();
           }
  @@ -763,7 +763,7 @@
       {
           // get all members of obj that are collections and store all their elements
           Iterator i = vecCds.iterator();
  -        
  +
           while (i.hasNext())
           {
               CollectionDescriptor cds = (CollectionDescriptor) i.next();
  @@ -791,7 +791,7 @@
                       // use empty list on insert (by Andy Malakov)
                       if (insert)
                       {
  -                        currentMtoNKeys = Collections.EMPTY_LIST;    
  +                        currentMtoNKeys = Collections.EMPTY_LIST;
                       }
                       else
                       {
  @@ -799,8 +799,8 @@
                           deleteMtoNImplementor(cds, obj, (Collection)col, currentMtoNKeys);
                       }
                   }
  -                
  -                
  +
  +
                   Iterator colIterator;
                   if (col instanceof ManageableCollection)
                   {
  @@ -821,7 +821,7 @@
                               col.getClass()
                               + " can not be managed by OJB. Use Array, Collection or ManageableCollection instead !");
                   }
  -                
  +
                   while (colIterator.hasNext())
                   {
                       Object otherObj = colIterator.next();
  @@ -849,7 +849,7 @@
                           storeCollectionObject(cds, otherObj);
                       }
                   }
  -                
  +
                   // invoke callback on collection
                   if (col instanceof ManageableCollection)
                   {
  @@ -1777,17 +1777,17 @@
           if (newObj == null && cld.isExtent())
           {
               Iterator extents = getDescriptorRepository().getAllConcreteSubclassDescriptors(cld).iterator();
  -            
  +
               while (extents.hasNext())
               {
                   ClassDescriptor extCld = (ClassDescriptor) extents.next();
  -                
  +
                   newObj = dbAccess.materializeObject(extCld, oid);
                   if (newObj != null)
                   {
                       break;
                   }
  -            }            
  +            }
           }
   
           // loading references is useful only when the Object could be found in db:
  @@ -1795,9 +1795,9 @@
           {
               if (oid.getObjectsRealClass() == null)
               {
  -                oid.setObjectsRealClass(newObj.getClass());   
  +                oid.setObjectsRealClass(newObj.getClass());
               }
  -            
  +
               /*
                * synchronize on newObj so the ODMG-layer can take a snapshot only of
                * fully cached (i.e. with all references + collections) objects
  @@ -1806,7 +1806,7 @@
               {
                   // cache object immediately , so that references
                   // can be established from referenced Objects back to this Object
  -                
  +
                   objectCache.cache(oid, newObj);
   
                   /*
  @@ -2507,9 +2507,7 @@
           if (query instanceof QueryBySQL)
           {
               if(logger.isDebugEnabled()) logger.debug("Creating SQL-RsIterator for class ["+cld.getClassNameOfObject()+"]");
  -
  -            String sql = ((QueryBySQL) query).getSql();
  -            return factory.createRsIterator(sql, cld, this);
  +            return factory.createRsIterator((QueryBySQL) query, cld, this);
           }
   
           if (!cld.isExtent() || !query.getWithExtents())
  @@ -2520,7 +2518,7 @@
               return factory.createRsIterator(query, cld, this);
           }
   
  -        
  +
           if(logger.isDebugEnabled()) logger.debug("Creating ChainingIterator for class ["+cld.getClassNameOfObject()+"]");
   
           ChainingIterator chainingIter = new ChainingIterator();
  @@ -2537,21 +2535,21 @@
           while (extents.hasNext())
           {
               ClassDescriptor extCld = (ClassDescriptor) extents.next();
  -            
  +
               // read same table only once
               if (chainingIter.containsIteratorForTable(extCld.getFullTableName()))
               {
                   if(logger.isDebugEnabled()) logger.debug("Skipping class ["+extCld.getClassNameOfObject()+"]");
               }
               else
  -            {    
  +            {
                   if(logger.isDebugEnabled()) logger.debug("Adding RsIterator of class ["+extCld.getClassNameOfObject()+"] to ChainingIterator");
   
                   // add the iterator to the chaining iterator.
                   chainingIter.addIterator(factory.createRsIterator(query, extCld, this));
               }
           }
  -        
  +
           return chainingIter;
       }
   
  
  
  
  1.19      +15 -17    db-ojb/src/java/org/apache/ojb/broker/accesslayer/SqlBasedRsIterator.java
  
  Index: SqlBasedRsIterator.java
  ===================================================================
  RCS file: /home/cvs/db-ojb/src/java/org/apache/ojb/broker/accesslayer/SqlBasedRsIterator.java,v
  retrieving revision 1.18
  retrieving revision 1.19
  diff -u -r1.18 -r1.19
  --- SqlBasedRsIterator.java	8 Aug 2003 18:48:01 -0000	1.18
  +++ SqlBasedRsIterator.java	14 Nov 2003 18:16:46 -0000	1.19
  @@ -54,13 +54,12 @@
    * <http://www.apache.org/>.
    */
   
  -import java.sql.SQLException;
  -
   import org.apache.ojb.broker.Identity;
   import org.apache.ojb.broker.PersistenceBrokerException;
  -import org.apache.ojb.broker.metadata.ClassDescriptor;
  -import org.apache.ojb.broker.query.Query;
   import org.apache.ojb.broker.core.PersistenceBrokerImpl;
  +import org.apache.ojb.broker.query.Query;
  +
  +import java.sql.SQLException;
   
   /**
    * RsIterator based on SQL-Statement
  @@ -70,19 +69,17 @@
    */
   public class SqlBasedRsIterator extends RsIterator
   {
  -    private String m_sql;
  -
       /**
        * SqlBasedRsIterator constructor.
        */
  -    public SqlBasedRsIterator(String sqlStatement, ClassDescriptor cld, PersistenceBrokerImpl broker)
  +    public SqlBasedRsIterator(RsQueryObject queryObject, PersistenceBrokerImpl broker)
               throws PersistenceBrokerException
       {
  -        super(cld, broker);
  -        if (logger.isDebugEnabled())
  -            logger.debug("SqlBasedRsIterator(" + sqlStatement + ", " + cld + ")");
  -        m_sql = sqlStatement;
  -        m_rsAndStmt = broker.serviceJdbcAccess().executeSQL(sqlStatement, cld, Query.SCROLLABLE);
  +        super(queryObject, broker);
  +        if(!queryObject.isSQLBased())
  +        {
  +            throw new PersistenceBrokerException("Given query is not a QueryBySQL object");
  +        }
       }
   
       /**
  @@ -119,12 +116,12 @@
           ResultSetAndStatement rsStmt;
           int count = 0;
   
  -        int fromPos = m_sql.toUpperCase().indexOf(" FROM ");
  +        int fromPos = queryObject.getSQLBasedQuery().toUpperCase().indexOf(" FROM ");
           if (fromPos >= 0)
           {
  -            countSql = "select count(*)" + m_sql.substring(fromPos);
  +            countSql = "select count(*)" + queryObject.getSQLBasedQuery().substring(fromPos);
           }
  -        
  +
           // BRJ: cut before ORDER BY
           int orderPos = countSql.toUpperCase().indexOf(" ORDER BY ");
           if (orderPos >= 0)
  @@ -132,7 +129,8 @@
               countSql = countSql.substring(0, orderPos);
           }
   
  -        rsStmt = m_broker.serviceJdbcAccess().executeSQL(countSql, m_cld, Query.NOT_SCROLLABLE);
  +        rsStmt = m_broker.serviceJdbcAccess().executeSQL(
  +                countSql, queryObject.getClassDescriptor(), Query.NOT_SCROLLABLE);
           try
           {
               if (rsStmt.m_rs.next())
  
  
  
  1.7       +4 -5      db-ojb/src/java/org/apache/ojb/broker/accesslayer/SqlBasedReportQueryRsIterator.java
  
  Index: SqlBasedReportQueryRsIterator.java
  ===================================================================
  RCS file: /home/cvs/db-ojb/src/java/org/apache/ojb/broker/accesslayer/SqlBasedReportQueryRsIterator.java,v
  retrieving revision 1.6
  retrieving revision 1.7
  diff -u -r1.6 -r1.7
  --- SqlBasedReportQueryRsIterator.java	26 Apr 2003 23:18:25 -0000	1.6
  +++ SqlBasedReportQueryRsIterator.java	14 Nov 2003 18:16:46 -0000	1.7
  @@ -55,7 +55,6 @@
    */
   
   import org.apache.ojb.broker.PersistenceBrokerException;
  -import org.apache.ojb.broker.metadata.ClassDescriptor;
   import org.apache.ojb.broker.core.PersistenceBrokerImpl;
   import org.apache.ojb.broker.util.SqlHelper;
   
  @@ -76,14 +75,14 @@
       /**
        * SqlBasedRsIterator constructor.
        */
  -    public SqlBasedReportQueryRsIterator(String sqlStatement, ClassDescriptor cld, PersistenceBrokerImpl broker)
  +    public SqlBasedReportQueryRsIterator(RsQueryObject queryObject, PersistenceBrokerImpl broker)
               throws PersistenceBrokerException
       {
   
  -        super(sqlStatement, cld, broker);
  +        super(queryObject, broker);
           try
           {
  -            rsMetaData = super.m_rsAndStmt.m_rs.getMetaData();
  +            rsMetaData = m_rsAndStmt.m_rs.getMetaData();
               columnCount = rsMetaData.getColumnCount();
           }
           catch (SQLException e)
  
  
  
  1.47      +85 -39    db-ojb/src/java/org/apache/ojb/broker/accesslayer/RsIterator.java
  
  Index: RsIterator.java
  ===================================================================
  RCS file: /home/cvs/db-ojb/src/java/org/apache/ojb/broker/accesslayer/RsIterator.java,v
  retrieving revision 1.46
  retrieving revision 1.47
  diff -u -r1.46 -r1.47
  --- RsIterator.java	11 Nov 2003 00:03:43 -0000	1.46
  +++ RsIterator.java	14 Nov 2003 18:16:46 -0000	1.47
  @@ -59,12 +59,13 @@
   import org.apache.ojb.broker.PersistenceBrokerException;
   import org.apache.ojb.broker.VirtualProxy;
   import org.apache.ojb.broker.cache.ObjectCache;
  +import org.apache.ojb.broker.core.PersistenceBrokerImpl;
   import org.apache.ojb.broker.metadata.ClassDescriptor;
   import org.apache.ojb.broker.metadata.DescriptorRepository;
   import org.apache.ojb.broker.metadata.FieldDescriptor;
   import org.apache.ojb.broker.metadata.JdbcConnectionDescriptor;
   import org.apache.ojb.broker.query.Query;
  -import org.apache.ojb.broker.core.PersistenceBrokerImpl;
  +import org.apache.ojb.broker.query.QueryBySQL;
   import org.apache.ojb.broker.util.logging.Logger;
   import org.apache.ojb.broker.util.logging.LoggerFactory;
   
  @@ -120,9 +121,9 @@
       protected ResultSetAndStatement m_rsAndStmt;
   
       /**
  -     * the classdescriptor providing the mapping information
  +     * the underlying query object
        */
  -    protected ClassDescriptor m_cld;
  +    protected org.apache.ojb.broker.accesslayer.RsQueryObject queryObject;
   
       /**
        * the proxy class to be used or null
  @@ -158,7 +159,6 @@
   
       private boolean advancedJDBCSupport = false;
       private boolean JDBCSupportAssessed = false;
  -    private Query m_query = null;
       private int m_current_row = 0;
       /**
        * Tracks whether or not the resources that are used by this
  @@ -167,24 +167,27 @@
       private boolean resourcesAreReleased = false;
   
       /**
  -     * Initialize base basic field
  +     * RsIterator constructor.
  +     *
  +     * @param queryObject query object
  +     * @param broker the broker we should use.
        */
  -    protected RsIterator(ClassDescriptor mif, PersistenceBrokerImpl broker)
  +    public RsIterator(RsQueryObject queryObject, PersistenceBrokerImpl broker)
       {
           cache = broker.serviceObjectCache();
           m_row = new HashMap();
           m_broker = broker;
  -        m_cld = mif;
  +        this.queryObject = queryObject;
   
           Class classToPrefetch = broker.getClassToPrefetch();
           if ((classToPrefetch != null)
  -                && classToPrefetch.isAssignableFrom(mif.getClassOfObject()))
  +                && classToPrefetch.isAssignableFrom(queryObject.getClassDescriptor().getClassOfObject()))
           {
               itemProxyClass = null;
           }
           else
           {
  -            itemProxyClass = mif.getProxyClass();
  +            itemProxyClass = queryObject.getClassDescriptor().getProxyClass();
           }
   
           /*
  @@ -194,24 +197,17 @@
           TODO: Find a better solution
           */
           afterLookupEvent = new PBLifeCycleEvent(m_broker, PBLifeCycleEvent.Type.AFTER_LOOKUP);
  -    }
   
  -    /**
  -     * RsIterator constructor.
  -     * @param query the SELECT producing the underlying resultset
  -     * @param mif ClassDescriptor of the result-class
  -     * @param broker the broker we should use.
  -     */
  -    public RsIterator(Query query, ClassDescriptor mif, PersistenceBrokerImpl broker)
  -    {
  -        this(mif, broker);
  -        m_query = query;
  -        m_rsAndStmt = broker.serviceJdbcAccess().executeQuery(query, mif);
  -        prefetchRelationships(query);
  +        m_rsAndStmt = queryObject.performQuery(broker.serviceJdbcAccess());
  +        /*
  +        TODO:
  +        how does prefetchRelationships handle QueryBySQL instances?
  +        Is it ok to pass query object?
  +        */
  +        prefetchRelationships(queryObject.getQuery());
           if (logger.isDebugEnabled())
           {
  -            logger.debug("RsIterator[query: " + query
  -                    + ", ClassDescriptor: " + mif.getClassNameOfObject() + "] initialized");
  +            logger.debug("RsIterator[" + queryObject + "] initialized");
           }
       }
   
  @@ -219,7 +215,7 @@
       {
           if (itemTopLevelClass == null)
           {
  -            itemTopLevelClass = m_broker.getTopLevelClass(m_cld.getClassOfObject());
  +            itemTopLevelClass = m_broker.getTopLevelClass(queryObject.getClassDescriptor().getClassOfObject());
           }
           return itemTopLevelClass;
       }
  @@ -341,7 +337,8 @@
           for (int i = 0; i < prefetchedRel.size(); i++)
           {
               relName = (String) prefetchedRel.get(i);
  -            prefetchers[i] = RelationshipPrefetcherFactory.createRelationshipPrefetcher(m_broker, m_cld, relName);
  +            prefetchers[i] = RelationshipPrefetcherFactory.createRelationshipPrefetcher(
  +                    m_broker, queryObject.getClassDescriptor(), relName);
               prefetchers[i].prepareRelationshipSettings();
           }
   
  @@ -381,7 +378,7 @@
       {
           // fill primary key values from Resultset
           FieldDescriptor fld;
  -        FieldDescriptor[] pkFields = m_cld.getPkFields();
  +        FieldDescriptor[] pkFields = queryObject.getClassDescriptor().getPkFields();
           Object[] pkValues = new Object[pkFields.length];
   
           for (int i = 0; i < pkFields.length; i++)
  @@ -391,7 +388,7 @@
           }
   
           // return identity object build up from primary keys
  -        return new Identity(m_cld.getClassOfObject(), getTopLevelClass(), pkValues);
  +        return new Identity(queryObject.getClassDescriptor().getClassOfObject(), getTopLevelClass(), pkValues);
       }
   
       /**
  @@ -416,14 +413,14 @@
           if (itemProxyClass != null)
           {
               // provide m_row with primary key data of current row
  -            m_cld.getRowReader().readPkValuesFrom(m_rsAndStmt.m_rs, m_row);
  +            queryObject.getClassDescriptor().getRowReader().readPkValuesFrom(m_rsAndStmt.m_rs, m_row);
               // assert: m_row is filled with primary key values from db
               return getProxyFromResultSet();
           }
           else
           {
               // 0. provide m_row with data of current row
  -            m_cld.getRowReader().readObjectArrayFrom(m_rsAndStmt.m_rs, m_row);
  +            queryObject.getClassDescriptor().getRowReader().readObjectArrayFrom(m_rsAndStmt.m_rs, m_row);
               // assert: m_row is filled from db
   
               // 1.read Identity
  @@ -436,7 +433,7 @@
               {
                   // 3. If Object is not in cache
                   // materialize Object with primitive attributes filled from current row
  -                result = m_cld.getRowReader().readObjectFrom(m_row);
  +                result = queryObject.getClassDescriptor().getRowReader().readObjectFrom(m_row);
                   // result may still be null!
                   if (result != null)
                   {
  @@ -459,7 +456,7 @@
                            * the class descriptor for the actual class.
                            */
                           // fill reference and collection attributes
  -                        ClassDescriptor cld = m_cld.getRepository().getDescriptorFor(result.getClass());
  +                        ClassDescriptor cld = queryObject.getClassDescriptor().getRepository().getDescriptorFor(result.getClass());
                           // don't force loading of reference
                           final boolean unforced = false;
                           // Maps ReferenceDescriptors to HashSets of owners
  @@ -470,11 +467,11 @@
               }
               else // Object is in cache
               {
  -                ClassDescriptor cld = m_cld.getRepository().getDescriptorFor(result.getClass());
  +                ClassDescriptor cld = queryObject.getClassDescriptor().getRepository().getDescriptorFor(result.getClass());
                   // if refresh is required, update the cache instance from the db
                   if (cld.isAlwaysRefresh())
                   {
  -                	m_cld.getRowReader().refreshObject(result, m_row);
  +                	queryObject.getClassDescriptor().getRowReader().refreshObject(result, m_row);
                   }
                   m_broker.refreshRelationships(result, cld);
               }
  @@ -523,7 +520,57 @@
        */
       protected int countedSize() throws PersistenceBrokerException
       {
  -        return m_broker.getCount(m_query);
  +        /*
  +        TODO: PB.getCount(..) should handle QueryBySQL instances too
  +        */
  +        if(queryObject.getQuery() instanceof QueryBySQL)
  +        {
  +            return countedSizeBySQL();
  +        }
  +        else
  +        {
  +            return m_broker.getCount(queryObject.getQuery());
  +        }
  +    }
  +
  +    protected int countedSizeBySQL() throws PersistenceBrokerException
  +    {
  +        String countSql = ((QueryBySQL)queryObject.getQuery()).getSql();
  +
  +        ResultSetAndStatement rsStmt;
  +        int count = 0;
  +
  +        int fromPos = countSql.toUpperCase().indexOf(" FROM ");
  +        if (fromPos >= 0)
  +        {
  +            countSql = "select count(*)" + countSql.substring(fromPos);
  +        }
  +
  +        // BRJ: cut before ORDER BY
  +        int orderPos = countSql.toUpperCase().indexOf(" ORDER BY ");
  +        if (orderPos >= 0)
  +        {
  +            countSql = countSql.substring(0, orderPos);
  +        }
  +
  +        rsStmt = m_broker.serviceJdbcAccess().executeSQL(
  +                countSql, queryObject.getClassDescriptor(), Query.NOT_SCROLLABLE);
  +        try
  +        {
  +            if (rsStmt.m_rs.next())
  +            {
  +                count = rsStmt.m_rs.getInt(1);
  +            }
  +        }
  +        catch (SQLException e)
  +        {
  +            throw new PersistenceBrokerException(e);
  +        }
  +        finally
  +        {
  +            rsStmt.close();
  +        }
  +        return count;
       }
   
       /**
  @@ -735,7 +782,7 @@
   
       public String toString()
       {
  -        return super.toString() + "\n" + m_cld.toString();
  +        return super.toString();
       }
   
       /**
  @@ -743,8 +790,7 @@
        */
       public ClassDescriptor getClassDescriptor()
       {
  -        return m_cld;
  +        return queryObject.getClassDescriptor();
       }
  -
   }
   
  
  
  
  1.6       +3 -8      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.5
  retrieving revision 1.6
  diff -u -r1.5 -r1.6
  --- ReportQueryRsIterator.java	26 Apr 2003 23:18:25 -0000	1.5
  +++ ReportQueryRsIterator.java	14 Nov 2003 18:16:46 -0000	1.6
  @@ -55,8 +55,6 @@
    */
   
   import org.apache.ojb.broker.PersistenceBrokerException;
  -import org.apache.ojb.broker.metadata.ClassDescriptor;
  -import org.apache.ojb.broker.query.Query;
   import org.apache.ojb.broker.core.PersistenceBrokerImpl;
   import org.apache.ojb.broker.util.SqlHelper;
   
  @@ -77,13 +75,10 @@
   
       /**
        * Constructor for ReportQueryRsIterator.
  -     * @param query
  -     * @param mif
  -     * @param broker
        */
  -    public ReportQueryRsIterator(Query query, ClassDescriptor mif, PersistenceBrokerImpl broker)
  +    public ReportQueryRsIterator(RsQueryObject queryObject, PersistenceBrokerImpl broker)
       {
  -        super(query, mif, broker);
  +        super(queryObject, broker);
           try
           {
               rsMetaData = super.m_rsAndStmt.m_rs.getMetaData();
  
  
  
  1.1                  db-ojb/src/java/org/apache/ojb/broker/accesslayer/RsQueryObject.java
  
  Index: RsQueryObject.java
  ===================================================================
  package org.apache.ojb.broker.accesslayer;
  
  /* ====================================================================
   * The Apache Software License, Version 1.1
   *
   * Copyright (c) 2001 The Apache Software Foundation.  All rights
   * reserved.
   *
   * Redistribution and use in source and binary forms, with or without
   * modification, are permitted provided that the following conditions
   * are met:
   *
   * 1. Redistributions of source code must retain the above copyright
   *    notice, this list of conditions and the following disclaimer.
   *
   * 2. Redistributions in binary form must reproduce the above copyright
   *    notice, this list of conditions and the following disclaimer in
   *    the documentation and/or other materials provided with the
   *    distribution.
   *
   * 3. The end-user documentation included with the redistribution,
   *    if any, must include the following acknowledgment:
   *       "This product includes software developed by the
   *        Apache Software Foundation (http://www.apache.org/)."
   *    Alternately, this acknowledgment may appear in the software itself,
   *    if and wherever such third-party acknowledgments normally appear.
   *
   * 4. The names "Apache" and "Apache Software Foundation" and
   *    "Apache ObjectRelationalBridge" must not be used to endorse or promote products
   *    derived from this software without prior written permission. For
   *    written permission, please contact apache@apache.org.
   *
   * 5. Products derived from this software may not be called "Apache",
   *    "Apache ObjectRelationalBridge", nor may "Apache" appear in their name, without
   *    prior written permission of the Apache Software Foundation.
   *
   * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
   * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
   * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
   * DISCLAIMED.  IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
   * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
   * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
   * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
   * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
   * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
   * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
   * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   * SUCH DAMAGE.
   * ====================================================================
   *
   * This software consists of voluntary contributions made by many
   * individuals on behalf of the Apache Software Foundation.  For more
   * information on the Apache Software Foundation, please see
   * <http://www.apache.org/>.
   */
  
  import org.apache.ojb.broker.query.Query;
  import org.apache.ojb.broker.query.QueryBySQL;
  import org.apache.ojb.broker.metadata.ClassDescriptor;
  import org.apache.ojb.broker.accesslayer.ResultSetAndStatement;
  import org.apache.ojb.broker.accesslayer.JdbcAccess;
  
  /**
   * Helper class for {@link RsIterator} queries.
   *
   * @author <a href="mailto:armin@codeAuLait.de">Armin Waibel</a>
   * @version $Id: RsQueryObject.java,v 1.1 2003/11/14 18:16:46 arminw Exp $
   */
  public class RsQueryObject
  {
      private Query query;
      private ClassDescriptor cld;
      private boolean isSQLBased;
  
      //*******************************************
      // static access methods
      //*******************************************
  
      /**
       * Returns a new instance of this class.
       */
      public static RsQueryObject get(ClassDescriptor cld, Query query)
      {
          return new RsQueryObject(cld, query);
      }
  
  
      //*******************************************
      // private constructors
      //*******************************************
      private RsQueryObject(ClassDescriptor cld, Query query)
      {
          this.query = query;
          this.cld = cld;
          if(query instanceof QueryBySQL)
          {
              isSQLBased = true;
          }
      }
  
      //*******************************************
      // public methods
      //*******************************************
      public ResultSetAndStatement performQuery(JdbcAccess jdbcAccess)
      {
          if (query instanceof QueryBySQL)
          {
  
              return jdbcAccess.executeSQL(((QueryBySQL) query).getSql(), cld, Query.SCROLLABLE);
          }
          else
          {
              return jdbcAccess.executeQuery(query, cld);
          }
      }
  
      public boolean usePaging()
      {
          return query.getEndAtIndex() != Query.NO_END_AT_INDEX
                  || query.getStartAtIndex() != Query.NO_START_AT_INDEX;
      }
  
      public int getStartIndex()
      {
          return query.getStartAtIndex();
      }
  
      public int getEndIndex()
      {
          return query.getEndAtIndex();
      }
  
      public ClassDescriptor getClassDescriptor()
      {
          return cld;
      }
  
      public Query getQuery()
      {
          return query;
      }
  
      public boolean isSQLBased()
      {
          return isSQLBased;
      }
  
      public String getSQLBasedQuery()
      {
          if(isSQLBased())
          {
              return ((QueryBySQL)query).getSql();
          }
          else
          {
              return null;
          }
      }
  
      public String toString()
      {
          return this.getClass().getName() +
                  "[" + "query: " + query + ", class descriptor: " + cld.getClassNameOfObject() + "]";
      }
  }
  
  
  

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