001: /*
002: * $Id: DropConstraintCommand.java,v 1.17 2005/12/20 18:32:28 ahimanikya Exp $
003: * =======================================================================
004: * Copyright (c) 2002-2006 Axion Development Team. All rights reserved.
005: *
006: * Redistribution and use in source and binary forms, with or without
007: * modification, are permitted provided that the following conditions
008: * are met:
009: *
010: * 1. Redistributions of source code must retain the above
011: * copyright notice, this list of conditions and the following
012: * disclaimer.
013: *
014: * 2. Redistributions in binary form must reproduce the above copyright
015: * notice, this list of conditions and the following disclaimer in
016: * the documentation and/or other materials provided with the
017: * distribution.
018: *
019: * 3. The names "Tigris", "Axion", nor the names of its contributors may
020: * not be used to endorse or promote products derived from this
021: * software without specific prior written permission.
022: *
023: * 4. Products derived from this software may not be called "Axion", nor
024: * may "Tigris" or "Axion" appear in their names without specific prior
025: * written permission.
026: *
027: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
028: * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
029: * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
030: * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
031: * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
032: * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
033: * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
034: * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
035: * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
036: * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
037: * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
038: * =======================================================================
039: */
040:
041: package org.axiondb.engine.commands;
042:
043: import java.util.Iterator;
044:
045: import org.axiondb.AxionException;
046: import org.axiondb.Constraint;
047: import org.axiondb.Database;
048: import org.axiondb.Table;
049: import org.axiondb.constraints.ForeignKeyConstraint;
050: import org.axiondb.constraints.UniqueConstraint;
051:
052: /**
053: * A <code>DROP CONSTRAINT</code> command.
054: *
055: * @version $Revision: 1.17 $ $Date: 2005/12/20 18:32:28 $
056: * @author Rodney Waldhoff
057: * @author Ahimanikya Satapathy
058: */
059: public class DropConstraintCommand extends ConstraintCommand {
060:
061: public DropConstraintCommand(String tableName,
062: String constraintName, boolean cascade) {
063: super (tableName);
064: _cascade = cascade;
065: setConstraintName(constraintName);
066: }
067:
068: public String getConstraintName() {
069: return _constraintName;
070: }
071:
072: public void setConstraintName(String name) {
073: _constraintName = name;
074: }
075:
076: public String toString() {
077: StringBuffer buf = new StringBuffer();
078: buf.append("ALTER TABLE ");
079: buf.append(getTableName());
080: buf.append(" DROP CONSTRAINT ");
081: buf.append(getConstraintName());
082: buf.append(_cascade ? " CASCADE " : "");
083: return buf.toString();
084: }
085:
086: protected void execute(Database db, Table table)
087: throws AxionException {
088: if (null == _constraintName) {
089: throw new AxionException(
090: "Constraint name must not be null.");
091: }
092:
093: Constraint constraint = table
094: .removeConstraint(getConstraintName());
095: if (constraint == null) {
096: throw new AxionException("Constraint " + _constraintName
097: + " not found.");
098: } else if (constraint instanceof ForeignKeyConstraint) {
099: handleForeignKeyConstraint(db, table, constraint);
100: } else if (constraint instanceof UniqueConstraint) {
101: handleUniqueConstraint(db, table, constraint);
102: }
103: }
104:
105: private void handleForeignKeyConstraint(Database db, Table table,
106: Constraint constraint) throws AxionException {
107: ForeignKeyConstraint fk = (ForeignKeyConstraint) constraint;
108: if (fk.getParentTableName().equals(fk.getChildTableName())) {
109: // do nothing
110: } else if (table.getName().equals(fk.getChildTableName())) {
111: Table parentTable = db.getTable(fk.getParentTableName());
112: parentTable.removeConstraint(getConstraintName());
113: } else {
114: // Add it back since parent table does not own ForeignKeyConstraint
115: table.addConstraint(constraint);
116: throw new AxionException("Constraint " + _constraintName
117: + " not found.");
118: }
119: }
120:
121: private void handleUniqueConstraint(Database db, Table table,
122: Constraint constraint) throws AxionException {
123: UniqueConstraint uc = (UniqueConstraint) constraint;
124: for (Iterator fks = uc.getFKs(); fks.hasNext();) {
125: String fkName = (String) fks.next();
126: ForeignKeyConstraint fk = (ForeignKeyConstraint) table
127: .getConstraint(fkName);
128: if (fk.getParentTableName().equals(fk.getChildTableName())) {
129: if (_cascade) {
130: table.removeConstraint(fkName);
131: } else {
132: throw new AxionException("FK: " + fkName
133: + " refering this PK, ca't drop constraint");
134: }
135: } else if (table.getName().equals(fk.getParentTableName())) {
136: if (_cascade) {
137: table.removeConstraint(fkName);
138: Table childTable = db.getTable(fk
139: .getChildTableName());
140: childTable.removeConstraint(fkName);
141: } else {
142: throw new AxionException("FK: " + fkName
143: + " refering this PK, ca't drop constraint");
144: }
145: }
146: }
147: }
148:
149: private boolean _cascade = false;
150: private String _constraintName;
151:
152: }
|