Skip to content

Global SQL Type adapter

xcesco edited this page May 2, 2018 · 1 revision

SQL type adapters are usually defined directly on field declaration. When you need to use same SQL Type Adapter for all field of a specific type for all data model classes, you have two choices: mark every field of that type with needed type adapter or define this global type adapter at data-source level. The first case is already analyzed in SQL Type adapter page. For the second case, it can be useful get an example:

Suppose we want to manage all Date field as Long type (default behaviour is stored them in a string). To do this just define the data-source, using @BindDataSource, in the following as follows:

@BindDataSource(daoSet = { DaoPerson.class }, 
  typeAdapters = {
    @BindSqlAdapter(adapter = Date2Long.class) },
  fileName = "person.db")
public interface AppDataSource {

}

And Date2Long type adapter class:

public class Date2Long implements SqlTypeAdapter<Date, Long> {
  @Override
  public Date toJava(Long dataValue) {
    if (dataValue != null) {
      return new Date(2);
    }
    return null;
  }

  @Override
  public Long toData(Date javaValue) {
    if (javaValue != null) {
      return javaValue.getTime();
    }
    return null;
  }

  @Override
  public String toString(Date javaValue) {
    return null;
  }
}

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