Example #1
0
  @Override
  public final List<String> getInputSchemata() {
    if (inputSchemata == null) {
      inputSchemata = new ArrayList<String>();

      // [#1312] Allow for ommitting inputSchema configuration. Generate
      // All schemata instead
      if (configuredSchemata.size() == 1
          && StringUtils.isBlank(configuredSchemata.get(0).getInputSchema())) {
        try {
          for (SchemaDefinition schema : getSchemata0()) {
            inputSchemata.add(schema.getName());
          }
        } catch (SQLException e) {
          log.error("Could not load schemata", e);
        }
      } else {
        for (Schema schema : configuredSchemata) {
          /* [pro] xx

          xx xxxxxxx xxxxxx xxx xxxxxxxxxxxxxxxx xxxxxx xxxxxx
          xx xxxxx xxxxxxxxxx xxxxxxxxxxxxxxx x
              xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
          x
          xxxx
          xx [/pro] */
          {
            inputSchemata.add(schema.getInputSchema());
          }
        }
      }
    }

    return inputSchemata;
  }
Example #2
0
  @Override
  public final List<String> getInputSchemata() {
    if (inputSchemata == null) {
      inputSchemata = new ArrayList<String>();

      // [#1312] Allow for ommitting inputSchema configuration. Generate
      // All schemata instead
      if (configuredSchemata.size() == 1
          && StringUtils.isBlank(configuredSchemata.get(0).getInputSchema())) {
        try {
          for (SchemaDefinition schema : getSchemata0()) {
            inputSchemata.add(schema.getName());
          }
        } catch (SQLException e) {
          log.error("Could not load schemata", e);
        }
      } else {
        for (Schema schema : configuredSchemata) {

          // [#1418] Oracle has case-insensitive schema names.
          if (this instanceof OracleDatabase) {
            inputSchemata.add(schema.getInputSchema().toUpperCase());
          } else {
            inputSchemata.add(schema.getInputSchema());
          }
        }
      }
    }

    return inputSchemata;
  }
Example #3
0
 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();
 }
  /** @see DATASOLR-72 */
  @Test
  public void loadExistingSchemaShouldMapSchemaInformationCorrectly() {

    setUpJsonResponse(JSON_RESPONSE_DEFAULT_SCHEMA);
    SchemaDefinition schemaDef = writer.loadExistingSchema("collection1");

    Assert.assertEquals("example", schemaDef.getName());
    Assert.assertEquals("id", schemaDef.getUniqueKey());
  }
Example #5
0
  @Override
  public final SchemaDefinition getSchema(String inputName) {
    for (SchemaDefinition schema : getSchemata()) {
      if (schema.getName().equals(inputName)) {
        return schema;
      }
    }

    return null;
  }
Example #6
0
  @Override
  public TypeSpec buildTypeSpec() {
    TypeSpec.Builder classBuilder = TypeSpec.classBuilder(schema.getSchemaClassName().simpleName());
    classBuilder.addModifiers(Modifier.PUBLIC);
    classBuilder.addSuperinterface(Types.getSchema(schema.getModelClassName()));

    classBuilder.addFields(buildFieldSpecs());
    classBuilder.addMethods(buildMethodSpecs());

    return classBuilder.build();
  }
  protected void updateSchema(SchemaDefinition schemaDefinition) {

    SchemaDefinition existing = loadExistingSchema(schemaDefinition.getCollectionName());

    List<FieldDefinition> fieldsToBeCreated = new ArrayList<FieldDefinition>();
    for (FieldDefinition fieldDefinition : schemaDefinition.getFields()) {
      if (!existing.containsField(fieldDefinition.getName())) {
        fieldsToBeCreated.add(fieldDefinition);
      }
    }

    writeFieldDefinitions(fieldsToBeCreated, schemaDefinition.getCollectionName());
  }
Example #8
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();
  }
Example #9
0
  // 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();
  }
  public DefaultDomainDefinition(
      SchemaDefinition schema, String name, DataTypeDefinition baseType) {
    super(schema.getDatabase(), schema, name, "");
    this.baseType = baseType;

    this.checkClauses = new ArrayList<String>();
  }
  public DefaultEnumDefinition(
      SchemaDefinition schema, String name, String comment, boolean isSynthetic) {
    super(schema.getDatabase(), schema, name, comment);

    this.literals = new ArrayList<String>();
    this.isSynthetic = isSynthetic;
  }
  public void writeSchema(SchemaDefinition schemaDefinition) {

    if (isSchemaPresent(schemaDefinition.getCollectionName())) {
      updateSchema(schemaDefinition);
      return;
    }

    createSchema(schemaDefinition);
  }
Example #13
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;
  }
Example #14
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();
  }
Example #15
0
  public List<MethodSpec> buildMethodSpecs() {
    List<MethodSpec> methodSpecs = new ArrayList<>();

    List<AnnotationSpec> overrideAndNonNull =
        Arrays.asList(Specs.buildNonNullAnnotationSpec(), Specs.buildOverrideAnnotationSpec());

    List<AnnotationSpec> overrideAndNullable =
        Arrays.asList(Specs.buildNullableAnnotationSpec(), Specs.buildOverrideAnnotationSpec());

    methodSpecs.add(
        MethodSpec.methodBuilder("getModelClass")
            .addAnnotations(overrideAndNonNull)
            .addModifiers(Modifier.PUBLIC)
            .returns(
                ParameterizedTypeName.get(ClassName.get(Class.class), schema.getModelClassName()))
            .addStatement("return $T.class", schema.getModelClassName())
            .build());

    methodSpecs.add(
        MethodSpec.methodBuilder("getTableName")
            .addAnnotations(overrideAndNonNull)
            .addModifiers(Modifier.PUBLIC)
            .returns(Types.String)
            .addStatement("return $L", TABLE_NAME)
            .build());

    methodSpecs.add(
        MethodSpec.methodBuilder("getPrimaryKey")
            .addAnnotations(overrideAndNonNull)
            .addModifiers(Modifier.PUBLIC)
            .returns(Types.WildcardColumnDef)
            .addStatement("return $N", primaryKey)
            .build());

    methodSpecs.add(
        MethodSpec.methodBuilder("getColumns")
            .addAnnotations(overrideAndNonNull)
            .addModifiers(Modifier.PUBLIC)
            .returns(Types.ColumnList)
            .addStatement("return $L", COLUMNS)
            .build());

    methodSpecs.add(
        MethodSpec.methodBuilder("getEscapedColumnNames")
            .addAnnotations(overrideAndNonNull)
            .addModifiers(Modifier.PUBLIC)
            .returns(Types.StringArray)
            .addStatement("return $L", ESCAPED_COLUMN_NAMES)
            .build());

    methodSpecs.add(
        MethodSpec.methodBuilder("getCreateTableStatement")
            .addAnnotations(overrideAndNonNull)
            .addModifiers(Modifier.PUBLIC)
            .returns(Types.String)
            .addStatement("return $S", sql.buildCreateTableStatement(schema))
            .build());

    methodSpecs.add(
        MethodSpec.methodBuilder("getCreateIndexStatements")
            .addAnnotations(overrideAndNonNull)
            .addModifiers(Modifier.PUBLIC)
            .returns(Types.getList(Types.String))
            .addCode(sql.buildCreateIndexStatements(schema))
            .build());

    methodSpecs.add(
        MethodSpec.methodBuilder("getDropTableStatement")
            .addAnnotations(overrideAndNonNull)
            .addModifiers(Modifier.PUBLIC)
            .returns(Types.String)
            .addStatement("return $S", sql.buildDropTableStatement(schema))
            .build());

    methodSpecs.add(
        MethodSpec.methodBuilder("getInsertStatement")
            .addAnnotations(overrideAndNonNull)
            .addModifiers(Modifier.PUBLIC)
            .returns(Types.String)
            .addStatement("return $S", sql.buildInsertStatement(schema))
            .build());

    methodSpecs.add(
        MethodSpec.methodBuilder("bindArgs")
            .addModifiers(Modifier.PUBLIC)
            .returns(TypeName.VOID)
            .addParameter(
                ParameterSpec.builder(Types.OrmaConnection, "conn")
                    .addAnnotation(Specs.buildNonNullAnnotationSpec())
                    .build())
            .addParameter(
                ParameterSpec.builder(Types.SQLiteStatement, "statement")
                    .addAnnotation(Specs.buildNonNullAnnotationSpec())
                    .build())
            .addParameter(
                ParameterSpec.builder(schema.getModelClassName(), "model")
                    .addAnnotation(Specs.buildNonNullAnnotationSpec())
                    .build())
            .addCode(buildBindArgs())
            .build());

    methodSpecs.add(
        MethodSpec.methodBuilder("createModelFromCursor")
            .addAnnotations(overrideAndNonNull)
            .addModifiers(Modifier.PUBLIC)
            .returns(schema.getModelClassName())
            .addParameter(
                ParameterSpec.builder(Types.OrmaConnection, "conn")
                    .addAnnotation(Specs.buildNonNullAnnotationSpec())
                    .build())
            .addParameter(
                ParameterSpec.builder(Types.Cursor, "cursor")
                    .addAnnotation(Specs.buildNonNullAnnotationSpec())
                    .build())
            .addCode(buildCreateModelFromCursor())
            .build());

    return methodSpecs;
  }