Search in sources :

Example 11 with JqlBaseListener

use of com.abubusoft.kripton.processor.sqlite.grammars.jsql.JqlBaseListener 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 12 with JqlBaseListener

use of com.abubusoft.kripton.processor.sqlite.grammars.jsql.JqlBaseListener 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)

Aggregations

JqlBaseListener (com.abubusoft.kripton.processor.sqlite.grammars.jsql.JqlBaseListener)12 One (com.abubusoft.kripton.common.One)7 Bind_parameterContext (com.abubusoft.kripton.processor.sqlite.grammars.jsql.JqlParser.Bind_parameterContext)7 Projected_columnsContext (com.abubusoft.kripton.processor.sqlite.grammars.jsql.JqlParser.Projected_columnsContext)4 SQLiteDaoDefinition (com.abubusoft.kripton.processor.sqlite.model.SQLiteDaoDefinition)4 Bind_dynamic_sqlContext (com.abubusoft.kripton.processor.sqlite.grammars.jsql.JqlParser.Bind_dynamic_sqlContext)3 Where_stmtContext (com.abubusoft.kripton.processor.sqlite.grammars.jsql.JqlParser.Where_stmtContext)3 ArrayList (java.util.ArrayList)3 Column_nameContext (com.abubusoft.kripton.processor.sqlite.grammars.jsql.JqlParser.Column_nameContext)2 Column_name_setContext (com.abubusoft.kripton.processor.sqlite.grammars.jsql.JqlParser.Column_name_setContext)2 Column_value_setContext (com.abubusoft.kripton.processor.sqlite.grammars.jsql.JqlParser.Column_value_setContext)2 Columns_to_updateContext (com.abubusoft.kripton.processor.sqlite.grammars.jsql.JqlParser.Columns_to_updateContext)2 Conflict_algorithmContext (com.abubusoft.kripton.processor.sqlite.grammars.jsql.JqlParser.Conflict_algorithmContext)2 Sql_stmtContext (com.abubusoft.kripton.processor.sqlite.grammars.jsql.JqlParser.Sql_stmtContext)2 LinkedHashSet (java.util.LinkedHashSet)2 BaseProcessorTest (base.BaseProcessorTest)1 BindSqlDelete (com.abubusoft.kripton.android.annotation.BindSqlDelete)1 BindSqlInsert (com.abubusoft.kripton.android.annotation.BindSqlInsert)1 BindSqlSelect (com.abubusoft.kripton.android.annotation.BindSqlSelect)1 BindSqlUpdate (com.abubusoft.kripton.android.annotation.BindSqlUpdate)1