001: package org.mandarax.jdbc;
002:
003: /*
004: * Copyright (C) 1999-2004 <a href="mailto:mandarax@jbdietrich.com">Jens Dietrich</a>
005: *
006: * This library is free software; you can redistribute it and/or
007: * modify it under the terms of the GNU Lesser General Public
008: * License as published by the Free Software Foundation; either
009: * version 2 of the License, or (at your option) any later version.
010: *
011: * This library is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * Lesser General Public License for more details.
015: *
016: * You should have received a copy of the GNU Lesser General Public
017: * License along with this library; if not, write to the Free Software
018: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
019: */
020:
021: import java.sql.*;
022: import java.util.*;
023:
024: /**
025: * Implementation of a static result set - used in meta data.
026: * The static result set consists of a list of columns and a list of list of values.
027: * @author <A HREF="mailto:mandarax@jbdietrich.com">Jens Dietrich</A>
028: * @version 3.3.2 <29 December 2004>
029: * @since 3.0
030: */
031:
032: public class StaticResultSet extends AbstractReadOnlyResultSet {
033: private List columnNames = null;
034: private int[] columnTypes = null;
035: private List values = null;
036: protected int pos = -1;
037:
038: /**
039: * Constructor.
040: * @param columnNames a list of column names
041: * @param values a list of list of values
042: */
043: public StaticResultSet(List columnNames, List values) {
044: super ();
045: this .columnNames = columnNames;
046: this .values = values;
047: }
048:
049: /**
050: * Constructor.
051: * @param columnNames an array of column names
052: * @param values a list of list of values
053: */
054: public StaticResultSet(String[] columnNames, List values) {
055: super ();
056: this .values = values;
057: this .columnNames = Arrays.asList(columnNames);
058: }
059:
060: /**
061: * Constructor.
062: * @param columnNames an array of column names
063: * @param values a list of list of values
064: * @param columnTypes an array of column types (defined in Types)
065: */
066: public StaticResultSet(String[] columnNames, List values,
067: int[] columnTypes) {
068: super ();
069: this .values = values;
070: this .columnNames = Arrays.asList(columnNames);
071: this .columnTypes = columnTypes;
072: }
073:
074: /**
075: * Get an empty result set for an array of columnn names.
076: * @param columnNames an array of column names
077: * @return a result set
078: */
079: public static ResultSet getEmptyResultSet(String[] columnNames) {
080: return new StaticResultSet(columnNames, new ArrayList(0));
081: }
082:
083: /**
084: * Indicates whether the cursor is at the first position.
085: * @return a boolean
086: */
087: public boolean isFirst() throws SQLException {
088: return pos == 0;
089: }
090:
091: /**
092: * Indicates whether the cursor is at the last position.
093: * @return a boolean
094: */
095: public boolean isLast() throws SQLException {
096: return pos == values.size() - 1;
097: }
098:
099: /**
100: * Moves the cursor to the first row in this ResultSet object.
101: * @return true if the cursor is on a valid row; false if there are no rows in the result set
102: */
103: public boolean first() throws SQLException {
104: if (values.size() > 0) {
105: pos = 0;
106: return true;
107: }
108: return false;
109: }
110:
111: /**
112: * Moves the cursor to the last row in this ResultSet object.
113: * @return true if the cursor is on a valid row; false if there are no rows in the results
114: */
115: public boolean last() throws SQLException {
116: if (values.size() > 0) {
117: pos = values.size() - 1;
118: return true;
119: }
120: return false;
121: }
122:
123: /**
124: * Retrieves the current result number. Numbering starts with 1.
125: * @return a number
126: */
127: public int getResultNumber() throws SQLException {
128: return pos == -1 ? -1 : pos + 1;
129: }
130:
131: /**
132: * Moves the cursor down one row from its current position. A ResultSet cursor is initially positioned
133: * before the first row; the first call to the method next makes the first row the current row;
134: * the second call makes the second row the current row, and so on.
135: * @return true if the new current row is valid; false if there are no more rows
136: */
137: public boolean next() throws SQLException {
138: pos = pos + 1;
139: return isValid();
140: }
141:
142: /**
143: * Moves the cursor to the previous row in this ResultSet object.
144: * @return true if the cursor is on a valid row; false if it is off the result set
145: */
146: public boolean previous() throws SQLException {
147: pos = pos - 1;
148: return isValid();
149: };
150:
151: /**
152: * Closes the result set.
153: */
154: public void close() throws SQLException {
155: // nothing to do here
156: }
157:
158: /**
159: * Get the valuesat the current cursor position.
160: * @return a list of values
161: */
162: private List getValues() throws SQLException {
163: if (!isValid())
164: throw new SQLException("Illegal cursor position " + pos
165: + " in result set.");
166: return ((List) values.get(pos));
167: }
168:
169: /**
170: * Get the current position of the cursor.
171: * @return an integer
172: */
173: public int getCursorPosition() throws SQLException {
174: return pos;
175: }
176:
177: /**
178: * Moves the cursor a relative number of results, either positive or negative.
179: * @param offset the number of results
180: * @return true if the cursor is on the result set; false otherwise.
181: */
182: public boolean relative(int offset) throws SQLException {
183: pos = pos + offset;
184: return isValid();
185: }
186:
187: /**
188: * Moves the cursor to the given result number.
189: * @param resultNo the number of results.
190: * @return true if the cursor is on the result set; false otherwise
191: */
192: public boolean absolute(int resultNo) throws SQLException {
193: pos = resultNo - 1; // the number of the first result is 1, not 0 !
194: return isValid();
195: }
196:
197: /**
198: * Moves the cursor to the front of this ResultSet object, just before the first row.
199: * This method has no effect if the result set contains no rows.
200: */
201: public void beforeFirst() throws SQLException {
202: pos = -1;
203: }
204:
205: /**
206: * Moves the cursor to the end of this ResultSet object, just after the last row.
207: * This method has no effect if the result set contains no rows.
208: */
209: public void afterLast() throws SQLException {
210: pos = values.size();
211: }
212:
213: /**
214: * Retrieves whether the cursor is before the first row in this ResultSet object.
215: * @return true if the cursor is before the first row; false if the cursor is at any other position or the result set contains no rows
216: */
217: public boolean isBeforeFirst() throws SQLException {
218: return pos == -1;
219: }
220:
221: /**
222: * Retrieves whether the cursor is after the last result in this ResultSet object.
223: * @return true if the cursor is after the last result; false if the cursor is at any other position or the result set contains no results
224: */
225: public boolean isAfterLast() throws SQLException {
226: return pos == values.size();
227: }
228:
229: /**
230: * Indicates whether the cursor is on a valid position.
231: * @return a boolean
232: */
233: private boolean isValid() {
234: return 0 <= pos && pos < values.size();
235: }
236:
237: /**
238: * Return the result set meta data.
239: * @return java.sql.ResultSetMetaData
240: * @throws java.sql.SQLException
241: */
242: public ResultSetMetaData getMetaData() throws SQLException {
243: return new StaticResultSetMetaData(this );
244: }
245:
246: /**
247: * Retrieves the current row number. The first row is number 1, the second number 2, and so on.
248: * @return int
249: * @throws java.sql.SQLException
250: */
251: public int getRow() throws SQLException {
252: return pos;
253: }
254:
255: /**
256: * Get the object in the result at a certain index.
257: * @param columnIndex
258: * @return java.lang.Object
259: * @throws java.sql.SQLException
260: */
261: public Object getObject(int columnIndex) throws SQLException {
262: return getValues().get(columnIndex - 1);
263: }
264:
265: /**
266: * Find the column (=slot) index.
267: * @param columnName
268: * @return int
269: * @throws java.sql.SQLException
270: * @see java.sql.ResultSet#findColumn(java.lang.String)
271: */
272: public int findColumn(String columnName) throws SQLException {
273: int index = columnNames.indexOf(columnName);
274: if (index == -1)
275: throw new SQLException("Cannot find column " + columnName
276: + " in result set");
277: return index + 1;
278: }
279:
280: /**
281: * Get the result for a certain column name.
282: * @param columnName
283: * @return java.lang.Object
284: * @throws java.sql.SQLException
285: */
286: public Object getObject(String columnName) throws SQLException {
287: // TODO this is not very effective
288: return getObject(findColumn(columnName));
289: }
290:
291: /**
292: * Get the statement.
293: * @return java.sql.Statement
294: * @throws java.sql.SQLException
295: */
296: public Statement getStatement() throws SQLException {
297: return null;
298: }
299:
300: /**
301: * Get the column names
302: * @return List
303: */
304: List getColumnNames() {
305: return columnNames;
306: }
307:
308: /**
309: * Get the column types.
310: * @return int[]
311: */
312: int[] getColumnTypes() {
313: return columnTypes;
314: }
315:
316: // Java 1.5
317: public Object getObject(String colName,
318: Map/*<String,Class<?>>*/map) throws SQLException {
319: return null;
320: }
321: }
|