Search in sources :

Example 1 with InvalidMethodSignException

use of com.abubusoft.kripton.processor.exceptions.InvalidMethodSignException in project kripton by xcesco.

the class ModifyRawHelper method generateJavaDoc.

/**
 * @param daoDefinition
 * @param method
 * @param methodBuilder
 * @param updateMode
 * @param whereCondition
 * @param where
 * @param methodParams
 * @param updateableParams
 *
 * @return sql generated
 */
private void generateJavaDoc(final SQLiteModelMethod method, MethodSpec.Builder methodBuilder, boolean updateMode, String whereCondition, Pair<String, List<Pair<String, TypeName>>> where, List<Pair<String, TypeName>> methodParams) {
    final List<SQLProperty> updatedProperties = new ArrayList<>();
    final One<Boolean> onWhereStatement = new One<Boolean>(false);
    String sqlModify = JQLChecker.getInstance().replace(method, method.jql, new JQLReplacerListenerImpl(method) {

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

        @Override
        public void onWhereStatementEnd(Where_stmtContext ctx) {
            onWhereStatement.value0 = false;
        }

        @Override
        public String onColumnNameToUpdate(String columnName) {
            SQLProperty tempProperty = currentEntity.get(columnName);
            AssertKripton.assertTrueOrUnknownPropertyInJQLException(tempProperty != null, method, columnName);
            updatedProperties.add(tempProperty);
            return tempProperty.columnName;
        }

        @Override
        public String onColumnName(String columnName) {
            SQLProperty tempProperty = currentEntity.get(columnName);
            AssertKripton.assertTrueOrUnknownPropertyInJQLException(tempProperty != null, method, columnName);
            return tempProperty.columnName;
        }

        @Override
        public String onBindParameter(String bindParameterName) {
            String resolvedParamName = method.findParameterNameByAlias(bindParameterName);
            AssertKripton.assertTrueOrUnknownParamInJQLException(resolvedParamName != null, method, bindParameterName);
            if (onWhereStatement.value0) {
                return "${" + bindParameterName + "}";
            } else {
                return ":" + bindParameterName;
            }
        }
    });
    if (updateMode) {
        methodBuilder.addJavadoc("<h2>SQL update</h2>\n");
        methodBuilder.addJavadoc("<pre>$L</pre>\n", sqlModify);
        methodBuilder.addJavadoc("\n");
        // list of updated fields
        methodBuilder.addJavadoc("<h2>Updated columns:</h2>\n");
        methodBuilder.addJavadoc("<ul>\n");
        for (SQLProperty property : updatedProperties) {
            methodBuilder.addJavadoc("\t<li>$L</li>\n", property.columnName);
        // methodBuilder.addJavadoc("<dd>is binded to query's parameter
        // <strong>$L</strong> and method's parameter
        // <strong>$L</strong></dd>\n", "${" + resolvedName + "}",
        // property.value0);
        }
        methodBuilder.addJavadoc("</ul>");
        methodBuilder.addJavadoc("\n\n");
    } else {
        methodBuilder.addJavadoc("<h2>SQL delete</h2>\n");
        methodBuilder.addJavadoc("<pre>$L</pre>\n", sqlModify);
        methodBuilder.addJavadoc("\n\n");
    }
    // list of where parameter
    methodBuilder.addJavadoc("<h2>Where parameters:</h2>\n");
    methodBuilder.addJavadoc("<dl>\n");
    for (Pair<String, TypeName> property : where.value1) {
        String rawName = method.findParameterNameByAlias(property.value0);
        methodBuilder.addJavadoc("\t<dt>$L</dt>", "${" + property.value0 + "}");
        methodBuilder.addJavadoc("<dd>is mapped to method's parameter <strong>$L</strong></dd>\n", rawName);
    }
    methodBuilder.addJavadoc("</dl>");
    methodBuilder.addJavadoc("\n\n");
    if (method.hasDynamicWhereConditions()) {
        methodBuilder.addJavadoc("<dl>\n");
        methodBuilder.addJavadoc("<dt>$L</dt><dd>is part of where conditions resolved at runtime. In above SQL it is displayed as #{$L}</dd>", method.dynamicWhereParameterName, JQLDynamicStatementType.DYNAMIC_WHERE);
        methodBuilder.addJavadoc("\n</dl>");
        methodBuilder.addJavadoc("\n\n");
    }
    // dynamic conditions
    if (method.hasDynamicWhereConditions()) {
        methodBuilder.addJavadoc("<h2>Method's parameters and associated dynamic parts:</h2>\n");
        methodBuilder.addJavadoc("<dl>\n");
        if (method.hasDynamicWhereConditions()) {
            methodBuilder.addJavadoc("<dt>$L</dt><dd>is part of where conditions resolved at runtime. In above SQL it is displayed as #{$L}</dd>", method.dynamicWhereParameterName, JQLDynamicStatementType.DYNAMIC_WHERE);
        }
        methodBuilder.addJavadoc("</dl>");
        methodBuilder.addJavadoc("\n\n");
    }
    // method parameters
    if (methodParams.size() > 0) {
        for (Pair<String, TypeName> param : methodParams) {
            String resolvedName = method.findParameterAliasByName(param.value0);
            methodBuilder.addJavadoc("@param $L", param.value0);
            if (method.isThisDynamicWhereConditionsName(param.value0)) {
                methodBuilder.addJavadoc("\n\tis used as dynamic where conditions\n");
            } else if (where.value1.contains(new Pair<>(resolvedName, param.value1))) {
                methodBuilder.addJavadoc("\n\tis used as where parameter <strong>$L</strong>\n", "${" + resolvedName + "}");
            } else {
                methodBuilder.addJavadoc("\n\tis used as updated field <strong>$L</strong>\n", resolvedName);
            }
        }
    }
    // if true, field must be associate to ben attributes
    TypeName returnType = method.getReturnClass();
    // define return value
    if (returnType == TypeName.VOID) {
    } else {
        methodBuilder.addJavadoc("\n");
        if (isIn(returnType, Boolean.TYPE, Boolean.class)) {
            if (updateMode) {
                methodBuilder.addJavadoc("@return <code>true</code> if record is updated, <code>false</code> otherwise");
            } else {
                methodBuilder.addJavadoc("@return <code>true</code> if record is deleted, <code>false</code> otherwise");
            }
        // methodBuilder.addCode("return result!=0;\n");
        } else if (isIn(returnType, Long.TYPE, Long.class, Integer.TYPE, Integer.class, Short.TYPE, Short.class)) {
            if (updateMode) {
                methodBuilder.addJavadoc("@return number of updated records");
            } else {
                methodBuilder.addJavadoc("@return number of deleted records");
            }
        // methodBuilder.addCode("return result;\n");
        } else {
            // more than one listener found
            throw (new InvalidMethodSignException(method, "invalid return type"));
        }
        methodBuilder.addJavadoc("\n");
    }
}
Also used : Where_stmtContext(com.abubusoft.kripton.processor.sqlite.grammars.jsql.JqlParser.Where_stmtContext) TypeName(com.squareup.javapoet.TypeName) One(com.abubusoft.kripton.common.One) ArrayList(java.util.ArrayList) InvalidMethodSignException(com.abubusoft.kripton.processor.exceptions.InvalidMethodSignException) JQLReplacerListenerImpl(com.abubusoft.kripton.processor.sqlite.grammars.jql.JQLReplacerListenerImpl) SQLProperty(com.abubusoft.kripton.processor.sqlite.model.SQLProperty) Pair(com.abubusoft.kripton.common.Pair)

Example 2 with InvalidMethodSignException

use of com.abubusoft.kripton.processor.exceptions.InvalidMethodSignException in project kripton by xcesco.

the class ModifyRawHelper method generateJavaDoc.

private void generateJavaDoc(final SQLiteModelMethod method, Builder methodBuilder, boolean updateMode) {
    List<Pair<String, TypeName>> methodParams = method.getParameters();
    final List<SQLProperty> updatedProperties = new ArrayList<>();
    final List<Pair<String, TypeName>> methodParamsUsedAsParameter = new ArrayList<>();
    // new
    String sqlModify = JQLChecker.getInstance().replace(method, method.jql, new JQLReplacerListenerImpl(method) {

        @Override
        public String onColumnNameToUpdate(String columnName) {
            SQLProperty tempProperty = currentEntity.get(columnName);
            AssertKripton.assertTrueOrUnknownPropertyInJQLException(tempProperty != null, method, columnName);
            updatedProperties.add(tempProperty);
            return tempProperty.columnName;
        }

        @Override
        public String onColumnName(String columnName) {
            SQLProperty tempProperty = currentEntity.get(columnName);
            AssertKripton.assertTrueOrUnknownPropertyInJQLException(tempProperty != null, method, columnName);
            return tempProperty.columnName;
        }

        @Override
        public String onBindParameter(String bindParameterName) {
            String resolvedParamName = method.findParameterNameByAlias(bindParameterName);
            AssertKripton.assertTrueOrUnknownParamInJQLException(resolvedParamName != null, method, bindParameterName);
            methodParamsUsedAsParameter.add(new Pair<>(resolvedParamName, method.findParameterType(resolvedParamName)));
            return "${" + bindParameterName + "}";
        }
    });
    if (updateMode) {
        methodBuilder.addJavadoc("<h2>SQL update</h2>\n");
        methodBuilder.addJavadoc("<pre>$L</pre>\n", sqlModify);
        methodBuilder.addJavadoc("\n");
        // list of updated fields
        methodBuilder.addJavadoc("<h2>Updated columns:</h2>\n");
        methodBuilder.addJavadoc("<ul>\n");
        for (SQLProperty property : updatedProperties) {
            methodBuilder.addJavadoc("\t<li>$L</li>\n", property.columnName);
        }
        methodBuilder.addJavadoc("</ul>");
        methodBuilder.addJavadoc("\n\n");
    } else {
        methodBuilder.addJavadoc("<h2>SQL delete</h2>\n");
        methodBuilder.addJavadoc("<pre>$L</pre>\n", sqlModify);
        methodBuilder.addJavadoc("\n\n");
    }
    // list of where parameter
    methodBuilder.addJavadoc("<h2>Parameters:</h2>\n");
    methodBuilder.addJavadoc("<dl>\n");
    for (Pair<String, TypeName> property : methodParamsUsedAsParameter) {
        String rawName = method.findParameterNameByAlias(property.value0);
        methodBuilder.addJavadoc("\t<dt>$L</dt>", "${" + property.value0 + "}");
        methodBuilder.addJavadoc("<dd>is mapped to method's parameter <strong>$L</strong></dd>\n", rawName);
    }
    methodBuilder.addJavadoc("</dl>");
    methodBuilder.addJavadoc("\n\n");
    if (method.hasDynamicWhereConditions()) {
        methodBuilder.addJavadoc("<dl>\n");
        methodBuilder.addJavadoc("<dt>$L</dt><dd>is part of where conditions resolved at runtime. In above SQL it is displayed as #{$L}</dd>", method.dynamicWhereParameterName, JQLDynamicStatementType.DYNAMIC_WHERE);
        methodBuilder.addJavadoc("\n</dl>");
        methodBuilder.addJavadoc("\n\n");
    }
    // dynamic conditions
    if (method.hasDynamicWhereConditions()) {
        methodBuilder.addJavadoc("<h2>Method's parameters and associated dynamic parts:</h2>\n");
        methodBuilder.addJavadoc("<dl>\n");
        if (method.hasDynamicWhereConditions()) {
            methodBuilder.addJavadoc("<dt>$L</dt><dd>is part of where conditions resolved at runtime. In above SQL it is displayed as #{$L}</dd>", method.dynamicWhereParameterName, JQLDynamicStatementType.DYNAMIC_WHERE);
        }
        methodBuilder.addJavadoc("</dl>");
        methodBuilder.addJavadoc("\n\n");
    }
    // method parameters
    if (methodParams.size() > 0) {
        for (Pair<String, TypeName> param : methodParams) {
            String resolvedName = method.findParameterAliasByName(param.value0);
            methodBuilder.addJavadoc("@param $L", param.value0);
            if (method.isThisDynamicWhereConditionsName(param.value0)) {
                methodBuilder.addJavadoc("\n\tis used as dynamic where conditions\n");
            } else {
                methodBuilder.addJavadoc("\n\tis used as for parameter <strong>$L</strong>\n", resolvedName);
            }
        }
    }
    // if true, field must be associate to ben attributes
    TypeName returnType = method.getReturnClass();
    // define return value
    if (returnType == TypeName.VOID) {
    } else {
        methodBuilder.addJavadoc("\n");
        if (isIn(returnType, Boolean.TYPE, Boolean.class)) {
            if (updateMode) {
                methodBuilder.addJavadoc("@return <code>true</code> if record is updated, <code>false</code> otherwise");
            } else {
                methodBuilder.addJavadoc("@return <code>true</code> if record is deleted, <code>false</code> otherwise");
            }
            methodBuilder.addCode("return result!=0;\n");
        } else if (isIn(returnType, Long.TYPE, Long.class, Integer.TYPE, Integer.class, Short.TYPE, Short.class)) {
            if (updateMode) {
                methodBuilder.addJavadoc("@return number of updated records");
            } else {
                methodBuilder.addJavadoc("@return number of deleted records");
            }
        // methodBuilder.addCode("return result;\n");
        } else {
            // more than one listener found
            throw (new InvalidMethodSignException(method, "invalid return type"));
        }
        methodBuilder.addJavadoc("\n");
    }
}
Also used : TypeName(com.squareup.javapoet.TypeName) ArrayList(java.util.ArrayList) InvalidMethodSignException(com.abubusoft.kripton.processor.exceptions.InvalidMethodSignException) JQLReplacerListenerImpl(com.abubusoft.kripton.processor.sqlite.grammars.jql.JQLReplacerListenerImpl) SQLProperty(com.abubusoft.kripton.processor.sqlite.model.SQLProperty) Pair(com.abubusoft.kripton.common.Pair)

Example 3 with InvalidMethodSignException

use of com.abubusoft.kripton.processor.exceptions.InvalidMethodSignException in project kripton by xcesco.

the class SelectRawListenerHelper 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) {
    // LiteralType listenerType=LiteralType.of(OnReadCursorListener.class);
    ClassName listenerType = ClassName.get(OnReadCursorListener.class);
    int counter = SqlBuilderHelper.countParameterOfType(method, listenerType);
    if (counter == 0) {
        // non listener found
        throw (new InvalidMethodSignException(method, "there is no parameter of type \"ReadCursorListener\""));
    }
    if (counter > 1) {
        // more than one listener found
        throw (new InvalidMethodSignException(method, "there are more than one parameter of type \"ReadCursorListener\""));
    }
    String listenerName = SqlSelectBuilder.getNameParameterOfType(method, listenerType);
    methodBuilder.addCode("\n");
    methodBuilder.beginControlFlow("if (_cursor.moveToFirst())");
    // generate index from columns
    methodBuilder.addCode("\n");
    methodBuilder.beginControlFlow("do\n");
    methodBuilder.addCode("$L.onRead(_cursor);\n", listenerName);
    methodBuilder.endControlFlow("while (_cursor.moveToNext())");
    // close cursor
    methodBuilder.endControlFlow();
    methodBuilder.endControlFlow();
}
Also used : ClassName(com.squareup.javapoet.ClassName) InvalidMethodSignException(com.abubusoft.kripton.processor.exceptions.InvalidMethodSignException)

Example 4 with InvalidMethodSignException

use of com.abubusoft.kripton.processor.exceptions.InvalidMethodSignException in project kripton by xcesco.

the class SqlModifyBuilder method detectModifyType.

/**
 * Detect method type
 *
 * @param method
 * @param jqlType
 *            jql type is necessary because method.jql can be not properly
 *            initialized
 * @return
 */
public static ModifyType detectModifyType(SQLiteModelMethod method, JQLType jqlType) {
    // Elements elementUtils = BaseProcessor.elementUtils;
    SQLiteDaoDefinition daoDefinition = method.getParent();
    SQLiteEntity entity = daoDefinition.getEntity();
    ModifyType updateResultType = null;
    // check type of arguments
    int count = 0;
    for (Pair<String, TypeName> param : method.getParameters()) {
        if (method.isThisDynamicWhereConditionsName(param.value0)) {
            // if current parameter is dynamic where, skip it
            continue;
        }
        if (TypeUtility.isEquals(param.value1, typeName(entity.getElement()))) {
            count++;
        }
    }
    if (count == 0) {
        // method to update raw data: no bean is used
        updateResultType = jqlType == JQLType.UPDATE ? ModifyType.UPDATE_RAW : ModifyType.DELETE_RAW;
        ModelAnnotation annotation;
        if (jqlType == JQLType.UPDATE) {
            annotation = method.getAnnotation(BindSqlUpdate.class);
            AssertKripton.assertTrueOrInvalidMethodSignException(AnnotationUtility.extractAsStringArray(method, annotation, AnnotationAttributeType.FIELDS).size() == 0, method, " can not use attribute %s in this kind of query definition", AnnotationAttributeType.FIELDS.getValue());
            AssertKripton.assertTrueOrInvalidMethodSignException(AnnotationUtility.extractAsStringArray(method, annotation, AnnotationAttributeType.EXCLUDED_FIELDS).size() == 0, method, " can not use attribute %s in this kind of query definition", AnnotationAttributeType.EXCLUDED_FIELDS.getValue());
        } else {
            annotation = method.getAnnotation(BindSqlDelete.class);
        }
        // check if there is only one parameter
        AssertKripton.failWithInvalidMethodSignException(method.getParameters().size() > 1 && TypeUtility.isEquals(method.getParameters().get(0).value1, daoDefinition.getEntityClassName()), method);
    } else if (count == 1) {
        updateResultType = jqlType == JQLType.UPDATE ? ModifyType.UPDATE_BEAN : ModifyType.DELETE_BEAN;
        // with dynamic where conditions, we have to add 1 to parameter
        // check size (one parameter is a bean and one is the where
        // conditions)
        AssertKripton.assertTrueOrInvalidMethodSignException(method.getParameters().size() == 1 + (method.hasDynamicWhereConditions() ? 1 : 0) + (method.hasDynamicWhereArgs() ? 1 : 0), method, " expected only one parameter of %s type", daoDefinition.getEntityClassName());
    } else {
        throw (new InvalidMethodSignException(method, "only one parameter of type " + typeName(entity.getElement()) + " can be used"));
    }
    return updateResultType;
}
Also used : TypeName(com.squareup.javapoet.TypeName) ArrayTypeName(com.squareup.javapoet.ArrayTypeName) ModelAnnotation(com.abubusoft.kripton.processor.core.ModelAnnotation) BindSqlDelete(com.abubusoft.kripton.android.annotation.BindSqlDelete) SQLiteEntity(com.abubusoft.kripton.processor.sqlite.model.SQLiteEntity) InvalidMethodSignException(com.abubusoft.kripton.processor.exceptions.InvalidMethodSignException) BindSqlUpdate(com.abubusoft.kripton.android.annotation.BindSqlUpdate) SQLiteDaoDefinition(com.abubusoft.kripton.processor.sqlite.model.SQLiteDaoDefinition)

Example 5 with InvalidMethodSignException

use of com.abubusoft.kripton.processor.exceptions.InvalidMethodSignException in project kripton by xcesco.

the class SqlInsertBuilder method detectInsertType.

public static InsertType detectInsertType(SQLiteModelMethod method) {
    SQLiteDaoDefinition daoDefinition = method.getParent();
    SQLiteEntity entity = daoDefinition.getEntity();
    InsertType insertResultType = null;
    // check type of arguments
    int count = 0;
    for (Pair<String, TypeName> param : method.getParameters()) {
        if (TypeUtility.isEquals(param.value1, typeName(entity.getElement()))) {
            count++;
        }
    }
    AssertKripton.failWithInvalidMethodSignException(method.getParameters().size() == 0, method, " INSERT operations require at least one parameter");
    if (count == 0) {
        // method to insert raw data: no bean is used
        insertResultType = InsertType.INSERT_RAW;
        ModelAnnotation annotation = method.getAnnotation(BindSqlInsert.class);
        // check value attribute
        AssertKripton.failWithInvalidMethodSignException(AnnotationUtility.extractAsStringArray(method, annotation, AnnotationAttributeType.FIELDS).size() > 0, method, " can not use attribute %s in this kind of query definition", AnnotationAttributeType.FIELDS.getValue());
        // check excludeFields attribute
        AssertKripton.failWithInvalidMethodSignException(AnnotationUtility.extractAsStringArray(method, annotation, AnnotationAttributeType.EXCLUDED_FIELDS).size() > 0, method, " can not use attribute %s in this kind of query definition", AnnotationAttributeType.EXCLUDED_FIELDS.getValue());
        // check if there is only one parameter
        AssertKripton.failWithInvalidMethodSignException(method.getParameters().size() != 1 && TypeUtility.isEquals(method.getParameters().get(0).value1, daoDefinition.getEntityClassName()), method);
        // check no
        AssertKripton.failWithInvalidMethodSignException(annotation.getAttributeAsBoolean(AnnotationAttributeType.INCLUDE_PRIMARY_KEY), method, "attribute '%s' can not be used here", AnnotationAttributeType.INCLUDE_PRIMARY_KEY.getValue());
    } else if (count == 1) {
        insertResultType = InsertType.INSERT_BEAN;
        AssertKripton.failWithInvalidMethodSignException(method.getParameters().size() > 1, method, " aspected only one parameter of %s type", daoDefinition.getEntityClassName());
    } else {
        throw (new InvalidMethodSignException(method, "only one parameter of type " + typeName(entity.getElement()) + " can be used"));
    }
    return insertResultType;
}
Also used : TypeName(com.squareup.javapoet.TypeName) ModelAnnotation(com.abubusoft.kripton.processor.core.ModelAnnotation) SQLiteEntity(com.abubusoft.kripton.processor.sqlite.model.SQLiteEntity) InvalidMethodSignException(com.abubusoft.kripton.processor.exceptions.InvalidMethodSignException) SQLiteDaoDefinition(com.abubusoft.kripton.processor.sqlite.model.SQLiteDaoDefinition)

Aggregations

InvalidMethodSignException (com.abubusoft.kripton.processor.exceptions.InvalidMethodSignException)12 TypeName (com.squareup.javapoet.TypeName)9 SQLProperty (com.abubusoft.kripton.processor.sqlite.model.SQLProperty)6 SQLiteDaoDefinition (com.abubusoft.kripton.processor.sqlite.model.SQLiteDaoDefinition)6 SQLiteEntity (com.abubusoft.kripton.processor.sqlite.model.SQLiteEntity)6 Pair (com.abubusoft.kripton.common.Pair)4 SQLiteStatement (android.database.sqlite.SQLiteStatement)3 ArrayList (java.util.ArrayList)3 ModelAnnotation (com.abubusoft.kripton.processor.core.ModelAnnotation)2 PropertyNotFoundException (com.abubusoft.kripton.processor.exceptions.PropertyNotFoundException)2 JQLReplacerListenerImpl (com.abubusoft.kripton.processor.sqlite.grammars.jql.JQLReplacerListenerImpl)2 ArrayTypeName (com.squareup.javapoet.ArrayTypeName)2 ParameterizedTypeName (com.squareup.javapoet.ParameterizedTypeName)2 BindSqlDelete (com.abubusoft.kripton.android.annotation.BindSqlDelete)1 BindSqlUpdate (com.abubusoft.kripton.android.annotation.BindSqlUpdate)1 OnReadBeanListener (com.abubusoft.kripton.android.sqlite.OnReadBeanListener)1 One (com.abubusoft.kripton.common.One)1 SQLTypeAdapterUtils (com.abubusoft.kripton.common.SQLTypeAdapterUtils)1 ModelProperty (com.abubusoft.kripton.processor.core.ModelProperty)1 JQLProjection (com.abubusoft.kripton.processor.sqlite.grammars.jql.JQLProjection)1