*
* 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.1 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
* USA.
*
* [Java is a trademark or registered trademark of Sun Microsystems, Inc.
* in the United States and other countries.]
*
* ---------------
* SSResultSet.java
* ---------------
* Author: Volker Berlin
*
*/
package smallsql.database;
import java.sql.*;
import java.math.*;
import java.io.InputStream;
import java.io.Reader;
import java.util.Map;
import java.util.Calendar;
import java.net.URL;
public class SSResultSet implements ResultSet {
SSResultSetMetaData metaData = new SSResultSetMetaData();
private CommandSelect cmd;
private boolean wasNull;
SSStatement st;
private boolean isUpdatable;
private boolean isInsertRow;
private ExpressionValue[] values;
SSResultSet( SSStatement st, CommandSelect cmd ){
this.st = st;
metaData.columns = cmd.columnExpressions;
this.cmd = cmd;
isUpdatable = st != null && st.rsConcurrency == CONCUR_UPDATABLE && !cmd.isGroupResult();
}
final private Expression getValue(int columnIndex) throws SQLException{
return metaData.getColumnExpression(columnIndex);
}
public void close() throws SQLException {
st.con.log.println("ResultSet.close");
cmd = null;
}
public boolean wasNull() throws SQLException {
return wasNull;
}
public String getString(int columnIndex) throws SQLException {
try{
String obj = getValue(columnIndex).getString();
wasNull = obj == null;
return obj;
}catch(Exception e){
throw Utils.createSQLException( e );
}
}
public boolean getBoolean(int columnIndex) throws SQLException {
try{
Expression expr = getValue(columnIndex);
wasNull = expr.isNull();
return expr.getBoolean();
}catch(Exception e){
throw Utils.createSQLException( e );
}
}
public byte getByte(int columnIndex) throws SQLException {
return (byte)getInt( columnIndex );
}
public short getShort(int columnIndex) throws SQLException {
return (short)getInt( columnIndex );
}
public int getInt(int columnIndex) throws SQLException {
try{
Expression expr = getValue(columnIndex);
wasNull = expr.isNull();
return expr.getInt();
}catch(Exception e){
throw Utils.createSQLException( e );
}
}
public long getLong(int columnIndex) throws SQLException {
try{
Expression expr = getValue(columnIndex);
wasNull = expr.isNull();
return expr.getLong();
}catch(Exception e){
throw Utils.createSQLException( e );
}
}
public float getFloat(int columnIndex) throws SQLException {
try{
Expression expr = getValue(columnIndex);
wasNull = expr.isNull();
return expr.getFloat();
}catch(Exception e){
throw Utils.createSQLException( e );
}
}
public double getDouble(int columnIndex) throws SQLException {
try{
Expression expr = getValue(columnIndex);
wasNull = expr.isNull();
return expr.getDouble();
}catch(Exception e){
throw Utils.createSQLException( e );
}
}
public BigDecimal getBigDecimal(int columnIndex, int scale) throws SQLException {
try{
MutableNumeric obj = getValue(columnIndex).getNumeric();
if(wasNull = obj == null) return null;
return obj.toBigDecimal(scale);
}catch(Exception e){
throw Utils.createSQLException( e );
}
}
public byte[] getBytes(int columnIndex) throws SQLException {
try{
byte[] obj = getValue(columnIndex).getBytes();
wasNull = obj == null;
return obj;
}catch(Exception e){
throw Utils.createSQLException( e );
}
}
public Date getDate(int columnIndex) throws SQLException {
try{
Expression expr = getValue(columnIndex);
if(wasNull = expr.isNull()) return null;
return DateTime.getDate( expr.getLong() );
}catch(Exception e){
throw Utils.createSQLException( e );
}
}
public Time getTime(int columnIndex) throws SQLException {
try{
Expression expr = getValue(columnIndex);
if(wasNull = expr.isNull()) return null;
return DateTime.getTime( expr.getLong() );
}catch(Exception e){
throw Utils.createSQLException( e );
}
}
public Timestamp getTimestamp(int columnIndex) throws SQLException {
try{
Expression expr = getValue(columnIndex);
if(wasNull = expr.isNull()) return null;
return DateTime.getTimestamp( expr.getLong() );
}catch(Exception e){
throw Utils.createSQLException( e );
}
}
public InputStream getAsciiStream(int columnIndex) throws SQLException {
throw new java.lang.UnsupportedOperationException("Method getAsciiStream() not yet implemented.");
}
public InputStream getUnicodeStream(int columnIndex) throws SQLException {
throw new java.lang.UnsupportedOperationException("Method getUnicodeStream() not yet implemented.");
}
public InputStream getBinaryStream(int columnIndex) throws SQLException {
throw new java.lang.UnsupportedOperationException("Method getBinaryStream() not yet implemented.");
}
public String getString(String columnName) throws SQLException {
return getString( findColumn( columnName ) );
}
public boolean getBoolean(String columnName) throws SQLException {
return getBoolean( findColumn( columnName ) );
}
public byte getByte(String columnName) throws SQLException {
return getByte( findColumn( columnName ) );
}
public short getShort(String columnName) throws SQLException {
return getShort( findColumn( columnName ) );
}
public int getInt(String columnName) throws SQLException {
return getInt( findColumn( columnName ) );
}
public long getLong(String columnName) throws SQLException {
return getLong( findColumn( columnName ) );
}
public float getFloat(String columnName) throws SQLException {
return getFloat( findColumn( columnName ) );
}
public double getDouble(String columnName) throws SQLException {
return getDouble( findColumn( columnName ) );
}
public BigDecimal getBigDecimal(String columnName, int scale) throws SQLException {
return getBigDecimal( findColumn( columnName ), scale );
}
public byte[] getBytes(String columnName) throws SQLException {
return getBytes( findColumn( columnName ) );
}
public Date getDate(String columnName) throws SQLException {
return getDate( findColumn( columnName ) );
}
public Time getTime(String columnName) throws SQLException {
return getTime( findColumn( columnName ) );
}
public Timestamp getTimestamp(String columnName) throws SQLException {
return getTimestamp( findColumn( columnName ) );
}
public InputStream getAsciiStream(String columnName) throws SQLException {
return getAsciiStream( findColumn( columnName ) );
}
public InputStream getUnicodeStream(String columnName) throws SQLException {
return getUnicodeStream( findColumn( columnName ) );
}
public InputStream getBinaryStream(String columnName) throws SQLException {
return getBinaryStream( findColumn( columnName ) );
}
public SQLWarning getWarnings() throws SQLException {
return null;
}
public void clearWarnings() throws SQLException {
}
public String getCursorName() throws SQLException {
throw new java.lang.UnsupportedOperationException("Method getCursorName() not yet implemented.");
}
public ResultSetMetaData getMetaData() throws SQLException {
return metaData;
}
public Object getObject(int columnIndex) throws SQLException {
try{
Object obj = getValue(columnIndex).getApiObject();
wasNull = obj == null;
return obj;
}catch(Exception e){
throw Utils.createSQLException( e );
}
}
public Object getObject(String columnName) throws SQLException {
return getObject( findColumn( columnName ) );
}
public int findColumn(String columnName) throws SQLException {
return getCmd().findColumn(columnName) + 1;
}
public Reader getCharacterStream(int columnIndex) throws SQLException {
throw new java.lang.UnsupportedOperationException("Method getCharacterStream() not yet implemented.");
}
public Reader getCharacterStream(String columnName) throws SQLException {
return getCharacterStream( findColumn( columnName ) );
}
public BigDecimal getBigDecimal(int columnIndex) throws SQLException {
try{
MutableNumeric obj = getValue(columnIndex).getNumeric();
if(wasNull = obj == null) return null;
return obj.toBigDecimal();
}catch(Exception e){
throw Utils.createSQLException( e );
}
}
public BigDecimal getBigDecimal(String columnName) throws SQLException {
return getBigDecimal( findColumn( columnName ) );
}
public boolean isBeforeFirst() throws SQLException {
return getCmd().isBeforeFirst();
}
public boolean isAfterLast() throws SQLException {
return getCmd().isAfterLast();
}
public boolean isFirst() throws SQLException {
return getCmd().isFirst();
}
public boolean isLast() throws SQLException {
try{
return getCmd().isLast();
}catch(Exception e){
throw Utils.createSQLException(e);
}
}
public void beforeFirst() throws SQLException {
try{
getCmd().beforeFirst();
}catch(Exception e){
throw Utils.createSQLException(e);
}
}
public boolean first() throws SQLException {
try{
if(st.rsType == ResultSet.TYPE_FORWARD_ONLY) throw Utils.createSQLException("ResultSet is forward only.");
return getCmd().first();
}catch(Exception e){
throw Utils.createSQLException(e);
}
}
public boolean previous() throws SQLException {
try{
return getCmd().previous();
}catch(Exception e){
throw Utils.createSQLException(e);
}
}
public boolean next() throws SQLException {
try{
return getCmd().next();
}catch(Exception e){
throw Utils.createSQLException(e);
}
}
public boolean last() throws SQLException {
try{
return getCmd().last();
}catch(Exception e){
throw Utils.createSQLException(e);
}
}
public void afterLast() throws SQLException {
try{
if(st.rsType == ResultSet.TYPE_FORWARD_ONLY) throw Utils.createSQLException("ResultSet is forward only.");
getCmd().afterLast();
}catch(Exception e){
throw Utils.createSQLException(e);
}
}
public boolean absolute(int row) throws SQLException {
try{
return getCmd().absolute(row);
}catch(Exception e){
throw Utils.createSQLException(e);
}
}
public boolean relative(int rows) throws SQLException {
try{
return getCmd().relative(rows);
}catch(Exception e){
throw Utils.createSQLException(e);
}
}
public int getRow() throws SQLException {
try{
return getCmd().getRow();
}catch(Exception e){
throw Utils.createSQLException(e);
}
}
public void setFetchDirection(int direction) throws SQLException {
}
public int getFetchDirection() throws SQLException {
return ResultSet.FETCH_UNKNOWN;
}
public void setFetchSize(int rows) throws SQLException {
}
public int getFetchSize() throws SQLException {
return 1;
}
public int getType() throws SQLException {
return getCmd().join.isScrollable() ? ResultSet.TYPE_SCROLL_SENSITIVE : ResultSet.TYPE_FORWARD_ONLY;
}
public int getConcurrency() throws SQLException {
return isUpdatable ? ResultSet.CONCUR_UPDATABLE : ResultSet.CONCUR_READ_ONLY;
}
public boolean rowUpdated() throws SQLException {
return false;
}
public boolean rowInserted() throws SQLException {
return getCmd().join.rowInserted();
}
public boolean rowDeleted() throws SQLException {
return getCmd().join.rowDeleted();
}
public void updateNull(int columnIndex) throws SQLException {
updateValue( columnIndex, null, SQLTokenizer.NULL);
}
public void updateBoolean(int columnIndex, boolean x) throws SQLException {
updateValue( columnIndex, x ? Boolean.TRUE : Boolean.FALSE, SQLTokenizer.BOOLEAN);
}
public void updateByte(int columnIndex, byte x) throws SQLException {
updateValue( columnIndex, Utils.getShort(x), SQLTokenizer.TINYINT);
}
public void updateShort(int columnIndex, short x) throws SQLException {
updateValue( columnIndex, Utils.getShort(x), SQLTokenizer.SMALLINT);
}
public void updateInt(int columnIndex, int x) throws SQLException {
updateValue( columnIndex, Utils.getInteger(x), SQLTokenizer.INT);
}
public void updateLong(int columnIndex, long x) throws SQLException {
updateValue( columnIndex, new Long(x), SQLTokenizer.BIGINT);
}
public void updateFloat(int columnIndex, float x) throws SQLException {
updateValue( columnIndex, new Float(x), SQLTokenizer.REAL);
}
public void updateDouble(int columnIndex, double x) throws SQLException {
updateValue( columnIndex, new Double(x), SQLTokenizer.DOUBLE);
}
public void updateBigDecimal(int columnIndex, BigDecimal x) throws SQLException {
updateValue( columnIndex, x, SQLTokenizer.DECIMAL);
}
public void updateString(int columnIndex, String x) throws SQLException {
updateValue( columnIndex, x, SQLTokenizer.VARCHAR);
}
public void updateBytes(int columnIndex, byte[] x) throws SQLException {
updateValue( columnIndex, x, SQLTokenizer.VARBINARY);
}
public void updateDate(int columnIndex, Date x) throws SQLException {
updateValue( columnIndex, DateTime.valueOf(x), SQLTokenizer.DATE);
}
public void updateTime(int columnIndex, Time x) throws SQLException {
updateValue( columnIndex, DateTime.valueOf(x), SQLTokenizer.TIME);
}
public void updateTimestamp(int columnIndex, Timestamp x) throws SQLException {
updateValue( columnIndex, DateTime.valueOf(x), SQLTokenizer.TIMESTAMP);
}
public void updateAsciiStream(int columnIndex, InputStream x, int length) throws SQLException {
updateValue( columnIndex, x, SQLTokenizer.LONGVARCHAR, length);
}
public void updateBinaryStream(int columnIndex, InputStream x, int length) throws SQLException {
updateValue( columnIndex, x, SQLTokenizer.LONGVARBINARY, length);
}
public void updateCharacterStream(int columnIndex, Reader x, int length) throws SQLException {
throw new java.lang.UnsupportedOperationException("Method updateCharacterStream() not yet implemented.");
}
public void updateObject(int columnIndex, Object x, int scale) throws SQLException {
updateValue( columnIndex, x, -1);
}
public void updateObject(int columnIndex, Object x) throws SQLException {
updateValue( columnIndex, x, -1);
}
public void updateNull(String columnName) throws SQLException {
updateNull( findColumn( columnName ) );
}
public void updateBoolean(String columnName, boolean x) throws SQLException {
updateBoolean( findColumn( columnName ), x );
}
public void updateByte(String columnName, byte x) throws SQLException {
updateByte( findColumn( columnName ), x );
}
public void updateShort(String columnName, short x) throws SQLException {
updateShort( findColumn( columnName ), x );
}
public void updateInt(String columnName, int x) throws SQLException {
updateInt( findColumn( columnName ), x );
}
public void updateLong(String columnName, long x) throws SQLException {
updateLong( findColumn( columnName ), x );
}
public void updateFloat(String columnName, float x) throws SQLException {
updateFloat( findColumn( columnName ), x );
}
public void updateDouble(String columnName, double x) throws SQLException {
updateDouble( findColumn( columnName ), x );
}
public void updateBigDecimal(String columnName, BigDecimal x) throws SQLException {
updateBigDecimal( findColumn( columnName ), x );
}
public void updateString(String columnName, String x) throws SQLException {
updateString( findColumn( columnName ), x );
}
public void updateBytes(String columnName, byte[] x) throws SQLException {
updateBytes( findColumn( columnName ), x );
}
public void updateDate(String columnName, Date x) throws SQLException {
updateDate( findColumn( columnName ), x );
}
public void updateTime(String columnName, Time x) throws SQLException {
updateTime( findColumn( columnName ), x );
}
public void updateTimestamp(String columnName, Timestamp x) throws SQLException {
updateTimestamp( findColumn( columnName ), x );
}
public void updateAsciiStream(String columnName, InputStream x, int length) throws SQLException {
updateAsciiStream( findColumn( columnName ), x, length );
}
public void updateBinaryStream(String columnName, InputStream x, int length) throws SQLException {
updateBinaryStream( findColumn( columnName ), x, length );
}
public void updateCharacterStream(String columnName, Reader x, int length) throws SQLException {
updateCharacterStream( findColumn( columnName ), x, length );
}
public void updateObject(String columnName, Object x, int scale) throws SQLException {
updateObject( findColumn( columnName ), x, scale );
}
public void updateObject(String columnName, Object x) throws SQLException {
updateObject( findColumn( columnName ), x );
}
public void insertRow() throws SQLException {
st.con.log.println("insertRow()");
getCmd().insertRow( st.con, values);
cancelRowUpdates();
}
public void updateRow() throws SQLException {
if(values == null) return;
st.con.log.println("updateRow()");
getCmd().updateRow( st.con, values);
cancelRowUpdates();
}
public void deleteRow() throws SQLException {
st.con.log.println("deleteRow()");
getCmd().deleteRow(st.con);
cancelRowUpdates();
}
public void refreshRow() throws SQLException {
throw new java.lang.UnsupportedOperationException("Method refreshRow() not yet implemented.");
}
public void cancelRowUpdates() throws SQLException {
if(values != null){
for(int i=values.length-1; i>=0; i--){
values[i].clear();
}
}
}
public void moveToInsertRow() throws SQLException {
if(isUpdatable){
isInsertRow = true;
}else{
throw Utils.createSQLException("ResultSet is read only.");
}
}
public void moveToCurrentRow() throws SQLException {
isInsertRow = false;
}
public Statement getStatement() throws SQLException {
return st;
}
public Object getObject(int i, Map map) throws SQLException {
return getObject( i );
}
public Ref getRef(int i) throws SQLException {
throw new java.lang.UnsupportedOperationException("Method getRef() not yet implemented.");
}
public Blob getBlob(int i) throws SQLException {
throw new java.lang.UnsupportedOperationException("Method getBlob() not yet implemented.");
}
public Clob getClob(int i) throws SQLException {
throw new java.lang.UnsupportedOperationException("Method getClob() not yet implemented.");
}
public Array getArray(int i) throws SQLException {
throw new java.lang.UnsupportedOperationException("Method getArray() not yet implemented.");
}
public Object getObject(String columnName, Map map) throws SQLException {
return getObject( columnName );
}
public Ref getRef(String columnName) throws SQLException {
return getRef( findColumn( columnName ) );
}
public Blob getBlob(String columnName) throws SQLException {
return getBlob( findColumn( columnName ) );
}
public Clob getClob(String columnName) throws SQLException {
return getClob( findColumn( columnName ) );
}
public Array getArray(String columnName) throws SQLException {
return getArray( findColumn( columnName ) );
}
public Date getDate(int columnIndex, Calendar cal) throws SQLException {
throw new java.lang.UnsupportedOperationException("Method getDate() not yet implemented.");
}
public Date getDate(String columnName, Calendar cal) throws SQLException {
return getDate( findColumn( columnName ), cal );
}
public Time getTime(int columnIndex, Calendar cal) throws SQLException {
throw new java.lang.UnsupportedOperationException("Method getTime() not yet implemented.");
}
public Time getTime(String columnName, Calendar cal) throws SQLException {
return getTime( findColumn( columnName ), cal );
}
public Timestamp getTimestamp(int columnIndex, Calendar cal) throws SQLException {
throw new java.lang.UnsupportedOperationException("Method getTimestamp() not yet implemented.");
}
public Timestamp getTimestamp(String columnName, Calendar cal) throws SQLException {
return getTimestamp( findColumn( columnName ), cal );
}
public URL getURL(int columnIndex) throws SQLException {
throw new java.lang.UnsupportedOperationException("Method getURL() not yet implemented.");
}
public URL getURL(String columnName) throws SQLException {
return getURL( findColumn( columnName ) );
}
public void updateRef(int columnIndex, Ref x) throws SQLException {
throw new java.lang.UnsupportedOperationException("Method updateRef() not yet implemented.");
}
public void updateRef(String columnName, Ref x) throws SQLException {
updateRef( findColumn( columnName ), x );
}
public void updateBlob(int columnIndex, Blob x) throws SQLException {
throw new java.lang.UnsupportedOperationException("Method updateBlob() not yet implemented.");
}
public void updateBlob(String columnName, Blob x) throws SQLException {
updateBlob( findColumn( columnName ), x );
}
public void updateClob(int columnIndex, Clob x) throws SQLException {
throw new java.lang.UnsupportedOperationException("Method updateClob() not yet implemented.");
}
public void updateClob(String columnName, Clob x) throws SQLException {
updateClob( findColumn( columnName ), x );
}
public void updateArray(int columnIndex, Array x) throws SQLException {
throw new java.lang.UnsupportedOperationException("Method updateArray() not yet implemented.");
}
public void updateArray(String columnName, Array x) throws SQLException {
updateArray( findColumn( columnName ), x );
}
final private ExpressionValue getUpdateValue(int columnIndex) throws SQLException{
if(values == null){
int count = metaData.getColumnCount();
values = new ExpressionValue[count];
while(count-- > 0){
values[count] = new ExpressionValue();
}
}
return values[ metaData.getColumnIdx( columnIndex ) ];
}
final private void updateValue(int columnIndex, Object x, int dataType) throws SQLException{
getUpdateValue( columnIndex ).set( x, dataType );
if(st.con.log.isLogging()){
st.con.log.println("param '"+metaData.getColumnName(columnIndex)+"' = "+x+"; type="+dataType);
}
}
final private void updateValue(int columnIndex, Object x, int dataType, int length) throws SQLException{
getUpdateValue( columnIndex ).set( x, dataType, length );
if(st.con.log.isLogging()){
st.con.log.println("param '"+metaData.getColumnName(columnIndex)+"' = "+x+"; type="+dataType+"; length="+length);
}
}
final private CommandSelect getCmd() throws SQLException {
if(cmd == null) throw Utils.createSQLException("ResultSet is closed.");
return cmd;
}
}