KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > opencrx > kernel > tools > FastResultSet


1 /*
2  * ====================================================================
3  * Project: openmdx, http://www.openmdx.org/
4  * Name: $Id: FastResultSet.java,v 1.1 2005/11/25 17:08:50 wfro Exp $
5  * Description:
6  * Revision: $Revision: 1.1 $
7  * Owner: OMEX AG, Switzerland, http://www.omex.ch
8  * Date: $Date: 2005/11/25 17:08:50 $
9  * ====================================================================
10  *
11  * This software is published under the BSD license
12  * as listed below.
13  *
14  * Copyright (c) 2004, OMEX AG, Switzerland
15  * All rights reserved.
16  *
17  * Redistribution and use in source and binary forms, with or
18  * without modification, are permitted provided that the following
19  * conditions are met:
20  *
21  * * Redistributions of source code must retain the above copyright
22  * notice, this list of conditions and the following disclaimer.
23  *
24  * * Redistributions in binary form must reproduce the above copyright
25  * notice, this list of conditions and the following disclaimer in
26  * the documentation and/or other materials provided with the
27  * distribution.
28  *
29  * * Neither the name of the openMDX team nor the names of its
30  * contributors may be used to endorse or promote products derived
31  * from this software without specific prior written permission.
32  *
33  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
34  * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
35  * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
36  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
37  * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS
38  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
39  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
40  * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
41  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
42  * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
43  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
44  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
45  * POSSIBILITY OF SUCH DAMAGE.
46  *
47  * ------------------
48  *
49  * This product includes software developed by the Apache Software
50  * Foundation (http://www.apache.org/).
51  */

52 package org.opencrx.kernel.tools;
53
54 import java.sql.ResultSet JavaDoc;
55 import java.sql.ResultSetMetaData JavaDoc;
56 import java.sql.SQLException JavaDoc;
57 import java.util.ArrayList JavaDoc;
58 import java.util.HashMap JavaDoc;
59 import java.util.List JavaDoc;
60 import java.util.Map JavaDoc;
61
62 /**
63  * FastResultSet
64  *
65  * Optimizes the access to a ResultSet and guarantees that a column is read
66  * at most once. This is required because for certain jdbc drivers (e.g.
67  * JdbcOdbc driver in .NET) a) rs.getObject() is very slow and b) may be
68  * called at most once.
69  */

70 public class FastResultSet {
71   
72   //-----------------------------------------------------------------------
73
public FastResultSet(
74       ResultSet JavaDoc rs
75   ) throws SQLException JavaDoc {
76       this.rs = rs;
77       this.columnNames = FastResultSet.getColumnNames(
78           rs.getMetaData()
79       );
80   }
81
82   //-----------------------------------------------------------------------
83
public FastResultSet(
84       ResultSet JavaDoc rs,
85       List JavaDoc columnNames
86   ) throws SQLException JavaDoc {
87       this.rs = rs;
88       this.columnNames = columnNames;
89   }
90
91   //-----------------------------------------------------------------------
92
static public List JavaDoc getColumnNames(
93     ResultSetMetaData JavaDoc rsmd
94   ) throws SQLException JavaDoc {
95     List JavaDoc columnNames = new ArrayList JavaDoc();
96     for(int i = 0; i < rsmd.getColumnCount(); i++) {
97       columnNames.add(
98         rsmd.getColumnName(i+1).toLowerCase()
99       );
100     }
101     return columnNames;
102   }
103
104   //-----------------------------------------------------------------------
105
/**
106    * Reads specified column from result set. Guarantees that columns
107    * OBJECT_OID, OBJECT_RID and OBJECT_IDX are read at most once otherwise
108    * error 'ResultSet can not re-read row data for column' is thrown by
109    * certain JDBC drivers.
110    */

111   public Object JavaDoc getObject(
112     String JavaDoc columnName
113   ) throws SQLException JavaDoc {
114     String JavaDoc columnNameLowerCase = columnName.toLowerCase();
115     Object JavaDoc value = this.columnValues.get(columnNameLowerCase);
116     if(value == null) {
117         int index = this.columnNames.indexOf(columnNameLowerCase);
118         if(index < 0) {
119             throw new SQLException JavaDoc("AbstractDatabase_1: column " + columnName + " not found");
120         }
121         // get all column values up to requested index
122
while(this.currentColumnIndex < index) {
123             this.currentColumnIndex++;
124             this.columnValues.put(
125                 this.columnNames.get(this.currentColumnIndex),
126                 value = this.rs.getObject(this.currentColumnIndex+1)
127             );
128         }
129     }
130     return value;
131   }
132   
133   //-----------------------------------------------------------------------
134
public void reset(
135   ) throws SQLException JavaDoc {
136     this.columnValues.clear();
137     this.currentColumnIndex = -1;
138   }
139   
140   //-----------------------------------------------------------------------
141
public boolean next(
142   ) throws SQLException JavaDoc {
143     boolean hasMore = this.rs.next();
144     this.reset();
145     return hasMore;
146   }
147   
148   //-----------------------------------------------------------------------
149
public List JavaDoc getColumnNames(
150   ) {
151     return this.columnNames;
152   }
153   
154   //-----------------------------------------------------------------------
155
// Variables
156
//-----------------------------------------------------------------------
157
private final ResultSet JavaDoc rs;
158   private int currentColumnIndex = -1;
159   private final Map JavaDoc columnValues = new HashMap JavaDoc();
160   private final List JavaDoc columnNames;
161 }
162
Popular Tags