private CodeBlock buildCreateModelFromCursor() {
   CodeBlock.Builder builder = CodeBlock.builder();
   builder.addStatement(
       "$T model = new $T()", schema.getModelClassName(), schema.getModelClassName()); // FIXME
   builder.add(buildPopulateValuesIntoCursor());
   builder.addStatement("return model");
   return builder.build();
 }
  private CodeBlock buildPopulateValuesIntoCursor() {
    CodeBlock.Builder builder = CodeBlock.builder();

    List<ColumnDefinition> columns = schema.getColumns();
    for (int i = 0; i < columns.size(); i++) {
      ColumnDefinition c = columns.get(i);
      RelationDefinition r = c.getRelation();
      TypeName type = c.getUnboxType();
      if (r == null) {
        CodeBlock.Builder getCursorExpr = CodeBlock.builder();

        if (Types.needsTypeAdapter(type)) {
          getCursorExpr.add(
              "conn.getTypeAdapterRegistry().$L($T.$L.type, $L)",
              c.nullable ? "deserializeNullable" : "deserialize",
              schema.getSchemaClassName(),
              c.name,
              cursorGetter(c, i));
        } else {
          getCursorExpr.add("$L", cursorGetter(c, i));
        }

        if (c.setter != null) {
          builder.addStatement("model.$L($L)", c.setter.getSimpleName(), getCursorExpr.build());

        } else {
          builder.addStatement("model.$L = $L", c.name, getCursorExpr.build());
        }
      } else { // SingleRelation
        if (c.setter != null) {
          builder.addStatement(
              "model.$L(new $T<>(conn, OrmaDatabase.schema$T, cursor.getLong($L)))",
              c.setter.getSimpleName(),
              r.relationType,
              r.modelType,
              i);

        } else {
          builder.addStatement(
              "model.$L = new $T<>(conn, OrmaDatabase.schema$T, cursor.getLong($L))",
              c.name,
              r.relationType,
              r.modelType,
              i);
        }
      }
    }
    return builder.build();
  }
Exemple #3
0
  private CodeBlock versionSetInit() {
    CodeBlock.Builder init =
        CodeBlock.builder().add("versionSet = $T.builder()\n", VersionSet.class);

    schemaTypeInfo.acceptVersionSetInit(init);

    return init.addStatement("    .build()").build();
  }
  // http://developer.android.com/intl/ja/reference/android/database/sqlite/SQLiteStatement.html
  private CodeBlock buildBindArgs() {
    CodeBlock.Builder builder = CodeBlock.builder();

    List<ColumnDefinition> columns = schema.getColumnsWithoutAutoId();
    for (int i = 0; i < columns.size(); i++) {
      int n = i + 1; // bind index starts 1
      ColumnDefinition c = columns.get(i);
      TypeName type = c.getUnboxType();
      RelationDefinition r = c.getRelation();
      boolean nullable = !type.isPrimitive() && c.nullable;

      if (nullable) {
        builder.beginControlFlow("if (model.$L != null)", c.name);
      }

      if (type.equals(TypeName.BOOLEAN)) {
        builder.addStatement(
            "statement.bindLong($L, model.$L ? 1 : 0)", n, c.getColumnGetterExpr());
      } else if (Types.looksLikeIntegerType(type)) {
        builder.addStatement("statement.bindLong($L, model.$L)", n, c.getColumnGetterExpr());
      } else if (Types.looksLikeFloatType(type)) {
        builder.addStatement("statement.bindDouble($L, model.$L)", n, c.getColumnGetterExpr());
      } else if (type.equals(Types.ByteArray)) {
        builder.addStatement("statement.bindBlob($L, model.$L)", n, c.getColumnGetterExpr());
      } else if (type.equals(Types.String)) {
        builder.addStatement("statement.bindString($L, model.$L)", n, c.getColumnGetterExpr());
      } else if (r != null && r.relationType.equals(Types.SingleRelation)) {
        builder.addStatement(
            "statement.bindLong($L, model.$L.getId())", n, c.getColumnGetterExpr());
      } else {
        builder.addStatement(
            "statement.bindString($L, conn.getTypeAdapterRegistry().serialize($T.$L.type, model.$L))",
            n,
            schema.getSchemaClassName(),
            c.name,
            c.getColumnGetterExpr());
      }

      if (nullable) {
        builder.endControlFlow();
        builder.beginControlFlow("else");
        builder.addStatement("statement.bindNull($L)", n);
        builder.endControlFlow();
      }
    }

    return builder.build();
  }
  @Override
  public void onWriteDefinition(TypeSpec.Builder typeBuilder) {

    typeBuilder.addField(
        FieldSpec.builder(
                ClassName.get(String.class),
                AUTHORITY,
                Modifier.PRIVATE,
                Modifier.STATIC,
                Modifier.FINAL)
            .initializer("$S", authority)
            .build());

    int code = 0;
    for (TableEndpointDefinition endpointDefinition : endpointDefinitions) {
      for (ContentUriDefinition contentUriDefinition : endpointDefinition.contentUriDefinitions) {
        typeBuilder.addField(
            FieldSpec.builder(
                    TypeName.INT,
                    contentUriDefinition.name,
                    Modifier.PRIVATE,
                    Modifier.STATIC,
                    Modifier.FINAL)
                .initializer(String.valueOf(code))
                .build());
        code++;
      }
    }

    FieldSpec.Builder uriField =
        FieldSpec.builder(
            ClassNames.URI_MATCHER, URI_MATCHER, Modifier.PRIVATE, Modifier.STATIC, Modifier.FINAL);

    CodeBlock.Builder initializer =
        CodeBlock.builder()
            .addStatement("new $T($T.NO_MATCH)", ClassNames.URI_MATCHER, ClassNames.URI_MATCHER)
            .add("static {\n");

    for (TableEndpointDefinition endpointDefinition : endpointDefinitions) {
      for (ContentUriDefinition contentUriDefinition : endpointDefinition.contentUriDefinitions) {
        String path;
        if (contentUriDefinition.path != null) {
          path = "\"" + contentUriDefinition.path + "\"";
        } else {
          path =
              CodeBlock.builder()
                  .add(
                      "$L.$L.getPath()",
                      contentUriDefinition.elementClassName,
                      contentUriDefinition.name)
                  .build()
                  .toString();
        }
        initializer.addStatement(
            "$L.addURI($L, $L, $L)", URI_MATCHER, AUTHORITY, path, contentUriDefinition.name);
      }
    }
    initializer.add("}\n");
    typeBuilder.addField(uriField.initializer(initializer.build()).build());

    typeBuilder.addMethod(
        MethodSpec.methodBuilder("getDatabaseName")
            .addAnnotation(Override.class)
            .addModifiers(Modifier.PUBLIC, Modifier.FINAL)
            .addStatement("return $S", databaseNameString)
            .returns(ClassName.get(String.class))
            .build());

    MethodSpec.Builder getTypeBuilder =
        MethodSpec.methodBuilder("getType")
            .addAnnotation(Override.class)
            .addParameter(ClassNames.URI, "uri")
            .returns(ClassName.get(String.class))
            .addModifiers(Modifier.PUBLIC, Modifier.FINAL);

    CodeBlock.Builder getTypeCode =
        CodeBlock.builder()
            .addStatement("$T type = null", ClassName.get(String.class))
            .beginControlFlow("switch($L.match(uri))", URI_MATCHER);

    for (TableEndpointDefinition tableEndpointDefinition : endpointDefinitions) {
      for (ContentUriDefinition uriDefinition : tableEndpointDefinition.contentUriDefinitions) {
        getTypeCode
            .beginControlFlow("case $L:", uriDefinition.name)
            .addStatement("type = $S", uriDefinition.type)
            .addStatement("break")
            .endControlFlow();
      }
    }
    getTypeCode
        .beginControlFlow("default:")
        .addStatement(
            "throw new $T($S + $L)",
            ClassName.get(IllegalArgumentException.class),
            "Unknown URI",
            "uri")
        .endControlFlow();
    getTypeCode.endControlFlow();
    getTypeCode.addStatement("return type");

    getTypeBuilder.addCode(getTypeCode.build());
    typeBuilder.addMethod(getTypeBuilder.build());

    for (MethodDefinition method : methods) {
      MethodSpec methodSpec = method.getMethodSpec();
      if (methodSpec != null) {
        typeBuilder.addMethod(methodSpec);
      }
    }
  }