-
Notifications
You must be signed in to change notification settings - Fork 16
@BindDataSource
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.
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.
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.
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);- Introduction
- Goals & Features
- Kotlin
- Immutable or Mutable Pojo
- Annotation Processor Args
- Credits
- Articles
- Benchmarks
- Setup
- Tutorial
- Usage
- Dependencies and inspirations
- Stackoverflow
- Documentation
- SQL logging
- Data source options
- Indices
- SQL Type adapter
- Global SQL Type adapter
- Constraints
- Live data: welcome Architectural components!!
- Paged Live data
- Dynamic parts
- Transactional and batch operations
- Async Transactional and batch operations
- Global transaction
- Support for immutable POJO
- Generate Content provider
- Generate Database schema generation
- Database migration
- BindSqlColumn
- BindContentProvider
- BindContentProviderEntry
- BindContentProviderPath
- BindDao
- BindDaoMany2Many
- BindDataSource
- BindDataSourceOptions
- BindDataSourceUpdateTask
- BindIndex
- BindSqlRelation
- BindSqlAdapter
- BindSqlChildSelect
- BindSqlDelete
- BindSqlDynamicOrderBy
- BindSqlDynamicWhere
- BindSqlDynamicWhereParams
- BindSqlInsert
- BindSqlPageSize
- BindSqlParam
- BindSqlSelect
- BindSqlUpdate
- BindSqlType
- BindSqlTransaction