KittyORM datatypes mapping
By design instead of mapping java types to SQLite supported SQL datatypes KittyORM maps them directly into SQLite affinities, KittyORM supports predefined mapping for primitive java types, primitive wrappers, byte array, enumerations and following types: java.lang.String
, java.util.Date
, java.util.Currency
, java.util.Calendar
, java.sql.Timestamp
, java.math.BigInteger
, java.math.BigDecimal
, java.io.File
, android.net.Uri
.
TEXT
datatype affinity mapping
SQLite affinity | Java type | Notes |
---|---|---|
TEXT |
Enumurations | To SQLite: Enum someValue.name() ; from SQLite: fieldType.getMethod("valueOf", String.class).invoke(fieldType, cursor.getString(someIndex)) |
TEXT |
java.lang.String |
- |
TEXT |
java.math.BigDecimal |
To SQLite: BigDecimal someValue.toString() ; from SQLite: new BigDecimal(cursor.getString(someIndex)) |
TEXT |
android.net.Uri |
To SQLite: Uri someValue.toString() ; from SQLite: Uri.parse(cursor.getString(someIndex)) |
TEXT |
java.io.File |
To SQLite: File someValue.getAbsolutePath() ; from SQLite: new File(cursor.getString(someIndex)) |
TEXT |
java.math.BigInteger |
To SQLite: BigInteger someValue.toString() ; from SQLite: new BigInteger(cursor.getString(someIndex)) |
TEXT |
java.util.Currency |
To SQLite: Currency someValue.getCurrencyCode() ; from SQLite: Currency.getInstance(cursor.getString(someIndex)) |
INTEGER
datatype affinity mapping
SQLite affinity | Java type | Notes |
---|---|---|
INTEGER |
java.util.Calendar |
To SQLite: Calendar someValue.getTimeInMillis() ; from SQLite: Calendar.getInstance().setTimeInMillis( cursor.getLong(someIndex) ) |
INTEGER |
java.sql.Timestamp |
To SQLite: Timestamp someValue.getTime() ; from SQLite: new Timestamp(cursor.getLong(someIndex)) |
INTEGER |
java.util.Date |
To SQLite: Date someValue.getTime() ; from SQLite: new Date(cursor.getLong(someIndex)) |
INTEGER |
byte |
- |
INTEGER |
int |
- |
INTEGER |
long |
- |
INTEGER |
short |
- |
INTEGER |
boolean |
To SQLite: boolean someValue ? 1 : 0 ; from SQLite: cursor.getInt(someIndex) == 1 |
INTEGER |
java.lang.Byte |
- |
INTEGER |
java.lang.Integer |
- |
INTEGER |
java.lang.Long |
- |
INTEGER |
java.lang.Short |
- |
INTEGER |
java.lang.Boolean |
To SQLite: Boolean someValue ? 1 : 0 ; from SQLite: cursor.getInt(someIndex) == 1 |
REAL
datatype affinity mapping
SQLite affinity | Java type | Notes |
---|---|---|
REAL |
float |
- |
REAL |
double |
- |
REAL |
java.lang.Float |
- |
REAL |
java.lang.Double |
- |
NONE
datatype affinity mapping
SQLite affinity | Java type | Notes |
---|---|---|
NONE |
byte[] |
- |
NONE |
java.lang.Byte[] |
- |
Custom mapping rules
By default, KittyORM provides mapping of most java types that you may want to use. However, KittyORM also offers some functionality for user defined mapping rules e.g. you can tell KittyORM how to store and retrieve any java objects you want. For those purposes KittyORM has KITTY_COLUMN_SERIALIZATION
annotation with what you can achieve storing your objects or objects data as TEXT
or NONE
SQLite datatype affinities at your database. To use it, take following steps:
Define at
KittyModel.class
implementation model table field with SQLite datatype affinity specified explicitly (TypeAffinities.TEXT
,TypeAffinities.BLOB
orTypeAffinities.NONE
only).1// Saving to text 2@KITTY_COLUMN( 3 columnOrder = 18, 4 columnAffinity = TypeAffinities.TEXT 5) 6@KITTY_COLUMN_SERIALIZATION 7public AnimalSounds stringSDF; 8 9// Saving to blob 10@KITTY_COLUMN( 11 columnOrder = 20, 12 columnAffinity = TypeAffinities.BLOB 13) 14@KITTY_COLUMN_SERIALIZATION 15public Bitmap byteArraySDF;
Write your methods how to transform your object to string\blob and back. If
serializationMethodName
ordeserializationMethodName
ofKITTY_COLUMN_SERIALIZATION
were not specified explicitly than KittyORM would try to call methodString\byte[] "fieldname" + Serialize
for serialization (no parameters) andYourType "fieldName" + Deserialize
for deserialization (String\byte[] fromCursor
as parameter). For example, for model fieldAnimalSounds stringSDF
default serialization method would beString stringSDFSerialize()
and default deserialization method would beAnimalSounds stringSDFDeserialize(String cvData)
.1String stringSDFSerialize() { 2 if(stringSDF == null) return null; 3 return new GsonBuilder().create().toJson(stringSDF); 4} 5 6AnimalSounds stringSDFDeserialize(String cvData) { 7 if(cvData == null) return null; 8 if(cvData.length() == 0) return null; 9 return new GsonBuilder().create().fromJson(cvData, AnimalSounds.class); 10} 11 12public byte[] byteArraySDFSerialize() {//byteArraySDFSerialize 13 if(byteArraySDF == null) return null; 14 ByteArrayOutputStream bmpStream = new ByteArrayOutputStream(); 15 byteArraySDF.compress(Bitmap.CompressFormat.PNG, 100, bmpStream); 16 return bmpStream.toByteArray(); 17} 18 19public Bitmap byteArraySDFDeserialize(byte[] cursorData) { 20 if(cursorData == null) return null; 21 if(cursorData.length == 0) return null; 22 return BitmapFactory.decodeByteArray(cursorData, 0, cursorData.length); 23}
Now you are ready for using custom mapping rules with KittyORM. Congratulations.