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();
}
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;
}
}
Aggregations