Ejemplo n.º 1
0
  protected String generateSingleColumnSQL(AddColumnStatement statement, Database database) {
    DatabaseDataType columnType =
        DataTypeFactory.getInstance()
            .fromDescription(
                statement.getColumnType()
                    + (statement.isAutoIncrement() ? "{autoIncrement:true}" : ""),
                database)
            .toDatabaseDataType(database);

    String alterTable =
        " ADD "
            + database.escapeColumnName(
                statement.getCatalogName(),
                statement.getSchemaName(),
                statement.getTableName(),
                statement.getColumnName())
            + " "
            + columnType;

    if (statement.isAutoIncrement() && database.supportsAutoIncrement()) {
      AutoIncrementConstraint autoIncrementConstraint = statement.getAutoIncrementConstraint();
      alterTable +=
          " "
              + database.getAutoIncrementClause(
                  autoIncrementConstraint.getStartWith(), autoIncrementConstraint.getIncrementBy());
    }

    alterTable += getDefaultClause(statement, database);

    if (!statement.isNullable()) {
      alterTable += " NOT NULL";
    } else {
      if (database instanceof SybaseDatabase
          || database instanceof SybaseASADatabase
          || database instanceof MySQLDatabase
          || (database instanceof MSSQLDatabase
              && columnType.toString().equalsIgnoreCase("timestamp"))) {
        alterTable += " NULL";
      }
    }

    if (statement.isPrimaryKey()) {
      alterTable += " PRIMARY KEY";
    }

    if (database instanceof MySQLDatabase && statement.getRemarks() != null) {
      alterTable += " COMMENT '" + statement.getRemarks() + "' ";
    }

    if (statement.getAddAfterColumn() != null && !statement.getAddAfterColumn().isEmpty()) {
      alterTable += " AFTER `" + statement.getAddAfterColumn() + "` ";
    }

    return alterTable;
  }
Ejemplo n.º 2
0
  private ValidationErrors validateSingleColumn(AddColumnStatement statement, Database database) {
    ValidationErrors validationErrors = new ValidationErrors();

    validationErrors.checkRequiredField("columnName", statement.getColumnName());
    validationErrors.checkRequiredField("columnType", statement.getColumnType());
    validationErrors.checkRequiredField("tableName", statement.getTableName());

    if (statement.isPrimaryKey()
        && (database instanceof H2Database
            || database instanceof DB2Database
            || database instanceof DerbyDatabase
            || database instanceof SQLiteDatabase)) {
      validationErrors.addError("Cannot add a primary key column");
    }

    // TODO HsqlDatabase autoincrement on non primary key? other databases?
    if (database instanceof MySQLDatabase
        && statement.isAutoIncrement()
        && !statement.isPrimaryKey()) {
      validationErrors.addError("Cannot add a non-primary key identity column");
    }

    // TODO is this feature valid for other databases?
    if ((statement.getAddAfterColumn() != null) && !(database instanceof MySQLDatabase)) {
      validationErrors.addError("Cannot add column on specific position");
    }
    if ((statement.getAddBeforeColumn() != null)
        && !((database instanceof H2Database) || (database instanceof HsqlDatabase))) {
      validationErrors.addError("Cannot add column on specific position");
    }
    if ((statement.getAddAtPosition() != null) && !(database instanceof FirebirdDatabase)) {
      validationErrors.addError("Cannot add column on specific position");
    }

    return validationErrors;
  }