View Javadoc

1   package liquibase.sqlgenerator.core;
2   
3   import liquibase.database.Database;
4   import liquibase.database.core.MaxDBDatabase;
5   import liquibase.database.core.MySQLDatabase;
6   import liquibase.database.core.OracleDatabase;
7   import liquibase.database.core.SQLiteDatabase;
8   import liquibase.database.core.SybaseASADatabase;
9   import liquibase.exception.ValidationErrors;
10  import liquibase.sql.Sql;
11  import liquibase.sql.UnparsedSql;
12  import liquibase.sqlgenerator.SqlGeneratorChain;
13  import liquibase.statement.core.DropUniqueConstraintStatement;
14  
15  public class DropUniqueConstraintGenerator extends AbstractSqlGenerator<DropUniqueConstraintStatement> {
16  
17      @Override
18      public boolean supports(DropUniqueConstraintStatement statement, Database database) {
19          return !(database instanceof SQLiteDatabase);
20      }
21  
22      @Override
23      public ValidationErrors validate(DropUniqueConstraintStatement dropUniqueConstraintStatement, Database database,
24              SqlGeneratorChain sqlGeneratorChain) {
25          ValidationErrors validationErrors = new ValidationErrors();
26          validationErrors.checkRequiredField("tableName", dropUniqueConstraintStatement.getTableName());
27          return validationErrors;
28      }
29  
30      @Override
31      public Sql[] generateSql(DropUniqueConstraintStatement statement, Database database,
32              SqlGeneratorChain sqlGeneratorChain) {
33          String sql;
34          if (database instanceof MySQLDatabase) {
35              sql = "ALTER TABLE " + database.escapeTableName(statement.getSchemaName(), statement.getTableName())
36                      + " DROP KEY " + database.escapeConstraintName(statement.getConstraintName());
37          } else if (database instanceof MaxDBDatabase) {
38              sql = "DROP INDEX " + database.escapeConstraintName(statement.getConstraintName()) + " ON "
39                      + database.escapeTableName(statement.getSchemaName(), statement.getTableName());
40          } else if (database instanceof OracleDatabase) {
41              sql = "ALTER TABLE " + database.escapeTableName(statement.getSchemaName(), statement.getTableName())
42                      + " DROP CONSTRAINT " + database.escapeConstraintName(statement.getConstraintName())
43                      + " DROP INDEX";
44          } else if (database instanceof SybaseASADatabase) {
45              sql = "ALTER TABLE " + database.escapeTableName(statement.getSchemaName(), statement.getTableName())
46                      + " DROP UNIQUE (" + statement.getUniqueColumns() + ")";
47          } else {
48              sql = "ALTER TABLE " + database.escapeTableName(statement.getSchemaName(), statement.getTableName())
49                      + " DROP CONSTRAINT " + database.escapeConstraintName(statement.getConstraintName());
50          }
51  
52          return new Sql[] { new UnparsedSql(sql) };
53      }
54  }