Source code: org/hsqldb/util/TransferResultSet.java
1 /* Copyright (c) 2001-2002, The HSQL Development Group
2 * All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are met:
6 *
7 * Redistributions of source code must retain the above copyright notice, this
8 * list of conditions and the following disclaimer.
9 *
10 * Redistributions in binary form must reproduce the above copyright notice,
11 * this list of conditions and the following disclaimer in the documentation
12 * and/or other materials provided with the distribution.
13 *
14 * Neither the name of the HSQL Development Group nor the names of its
15 * contributors may be used to endorse or promote products derived from this
16 * software without specific prior written permission.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 * ARE DISCLAIMED. IN NO EVENT SHALL HSQL DEVELOPMENT GROUP, HSQLDB.ORG,
22 * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
23 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
24 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
25 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
26 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
28 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 */
30
31
32 package org.hsqldb.util;
33
34 import java.sql.*;
35 import java.util.*;
36
37 /**
38 * Helper class for transferring a result set
39 *
40 * @author Nicolas BAZIN
41 * @version 1.7.0
42 */
43 class TransferResultSet {
44
45 Vector vRows = null;
46 int iRowIdx;
47 int iMaxRowIdx;
48 int iColumnCount;
49 String[] sColumnNames = null;
50 int[] iColumnTypes = null;
51
52 TransferResultSet(ResultSet r) {
53
54 iRowIdx = 0;
55 iMaxRowIdx = 0;
56 iColumnCount = 0;
57 vRows = new Vector();
58
59 try {
60 while (r.next()) {
61 if (sColumnNames == null) {
62 iColumnCount = r.getMetaData().getColumnCount();
63 sColumnNames = new String[iColumnCount + 1];
64 iColumnTypes = new int[iColumnCount + 1];
65
66 for (int Idx = 0; Idx < iColumnCount; Idx++) {
67 sColumnNames[Idx + 1] =
68 r.getMetaData().getColumnName(Idx + 1);
69 iColumnTypes[Idx + 1] =
70 r.getMetaData().getColumnType(Idx + 1);
71 }
72
73 vRows.addElement(null);
74 }
75
76 iMaxRowIdx++;
77
78 Object[] Values = new Object[iColumnCount + 1];
79
80 for (int Idx = 0; Idx < iColumnCount; Idx++) {
81 Values[Idx + 1] = r.getObject(Idx + 1);
82 }
83
84 vRows.addElement(Values);
85 }
86 } catch (SQLException SQLE) {
87 iRowIdx = 0;
88 iMaxRowIdx = 0;
89 iColumnCount = 0;
90 vRows = new Vector();
91 }
92 }
93
94 TransferResultSet() {
95
96 iRowIdx = 0;
97 iMaxRowIdx = 0;
98 iColumnCount = 0;
99 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) || (Name.length != Values.length)
106 || (Name.length != (nbColumns + 1))) {
107 throw new Exception("Size of parameter incoherent");
108 }
109
110 if (sColumnNames == null) {
111 iColumnCount = nbColumns;
112 sColumnNames = Name;
113 iColumnTypes = type;
114
115 vRows.addElement(null);
116 }
117
118 if ((iMaxRowIdx > 0) && (this.getColumnCount() != nbColumns)) {
119 throw new Exception("Wrong number of columns: "
120 + this.getColumnCount()
121 + " 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 }