KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > h2 > command > ddl > DropTable


1 /*
2  * Copyright 2004-2006 H2 Group. Licensed under the H2 License, Version 1.0 (http://h2database.com/html/license.html).
3  * Initial Developer: H2 Group
4  */

5 package org.h2.command.ddl;
6
7 import java.sql.SQLException JavaDoc;
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 /**
17  * @author Thomas
18  */

19 public class DropTable extends SchemaCommand {
20
21     private boolean ifExists;
22     private String JavaDoc 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 JavaDoc tableName) {
46         this.tableName = tableName;
47     }
48
49     private void prepareDrop() throws SQLException JavaDoc {
50         table = getSchema().findTableOrView(session, tableName);
51         // TODO drop table: drops views as well (is this ok?)
52
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 JavaDoc {
69         // need to get the table again, because it may be dropped already meanwhile (dependent object, or same object)
70
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 JavaDoc {
82         session.commit();
83         prepareDrop();
84         executeDrop();
85         return 0;
86     }
87
88 }
89
Popular Tags