1 package com.quadcap.sql; 2 3 40 41 import java.io.IOException ; 42 43 import java.util.ArrayList ; 44 import java.util.Enumeration ; 45 import java.util.HashMap ; 46 import java.util.Iterator ; 47 import java.util.Map ; 48 49 import java.sql.SQLException ; 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 64 65 public class StmtDropTable implements Stmt { 66 Map deleted; 67 String tableName; 68 Relation table; 69 boolean view; 70 boolean restrict; 71 72 public StmtDropTable() {} 73 74 public StmtDropTable(String 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 , SQLException { 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 ("no such " + getType() + 87 ": " + tableName, "42000"); 88 } 89 90 Enumeration views = db.getViews(tableName); 91 if (restrict) { 92 if (views.hasMoreElements()) { 93 throw new SQLException ("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 ( 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 (); 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, false); 128 t.deleted = deleted; 129 t.execute(session); 130 } 131 } 132 } 133 134 if (table instanceof Table) { 136 IndexConstraint ic = null; 139 ArrayList list = new ArrayList (); 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 deleteRows(session, t, ic); 161 162 session.doStep(new DeleteConstraint(session, t, ic)); 164 } 165 166 session.doStep(new DropTable(session, table)); 168 } 169 170 final void deleteRows(Session session, Table t, IndexConstraint c) 171 throws IOException , SQLException 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 getType() { 187 return view ? "view" : "table"; 188 } 189 } 190 | Popular Tags |