Search in sources :

Example 26 with One

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

the class BindTableGenerator method buldIndexes.

public static Pair<String, String> buldIndexes(final SQLiteEntity entity, boolean unique, int counter) {
    Pair<String, String> result = new Pair<>();
    result.value0 = "";
    result.value1 = "";
    ModelAnnotation annotationTable = entity.getAnnotation(BindTable.class);
    if (annotationTable == null)
        return result;
    List<String> indexes = null;
    String uniqueString;
    if (unique) {
        uniqueString = "UNIQUE ";
        indexes = annotationTable.getAttributeAsArray(AnnotationAttributeType.UNIQUE_INDEXES);
    } else {
        uniqueString = "";
        indexes = annotationTable.getAttributeAsArray(AnnotationAttributeType.INDEXES);
    }
    if (indexes == null || indexes.size() == 0)
        return result;
    // CREATE INDEX index_name ON tab_name (column1, column2)
    // Matcher matcher = patternIndex.matcher(rawIndexes);
    List<String> listCreateIndex = new ArrayList<>();
    List<String> listDropIndex = new ArrayList<>();
    for (String index : indexes) {
        String createIndex = String.format(" CREATE %sINDEX idx_%s_%s on %s (%s)", uniqueString, entity.getTableName(), counter++, entity.getTableName(), index);
        String dropIndex = String.format(" DROP INDEX IF EXISTS idx_%s_%s", entity.getTableName(), counter);
        final One<Integer> fieldCounter = new One<Integer>(0);
        createIndex = JQLChecker.getInstance().replace(new JQLContext() {

            @Override
            public String getContextDescription() {
                return "While table definition generation for entity " + entity.getName();
            }
        }, createIndex, new JQLReplacerListenerImpl(null) {

            @Override
            public String onColumnName(String columnName) {
                fieldCounter.value0++;
                SQLProperty property = entity.findPropertyByName(columnName);
                AssertKripton.assertTrue(property != null, "class '%s' in @%s(indexes) use unknown property '%s'", entity.getName(), BindTable.class.getSimpleName(), columnName);
                return property.columnName;
            }

            @Override
            public String onColumnFullyQualifiedName(String tableName, String columnName) {
                AssertKripton.fail("Inconsistent state");
                return null;
            }
        });
        AssertKripton.assertTrue(fieldCounter.value0 > 0, "class '%s' have @%s(indexes) with no well formed indexes", entity.getName(), BindTable.class.getSimpleName());
        listCreateIndex.add(createIndex);
        listDropIndex.add(dropIndex);
    }
    result.value0 = StringUtils.join(listCreateIndex, ";");
    result.value1 = StringUtils.join(listDropIndex, ";");
    return result;
}
Also used : JQLContext(com.abubusoft.kripton.processor.sqlite.grammars.jql.JQLContext) One(com.abubusoft.kripton.common.One) ArrayList(java.util.ArrayList) BindTable(com.abubusoft.kripton.android.annotation.BindTable) JQLReplacerListenerImpl(com.abubusoft.kripton.processor.sqlite.grammars.jql.JQLReplacerListenerImpl) ModelAnnotation(com.abubusoft.kripton.processor.core.ModelAnnotation) SQLProperty(com.abubusoft.kripton.processor.sqlite.model.SQLProperty) Pair(com.abubusoft.kripton.common.Pair)

Example 27 with One

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

the class JQLChecker method extractColumnsToInsertOrUpdate.

public Set<String> extractColumnsToInsertOrUpdate(final JQLContext jqlContext, String jqlValue, final Finder<SQLProperty> entity) {
    final Set<String> result = new LinkedHashSet<String>();
    final One<Boolean> selectionOn = new One<Boolean>(null);
    final One<Boolean> insertOn = new One<Boolean>(null);
    // Column_name_set is needed for insert
    // Columns_to_update is needed for update
    analyzeInternal(jqlContext, jqlValue, new JqlBaseListener() {

        @Override
        public void enterColumn_name_set(Column_name_setContext ctx) {
            if (insertOn.value0 == null) {
                insertOn.value0 = true;
            }
        }

        @Override
        public void exitColumn_name_set(Column_name_setContext ctx) {
            insertOn.value0 = false;
        }

        @Override
        public void enterColumns_to_update(Columns_to_updateContext ctx) {
            if (selectionOn.value0 == null) {
                selectionOn.value0 = true;
            }
        }

        @Override
        public void exitColumns_to_update(Columns_to_updateContext ctx) {
            selectionOn.value0 = false;
        }

        @Override
        public void enterColumn_name(Column_nameContext ctx) {
            // works for INSERTS
            if (insertOn.value0 != null && insertOn.value0 == true) {
                result.add(ctx.getText());
            }
        }

        @Override
        public void enterColumn_name_to_update(Column_name_to_updateContext ctx) {
            result.add(ctx.getText());
        }
    });
    return result;
}
Also used : LinkedHashSet(java.util.LinkedHashSet) Column_name_setContext(com.abubusoft.kripton.processor.sqlite.grammars.jsql.JqlParser.Column_name_setContext) Column_name_to_updateContext(com.abubusoft.kripton.processor.sqlite.grammars.jsql.JqlParser.Column_name_to_updateContext) One(com.abubusoft.kripton.common.One) Columns_to_updateContext(com.abubusoft.kripton.processor.sqlite.grammars.jsql.JqlParser.Columns_to_updateContext) JqlBaseListener(com.abubusoft.kripton.processor.sqlite.grammars.jsql.JqlBaseListener) Column_nameContext(com.abubusoft.kripton.processor.sqlite.grammars.jsql.JqlParser.Column_nameContext)

Example 28 with One

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

the class JQLChecker method extractProjections.

/**
 * Retrieve set of projected field.
 *
 * @param jql
 * @return
 */
public Set<JQLProjection> extractProjections(final JQLContext jqlContext, String jqlValue, final Finder<SQLProperty> entity) {
    final Set<JQLProjection> result = new LinkedHashSet<JQLProjection>();
    final One<Boolean> projection = new One<Boolean>(null);
    analyzeInternal(jqlContext, jqlValue, new JqlBaseListener() {

        @Override
        public void enterProjected_columns(Projected_columnsContext ctx) {
            if (projection.value0 == null) {
                projection.value0 = true;
            }
        }

        @Override
        public void exitProjected_columns(Projected_columnsContext ctx) {
            projection.value0 = false;
        }

        @Override
        public void enterResult_column(Result_columnContext ctx) {
            if (projection.value0 != true)
                return;
            ProjectionBuilder builder = ProjectionBuilder.create();
            if (ctx.getText().endsWith("*")) {
                builder.type(ProjectionType.STAR);
            } else if (ctx.table_name() != null) {
                builder.table(ctx.expr().table_name().getText());
            } else if (ctx.expr().column_fully_qualified_name() != null && ctx.expr().column_fully_qualified_name().column_simple_name() != null) {
                if (ctx.expr().column_fully_qualified_name().table_simple_name() != null) {
                    builder.table(ctx.expr().column_fully_qualified_name().table_simple_name().getText());
                }
                String jqlColumnName = ctx.expr().column_fully_qualified_name().column_simple_name().getText();
                builder.column(jqlColumnName);
                builder.property(entity.findPropertyByName(jqlColumnName));
                builder.type(ProjectionType.COLUMN);
            } else {
                builder.type(ProjectionType.COMPLEX);
                builder.expression(ctx.expr().getText());
            }
            if (ctx.column_alias() != null) {
                builder.alias(ctx.column_alias().getText());
            }
            result.add(builder.build());
        }

        @Override
        public void exitResult_column(Result_columnContext ctx) {
        }
    });
    if (result.size() == 1 && result.toArray(new JQLProjection[1])[0].type == ProjectionType.STAR) {
        // the projected columns are full
        result.clear();
        if (entity != null) {
            for (SQLProperty item : entity.getCollection()) {
                JQLProjection col = new JQLProjection(ProjectionType.COLUMN, entity.getSimpleName(), item.getName(), null, null, item);
                result.add(col);
            }
        }
    }
    return result;
}
Also used : LinkedHashSet(java.util.LinkedHashSet) Projected_columnsContext(com.abubusoft.kripton.processor.sqlite.grammars.jsql.JqlParser.Projected_columnsContext) One(com.abubusoft.kripton.common.One) Result_columnContext(com.abubusoft.kripton.processor.sqlite.grammars.jsql.JqlParser.Result_columnContext) JqlBaseListener(com.abubusoft.kripton.processor.sqlite.grammars.jsql.JqlBaseListener) SQLProperty(com.abubusoft.kripton.processor.sqlite.model.SQLProperty) ProjectionBuilder(com.abubusoft.kripton.processor.sqlite.grammars.jql.JQLProjection.ProjectionBuilder)

Example 29 with One

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

the class SqlBuilderHelper method generateLog.

/**
 * <p>
 * Generate log for INSERT operations
 * </p>
 *
 * @param method
 * @param methodBuilder
 */
public static void generateLog(final SQLiteModelMethod method, MethodSpec.Builder methodBuilder) {
    SQLiteDaoDefinition daoDefinition = method.getParent();
    // log is enabled
    if (daoDefinition.isLogEnabled()) {
        // generate log section - BEGIN
        methodBuilder.addComment("log section BEGIN");
        methodBuilder.beginControlFlow("if (_context.isLogEnabled())");
        methodBuilder.addCode("// log for insert -- BEGIN \n");
        JQLChecker checker = JQLChecker.getInstance();
        final One<Boolean> inWhere = new One<Boolean>(false);
        String sql = checker.replace(method, method.jql, new JQLReplacerListenerImpl(method) {

            @Override
            public String onBindParameter(String bindParameterName) {
                if (inWhere.value0) {
                    return "?";
                }
                return null;
            }

            @Override
            public void onWhereStatementBegin(Where_stmtContext ctx) {
                super.onWhereStatementBegin(ctx);
                inWhere.value0 = true;
            }

            @Override
            public void onWhereStatementEnd(Where_stmtContext ctx) {
                super.onWhereStatementEnd(ctx);
                inWhere.value0 = false;
            }
        });
        if (method.jql.containsSelectOperation) {
            // log
            // manage log
            methodBuilder.addCode("\n");
            methodBuilder.addStatement("$T.info($S)", Logger.class, sql);
        } else {
            sql = checker.replaceVariableStatements(method, sql, new JQLReplaceVariableStatementListenerImpl() {

                @Override
                public String onColumnNameSet(String statement) {
                    return "%s";
                }

                @Override
                public String onColumnValueSet(String statement) {
                    return "%s";
                }
            });
            methodBuilder.addStatement("$T _columnNameBuffer=new $T()", StringBuffer.class, StringBuffer.class);
            methodBuilder.addStatement("$T _columnValueBuffer=new $T()", StringBuffer.class, StringBuffer.class);
            methodBuilder.addStatement("String _columnSeparator=$S", "");
            SqlBuilderHelper.forEachColumnInContentValue(methodBuilder, method, "_contentValues.keys()", false, new OnColumnListener() {

                @Override
                public void onColumnCheck(MethodSpec.Builder methodBuilder, String columNameVariable) {
                    methodBuilder.addStatement("_columnNameBuffer.append(_columnSeparator+$L)", columNameVariable);
                    methodBuilder.addStatement("_columnValueBuffer.append(_columnSeparator+$S+$L)", ":", columNameVariable);
                    methodBuilder.addStatement("_columnSeparator=$S", ", ");
                }
            });
            methodBuilder.addStatement("$T.info($S, _columnNameBuffer.toString(), _columnValueBuffer.toString())", Logger.class, sql);
        }
        generateLogForContentValues(method, methodBuilder);
        methodBuilder.addCode("// log for insert -- END \n\n");
        SqlBuilderHelper.generateLogForWhereParameters(method, methodBuilder);
        // generate log section - END
        methodBuilder.endControlFlow();
        methodBuilder.addComment("log section END");
    }
}
Also used : JQLReplaceVariableStatementListenerImpl(com.abubusoft.kripton.processor.sqlite.grammars.jql.JQLReplaceVariableStatementListenerImpl) Where_stmtContext(com.abubusoft.kripton.processor.sqlite.grammars.jsql.JqlParser.Where_stmtContext) JQLChecker(com.abubusoft.kripton.processor.sqlite.grammars.jql.JQLChecker) MethodSpec(com.squareup.javapoet.MethodSpec) One(com.abubusoft.kripton.common.One) SQLiteDaoDefinition(com.abubusoft.kripton.processor.sqlite.model.SQLiteDaoDefinition) JQLReplacerListenerImpl(com.abubusoft.kripton.processor.sqlite.grammars.jql.JQLReplacerListenerImpl)

Example 30 with One

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

the class SqlBuilderHelper method generateSQLForInsertDynamic.

/**
 * <p>
 * Generate log for INSERT operations
 * </p>
 *
 * @param method
 * @param methodBuilder
 */
public static void generateSQLForInsertDynamic(final SQLiteModelMethod method, MethodSpec.Builder methodBuilder) {
    methodBuilder.addComment("generate SQL for insert");
    JQLChecker checker = JQLChecker.getInstance();
    // replace the table name, other pieces will be removed
    String sql = checker.replace(method, method.jql, new JQLReplacerListenerImpl(method) {

        @Override
        public String onBindParameter(String bindParameterName) {
            return "?";
        }
    });
    final One<Integer> counter = new One<Integer>(0);
    sql = checker.replaceVariableStatements(method, sql, new JQLReplaceVariableStatementListenerImpl() {

        @Override
        public String onColumnNameSet(String statement) {
            counter.value0++;
            return "%s";
        }

        @Override
        public String onColumnValueSet(String statement) {
            counter.value0++;
            return "%s";
        }
    });
    if (counter.value0 == 2) {
        methodBuilder.addStatement("String _sql=String.format($S, _contentValues.keyList(), _contentValues.keyValueList())", sql);
    } else {
        methodBuilder.addStatement("String _sql=String.format($S, _contentValues.keyList())", sql);
    }
}
Also used : JQLReplaceVariableStatementListenerImpl(com.abubusoft.kripton.processor.sqlite.grammars.jql.JQLReplaceVariableStatementListenerImpl) JQLChecker(com.abubusoft.kripton.processor.sqlite.grammars.jql.JQLChecker) JQLReplacerListenerImpl(com.abubusoft.kripton.processor.sqlite.grammars.jql.JQLReplacerListenerImpl) One(com.abubusoft.kripton.common.One)

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