KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > mckoi > database > interpret > DropTable


1 /**
2  * com.mckoi.database.interpret.DropTable 14 Sep 2001
3  *
4  * Mckoi SQL Database ( http://www.mckoi.com/database )
5  * Copyright (C) 2000, 2001, 2002 Diehl and Associates, Inc.
6  *
7  * This program is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License
9  * Version 2 as published by the Free Software Foundation.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License Version 2 for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * Version 2 along with this program; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
19  *
20  * Change Log:
21  *
22  *
23  */

24
25 package com.mckoi.database.interpret;
26
27 import com.mckoi.database.*;
28 import java.util.ArrayList JavaDoc;
29 import java.util.List JavaDoc;
30
31 /**
32  * The logic of the 'DROP TABLE' SQL command.
33  *
34  * @author Tobias Downer
35  */

36
37 public class DropTable extends Statement {
38
39   /**
40    * Only create if table doesn't exist.
41    */

42   boolean only_if_exists = false;
43
44   /**
45    * The list of tables to drop.
46    */

47   ArrayList JavaDoc drop_tables = new ArrayList JavaDoc();
48
49
50 // /**
51
// * Adds the table name to the list of tables to drop.
52
// */
53
// void addTable(String table) throws ParseException {
54
// if (drop_tables.contains(table)) {
55
// throw new ParseException("Duplicate table in drop");
56
// }
57
// drop_tables.add(table);
58
// }
59

60
61   // ---------- Implemented from Statement ----------
62

63   public void prepare() throws DatabaseException {
64
65     only_if_exists = cmd.getBoolean("only_if_exists");
66     drop_tables = (ArrayList JavaDoc) cmd.getObject("table_list");
67
68     // Check there are no duplicate entries in the list of tables to drop
69
for (int i = 0; i < drop_tables.size(); ++i) {
70       Object JavaDoc check = drop_tables.get(i);
71       for (int n = i + 1; n < drop_tables.size(); ++n) {
72         if (drop_tables.get(n).equals(check)) {
73           throw new DatabaseException("Duplicate table in drop: " + check);
74         }
75       }
76     }
77
78   }
79
80   public Table evaluate() throws DatabaseException {
81
82     DatabaseQueryContext context = new DatabaseQueryContext(database);
83
84     int list_size = drop_tables.size();
85     ArrayList JavaDoc resolved_tables = new ArrayList JavaDoc(list_size);
86     // Check the user has privs to delete these tables...
87
for (int i = 0; i < list_size; ++i) {
88       String JavaDoc table_name = drop_tables.get(i).toString();
89       TableName tname = resolveTableName(table_name, database);
90       // Does the table exist?
91
if (!only_if_exists && !database.tableExists(tname)) {
92         throw new DatabaseException("Table '" + tname + "' does not exist.");
93       }
94
95       resolved_tables.add(tname);
96       // Does the user have privs to drop this tables?
97
if (!database.getDatabase().canUserDropTableObject(context,
98                                                          user, tname)) {
99         throw new UserAccessException(
100            "User not permitted to drop table: " + tname);
101       }
102     }
103
104     // Check there are no referential links to any tables being dropped
105
for (int i = 0; i < list_size; ++i) {
106       TableName tname = (TableName) resolved_tables.get(i);
107       // Any tables that have a referential link to this table.
108
Transaction.ColumnGroupReference[] refs =
109                       database.queryTableImportedForeignKeyReferences(tname);
110       for (int n = 0; n < refs.length; ++n) {
111         // If the key table isn't being dropped then error
112
if (!resolved_tables.contains(refs[n].key_table_name)) {
113           throw new DatabaseConstraintViolationException(
114             DatabaseConstraintViolationException.DROP_TABLE_VIOLATION,
115               "Constraint violation (" + refs[n].name + ") dropping table " +
116               tname + " because of referential link from " +
117               refs[n].key_table_name);
118         }
119       }
120     }
121
122
123     // If the 'only if exists' flag is false, we need to check tables to drop
124
// exist first.
125
if (!only_if_exists) {
126       // For each table to drop.
127
for (int i = 0; i < list_size; ++i) {
128         // Does the table already exist?
129
// String table_name = drop_tables.get(i).toString();
130
//// TableName tname =
131
//// TableName.resolve(database.getCurrentSchema(), table_name);
132
// TableName tname = resolveTableName(table_name, database);
133
TableName tname = (TableName) resolved_tables.get(i);
134
135         // If table doesn't exist, throw an error
136
if (!database.tableExists(tname)) {
137           throw new DatabaseException("Can not drop table '" + tname +
138                                       "'. It does not exist.");
139         }
140       }
141     }
142
143     // For each table to drop.
144
int dropped_table_count = 0;
145     GrantManager grant_manager = database.getGrantManager();
146     for (int i = 0; i < list_size; ++i) {
147       // Does the table already exist?
148
// String table_name = drop_tables.get(i).toString();
149
// TableName tname = resolveTableName(table_name, database);
150
TableName tname = (TableName) resolved_tables.get(i);
151       if (database.tableExists(tname)) {
152         // Drop table in the transaction
153
database.dropTable(tname);
154         // Drop the grants for this object
155
grant_manager.revokeAllGrantsOnObject(
156                                       GrantManager.TABLE, tname.toString());
157         // Drop all constraints from the schema
158
database.dropAllConstraintsForTable(tname);
159         ++dropped_table_count;
160       }
161     }
162
163     return FunctionTable.resultTable(context, 0);
164   }
165
166
167 }
168
Popular Tags