1 | |
package liquibase.sqlgenerator.core; |
2 | |
|
3 | |
import liquibase.database.Database; |
4 | |
import liquibase.database.core.DB2Database; |
5 | |
import liquibase.database.core.MSSQLDatabase; |
6 | |
import liquibase.database.core.MySQLDatabase; |
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.AddPrimaryKeyStatement; |
14 | |
import liquibase.util.StringUtils; |
15 | |
|
16 | 166 | public class AddPrimaryKeyGenerator extends AbstractSqlGenerator<AddPrimaryKeyStatement> { |
17 | |
|
18 | |
@Override |
19 | |
public boolean supports(AddPrimaryKeyStatement statement, Database database) { |
20 | 117 | return (!(database instanceof SQLiteDatabase)); |
21 | |
} |
22 | |
|
23 | |
@Override |
24 | |
public ValidationErrors validate(AddPrimaryKeyStatement addPrimaryKeyStatement, Database database, |
25 | |
SqlGeneratorChain sqlGeneratorChain) { |
26 | 29 | ValidationErrors validationErrors = new ValidationErrors(); |
27 | 29 | validationErrors.checkRequiredField("columnNames", addPrimaryKeyStatement.getColumnNames()); |
28 | 29 | validationErrors.checkRequiredField("tableName", addPrimaryKeyStatement.getTableName()); |
29 | 29 | return validationErrors; |
30 | |
} |
31 | |
|
32 | |
@Override |
33 | |
public Sql[] generateSql(AddPrimaryKeyStatement statement, Database database, SqlGeneratorChain sqlGeneratorChain) { |
34 | |
String sql; |
35 | 0 | if (statement.getConstraintName() == null || database instanceof MySQLDatabase |
36 | |
|| database instanceof SybaseASADatabase) { |
37 | 0 | sql = "ALTER TABLE " + database.escapeTableName(statement.getSchemaName(), statement.getTableName()) |
38 | |
+ " ADD PRIMARY KEY (" + database.escapeColumnNameList(statement.getColumnNames()) + ")"; |
39 | |
} else { |
40 | 0 | sql = "ALTER TABLE " + database.escapeTableName(statement.getSchemaName(), statement.getTableName()) |
41 | |
+ " ADD CONSTRAINT " + database.escapeConstraintName(statement.getConstraintName()) |
42 | |
+ " PRIMARY KEY (" + database.escapeColumnNameList(statement.getColumnNames()) + ")"; |
43 | |
} |
44 | |
|
45 | 0 | if (StringUtils.trimToNull(statement.getTablespace()) != null && database.supportsTablespaces()) { |
46 | 0 | if (database instanceof MSSQLDatabase) { |
47 | 0 | sql += " ON " + statement.getTablespace(); |
48 | 0 | } else if (database instanceof DB2Database || database instanceof SybaseASADatabase) { |
49 | |
; |
50 | |
} else { |
51 | 0 | sql += " USING INDEX TABLESPACE " + statement.getTablespace(); |
52 | |
} |
53 | |
} |
54 | |
|
55 | 0 | return new Sql[] { new UnparsedSql(sql) }; |
56 | |
} |
57 | |
} |