001    package org.apache.ojb.broker.accesslayer;
002    
003    /* Copyright 2002-2005 The Apache Software Foundation
004     *
005     * Licensed under the Apache License, Version 2.0 (the "License");
006     * you may not use this file except in compliance with the License.
007     * You may obtain a copy of the License at
008     *
009     *     http://www.apache.org/licenses/LICENSE-2.0
010     *
011     * Unless required by applicable law or agreed to in writing, software
012     * distributed under the License is distributed on an "AS IS" BASIS,
013     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014     * See the License for the specific language governing permissions and
015     * limitations under the License.
016     */
017    
018    import org.apache.ojb.broker.Identity;
019    import org.apache.ojb.broker.PersistenceBrokerException;
020    import org.apache.ojb.broker.core.PersistenceBrokerImpl;
021    
022    /**
023     * RsIterator based on SQL-Statement
024     *
025     * @author <a href="mailto:jbraeuchi@hotmail.com">Jakob Braeuchi</a>
026     * @version $Id: SqlBasedRsIterator.java,v 1.1 2007-08-24 22:17:30 ewestfal Exp $
027     */
028    public class SqlBasedRsIterator extends RsIterator
029    {
030        /**
031         * SqlBasedRsIterator constructor.
032         */
033        public SqlBasedRsIterator(RsQueryObject queryObject, PersistenceBrokerImpl broker)
034                throws PersistenceBrokerException
035        {
036            super(queryObject, broker);
037            if(!queryObject.isSQLBased())
038            {
039                throw new PersistenceBrokerException("Given query is not a QueryBySQL object");
040            }
041        }
042    
043        /**
044         * returns a proxy or a fully materialized Object from the current row of the
045         * underlying resultset.
046         */
047        protected Object getObjectFromResultSet() throws PersistenceBrokerException
048        {
049    
050            try
051            {
052                // if all primitive attributes of the object are contained in the ResultSet
053                // the fast direct mapping can be used
054                return super.getObjectFromResultSet();
055            }
056                    // if the full loading failed we assume that at least PK attributes are contained
057                    // in the ResultSet and perform a slower Identity based loading...
058                    // This may of course also fail and can throw another PersistenceBrokerException
059            catch (PersistenceBrokerException e)
060            {
061                Identity oid = getIdentityFromResultSet();
062                return getBroker().getObjectByIdentity(oid);
063            }
064    
065        }
066    }