Search in sources :

Example 6 with TransactionResult

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;
        }
    });
}
Also used : TransactionResult(com.abubusoft.kripton.android.sqlite.TransactionResult) Transaction(sqlite.kripton64.BindBean64ADataSource.Transaction) HashMap(java.util.HashMap) List(java.util.List) HashSet(java.util.HashSet) Test(org.junit.Test) BaseAndroidTest(base.BaseAndroidTest)

Example 7 with TransactionResult

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;
        }
    });
}
Also used : TransactionResult(com.abubusoft.kripton.android.sqlite.TransactionResult) Transaction(sqlite.kripton84.BindBean84ADataSource.Transaction) List(java.util.List) Test(org.junit.Test) BaseAndroidTest(base.BaseAndroidTest)

Example 8 with TransactionResult

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;
        }
    });
}
Also used : Comment(sqlite.quickstart.model.Comment) BindQuickStartDataSource(sqlite.quickstart.persistence.BindQuickStartDataSource) TransactionResult(com.abubusoft.kripton.android.sqlite.TransactionResult) User(sqlite.quickstart.model.User) Post(sqlite.quickstart.model.Post) BindQuickStartDaoFactory(sqlite.quickstart.persistence.BindQuickStartDaoFactory) Test(org.junit.Test) BaseAndroidTest(base.BaseAndroidTest)

Example 9 with TransactionResult

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());
    }
}
Also used : TransactionResult(com.abubusoft.kripton.android.sqlite.TransactionResult) SQLiteDatabase(android.database.sqlite.SQLiteDatabase) Builder(com.squareup.javapoet.FieldSpec.Builder) AbstractExecutable(com.abubusoft.kripton.android.sqlite.AbstractDataSource.AbstractExecutable) Logger(com.abubusoft.kripton.android.Logger) ParameterizedTypeName(com.squareup.javapoet.ParameterizedTypeName)

Example 10 with TransactionResult

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;
        }
    });
}
Also used : BindFamilyDaoFactory(sqlite.feature.jql.persistence.BindFamilyDaoFactory) TransactionResult(com.abubusoft.kripton.android.sqlite.TransactionResult) BindFamilyDataSource(sqlite.feature.jql.persistence.BindFamilyDataSource) List(java.util.List) Person(sqlite.feature.jql.entities.Person) Child(sqlite.feature.jql.entities.Child) Test(org.junit.Test) BaseAndroidTest(base.BaseAndroidTest)

Aggregations

TransactionResult (com.abubusoft.kripton.android.sqlite.TransactionResult)28 BaseAndroidTest (base.BaseAndroidTest)27 Test (org.junit.Test)27 List (java.util.List)13 Transaction (sqlite.feature.foreignKey.BindDummyDataSource.Transaction)4 One (com.abubusoft.kripton.common.One)3 HashMap (java.util.HashMap)3 HashSet (java.util.HashSet)3 Country (sqlite.feature.rx.model.Country)3 BindXenoDaoFactory (sqlite.feature.rx.persistence.BindXenoDaoFactory)3 BindXenoDataSource (sqlite.feature.rx.persistence.BindXenoDataSource)3 CountryDaoImpl (sqlite.feature.rx.persistence.CountryDaoImpl)3 Person (sqlite.feature.speed.model.Person)3 BindPersonDaoFactory (sqlite.feature.speed.persistence.BindPersonDaoFactory)3 BindPersonDataSource (sqlite.feature.speed.persistence.BindPersonDataSource)3 Transaction (sqlite.feature.speed.persistence.BindPersonDataSource.Transaction)3 PersonDaoImpl (sqlite.feature.speed.persistence.PersonDaoImpl)3 ArrayList (java.util.ArrayList)2 BindKripton180BeanDaoFactory (sqlite.feature.typeadapter.kripton180.bean.BindKripton180BeanDaoFactory)2 BindKripton180BeanDataSource (sqlite.feature.typeadapter.kripton180.bean.BindKripton180BeanDataSource)2