Search in sources :

Example 11 with InvalidMethodSignException

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

the class InsertRawHelper method generate.

@Override
public void generate(TypeSpec.Builder classBuilder, MethodSpec.Builder methodBuilder, boolean mapFields, final SQLiteModelMethod method, TypeName returnType) {
    final SQLiteDaoDefinition daoDefinition = method.getParent();
    final SQLiteEntity entity = daoDefinition.getEntity();
    // boolean nullable;
    // generate javadoc
    generateJavaDoc(methodBuilder, method, returnType);
    // standard INSERT
    if (method.jql.hasDynamicParts() || method.jql.containsSelectOperation) {
        methodBuilder.addStatement("$T _contentValues=contentValuesForUpdate()", KriptonContentValues.class);
    } else {
        String psName = method.buildPreparedStatementName();
        // generate SQL for insert
        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);
    }
    if (method.jql.containsSelectOperation) {
        // INSERT-SELECT
        GenericSQLHelper.generateGenericExecSQL(methodBuilder, method);
    } else {
        methodBuilder.addCode("\n");
        List<Pair<String, TypeName>> fieldsToUpdate = method.getParameters();
        fieldsToUpdate = SqlBuilderHelper.orderContentValues(method, fieldsToUpdate);
        for (Pair<String, TypeName> item : fieldsToUpdate) {
            String propertyName = method.findParameterAliasByName(item.value0);
            SQLProperty property = entity.get(propertyName);
            if (property == null)
                throw (new PropertyNotFoundException(method, propertyName, item.value1));
            // check same type
            TypeUtility.checkTypeCompatibility(method, item, property);
            if (method.isLogEnabled()) {
                methodBuilder.addCode("_contentValues.put($S, ", property.columnName);
            } else {
                methodBuilder.addCode("_contentValues.put(");
            }
            // it does not need to be converted in string
            SQLTransformer.javaMethodParam2ContentValues(methodBuilder, method, item.value0, item.value1, property);
            methodBuilder.addCode(");\n");
        }
        methodBuilder.addCode("\n");
        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 (daoDefinition.getParent().generateRx) {
            GenericSQLHelper.generateSubjectNext(methodBuilder, SubjectType.INSERT);
        }
        // support for livedata
        if (daoDefinition.hasLiveData()) {
            methodBuilder.addComment("support for livedata");
            methodBuilder.addStatement(BindDaoBuilder.METHOD_NAME_REGISTRY_EVENT + "(result)");
        }
        // define return value
        if (returnType == TypeName.VOID) {
        } else if (TypeUtility.isTypeIncludedIn(returnType, Boolean.TYPE, Boolean.class)) {
            methodBuilder.addCode("return result!=-1;\n");
        } else if (TypeUtility.isTypeIncludedIn(returnType, Long.TYPE, Long.class)) {
            methodBuilder.addCode("return result;\n");
        } else if (TypeUtility.isTypeIncludedIn(returnType, Integer.TYPE, Integer.class)) {
            methodBuilder.addCode("return (int)result;\n");
        } else {
            // more than one listener found
            throw (new InvalidMethodSignException(method, "invalid return type"));
        }
    }
}
Also used : TypeName(com.squareup.javapoet.TypeName) PropertyNotFoundException(com.abubusoft.kripton.processor.exceptions.PropertyNotFoundException) InvalidMethodSignException(com.abubusoft.kripton.processor.exceptions.InvalidMethodSignException) SQLiteDaoDefinition(com.abubusoft.kripton.processor.sqlite.model.SQLiteDaoDefinition) SQLiteStatement(android.database.sqlite.SQLiteStatement) SQLProperty(com.abubusoft.kripton.processor.sqlite.model.SQLProperty) SQLiteEntity(com.abubusoft.kripton.processor.sqlite.model.SQLiteEntity) Pair(com.abubusoft.kripton.common.Pair)

Example 12 with InvalidMethodSignException

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

the class ModifyRawHelper method generate.

@Override
public void generate(TypeSpec.Builder classBuilder, MethodSpec.Builder methodBuilder, boolean updateMode, SQLiteModelMethod method, TypeName returnType) {
    SQLiteDaoDefinition daoDefinition = method.getParent();
    SQLiteEntity entity = daoDefinition.getEntity();
    // separate params used for update bean and params used in
    // whereCondition
    // analyze whereCondition
    String whereCondition = extractWhereConditions(updateMode, method);
    // this method is invoked to check if every parameter is binded to an method param
    SqlUtility.extractParametersFromString(method.jql.value, method, entity);
    Pair<String, List<Pair<String, TypeName>>> where = SqlUtility.extractParametersFromString(whereCondition, method, entity);
    // defines which parameter is used like update field and which is used
    // in where condition.
    List<Pair<String, TypeName>> methodParams = method.getParameters();
    List<Pair<String, TypeName>> updateableParams = new ArrayList<Pair<String, TypeName>>();
    List<Pair<String, TypeName>> whereParams = new ArrayList<Pair<String, TypeName>>();
    String name;
    for (Pair<String, TypeName> param : methodParams) {
        name = method.findParameterAliasByName(param.value0);
        if (method.isThisDynamicWhereConditionsName(name)) {
            // skip for dynamic where
            continue;
        }
        if (method.isThisDynamicWhereArgsName(name)) {
            // skip for dynamic where
            continue;
        }
        if (where.value1.contains(new Pair<>(name, param.value1))) {
            whereParams.add(param);
        } else {
            updateableParams.add(param);
        }
    }
    // clear contentValues
    if (method.jql.hasDynamicParts() || method.jql.containsSelectOperation) {
        methodBuilder.addStatement("$T _contentValues=contentValuesForUpdate()", KriptonContentValues.class);
    } else {
        String psName = method.buildPreparedStatementName();
        // generate SQL for insert
        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);
    }
    if (method.jql.containsSelectOperation) {
        generateJavaDoc(method, methodBuilder, updateMode);
        GenericSQLHelper.generateGenericExecSQL(methodBuilder, method);
    } else {
        // generate javadoc
        generateJavaDoc(method, methodBuilder, updateMode, whereCondition, where, methodParams);
        if (updateMode) {
            AssertKripton.assertTrueOrInvalidMethodSignException(updateableParams.size() > 0, method, "no column was selected for update");
            // order item for content values
            updateableParams = SqlBuilderHelper.orderContentValues(method, updateableParams);
            for (Pair<String, TypeName> item : updateableParams) {
                String resolvedParamName = method.findParameterAliasByName(item.value0);
                SQLProperty property = entity.get(resolvedParamName);
                if (property == null)
                    throw (new PropertyNotFoundException(method, resolvedParamName, item.value1));
                // check same type
                TypeUtility.checkTypeCompatibility(method, item, property);
                // here it needed raw parameter typeName
                if (method.isLogEnabled()) {
                    methodBuilder.addCode("_contentValues.put($S, ", property.columnName);
                } else {
                    methodBuilder.addCode("_contentValues.put(");
                }
                SQLTransformer.javaMethodParam2ContentValues(methodBuilder, method, item.value0, TypeUtility.typeName(property.getElement()), property);
                methodBuilder.addCode(");\n");
            // if (nullable) {
            // methodBuilder.nextControlFlow("else");
            // 
            // if (method.isLogEnabled()) {
            // methodBuilder.addStatement("_contentValues.putNull($S)", property.columnName);
            // } else {
            // methodBuilder.addStatement("_contentValues.putNull()");
            // }
            // 
            // methodBuilder.endControlFlow();
            // }
            }
            methodBuilder.addCode("\n");
        } else {
            if (updateableParams.size() > 0) {
                String separator = "";
                StringBuilder buffer = new StringBuilder();
                for (Pair<String, TypeName> item : updateableParams) {
                    String resolvedParamName = method.findParameterAliasByName(item.value0);
                    buffer.append(separator + resolvedParamName);
                    separator = ", ";
                }
                // in DELETE can not be updated fields
                if (updateableParams.size() > 1) {
                    throw (new InvalidMethodSignException(method, " parameters " + buffer.toString() + " are not used in where conditions"));
                } else {
                    throw (new InvalidMethodSignException(method, " parameter " + buffer.toString() + " is not used in where conditions"));
                }
            }
        }
        // build where condition
        generateWhereCondition(methodBuilder, method, where);
        methodBuilder.addCode("\n");
        ModifyBeanHelper.generateModifyQueryCommonPart(method, classBuilder, methodBuilder);
        // support for livedata
        if (daoDefinition.hasLiveData()) {
            methodBuilder.addComment("support for livedata");
            methodBuilder.addStatement(BindDaoBuilder.METHOD_NAME_REGISTRY_EVENT + "(result)");
        }
        // if true, field must be associate to ben attributes
        if (returnType == TypeName.VOID) {
        } else {
            if (isIn(returnType, Boolean.TYPE, Boolean.class)) {
                methodBuilder.addStatement("return result!=0");
            } else if (isIn(returnType, Long.TYPE, Long.class, Integer.TYPE, Integer.class, Short.TYPE, Short.class)) {
                methodBuilder.addStatement("return result");
            } else {
                // more than one listener found
                throw (new InvalidMethodSignException(method, "invalid return type"));
            }
        }
    }
}
Also used : TypeName(com.squareup.javapoet.TypeName) PropertyNotFoundException(com.abubusoft.kripton.processor.exceptions.PropertyNotFoundException) ArrayList(java.util.ArrayList) InvalidMethodSignException(com.abubusoft.kripton.processor.exceptions.InvalidMethodSignException) SQLiteDaoDefinition(com.abubusoft.kripton.processor.sqlite.model.SQLiteDaoDefinition) SQLiteStatement(android.database.sqlite.SQLiteStatement) SQLProperty(com.abubusoft.kripton.processor.sqlite.model.SQLProperty) ArrayList(java.util.ArrayList) List(java.util.List) SQLiteEntity(com.abubusoft.kripton.processor.sqlite.model.SQLiteEntity) Pair(com.abubusoft.kripton.common.Pair)

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