-
Notifications
You must be signed in to change notification settings - Fork 16
SQL Type adapter
xcesco edited this page May 14, 2018
·
3 revisions
Kripton manages many field type without any particular configuration to do. If you need to persist an unsupported type, you can easily do it with the use of SQL Type Adapter. To use a SQL Type Adapter, it is necessary to use @BindSqlAdapter annotation. Kripton does not support natively Bitmap field. Let me show you how to support them:
@BindSqlType
public class Person {
public long id;
@BindSqlAdapter(adapter = BitmapTypeAdapter.class)
public Bitmap image;
}
And associated SQL type adapter:
public class BitmapTypeAdapter implements SqlTypeAdapter<Bitmap, byte[]> {
@Override
public Bitmap toJava(byte[] dataValue) {
if (dataValue == null)
return null;
return BitmapFactory.decodeByteArray(dataValue, 0, dataValue.length);
}
@Override
public byte[] toData(Bitmap bitmap) {
if (bitmap == null)
return null;
ByteArrayOutputStream stream = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.PNG, 100, stream);
return stream.toByteArray();
}
@Override
public String toString(Bitmap javaValue) {
throw (new KriptonRuntimeException("Unsupported operation!"));
}
}
A SQL Type Adapter class is a SqlTypeAdapter
interface implementation. It has two parameter type: the first is the field type (in our example is Bitmap), the second is the type that we want to use as replacement and that will be used to store data into an SQLite table column. It implements three methods:
-
toJava
: converts data retrieved from an SQLite table into a field. -
toData
: converts a field into data to store into an SQLite table. -
toString
: used if you want to use a field as query's parameter (SQLite wrapper for Android convert all SQL parameter used in where condition in its string representation).
- Introduction
- Goals & Features
- Kotlin
- Immutable or Mutable Pojo
- Annotation Processor Args
- Credits
- Articles
- Benchmarks
- Setup
- Tutorial
- Usage
- Dependencies and inspirations
- Stackoverflow
- Documentation
- SQL logging
- Data source options
- Indices
- SQL Type adapter
- Global SQL Type adapter
- Constraints
- Live data: welcome Architectural components!!
- Paged Live data
- Dynamic parts
- Transactional and batch operations
- Async Transactional and batch operations
- Global transaction
- Support for immutable POJO
- Generate Content provider
- Generate Database schema generation
- Database migration
- BindSqlColumn
- BindContentProvider
- BindContentProviderEntry
- BindContentProviderPath
- BindDao
- BindDaoMany2Many
- BindDataSource
- BindDataSourceOptions
- BindDataSourceUpdateTask
- BindIndex
- BindSqlRelation
- BindSqlAdapter
- BindSqlChildSelect
- BindSqlDelete
- BindSqlDynamicOrderBy
- BindSqlDynamicWhere
- BindSqlDynamicWhereParams
- BindSqlInsert
- BindSqlPageSize
- BindSqlParam
- BindSqlSelect
- BindSqlUpdate
- BindSqlType
- BindSqlTransaction