package org.mandarax.jdbc.server;
/*
* Copyright (C) 1999-2004 <a href="mailto:mandarax@jbdietrich.com">Jens Dietrich</a>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
import java.sql.*;
import org.mandarax.jdbc.*;
import org.mandarax.kernel.InferenceException;
import org.mandarax.kernel.Predicate;
/**
* Result set implementation. All update related features are not supported -
* a UnsupportedFeatureException will be thrown.
* @author <A HREF="mailto:mandarax@jbdietrich.com">Jens Dietrich</A>
* @version 3.3.2 <29 December 2004>
* @since 3.0
*/
class ResultSetImpl extends AbstractReadOnlyResultSet implements PseudoColumns {
private org.mandarax.kernel.ResultSet delegate = null;
private Statement statement = null;
private Predicate predicate = null;
private ResultSetMetaData metaData = null;
private Integer resultCount = null;
/**
* Constructor.
* @param delegate the wrapped mandarax result set
* @param statement the statement (the factory that produced this result set)
* @param predicate the predicate in the result set
*/
ResultSetImpl(org.mandarax.kernel.ResultSet delegate,Statement statement,Predicate predicate) {
super();
this.delegate = delegate;
this.statement = statement;
this.predicate = predicate;
}
/**
* Move cursor to the next position.
* @return boolean
* @throws java.sql.SQLException
*/
public boolean next() throws SQLException {
try{
return delegate.next();
}
catch (InferenceException x) {
throw new JDBCException(x.getMessage(),x);
}
}
/**
* Close the result set.
* @throws java.sql.SQLException
*/
public void close() throws SQLException {
try{
delegate.close();
}
catch (InferenceException x) {
throw new JDBCException(x.getMessage(),x);
}
}
/**
* Return the result set meta data.
* @return java.sql.ResultSetMetaData
* @throws java.sql.SQLException
*/
public ResultSetMetaData getMetaData() throws SQLException {
if (metaData==null) metaData = new ResultSetMetaDataImpl(this,delegate,predicate);
return metaData;
}
/**
* Get the object in the result at a certain index.
* @param columnIndex
* @return java.lang.Object
* @throws java.sql.SQLException
*/
public Object getObject(int columnIndex) throws SQLException {
try{
int realColCount = predicate.getStructure().length;
if (realColCount>=columnIndex) {
// normal slot
String columnName = JDBC2KBUtils.getSlotName(predicate,columnIndex-1); // jdbc indexing starts with 1 !
Class type = predicate.getStructure()[columnIndex-1]; // jdbc indexing starts with 1 !
Object result = delegate.getResult(type,columnName);
if (result==null && columnName.equalsIgnoreCase("COUNT(*)")) return getResultCount();
else return result;
}
else {
// pseudo column
String columnName = PseudoColumns.PSEUDO_COLUMNS[columnIndex-(realColCount+1)];
return getObject(columnName);
}
}
catch (InferenceException x) {
throw new JDBCException(x.getMessage(),x);
}
}
/**
* Get the result for a certain slot (column) name.
* @param columnName
* @return java.lang.Object
* @throws java.sql.SQLException
*/
public Object getObject(String columnName) throws SQLException {
try{
// pseudo columns
if (DERIVATION.equalsIgnoreCase(columnName)) {
return delegate.getProof();
}
// 'normal' columns
int index = JDBC2KBUtils.getSlotNumber(predicate,columnName);
Class type = predicate.getStructure()[index];
Object result = delegate.getResult(type,columnName);
if (result==null && columnName.equalsIgnoreCase("COUNT(*)")) return getResultCount();
else return result;
}
catch (InferenceException x) {
throw new JDBCException(x.getMessage(),x);
}
}
/**
* Find the column (=slot) index.
* @param columnName
* @return int
* @throws java.sql.SQLException
*/
public int findColumn(String columnName) throws SQLException {
return JDBC2KBUtils.getSlotNumber(predicate,columnName);
}
/**
* Retrieves whether the cursor is before the first row in this ResultSet object.
* @return boolean
* @throws java.sql.SQLException
*/
public boolean isBeforeFirst() throws SQLException {
try{
return delegate.isBeforeFirst();
}
catch (InferenceException x) {
throw new JDBCException(x.getMessage(),x);
}
}
/**
* Retrieves whether the cursor is after the last row in this ResultSet object.
* @return boolean
* @throws java.sql.SQLException
*/
public boolean isAfterLast() throws SQLException {
try{
return delegate.isAfterLast();
}
catch (InferenceException x) {
throw new JDBCException(x.getMessage(),x);
}
}
/**
* Retrieves whether the cursor is on the first row of this ResultSet object.
* @return boolean
* @throws java.sql.SQLException
*/
public boolean isFirst() throws SQLException {
try{
return delegate.isFirst();
}
catch (InferenceException x) {
throw new JDBCException(x.getMessage(),x);
}
}
/**
* Retrieves whether the cursor is on the last row of this ResultSet object.
* @return boolean
* @throws java.sql.SQLException
*/
public boolean isLast() throws SQLException {
try{
return delegate.isLast();
}
catch (InferenceException x) {
throw new JDBCException(x.getMessage(),x);
}
}
/**
* Moves the cursor to the front of this ResultSet object,
* just before the first row. This method has no effect if the result set contains no rows.
* @throws java.sql.SQLException
*/
public void beforeFirst() throws SQLException {
try{
delegate.beforeFirst();
}
catch (InferenceException x) {
throw new JDBCException(x.getMessage(),x);
}
}
/**
* Moves the cursor to the end of this ResultSet object,
* just after the last row. This method has no effect if the result set contains no rows.
* @throws java.sql.SQLException
*/
public void afterLast() throws SQLException {
try{
delegate.afterLast();
}
catch (InferenceException x) {
throw new JDBCException(x.getMessage(),x);
}
}
/**
* Moves the cursor to the first row in this ResultSet object.
* @return boolean
* @throws java.sql.SQLException
*/
public boolean first() throws SQLException {
try{
return delegate.first();
}
catch (InferenceException x) {
throw new JDBCException(x.getMessage(),x);
}
}
/**
* Moves the cursor to the last row in this ResultSet object.
* @return boolean
* @throws java.sql.SQLException
*/
public boolean last() throws SQLException {
try{
return delegate.last();
}
catch (InferenceException x) {
throw new JDBCException(x.getMessage(),x);
}
}
/**
* Retrieves the current row number. The first row is number 1, the second number 2, and so on.
* @return int
* @throws java.sql.SQLException
*/
public int getRow() throws SQLException {
try{
return delegate.getResultNumber();
}
catch (InferenceException x) {
throw new JDBCException(x.getMessage(),x);
}
}
/**
* Moves the cursor to the given result number in this ResultSet object.
* @param row
* @return boolean
* @throws java.sql.SQLException
*/
public boolean absolute(int row) throws SQLException {
try{
return delegate.absolute(row);
}
catch (InferenceException x) {
throw new JDBCException(x.getMessage(),x);
}
}
/**
* Moves the cursor a relative number of results, either positive or negative.
* @param rows
* @return boolean
* @throws java.sql.SQLException
*/
public boolean relative(int rows) throws SQLException {
try{
return delegate.relative(rows);
}
catch (InferenceException x) {
throw new JDBCException(x.getMessage(),x);
}
}
/**
* Move the cursor to the previous position.
* @return boolean
* @throws java.sql.SQLException
*/
public boolean previous() throws SQLException {
try{
return delegate.previous();
}
catch (InferenceException x) {
throw new JDBCException(x.getMessage(),x);
}
}
/**
* Get the statement.
* @return java.sql.Statement
* @throws java.sql.SQLException
*/
public Statement getStatement() throws SQLException {
return statement;
}
/**
* Count the results.
* @return a result count
*/
private synchronized Integer getResultCount() throws SQLException {
if (resultCount==null) {
try {
int currentCursorPosition = delegate.getResultNumber();
delegate.last();
resultCount = new Integer(delegate.getResultNumber());
delegate.absolute(currentCursorPosition);
}
catch (InferenceException x) {
throw new JDBCException(x.getMessage(),x);
}
}
return resultCount;
}
}
|