001: /*
002: * $Id: DropTableCommand.java,v 1.17 2005/12/20 18:32:28 ahimanikya Exp $
003: * =======================================================================
004: * Copyright (c) 2002-2004 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: import java.util.List;
045:
046: import org.axiondb.AxionException;
047: import org.axiondb.Database;
048: import org.axiondb.Table;
049: import org.axiondb.constraints.ForeignKeyConstraint;
050: import org.axiondb.engine.tables.TableView;
051:
052: /**
053: * A <code>DROP TABLE</code> command. One can't drop SYSTEM_TABLE or VIEW using this
054: * command. To drop view use <code>DROP VIEW viewname</code>
055: *
056: * @version $Revision: 1.17 $ $Date: 2005/12/20 18:32:28 $
057: * @author Rodney Waldhoff
058: * @author Ahimanikya Satapathy
059: */
060: public class DropTableCommand extends DropCommand {
061:
062: public DropTableCommand(String tableName, boolean exists,
063: boolean cascade) {
064: setObjectName(tableName);
065: setIfExists(exists);
066: setCascade(cascade);
067: }
068:
069: public boolean execute(Database db) throws AxionException {
070: assertNotReadOnly(db);
071: if (!isIfExists() || db.hasTable(getObjectName())) {
072: Table t = db.getTable(getObjectName());
073: if (t != null && !t.getType().equals(TableView.VIEW)
074: && !t.getType().equals(Table.SYSTEM_TABLE_TYPE)) {
075: checkConstraint(db, t);
076: List depedentViews = db
077: .getDependentViews(getObjectName());
078: dropDepedentViews(db, depedentViews);
079: db.dropTable(getObjectName());
080: } else {
081: throw new AxionException("No table " + getObjectName()
082: + " found");
083: }
084: }
085: return false;
086: }
087:
088: private void dropDepedentViews(Database db, List depedentViews)
089: throws AxionException {
090: if (depedentViews.size() > 0) {
091: if (isCascade()) {
092: db.dropDependentViews(depedentViews);
093: } else {
094: throw new AxionException("Can't drop table: "
095: + getObjectName()
096: + " has reference in another View...");
097: }
098: }
099: }
100:
101: private void checkConstraint(Database db, Table table)
102: throws AxionException {
103: for (Iterator iter = table.getConstraints(); iter.hasNext();) {
104: Object constraint = iter.next();
105: if (constraint instanceof ForeignKeyConstraint) {
106: ForeignKeyConstraint fk = (ForeignKeyConstraint) constraint;
107: if (fk.getParentTableName().equals(
108: fk.getChildTableName())) {
109: // do nothing
110: } else if (table.getName().equals(
111: fk.getChildTableName())) {
112: Table parentTable = db.getTable(fk
113: .getParentTableName());
114: parentTable.removeConstraint(fk.getName());
115: } else if (isCascade()
116: && table.getName().equals(
117: fk.getParentTableName())) {
118: Table childTable = db.getTable(fk
119: .getChildTableName());
120: childTable.removeConstraint(fk.getName());
121: } else { // someone else refering this table
122: throw new AxionException(
123: "Child table exist, can't drop");
124: }
125: }
126: }
127: }
128:
129: }
|