use of com.abubusoft.kripton.android.sqlite.TransactionResult in project kripton by xcesco.
the class Test64BRuntime method testRunSqlite.
@Test
public void testRunSqlite() {
BindBean64ADataSource dataSource = BindBean64ADataSource.instance();
dataSource.execute(new Transaction() {
@Override
public TransactionResult onExecute(BindBean64ADaoFactory daoFactory) {
Bean64ADaoImpl dao = daoFactory.getBean64ADao();
Bean64A bean = new Bean64A();
bean.valueString = "hello";
bean.valueMapStringBean = new HashMap<>();
bean.valueMapStringBean.put("key1", new Bean64A());
bean.valueSetString = new HashSet<String>();
bean.valueSetString.add("hello");
dao.insert(bean);
List<Bean64A> list = dao.selectList(bean.id);
Assert.assertEquals("not list ", 1, list.size());
Assert.assertEquals("not map", 1, list.size());
Assert.assertEquals("not set", 1, list.get(0).valueSetString.size());
return TransactionResult.COMMIT;
}
});
}
use of com.abubusoft.kripton.android.sqlite.TransactionResult in project kripton by xcesco.
the class Test84RuntimeA method testRunSqlite.
@Test
public void testRunSqlite() throws IOException, InstantiationException, IllegalAccessException {
Assert.assertNotNull(Bean84ATable.class.getName() != null);
Assert.assertNotNull(Bean84ADaoImpl.class.getName() != null);
BindBean84ADataSource dataSource = BindBean84ADataSource.instance();
// dataSource.openWritableDatabase();
dataSource.execute(new Transaction() {
@Override
public TransactionResult onExecute(BindBean84ADaoFactory daoFactory) {
Bean84ADaoImpl dao = daoFactory.getBean84ADao();
Bean84A bean = new Bean84A();
bean.valueString = "hello";
dao.insertAll(bean);
List<Bean84A> list = dao.selectById(bean.id);
Assert.assertEquals("not list ", 1, list.size());
Assert.assertEquals("not map", 1, list.size());
return TransactionResult.COMMIT;
}
});
}
use of com.abubusoft.kripton.android.sqlite.TransactionResult in project kripton by xcesco.
the class TestQuickstartRuntime method testRunSqlite1.
@Test
public void testRunSqlite1() {
BindQuickStartDataSource dataSource = BindQuickStartDataSource.instance();
dataSource.execute(new BindQuickStartDataSource.Transaction() {
@Override
public TransactionResult onExecute(BindQuickStartDaoFactory daoFactory) {
User user = new User();
user.id = 1;
user.name = "user";
user.username = "username";
daoFactory.getUserDao().insert(user);
Post post = new Post();
post.id = 2;
post.title = "post";
post.userId = user.id;
daoFactory.getPostDao().insert(post);
Comment comment = new Comment();
comment.id = 3;
comment.postId = post.id;
daoFactory.getCommentDao().insert(comment);
return TransactionResult.COMMIT;
}
});
}
use of com.abubusoft.kripton.android.sqlite.TransactionResult in project kripton by xcesco.
the class BindDataSourceBuilder method generateMethodExecuteTransaction.
/**
* <p>
* Generate transaction an execute method
* </p>
*
* @param daoFactory
*/
public void generateMethodExecuteTransaction(String daoFactory) {
// create interface
String transationExecutorName = "Transaction";
// @formatter:off
ParameterizedTypeName parameterizedTypeName = ParameterizedTypeName.get(className(AbstractExecutable.class), className(daoFactory));
classBuilder.addType(TypeSpec.interfaceBuilder(transationExecutorName).addModifiers(Modifier.PUBLIC).addSuperinterface(parameterizedTypeName).addJavadoc("Rapresents transational operation.\n").addMethod(MethodSpec.methodBuilder("onExecute").addParameter(className(daoFactory), "daoFactory").addJavadoc("Execute transation. Method need to return {@link TransactionResult#COMMIT} to commit results\nor {@link TransactionResult#ROLLBACK} to rollback.").addJavadoc("\nIf exception is thrown, a rollback will be done.").addJavadoc("\n\n@param daoFactory\n@return\n@throws Throwable\n").addModifiers(Modifier.PUBLIC, Modifier.ABSTRACT).returns(TransactionResult.class).build()).build());
// @formatter:on
{
MethodSpec.Builder executeMethod = MethodSpec.methodBuilder("execute").addModifiers(Modifier.PUBLIC).addParameter(className(transationExecutorName), "transaction");
// generate javadoc
executeMethod.addJavadoc("<p>Executes a transaction. This method <strong>is thread safe</strong> to avoid concurrent problems. The " + "drawback is only one transaction at time can be executed. The database will be open in write mode. This method uses default error listener to intercept errors.</p>\n");
executeMethod.addJavadoc("\n");
executeMethod.addJavadoc("@param transaction\n\ttransaction to execute\n");
executeMethod.addStatement("execute(transaction, onErrorListener)");
classBuilder.addMethod(executeMethod.build());
}
{
MethodSpec.Builder executeMethod = MethodSpec.methodBuilder("execute").addModifiers(Modifier.PUBLIC).addParameter(className(transationExecutorName), "transaction").addParameter(className(OnErrorListener.class), "onErrorListener");
executeMethod.addStatement("boolean needToOpened=!this.isOpenInWriteMode()");
executeMethod.addStatement("boolean success=false");
executeMethod.addCode("@SuppressWarnings(\"resource\")\n");
executeMethod.addStatement("$T connection=needToOpened ? openWritableDatabase() : database()", SQLiteDatabase.class);
// support for live data
executeMethod.addStatement("$L currentDaoFactory=_daoFactorySingleThread.bindToThread()", DATA_SOURCE_SINGLE_THREAD_NAME);
executeMethod.addStatement("currentDaoFactory.onSessionOpened()");
executeMethod.beginControlFlow("try");
executeMethod.addCode("connection.beginTransaction();\n");
executeMethod.beginControlFlow("if (transaction!=null && $T.$L == transaction.onExecute(currentDaoFactory))", TransactionResult.class, TransactionResult.COMMIT);
executeMethod.addStatement("connection.setTransactionSuccessful()");
// support for live data
executeMethod.addStatement("success=true");
executeMethod.endControlFlow();
executeMethod.nextControlFlow("catch($T e)", Throwable.class);
executeMethod.addStatement("$T.error(e.getMessage())", Logger.class);
executeMethod.addStatement("e.printStackTrace()");
executeMethod.addStatement("if (onErrorListener!=null) onErrorListener.onError(e)");
executeMethod.nextControlFlow("finally");
executeMethod.beginControlFlow("try");
executeMethod.addStatement("connection.endTransaction()");
executeMethod.nextControlFlow("catch ($T e)", Throwable.class);
executeMethod.addStatement("$T.warn(\"error closing transaction %s\", e.getMessage())", Logger.class);
executeMethod.endControlFlow();
executeMethod.addCode("if (needToOpened) { close(); }\n");
// support for live data
executeMethod.addCode("if (success) { currentDaoFactory.onSessionClosed(); } else { currentDaoFactory.onSessionClear(); }\n");
executeMethod.endControlFlow();
// generate javadoc
executeMethod.addJavadoc("<p>Executes a transaction. This method <strong>is thread safe</strong> to avoid concurrent problems. The " + "drawback is only one transaction at time can be executed. The database will be open in write mode.</p>\n");
executeMethod.addJavadoc("\n");
executeMethod.addJavadoc("@param transaction\n\ttransaction to execute\n");
executeMethod.addJavadoc("@param onErrorListener\n\terror listener\n");
classBuilder.addMethod(executeMethod.build());
}
}
use of com.abubusoft.kripton.android.sqlite.TransactionResult in project kripton by xcesco.
the class TestFeatJQLRuntime method testJQL1.
@Test
public void testJQL1() {
BindFamilyDataSource dataSource = BindFamilyDataSource.instance();
// transaction to insert elements
dataSource.execute(new BindFamilyDataSource.Transaction() {
@Override
public TransactionResult onExecute(BindFamilyDaoFactory daoFactory) {
// TODO Auto-generated method stub
Person person = new Person();
person.name = "Tonj Manero";
daoFactory.getDaoPerson().insertBean(person);
Child child = new Child();
child.name = "Luna";
child.parentId = person.id;
daoFactory.getDaoChild().insertBean(child);
daoFactory.getDaoChild().insertBean(child);
List<Child> list = daoFactory.getDaoChild().selectByParent(person.id);
assertTrue(2 == list.size());
for (int i = 0; i < list.size(); i++) {
assertTrue(list.get(i).name.equals("Luna"));
}
return TransactionResult.COMMIT;
}
});
}
Aggregations