Search in sources :

Example 6 with One

use of com.abubusoft.kripton.common.One in project kripton by xcesco.

the class JQLBuilder method buildJQLInsert.

/**
 * <pre>
 *
 * INSERT INTO person (name, surname, birth_city, birth_day) VALUES (${name}, ${surname}, ${birthCity}, ${birthDay})
 * </pre>
 *
 * @param method
 * @param preparedJql
 * @return
 */
private static JQL buildJQLInsert(SQLiteModelMethod method, final JQL result, String preparedJql) {
    if (StringUtils.hasText(preparedJql)) {
        result.value = preparedJql;
        // INSERT can contains bind parameter in column values and select
        // statement
        final One<Boolean> inColumnValueSet = new One<Boolean>(false);
        final One<Boolean> inWhereStatement = new One<Boolean>(false);
        JQLChecker.getInstance().analyze(method, result, new JqlBaseListener() {

            @Override
            public void enterConflict_algorithm(Conflict_algorithmContext ctx) {
                result.conflictAlgorithmType = ConflictAlgorithmType.valueOf(ctx.getText().toUpperCase());
            }

            @Override
            public void enterProjected_columns(Projected_columnsContext ctx) {
                result.containsSelectOperation = true;
            }

            @Override
            public void enterWhere_stmt(Where_stmtContext ctx) {
                inWhereStatement.value0 = true;
            }

            @Override
            public void exitWhere_stmt(Where_stmtContext ctx) {
                inWhereStatement.value0 = false;
            }

            @Override
            public void enterColumn_value_set(Column_value_setContext ctx) {
                inColumnValueSet.value0 = true;
            }

            @Override
            public void exitColumn_value_set(Column_value_setContext ctx) {
                inColumnValueSet.value0 = false;
            }

            @Override
            public void enterBind_parameter(Bind_parameterContext ctx) {
                if (inWhereStatement.value0) {
                    result.bindParameterOnWhereStatementCounter++;
                } else if (inColumnValueSet.value0) {
                    result.bindParameterAsColumnValueCounter++;
                }
                AssertKripton.assertTrue(inWhereStatement.value0 || inColumnValueSet.value0, "unknown situation!");
            }
        });
        if (result.containsSelectOperation) {
            AssertKripton.assertTrueOrInvalidMethodSignException(method.getReturnClass().equals(TypeName.VOID), method, "defined JQL requires that method's return type is void");
        }
    // ASSERT: a INSERT-SELECT SQL can not contains parameters on values
    // section.
    } else {
        // use annotation's attribute value and exclude and bean definition
        // to
        final Class<? extends Annotation> annotation = BindSqlInsert.class;
        final SQLiteDaoDefinition dao = method.getParent();
        final boolean includePrimaryKey = AnnotationUtility.extractAsBoolean(method.getElement(), annotation, AnnotationAttributeType.INCLUDE_PRIMARY_KEY);
        // define field list
        // every method parameter can be used only as insert field
        InsertType insertResultType = SqlInsertBuilder.detectInsertType(method);
        Set<String> fields;
        if (insertResultType == InsertType.INSERT_BEAN) {
            fields = extractFieldsFromAnnotation(method, annotation, includePrimaryKey);
        } else {
            fields = extractFieldsFromMethodParameters(method, annotation);
        }
        result.conflictAlgorithmType = ConflictAlgorithmType.valueOf(AnnotationUtility.extractAsEnumerationValue(method.getElement(), annotation, AnnotationAttributeType.CONFLICT_ALGORITHM_TYPE));
        StringBuilder builder = new StringBuilder();
        builder.append(INSERT_KEYWORD);
        builder.append(" " + result.conflictAlgorithmType.getSqlForInsert());
        builder.append(INTO_KEYWORD);
        builder.append(" " + dao.getEntitySimplyClassName());
        builder.append(" (");
        builder.append(forEachFields(fields, new OnFieldListener() {

            @Override
            public String onField(String item) {
                return item;
            }
        }));
        builder.append(") ");
        builder.append(VALUES_KEYWORD);
        final One<String> prefix = new One<>("");
        if (result.hasParamBean()) {
            prefix.value0 = result.paramBean + ".";
        }
        builder.append(" (");
        builder.append(forEachFields(fields, new OnFieldListener() {

            @Override
            public String onField(String item) {
                return "${" + prefix.value0 + item + "}";
            }
        }));
        builder.append(")");
        result.value = builder.toString();
    }
    result.operationType = JQLType.INSERT;
    result.dynamicReplace = new HashMap<>();
    return result;
}
Also used : Projected_columnsContext(com.abubusoft.kripton.processor.sqlite.grammars.jsql.JqlParser.Projected_columnsContext) Where_stmtContext(com.abubusoft.kripton.processor.sqlite.grammars.jsql.JqlParser.Where_stmtContext) Conflict_algorithmContext(com.abubusoft.kripton.processor.sqlite.grammars.jsql.JqlParser.Conflict_algorithmContext) One(com.abubusoft.kripton.common.One) JqlBaseListener(com.abubusoft.kripton.processor.sqlite.grammars.jsql.JqlBaseListener) Bind_parameterContext(com.abubusoft.kripton.processor.sqlite.grammars.jsql.JqlParser.Bind_parameterContext) BindSqlInsert(com.abubusoft.kripton.android.annotation.BindSqlInsert) SQLiteDaoDefinition(com.abubusoft.kripton.processor.sqlite.model.SQLiteDaoDefinition) Column_value_setContext(com.abubusoft.kripton.processor.sqlite.grammars.jsql.JqlParser.Column_value_setContext) InsertType(com.abubusoft.kripton.processor.sqlite.SqlInsertBuilder.InsertType)

Example 7 with One

use of com.abubusoft.kripton.common.One in project kripton by xcesco.

the class SQLiteUpdateTestDatabase method findTask.

List<SQLiteUpdateTask> findTask(int previousVersion, int currentVersion) {
    List<SQLiteUpdateTask> result = new ArrayList<>();
    final One<Integer> ref = new One<>(null);
    for (int i = previousVersion; i < currentVersion; i++) {
        ref.value0 = i;
        SQLiteUpdateTask t = null;
        for (Pair<Integer, ? extends SQLiteUpdateTask> item : updateTasks) {
            if (item.value0 - 1 == ref.value0) {
                t = item.value1;
                break;
            }
        }
        if (t != null) {
            result.add(t);
        }
    }
    return result;
}
Also used : One(com.abubusoft.kripton.common.One) ArrayList(java.util.ArrayList) SQLiteUpdateTask(com.abubusoft.kripton.android.sqlite.SQLiteUpdateTask)

Example 8 with One

use of com.abubusoft.kripton.common.One in project kripton by xcesco.

the class TestSpeedRuntime method testInsertBatch.

@Test
public void testInsertBatch() {
    final int ITEM_COUNTER = 10000;
    final One<Long> start = new One<>();
    final One<Long> end = new One<>();
    final One<Integer> index = new One<>();
    final int COUNTER = 100;
    final BindPersonDataSource ds = BindPersonDataSource.instance();
    ds.openWritableDatabase();
    final List<Person> list = new ArrayList<Person>();
    for (int i = 0; i < ITEM_COUNTER; i++) {
        Person bean = new Person();
        bean.name = "name" + index.value0;
        bean.surname = "surname" + index.value0;
        list.add(bean);
    }
    start.value0 = System.currentTimeMillis();
    for (int i = 0; i < COUNTER; i++) {
        index.value0 = i;
        ds.execute(new Transaction() {

            @Override
            public TransactionResult onExecute(BindPersonDaoFactory daoFactory) {
                PersonDaoImpl dao = daoFactory.getPersonDao();
                for (int i = 0; i < list.size(); i++) {
                    dao.insert(list.get(i));
                }
                return TransactionResult.COMMIT;
            }
        });
    }
    end.value0 = System.currentTimeMillis();
    ds.close();
    // 3100
    System.out.println("Average time to insert " + ITEM_COUNTER + " items: " + ((end.value0 - start.value0) * 1.0 / COUNTER) + " ms");
}
Also used : TransactionResult(com.abubusoft.kripton.android.sqlite.TransactionResult) One(com.abubusoft.kripton.common.One) BindPersonDaoFactory(sqlite.feature.speed.persistence.BindPersonDaoFactory) ArrayList(java.util.ArrayList) PersonDaoImpl(sqlite.feature.speed.persistence.PersonDaoImpl) Transaction(sqlite.feature.speed.persistence.BindPersonDataSource.Transaction) BindPersonDataSource(sqlite.feature.speed.persistence.BindPersonDataSource) Person(sqlite.feature.speed.model.Person) Test(org.junit.Test) BaseAndroidTest(base.BaseAndroidTest)

Example 9 with One

use of com.abubusoft.kripton.common.One in project kripton by xcesco.

the class TestSpeedRuntime method testTransaction.

@Test
public void testTransaction() {
    final One<Long> start = new One<>();
    final One<Long> end = new One<>();
    final One<Integer> index = new One<>();
    final int COUNTER = 2000;
    final BindPersonDataSource ds = BindPersonDataSource.instance();
    ds.openWritableDatabase();
    start.value0 = System.currentTimeMillis();
    for (int i = 0; i < COUNTER; i++) {
        index.value0 = i;
        ds.execute(new Transaction() {

            @Override
            public TransactionResult onExecute(BindPersonDaoFactory daoFactory) {
                PersonDaoImpl dao = daoFactory.getPersonDao();
                Person bean = new Person();
                bean.name = "name" + index.value0;
                bean.surname = "surname" + index.value0;
                // Object bean = new
                dao.insert(bean);
                return TransactionResult.COMMIT;
            }
        });
    }
    end.value0 = System.currentTimeMillis();
    ds.close();
    // 3100
    System.out.println("Esecuzione terminata in " + (end.value0 - start.value0) + " ms");
}
Also used : TransactionResult(com.abubusoft.kripton.android.sqlite.TransactionResult) One(com.abubusoft.kripton.common.One) BindPersonDaoFactory(sqlite.feature.speed.persistence.BindPersonDaoFactory) PersonDaoImpl(sqlite.feature.speed.persistence.PersonDaoImpl) Transaction(sqlite.feature.speed.persistence.BindPersonDataSource.Transaction) BindPersonDataSource(sqlite.feature.speed.persistence.BindPersonDataSource) Person(sqlite.feature.speed.model.Person) Test(org.junit.Test) BaseAndroidTest(base.BaseAndroidTest)

Example 10 with One

use of com.abubusoft.kripton.common.One in project kripton by xcesco.

the class AnnotationUtility method extractAsBoolean.

public static boolean extractAsBoolean(Element item, Class<? extends Annotation> annotationClass, AnnotationAttributeType attribute) {
    final Elements elementUtils = BaseProcessor.elementUtils;
    final One<Boolean> result = new One<Boolean>(false);
    extractString(elementUtils, item, annotationClass, attribute, new OnAttributeFoundListener() {

        @Override
        public void onFound(String value) {
            result.value0 = Boolean.parseBoolean(value);
        }
    });
    return result.value0;
}
Also used : One(com.abubusoft.kripton.common.One) Elements(javax.lang.model.util.Elements)

Aggregations

One (com.abubusoft.kripton.common.One)36 ArrayList (java.util.ArrayList)13 JQLReplacerListenerImpl (com.abubusoft.kripton.processor.sqlite.grammars.jql.JQLReplacerListenerImpl)12 SQLProperty (com.abubusoft.kripton.processor.sqlite.model.SQLProperty)10 Elements (javax.lang.model.util.Elements)9 Where_stmtContext (com.abubusoft.kripton.processor.sqlite.grammars.jsql.JqlParser.Where_stmtContext)8 SQLiteDaoDefinition (com.abubusoft.kripton.processor.sqlite.model.SQLiteDaoDefinition)8 JQLChecker (com.abubusoft.kripton.processor.sqlite.grammars.jql.JQLChecker)7 JQLReplaceVariableStatementListenerImpl (com.abubusoft.kripton.processor.sqlite.grammars.jql.JQLReplaceVariableStatementListenerImpl)7 JqlBaseListener (com.abubusoft.kripton.processor.sqlite.grammars.jsql.JqlBaseListener)7 Pair (com.abubusoft.kripton.common.Pair)5 SQLiteEntity (com.abubusoft.kripton.processor.sqlite.model.SQLiteEntity)5 Bind_parameterContext (com.abubusoft.kripton.processor.sqlite.grammars.jsql.JqlParser.Bind_parameterContext)4 Projected_columnsContext (com.abubusoft.kripton.processor.sqlite.grammars.jsql.JqlParser.Projected_columnsContext)4 TypeName (com.squareup.javapoet.TypeName)4 LinkedHashSet (java.util.LinkedHashSet)4 List (java.util.List)4 Test (org.junit.Test)4 BaseAndroidTest (base.BaseAndroidTest)3 TransactionResult (com.abubusoft.kripton.android.sqlite.TransactionResult)3