Search in sources :

Example 6 with InvalidMethodSignException

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

the class InsertBeanHelper method generate.

@Override
public void generate(TypeSpec.Builder classBuilder, MethodSpec.Builder methodBuilder, boolean mapFields, SQLiteModelMethod method, TypeName returnType) {
    SQLiteDaoDefinition daoDefinition = method.getParent();
    SQLiteEntity entity = daoDefinition.getEntity();
    // retrieve content values
    if (method.jql.hasDynamicParts() || method.jql.containsSelectOperation) {
        methodBuilder.addStatement("$T _contentValues=contentValuesForUpdate()", KriptonContentValues.class);
    } else {
        String psName = method.buildPreparedStatementName();
        classBuilder.addField(FieldSpec.builder(TypeName.get(SQLiteStatement.class), psName, Modifier.PRIVATE, Modifier.STATIC).build());
        methodBuilder.beginControlFlow("if ($L==null)", psName);
        SqlBuilderHelper.generateSQLForStaticQuery(method, methodBuilder);
        methodBuilder.addStatement("$L = $T.compile(_context, _sql)", psName, KriptonDatabaseWrapper.class);
        methodBuilder.endControlFlow();
        methodBuilder.addStatement("$T _contentValues=contentValuesForUpdate($L)", KriptonContentValues.class, psName);
    }
    List<SQLProperty> listUsedProperty = CodeBuilderUtility.extractUsedProperties(methodBuilder, method, BindSqlInsert.class);
    CodeBuilderUtility.generateContentValuesFromEntity(BaseProcessor.elementUtils, method, BindSqlInsert.class, methodBuilder, null);
    ModelProperty primaryKey = entity.getPrimaryKey();
    // generate javadoc and query
    generateJavaDoc(methodBuilder, method, returnType, listUsedProperty, primaryKey);
    SqlBuilderHelper.generateLog(method, methodBuilder);
    methodBuilder.addComment("insert operation");
    if (method.jql.hasDynamicParts() || method.jql.containsSelectOperation) {
        // does not memorize compiled statement, it can vary every time
        // generate SQL for insert
        SqlBuilderHelper.generateSQLForInsertDynamic(method, methodBuilder);
        methodBuilder.addStatement("long result = $T.insert(_context, _sql, _contentValues)", KriptonDatabaseWrapper.class);
    } else {
        String psName = method.buildPreparedStatementName();
        methodBuilder.addStatement("long result = $T.insert($L, _contentValues)", KriptonDatabaseWrapper.class, psName);
    }
    if (method.getParent().getParent().generateRx) {
        GenericSQLHelper.generateSubjectNext(methodBuilder, SubjectType.INSERT);
    }
    if (primaryKey != null) {
        if (primaryKey.isPublicField()) {
            methodBuilder.addCode("$L.$L=result;\n", method.getParameters().get(0).value0, primaryKey.getName());
        } else {
            methodBuilder.addCode("$L.$L(result);\n", method.getParameters().get(0).value0, PropertyUtility.setter(typeName(entity.getElement()), primaryKey));
        }
    }
    // support for livedata
    if (daoDefinition.hasLiveData()) {
        methodBuilder.addComment("support for livedata");
        methodBuilder.addStatement(BindDaoBuilder.METHOD_NAME_REGISTRY_EVENT + "(result>0?1:0)");
    }
    // define return value
    if (returnType == TypeName.VOID) {
    } else if (TypeUtility.isTypeIncludedIn(returnType, String.class)) {
        methodBuilder.addCode("\n");
        methodBuilder.addCode("return String.valueOf(result);\n");
    } else if (TypeUtility.isTypeIncludedIn(returnType, Boolean.TYPE, Boolean.class)) {
        methodBuilder.addCode("\n");
        methodBuilder.addCode("return result!=-1;\n");
    } else if (TypeUtility.isTypeIncludedIn(returnType, Long.TYPE, Long.class)) {
        methodBuilder.addCode("\n");
        methodBuilder.addCode("return result;\n");
    } else if (TypeUtility.isTypeIncludedIn(returnType, Integer.TYPE, Integer.class)) {
        methodBuilder.addCode("\n");
        methodBuilder.addCode("return (int)result;\n");
    } else if (TypeUtility.isEquals(returnType, entity)) {
        methodBuilder.addCode("\n");
        methodBuilder.addCode("return $L;\n", method.getParameters().get(0).value0);
    } else {
        // more than one listener found
        throw (new InvalidMethodSignException(method, "invalid return type"));
    }
}
Also used : SQLiteStatement(android.database.sqlite.SQLiteStatement) SQLProperty(com.abubusoft.kripton.processor.sqlite.model.SQLProperty) ModelProperty(com.abubusoft.kripton.processor.core.ModelProperty) SQLiteEntity(com.abubusoft.kripton.processor.sqlite.model.SQLiteEntity) InvalidMethodSignException(com.abubusoft.kripton.processor.exceptions.InvalidMethodSignException) SQLiteDaoDefinition(com.abubusoft.kripton.processor.sqlite.model.SQLiteDaoDefinition)

Example 7 with InvalidMethodSignException

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

the class AbstractSelectCodeGenerator method checkUnusedParameters.

/**
 * Check if there are unused method parameters. In this case an exception
 * was throws.
 *
 * @param method
 * @param usedMethodParameters
 */
public static void checkUnusedParameters(SQLiteModelMethod method, Set<String> usedMethodParameters, TypeName excludedClasses) {
    int paramsCount = method.getParameters().size();
    int usedCount = usedMethodParameters.size();
    if (paramsCount > usedCount) {
        StringBuilder sb = new StringBuilder();
        String separator = "";
        for (Pair<String, TypeName> item : method.getParameters()) {
            if (excludedClasses != null && item.value1.equals(excludedClasses)) {
                usedCount++;
            } else {
                if (!usedMethodParameters.contains(item.value0)) {
                    sb.append(separator + "'" + item.value0 + "'");
                    separator = ", ";
                }
            }
        }
        if (paramsCount > usedCount) {
            throw (new InvalidMethodSignException(method, "unused parameter(s) " + sb.toString()));
        }
    }
}
Also used : ParameterizedTypeName(com.squareup.javapoet.ParameterizedTypeName) TypeName(com.squareup.javapoet.TypeName) InvalidMethodSignException(com.abubusoft.kripton.processor.exceptions.InvalidMethodSignException)

Example 8 with InvalidMethodSignException

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

the class ModifyBeanHelper method buildReturnCode.

/**
 * @param methodBuilder
 * @param updateMode
 * @param method
 * @param returnType
 */
public void buildReturnCode(MethodSpec.Builder methodBuilder, boolean updateMode, SQLiteModelMethod method, TypeName returnType) {
    if (returnType == TypeName.VOID) {
    } else if (isTypeIncludedIn(returnType, Boolean.TYPE, Boolean.class)) {
        methodBuilder.addJavadoc("\n");
        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.addJavadoc("\n");
        methodBuilder.addCode("return result!=0;\n");
    } else if (isTypeIncludedIn(returnType, Long.TYPE, Long.class, Integer.TYPE, Integer.class, Short.TYPE, Short.class)) {
        methodBuilder.addJavadoc("\n");
        if (updateMode) {
            methodBuilder.addJavadoc("@return number of updated records");
        } else {
            methodBuilder.addJavadoc("@return number of deleted records");
        }
        methodBuilder.addJavadoc("\n");
        methodBuilder.addCode("return result;\n");
    } else {
        // more than one listener found
        throw (new InvalidMethodSignException(method, "invalid return type"));
    }
}
Also used : InvalidMethodSignException(com.abubusoft.kripton.processor.exceptions.InvalidMethodSignException)

Example 9 with InvalidMethodSignException

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

the class SelectBeanListenerHelper 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();
    // LiteralType listenerType = LiteralType.of(OnReadBeanListener.class, entity.getElement());
    ParameterizedTypeName listenerType = ParameterizedTypeName.get(ClassName.get(OnReadBeanListener.class), TypeName.get(entity.getElement().asType()));
    // List<SQLProperty> fields = fieldList.value1;
    TypeName entityClass = typeName(entity.getElement());
    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("$T resultBean=new $T();", entityClass, entityClass);
    methodBuilder.addCode("\n");
    // methodBuilder.beginControlFlow("try");
    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("int rowCount=_cursor.getCount();\n");
    methodBuilder.beginControlFlow("do\n");
    // reset mapping
    methodBuilder.addCode("// reset mapping\n");
    {
        int i = 0;
        for (SQLProperty item : entity.getCollection()) {
            if (item.isNullable()) {
                SQLTransformer.resetBean(methodBuilder, entityClass, "resultBean", item, "_cursor", "index" + i + "");
                methodBuilder.addCode(";");
                methodBuilder.addCode("\n");
            } else {
                methodBuilder.addCode("// " + item.getName() + " does not need reset\n");
            }
            i++;
        }
    }
    methodBuilder.addCode("\n");
    // generate mapping
    methodBuilder.addCode("// generate mapping\n");
    {
        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("$L.onRead(resultBean, _cursor.getPosition(), rowCount);\n", listenerName);
    methodBuilder.endControlFlow("while (_cursor.moveToNext())");
    // close try { open cursor
    methodBuilder.endControlFlow();
    // close method
    methodBuilder.endControlFlow();
}
Also used : ParameterizedTypeName(com.squareup.javapoet.ParameterizedTypeName) TypeName(com.squareup.javapoet.TypeName) OnReadBeanListener(com.abubusoft.kripton.android.sqlite.OnReadBeanListener) 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) InvalidMethodSignException(com.abubusoft.kripton.processor.exceptions.InvalidMethodSignException) SQLiteDaoDefinition(com.abubusoft.kripton.processor.sqlite.model.SQLiteDaoDefinition) ParameterizedTypeName(com.squareup.javapoet.ParameterizedTypeName)

Example 10 with InvalidMethodSignException

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

the class SqlModifyBuilder method generate.

/**
 * @param elementUtils
 * @param builder
 * @param method
 * @param updateMode
 */
public static void generate(TypeSpec.Builder classBuilder, SQLiteModelMethod method) {
    ModifyType updateResultType = detectModifyType(method, method.jql.operationType);
    // if true, field must be associate to ben attributes
    TypeName returnType = method.getReturnClass();
    if (updateResultType == null) {
        throw (new InvalidMethodSignException(method));
    }
    // generate method code
    MethodSpec.Builder methodBuilder = MethodSpec.methodBuilder(method.getName()).addAnnotation(Override.class).addModifiers(Modifier.PUBLIC);
    ParameterSpec parameterSpec;
    for (Pair<String, TypeName> item : method.getParameters()) {
        parameterSpec = ParameterSpec.builder(item.value1, item.value0).build();
        methodBuilder.addParameter(parameterSpec);
    }
    methodBuilder.returns(returnType);
    // generate inner code
    updateResultType.generate(classBuilder, methodBuilder, method, returnType);
    MethodSpec methodSpec = methodBuilder.build();
    classBuilder.addMethod(methodSpec);
    if (method.contentProviderEntryPathEnabled) {
        // delete-select, update-select can be used with content provider
        // insert-select no
        // if (method.jql.containsSelectOperation && updateResultType==) {
        // AssertKripton.failWithInvalidMethodSignException(true, method, "
        // SQL with inner SELECT can not be used in content provider");
        // }
        generateModifierForContentProvider(BaseProcessor.elementUtils, classBuilder, method, updateResultType);
    }
}
Also used : TypeName(com.squareup.javapoet.TypeName) ArrayTypeName(com.squareup.javapoet.ArrayTypeName) MethodSpec(com.squareup.javapoet.MethodSpec) ParameterSpec(com.squareup.javapoet.ParameterSpec) InvalidMethodSignException(com.abubusoft.kripton.processor.exceptions.InvalidMethodSignException)

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