コード例 #1
0
  public Sql[] generateSql(
      DropIndexStatement statement, Database database, SqlGeneratorChain sqlGeneratorChain) {
    List<String> associatedWith = StringUtils.splitAndTrim(statement.getAssociatedWith(), ",");
    if (associatedWith != null) {
      if (associatedWith.contains(Index.MARK_PRIMARY_KEY)
          || associatedWith.contains(Index.MARK_UNIQUE_CONSTRAINT)) {
        return new Sql[0];
      } else if (associatedWith.contains(Index.MARK_FOREIGN_KEY)) {
        if (!(database instanceof OracleDatabase || database instanceof MSSQLDatabase)) {
          return new Sql[0];
        }
      }
    }

    String schemaName = statement.getTableSchemaName();

    if (database instanceof MySQLDatabase) {
      return new Sql[] {
        new UnparsedSql(
            "DROP INDEX "
                + database.escapeIndexName(null, null, statement.getIndexName())
                + " ON "
                + database.escapeTableName(
                    statement.getTableCatalogName(), schemaName, statement.getTableName()))
      };
    } else if (database instanceof MSSQLDatabase) {
      return new Sql[] {
        new UnparsedSql(
            "DROP INDEX "
                + database.escapeTableName(null, schemaName, statement.getTableName())
                + "."
                + database.escapeIndexName(null, null, statement.getIndexName()))
      };
    } else if (database instanceof PostgresDatabase) {
      return new Sql[] {
        new UnparsedSql(
            "DROP INDEX "
                + database.escapeIndexName(
                    statement.getTableCatalogName(), schemaName, statement.getIndexName()))
      };
    }

    return new Sql[] {
      new UnparsedSql(
          "DROP INDEX "
              + database.escapeIndexName(
                  statement.getTableCatalogName(), schemaName, statement.getIndexName()))
    };
  }
コード例 #2
0
  @Override
  public Sql[] generateSql(
      CreateIndexStatement statement, Database database, SqlGeneratorChain sqlGeneratorChain) {

    // Default filter of index creation:
    // creation of all indexes with associations are switched off.
    List<String> associatedWith = StringUtils.splitAndTrim(statement.getAssociatedWith(), ",");
    if (associatedWith != null
        && (associatedWith.contains(Index.MARK_PRIMARY_KEY)
            || associatedWith.contains(Index.MARK_UNIQUE_CONSTRAINT)
            || associatedWith.contains(Index.MARK_FOREIGN_KEY))) {
      return new Sql[0];
    }

    StringBuilder buffer = new StringBuilder();

    buffer.append("CREATE ");
    if (statement.isUnique() != null && statement.isUnique()) {
      buffer.append("UNIQUE ");
    }
    buffer.append("INDEX ");

    if (statement.getIndexName() != null) {
      String indexSchema = statement.getTableSchemaName();
      buffer
          .append(database.escapeIndexName(null, indexSchema, statement.getIndexName()))
          .append(" ");
    }
    buffer.append("ON ");
    buffer
        .append(
            database.escapeTableName(
                statement.getTableCatalogName(),
                statement.getTableSchemaName(),
                statement.getTableName()))
        .append("(");
    Iterator<String> iterator = Arrays.asList(statement.getColumns()).iterator();
    while (iterator.hasNext()) {
      String column = iterator.next();
      buffer.append(
          database.escapeColumnName(
              statement.getTableCatalogName(),
              statement.getTableSchemaName(),
              statement.getTableName(),
              column));
      if (iterator.hasNext()) {
        buffer.append(", ");
      }
    }
    buffer.append(")");

    if (StringUtils.trimToNull(statement.getTablespace()) != null
        && database.supportsTablespaces()) {
      if (database instanceof MSSQLDatabase || database instanceof SybaseASADatabase) {
        buffer.append(" ON ").append(statement.getTablespace());
      } else if (database instanceof DB2Database || database instanceof InformixDatabase) {
        buffer.append(" IN ").append(statement.getTablespace());
      } else {
        buffer.append(" TABLESPACE ").append(statement.getTablespace());
      }
    }

    return new Sql[] {new UnparsedSql(buffer.toString())};
  }