View Javadoc

1   package org.apache.ojb.broker.accesslayer;
2   
3   /* Copyright 2002-2005 The Apache Software Foundation
4    *
5    * Licensed under the Apache License, Version 2.0 (the "License");
6    * you may not use this file except in compliance with the License.
7    * You may obtain a copy of the License at
8    *
9    *     http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  
18  import java.sql.ResultSetMetaData;
19  import java.sql.SQLException;
20  
21  import org.apache.ojb.broker.PersistenceBrokerException;
22  import org.apache.ojb.broker.core.PersistenceBrokerImpl;
23  import org.apache.ojb.broker.metadata.FieldDescriptor;
24  import org.apache.ojb.broker.metadata.JdbcTypesHelper;
25  import org.apache.ojb.broker.query.ReportQuery;
26  
27  /**
28   * RsIterator for ReportQueries
29   *
30   * @author <a href="mailto:jbraeuchi@gmx.ch">Jakob Braeuchi</a>
31   * @version $Id: ReportQueryRsIterator.java,v 1.1 2007-08-24 22:17:30 ewestfal Exp $
32   */
33  public class ReportQueryRsIterator extends RsIterator
34  {
35  
36      private int m_attributeCount;
37      private int[] m_jdbcTypes;
38      
39      /**
40       * Constructor for ReportQueryRsIterator.
41       */
42      public ReportQueryRsIterator(RsQueryObject queryObject, PersistenceBrokerImpl broker)
43      {
44          super(queryObject, broker);
45          try
46          {
47              // BRJ: use only explicit attributes (columns) ! 
48              // ignore those automatically added for ordering or grouping 
49              ReportQuery q = (ReportQuery)queryObject.getQuery();
50              m_attributeCount = q.getAttributes().length;
51              
52              init_jdbcTypes();
53          }
54          catch (SQLException e)
55          {
56              releaseDbResources();
57              throw new PersistenceBrokerException(e);
58          }
59      }
60  
61      /**
62       * get the jdbcTypes from the Query or the ResultSet if not available from the Query
63       * @throws SQLException
64       */
65      private void init_jdbcTypes() throws SQLException
66      {
67          ReportQuery q = (ReportQuery) getQueryObject().getQuery();
68          m_jdbcTypes = new int[m_attributeCount];
69          
70          // try to get jdbcTypes from Query
71          if (q.getJdbcTypes() != null)
72          {
73              m_jdbcTypes = q.getJdbcTypes();
74          }
75          else
76          {
77              ResultSetMetaData rsMetaData = getRsAndStmt().m_rs.getMetaData();
78              for (int i = 0; i < m_attributeCount; i++)
79              {
80                  m_jdbcTypes[i] = rsMetaData.getColumnType(i + 1);
81              }
82              
83          }
84      }
85      
86      
87      /**
88       * returns an Object[] representing the columns of the current ResultSet
89       * row. There is no OJB object materialization, Proxy generation etc.
90       * involved to maximize performance.
91       */
92      protected Object getObjectFromResultSet() throws PersistenceBrokerException
93      {
94          Object[] result = new Object[m_attributeCount];
95          ReportQuery q =(ReportQuery) getQueryObject().getQuery();
96  
97          for (int i = 0; i < m_attributeCount; i++)
98          {
99              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 }