Skip to content

@BindDataSource

xcesco edited this page Jan 23, 2017 · 8 revisions

This annotation decorate an interface to define a datasource associated to a database schema. Between its attributes there is a DAO collection. Every DAO is defined by an interface annotated with @BindDao. Every DAO and is associated to a specific Java class which is associated to a specific table.

A data source class name have to finish with DataSource suffix.

Attributes

List of attributes is:

  • dao: collection of DAO associated to data source.
  • fileName: filename used to store database
  • version: database version. Default version is 1.
  • log: controls generation of log of SQL on logcat.
  • tableNamePrefix: rapresents a prefix of table name.
  • asyncTask: if true, generate async task name Bind<data source name without DataSource prefix>AsyncTask
  • cursor: if true, generate a wrapped cursor for every Java class managed by data source. Cursor's name is Bind<data source name without DataSource prefix>Cursor.

Example

Consider this interface to define a data source:

@BindDataSource(dao={DaoChannel.class}, fileName = "dummy" , version=1)
public interface Dummy01DataSource {
}

With this interface it is defined a data source name Dummy and is referred a DAO named DaoChannel.

When Kripton annotation processor process interface Dummy01DataSource, it generate a class name BindDummy01DataSource similar to:

public class BindDummy01DataSource extends AbstractDataSource implements BindDummy01DaoFactory, Dummy01DataSource {
  /**
   * <p><singleton of datasource,/p>
   */
  private static BindDummy01DataSource instance;

  /**
   * <p><file name used to save database,/p>
   */
  public static final String name = "dummy";

  /**
   * <p>database version</p>
   */
  public static final int version = 1;

  /**
   * <p>dao instance</p>
   */
  protected DaoChannelImpl daoChannel = new DaoChannelImpl(this);

  protected BindDummy01DataSource(Context context) {
    super(context, name, null, version);
  }

  @Override
  public DaoChannelImpl getDaoChannel() {
    return daoChannel;
  }

  /**
   * <p>executes a transaction. This method is synchronized to avoid concurrent problems. 
   * The database will be open in write mode.</p>
   *
   * @param transaction transaction to execute
   */
  public synchronized void execute(Transaction transaction) {
    SQLiteDatabase connection=openDatabase();
    try {
      connection.beginTransaction();
      if (transaction!=null && transaction.onExecute(this)) {
        connection.setTransactionSuccessful();
      }
    } catch(Throwable e) {
      Logger.error(e.getMessage());
      e.printStackTrace();
    } finally {
      connection.endTransaction();
      close();
    }
  }

  /**
   * instance
   */
  public static synchronized BindDummy01DataSource instance() {
    if (instance==null) {
      instance=new BindDummy01DataSource(KriptonLibrary.context());
    }
    return instance;
  }

  /**
   * onCreate
   */
  @Override
  public void onCreate(SQLiteDatabase database) {
    // generate tables
    Logger.info("DDL: %s",ChannelTable.CREATE_TABLE_SQL);
    database.execSQL(ChannelTable.CREATE_TABLE_SQL);
    if (databaseListener != null) {
      databaseListener.onCreate(database);
    }
  }

  /**
   * onUpgrade
   */
  @Override
  public void onUpgrade(SQLiteDatabase database, int oldVersion, int newVersion) {
    if (databaseListener != null) {
      databaseListener.onUpdate(database, oldVersion, newVersion, true);
    } else {
      // drop tables
      Logger.info("DDL: %s",ChannelTable.DROP_TABLE_SQL);
      database.execSQL(ChannelTable.DROP_TABLE_SQL);

      // generate tables
      Logger.info("DDL: %s",ChannelTable.CREATE_TABLE_SQL);
      database.execSQL(ChannelTable.CREATE_TABLE_SQL);
    }
  }

  /**
   * onConfigure
   */
  @Override
  public void onConfigure(SQLiteDatabase database) {
    // configure database
    if (databaseListener != null) {
      databaseListener.onConfigure(database);
    }
  }

  /**
   * interface to define transactions
   */
  public interface Transaction extends AbstractTransaction<BindDummy01DaoFactory> {
  }

  /**
   * Simple class implements interface to define transactions
   */
  public abstract static class SimpleTransaction implements Transaction {
    @Override
    public void onError(Throwable e) {
        Logger.error(e.getMessage());
        e.printStackTrace();
    }
  }
}

Generated data source class derives from AbstractDataSource that derive from SQLiteOpenHelper.

Usage

To use a data source in a client application, just retrieve a reference to singleton instance of data source

BindDummy01DataSource dataSource = BindDummy01DataSource.instance();

To execute a transaction, just invoke the following code

dataSource.execute(new SimpleTransaction() {

  @Override
  public boolean onExecute(BindDummy01DaoFactory daoFactory) {
    DaoChannelImpl dao = daoFactory.getDaoChannel();

    long result = dao.insertRaw1("test", 52);
    dao.insertRaw2("test2", 23)
    
    // commit transaction
    return true;
  }

});

If you want to use directly dao without a transaction just retrieve DAO implementation from data source

DaoChannelImpl dao=dataSource.getDaoChannel();
dao.insertRaw1("test", 52);

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