After much too-ing and fro-ing DBEnums have been finalised and added to the available datatypes.
Previously I talked about using sub-subclasses of DBRow and the example used a field called 'typeNumber' to automatically limit the results. Enumerations would help turn the database's inscrutable values into nice Java objects.
So toaomalkster, with a little distraction from me, has added DBIntegerEnum and DBStringEnum.
These are intended to be used instead of DBInteger and DBString when the database values have a special meaning. For instance in my earlier blog entry a 1 meant manager and a 2 meant Team Leader. For obvious reasons DBV can't generate DBEnums automatically so you'll have to add them yourselves.
Fortunately this is easy: change DBInteger to DBIntegerEnum<YourEnumClass> then define YourEnumClass as, for example:
public enum RecordType implements DBEnumValue<Integer> { YOUR_FIRST_ENUM_RECORD(1), YOUR_SECOND_ENUM_RECORD(2)... ; private int code; private RecordType(int code) { this.code = code; } /** Gets the integer value stored in the database */ @Override public Integer getCode(){ return code; } // Other methods as required }
After that DBV will automatically turn the literal database value into an enumeration value for you to use. The enumeration can be used to set the field, limit the permitted and excluded values, and do everything else a String, Number, or Date can do.
I'm looking forward to adding enumerations to the 400+ tables at work, sort of ...
Anonymous
DBStringEnum works just the same but replace "Integer" with "String".
You can use it with Boolean, Date, Double, etc but I've never encountered a DB enumeration like that. Do you have an example?