01: /*
02: * TableDropper.java
03: *
04: * This file is part of SQL Workbench/J, http://www.sql-workbench.net
05: *
06: * Copyright 2002-2008, Thomas Kellerer
07: * No part of this code maybe reused without the permission of the author
08: *
09: * To contact the author please send an email to: support@sql-workbench.net
10: *
11: */
12: package workbench.db;
13:
14: import java.sql.SQLException;
15: import java.sql.Statement;
16: import workbench.util.SqlUtil;
17:
18: /**
19: * @author support@sql-workbench.net
20: */
21: public class TableDropper {
22: private WbConnection dbConnection;
23:
24: public TableDropper(WbConnection con) {
25: this .dbConnection = con;
26: }
27:
28: /**
29: * Drop given table. If this is successful and the
30: * DBMS requires a COMMIT for DDL statements then
31: * the DROP will be commited (or rolled back in case
32: * of an error
33: */
34: public void dropTable(TableIdentifier aTable) throws SQLException {
35: Statement stmt = null;
36: DbMetadata meta = this .dbConnection.getMetadata();
37: DbSettings dbs = meta.getDbSettings();
38:
39: try {
40: StringBuilder sql = new StringBuilder();
41: sql.append("DROP TABLE ");
42: sql.append(aTable.getTableExpression(this .dbConnection));
43: String cascade = dbs.getCascadeConstraintsVerb("TABLE");
44: if (cascade != null) {
45: sql.append(' ');
46: sql.append(cascade);
47: }
48: stmt = this .dbConnection.createStatement();
49: stmt.executeUpdate(sql.toString());
50: if (this .dbConnection.shouldCommitDDL()) {
51: this .dbConnection.commit();
52: }
53: } catch (SQLException e) {
54: if (this .dbConnection.shouldCommitDDL()) {
55: try {
56: this .dbConnection.rollback();
57: } catch (Throwable th) {
58: }
59: }
60: throw e;
61: } finally {
62: SqlUtil.closeStatement(stmt);
63: }
64: }
65: }
|