Skip to content

Data source options

xcesco edited this page Apr 26, 2018 · 4 revisions

There are two ways to customize data source creation: by code or by annotation.

When data-source is created, usually at application startup, it is possible to define some features: if it is in-memory if DDL log is enabled, the populator and the migration-version tasks and so on.

By code, it is possible to initialize data source using DataSourceOptions and its builder.

DataSourceOptions.Builder optionsBuilder=DataSourceOptions.builder();			
  optionsBuilder.inMemory(false);
  optionsBuilder.databaseLifecycleHandler(new DatabaseLifecycleHandler() {
			
  @Override
  public void onUpdate(SQLiteDatabase database, int oldVersion, int newVersion, boolean upgrade) {
    Logger.info("databaseLifecycleHandler - onUpdate");
  }

  @Override
  public void onCreate(SQLiteDatabase database) {
    Logger.info("databaseLifecycleHandler - onCreate "+database.getVersion());
    ...
  }

  @Override
  public void onConfigure(SQLiteDatabase database) {
   Logger.info("databaseLifecycleHandler - onConfigure");
    ...
  }
});
optionsBuilder.addUpdateTask(1, new SQLiteUpdateTask() {
  @Override
  public void execute(SQLiteDatabase database, int previousVersion, int currentVersion) {
    ...
  }
});

// create data source		
BindAppWithConfigDataSource ds=BindAppWithConfigDataSource.build(optionsBuilder.build());

To achieve the same result with @BindDataSourceOptions just define data source:

@BindDataSourceOptions(
  logEnabled = true, 
  populator = PersonPopulator.class, 
  cursorFactory = PersonCursorFactory.class, 
  databaseLifecycleHandler=PersonLifecycleHandler.class, 
  updateTasks = {
    @BindDataSourceUpdateTask(version = 2, task = PersonUpdateTask.class)
  })
@BindDataSource(daoSet = { DaoPerson.class }, fileName = "app.db")
public interface AppWithConfigDataSource {

}

And the code for creating data source will become

// create data source		
BindAppWithConfigDataSource ds=BindAppWithConfigDataSource.instance();

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