Skip to content

@BindSqlAdapter

xcesco edited this page May 2, 2018 · 1 revision

This annotation decorates a field to use a particular SQL Type Adapter to customize persistence on an SQLite table. A type adapter must implements SqlTypeAdapter interface. 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).

Attributes

  • adapter: SQLite Type Adapter class

Usage

@BindTable
public class Person {	
  public long id;

  @BindSqlAdapter(adapter = BitmapTypeAdapter.class)
  public Bitmap image;
}

And the 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!"));
  }
}

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