Skip to content

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).

Table of Contents

Query definition

Features

Relations

Multithread supports

Modularization

Annotations for data convertion

Annotations for SQLite ORM

Annotations for shared preferences

Clone this wiki locally