001: package com.quadcap.sql;
002:
003: /* Copyright 1999 - 2003 Quadcap Software. All rights reserved.
004: *
005: * This software is distributed under the Quadcap Free Software License.
006: * This software may be used or modified for any purpose, personal or
007: * commercial. Open Source redistributions are permitted. Commercial
008: * redistribution of larger works derived from, or works which bundle
009: * this software requires a "Commercial Redistribution License"; see
010: * http://www.quadcap.com/purchase.
011: *
012: * Redistributions qualify as "Open Source" under one of the following terms:
013: *
014: * Redistributions are made at no charge beyond the reasonable cost of
015: * materials and delivery.
016: *
017: * Redistributions are accompanied by a copy of the Source Code or by an
018: * irrevocable offer to provide a copy of the Source Code for up to three
019: * years at the cost of materials and delivery. Such redistributions
020: * must allow further use, modification, and redistribution of the Source
021: * Code under substantially the same terms as this license.
022: *
023: * Redistributions of source code must retain the copyright notices as they
024: * appear in each source code file, these license terms, and the
025: * disclaimer/limitation of liability set forth as paragraph 6 below.
026: *
027: * Redistributions in binary form must reproduce this Copyright Notice,
028: * these license terms, and the disclaimer/limitation of liability set
029: * forth as paragraph 6 below, in the documentation and/or other materials
030: * provided with the distribution.
031: *
032: * The Software is provided on an "AS IS" basis. No warranty is
033: * provided that the Software is free of defects, or fit for a
034: * particular purpose.
035: *
036: * Limitation of Liability. Quadcap Software shall not be liable
037: * for any damages suffered by the Licensee or any third party resulting
038: * from use of the Software.
039: */
040:
041: import java.io.IOException;
042:
043: import java.util.ArrayList;
044: import java.util.Enumeration;
045: import java.util.HashMap;
046: import java.util.Iterator;
047: import java.util.Map;
048:
049: import java.sql.SQLException;
050:
051: import com.quadcap.sql.file.BlockFile;
052: import com.quadcap.sql.file.ByteUtil;
053:
054: import com.quadcap.sql.index.Btree;
055: import com.quadcap.sql.index.BCursor;
056:
057: import com.quadcap.util.Debug;
058:
059: /**
060: * Implementation of the SQL <b>DROP TABLE</b> statement.
061: *
062: * @author Stan Bailes
063: */
064:
065: public class StmtDropTable implements Stmt {
066: Map deleted;
067: String tableName;
068: Relation table;
069: boolean view;
070: boolean restrict;
071:
072: public StmtDropTable() {
073: }
074:
075: public StmtDropTable(String name, boolean view, boolean restrict) {
076: this .tableName = name;
077: this .view = view;
078: this .restrict = restrict;
079: }
080:
081: public void execute(Session session) throws IOException,
082: SQLException {
083: session.getTableWriteLock("#Schema");
084: session.getTableWriteLock(tableName);
085: Database db = session.getDatabase();
086: table = db.getRelation(tableName);
087: if (table == null) {
088: throw new SQLException("no such " + getType() + ": "
089: + tableName, "42000");
090: }
091:
092: Enumeration views = db.getViews(tableName);
093: if (restrict) {
094: if (views.hasMoreElements()) {
095: throw new SQLException("Can't drop " + getType()
096: + " with RESTRICT because of"
097: + " referencing view: " + views.nextElement(),
098: "23000");
099: }
100: if (table instanceof Table) {
101: Table t = (Table) table;
102: int num = t.getNumConstraints();
103: for (int i = 0; i < num; i++) {
104: Constraint c = t.getConstraint(i);
105: if (c instanceof ExportedKeyConstraint) {
106: ExportedKeyConstraint ex = (ExportedKeyConstraint) c;
107: if (!ex.getFTableName().equals(tableName)) {
108: throw new SQLException("Can't drop "
109: + getType()
110: + " with RESTRICT because of "
111: + " referencing constraint: "
112: + ex.getName() + ", table "
113: + ex.getFTableName(), "23000");
114: }
115: }
116: }
117: }
118: }
119:
120: if (!restrict) {
121: if (deleted == null)
122: deleted = new HashMap();
123: while (views.hasMoreElements()) {
124: String view1 = views.nextElement().toString();
125: if (!deleted.containsKey(view1)) {
126: deleted.put(view1, "");
127: StmtDropTable t = new StmtDropTable(view1, true,
128: false);
129: t.deleted = deleted;
130: t.execute(session);
131: }
132: }
133: }
134:
135: // ---- Now, we're ready to rock.
136: if (table instanceof Table) {
137: // ---- First, make a copy of all the constraints to provide stable
138: // ---- enumeration.
139: IndexConstraint ic = null;
140: ArrayList list = new ArrayList();
141: Table t = (Table) table;
142: ic = t.getAnyIndex(session);
143: int num = t.getNumConstraints();
144: Constraint[] cv = new Constraint[num];
145: for (int i = 0; i < num; i++) {
146: cv[i] = t.getConstraint(i);
147: }
148: for (int i = 0; i < num; i++) {
149: Constraint c = cv[i];
150: if (c instanceof ForeignKeyConstraint) {
151: ForeignKeyConstraint fc = (ForeignKeyConstraint) c;
152: if (!fc.isSelfReferencing(db)) {
153: DeleteConstraint.deleteForeign(session, fc);
154: }
155: }
156: if (c != ic) {
157: session.doStep(new DeleteConstraint(session, t, c));
158: }
159: }
160: // ---- use the index to drop the table rows
161: deleteRows(session, t, ic);
162:
163: // ---- Remove the index constraint
164: session.doStep(new DeleteConstraint(session, t, ic));
165: }
166:
167: // ---- DROP the table
168: session.doStep(new DropTable(session, table));
169: }
170:
171: final void deleteRows(Session session, Table t, IndexConstraint c)
172: throws IOException, SQLException {
173: Database db = session.getDatabase();
174: BlockFile file = db.getFile();
175: Btree index = c.getIndex(db);
176: BCursor bc = index.getCursor();
177: try {
178: while (bc.next()) {
179: session.doStep(new DeleteRow(session, t, bc
180: .getValAsLong()));
181: }
182: } finally {
183: bc.release();
184: }
185: }
186:
187: String getType() {
188: return view ? "view" : "table";
189: }
190: }
|