1 | |
package liquibase.sqlgenerator.core; |
2 | |
|
3 | |
import liquibase.database.Database; |
4 | |
import liquibase.database.core.DB2Database; |
5 | |
import liquibase.database.core.InformixDatabase; |
6 | |
import liquibase.database.core.MSSQLDatabase; |
7 | |
import liquibase.database.core.OracleDatabase; |
8 | |
import liquibase.database.core.SQLiteDatabase; |
9 | |
import liquibase.database.core.SybaseASADatabase; |
10 | |
import liquibase.database.core.SybaseDatabase; |
11 | |
import liquibase.exception.ValidationErrors; |
12 | |
import liquibase.sql.Sql; |
13 | |
import liquibase.sql.UnparsedSql; |
14 | |
import liquibase.sqlgenerator.SqlGeneratorChain; |
15 | |
import liquibase.statement.core.AddUniqueConstraintStatement; |
16 | |
import liquibase.util.StringUtils; |
17 | |
|
18 | 145 | public class AddUniqueConstraintGenerator extends AbstractSqlGenerator<AddUniqueConstraintStatement> { |
19 | |
|
20 | |
@Override |
21 | |
public boolean supports(AddUniqueConstraintStatement statement, Database database) { |
22 | 76 | return !(database instanceof SQLiteDatabase) && !(database instanceof MSSQLDatabase) |
23 | |
&& !(database instanceof SybaseDatabase) && !(database instanceof SybaseASADatabase) |
24 | |
&& !(database instanceof InformixDatabase); |
25 | |
} |
26 | |
|
27 | |
@Override |
28 | |
public ValidationErrors validate(AddUniqueConstraintStatement addUniqueConstraintStatement, Database database, |
29 | |
SqlGeneratorChain sqlGeneratorChain) { |
30 | 30 | ValidationErrors validationErrors = new ValidationErrors(); |
31 | 30 | validationErrors.checkRequiredField("columnNames", addUniqueConstraintStatement.getColumnNames()); |
32 | 30 | validationErrors.checkRequiredField("tableName", addUniqueConstraintStatement.getTableName()); |
33 | 30 | return validationErrors; |
34 | |
} |
35 | |
|
36 | |
@Override |
37 | |
public Sql[] generateSql(AddUniqueConstraintStatement statement, Database database, |
38 | |
SqlGeneratorChain sqlGeneratorChain) { |
39 | |
|
40 | 0 | String sql = null; |
41 | 0 | if (statement.getConstraintName() == null) { |
42 | 0 | sql = String.format("ALTER TABLE %s ADD UNIQUE (%s)", |
43 | |
database.escapeTableName(statement.getSchemaName(), statement.getTableName()), |
44 | |
database.escapeColumnNameList(statement.getColumnNames())); |
45 | |
} else { |
46 | 0 | sql = String.format("ALTER TABLE %s ADD CONSTRAINT %s UNIQUE (%s)", |
47 | |
database.escapeTableName(statement.getSchemaName(), statement.getTableName()), |
48 | |
database.escapeConstraintName(statement.getConstraintName()), |
49 | |
database.escapeColumnNameList(statement.getColumnNames())); |
50 | |
} |
51 | 0 | if (database instanceof OracleDatabase) { |
52 | 0 | if (statement.isDeferrable() || statement.isInitiallyDeferred()) { |
53 | 0 | if (statement.isDeferrable()) { |
54 | 0 | sql += " DEFERRABLE"; |
55 | |
} |
56 | |
|
57 | 0 | if (statement.isInitiallyDeferred()) { |
58 | 0 | sql += " INITIALLY DEFERRED"; |
59 | |
} |
60 | |
} |
61 | 0 | if (statement.isDisabled()) { |
62 | 0 | sql += " DISABLE"; |
63 | |
} |
64 | |
} |
65 | |
|
66 | 0 | if (StringUtils.trimToNull(statement.getTablespace()) != null && database.supportsTablespaces()) { |
67 | 0 | if (database instanceof MSSQLDatabase) { |
68 | 0 | sql += " ON " + statement.getTablespace(); |
69 | 0 | } else if (database instanceof DB2Database || database instanceof SybaseASADatabase |
70 | |
|| database instanceof InformixDatabase) { |
71 | |
; |
72 | |
} else { |
73 | 0 | sql += " USING INDEX TABLESPACE " + statement.getTablespace(); |
74 | |
} |
75 | |
} |
76 | |
|
77 | 0 | return new Sql[] { new UnparsedSql(sql) }; |
78 | |
|
79 | |
} |
80 | |
} |