Added support for byte arrays and full table loads.
This commit is contained in:
@@ -197,16 +197,20 @@ public class DepotMarshaller<T>
|
||||
|
||||
/**
|
||||
* Creates a query for instances of this persistent object type using the
|
||||
* supplied key.
|
||||
* supplied key. If null is supplied all instances will be loaded.
|
||||
*/
|
||||
public PreparedStatement createQuery (
|
||||
Connection conn, DepotRepository.Key key)
|
||||
throws SQLException
|
||||
{
|
||||
String query = "select " + _fullColumnList + " from " + getTableName() +
|
||||
" where " + key.toWhereClause();
|
||||
String query = "select " + _fullColumnList + " from " + getTableName();
|
||||
if (key != null) {
|
||||
query += " where " + key.toWhereClause();
|
||||
}
|
||||
PreparedStatement pstmt = conn.prepareStatement(query);
|
||||
key.bindArguments(pstmt, 1);
|
||||
if (key != null) {
|
||||
key.bindArguments(pstmt, 1);
|
||||
}
|
||||
return pstmt;
|
||||
}
|
||||
|
||||
|
||||
@@ -79,6 +79,15 @@ public class DepotRepository
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Loads all persistent objects of the specified type.
|
||||
*/
|
||||
protected <T,C extends Collection<T>> Collection<T> findAll (Class<T> type)
|
||||
throws PersistenceException
|
||||
{
|
||||
return findAll(type, null);
|
||||
}
|
||||
|
||||
/**
|
||||
* Loads all persistent objects that match the specified key.
|
||||
*/
|
||||
|
||||
@@ -55,19 +55,19 @@ public abstract class FieldMarshaller
|
||||
|
||||
// primitive types
|
||||
if (ftype.equals(Boolean.TYPE)) {
|
||||
marshaller = new PBooleanMarshaller();
|
||||
marshaller = new BooleanMarshaller();
|
||||
} else if (ftype.equals(Byte.TYPE)) {
|
||||
marshaller = new PByteMarshaller();
|
||||
marshaller = new ByteMarshaller();
|
||||
} else if (ftype.equals(Short.TYPE)) {
|
||||
marshaller = new PShortMarshaller();
|
||||
marshaller = new ShortMarshaller();
|
||||
} else if (ftype.equals(Integer.TYPE)) {
|
||||
marshaller = new PIntMarshaller();
|
||||
marshaller = new IntMarshaller();
|
||||
} else if (ftype.equals(Long.TYPE)) {
|
||||
marshaller = new PLongMarshaller();
|
||||
marshaller = new LongMarshaller();
|
||||
} else if (ftype.equals(Float.TYPE)) {
|
||||
marshaller = new PFloatMarshaller();
|
||||
marshaller = new FloatMarshaller();
|
||||
} else if (ftype.equals(Double.TYPE)) {
|
||||
marshaller = new PDoubleMarshaller();
|
||||
marshaller = new DoubleMarshaller();
|
||||
|
||||
// "natural" types
|
||||
} else if (ftype.equals(Byte.class) ||
|
||||
@@ -79,7 +79,9 @@ public abstract class FieldMarshaller
|
||||
ftype.equals(String.class)) {
|
||||
marshaller = new ObjectMarshaller();
|
||||
|
||||
// TODO: byte array, (other array types?)
|
||||
// some primitive array types
|
||||
} else if (ftype.equals(byte[].class)) {
|
||||
marshaller = new ByteArrayMarshaller();
|
||||
|
||||
// SQL types
|
||||
} else if (ftype.equals(Date.class) ||
|
||||
@@ -213,7 +215,7 @@ public abstract class FieldMarshaller
|
||||
_columnDefinition = builder.toString();
|
||||
}
|
||||
|
||||
protected static class PBooleanMarshaller extends FieldMarshaller {
|
||||
protected static class BooleanMarshaller extends FieldMarshaller {
|
||||
public void setValue (Object po, PreparedStatement ps, int column)
|
||||
throws SQLException, IllegalAccessException {
|
||||
ps.setBoolean(column, _field.getBoolean(po));
|
||||
@@ -227,7 +229,7 @@ public abstract class FieldMarshaller
|
||||
}
|
||||
}
|
||||
|
||||
protected static class PByteMarshaller extends FieldMarshaller {
|
||||
protected static class ByteMarshaller extends FieldMarshaller {
|
||||
public void setValue (Object po, PreparedStatement ps, int column)
|
||||
throws SQLException, IllegalAccessException {
|
||||
ps.setByte(column, _field.getByte(po));
|
||||
@@ -241,7 +243,7 @@ public abstract class FieldMarshaller
|
||||
}
|
||||
}
|
||||
|
||||
protected static class PShortMarshaller extends FieldMarshaller {
|
||||
protected static class ShortMarshaller extends FieldMarshaller {
|
||||
public void setValue (Object po, PreparedStatement ps, int column)
|
||||
throws SQLException, IllegalAccessException {
|
||||
ps.setShort(column, _field.getShort(po));
|
||||
@@ -255,7 +257,7 @@ public abstract class FieldMarshaller
|
||||
}
|
||||
}
|
||||
|
||||
protected static class PIntMarshaller extends FieldMarshaller {
|
||||
protected static class IntMarshaller extends FieldMarshaller {
|
||||
public void setValue (Object po, PreparedStatement ps, int column)
|
||||
throws SQLException, IllegalAccessException {
|
||||
ps.setInt(column, _field.getInt(po));
|
||||
@@ -269,7 +271,7 @@ public abstract class FieldMarshaller
|
||||
}
|
||||
}
|
||||
|
||||
protected static class PLongMarshaller extends FieldMarshaller {
|
||||
protected static class LongMarshaller extends FieldMarshaller {
|
||||
public void setValue (Object po, PreparedStatement ps, int column)
|
||||
throws SQLException, IllegalAccessException {
|
||||
ps.setLong(column, _field.getLong(po));
|
||||
@@ -283,7 +285,7 @@ public abstract class FieldMarshaller
|
||||
}
|
||||
}
|
||||
|
||||
protected static class PFloatMarshaller extends FieldMarshaller {
|
||||
protected static class FloatMarshaller extends FieldMarshaller {
|
||||
public void setValue (Object po, PreparedStatement ps, int column)
|
||||
throws SQLException, IllegalAccessException {
|
||||
ps.setFloat(column, _field.getFloat(po));
|
||||
@@ -297,7 +299,7 @@ public abstract class FieldMarshaller
|
||||
}
|
||||
}
|
||||
|
||||
protected static class PDoubleMarshaller extends FieldMarshaller {
|
||||
protected static class DoubleMarshaller extends FieldMarshaller {
|
||||
public void setValue (Object po, PreparedStatement ps, int column)
|
||||
throws SQLException, IllegalAccessException {
|
||||
ps.setDouble(column, _field.getDouble(po));
|
||||
@@ -353,6 +355,20 @@ public abstract class FieldMarshaller
|
||||
}
|
||||
}
|
||||
|
||||
protected static class ByteArrayMarshaller extends FieldMarshaller {
|
||||
public void setValue (Object po, PreparedStatement ps, int column)
|
||||
throws SQLException, IllegalAccessException {
|
||||
ps.setBytes(column, (byte[])_field.get(po));
|
||||
}
|
||||
public void getValue (ResultSet rs, Object po)
|
||||
throws SQLException, IllegalAccessException {
|
||||
_field.set(po, rs.getBytes(getColumnName()));
|
||||
}
|
||||
public String getColumnType () {
|
||||
return "VARBINARY";
|
||||
}
|
||||
}
|
||||
|
||||
protected Field _field;
|
||||
protected String _columnName, _columnDefinition;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user