001: package liquibase.database.sql;
002:
003: import liquibase.database.Database;
004: import liquibase.exception.StatementNotSupportedOnDatabaseException;
005:
006: public class AddForeignKeyConstraintStatement implements SqlStatement {
007:
008: private String baseTableSchemaName;
009: private String baseTableName;
010: private String baseColumnNames;
011:
012: private String referencedTableSchemaName;
013: private String referencedTableName;
014: private String referencedColumnNames;
015:
016: private String constraintName;
017:
018: private boolean deferrable;
019: private boolean initiallyDeferred;
020:
021: private boolean deleteCascade;
022:
023: public AddForeignKeyConstraintStatement(String constraintName,
024: String baseTableSchemaName, String baseTableName,
025: String baseColumnNames, String referencedTableSchemaName,
026: String referencedTableName, String referencedColumnNames) {
027: this .baseTableSchemaName = baseTableSchemaName;
028: this .baseTableName = baseTableName;
029: this .baseColumnNames = baseColumnNames;
030: this .referencedTableSchemaName = referencedTableSchemaName;
031: this .referencedTableName = referencedTableName;
032: this .referencedColumnNames = referencedColumnNames;
033: this .constraintName = constraintName;
034: }
035:
036: public String getBaseTableSchemaName() {
037: return baseTableSchemaName;
038: }
039:
040: public String getBaseTableName() {
041: return baseTableName;
042: }
043:
044: public String getBaseColumnNames() {
045: return baseColumnNames;
046: }
047:
048: public String getReferencedTableSchemaName() {
049: return referencedTableSchemaName;
050: }
051:
052: public String getReferencedTableName() {
053: return referencedTableName;
054: }
055:
056: public String getReferencedColumnNames() {
057: return referencedColumnNames;
058: }
059:
060: public String getConstraintName() {
061: return constraintName;
062: }
063:
064: public boolean isDeferrable() {
065: return deferrable;
066: }
067:
068: public AddForeignKeyConstraintStatement setDeferrable(
069: boolean deferrable) {
070: this .deferrable = deferrable;
071: return this ;
072: }
073:
074: public boolean isInitiallyDeferred() {
075: return initiallyDeferred;
076: }
077:
078: public AddForeignKeyConstraintStatement setInitiallyDeferred(
079: boolean initiallyDeferred) {
080: this .initiallyDeferred = initiallyDeferred;
081: return this ;
082: }
083:
084: public boolean isDeleteCascade() {
085: return deleteCascade;
086: }
087:
088: public AddForeignKeyConstraintStatement setDeleteCascade(
089: boolean deleteCascade) {
090: this .deleteCascade = deleteCascade;
091: return this ;
092: }
093:
094: public String getSqlStatement(Database database)
095: throws StatementNotSupportedOnDatabaseException {
096: String sql = "ALTER TABLE "
097: + database.escapeTableName(getBaseTableSchemaName(),
098: getBaseTableName())
099: + " ADD CONSTRAINT "
100: + getConstraintName()
101: + " FOREIGN KEY ("
102: + database.escapeColumnNameList(getBaseColumnNames())
103: + ") REFERENCES "
104: + database.escapeTableName(
105: getReferencedTableSchemaName(),
106: getReferencedTableName())
107: + "("
108: + database
109: .escapeColumnNameList(getReferencedColumnNames())
110: + ")";
111:
112: if (isDeleteCascade()) {
113: sql += " ON DELETE CASCADE";
114: }
115:
116: if (isDeferrable() || isInitiallyDeferred()) {
117: if (!database.supportsInitiallyDeferrableColumns()) {
118: throw new StatementNotSupportedOnDatabaseException(
119: "Database does not support deferrable foreign keys",
120: this , database);
121: }
122:
123: if (isDeferrable()) {
124: sql += " DEFERRABLE";
125: }
126:
127: if (isInitiallyDeferred()) {
128: sql += " INITIALLY DEFERRED";
129: }
130: }
131:
132: return sql;
133: }
134:
135: public String getEndDelimiter(Database database) {
136: return ";";
137: }
138:
139: public boolean supportsDatabase(Database database) {
140: return true;
141: }
142: }
|