1 5 package org.h2.command.ddl; 6 7 import java.sql.SQLException ; 8 9 import org.h2.engine.Database; 10 import org.h2.engine.Right; 11 import org.h2.engine.Session; 12 import org.h2.message.Message; 13 import org.h2.schema.Schema; 14 import org.h2.table.Table; 15 16 19 public class DropTable extends SchemaCommand { 20 21 private boolean ifExists; 22 private String tableName; 23 private Table table; 24 private DropTable next; 25 26 public DropTable(Session session, Schema schema) { 27 super(session, schema); 28 } 29 30 public void addNextDropTable(DropTable next) { 31 if(this.next == null) { 32 this.next = next; 33 } else { 34 this.next.addNextDropTable(next); 35 } 36 } 37 38 public void setIfExists(boolean b) { 39 ifExists = b; 40 if(next != null) { 41 next.setIfExists(b); 42 } 43 } 44 45 public void setTableName(String tableName) { 46 this.tableName = tableName; 47 } 48 49 private void prepareDrop() throws SQLException { 50 table = getSchema().findTableOrView(session, tableName); 51 if(table == null) { 53 if(!ifExists) { 54 throw Message.getSQLException(Message.TABLE_OR_VIEW_NOT_FOUND_1, tableName); 55 } 56 } else { 57 session.getUser().checkRight(table, Right.ALL); 58 if(!table.canDrop()) { 59 throw Message.getSQLException(Message.CANT_DROP_TABLE_1, tableName); 60 } 61 table.lock(session, true); 62 } 63 if(next != null) { 64 next.prepareDrop(); 65 } 66 } 67 68 private void executeDrop() throws SQLException { 69 table = getSchema().findTableOrView(session, tableName); 71 if(table != null) { 72 table.setModified(); 73 Database db = session.getDatabase(); 74 db.removeSchemaObject(session, table); 75 } 76 if(next != null) { 77 next.executeDrop(); 78 } 79 } 80 81 public int update() throws SQLException { 82 session.commit(); 83 prepareDrop(); 84 executeDrop(); 85 return 0; 86 } 87 88 } 89 | Popular Tags |