Add support for extracting our column data by position rather than name.

This commit is contained in:
Michael Bayne
2010-12-08 20:00:21 +00:00
parent 44b9b4ff90
commit 0bfe3959fc
@@ -140,6 +140,9 @@ public abstract class FieldMarshaller<T>
@Override public T getFromSet (ResultSet rs) throws SQLException { @Override public T getFromSet (ResultSet rs) throws SQLException {
return delegate.getFromSet(rs); return delegate.getFromSet(rs);
} }
@Override public T getFromSet (ResultSet rs, int index) throws SQLException {
return delegate.getFromSet(rs, index);
}
@Override public void writeToObject (Object po, T value) @Override public void writeToObject (Object po, T value)
throws IllegalArgumentException, IllegalAccessException { throws IllegalArgumentException, IllegalAccessException {
_field.set(po, xformer.fromPersistent(value)); _field.set(po, xformer.fromPersistent(value));
@@ -237,6 +240,12 @@ public abstract class FieldMarshaller<T>
public abstract T getFromSet (ResultSet rs) public abstract T getFromSet (ResultSet rs)
throws SQLException; throws SQLException;
/**
* Reads and returns this field from the result set.
*/
public abstract T getFromSet (ResultSet rs, int index)
throws SQLException;
/** /**
* Writes the given value to the given persistent value. * Writes the given value to the given persistent value.
*/ */
@@ -319,11 +328,12 @@ public abstract class FieldMarshaller<T>
@Override public String getColumnType (ColumnTyper typer, int length) { @Override public String getColumnType (ColumnTyper typer, int length) {
return typer.getByteType(length); return typer.getByteType(length);
} }
@Override public Object getFromSet (ResultSet rs) @Override public Object getFromSet (ResultSet rs) throws SQLException {
throws SQLException { return massageResult(super.getFromSet(rs));
// work around the fact that HSQLDB (at least) returns Integer rather than }
// Byte for TINYINT columns // works around the fact that HSQLDB (at least) returns Integer rather than Byte
Object value = super.getFromSet(rs); // for TINYINT columns
protected Object massageResult (Object value) {
return (value == null) ? null : ((Number)value).byteValue(); return (value == null) ? null : ((Number)value).byteValue();
} }
}; };
@@ -332,11 +342,12 @@ public abstract class FieldMarshaller<T>
@Override public String getColumnType (ColumnTyper typer, int length) { @Override public String getColumnType (ColumnTyper typer, int length) {
return typer.getShortType(length); return typer.getShortType(length);
} }
@Override public Object getFromSet (ResultSet rs) @Override public Object getFromSet (ResultSet rs) throws SQLException {
throws SQLException { return massageResult(super.getFromSet(rs));
// work around the fact that HSQLDB (at least) returns Integer rather than }
// Short for SMALLINT columns // works around the fact that HSQLDB (at least) returns Integer rather than Short
Object value = super.getFromSet(rs); // for SMALLINT columns
protected Object massageResult (Object value) {
return (value == null) ? null : ((Number)value).shortValue(); return (value == null) ? null : ((Number)value).shortValue();
} }
}; };
@@ -357,11 +368,12 @@ public abstract class FieldMarshaller<T>
@Override public String getColumnType (ColumnTyper typer, int length) { @Override public String getColumnType (ColumnTyper typer, int length) {
return typer.getFloatType(length); return typer.getFloatType(length);
} }
@Override public Object getFromSet (ResultSet rs) @Override public Object getFromSet (ResultSet rs) throws SQLException {
throws SQLException { return massageResult(super.getFromSet(rs));
// work around the fact that HSQLDB (at least) returns Double rather than }
// Float for REAL columns // works around the fact that HSQLDB (at least) returns Double rather than Float
Object value = super.getFromSet(rs); // for REAL columns
protected Object massageResult (Object value) {
return (value == null) ? null : ((Number)value).floatValue(); return (value == null) ? null : ((Number)value).floatValue();
} }
}; };
@@ -475,10 +487,12 @@ public abstract class FieldMarshaller<T>
throws IllegalArgumentException, IllegalAccessException { throws IllegalArgumentException, IllegalAccessException {
return _field.getBoolean(po); return _field.getBoolean(po);
} }
@Override public Boolean getFromSet (ResultSet rs) @Override public Boolean getFromSet (ResultSet rs) throws SQLException {
throws SQLException {
return rs.getBoolean(getColumnName()); return rs.getBoolean(getColumnName());
} }
@Override public Boolean getFromSet (ResultSet rs, int index) throws SQLException {
return rs.getBoolean(index);
}
@Override public void writeToObject (Object po, Boolean value) @Override public void writeToObject (Object po, Boolean value)
throws IllegalArgumentException, IllegalAccessException { throws IllegalArgumentException, IllegalAccessException {
_field.setBoolean(po, value); _field.setBoolean(po, value);
@@ -497,10 +511,12 @@ public abstract class FieldMarshaller<T>
throws IllegalArgumentException, IllegalAccessException { throws IllegalArgumentException, IllegalAccessException {
return _field.getByte(po); return _field.getByte(po);
} }
@Override public Byte getFromSet (ResultSet rs) @Override public Byte getFromSet (ResultSet rs) throws SQLException {
throws SQLException {
return rs.getByte(getColumnName()); return rs.getByte(getColumnName());
} }
@Override public Byte getFromSet (ResultSet rs, int index) throws SQLException {
return rs.getByte(index);
}
@Override public void writeToObject (Object po, Byte value) @Override public void writeToObject (Object po, Byte value)
throws IllegalArgumentException, IllegalAccessException { throws IllegalArgumentException, IllegalAccessException {
_field.setByte(po, value); _field.setByte(po, value);
@@ -519,10 +535,12 @@ public abstract class FieldMarshaller<T>
throws IllegalArgumentException, IllegalAccessException { throws IllegalArgumentException, IllegalAccessException {
return _field.getShort(po); return _field.getShort(po);
} }
@Override public Short getFromSet (ResultSet rs) @Override public Short getFromSet (ResultSet rs) throws SQLException {
throws SQLException {
return rs.getShort(getColumnName()); return rs.getShort(getColumnName());
} }
@Override public Short getFromSet (ResultSet rs, int index) throws SQLException {
return rs.getShort(index);
}
@Override public void writeToObject (Object po, Short value) @Override public void writeToObject (Object po, Short value)
throws IllegalArgumentException, IllegalAccessException { throws IllegalArgumentException, IllegalAccessException {
_field.setShort(po, value); _field.setShort(po, value);
@@ -541,10 +559,12 @@ public abstract class FieldMarshaller<T>
throws IllegalArgumentException, IllegalAccessException { throws IllegalArgumentException, IllegalAccessException {
return _field.getInt(po); return _field.getInt(po);
} }
@Override public Integer getFromSet (ResultSet rs) @Override public Integer getFromSet (ResultSet rs) throws SQLException {
throws SQLException {
return rs.getInt(getColumnName()); return rs.getInt(getColumnName());
} }
@Override public Integer getFromSet (ResultSet rs, int index) throws SQLException {
return rs.getInt(index);
}
@Override public void writeToObject (Object po, Integer value) @Override public void writeToObject (Object po, Integer value)
throws IllegalArgumentException, IllegalAccessException { throws IllegalArgumentException, IllegalAccessException {
_field.setInt(po, value); _field.setInt(po, value);
@@ -563,10 +583,12 @@ public abstract class FieldMarshaller<T>
throws IllegalArgumentException, IllegalAccessException { throws IllegalArgumentException, IllegalAccessException {
return _field.getLong(po); return _field.getLong(po);
} }
@Override public Long getFromSet (ResultSet rs) @Override public Long getFromSet (ResultSet rs) throws SQLException {
throws SQLException {
return rs.getLong(getColumnName()); return rs.getLong(getColumnName());
} }
@Override public Long getFromSet (ResultSet rs, int index) throws SQLException {
return rs.getLong(index);
}
@Override public void writeToObject (Object po, Long value) @Override public void writeToObject (Object po, Long value)
throws IllegalArgumentException, IllegalAccessException { throws IllegalArgumentException, IllegalAccessException {
_field.setLong(po, value); _field.setLong(po, value);
@@ -585,10 +607,12 @@ public abstract class FieldMarshaller<T>
throws IllegalArgumentException, IllegalAccessException { throws IllegalArgumentException, IllegalAccessException {
return _field.getFloat(po); return _field.getFloat(po);
} }
@Override public Float getFromSet (ResultSet rs) @Override public Float getFromSet (ResultSet rs) throws SQLException {
throws SQLException {
return rs.getFloat(getColumnName()); return rs.getFloat(getColumnName());
} }
@Override public Float getFromSet (ResultSet rs, int index) throws SQLException {
return rs.getFloat(index);
}
@Override public void writeToObject (Object po, Float value) @Override public void writeToObject (Object po, Float value)
throws IllegalArgumentException, IllegalAccessException { throws IllegalArgumentException, IllegalAccessException {
_field.setFloat(po, value); _field.setFloat(po, value);
@@ -607,10 +631,12 @@ public abstract class FieldMarshaller<T>
throws IllegalArgumentException, IllegalAccessException { throws IllegalArgumentException, IllegalAccessException {
return _field.getDouble(po); return _field.getDouble(po);
} }
@Override public Double getFromSet (ResultSet rs) @Override public Double getFromSet (ResultSet rs) throws SQLException {
throws SQLException {
return rs.getDouble(getColumnName()); return rs.getDouble(getColumnName());
} }
@Override public Double getFromSet (ResultSet rs, int index) throws SQLException {
return rs.getDouble(index);
}
@Override public void writeToObject (Object po, Double value) @Override public void writeToObject (Object po, Double value)
throws IllegalArgumentException, IllegalAccessException { throws IllegalArgumentException, IllegalAccessException {
_field.setDouble(po, value); _field.setDouble(po, value);
@@ -626,10 +652,12 @@ public abstract class FieldMarshaller<T>
throws IllegalArgumentException, IllegalAccessException { throws IllegalArgumentException, IllegalAccessException {
return _field.get(po); return _field.get(po);
} }
@Override public Object getFromSet (ResultSet rs) @Override public Object getFromSet (ResultSet rs) throws SQLException {
throws SQLException {
return rs.getObject(getColumnName()); return rs.getObject(getColumnName());
} }
@Override public Object getFromSet (ResultSet rs, int index) throws SQLException {
return rs.getObject(index);
}
@Override public void writeToObject (Object po, Object value) @Override public void writeToObject (Object po, Object value)
throws IllegalArgumentException, IllegalAccessException { throws IllegalArgumentException, IllegalAccessException {
_field.set(po, value); _field.set(po, value);
@@ -648,10 +676,12 @@ public abstract class FieldMarshaller<T>
throws IllegalArgumentException, IllegalAccessException { throws IllegalArgumentException, IllegalAccessException {
return (byte[]) _field.get(po); return (byte[]) _field.get(po);
} }
@Override public byte[] getFromSet (ResultSet rs) @Override public byte[] getFromSet (ResultSet rs) throws SQLException {
throws SQLException {
return rs.getBytes(getColumnName()); return rs.getBytes(getColumnName());
} }
@Override public byte[] getFromSet (ResultSet rs, int index) throws SQLException {
return rs.getBytes(index);
}
@Override public void writeToObject (Object po, byte[] value) @Override public void writeToObject (Object po, byte[] value)
throws IllegalArgumentException, IllegalAccessException { throws IllegalArgumentException, IllegalAccessException {
_field.set(po, value); _field.set(po, value);
@@ -670,17 +700,13 @@ public abstract class FieldMarshaller<T>
throws IllegalArgumentException, IllegalAccessException { throws IllegalArgumentException, IllegalAccessException {
return (int[]) _field.get(po); return (int[]) _field.get(po);
} }
@Override public int[] getFromSet (ResultSet rs) @Override public int[] getFromSet (ResultSet rs) throws SQLException {
throws SQLException { // TODO: why not use getBytes()?
byte[] raw = (byte[]) rs.getObject(getColumnName()); return fromBytes((byte[]) rs.getObject(getColumnName()));
int[] value; }
if (raw == null) { @Override public int[] getFromSet (ResultSet rs, int index) throws SQLException {
value = null; // TODO: why not use getBytes()?
} else { return fromBytes((byte[]) rs.getObject(index));
value = new int[raw.length/4];
ByteBuffer.wrap(raw).asIntBuffer().get(value);
}
return value;
} }
@Override public void writeToObject (Object po, int[] value) @Override public void writeToObject (Object po, int[] value)
throws IllegalArgumentException, IllegalAccessException { throws IllegalArgumentException, IllegalAccessException {
@@ -698,6 +724,16 @@ public abstract class FieldMarshaller<T>
} }
ps.setObject(column, raw); ps.setObject(column, raw);
} }
protected final int[] fromBytes (byte[] raw) {
int[] value;
if (raw == null) {
value = null;
} else {
value = new int[raw.length/4];
ByteBuffer.wrap(raw).asIntBuffer().get(value);
}
return value;
}
} }
protected static class ByteEnumMarshaller<E extends Enum<E> & ByteEnum> protected static class ByteEnumMarshaller<E extends Enum<E> & ByteEnum>
@@ -725,6 +761,9 @@ public abstract class FieldMarshaller<T>
@Override public ByteEnum getFromSet (ResultSet rs) throws SQLException { @Override public ByteEnum getFromSet (ResultSet rs) throws SQLException {
return ByteEnumUtil.fromByte(_eclass, rs.getByte(getColumnName())); return ByteEnumUtil.fromByte(_eclass, rs.getByte(getColumnName()));
} }
@Override public ByteEnum getFromSet (ResultSet rs, int index) throws SQLException {
return ByteEnumUtil.fromByte(_eclass, rs.getByte(index));
}
@Override public void writeToObject (Object po, ByteEnum value) @Override public void writeToObject (Object po, ByteEnum value)
throws IllegalArgumentException, IllegalAccessException { throws IllegalArgumentException, IllegalAccessException {
_field.set(po, value); _field.set(po, value);
@@ -751,8 +790,10 @@ public abstract class FieldMarshaller<T>
return value; return value;
} }
@Override public E getFromSet (ResultSet rs) throws SQLException { @Override public E getFromSet (ResultSet rs) throws SQLException {
String svalue = rs.getString(getColumnName()); return fromString(rs.getString(getColumnName()));
return (svalue == null) ? null : Enum.valueOf(_eclass, svalue); }
@Override public E getFromSet (ResultSet rs, int index) throws SQLException {
return fromString(rs.getString(index));
} }
@Override public void writeToObject (Object po, E value) @Override public void writeToObject (Object po, E value)
throws IllegalArgumentException, IllegalAccessException { throws IllegalArgumentException, IllegalAccessException {
@@ -763,6 +804,9 @@ public abstract class FieldMarshaller<T>
String svalue = (value == null) ? null : value.name(); String svalue = (value == null) ? null : value.name();
ps.setString(column, svalue); ps.setString(column, svalue);
} }
protected final E fromString (String svalue) {
return (svalue == null) ? null : Enum.valueOf(_eclass, svalue);
}
protected Class<E> _eclass; protected Class<E> _eclass;
} }