001: /* Copyright (c) 2001-2005, The HSQL Development Group
002: * All rights reserved.
003: *
004: * Redistribution and use in source and binary forms, with or without
005: * modification, are permitted provided that the following conditions are met:
006: *
007: * Redistributions of source code must retain the above copyright notice, this
008: * list of conditions and the following disclaimer.
009: *
010: * Redistributions in binary form must reproduce the above copyright notice,
011: * this list of conditions and the following disclaimer in the documentation
012: * and/or other materials provided with the distribution.
013: *
014: * Neither the name of the HSQL Development Group nor the names of its
015: * contributors may be used to endorse or promote products derived from this
016: * software without specific prior written permission.
017: *
018: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
019: * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
020: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
021: * ARE DISCLAIMED. IN NO EVENT SHALL HSQL DEVELOPMENT GROUP, HSQLDB.ORG,
022: * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
023: * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
024: * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
025: * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
026: * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
027: * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
028: * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
029: */
030:
031: package org.hsqldb.util;
032:
033: import java.sql.ResultSet;
034: import java.sql.SQLException;
035: import java.util.Vector;
036:
037: /**
038: * Helper class for transferring a result set
039: *
040: * @author Nicolas BAZIN
041: * @version 1.7.0
042: */
043: class TransferResultSet {
044:
045: Vector vRows = null;
046: int iRowIdx;
047: int iMaxRowIdx;
048: int iColumnCount;
049: String[] sColumnNames = null;
050: int[] iColumnTypes = null;
051:
052: TransferResultSet(ResultSet r) {
053:
054: iRowIdx = 0;
055: iMaxRowIdx = 0;
056: iColumnCount = 0;
057: vRows = new Vector();
058:
059: try {
060: while (r.next()) {
061: if (sColumnNames == null) {
062: iColumnCount = r.getMetaData().getColumnCount();
063: sColumnNames = new String[iColumnCount + 1];
064: iColumnTypes = new int[iColumnCount + 1];
065:
066: for (int Idx = 0; Idx < iColumnCount; Idx++) {
067: sColumnNames[Idx + 1] = r.getMetaData()
068: .getColumnName(Idx + 1);
069: iColumnTypes[Idx + 1] = r.getMetaData()
070: .getColumnType(Idx + 1);
071: }
072:
073: vRows.addElement(null);
074: }
075:
076: iMaxRowIdx++;
077:
078: Object[] Values = new Object[iColumnCount + 1];
079:
080: for (int Idx = 0; Idx < iColumnCount; Idx++) {
081: Values[Idx + 1] = r.getObject(Idx + 1);
082: }
083:
084: vRows.addElement(Values);
085: }
086: } catch (SQLException SQLE) {
087: iRowIdx = 0;
088: iMaxRowIdx = 0;
089: iColumnCount = 0;
090: vRows = new Vector();
091: }
092: }
093:
094: TransferResultSet() {
095:
096: iRowIdx = 0;
097: iMaxRowIdx = 0;
098: iColumnCount = 0;
099: vRows = new Vector();
100: }
101:
102: void addRow(String[] Name, int[] type, Object[] Values,
103: int nbColumns) throws Exception {
104:
105: if ((Name.length != type.length)
106: || (Name.length != Values.length)
107: || (Name.length != (nbColumns + 1))) {
108: throw new Exception("Size of parameter incoherent");
109: }
110:
111: if (sColumnNames == null) {
112: iColumnCount = nbColumns;
113: sColumnNames = Name;
114: iColumnTypes = type;
115:
116: vRows.addElement(null);
117: }
118:
119: if ((iMaxRowIdx > 0) && (this .getColumnCount() != nbColumns)) {
120: throw new Exception("Wrong number of columns: "
121: + this .getColumnCount() + " column is expected");
122: }
123:
124: iMaxRowIdx++;
125:
126: vRows.addElement(Values);
127: }
128:
129: boolean next() {
130:
131: iRowIdx++;
132:
133: return ((iRowIdx <= iMaxRowIdx) && (iMaxRowIdx > 0));
134: }
135:
136: String getColumnName(int columnIdx) {
137:
138: if ((iMaxRowIdx <= 0) || (iMaxRowIdx < iRowIdx)) {
139: return null;
140: }
141:
142: return sColumnNames[columnIdx];
143: }
144:
145: int getColumnCount() {
146:
147: if ((iMaxRowIdx <= 0) || (iMaxRowIdx < iRowIdx)) {
148: return 0;
149: }
150:
151: return iColumnCount;
152: }
153:
154: int getColumnType(int columnIdx) {
155:
156: if ((iMaxRowIdx <= 0) || (iMaxRowIdx < iRowIdx)) {
157: return 0;
158: }
159:
160: return iColumnTypes[columnIdx];
161: }
162:
163: Object getObject(int columnIdx) {
164:
165: if ((iMaxRowIdx <= 0) || (iMaxRowIdx < iRowIdx)) {
166: return null;
167: }
168:
169: return ((Object[]) vRows.elementAt(iRowIdx))[columnIdx];
170: }
171: }
|