Skip to content

@BindDataSourceOptions

xcesco edited this page Apr 7, 2018 · 3 revisions

Used to define data-source options with annotation. There are two ways to customize data source creating: by code or by annotation. This annotation implements the second way. 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 with @BindDataSourceOptions:

@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();

Attributes

  • cursorFactory: cursorFactory
  • databaseErrorHandler: databaseErrorHandler
  • databaseLifecycleHandler: databaseLifecycleHandler
  • inMemory: if true, generate database in memory.
  • logEnabled: Log enabled. This flag controls log that is not generated by annotation processor, but the log associated to database operation like open/close connnections, create tables and so on.
  • populator: is executed after database creation.
  • updateTasks: Allows to specify migration task between versions.

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