1 24 25 package com.mckoi.database.interpret; 26 27 import com.mckoi.database.*; 28 import java.util.ArrayList ; 29 import java.util.List ; 30 31 36 37 public class DropTable extends Statement { 38 39 42 boolean only_if_exists = false; 43 44 47 ArrayList drop_tables = new ArrayList (); 48 49 50 60 61 63 public void prepare() throws DatabaseException { 64 65 only_if_exists = cmd.getBoolean("only_if_exists"); 66 drop_tables = (ArrayList ) cmd.getObject("table_list"); 67 68 for (int i = 0; i < drop_tables.size(); ++i) { 70 Object 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 resolved_tables = new ArrayList (list_size); 86 for (int i = 0; i < list_size; ++i) { 88 String table_name = drop_tables.get(i).toString(); 89 TableName tname = resolveTableName(table_name, database); 90 if (!only_if_exists && !database.tableExists(tname)) { 92 throw new DatabaseException("Table '" + tname + "' does not exist."); 93 } 94 95 resolved_tables.add(tname); 96 if (!database.getDatabase().canUserDropTableObject(context, 98 user, tname)) { 99 throw new UserAccessException( 100 "User not permitted to drop table: " + tname); 101 } 102 } 103 104 for (int i = 0; i < list_size; ++i) { 106 TableName tname = (TableName) resolved_tables.get(i); 107 Transaction.ColumnGroupReference[] refs = 109 database.queryTableImportedForeignKeyReferences(tname); 110 for (int n = 0; n < refs.length; ++n) { 111 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 (!only_if_exists) { 126 for (int i = 0; i < list_size; ++i) { 128 TableName tname = (TableName) resolved_tables.get(i); 134 135 if (!database.tableExists(tname)) { 137 throw new DatabaseException("Can not drop table '" + tname + 138 "'. It does not exist."); 139 } 140 } 141 } 142 143 int dropped_table_count = 0; 145 GrantManager grant_manager = database.getGrantManager(); 146 for (int i = 0; i < list_size; ++i) { 147 TableName tname = (TableName) resolved_tables.get(i); 151 if (database.tableExists(tname)) { 152 database.dropTable(tname); 154 grant_manager.revokeAllGrantsOnObject( 156 GrantManager.TABLE, tname.toString()); 157 database.dropAllConstraintsForTable(tname); 159 ++dropped_table_count; 160 } 161 } 162 163 return FunctionTable.resultTable(context, 0); 164 } 165 166 167 } 168 | Popular Tags |