1 | |
package liquibase.sqlgenerator.core; |
2 | |
|
3 | |
import liquibase.database.Database; |
4 | |
import liquibase.database.core.InformixDatabase; |
5 | |
import liquibase.database.core.MSSQLDatabase; |
6 | |
import liquibase.database.core.SQLiteDatabase; |
7 | |
import liquibase.database.core.OracleDatabase; |
8 | |
import liquibase.exception.ValidationErrors; |
9 | |
import liquibase.sql.Sql; |
10 | |
import liquibase.sql.UnparsedSql; |
11 | |
import liquibase.sqlgenerator.SqlGenerator; |
12 | |
import liquibase.sqlgenerator.SqlGeneratorChain; |
13 | |
import liquibase.statement.core.AddForeignKeyConstraintStatement; |
14 | |
|
15 | 156 | public class AddForeignKeyConstraintGenerator extends AbstractSqlGenerator<AddForeignKeyConstraintStatement> { |
16 | |
|
17 | |
@Override |
18 | |
@SuppressWarnings({ "SimplifiableIfStatement" }) |
19 | |
public boolean supports(AddForeignKeyConstraintStatement statement, Database database) { |
20 | 117 | if (statement.getReferencesUniqueColumn() && !(database instanceof OracleDatabase)) { |
21 | 0 | return false; |
22 | |
} |
23 | 117 | return (!(database instanceof SQLiteDatabase)); |
24 | |
} |
25 | |
|
26 | |
public ValidationErrors validate(AddForeignKeyConstraintStatement addForeignKeyConstraintStatement, |
27 | |
Database database, SqlGeneratorChain sqlGeneratorChain) { |
28 | 29 | ValidationErrors validationErrors = new ValidationErrors(); |
29 | |
|
30 | 29 | if ((addForeignKeyConstraintStatement.isInitiallyDeferred() || addForeignKeyConstraintStatement.isDeferrable()) |
31 | |
&& !database.supportsInitiallyDeferrableColumns()) { |
32 | 0 | validationErrors.checkDisallowedField("initiallyDeferred", |
33 | |
addForeignKeyConstraintStatement.isInitiallyDeferred(), database); |
34 | 0 | validationErrors.checkDisallowedField("deferrable", addForeignKeyConstraintStatement.isDeferrable(), |
35 | |
database); |
36 | |
} |
37 | |
|
38 | 29 | validationErrors.checkRequiredField("baseColumnNames", addForeignKeyConstraintStatement.getBaseColumnNames()); |
39 | 29 | validationErrors.checkRequiredField("baseTableNames", addForeignKeyConstraintStatement.getBaseTableName()); |
40 | 29 | validationErrors.checkRequiredField("referencedColumnNames", |
41 | |
addForeignKeyConstraintStatement.getReferencedColumnNames()); |
42 | 29 | validationErrors.checkRequiredField("referencedTableName", |
43 | |
addForeignKeyConstraintStatement.getReferencedTableName()); |
44 | |
|
45 | 29 | return validationErrors; |
46 | |
} |
47 | |
|
48 | |
public Sql[] generateSql(AddForeignKeyConstraintStatement statement, Database database, |
49 | |
SqlGeneratorChain sqlGeneratorChain) { |
50 | 0 | StringBuilder sb = new StringBuilder(); |
51 | 0 | sb.append("ALTER TABLE ") |
52 | |
.append(database.escapeTableName(statement.getBaseTableSchemaName(), statement.getBaseTableName())) |
53 | |
.append(" ADD CONSTRAINT "); |
54 | 0 | if (!(database instanceof InformixDatabase)) { |
55 | 0 | sb.append(database.escapeConstraintName(statement.getConstraintName())); |
56 | |
} |
57 | 0 | sb.append(" FOREIGN KEY (") |
58 | |
.append(database.escapeColumnNameList(statement.getBaseColumnNames())) |
59 | |
.append(") REFERENCES ") |
60 | |
.append(database.escapeTableName(statement.getReferencedTableSchemaName(), |
61 | |
statement.getReferencedTableName())).append(" (") |
62 | |
.append(database.escapeColumnNameList(statement.getReferencedColumnNames())).append(")"); |
63 | |
|
64 | 0 | if (statement.getOnUpdate() != null) { |
65 | 0 | if ((database instanceof OracleDatabase) && statement.getOnUpdate().equalsIgnoreCase("RESTRICT")) { |
66 | |
|
67 | 0 | } else if (database instanceof InformixDatabase) { |
68 | |
|
69 | |
} else { |
70 | 0 | sb.append(" ON UPDATE ").append(statement.getOnUpdate()); |
71 | |
} |
72 | |
} |
73 | |
|
74 | 0 | if (statement.getOnDelete() != null) { |
75 | 0 | if ((database instanceof OracleDatabase) |
76 | |
&& (statement.getOnDelete().equalsIgnoreCase("RESTRICT") || statement.getOnDelete() |
77 | |
.equalsIgnoreCase("NO ACTION"))) { |
78 | |
|
79 | 0 | } else if ((database instanceof MSSQLDatabase) && statement.getOnDelete().equalsIgnoreCase("RESTRICT")) { |
80 | |
|
81 | 0 | } else if (database instanceof InformixDatabase && !(statement.getOnDelete().equalsIgnoreCase("CASCADE"))) { |
82 | |
|
83 | |
|
84 | |
} else { |
85 | 0 | sb.append(" ON DELETE ").append(statement.getOnDelete()); |
86 | |
} |
87 | |
} |
88 | |
|
89 | 0 | if (statement.isDeferrable() || statement.isInitiallyDeferred()) { |
90 | 0 | if (statement.isDeferrable()) { |
91 | 0 | sb.append(" DEFERRABLE"); |
92 | |
} |
93 | |
|
94 | 0 | if (statement.isInitiallyDeferred()) { |
95 | 0 | sb.append(" INITIALLY DEFERRED"); |
96 | |
} |
97 | |
} |
98 | |
|
99 | 0 | if (database instanceof InformixDatabase) { |
100 | 0 | sb.append(" CONSTRAINT "); |
101 | 0 | sb.append(database.escapeConstraintName(statement.getConstraintName())); |
102 | |
} |
103 | |
|
104 | 0 | return new Sql[] { new UnparsedSql(sb.toString()) }; |
105 | |
} |
106 | |
} |