package net.sf.soapjdbc;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.Reader;
import java.io.StringReader;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.net.MalformedURLException;
import java.net.URL;
import java.sql.Array;
import java.sql.Blob;
import java.sql.Clob;
import java.sql.Date;
import java.sql.Ref;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.SQLWarning;
import java.sql.Statement;
import java.sql.Time;
import java.sql.Timestamp;
import java.sql.Types;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Map;
import java.util.TreeMap;
import javax.sql.rowset.serial.SerialBlob;
import javax.sql.rowset.serial.SerialClob;
public class SoapJdbcResultSet implements ResultSet {
private boolean mIsClosed = false;
private boolean mWasNullFlag;
private SoapJdbcResultSetRowData mRowData;
private SoapJdbcResultSetMetaData mMetaData;
private int mFetchSize;
private int mFetchDirection;
private ArrayList<InputStream> mStreams;
private Statement mCurrentStatement;
private TreeMap<String, Integer> mMapping;
public SoapJdbcResultSet(SoapJdbcResultSetMetaData metaData, SoapJdbcResultSetRowData rowData) {
mRowData = rowData;
mMetaData = metaData;
mStreams = new ArrayList<InputStream>();
}
public boolean next() throws SQLException {
checkIfClosed();
for (InputStream stream : mStreams) {
try {
stream.close();
} catch (IOException e) {
// we can ignore this message
}
}
mStreams.clear();
if (mRowData.hasNext()) {
mRowData.next();
return true;
}
return false;
}
private void checkIfClosed() throws SQLException {
if (mIsClosed) {
throw new SQLException("ResultSet is closed", SoapJdbcSQLError.SQL_STATE_GENERAL_ERROR);
} // if
}
private void checkColumnBounds(int columnIndex) throws SQLException {
if (columnIndex < 1 || columnIndex > mMetaData.getColumnCount()) {
throw new SQLException("Column " + columnIndex + "out of range: " + mMetaData.getColumnCount(),
SoapJdbcSQLError.SQL_STATE_ILLEGAL_ARGUMENT);
}
}
public void close() {
mIsClosed = true;
}
public boolean wasNull() throws SQLException {
checkIfClosed();
return mWasNullFlag;
}
public String getString(int columnIndex) throws SQLException {
Object value = getValue(columnIndex);
return value == null ? null : value.toString();
}
private Object getValue(int columnIndex) throws SQLException {
checkIfClosed();
checkColumnBounds(columnIndex);
Object[] currentRow = mRowData.getCurrentRow();
Object value = currentRow[columnIndex - 1];
mWasNullFlag = value == null;
return value;
}
public boolean getBoolean(int columnIndex) throws SQLException {
return getInt(columnIndex) == 1;
}
public byte getByte(int columnIndex) throws SQLException {
Object value = getValue(columnIndex);
try {
return value == null ? 0 : Byte.parseByte(value.toString());
} catch (NumberFormatException nfe) {
throw new SQLException("out of range", SoapJdbcSQLError.SQL_STATE_ILLEGAL_ARGUMENT);
} // try
}
public short getShort(int columnIndex) throws SQLException {
Object value = getValue(columnIndex);
try {
return value == null ? 0 : Short.parseShort(value.toString());
} catch (NumberFormatException nfe) {
throw new SQLException("out of range", SoapJdbcSQLError.SQL_STATE_ILLEGAL_ARGUMENT);
} // try
}
public int getInt(int columnIndex) throws SQLException {
Object value = getValue(columnIndex);
try {
return value == null ? 0 : Integer.parseInt(value.toString());
} catch (NumberFormatException nfe) {
throw new SQLException("out of range", SoapJdbcSQLError.SQL_STATE_ILLEGAL_ARGUMENT);
} // try
}
public long getLong(int columnIndex) throws SQLException {
Object value = getValue(columnIndex);
try {
return value == null ? 0 : Long.parseLong(value.toString());
} catch (NumberFormatException nfe) {
throw new SQLException("out of range", SoapJdbcSQLError.SQL_STATE_ILLEGAL_ARGUMENT);
} // try
}
public float getFloat(int columnIndex) throws SQLException {
Object value = getValue(columnIndex);
try {
return value == null ? 0 : Float.parseFloat(value.toString());
} catch (NumberFormatException nfe) {
throw new SQLException("out of range", SoapJdbcSQLError.SQL_STATE_ILLEGAL_ARGUMENT);
} // try
}
public double getDouble(int columnIndex) throws SQLException {
Object value = getValue(columnIndex);
try {
return value == null ? 0 : Double.parseDouble(value.toString());
} catch (NumberFormatException nfe) {
throw new SQLException("out of range", SoapJdbcSQLError.SQL_STATE_ILLEGAL_ARGUMENT);
} // try
}
public BigDecimal getBigDecimal(int columnIndex, int scale) throws SQLException {
String value = getString(columnIndex);
if (value == null) {
return null;
}
BigDecimal bigDecimal;
try {
bigDecimal = new BigDecimal(value);
} catch (NumberFormatException nfe) {
throw new SQLException("Bad format for BigDecimal: " + value + " in column: " + columnIndex, SoapJdbcSQLError.SQL_STATE_ILLEGAL_ARGUMENT);
} // try
try {
return bigDecimal.setScale(scale);
} catch (ArithmeticException ex) {
try {
return bigDecimal.setScale(scale, BigDecimal.ROUND_HALF_UP);
} catch (ArithmeticException arEx) {
throw new SQLException("Bad format for BigDecimal: " + value + " in column: " + columnIndex,
SoapJdbcSQLError.SQL_STATE_ILLEGAL_ARGUMENT);
}
}
}
public byte[] getBytes(int columnIndex) throws SQLException {
String value = getString(columnIndex);
return value == null ? null : value.getBytes();
}
public Date getDate(int columnIndex) throws SQLException {
String value = getString(columnIndex);
try {
return value == null ? null : new Date(DateFormat.getDateInstance().parse(value).getTime());
} catch (ParseException e) {
throw new SQLException("Bad format for Date: " + value + " in column: " + columnIndex, SoapJdbcSQLError.SQL_STATE_ILLEGAL_ARGUMENT);
} // try
}
public Time getTime(int columnIndex) throws SQLException {
String value = getString(columnIndex);
try {
return value == null ? null : new Time(DateFormat.getDateInstance().parse(value).getTime());
} catch (ParseException e) {
throw new SQLException("Bad format for Time: " + value + " in column: " + columnIndex, SoapJdbcSQLError.SQL_STATE_ILLEGAL_ARGUMENT);
} // try
}
public Timestamp getTimestamp(int columnIndex) throws SQLException {
String value = getString(columnIndex);
try {
DateFormat f = new SimpleDateFormat("yyyy-MM-DD");
return value == null ? null : new Timestamp(f.parse(value).getTime());
} catch (ParseException e) {
throw new SQLException("Bad format for Timestamp: " + value + " in column: " + columnIndex, SoapJdbcSQLError.SQL_STATE_ILLEGAL_ARGUMENT);
} // try
}
public InputStream getAsciiStream(int columnIndex) throws SQLException {
return getBinaryStream(columnIndex);
}
public InputStream getUnicodeStream(int columnIndex) throws SQLException {
return getBinaryStream(columnIndex);
}
public InputStream getBinaryStream(int columnIndex) throws SQLException {
byte[] value = getBytes(columnIndex);
if (value == null) {
return null;
} // if
ByteArrayInputStream stream = new ByteArrayInputStream(value);
mStreams.add(stream);
return stream;
}
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() {
return null;
} // getWarnings
public void clearWarnings() {
// do nothing
} // lcearWarnings
public String getCursorName() throws SQLException {
throw new NotUpdatableException();
}
public SoapJdbcResultSetMetaData getMetaData() {
return mMetaData;
}
public Object getObject(int columnIndex) throws SQLException {
Object value = getValue(columnIndex);
if (value == null) {
return null;
}
switch (mMetaData.getColumnType(columnIndex)) {
case Types.BIT:
case Types.BOOLEAN:
return Boolean.valueOf(value.toString());
case Types.TINYINT:
case Types.SMALLINT:
case Types.INTEGER:
try {
return Integer.valueOf(value.toString());
} catch (NumberFormatException nfe) {
throw new SQLException("Bad format for BigInteger: " + value + " in column: " + columnIndex,
SoapJdbcSQLError.SQL_STATE_ILLEGAL_ARGUMENT);
}
case Types.BIGINT:
try {
return new BigInteger(value.toString());
} catch (NumberFormatException nfe) {
throw new SQLException("Bad format for BigInteger: " + value + " in column: " + columnIndex,
SoapJdbcSQLError.SQL_STATE_ILLEGAL_ARGUMENT);
}
case Types.DECIMAL:
case Types.NUMERIC:
try {
return new BigDecimal(value.toString());
} catch (NumberFormatException nfe) {
throw new SQLException("Bad format for BigDecimal: " + value + " in column: " + columnIndex,
SoapJdbcSQLError.SQL_STATE_ILLEGAL_ARGUMENT);
}
case Types.REAL:
try {
return Float.parseFloat(value.toString());
} catch (NumberFormatException nfe) {
throw new SQLException("Bad format for BigDecimal: " + value + " in column: " + columnIndex,
SoapJdbcSQLError.SQL_STATE_ILLEGAL_ARGUMENT);
}
case Types.FLOAT:
case Types.DOUBLE:
try {
return Double.parseDouble(value.toString());
} catch (NumberFormatException nfe) {
throw new SQLException("Bad format for BigDecimal: " + value + " in column: " + columnIndex,
SoapJdbcSQLError.SQL_STATE_ILLEGAL_ARGUMENT);
}
case Types.CHAR:
case Types.VARCHAR:
case Types.LONGVARCHAR:
case Types.BINARY:
case Types.VARBINARY:
case Types.LONGVARBINARY:
return value.toString().getBytes();
case Types.DATE:
try {
return new Date(DateFormat.getDateInstance().parse(value.toString()).getTime());
} catch (ParseException e) {
throw new SQLException("Bad format for Date: " + value + " in column: " + columnIndex, SoapJdbcSQLError.SQL_STATE_ILLEGAL_ARGUMENT);
} // try
case Types.TIME:
try {
return new Time(DateFormat.getDateInstance().parse(value.toString()).getTime());
} catch (ParseException e) {
throw new SQLException("Bad format for Time: " + value + " in column: " + columnIndex, SoapJdbcSQLError.SQL_STATE_ILLEGAL_ARGUMENT);
} // try
case Types.TIMESTAMP:
try {
return new Timestamp(DateFormat.getDateInstance().parse(value.toString()).getTime());
} catch (ParseException e) {
throw new SQLException("Bad format for Timestamp: " + value + " in column: " + columnIndex,
SoapJdbcSQLError.SQL_STATE_ILLEGAL_ARGUMENT);
} // try
default:
return value.toString();
}
}
public Object getObject(String columnName) throws SQLException {
return getObject(findColumn(columnName));
}
public int findColumn(String columnName) throws SQLException {
if (mMapping == null) {
buildIndexMapping();
} // if
Integer index = mMapping.get(columnName);
if (index == null) {
int i = 1;
for (SoapJdbcResultSetMetaDataField field : mMetaData.mFields) {
if (field.columnName.equalsIgnoreCase(columnName)) {
return index;
} // if
i++;
} // for
} // if
if (index == null) {
throw new SQLException("Column: " + columnName + "not_found!", SoapJdbcSQLError.SQL_STATE_COLUMN_NOT_FOUND);
}
return index;
}
public Reader getCharacterStream(int columnIndex) throws SQLException {
String value = getString(columnIndex);
return value == null ? null : new StringReader(value);
}
public Reader getCharacterStream(String columnName) throws SQLException {
return getCharacterStream(findColumn(columnName));
}
public BigDecimal getBigDecimal(int columnIndex) throws SQLException {
String value = getString(columnIndex);
try {
return value == null ? null : new BigDecimal(value);
} catch (NumberFormatException nfe) {
throw new SQLException("Bad format for BigDecimal: " + value + " in column: " + columnIndex, SoapJdbcSQLError.SQL_STATE_ILLEGAL_ARGUMENT);
} // try
}
public BigDecimal getBigDecimal(String columnName) throws SQLException {
return getBigDecimal(findColumn(columnName));
}
public boolean isBeforeFirst() throws SQLException {
checkIfClosed();
return mRowData.isBeforeFirst();
}
public boolean isAfterLast() throws SQLException {
checkIfClosed();
return mRowData.isAfterLast();
}
public boolean isFirst() throws SQLException {
checkIfClosed();
return mRowData.isFirst();
}
public boolean isLast() throws SQLException {
checkIfClosed();
return mRowData.isLast();
}
public void beforeFirst() throws SQLException {
// checkIfClosed();
//
// mRowData.beforeFirst();
throw new NotImplementedException();
}
public void afterLast() throws SQLException {
// checkIfClosed();
//
// mRowData.afterLast();
throw new NotImplementedException();
}
public boolean first() throws SQLException {
throw new NotImplementedException();
}
public boolean last() throws SQLException {
throw new NotImplementedException();
}
public int getRow() throws SQLException {
checkIfClosed();
if (mRowData.isEmpty() || mRowData.isBeforeFirst() || mRowData.isAfterLast()) {
return 0;
} // if
return mRowData.getCurrentRowIndex() + 1;
}
public boolean absolute(int row) throws SQLException {
throw new NotImplementedException();
}
public boolean relative(int rows) throws SQLException {
// checkIfClosed();
//
// if (mRowData.isEmpty()) {
// return false;
//
// }
//
// mRowData.moveRowRelative(rows);
//
// return (!mRowData.isAfterLast() && !mRowData.isBeforeFirst());
throw new NotImplementedException();
}
public boolean previous() throws SQLException {
throw new NotImplementedException();
}
public void setFetchDirection(int direction) throws SQLException {
if (direction != FETCH_FORWARD) {
throw new SQLException("Fetch-direction not supported", SoapJdbcSQLError.SQL_STATE_ILLEGAL_ARGUMENT);
}
mFetchDirection = direction;
}
public int getFetchDirection() {
return mFetchDirection;
}
public void setFetchSize(int rows) throws SQLException {
if (rows < 0) {
throw new SQLException("Fetchsize must be >= 0", SoapJdbcSQLError.SQL_STATE_ILLEGAL_ARGUMENT);
} // if
mFetchSize = rows;
}
public int getFetchSize() {
return mFetchSize;
}
public int getType() {
return TYPE_FORWARD_ONLY;
}
public int getConcurrency() {
return CONCUR_READ_ONLY;
}
public boolean rowUpdated() throws SQLException {
throw new NotImplementedException();
}
public boolean rowInserted() throws SQLException {
throw new NotImplementedException();
}
public boolean rowDeleted() throws SQLException {
throw new NotImplementedException();
}
public void updateNull(int columnIndex) throws SQLException {
throw new NotUpdatableException();
}
public void updateBoolean(int columnIndex, boolean x) throws SQLException {
throw new NotUpdatableException();
}
public void updateByte(int columnIndex, byte x) throws SQLException {
throw new NotUpdatableException();
}
public void updateShort(int columnIndex, short x) throws SQLException {
throw new NotUpdatableException();
}
public void updateInt(int columnIndex, int x) throws SQLException {
throw new NotUpdatableException();
}
public void updateLong(int columnIndex, long x) throws SQLException {
throw new NotUpdatableException();
}
public void updateFloat(int columnIndex, float x) throws SQLException {
throw new NotUpdatableException();
}
public void updateDouble(int columnIndex, double x) throws SQLException {
throw new NotUpdatableException();
}
public void updateBigDecimal(int columnIndex, BigDecimal x) throws SQLException {
throw new NotUpdatableException();
}
public void updateString(int columnIndex, String x) throws SQLException {
throw new NotUpdatableException();
}
public void updateBytes(int columnIndex, byte[] x) throws SQLException {
throw new NotUpdatableException();
}
public void updateDate(int columnIndex, Date x) throws SQLException {
throw new NotUpdatableException();
}
public void updateTime(int columnIndex, Time x) throws SQLException {
throw new NotUpdatableException();
}
public void updateTimestamp(int columnIndex, Timestamp x) throws SQLException {
throw new NotUpdatableException();
}
public void updateAsciiStream(int columnIndex, InputStream x, int length) throws SQLException {
throw new NotUpdatableException();
}
public void updateBinaryStream(int columnIndex, InputStream x, int length) throws SQLException {
throw new NotUpdatableException();
}
public void updateCharacterStream(int columnIndex, Reader x, int length) throws SQLException {
throw new NotUpdatableException();
}
public void updateObject(int columnIndex, Object x, int scale) throws SQLException {
throw new NotUpdatableException();
}
public void updateObject(int columnIndex, Object x) throws SQLException {
throw new NotUpdatableException();
}
public void updateNull(String columnName) throws SQLException {
throw new NotUpdatableException();
}
public void updateBoolean(String columnName, boolean x) throws SQLException {
throw new NotUpdatableException();
}
public void updateByte(String columnName, byte x) throws SQLException {
throw new NotUpdatableException();
}
public void updateShort(String columnName, short x) throws SQLException {
throw new NotUpdatableException();
}
public void updateInt(String columnName, int x) throws SQLException {
throw new NotUpdatableException();
}
public void updateLong(String columnName, long x) throws SQLException {
throw new NotUpdatableException();
}
public void updateFloat(String columnName, float x) throws SQLException {
throw new NotUpdatableException();
}
public void updateDouble(String columnName, double x) throws SQLException {
throw new NotUpdatableException();
}
public void updateBigDecimal(String columnName, BigDecimal x) throws SQLException {
throw new NotUpdatableException();
}
public void updateString(String columnName, String x) throws SQLException {
throw new NotUpdatableException();
}
public void updateBytes(String columnName, byte[] x) throws SQLException {
throw new NotUpdatableException();
}
public void updateDate(String columnName, Date x) throws SQLException {
throw new NotUpdatableException();
}
public void updateTime(String columnName, Time x) throws SQLException {
throw new NotUpdatableException();
}
public void updateTimestamp(String columnName, Timestamp x) throws SQLException {
throw new NotUpdatableException();
}
public void updateAsciiStream(String columnName, InputStream x, int length) throws SQLException {
throw new NotUpdatableException();
}
public void updateBinaryStream(String columnName, InputStream x, int length) throws SQLException {
throw new NotUpdatableException();
}
public void updateCharacterStream(String columnName, Reader reader, int length) throws SQLException {
throw new NotUpdatableException();
}
public void updateObject(String columnName, Object x, int scale) throws SQLException {
throw new NotUpdatableException();
}
public void updateObject(String columnName, Object x) throws SQLException {
throw new NotUpdatableException();
}
public void insertRow() throws SQLException {
throw new NotUpdatableException();
}
public void updateRow() throws SQLException {
throw new NotUpdatableException();
}
public void deleteRow() throws SQLException {
throw new NotUpdatableException();
}
public void refreshRow() throws SQLException {
throw new NotUpdatableException();
}
public void cancelRowUpdates() throws SQLException {
throw new NotUpdatableException();
}
public void moveToInsertRow() throws SQLException {
throw new NotUpdatableException();
}
public void moveToCurrentRow() throws SQLException {
throw new NotUpdatableException();
}
public Statement getStatement() {
return mCurrentStatement;
}
public Object getObject(int columnIndex, Map<String, Class<?>> arg1) throws SQLException {
return getObject(columnIndex);
}
public Ref getRef(int i) throws SQLException {
throw new NotImplementedException();
}
public Blob getBlob(int i) throws SQLException {
byte[] value = getBytes(i);
return value == null ? null : new SerialBlob(value);
}
public Clob getClob(int i) throws SQLException {
String value = getString(i);
return value == null ? null : new SerialClob(value.toCharArray());
}
public Array getArray(int i) throws SQLException {
throw new NotImplementedException();
}
public Object getObject(String columnName, Map<String, Class<?>> typeMap) throws SQLException {
return getObject(findColumn(columnName), typeMap);
}
public Ref getRef(String colName) throws SQLException {
return getRef(findColumn(colName));
}
public Blob getBlob(String colName) throws SQLException {
return getBlob(findColumn(colName));
}
public Clob getClob(String colName) throws SQLException {
return getClob(findColumn(colName));
}
public Array getArray(String colName) throws SQLException {
return getArray(findColumn(colName));
}
public Date getDate(int columnIndex, Calendar cal) throws SQLException {
return getDate(columnIndex);
}
public Date getDate(String columnName, Calendar cal) throws SQLException {
return getDate(findColumn(columnName));
}
public Time getTime(int columnIndex, Calendar cal) throws SQLException {
return getTime(columnIndex);
}
public Time getTime(String columnName, Calendar cal) throws SQLException {
return getTime(findColumn(columnName));
}
public Timestamp getTimestamp(int columnIndex, Calendar cal) throws SQLException {
return getTimestamp(columnIndex);
}
public Timestamp getTimestamp(String columnName, Calendar cal) throws SQLException {
return getTimestamp(findColumn(columnName));
}
public URL getURL(int columnIndex) throws SQLException {
String value = getString(columnIndex);
try {
return value == null ? null : new URL(value);
} catch (MalformedURLException mfe) {
throw new SQLException("Malformed URL: " + value, SoapJdbcSQLError.SQL_STATE_ILLEGAL_ARGUMENT);
}
}
public URL getURL(String columnName) throws SQLException {
return getURL(findColumn(columnName));
}
public void updateRef(int columnIndex, Ref x) throws SQLException {
throw new NotImplementedException();
}
public void updateRef(String columnName, Ref x) throws SQLException {
throw new NotImplementedException();
}
public void updateBlob(int columnIndex, Blob x) throws SQLException {
throw new NotImplementedException();
}
public void updateBlob(String columnName, Blob x) throws SQLException {
throw new NotImplementedException();
}
public void updateClob(int columnIndex, Clob x) throws SQLException {
throw new NotImplementedException();
}
public void updateClob(String columnName, Clob x) throws SQLException {
throw new NotImplementedException();
}
public void updateArray(int columnIndex, Array x) throws SQLException {
throw new NotImplementedException();
}
public void updateArray(String columnName, Array x) throws SQLException {
throw new NotImplementedException();
}
/**
* Builds a hash between column names and their indices for fast retrieval.
*/
private void buildIndexMapping() {
mMapping = new TreeMap<String, Integer>();
int index = 1;
for (SoapJdbcResultSetMetaDataField field : mMetaData.mFields) {
if (!mMapping.containsKey(field.columnName)) {
mMapping.put(field.columnName, new Integer(index));
mMapping.put(field.columnName.toLowerCase(), new Integer(index));
mMapping.put(field.columnName.toUpperCase(), new Integer(index++));
} // if
} // for
}
}