Search in sources :

Example 1 with JQLProjection

use of com.abubusoft.kripton.processor.sqlite.grammars.jql.JQLProjection in project kripton by xcesco.

the class SelectBeanHelper method generateSpecializedPart.

@Override
public void generateSpecializedPart(SQLiteModelMethod method, TypeSpec.Builder classBuilder, MethodSpec.Builder methodBuilder, Set<JQLProjection> fieldList, boolean mapFields) {
    SQLiteDaoDefinition daoDefinition = method.getParent();
    SQLiteEntity entity = daoDefinition.getEntity();
    // List<SQLProperty> fields = fieldList.value1;
    // TypeName collectionClass;
    TypeName entityClass = typeName(entity.getElement());
    methodBuilder.addCode("\n");
    methodBuilder.addCode("$T resultBean=null;\n", entityClass);
    methodBuilder.addCode("\n");
    methodBuilder.beginControlFlow("if (_cursor.moveToFirst())");
    // generate index from columns
    methodBuilder.addCode("\n");
    {
        int i = 0;
        for (JQLProjection a : fieldList) {
            SQLProperty item = a.property;
            methodBuilder.addStatement("int index$L=_cursor.getColumnIndex($S)", (i++), item.columnName);
            if (item.hasTypeAdapter()) {
                methodBuilder.addStatement("$T $LAdapter=$T.getAdapter($T.class)", item.typeAdapter.getAdapterTypeName(), item.getName(), SQLTypeAdapterUtils.class, item.typeAdapter.getAdapterTypeName());
            }
        }
    }
    methodBuilder.addCode("\n");
    methodBuilder.addCode("resultBean=new $T();\n\n", entityClass);
    // generate mapping
    int i = 0;
    for (JQLProjection a : fieldList) {
        SQLProperty item = a.property;
        if (item.isNullable()) {
            methodBuilder.addCode("if (!_cursor.isNull(index$L)) { ", i);
        }
        SQLTransformer.cursor2Java(methodBuilder, entityClass, item, "resultBean", "_cursor", "index" + i + "");
        methodBuilder.addCode(";");
        if (item.isNullable()) {
            methodBuilder.addCode(" }");
        }
        methodBuilder.addCode("\n");
        i++;
    }
    methodBuilder.addCode("\n");
    methodBuilder.endControlFlow();
    methodBuilder.addCode("return resultBean;\n");
    // close try { open cursor
    methodBuilder.endControlFlow();
}
Also used : TypeName(com.squareup.javapoet.TypeName) JQLProjection(com.abubusoft.kripton.processor.sqlite.grammars.jql.JQLProjection) SQLTypeAdapterUtils(com.abubusoft.kripton.common.SQLTypeAdapterUtils) SQLProperty(com.abubusoft.kripton.processor.sqlite.model.SQLProperty) SQLiteEntity(com.abubusoft.kripton.processor.sqlite.model.SQLiteEntity) SQLiteDaoDefinition(com.abubusoft.kripton.processor.sqlite.model.SQLiteDaoDefinition)

Example 2 with JQLProjection

use of com.abubusoft.kripton.processor.sqlite.grammars.jql.JQLProjection in project kripton by xcesco.

the class SelectPaginatedResultHelper method generateSpecializedPart.

@Override
public void generateSpecializedPart(SQLiteModelMethod method, TypeSpec.Builder classBuilder, MethodSpec.Builder methodBuilder, Set<JQLProjection> fieldList, boolean mapFields) {
    SQLiteDaoDefinition daoDefinition = method.getParent();
    SQLiteEntity entity = daoDefinition.getEntity();
    // List<SQLProperty> fields = fieldList.value1;
    TypeName entityClass = typeName(entity.getElement());
    methodBuilder.addCode("\n");
    methodBuilder.addStatement("$T<$T> resultList=new $T<$T>(_cursor.getCount())", List.class, entityClass, ArrayList.class, entityClass);
    methodBuilder.addStatement("$T resultBean=null", entityClass);
    methodBuilder.addCode("\n");
    methodBuilder.beginControlFlow("if (_cursor.moveToFirst())");
    // generate index from columns
    methodBuilder.addCode("\n");
    {
        int i = 0;
        for (JQLProjection a : fieldList) {
            SQLProperty item = a.property;
            methodBuilder.addStatement("int index$L=_cursor.getColumnIndex($S)", (i++), item.columnName);
            if (item.hasTypeAdapter()) {
                methodBuilder.addStatement("$T $LAdapter=$T.getAdapter($T.class)", item.typeAdapter.getAdapterTypeName(), item.getName(), SQLTypeAdapterUtils.class, item.typeAdapter.getAdapterTypeName());
            }
        }
    }
    methodBuilder.addCode("\n");
    methodBuilder.beginControlFlow("do\n");
    methodBuilder.addCode("resultBean=new $T();\n\n", entityClass);
    // generate mapping
    int i = 0;
    for (JQLProjection a : fieldList) {
        SQLProperty item = a.property;
        if (item.isNullable()) {
            methodBuilder.addCode("if (!_cursor.isNull(index$L)) { ", i);
        }
        SQLTransformer.cursor2Java(methodBuilder, typeName(entity.getElement()), item, "resultBean", "_cursor", "index" + i + "");
        methodBuilder.addCode(";");
        if (item.isNullable()) {
            methodBuilder.addCode(" }");
        }
        methodBuilder.addCode("\n");
        i++;
    }
    methodBuilder.addCode("\n");
    methodBuilder.addCode("resultList.add(resultBean);\n");
    methodBuilder.endControlFlow("while (_cursor.moveToNext())");
    methodBuilder.endControlFlow();
    methodBuilder.addCode("\n");
    methodBuilder.addCode("return resultList;\n");
    methodBuilder.endControlFlow();
}
Also used : TypeName(com.squareup.javapoet.TypeName) JQLProjection(com.abubusoft.kripton.processor.sqlite.grammars.jql.JQLProjection) SQLTypeAdapterUtils(com.abubusoft.kripton.common.SQLTypeAdapterUtils) SQLProperty(com.abubusoft.kripton.processor.sqlite.model.SQLProperty) SQLiteEntity(com.abubusoft.kripton.processor.sqlite.model.SQLiteEntity) SQLiteDaoDefinition(com.abubusoft.kripton.processor.sqlite.model.SQLiteDaoDefinition)

Example 3 with JQLProjection

use of com.abubusoft.kripton.processor.sqlite.grammars.jql.JQLProjection in project kripton by xcesco.

the class SelectPaginatedResultHelper method generate.

@Override
public void generate(TypeSpec.Builder classBuilder, boolean mapFields, SQLiteModelMethod method) {
    SQLiteDaoDefinition daoDefinition = method.getParent();
    String pagedResultName = buildSpecializedPagedResultClass(classBuilder, method);
    Set<JQLProjection> fieldList = JQLChecker.getInstance().extractProjections(method, method.jql.value, daoDefinition.getEntity());
    {
        MethodSpec.Builder methodBuilder = generateMethodBuilder(method);
        // create PaginatedResult
        String separator = "";
        methodBuilder.addCode("$L paginatedResult=new $L(", pagedResultName, pagedResultName);
        for (Pair<String, TypeName> item : method.getParameters()) {
            // field
            methodBuilder.addCode(separator + "$L", item.value0);
            separator = ", ";
        }
        methodBuilder.addCode(");\n");
        generateCommonPart(method, classBuilder, methodBuilder, fieldList, selectType.isMapFields(), GenerationType.NO_CONTENT, null);
        methodBuilder.addStatement("return paginatedResult");
        classBuilder.addMethod(methodBuilder.build());
    }
    // generate paged result method
    {
        MethodSpec.Builder methodBuilder = MethodSpec.methodBuilder(method.getName()).addModifiers(Modifier.PRIVATE);
        generateMethodSignature(method, methodBuilder, TypeUtility.parameterizedTypeName(TypeUtility.className(List.class), TypeUtility.typeName(daoDefinition.getEntityClassName())), ParameterSpec.builder(TypeUtility.typeName(pagedResultName), "paginatedResult").build());
        generateCommonPart(method, classBuilder, methodBuilder, fieldList, selectType.isMapFields(), GenerationType.NO_METHOD_SIGN, null, JavadocPart.build(JavadocPartType.ADD_PARAMETER, "paginatedResult", "handler of paginated result"), JavadocPart.build(JavadocPartType.RETURN, "", "result list"));
        generateSpecializedPart(method, classBuilder, methodBuilder, fieldList, selectType.isMapFields());
        classBuilder.addMethod(methodBuilder.build());
    }
}
Also used : JQLProjection(com.abubusoft.kripton.processor.sqlite.grammars.jql.JQLProjection) ArrayList(java.util.ArrayList) List(java.util.List) SQLiteDaoDefinition(com.abubusoft.kripton.processor.sqlite.model.SQLiteDaoDefinition) Pair(com.abubusoft.kripton.common.Pair)

Example 4 with JQLProjection

use of com.abubusoft.kripton.processor.sqlite.grammars.jql.JQLProjection in project kripton by xcesco.

the class SelectBeanListHelper method generateSpecializedPart.

/*
	 * (non-Javadoc)
	 * 
	 * @see com.abubusoft.kripton.processor.sqlite.SQLiteSelectBuilder.
	 * SelectCodeGenerator#generate(com.squareup.javapoet.MethodSpec.Builder)
	 */
@Override
public void generateSpecializedPart(SQLiteModelMethod method, TypeSpec.Builder classBuilder, MethodSpec.Builder methodBuilder, Set<JQLProjection> fieldList, boolean mapFields) {
    SQLiteDaoDefinition daoDefinition = method.getParent();
    SQLiteEntity entity = daoDefinition.getEntity();
    TypeName returnTypeName = method.getReturnClass();
    ParameterizedTypeName returnListName = (ParameterizedTypeName) returnTypeName;
    TypeName collectionClass;
    TypeName entityClass = typeName(entity.getElement());
    ClassName listClazzName = returnListName.rawType;
    collectionClass = defineCollection(listClazzName);
    methodBuilder.addCode("\n");
    if (TypeUtility.isTypeEquals(collectionClass, TypeUtility.typeName(ArrayList.class))) {
        methodBuilder.addCode("$T<$T> resultList=new $T<$T>(_cursor.getCount());\n", collectionClass, entityClass, collectionClass, entityClass);
    } else {
        methodBuilder.addCode("$T<$T> resultList=new $T<$T>();\n", collectionClass, entityClass, collectionClass, entityClass);
    }
    methodBuilder.addCode("$T resultBean=null;\n", entityClass);
    methodBuilder.addCode("\n");
    methodBuilder.beginControlFlow("if (_cursor.moveToFirst())");
    // generate index from columns
    methodBuilder.addCode("\n");
    {
        int i = 0;
        for (JQLProjection a : fieldList) {
            SQLProperty item = a.property;
            methodBuilder.addStatement("int index$L=_cursor.getColumnIndex($S)", (i++), item.columnName);
            if (item.hasTypeAdapter()) {
                methodBuilder.addStatement("$T $LAdapter=$T.getAdapter($T.class)", item.typeAdapter.getAdapterTypeName(), item.getName(), SQLTypeAdapterUtils.class, item.typeAdapter.getAdapterTypeName());
            }
        }
    }
    methodBuilder.addCode("\n");
    methodBuilder.beginControlFlow("do\n");
    methodBuilder.addCode("resultBean=new $T();\n\n", entityClass);
    // generate mapping
    int i = 0;
    for (JQLProjection a : fieldList) {
        SQLProperty item = a.property;
        if (item.isNullable()) {
            methodBuilder.addCode("if (!_cursor.isNull(index$L)) { ", i);
        }
        SQLTransformer.cursor2Java(methodBuilder, typeName(entity.getElement()), item, "resultBean", "_cursor", "index" + i + "");
        methodBuilder.addCode(";");
        if (item.isNullable()) {
            methodBuilder.addCode(" }");
        }
        methodBuilder.addCode("\n");
        i++;
    }
    methodBuilder.addCode("\n");
    methodBuilder.addCode("resultList.add(resultBean);\n");
    methodBuilder.endControlFlow("while (_cursor.moveToNext())");
    methodBuilder.endControlFlow();
    methodBuilder.addCode("\n");
    methodBuilder.addCode("return resultList;\n");
    // close try { open cursor
    methodBuilder.endControlFlow();
}
Also used : ParameterizedTypeName(com.squareup.javapoet.ParameterizedTypeName) TypeName(com.squareup.javapoet.TypeName) JQLProjection(com.abubusoft.kripton.processor.sqlite.grammars.jql.JQLProjection) SQLTypeAdapterUtils(com.abubusoft.kripton.common.SQLTypeAdapterUtils) ClassName(com.squareup.javapoet.ClassName) ArrayList(java.util.ArrayList) SQLProperty(com.abubusoft.kripton.processor.sqlite.model.SQLProperty) SQLiteEntity(com.abubusoft.kripton.processor.sqlite.model.SQLiteEntity) SQLiteDaoDefinition(com.abubusoft.kripton.processor.sqlite.model.SQLiteDaoDefinition) ParameterizedTypeName(com.squareup.javapoet.ParameterizedTypeName)

Example 5 with JQLProjection

use of com.abubusoft.kripton.processor.sqlite.grammars.jql.JQLProjection in project kripton by xcesco.

the class TestJqlChecker method testSelect01.

/**
 * extract projections from a select
 */
@Test
public void testSelect01() {
    // String sql="SELECT count(*) FROM channel WHERE
    // updateTime=${bean.updateTime}";
    String sql = "SELECT count(*) as alias1, field2, field3 as alias3, table1.field3 as alias3, table2.field4 as alias4 FROM channel WHERE updateTime=${ bean.field1 } and field=${ field2  } and #{" + JQLDynamicStatementType.DYNAMIC_WHERE + "}";
    String logSql = "SELECT count(*) as alias1, field2, field3 as alias3, table1.field3 as alias3, table2.field4 as alias4 FROM channel WHERE updateTime=? and field=? and \"+DYNAMIC_WHERE+\"";
    // String usedSql = "SELECT count(*) FROM channel WHERE
    // updateTime=${bean.updateTime}";
    JQL jql = new JQL();
    jql.value = sql;
    JQLChecker checker = JQLChecker.getInstance();
    // verify sql
    checker.verify(dummyContext, jql);
    Finder<SQLProperty> entityMock = new Finder<SQLProperty>() {

        @Override
        public String getSimpleName() {
            // TODO Auto-generated method stub
            return null;
        }

        @Override
        public List<SQLProperty> getCollection() {
            // TODO Auto-generated method stub
            return null;
        }

        @Override
        public SQLProperty findPropertyByName(String name) {
            // TODO Auto-generated method stub
            return null;
        }

        @Override
        public String getTableName() {
            // TODO Auto-generated method stub
            return null;
        }
    };
    // check projections
    Set<JQLProjection> projections = checker.extractProjections(dummyContext, jql.value, entityMock);
    {
        LinkedHashSet<JQLProjection> aspected = new LinkedHashSet<>();
        aspected.add(JQLProjection.ProjectionBuilder.create().type(ProjectionType.COMPLEX).expression("count(*)").alias("alias1").build());
        aspected.add(JQLProjection.ProjectionBuilder.create().type(ProjectionType.COLUMN).column("field2").build());
        aspected.add(JQLProjection.ProjectionBuilder.create().type(ProjectionType.COLUMN).column("field3").alias("alias3").build());
        aspected.add(JQLProjection.ProjectionBuilder.create().type(ProjectionType.COLUMN).table("table1").column("field3").alias("alias3").build());
        aspected.add(JQLProjection.ProjectionBuilder.create().type(ProjectionType.COLUMN).table("table2").column("field4").alias("alias4").build());
        checkCollectionExactly(projections, aspected);
    }
    // check bind parameters
    {
        List<JQLPlaceHolder> aspected = new ArrayList<>();
        aspected.add(new JQLPlaceHolder(JQLPlaceHolderType.PARAMETER, "bean.field1"));
        aspected.add(new JQLPlaceHolder(JQLPlaceHolderType.PARAMETER, "field2"));
        aspected.add(new JQLPlaceHolder(JQLPlaceHolderType.DYNAMIC_SQL, JQLDynamicStatementType.DYNAMIC_WHERE.toString()));
        List<JQLPlaceHolder> actual = checker.extractPlaceHoldersAsList(dummyContext, jql.value);
        checkCollectionExactly(actual, aspected);
    }
    // prepare for log
    String sqlLogResult = checker.replace(dummyContext, jql, new JQLReplacerListenerImpl(null) {

        @Override
        public String onDynamicSQL(JQLDynamicStatementType dynamicStatement) {
            return String.format("\"+%s+\"", dynamicStatement);
        }

        @Override
        public String onBindParameter(String bindParameterName) {
            return "?";
        }

        @Override
        public String onColumnFullyQualifiedName(String tableName, String columnName) {
            return null;
        }
    });
    assertEquals("sql for log generation failed", logSql, sqlLogResult);
}
Also used : LinkedHashSet(java.util.LinkedHashSet) JQLChecker(com.abubusoft.kripton.processor.sqlite.grammars.jql.JQLChecker) JQL(com.abubusoft.kripton.processor.sqlite.grammars.jql.JQL) Finder(com.abubusoft.kripton.processor.core.Finder) JQLPlaceHolder(com.abubusoft.kripton.processor.sqlite.grammars.jql.JQLPlaceHolder) JQLReplacerListenerImpl(com.abubusoft.kripton.processor.sqlite.grammars.jql.JQLReplacerListenerImpl) JQLDynamicStatementType(com.abubusoft.kripton.processor.sqlite.grammars.jql.JQL.JQLDynamicStatementType) JQLProjection(com.abubusoft.kripton.processor.sqlite.grammars.jql.JQLProjection) SQLProperty(com.abubusoft.kripton.processor.sqlite.model.SQLProperty) ArrayList(java.util.ArrayList) List(java.util.List) BaseProcessorTest(base.BaseProcessorTest) Test(org.junit.Test)

Aggregations

JQLProjection (com.abubusoft.kripton.processor.sqlite.grammars.jql.JQLProjection)10 SQLiteDaoDefinition (com.abubusoft.kripton.processor.sqlite.model.SQLiteDaoDefinition)9 SQLProperty (com.abubusoft.kripton.processor.sqlite.model.SQLProperty)7 TypeName (com.squareup.javapoet.TypeName)7 SQLiteEntity (com.abubusoft.kripton.processor.sqlite.model.SQLiteEntity)6 ParameterizedTypeName (com.squareup.javapoet.ParameterizedTypeName)5 SQLTypeAdapterUtils (com.abubusoft.kripton.common.SQLTypeAdapterUtils)4 MethodSpec (com.squareup.javapoet.MethodSpec)3 ArrayList (java.util.ArrayList)3 OnReadBeanListener (com.abubusoft.kripton.android.sqlite.OnReadBeanListener)2 Pair (com.abubusoft.kripton.common.Pair)2 JQLChecker (com.abubusoft.kripton.processor.sqlite.grammars.jql.JQLChecker)2 JQLPlaceHolder (com.abubusoft.kripton.processor.sqlite.grammars.jql.JQLPlaceHolder)2 JQLReplacerListenerImpl (com.abubusoft.kripton.processor.sqlite.grammars.jql.JQLReplacerListenerImpl)2 ClassName (com.squareup.javapoet.ClassName)2 LinkedHashSet (java.util.LinkedHashSet)2 List (java.util.List)2 BaseProcessorTest (base.BaseProcessorTest)1 OnReadCursorListener (com.abubusoft.kripton.android.sqlite.OnReadCursorListener)1 StringUtils (com.abubusoft.kripton.common.StringUtils)1