Search in sources :

Example 6 with StorIOContentResolverColumnMeta

use of com.pushtorefresh.storio.contentresolver.annotations.processor.introspection.StorIOContentResolverColumnMeta in project storio by pushtorefresh.

the class PutResolverGenerator method createMapToContentValuesMethodSpec.

@NotNull
private MethodSpec createMapToContentValuesMethodSpec(@NotNull final StorIOContentResolverTypeMeta storIOContentResolverTypeMeta, @NotNull final ClassName storIOContentResolverClassName) {
    MethodSpec.Builder builder = MethodSpec.methodBuilder("mapToContentValues").addJavadoc("{@inheritDoc}\n").addAnnotation(Override.class).addAnnotation(ANDROID_NON_NULL_ANNOTATION_CLASS_NAME).addModifiers(PUBLIC).returns(ClassName.get("android.content", "ContentValues")).addParameter(ParameterSpec.builder(storIOContentResolverClassName, "object").addAnnotation(ANDROID_NON_NULL_ANNOTATION_CLASS_NAME).build()).addStatement("ContentValues contentValues = new ContentValues($L)", storIOContentResolverTypeMeta.columns.size()).addCode("\n");
    for (final StorIOContentResolverColumnMeta columnMeta : storIOContentResolverTypeMeta.columns.values()) {
        final boolean ignoreNull = columnMeta.storIOColumn.ignoreNull();
        if (ignoreNull) {
            builder.beginControlFlow("if (object.$L != null)", columnMeta.elementName + (columnMeta.isMethod() ? "()" : ""));
        }
        builder.addStatement("contentValues.put($S, object.$L)", columnMeta.storIOColumn.name(), columnMeta.elementName + (columnMeta.isMethod() ? "()" : ""));
        if (ignoreNull) {
            builder.endControlFlow();
        }
    }
    return builder.addCode("\n").addStatement("return contentValues").build();
}
Also used : MethodSpec(com.squareup.javapoet.MethodSpec) StorIOContentResolverColumnMeta(com.pushtorefresh.storio.contentresolver.annotations.processor.introspection.StorIOContentResolverColumnMeta) NotNull(org.jetbrains.annotations.NotNull)

Example 7 with StorIOContentResolverColumnMeta

use of com.pushtorefresh.storio.contentresolver.annotations.processor.introspection.StorIOContentResolverColumnMeta in project storio by pushtorefresh.

the class QueryGenerator method createWhere.

@NotNull
public static Map<String, String> createWhere(@NotNull final StorIOContentResolverTypeMeta storIOContentResolverTypeMeta, @NotNull final String varName) {
    final StringBuilder whereClause = new StringBuilder();
    final StringBuilder whereArgs = new StringBuilder();
    int i = 0;
    for (final StorIOContentResolverColumnMeta columnMeta : storIOContentResolverTypeMeta.columns.values()) {
        if (columnMeta.storIOColumn.key()) {
            if (i == 0) {
                whereClause.append(columnMeta.storIOColumn.name()).append(" = ?");
                whereArgs.append(varName).append(".").append(columnMeta.elementName);
            } else {
                whereClause.append(" AND ").append(columnMeta.storIOColumn.name()).append(" = ?");
                whereArgs.append(", ").append(varName).append(".").append(columnMeta.elementName);
            }
            if (columnMeta.isMethod()) {
                whereArgs.append("()");
            }
            i++;
        }
    }
    if (whereClause.length() == 0 || whereArgs.length() == 0) {
        return Collections.emptyMap();
    } else {
        final Map<String, String> result = new HashMap<String, String>(2);
        // example: "email = ? AND user_id = ?"
        result.put(WHERE_CLAUSE, whereClause.toString());
        // example: "object.email, object.userId"
        result.put(WHERE_ARGS, whereArgs.toString());
        return result;
    }
}
Also used : HashMap(java.util.HashMap) StorIOContentResolverColumnMeta(com.pushtorefresh.storio.contentresolver.annotations.processor.introspection.StorIOContentResolverColumnMeta) NotNull(org.jetbrains.annotations.NotNull)

Aggregations

StorIOContentResolverColumnMeta (com.pushtorefresh.storio.contentresolver.annotations.processor.introspection.StorIOContentResolverColumnMeta)7 NotNull (org.jetbrains.annotations.NotNull)5 ProcessingException (com.pushtorefresh.storio.common.annotations.processor.ProcessingException)3 JavaType (com.pushtorefresh.storio.common.annotations.processor.introspection.JavaType)3 MethodSpec (com.squareup.javapoet.MethodSpec)3 SkipNotAnnotatedClassWithAnnotatedParentException (com.pushtorefresh.storio.common.annotations.processor.SkipNotAnnotatedClassWithAnnotatedParentException)2 Common.getFromCursorString (com.pushtorefresh.storio.common.annotations.processor.generate.Common.getFromCursorString)2 StorIOContentResolverTypeMeta (com.pushtorefresh.storio.contentresolver.annotations.processor.introspection.StorIOContentResolverTypeMeta)2 HashMap (java.util.HashMap)2 TypeElement (javax.lang.model.element.TypeElement)2 StorIOContentResolverColumn (com.pushtorefresh.storio.contentresolver.annotations.StorIOContentResolverColumn)1 ParameterizedTypeName (com.squareup.javapoet.ParameterizedTypeName)1 TypeName (com.squareup.javapoet.TypeName)1 Map (java.util.Map)1 Element (javax.lang.model.element.Element)1 ExecutableElement (javax.lang.model.element.ExecutableElement)1