Skip to content

Instantly share code, notes, and snippets.

@panchishin
Created October 31, 2017 18:42
Show Gist options
  • Save panchishin/406ba36e152500ea3ab45afb040f9d22 to your computer and use it in GitHub Desktop.
Save panchishin/406ba36e152500ea3ab45afb040f9d22 to your computer and use it in GitHub Desktop.
A wrapper for Java result set. The wrapper fetches all data and stores it but provides the same interface as a result set without having to keep the DB session open
package amp.util.db;
import java.io.InputStream;
import java.io.Reader;
import java.math.BigDecimal;
import java.net.URL;
import java.sql.Array;
import java.sql.Blob;
import java.sql.Clob;
import java.sql.Date;
import java.sql.NClob;
import java.sql.Ref;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.RowId;
import java.sql.SQLException;
import java.sql.SQLWarning;
import java.sql.SQLXML;
import java.sql.Statement;
import java.sql.Time;
import java.sql.Timestamp;
import java.util.Calendar;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
public class VirtualResultSet implements ResultSet {
private final int COLUMNS;
private final Iterator<Object> rowIterator;
private Object[] currentRowObjects;
private String[] columnNames;
private final List<Object> resultList;
public VirtualResultSet(ResultSet resultSet) {
int columns = 0;
if (resultSet instanceof VirtualResultSet) {
VirtualResultSet parent = (VirtualResultSet) resultSet;
columns = parent.COLUMNS;
columnNames = parent.columnNames;
resultList = parent.resultList;
} else {
resultList = new LinkedList<Object>();
try {
ResultSetMetaData meta = resultSet.getMetaData();
columns = meta.getColumnCount();
columnNames = new String[columns];
for (int index = 0; index < columns; index++) {
columnNames[index] = meta.getColumnName(index + 1);
}
while (resultSet.next()) {
Object[] resultObjects = new Object[columns];
for (int index = 0; index < columns; index++) {
resultObjects[index] = resultSet.getObject(index + 1);
}
resultList.add(resultObjects);
}
} catch (SQLException e) {
// no op
}
}
COLUMNS = columns;
rowIterator = resultList.iterator();
}
public VirtualResultSet() {
COLUMNS = 0;
resultList = new LinkedList<Object>();
rowIterator = resultList.iterator();
}
/*
* (non-Javadoc)
*
* @see java.sql.ResultSet#next()
*/
public boolean next() throws SQLException {
if (rowIterator.hasNext()) {
currentRowObjects = (Object[]) rowIterator.next();
return true;
} else {
return false;
}
}
/*
* (non-Javadoc)
*
* @see java.sql.ResultSet#close()
*/
public void close() throws SQLException {
// no-op
}
/*
* (non-Javadoc)
*
* @see java.sql.ResultSet#wasNull()
*/
public boolean wasNull() throws SQLException {
throw new Error("Unsupported");
}
/*
* (non-Javadoc)
*
* @see java.sql.ResultSet#getString(int)
*/
public String getString(int columnIndex) throws SQLException {
if ( currentRowObjects[columnIndex - 1] == null ) {
return "";
} else {
return currentRowObjects[columnIndex - 1].toString();
}
}
public boolean getBoolean(int columnIndex) throws SQLException {
final String text = getString(columnIndex);
if ( text == null ) {
return false;
}
if ( text.equals("1") || text.toLowerCase().startsWith("t") ) {
return true;
} else {
return false;
}
}
public byte getByte(int columnIndex) throws SQLException {
byte[] bytes = getString(columnIndex).getBytes();
if (bytes.length >= 1) {
return bytes[0];
}
throw new SQLException("Not enough bytes");
}
public short getShort(int columnIndex) throws SQLException {
return Short.valueOf(getString(columnIndex)).shortValue();
}
public int getInt(int columnIndex) throws SQLException {
try {
return Integer.valueOf(getString(columnIndex)).intValue();
} catch (NumberFormatException e) {
return 0;
}
}
public long getLong(int columnIndex) throws SQLException {
return Long.valueOf(getString(columnIndex)).longValue();
}
public float getFloat(int columnIndex) throws SQLException {
return Float.valueOf(getString(columnIndex)).floatValue();
}
public double getDouble(int columnIndex) throws SQLException {
return Double.valueOf(getString(columnIndex)).doubleValue();
}
public BigDecimal getBigDecimal(int columnIndex, int scale) throws SQLException {
throw new Error("Unsupported");
}
public byte[] getBytes(int columnIndex) throws SQLException {
return getString(columnIndex).getBytes();
}
public Date getDate(int columnIndex) throws SQLException {
Object result = getObject(columnIndex);
if ( result instanceof Date || result == null ) {
return (Date) result;
}
if ( result instanceof Timestamp ) {
return new Date(((Timestamp) result).getTime());
}
throw new Error("Unsupported conversion");
}
public Time getTime(int columnIndex) throws SQLException {
Object result = getObject(columnIndex);
if ( result instanceof Time || result == null) {
return (Time) result;
}
if ( result instanceof Timestamp) {
return new Time(((Timestamp) result).getTime());
}
throw new Error("Unsupported conversion");
}
public Timestamp getTimestamp(int columnIndex) throws SQLException {
Object result = getObject(columnIndex);
if ( result instanceof Timestamp || result == null ) {
return (Timestamp) result;
}
throw new Error("Unsupported conversion");
}
public InputStream getAsciiStream(int columnIndex) throws SQLException {
throw new Error("Unsupported");
}
public InputStream getUnicodeStream(int columnIndex) throws SQLException {
throw new Error("Unsupported");
}
public InputStream getBinaryStream(int columnIndex) throws SQLException {
throw new Error("Unsupported");
}
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 {
throw new Error("Unsupported");
}
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 {
throw new Error("Unsupported");
}
public InputStream getUnicodeStream(String columnName) throws SQLException {
throw new Error("Unsupported");
}
public InputStream getBinaryStream(String columnName) throws SQLException {
throw new Error("Unsupported");
}
public SQLWarning getWarnings() throws SQLException {
throw new Error("Unsupported");
}
public void clearWarnings() throws SQLException {
throw new Error("Unsupported");
}
public String getCursorName() throws SQLException {
throw new Error("Unsupported");
}
public ResultSetMetaData getMetaData() throws SQLException {
throw new Error("Unsupported");
}
public Object getObject(int columnIndex) throws SQLException {
return currentRowObjects[columnIndex - 1];
}
public Object getObject(String columnName) throws SQLException {
return currentRowObjects[findColumn(columnName)];
}
public int findColumn(String columnName) throws SQLException {
for (int index = 0; index < COLUMNS; index++) {
if (columnNames[index].equalsIgnoreCase(columnName)) {
return index + 1;
}
}
throw new SQLException("Unknown column '" + columnName + '"');
}
public Reader getCharacterStream(int columnIndex) throws SQLException {
throw new Error("Unsupported");
}
public Reader getCharacterStream(String columnName) throws SQLException {
throw new Error("Unsupported");
}
public BigDecimal getBigDecimal(int columnIndex) throws SQLException {
throw new Error("Unsupported");
}
public BigDecimal getBigDecimal(String columnName) throws SQLException {
throw new Error("Unsupported");
}
public boolean isBeforeFirst() throws SQLException {
throw new Error("Unsupported");
}
public boolean isAfterLast() throws SQLException {
throw new Error("Unsupported");
}
public boolean isFirst() throws SQLException {
throw new Error("Unsupported");
}
public boolean isLast() throws SQLException {
throw new Error("Unsupported");
}
public void beforeFirst() throws SQLException {
throw new Error("Unsupported");
}
public void afterLast() throws SQLException {
throw new Error("Unsupported");
}
public boolean first() throws SQLException {
throw new Error("Unsupported");
}
public boolean last() throws SQLException {
throw new Error("Unsupported");
}
public int getRow() throws SQLException {
throw new Error("Unsupported");
}
public boolean absolute(int row) throws SQLException {
throw new Error("Unsupported");
}
public boolean relative(int rows) throws SQLException {
throw new Error("Unsupported");
}
public boolean previous() throws SQLException {
throw new Error("Unsupported");
}
public void setFetchDirection(int direction) throws SQLException {
throw new Error("Unsupported");
}
public int getFetchDirection() throws SQLException {
throw new Error("Unsupported");
}
public void setFetchSize(int rows) throws SQLException {
throw new Error("Unsupported");
}
public int getFetchSize() throws SQLException {
throw new Error("Unsupported");
}
public int getType() throws SQLException {
throw new Error("Unsupported");
}
public int getConcurrency() throws SQLException {
throw new Error("Unsupported");
}
public boolean rowUpdated() throws SQLException {
throw new Error("Unsupported");
}
public boolean rowInserted() throws SQLException {
throw new Error("Unsupported");
}
public boolean rowDeleted() throws SQLException {
throw new Error("Unsupported");
}
public void updateNull(int columnIndex) throws SQLException {
throw new Error("Unsupported");
}
public void updateBoolean(int columnIndex, boolean x) throws SQLException {
throw new Error("Unsupported");
}
public void updateByte(int columnIndex, byte x) throws SQLException {
throw new Error("Unsupported");
}
public void updateShort(int columnIndex, short x) throws SQLException {
throw new Error("Unsupported");
}
public void updateInt(int columnIndex, int x) throws SQLException {
throw new Error("Unsupported");
}
public void updateLong(int columnIndex, long x) throws SQLException {
throw new Error("Unsupported");
}
public void updateFloat(int columnIndex, float x) throws SQLException {
throw new Error("Unsupported");
}
public void updateDouble(int columnIndex, double x) throws SQLException {
throw new Error("Unsupported");
}
public void updateBigDecimal(int columnIndex, BigDecimal x) throws SQLException {
throw new Error("Unsupported");
}
public void updateString(int columnIndex, String x) throws SQLException {
throw new Error("Unsupported");
}
public void updateBytes(int columnIndex, byte[] x) throws SQLException {
throw new Error("Unsupported");
}
public void updateDate(int columnIndex, Date x) throws SQLException {
throw new Error("Unsupported");
}
public void updateTime(int columnIndex, Time x) throws SQLException {
throw new Error("Unsupported");
}
public void updateTimestamp(int columnIndex, Timestamp x) throws SQLException {
throw new Error("Unsupported");
}
public void updateAsciiStream(int columnIndex, InputStream x, int length) throws SQLException {
throw new Error("Unsupported");
}
public void updateBinaryStream(int columnIndex, InputStream x, int length) throws SQLException {
throw new Error("Unsupported");
}
public void updateCharacterStream(int columnIndex, Reader x, int length) throws SQLException {
throw new Error("Unsupported");
}
public void updateObject(int columnIndex, Object x, int scale) throws SQLException {
throw new Error("Unsupported");
}
public void updateObject(int columnIndex, Object x) throws SQLException {
throw new Error("Unsupported");
}
public void updateNull(String columnName) throws SQLException {
throw new Error("Unsupported");
}
public void updateBoolean(String columnName, boolean x) throws SQLException {
throw new Error("Unsupported");
}
public void updateByte(String columnName, byte x) throws SQLException {
throw new Error("Unsupported");
}
public void updateShort(String columnName, short x) throws SQLException {
throw new Error("Unsupported");
}
public void updateInt(String columnName, int x) throws SQLException {
throw new Error("Unsupported");
}
public void updateLong(String columnName, long x) throws SQLException {
throw new Error("Unsupported");
}
public void updateFloat(String columnName, float x) throws SQLException {
throw new Error("Unsupported");
}
public void updateDouble(String columnName, double x) throws SQLException {
throw new Error("Unsupported");
}
public void updateBigDecimal(String columnName, BigDecimal x) throws SQLException {
throw new Error("Unsupported");
}
public void updateString(String columnName, String x) throws SQLException {
throw new Error("Unsupported");
}
public void updateBytes(String columnName, byte[] x) throws SQLException {
throw new Error("Unsupported");
}
public void updateDate(String columnName, Date x) throws SQLException {
throw new Error("Unsupported");
}
public void updateTime(String columnName, Time x) throws SQLException {
throw new Error("Unsupported");
}
public void updateTimestamp(String columnName, Timestamp x) throws SQLException {
throw new Error("Unsupported");
}
public void updateAsciiStream(String columnName, InputStream x, int length) throws SQLException {
throw new Error("Unsupported");
}
public void updateBinaryStream(String columnName, InputStream x, int length) throws SQLException {
throw new Error("Unsupported");
}
public void updateCharacterStream(String columnName, Reader reader, int length) throws SQLException {
throw new Error("Unsupported");
}
public void updateObject(String columnName, Object x, int scale) throws SQLException {
throw new Error("Unsupported");
}
public void updateObject(String columnName, Object x) throws SQLException {
throw new Error("Unsupported");
}
public void insertRow() throws SQLException {
throw new Error("Unsupported");
}
public void updateRow() throws SQLException {
throw new Error("Unsupported");
}
public void deleteRow() throws SQLException {
throw new Error("Unsupported");
}
public void refreshRow() throws SQLException {
throw new Error("Unsupported");
}
public void cancelRowUpdates() throws SQLException {
throw new Error("Unsupported");
}
public void moveToInsertRow() throws SQLException {
throw new Error("Unsupported");
}
public void moveToCurrentRow() throws SQLException {
throw new Error("Unsupported");
}
public Statement getStatement() throws SQLException {
throw new Error("Unsupported");
}
public Ref getRef(int i) throws SQLException {
throw new Error("Unsupported");
}
public Blob getBlob(int i) throws SQLException {
throw new Error("Unsupported");
}
public Clob getClob(int i) throws SQLException {
throw new Error("Unsupported");
}
public Array getArray(int i) throws SQLException {
throw new Error("Unsupported");
}
public Ref getRef(String colName) throws SQLException {
throw new Error("Unsupported");
}
public Blob getBlob(String colName) throws SQLException {
throw new Error("Unsupported");
}
public Clob getClob(String colName) throws SQLException {
throw new Error("Unsupported");
}
public Array getArray(String colName) throws SQLException {
throw new Error("Unsupported");
}
public Date getDate(int columnIndex, Calendar cal) throws SQLException {
throw new Error("Unsupported");
}
public Date getDate(String columnName, Calendar cal) throws SQLException {
throw new Error("Unsupported");
}
public Time getTime(int columnIndex, Calendar cal) throws SQLException {
throw new Error("Unsupported");
}
public Time getTime(String columnName, Calendar cal) throws SQLException {
throw new Error("Unsupported");
}
public Timestamp getTimestamp(int columnIndex, Calendar cal) throws SQLException {
throw new Error("Unsupported");
}
public Timestamp getTimestamp(String columnName, Calendar cal) throws SQLException {
throw new Error("Unsupported");
}
public URL getURL(int columnIndex) throws SQLException {
throw new Error("Unsupported");
}
public URL getURL(String columnName) throws SQLException {
throw new Error("Unsupported");
}
public void updateRef(int columnIndex, Ref x) throws SQLException {
throw new Error("Unsupported");
}
public void updateRef(String columnName, Ref x) throws SQLException {
throw new Error("Unsupported");
}
public void updateBlob(int columnIndex, Blob x) throws SQLException {
throw new Error("Unsupported");
}
public void updateBlob(String columnName, Blob x) throws SQLException {
throw new Error("Unsupported");
}
public void updateClob(int columnIndex, Clob x) throws SQLException {
throw new Error("Unsupported");
}
public void updateClob(String columnName, Clob x) throws SQLException {
throw new Error("Unsupported");
}
public void updateArray(int columnIndex, Array x) throws SQLException {
throw new Error("Unsupported");
}
public void updateArray(String columnName, Array x) throws SQLException {
throw new Error("Unsupported");
}
public int getHoldability() throws SQLException {
throw new Error("Unsupported");
}
public Reader getNCharacterStream(int columnIndex) throws SQLException {
throw new Error("Unsupported");
}
public Reader getNCharacterStream(String columnLabel) throws SQLException {
throw new Error("Unsupported");
}
public NClob getNClob(int columnIndex) throws SQLException {
throw new Error("Unsupported");
}
public NClob getNClob(String columnLabel) throws SQLException {
throw new Error("Unsupported");
}
public String getNString(int columnIndex) throws SQLException {
throw new Error("Unsupported");
}
public String getNString(String columnLabel) throws SQLException {
throw new Error("Unsupported");
}
public Object getObject(int columnIndex, Map<String, Class<?>> map) throws SQLException {
throw new Error("Unsupported");
}
public Object getObject(String columnLabel, Map<String, Class<?>> map) throws SQLException {
throw new Error("Unsupported");
}
public RowId getRowId(int columnIndex) throws SQLException {
throw new Error("Unsupported");
}
public RowId getRowId(String columnLabel) throws SQLException {
throw new Error("Unsupported");
}
public SQLXML getSQLXML(int columnIndex) throws SQLException {
throw new Error("Unsupported");
}
public SQLXML getSQLXML(String columnLabel) throws SQLException {
throw new Error("Unsupported");
}
public boolean isClosed() throws SQLException {
throw new Error("Unsupported");
}
public void updateAsciiStream(int columnIndex, InputStream x) throws SQLException {
throw new Error("Unsupported");
}
public void updateAsciiStream(String columnLabel, InputStream x) throws SQLException {
throw new Error("Unsupported");
}
public void updateAsciiStream(int columnIndex, InputStream x, long length) throws SQLException {
throw new Error("Unsupported");
}
public void updateAsciiStream(String columnLabel, InputStream x, long length) throws SQLException {
throw new Error("Unsupported");
}
public void updateBinaryStream(int columnIndex, InputStream x) throws SQLException {
throw new Error("Unsupported");
}
public void updateBinaryStream(String columnLabel, InputStream x) throws SQLException {
throw new Error("Unsupported");
}
public void updateBinaryStream(int columnIndex, InputStream x, long length) throws SQLException {
throw new Error("Unsupported");
}
public void updateBinaryStream(String columnLabel, InputStream x, long length) throws SQLException {
throw new Error("Unsupported");
}
public void updateBlob(int columnIndex, InputStream inputStream) throws SQLException {
throw new Error("Unsupported");
}
public void updateBlob(String columnLabel, InputStream inputStream) throws SQLException {
throw new Error("Unsupported");
}
public void updateBlob(int columnIndex, InputStream inputStream, long length) throws SQLException {
throw new Error("Unsupported");
}
public void updateBlob(String columnLabel, InputStream inputStream, long length) throws SQLException {
throw new Error("Unsupported");
}
public void updateCharacterStream(int columnIndex, Reader x) throws SQLException {
throw new Error("Unsupported");
}
public void updateCharacterStream(String columnLabel, Reader reader) throws SQLException {
throw new Error("Unsupported");
}
public void updateCharacterStream(int columnIndex, Reader x, long length) throws SQLException {
throw new Error("Unsupported");
}
public void updateCharacterStream(String columnLabel, Reader reader, long length) throws SQLException {
throw new Error("Unsupported");
}
public void updateClob(int columnIndex, Reader reader) throws SQLException {
throw new Error("Unsupported");
}
public void updateClob(String columnLabel, Reader reader) throws SQLException {
throw new Error("Unsupported");
}
public void updateClob(int columnIndex, Reader reader, long length) throws SQLException {
throw new Error("Unsupported");
}
public void updateClob(String columnLabel, Reader reader, long length) throws SQLException {
throw new Error("Unsupported");
}
public void updateNCharacterStream(int columnIndex, Reader x) throws SQLException {
throw new Error("Unsupported");
}
public void updateNCharacterStream(String columnLabel, Reader reader) throws SQLException {
throw new Error("Unsupported");
}
public void updateNCharacterStream(int columnIndex, Reader x, long length) throws SQLException {
throw new Error("Unsupported");
}
public void updateNCharacterStream(String columnLabel, Reader reader, long length) throws SQLException {
throw new Error("Unsupported");
}
public void updateNClob(int columnIndex, NClob clob) throws SQLException {
throw new Error("Unsupported");
}
public void updateNClob(String columnLabel, NClob clob) throws SQLException {
throw new Error("Unsupported");
}
public void updateNClob(int columnIndex, Reader reader) throws SQLException {
throw new Error("Unsupported");
}
public void updateNClob(String columnLabel, Reader reader) throws SQLException {
throw new Error("Unsupported");
}
public void updateNClob(int columnIndex, Reader reader, long length) throws SQLException {
throw new Error("Unsupported");
}
public void updateNClob(String columnLabel, Reader reader, long length) throws SQLException {
throw new Error("Unsupported");
}
public void updateNString(int columnIndex, String string) throws SQLException {
throw new Error("Unsupported");
}
public void updateNString(String columnLabel, String string) throws SQLException {
throw new Error("Unsupported");
}
public void updateRowId(int columnIndex, RowId x) throws SQLException {
throw new Error("Unsupported");
}
public void updateRowId(String columnLabel, RowId x) throws SQLException {
throw new Error("Unsupported");
}
public void updateSQLXML(int columnIndex, SQLXML xmlObject) throws SQLException {
throw new Error("Unsupported");
}
public void updateSQLXML(String columnLabel, SQLXML xmlObject) throws SQLException {
throw new Error("Unsupported");
}
public boolean isWrapperFor(Class<?> iface) throws SQLException {
throw new Error("Unsupported");
}
public <T> T unwrap(Class<T> iface) throws SQLException {
throw new Error("Unsupported");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment