001: /*
002: * GenericObjectDropper.java
003: *
004: * This file is part of SQL Workbench/J, http://www.sql-workbench.net
005: *
006: * Copyright 2002-2008, Thomas Kellerer
007: * No part of this code maybe reused without the permission of the author
008: *
009: * To contact the author please send an email to: support@sql-workbench.net
010: *
011: */
012: package workbench.db;
013:
014: import java.sql.SQLException;
015: import java.sql.Statement;
016: import java.util.List;
017:
018: import workbench.interfaces.ObjectDropper;
019: import workbench.log.LogMgr;
020: import workbench.util.SqlUtil;
021: import workbench.util.StringUtil;
022:
023: /**
024: * A helper class to drop different types of objects
025: *
026: * @author support@sql-workbench.net
027: */
028: public class GenericObjectDropper implements ObjectDropper {
029: private List<? extends DbObject> objects;
030: private WbConnection connection;
031: private Statement currentStatement;
032: private boolean cascadeConstraints;
033: private TableIdentifier objectTable;
034:
035: public GenericObjectDropper() {
036: }
037:
038: public List<? extends DbObject> getObjects() {
039: return objects;
040: }
041:
042: public boolean supportsCascade() {
043: boolean canCascade = false;
044:
045: if (objects != null && this .connection != null) {
046: int numTypes = this .objects.size();
047: for (int i = 0; i < numTypes; i++) {
048: String type = this .objects.get(i).getObjectType();
049: String verb = this .connection.getDbSettings()
050: .getCascadeConstraintsVerb(type);
051:
052: // if at least one type can be dropped with CASCADE, enable the checkbox
053: if (!StringUtil.isEmptyString(verb)) {
054: canCascade = true;
055: break;
056: }
057: }
058: }
059: return canCascade;
060: }
061:
062: public void setObjects(List<? extends DbObject> toDrop) {
063: this .objects = toDrop;
064: }
065:
066: public void setObjectTable(TableIdentifier tbl) {
067: this .objectTable = tbl;
068: }
069:
070: public void setConnection(WbConnection aConn) {
071: this .connection = aConn;
072: }
073:
074: public void dropObjects() throws SQLException {
075: boolean needCommit = this .connection.shouldCommitDDL();
076:
077: try {
078: if (this .connection == null)
079: throw new NullPointerException("No connection!");
080: if (this .connection.isBusy())
081: return;
082: if (this .objects == null || this .objects.size() == 0)
083: return;
084: int count = this .objects.size();
085: this .connection.setBusy(true);
086:
087: currentStatement = this .connection.createStatement();
088:
089: String cascade = null;
090:
091: boolean needTableForIndexDrop = this .connection
092: .getDbSettings().needsTableForDropIndex();
093:
094: for (int i = 0; i < count; i++) {
095: String name = this .objects.get(i).getObjectName();
096: String type = this .objects.get(i).getObjectType();
097:
098: StringBuilder sql = new StringBuilder(120);
099: sql.append("DROP ");
100: sql.append(type);
101: sql.append(' ');
102: sql.append(name);
103:
104: if (needTableForIndexDrop && "INDEX".equals(type)
105: && objectTable != null) {
106: sql.append(" ON ");
107: sql.append(objectTable
108: .getTableExpression(this .connection));
109: }
110:
111: if (this .cascadeConstraints) {
112: cascade = this .connection.getDbSettings()
113: .getCascadeConstraintsVerb(type);
114: if (cascade != null) {
115: sql.append(' ');
116: sql.append(cascade);
117: }
118: }
119: LogMgr.logDebug("ObjectDropper.execute()",
120: "Using SQL: " + sql);
121: currentStatement.execute(sql.toString());
122: }
123:
124: if (needCommit) {
125: this .connection.commit();
126: }
127: } catch (SQLException e) {
128: if (needCommit) {
129: try {
130: this .connection.rollback();
131: } catch (Throwable th) {
132: }
133: }
134: throw e;
135: } finally {
136: SqlUtil.closeStatement(currentStatement);
137: this .currentStatement = null;
138: this .connection.setBusy(false);
139: }
140: }
141:
142: public void cancel() throws SQLException {
143: if (this .currentStatement == null)
144: return;
145: this .currentStatement.cancel();
146: if (this .connection.shouldCommitDDL()) {
147: try {
148: this .connection.rollback();
149: } catch (Throwable th) {
150: }
151: }
152: }
153:
154: public void setCascade(boolean flag) {
155: this.cascadeConstraints = flag;
156: }
157:
158: }
|