Search in sources :

Example 1 with ConflictAlgorithmType

use of com.abubusoft.kripton.android.sqlite.ConflictAlgorithmType in project kripton by xcesco.

the class SqlInsertBuilder method generateInsertForContentProvider.

/**
 * <p>
 * Generate insert used in content provider class.
 * </p>
 *
 * @param methodBuilder
 * @param method
 * @param insertResultType
 */
private static void generateInsertForContentProvider(TypeSpec.Builder classBuilder, final SQLiteModelMethod method, InsertType insertResultType) {
    final SQLiteDaoDefinition daoDefinition = method.getParent();
    final SQLiteEntity entity = daoDefinition.getEntity();
    final Set<String> columns = new LinkedHashSet<>();
    MethodSpec.Builder methodBuilder = MethodSpec.methodBuilder(method.contentProviderMethodName);
    ParameterSpec parameterSpec;
    parameterSpec = ParameterSpec.builder(Uri.class, "uri").build();
    methodBuilder.addParameter(parameterSpec);
    parameterSpec = ParameterSpec.builder(ContentValues.class, "contentValues").build();
    methodBuilder.addParameter(parameterSpec);
    methodBuilder.returns(Long.TYPE);
    SqlBuilderHelper.generateLogForContentProviderBeginning(method, methodBuilder);
    // just detect which columns are admitted
    JQLChecker.getInstance().replace(method, method.jql, new JQLReplacerListenerImpl(method) {

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

        @Override
        public String onColumnFullyQualifiedName(String tableName, String columnName) {
            AssertKripton.fail("Inconsistent state");
            return null;
        }
    });
    // generate columnCheckSet
    SqlBuilderHelper.generateColumnCheckSet(classBuilder, method, columns);
    // retrieve content values
    methodBuilder.addStatement("$T _contentValues=contentValuesForContentProvider(contentValues)", KriptonContentValues.class);
    // generate column check
    SqlBuilderHelper.forEachColumnInContentValue(methodBuilder, method, "_contentValues.values().keySet()", true, null);
    methodBuilder.addCode("\n");
    // every controls was done in constructor of SQLiteModelMethod
    for (ContentUriPlaceHolder variable : method.contentProviderUriVariables) {
        SQLProperty entityProperty = entity.get(variable.value);
        if (entityProperty != null) {
            methodBuilder.addCode("// Add parameter $L at path segment $L\n", variable.value, variable.pathSegmentIndex);
            TypeName entityPropertyType = entityProperty.getPropertyType().getTypeName();
            if (TypeUtility.isString(entityPropertyType)) {
                methodBuilder.addStatement("contentValues.put($S, uri.getPathSegments().get($L))", entityProperty.columnName, variable.pathSegmentIndex);
            } else {
                methodBuilder.addStatement("contentValues.put($S, Long.valueOf(uri.getPathSegments().get($L)))", entityProperty.columnName, variable.pathSegmentIndex);
            }
        }
    }
    // generate log for inser operation
    SqlBuilderHelper.generateLogForContentValuesContentProvider(method, methodBuilder);
    ConflictAlgorithmType conflictAlgorithmType = InsertBeanHelper.getConflictAlgorithmType(method);
    String conflictString1 = "";
    String conflictString2 = "";
    if (conflictAlgorithmType != ConflictAlgorithmType.NONE) {
        conflictString1 = "WithOnConflict";
        conflictString2 = ", " + conflictAlgorithmType.getConflictAlgorithm();
        methodBuilder.addCode("// conflict algorithm $L\n", method.jql.conflictAlgorithmType);
    }
    methodBuilder.addComment("insert operation");
    methodBuilder.addStatement("long result = database().insert$L($S, null, _contentValues.values()$L)", conflictString1, daoDefinition.getEntity().getTableName(), conflictString2);
    if (method.getParent().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>0?1:0)");
    }
    methodBuilder.addStatement("return result");
    // javadoc
    // we add at last javadoc, because need info is built at last.
    SqlBuilderHelper.generateJavaDocForContentProvider(method, methodBuilder);
    methodBuilder.addJavadoc("@param uri $S\n", method.contentProviderUriTemplate.replace("*", "[*]"));
    methodBuilder.addJavadoc("@param contentValues content values\n");
    methodBuilder.addJavadoc("@return new row's id\n");
    classBuilder.addMethod(methodBuilder.build());
}
Also used : LinkedHashSet(java.util.LinkedHashSet) TypeName(com.squareup.javapoet.TypeName) MethodSpec(com.squareup.javapoet.MethodSpec) ParameterSpec(com.squareup.javapoet.ParameterSpec) ConflictAlgorithmType(com.abubusoft.kripton.android.sqlite.ConflictAlgorithmType) SQLiteDaoDefinition(com.abubusoft.kripton.processor.sqlite.model.SQLiteDaoDefinition) JQLReplacerListenerImpl(com.abubusoft.kripton.processor.sqlite.grammars.jql.JQLReplacerListenerImpl) ContentUriPlaceHolder(com.abubusoft.kripton.processor.sqlite.grammars.uri.ContentUriPlaceHolder) SQLProperty(com.abubusoft.kripton.processor.sqlite.model.SQLProperty) SQLiteEntity(com.abubusoft.kripton.processor.sqlite.model.SQLiteEntity)

Example 2 with ConflictAlgorithmType

use of com.abubusoft.kripton.android.sqlite.ConflictAlgorithmType in project kripton by xcesco.

the class InsertBeanHelper method getConflictAlgorithmType.

public static ConflictAlgorithmType getConflictAlgorithmType(SQLiteModelMethod method) {
    ModelAnnotation annotation = method.getAnnotation(BindSqlInsert.class);
    String value = annotation.getAttribute(AnnotationAttributeType.CONFLICT_ALGORITHM_TYPE);
    if (value != null && value.indexOf(".") > -1) {
        value = value.substring(value.lastIndexOf(".") + 1);
    }
    ConflictAlgorithmType conflictAlgorithmType = ConflictAlgorithmType.valueOf(value);
    return conflictAlgorithmType;
}
Also used : ModelAnnotation(com.abubusoft.kripton.processor.core.ModelAnnotation) ConflictAlgorithmType(com.abubusoft.kripton.android.sqlite.ConflictAlgorithmType)

Aggregations

ConflictAlgorithmType (com.abubusoft.kripton.android.sqlite.ConflictAlgorithmType)2 ModelAnnotation (com.abubusoft.kripton.processor.core.ModelAnnotation)1 JQLReplacerListenerImpl (com.abubusoft.kripton.processor.sqlite.grammars.jql.JQLReplacerListenerImpl)1 ContentUriPlaceHolder (com.abubusoft.kripton.processor.sqlite.grammars.uri.ContentUriPlaceHolder)1 SQLProperty (com.abubusoft.kripton.processor.sqlite.model.SQLProperty)1 SQLiteDaoDefinition (com.abubusoft.kripton.processor.sqlite.model.SQLiteDaoDefinition)1 SQLiteEntity (com.abubusoft.kripton.processor.sqlite.model.SQLiteEntity)1 MethodSpec (com.squareup.javapoet.MethodSpec)1 ParameterSpec (com.squareup.javapoet.ParameterSpec)1 TypeName (com.squareup.javapoet.TypeName)1 LinkedHashSet (java.util.LinkedHashSet)1