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.13.2.3 2005/12/21 22:22:58 tomdz Exp $
032: */
033: public class ReportQueryRsIterator extends RsIterator {
034:
035: private int m_attributeCount;
036: private int[] m_jdbcTypes;
037:
038: /**
039: * Constructor for ReportQueryRsIterator.
040: */
041: public ReportQueryRsIterator(RsQueryObject queryObject,
042: PersistenceBrokerImpl broker) {
043: super (queryObject, broker);
044: try {
045: // BRJ: use only explicit attributes (columns) !
046: // ignore those automatically added for ordering or grouping
047: ReportQuery q = (ReportQuery) queryObject.getQuery();
048: m_attributeCount = q.getAttributes().length;
049:
050: init_jdbcTypes();
051: } catch (SQLException e) {
052: releaseDbResources();
053: throw new PersistenceBrokerException(e);
054: }
055: }
056:
057: /**
058: * get the jdbcTypes from the Query or the ResultSet if not available from the Query
059: * @throws SQLException
060: */
061: private void init_jdbcTypes() throws SQLException {
062: ReportQuery q = (ReportQuery) getQueryObject().getQuery();
063: m_jdbcTypes = new int[m_attributeCount];
064:
065: // try to get jdbcTypes from Query
066: if (q.getJdbcTypes() != null) {
067: m_jdbcTypes = q.getJdbcTypes();
068: } else {
069: ResultSetMetaData rsMetaData = getRsAndStmt().m_rs
070: .getMetaData();
071: for (int i = 0; i < m_attributeCount; i++) {
072: m_jdbcTypes[i] = rsMetaData.getColumnType(i + 1);
073: }
074:
075: }
076: }
077:
078: /**
079: * returns an Object[] representing the columns of the current ResultSet
080: * row. There is no OJB object materialization, Proxy generation etc.
081: * involved to maximize performance.
082: */
083: protected Object getObjectFromResultSet()
084: throws PersistenceBrokerException {
085: Object[] result = new Object[m_attributeCount];
086: ReportQuery q = (ReportQuery) getQueryObject().getQuery();
087:
088: for (int i = 0; i < m_attributeCount; i++) {
089: try {
090: int jdbcType = m_jdbcTypes[i];
091: String attr = q.getAttributes()[i];
092: FieldDescriptor fld = (FieldDescriptor) q
093: .getAttributeFieldDescriptors().get(attr);
094: Object val = JdbcTypesHelper.getObjectFromColumn(
095: getRsAndStmt().m_rs, new Integer(jdbcType),
096: i + 1);
097:
098: if (fld != null && fld.getFieldConversion() != null) {
099: val = fld.getFieldConversion().sqlToJava(val);
100: }
101: result[i] = val;
102: } catch (SQLException e) {
103: throw new PersistenceBrokerException(e);
104: }
105: }
106: return result;
107: }
108:
109: }
|