From 8620ace13db92eb7e60913bcca8916cda1be59a9 Mon Sep 17 00:00:00 2001 From: Michael Bayne Date: Thu, 21 Sep 2006 00:31:21 +0000 Subject: [PATCH] Added support for byte arrays and full table loads. --- .../jdbc/depot/DepotMarshaller.java | 12 +++-- .../jdbc/depot/DepotRepository.java | 9 ++++ .../jdbc/depot/FieldMarshaller.java | 46 +++++++++++++------ 3 files changed, 48 insertions(+), 19 deletions(-) diff --git a/src/java/com/samskivert/jdbc/depot/DepotMarshaller.java b/src/java/com/samskivert/jdbc/depot/DepotMarshaller.java index 4186198..5531a85 100644 --- a/src/java/com/samskivert/jdbc/depot/DepotMarshaller.java +++ b/src/java/com/samskivert/jdbc/depot/DepotMarshaller.java @@ -197,16 +197,20 @@ public class DepotMarshaller /** * 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; } diff --git a/src/java/com/samskivert/jdbc/depot/DepotRepository.java b/src/java/com/samskivert/jdbc/depot/DepotRepository.java index 39eb6cf..66a8c35 100644 --- a/src/java/com/samskivert/jdbc/depot/DepotRepository.java +++ b/src/java/com/samskivert/jdbc/depot/DepotRepository.java @@ -79,6 +79,15 @@ public class DepotRepository }); } + /** + * Loads all persistent objects of the specified type. + */ + protected > Collection findAll (Class type) + throws PersistenceException + { + return findAll(type, null); + } + /** * Loads all persistent objects that match the specified key. */ diff --git a/src/java/com/samskivert/jdbc/depot/FieldMarshaller.java b/src/java/com/samskivert/jdbc/depot/FieldMarshaller.java index 6d1fc29..8e79901 100644 --- a/src/java/com/samskivert/jdbc/depot/FieldMarshaller.java +++ b/src/java/com/samskivert/jdbc/depot/FieldMarshaller.java @@ -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; }