diff --git a/src/main/java/com/samskivert/depot/impl/FieldMarshaller.java b/src/main/java/com/samskivert/depot/impl/FieldMarshaller.java index d674c85..323f646 100644 --- a/src/main/java/com/samskivert/depot/impl/FieldMarshaller.java +++ b/src/main/java/com/samskivert/depot/impl/FieldMarshaller.java @@ -423,6 +423,10 @@ public abstract class FieldMarshaller @SuppressWarnings("unchecked") Class dtype = (Class)ftype; return new ByteEnumMarshaller(dtype); + } else if (Enum.class.isAssignableFrom(ftype)) { + @SuppressWarnings("unchecked") Class dtype = (Class)ftype; + return new EnumMarshaller(dtype); + } else { return null; } @@ -728,6 +732,36 @@ public abstract class FieldMarshaller protected Class _eclass; } + protected static class EnumMarshaller> extends FieldMarshaller { + public EnumMarshaller (Class clazz) { + _eclass = clazz; + } + + @Override public String getColumnType (ColumnTyper typer, int length) { + return typer.getStringType(length); + } + @Override public E getFromObject (Object po) + throws IllegalArgumentException, IllegalAccessException { + @SuppressWarnings("unchecked") E value = (E) _field.get(po); + return value; + } + @Override public E getFromSet (ResultSet rs) throws SQLException { + String svalue = rs.getString(getColumnName()); + return (svalue == null) ? null : Enum.valueOf(_eclass, svalue); + } + @Override public void writeToObject (Object po, E value) + throws IllegalArgumentException, IllegalAccessException { + _field.set(po, value); + } + @Override public void writeToStatement (PreparedStatement ps, int column, E value) + throws SQLException { + String svalue = (value == null) ? null : value.name(); + ps.setString(column, svalue); + } + + protected Class _eclass; + } + // used to fool the type system when creating ByteEnumMarshallers; look away protected static enum Dummy implements ByteEnum { DUMMY; diff --git a/src/test/java/com/samskivert/depot/AllTypesRecord.java b/src/test/java/com/samskivert/depot/AllTypesRecord.java index 4de61d3..260b2bc 100644 --- a/src/test/java/com/samskivert/depot/AllTypesRecord.java +++ b/src/test/java/com/samskivert/depot/AllTypesRecord.java @@ -57,6 +57,7 @@ public class AllTypesRecord extends PersistentRecord public static final ColumnExp DATE = colexp(_R, "date"); public static final ColumnExp TIME = colexp(_R, "time"); public static final ColumnExp TIMESTAMP = colexp(_R, "timestamp"); + public static final ColumnExp TEST_ENUM = colexp(_R, "testEnum"); public static final ColumnExp NULL_BOXED_BOOLEAN = colexp(_R, "nullBoxedBoolean"); public static final ColumnExp NULL_BOXED_BYTE = colexp(_R, "nullBoxedByte"); public static final ColumnExp NULL_BOXED_SHORT = colexp(_R, "nullBoxedShort"); @@ -70,10 +71,13 @@ public class AllTypesRecord extends PersistentRecord public static final ColumnExp NULL_DATE = colexp(_R, "nullDate"); public static final ColumnExp NULL_TIME = colexp(_R, "nullTime"); public static final ColumnExp NULL_TIMESTAMP = colexp(_R, "nullTimestamp"); + public static final ColumnExp NULL_TEST_ENUM = colexp(_R, "nullTestEnum"); // AUTO-GENERATED: FIELDS END public static final int SCHEMA_VERSION = 1; + public enum TestEnum { ONE, TWO, THREE; } + @Id public int recordId; public boolean booleanValue; @@ -102,6 +106,8 @@ public class AllTypesRecord extends PersistentRecord // public Blob blob; // tested by byte[] // public Clob clob; // not clear how to test this + public TestEnum testEnum; + @Column(nullable=true) public Boolean nullBoxedBoolean; @Column(nullable=true) public Byte nullBoxedByte; @Column(nullable=true) public Short nullBoxedShort; @@ -115,6 +121,7 @@ public class AllTypesRecord extends PersistentRecord @Column(nullable=true) public Date nullDate; @Column(nullable=true) public Time nullTime; @Column(nullable=true) public Timestamp nullTimestamp; + @Column(nullable=true) public TestEnum nullTestEnum; @Override public boolean equals (Object other) { if (!(other instanceof AllTypesRecord)) { @@ -141,6 +148,7 @@ public class AllTypesRecord extends PersistentRecord date.equals(orec.date) && time.equals(orec.time) && timestamp.equals(orec.timestamp) && + testEnum.equals(orec.testEnum) && (nullBoxedBoolean == orec.nullBoxedBoolean) && (nullBoxedByte == orec.nullBoxedByte) && (nullBoxedShort == orec.nullBoxedShort) && @@ -153,7 +161,8 @@ public class AllTypesRecord extends PersistentRecord (nullString == orec.nullString) && (nullDate == orec.nullDate) && (nullTime == orec.nullTime) && - (nullTimestamp == orec.nullTimestamp); + (nullTimestamp == orec.nullTimestamp) && + (nullTestEnum == orec.nullTestEnum); } public static AllTypesRecord createRecord (int recordId) @@ -180,6 +189,7 @@ public class AllTypesRecord extends PersistentRecord rec.date = Date.valueOf("2010-01-18"); rec.time = Time.valueOf("19:19:19"); rec.timestamp = new Timestamp(System.currentTimeMillis()+20); + rec.testEnum = TestEnum.TWO; return rec; }