KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > scriptella > jdbc > ResultSetAdapter


1 /*
2  * Copyright 2006-2007 The Scriptella Project Team.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package scriptella.jdbc;
17
18 import scriptella.spi.ParametersCallback;
19 import scriptella.util.ColumnsMap;
20 import scriptella.util.IOUtils;
21
22 import java.io.Closeable JavaDoc;
23 import java.sql.ResultSet JavaDoc;
24 import java.sql.ResultSetMetaData JavaDoc;
25 import java.sql.SQLException JavaDoc;
26
27
28 /**
29  * Represents SQL query result set as {@link ParametersCallback}.
30  * <p>This class exposes pseudo column <code>rownum</code> -current row number starting at 1.
31  *
32  * @author Fyodor Kupolov
33  * @version 1.0
34  */

35 public class ResultSetAdapter implements ParametersCallback, Closeable JavaDoc {
36
37     private ResultSet JavaDoc resultSet;
38     private ColumnsMap columnsMap;
39     private ParametersCallback params; //parent parameters callback to use
40
private JdbcTypesConverter converter;
41     private int columnsCount;
42     private int[] jdbcTypes;
43
44     /**
45      * Instantiates an adapter, prepares a cache and builds a map of column names.
46      *
47      * @param resultSet resultset to adapt.
48      * @param parametersCallback parent parameter callback.
49      * @param converter type converter to use for getting column values as object.
50      */

51     public ResultSetAdapter(ResultSet JavaDoc resultSet,
52                             ParametersCallback parametersCallback, JdbcTypesConverter converter) {
53         this.params = parametersCallback;
54         this.resultSet = resultSet;
55         this.converter = converter;
56     }
57
58     private void initMetaData() {
59         columnsMap = new ColumnsMap();
60         try {
61             final ResultSetMetaData JavaDoc m = resultSet.getMetaData();
62             columnsCount = m.getColumnCount();
63             jdbcTypes = new int[columnsCount];
64             for (int i = 1; i <= columnsCount; i++) {
65                 columnsMap.registerColumn(m.getColumnName(i), i);
66                 jdbcTypes[i-1]=m.getColumnType(i); //Store column types for converter
67
}
68         } catch (SQLException JavaDoc e) {
69             throw new JdbcException("Unable to process result set ", e);
70         }
71     }
72
73     /**
74      * @return true if the new current row is valid; false if there are no more rows
75      * @see java.sql.ResultSet#next()
76      */

77     public boolean next() {
78         try {
79             return resultSet.next();
80         } catch (SQLException JavaDoc e) {
81             throw new JdbcException("Unable to move cursor to the next row", e);
82         }
83     }
84
85
86     public Object JavaDoc getParameter(final String JavaDoc name) {
87         if (columnsMap==null) { //if first time access
88
initMetaData();
89         }
90         try {
91             Integer JavaDoc index = columnsMap.find(name);
92             int ind = index==null?-1:index - 1;
93             if (ind >= 0 && ind < columnsCount) { //if index found and in range
94
return converter.getObject(resultSet, ind + 1, jdbcTypes[ind]);
95             } else { //otherwise call uppper level params
96
return params.getParameter(name);
97             }
98
99         } catch (SQLException JavaDoc e) {
100             throw new JdbcException("Unable to get parameter " + name, e);
101         }
102     }
103
104     /**
105      * Closes the underlying resultset.
106      * <p>This method should operate without raising exceptions.
107      */

108     public void close() {
109         if (resultSet != null) {
110             JdbcUtils.closeSilent(resultSet);
111             IOUtils.closeSilently(converter);
112             resultSet = null;
113             params = null;
114             columnsMap = null;
115             jdbcTypes=null;
116         }
117     }
118 }
119
Popular Tags