示例#1
0
  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();
  }
示例#2
0
  public List<FieldSpec> buildFieldSpecs() {
    List<FieldSpec> fieldSpecs = new ArrayList<>();

    List<FieldSpec> columns = new ArrayList<>();

    schema
        .getColumns()
        .forEach(
            columnDef -> {
              FieldSpec fieldSpec = buildColumnFieldSpec(columnDef);
              columns.add(fieldSpec);

              if (columnDef.primaryKey) {
                primaryKey = fieldSpec;
              }
            });

    if (primaryKey == null) {
      // Even if primary key is omitted, "_rowid_" is always available.
      // (WITHOUT ROWID is not supported by Orma)
      primaryKey = buildRowIdColumnFieldSpec();
      fieldSpecs.add(primaryKey);
    }

    fieldSpecs.addAll(columns);

    fieldSpecs.add(
        FieldSpec.builder(Types.String, TABLE_NAME)
            .addModifiers(publicStaticFinal)
            .initializer("$S", schema.tableName)
            .build());

    fieldSpecs.add(
        FieldSpec.builder(Types.ColumnList, COLUMNS)
            .addModifiers(publicStaticFinal)
            .initializer(buildColumnsInitializer(columns))
            .build());

    fieldSpecs.add(
        FieldSpec.builder(Types.StringArray, ESCAPED_COLUMN_NAMES)
            .addModifiers(publicStaticFinal)
            .initializer(buildEscapedColumnNamesInitializer())
            .build());

    return fieldSpecs;
  }
示例#3
0
  public CodeBlock buildEscapedColumnNamesInitializer() {
    CodeBlock.Builder builder = CodeBlock.builder();

    builder.add("{\n").indent();

    List<ColumnDefinition> columns = schema.getColumns();

    for (int i = 0; i < columns.size(); i++) {
      builder.add("$S", '"' + columns.get(i).columnName + '"');
      if ((i + 1) != columns.size()) {
        builder.add(",\n");
      } else {
        builder.add("\n");
      }
    }

    builder.unindent().add("}");

    return builder.build();
  }