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 java.sql.ResultSetMetaData;
019 import java.sql.SQLException;
020
021 import org.apache.ojb.broker.PersistenceBrokerException;
022 import org.apache.ojb.broker.core.PersistenceBrokerImpl;
023 import org.apache.ojb.broker.metadata.FieldDescriptor;
024 import org.apache.ojb.broker.metadata.JdbcTypesHelper;
025 import org.apache.ojb.broker.query.ReportQuery;
026
027 /**
028 * RsIterator for ReportQueries
029 *
030 * @author <a href="mailto:jbraeuchi@gmx.ch">Jakob Braeuchi</a>
031 * @version $Id: ReportQueryRsIterator.java,v 1.1 2007-08-24 22:17:30 ewestfal Exp $
032 */
033 public class ReportQueryRsIterator extends RsIterator
034 {
035
036 private int m_attributeCount;
037 private int[] m_jdbcTypes;
038
039 /**
040 * Constructor for ReportQueryRsIterator.
041 */
042 public ReportQueryRsIterator(RsQueryObject queryObject, PersistenceBrokerImpl broker)
043 {
044 super(queryObject, broker);
045 try
046 {
047 // BRJ: use only explicit attributes (columns) !
048 // ignore those automatically added for ordering or grouping
049 ReportQuery q = (ReportQuery)queryObject.getQuery();
050 m_attributeCount = q.getAttributes().length;
051
052 init_jdbcTypes();
053 }
054 catch (SQLException e)
055 {
056 releaseDbResources();
057 throw new PersistenceBrokerException(e);
058 }
059 }
060
061 /**
062 * get the jdbcTypes from the Query or the ResultSet if not available from the Query
063 * @throws SQLException
064 */
065 private void init_jdbcTypes() throws SQLException
066 {
067 ReportQuery q = (ReportQuery) getQueryObject().getQuery();
068 m_jdbcTypes = new int[m_attributeCount];
069
070 // try to get jdbcTypes from Query
071 if (q.getJdbcTypes() != null)
072 {
073 m_jdbcTypes = q.getJdbcTypes();
074 }
075 else
076 {
077 ResultSetMetaData rsMetaData = getRsAndStmt().m_rs.getMetaData();
078 for (int i = 0; i < m_attributeCount; i++)
079 {
080 m_jdbcTypes[i] = rsMetaData.getColumnType(i + 1);
081 }
082
083 }
084 }
085
086
087 /**
088 * returns an Object[] representing the columns of the current ResultSet
089 * row. There is no OJB object materialization, Proxy generation etc.
090 * involved to maximize performance.
091 */
092 protected Object getObjectFromResultSet() throws PersistenceBrokerException
093 {
094 Object[] result = new Object[m_attributeCount];
095 ReportQuery q =(ReportQuery) getQueryObject().getQuery();
096
097 for (int i = 0; i < m_attributeCount; i++)
098 {
099 try
100 {
101 int jdbcType = m_jdbcTypes[i];
102 String attr = q.getAttributes()[i];
103 FieldDescriptor fld = (FieldDescriptor) q.getAttributeFieldDescriptors().get(attr);
104 Object val =JdbcTypesHelper.getObjectFromColumn(getRsAndStmt().m_rs, new Integer(jdbcType), i + 1);
105
106 if (fld != null && fld.getFieldConversion() != null)
107 {
108 val = fld.getFieldConversion().sqlToJava(val);
109 }
110 result[i] = val;
111 }
112 catch (SQLException e)
113 {
114 throw new PersistenceBrokerException(e);
115 }
116 }
117 return result;
118 }
119
120
121 }