001: package org.mandarax.jdbc.server;
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:
023: import org.mandarax.jdbc.*;
024: import org.mandarax.kernel.InferenceException;
025: import org.mandarax.kernel.Predicate;
026:
027: /**
028: * Result set implementation. All update related features are not supported -
029: * a UnsupportedFeatureException will be thrown.
030: * @author <A HREF="mailto:mandarax@jbdietrich.com">Jens Dietrich</A>
031: * @version 3.3.2 <29 December 2004>
032: * @since 3.0
033: */
034:
035: class ResultSetImpl extends AbstractReadOnlyResultSet implements
036: PseudoColumns {
037:
038: private org.mandarax.kernel.ResultSet delegate = null;
039: private Statement statement = null;
040: private Predicate predicate = null;
041: private ResultSetMetaData metaData = null;
042: private Integer resultCount = null;
043:
044: /**
045: * Constructor.
046: * @param delegate the wrapped mandarax result set
047: * @param statement the statement (the factory that produced this result set)
048: * @param predicate the predicate in the result set
049: */
050: ResultSetImpl(org.mandarax.kernel.ResultSet delegate,
051: Statement statement, Predicate predicate) {
052: super ();
053: this .delegate = delegate;
054: this .statement = statement;
055: this .predicate = predicate;
056: }
057:
058: /**
059: * Move cursor to the next position.
060: * @return boolean
061: * @throws java.sql.SQLException
062: */
063: public boolean next() throws SQLException {
064: try {
065: return delegate.next();
066: } catch (InferenceException x) {
067: throw new JDBCException(x.getMessage(), x);
068: }
069: }
070:
071: /**
072: * Close the result set.
073: * @throws java.sql.SQLException
074: */
075: public void close() throws SQLException {
076: try {
077: delegate.close();
078: } catch (InferenceException x) {
079: throw new JDBCException(x.getMessage(), x);
080: }
081: }
082:
083: /**
084: * Return the result set meta data.
085: * @return java.sql.ResultSetMetaData
086: * @throws java.sql.SQLException
087: */
088: public ResultSetMetaData getMetaData() throws SQLException {
089: if (metaData == null)
090: metaData = new ResultSetMetaDataImpl(this , delegate,
091: predicate);
092: return metaData;
093: }
094:
095: /**
096: * Get the object in the result at a certain index.
097: * @param columnIndex
098: * @return java.lang.Object
099: * @throws java.sql.SQLException
100: */
101: public Object getObject(int columnIndex) throws SQLException {
102: try {
103: int realColCount = predicate.getStructure().length;
104: if (realColCount >= columnIndex) {
105: // normal slot
106: String columnName = JDBC2KBUtils.getSlotName(predicate,
107: columnIndex - 1); // jdbc indexing starts with 1 !
108: Class type = predicate.getStructure()[columnIndex - 1]; // jdbc indexing starts with 1 !
109: Object result = delegate.getResult(type, columnName);
110: if (result == null
111: && columnName.equalsIgnoreCase("COUNT(*)"))
112: return getResultCount();
113: else
114: return result;
115: } else {
116: // pseudo column
117: String columnName = PseudoColumns.PSEUDO_COLUMNS[columnIndex
118: - (realColCount + 1)];
119: return getObject(columnName);
120: }
121: } catch (InferenceException x) {
122: throw new JDBCException(x.getMessage(), x);
123: }
124: }
125:
126: /**
127: * Get the result for a certain slot (column) name.
128: * @param columnName
129: * @return java.lang.Object
130: * @throws java.sql.SQLException
131: */
132: public Object getObject(String columnName) throws SQLException {
133: try {
134: // pseudo columns
135: if (DERIVATION.equalsIgnoreCase(columnName)) {
136: return delegate.getProof();
137: }
138: // 'normal' columns
139: int index = JDBC2KBUtils.getSlotNumber(predicate,
140: columnName);
141: Class type = predicate.getStructure()[index];
142: Object result = delegate.getResult(type, columnName);
143: if (result == null
144: && columnName.equalsIgnoreCase("COUNT(*)"))
145: return getResultCount();
146: else
147: return result;
148: } catch (InferenceException x) {
149: throw new JDBCException(x.getMessage(), x);
150: }
151: }
152:
153: /**
154: * Find the column (=slot) index.
155: * @param columnName
156: * @return int
157: * @throws java.sql.SQLException
158: */
159: public int findColumn(String columnName) throws SQLException {
160: return JDBC2KBUtils.getSlotNumber(predicate, columnName);
161: }
162:
163: /**
164: * Retrieves whether the cursor is before the first row in this ResultSet object.
165: * @return boolean
166: * @throws java.sql.SQLException
167: */
168: public boolean isBeforeFirst() throws SQLException {
169: try {
170: return delegate.isBeforeFirst();
171: } catch (InferenceException x) {
172: throw new JDBCException(x.getMessage(), x);
173: }
174: }
175:
176: /**
177: * Retrieves whether the cursor is after the last row in this ResultSet object.
178: * @return boolean
179: * @throws java.sql.SQLException
180: */
181: public boolean isAfterLast() throws SQLException {
182: try {
183: return delegate.isAfterLast();
184: } catch (InferenceException x) {
185: throw new JDBCException(x.getMessage(), x);
186: }
187: }
188:
189: /**
190: * Retrieves whether the cursor is on the first row of this ResultSet object.
191: * @return boolean
192: * @throws java.sql.SQLException
193: */
194: public boolean isFirst() throws SQLException {
195: try {
196: return delegate.isFirst();
197: } catch (InferenceException x) {
198: throw new JDBCException(x.getMessage(), x);
199: }
200: }
201:
202: /**
203: * Retrieves whether the cursor is on the last row of this ResultSet object.
204: * @return boolean
205: * @throws java.sql.SQLException
206: */
207: public boolean isLast() throws SQLException {
208: try {
209: return delegate.isLast();
210: } catch (InferenceException x) {
211: throw new JDBCException(x.getMessage(), x);
212: }
213: }
214:
215: /**
216: * Moves the cursor to the front of this ResultSet object,
217: * just before the first row. This method has no effect if the result set contains no rows.
218: * @throws java.sql.SQLException
219: */
220: public void beforeFirst() throws SQLException {
221: try {
222: delegate.beforeFirst();
223: } catch (InferenceException x) {
224: throw new JDBCException(x.getMessage(), x);
225: }
226: }
227:
228: /**
229: * Moves the cursor to the end of this ResultSet object,
230: * just after the last row. This method has no effect if the result set contains no rows.
231: * @throws java.sql.SQLException
232: */
233: public void afterLast() throws SQLException {
234: try {
235: delegate.afterLast();
236: } catch (InferenceException x) {
237: throw new JDBCException(x.getMessage(), x);
238: }
239: }
240:
241: /**
242: * Moves the cursor to the first row in this ResultSet object.
243: * @return boolean
244: * @throws java.sql.SQLException
245: */
246: public boolean first() throws SQLException {
247: try {
248: return delegate.first();
249: } catch (InferenceException x) {
250: throw new JDBCException(x.getMessage(), x);
251: }
252: }
253:
254: /**
255: * Moves the cursor to the last row in this ResultSet object.
256: * @return boolean
257: * @throws java.sql.SQLException
258: */
259: public boolean last() throws SQLException {
260: try {
261: return delegate.last();
262: } catch (InferenceException x) {
263: throw new JDBCException(x.getMessage(), x);
264: }
265: }
266:
267: /**
268: * Retrieves the current row number. The first row is number 1, the second number 2, and so on.
269: * @return int
270: * @throws java.sql.SQLException
271: */
272: public int getRow() throws SQLException {
273: try {
274: return delegate.getResultNumber();
275: } catch (InferenceException x) {
276: throw new JDBCException(x.getMessage(), x);
277: }
278: }
279:
280: /**
281: * Moves the cursor to the given result number in this ResultSet object.
282: * @param row
283: * @return boolean
284: * @throws java.sql.SQLException
285: */
286: public boolean absolute(int row) throws SQLException {
287: try {
288: return delegate.absolute(row);
289: } catch (InferenceException x) {
290: throw new JDBCException(x.getMessage(), x);
291: }
292: }
293:
294: /**
295: * Moves the cursor a relative number of results, either positive or negative.
296: * @param rows
297: * @return boolean
298: * @throws java.sql.SQLException
299: */
300: public boolean relative(int rows) throws SQLException {
301: try {
302: return delegate.relative(rows);
303: } catch (InferenceException x) {
304: throw new JDBCException(x.getMessage(), x);
305: }
306: }
307:
308: /**
309: * Move the cursor to the previous position.
310: * @return boolean
311: * @throws java.sql.SQLException
312: */
313: public boolean previous() throws SQLException {
314: try {
315: return delegate.previous();
316: } catch (InferenceException x) {
317: throw new JDBCException(x.getMessage(), x);
318: }
319: }
320:
321: /**
322: * Get the statement.
323: * @return java.sql.Statement
324: * @throws java.sql.SQLException
325: */
326: public Statement getStatement() throws SQLException {
327: return statement;
328: }
329:
330: /**
331: * Count the results.
332: * @return a result count
333: */
334: private synchronized Integer getResultCount() throws SQLException {
335: if (resultCount == null) {
336: try {
337: int currentCursorPosition = delegate.getResultNumber();
338: delegate.last();
339: resultCount = new Integer(delegate.getResultNumber());
340: delegate.absolute(currentCursorPosition);
341: } catch (InferenceException x) {
342: throw new JDBCException(x.getMessage(), x);
343: }
344: }
345: return resultCount;
346: }
347: }
|