-
Notifications
You must be signed in to change notification settings - Fork 10
Object Transformers
For persisting third party objects and objects that do not need their own table, SqliteMagic has object transformers. Object transformer transforms unsupported Java data type into some primitively supported data type.
Example scenario:
java.util.Date is a third party non-primitive data type. By using object transformer we can translate this type into supported java.lang.Long type, which then by the system is translated into SQLite INTEGER type in the database.
In order to define a object transformer just create 2 static methods with annotations @ObjectToDbValue, which transforms transformable object into supported type and @DbValueToObject, which does the opposite. Method names can be anything.
That's it - the annotation processor can take it from there.
Example:
public final class DateTransformer {
@ObjectToDbValue
public static Long objectToDbValue(Date javaObject) {
return javaObject == null ? null : javaObject.getTime();
}
@DbValueToObject
public static Date dbValueToObject(Long dbObject) {
return dbObject == null ? null : new Date(dbObject);
}
}Type converters can be in either inside the same class or separate classes, classes can have any other methods and even any other transformer methods etc. The main requirement is that there must be 2 static methods with the appropriate annotations. Also, there can only be one transformer for each type.
In order to keep the runtime library as lightweight as possible, the only transformer included out of the box is for boolean/Boolean (since SQLite does not support boolean types).