Search in sources :

Example 6 with StorIOSQLiteColumnMeta

use of com.pushtorefresh.storio.sqlite.annotations.processor.introspection.StorIOSQLiteColumnMeta in project storio by pushtorefresh.

the class GetResolverGenerator method createMapFromCursorMethodSpec.

@NotNull
private MethodSpec createMapFromCursorMethodSpec(@NotNull StorIOSQLiteTypeMeta storIOSQLiteTypeMeta, @NotNull ClassName storIOSQLiteTypeClassName) {
    final MethodSpec.Builder builder = MethodSpec.methodBuilder("mapFromCursor").addJavadoc("{@inheritDoc}\n").addAnnotation(Override.class).addAnnotation(ANDROID_NON_NULL_ANNOTATION_CLASS_NAME).addModifiers(PUBLIC).returns(storIOSQLiteTypeClassName).addParameter(ParameterSpec.builder(ClassName.get("android.database", "Cursor"), "cursor").addAnnotation(ANDROID_NON_NULL_ANNOTATION_CLASS_NAME).build()).addStatement("$T object = new $T()", storIOSQLiteTypeClassName, storIOSQLiteTypeClassName).addCode("\n");
    for (final StorIOSQLiteColumnMeta columnMeta : storIOSQLiteTypeMeta.columns.values()) {
        final String columnIndex = "cursor.getColumnIndex(\"" + columnMeta.storIOColumn.name() + "\")";
        final JavaType javaType = columnMeta.javaType;
        final String getFromCursor = getFromCursorString(columnMeta, javaType, columnIndex);
        final boolean isBoxed = javaType.isBoxedType();
        if (isBoxed) {
            // otherwise -> if primitive and value from cursor null -> fail early
            builder.beginControlFlow("if (!cursor.isNull($L))", columnIndex);
        }
        builder.addStatement("object.$L = cursor.$L", columnMeta.elementName, getFromCursor);
        if (isBoxed) {
            builder.endControlFlow();
        }
    }
    return builder.addCode("\n").addStatement("return object").build();
}
Also used : StorIOSQLiteColumnMeta(com.pushtorefresh.storio.sqlite.annotations.processor.introspection.StorIOSQLiteColumnMeta) JavaType(com.pushtorefresh.storio.common.annotations.processor.introspection.JavaType) MethodSpec(com.squareup.javapoet.MethodSpec) Common.getFromCursorString(com.pushtorefresh.storio.common.annotations.processor.generate.Common.getFromCursorString) NotNull(org.jetbrains.annotations.NotNull)

Example 7 with StorIOSQLiteColumnMeta

use of com.pushtorefresh.storio.sqlite.annotations.processor.introspection.StorIOSQLiteColumnMeta in project storio by pushtorefresh.

the class QueryGenerator method createWhere.

@NotNull
public static Map<String, String> createWhere(@NotNull StorIOSQLiteTypeMeta storIOSQLiteTypeMeta, @NotNull String varName) {
    final StringBuilder whereClause = new StringBuilder();
    final StringBuilder whereArgs = new StringBuilder();
    int i = 0;
    for (final StorIOSQLiteColumnMeta columnMeta : storIOSQLiteTypeMeta.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 : StorIOSQLiteColumnMeta(com.pushtorefresh.storio.sqlite.annotations.processor.introspection.StorIOSQLiteColumnMeta) HashMap(java.util.HashMap) NotNull(org.jetbrains.annotations.NotNull)

Aggregations

StorIOSQLiteColumnMeta (com.pushtorefresh.storio.sqlite.annotations.processor.introspection.StorIOSQLiteColumnMeta)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 StorIOSQLiteTypeMeta (com.pushtorefresh.storio.sqlite.annotations.processor.introspection.StorIOSQLiteTypeMeta)2 TypeElement (javax.lang.model.element.TypeElement)2 StorIOSQLiteColumn (com.pushtorefresh.storio.sqlite.annotations.StorIOSQLiteColumn)1 ParameterizedTypeName (com.squareup.javapoet.ParameterizedTypeName)1 TypeName (com.squareup.javapoet.TypeName)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1 Element (javax.lang.model.element.Element)1 ExecutableElement (javax.lang.model.element.ExecutableElement)1