Пример #1
0
  public String getSqlStatement(Database database) throws StatementNotSupportedOnDatabaseException {
    if (!supportsDatabase(database)) {
      throw new StatementNotSupportedOnDatabaseException(this, database);
    }

    StringBuffer buffer = new StringBuffer();
    buffer.append("CREATE SEQUENCE ");
    buffer.append(database.escapeSequenceName(getSchemaName(), getSequenceName()));
    if (getStartValue() != null) {
      if (database instanceof FirebirdDatabase) {
        throw new StatementNotSupportedOnDatabaseException(
            "Firebird does not support creating sequences with startValue", this, database);
      } else {
        buffer.append(" START WITH ").append(getStartValue());
      }
    }
    if (getIncrementBy() != null) {
      if (database instanceof FirebirdDatabase) {
        throw new StatementNotSupportedOnDatabaseException(
            "Firebird does not support creating sequences with increments", this, database);
      } else {
        buffer.append(" INCREMENT BY ").append(getIncrementBy());
      }
    }
    if (getMinValue() != null) {
      if (database instanceof FirebirdDatabase || database instanceof HsqlDatabase) {
        throw new StatementNotSupportedOnDatabaseException(
            "Database does not support creating sequences with minValue", this, database);
      } else {
        buffer.append(" MINVALUE ").append(getMinValue());
      }
    }
    if (getMaxValue() != null) {
      if (database instanceof FirebirdDatabase || database instanceof HsqlDatabase) {
        throw new StatementNotSupportedOnDatabaseException(
            "Database does not support creating sequences with maxValue", this, database);
      } else {
        buffer.append(" MAXVALUE ").append(getMaxValue());
      }
    }

    if (getOrdered() != null) {
      if (database instanceof OracleDatabase
          || database instanceof DB2Database
          || database instanceof MaxDBDatabase) {
        if (getOrdered()) {
          buffer.append(" ORDER");
        }
      } else {
        throw new StatementNotSupportedOnDatabaseException(
            "Database does not support creating sequences with 'order'", this, database);
      }
    }

    return buffer.toString();
  }
  @Override
  public Sql[] generateSql(
      RenameSequenceStatement statement, Database database, SqlGeneratorChain sqlGeneratorChain) {
    String sql;

    if (database instanceof PostgresDatabase) {
      sql =
          "ALTER SEQUENCE "
              + database.escapeSequenceName(
                  statement.getCatalogName(),
                  statement.getSchemaName(),
                  statement.getOldSequenceName())
              + " RENAME TO "
              + database.escapeObjectName(statement.getNewSequenceName(), Sequence.class);
    } else if (database instanceof OracleDatabase) {
      sql =
          "RENAME "
              + database.escapeObjectName(statement.getOldSequenceName(), Sequence.class)
              + " TO "
              + database.escapeObjectName(statement.getNewSequenceName(), Sequence.class);
    } else if (database instanceof MSSQLDatabase) {
      sql =
          "SP_RENAME "
              + database.escapeObjectName(statement.getOldSequenceName(), Sequence.class)
              + " ,"
              + database.escapeObjectName(statement.getNewSequenceName(), Sequence.class);
    } else {
      sql =
          "ALTER SEQUENCE "
              + database.escapeSequenceName(
                  statement.getCatalogName(),
                  statement.getSchemaName(),
                  statement.getOldSequenceName())
              + " RENAME TO "
              + database.escapeObjectName(statement.getNewSequenceName(), Sequence.class);
    }

    return new Sql[] {
      new UnparsedSql(sql, getAffectedOldSequence(statement), getAffectedNewSequence(statement))
    };
  }