KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > quadcap > sql > StmtDropTable


1 package com.quadcap.sql;
2
3 /* Copyright 1999 - 2003 Quadcap Software. All rights reserved.
4  *
5  * This software is distributed under the Quadcap Free Software License.
6  * This software may be used or modified for any purpose, personal or
7  * commercial. Open Source redistributions are permitted. Commercial
8  * redistribution of larger works derived from, or works which bundle
9  * this software requires a "Commercial Redistribution License"; see
10  * http://www.quadcap.com/purchase.
11  *
12  * Redistributions qualify as "Open Source" under one of the following terms:
13  *
14  * Redistributions are made at no charge beyond the reasonable cost of
15  * materials and delivery.
16  *
17  * Redistributions are accompanied by a copy of the Source Code or by an
18  * irrevocable offer to provide a copy of the Source Code for up to three
19  * years at the cost of materials and delivery. Such redistributions
20  * must allow further use, modification, and redistribution of the Source
21  * Code under substantially the same terms as this license.
22  *
23  * Redistributions of source code must retain the copyright notices as they
24  * appear in each source code file, these license terms, and the
25  * disclaimer/limitation of liability set forth as paragraph 6 below.
26  *
27  * Redistributions in binary form must reproduce this Copyright Notice,
28  * these license terms, and the disclaimer/limitation of liability set
29  * forth as paragraph 6 below, in the documentation and/or other materials
30  * provided with the distribution.
31  *
32  * The Software is provided on an "AS IS" basis. No warranty is
33  * provided that the Software is free of defects, or fit for a
34  * particular purpose.
35  *
36  * Limitation of Liability. Quadcap Software shall not be liable
37  * for any damages suffered by the Licensee or any third party resulting
38  * from use of the Software.
39  */

40
41 import java.io.IOException JavaDoc;
42
43 import java.util.ArrayList JavaDoc;
44 import java.util.Enumeration JavaDoc;
45 import java.util.HashMap JavaDoc;
46 import java.util.Iterator JavaDoc;
47 import java.util.Map JavaDoc;
48
49 import java.sql.SQLException JavaDoc;
50
51 import com.quadcap.sql.file.BlockFile;
52 import com.quadcap.sql.file.ByteUtil;
53
54 import com.quadcap.sql.index.Btree;
55 import com.quadcap.sql.index.BCursor;
56
57 import com.quadcap.util.Debug;
58
59 /**
60  * Implementation of the SQL <b>DROP TABLE</b> statement.
61  *
62  * @author Stan Bailes
63  */

64
65 public class StmtDropTable implements Stmt {
66     Map JavaDoc deleted;
67     String JavaDoc tableName;
68     Relation table;
69     boolean view;
70     boolean restrict;
71
72     public StmtDropTable() {}
73     
74     public StmtDropTable(String JavaDoc name, boolean view, boolean restrict) {
75     this.tableName = name;
76     this.view = view;
77     this.restrict = restrict;
78     }
79
80     public void execute(Session session) throws IOException JavaDoc, SQLException JavaDoc {
81     session.getTableWriteLock("#Schema");
82     session.getTableWriteLock(tableName);
83     Database db = session.getDatabase();
84         table = db.getRelation(tableName);
85     if (table == null) {
86         throw new SQLException JavaDoc("no such " + getType() +
87                                    ": " + tableName, "42000");
88     }
89
90     Enumeration JavaDoc views = db.getViews(tableName);
91     if (restrict) {
92         if (views.hasMoreElements()) {
93         throw new SQLException JavaDoc("Can't drop " + getType() +
94                        " with RESTRICT because of" +
95                        " referencing view: " + views.nextElement(),
96                        "23000");
97         }
98         if (table instanceof Table) {
99                 Table t = (Table)table;
100                 int num = t.getNumConstraints();
101                 for (int i = 0; i < num; i++) {
102                     Constraint c = t.getConstraint(i);
103             if (c instanceof ExportedKeyConstraint) {
104             ExportedKeyConstraint ex =
105                 (ExportedKeyConstraint)c;
106                         if (!ex.getFTableName().equals(tableName)) {
107                             throw new SQLException JavaDoc(
108                                 "Can't drop " + getType() +
109                                 " with RESTRICT because of " +
110                                 " referencing constraint: " +
111                                 ex.getName() + ", table " +
112                                 ex.getFTableName(),
113                                 "23000");
114                         }
115                     }
116         }
117         }
118     }
119     
120
121     if (!restrict) {
122             if (deleted == null) deleted = new HashMap JavaDoc();
123         while (views.hasMoreElements()) {
124                 String JavaDoc view1 = views.nextElement().toString();
125                 if (!deleted.containsKey(view1)) {
126                     deleted.put(view1, "");
127                     StmtDropTable t = new StmtDropTable(view1, true, false);
128                     t.deleted = deleted;
129             t.execute(session);
130                 }
131         }
132     }
133
134         // ---- Now, we're ready to rock.
135
if (table instanceof Table) {
136             // ---- First, make a copy of all the constraints to provide stable
137
// ---- enumeration.
138
IndexConstraint ic = null;
139             ArrayList JavaDoc list = new ArrayList JavaDoc();
140             Table t = (Table)table;
141             ic = t.getAnyIndex(session);
142             int num = t.getNumConstraints();
143             Constraint[] cv = new Constraint[num];
144             for (int i = 0; i < num; i++) {
145                 cv[i] = t.getConstraint(i);
146             }
147             for (int i = 0 ; i < num; i++) {
148                 Constraint c = cv[i];
149                 if (c instanceof ForeignKeyConstraint) {
150                     ForeignKeyConstraint fc = (ForeignKeyConstraint)c;
151                     if (!fc.isSelfReferencing(db)) {
152                         DeleteConstraint.deleteForeign(session, fc);
153                     }
154                 }
155                 if (c != ic) {
156                     session.doStep(new DeleteConstraint(session, t, c));
157                 }
158             }
159             // ---- use the index to drop the table rows
160
deleteRows(session, t, ic);
161
162             // ---- Remove the index constraint
163
session.doStep(new DeleteConstraint(session, t, ic));
164         }
165
166         // ---- DROP the table
167
session.doStep(new DropTable(session, table));
168     }
169
170     final void deleteRows(Session session, Table t, IndexConstraint c)
171     throws IOException JavaDoc, SQLException JavaDoc
172     {
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.getValAsLong()));
180             }
181         } finally {
182             bc.release();
183         }
184     }
185
186     String JavaDoc getType() {
187     return view ? "view" : "table";
188     }
189 }
190
Popular Tags