001: /*
002: * ColumnDropper.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.ArrayList;
017: import java.util.List;
018: import workbench.interfaces.ObjectDropper;
019: import workbench.log.LogMgr;
020: import workbench.util.SqlUtil;
021: import workbench.util.StringUtil;
022:
023: /**
024: *
025: * @author support@sql-workbench.net
026: */
027: public class ColumnDropper implements ObjectDropper {
028: private WbConnection conn;
029: private List<ColumnIdentifier> columns;
030: private TableIdentifier table;
031: private boolean cancelDrop = false;
032: private Statement currentStatement;
033:
034: public ColumnDropper() {
035: }
036:
037: public ColumnDropper(WbConnection db, TableIdentifier tbl,
038: List<ColumnIdentifier> toDrop) {
039: this .conn = db;
040: this .columns = toDrop;
041: this .table = tbl;
042: }
043:
044: public boolean supportsCascade() {
045: return false;
046: }
047:
048: public void setCascade(boolean flag) {
049: }
050:
051: public void cancel() throws SQLException {
052: cancelDrop = true;
053: if (this .currentStatement != null) {
054: this .currentStatement.cancel();
055: }
056: }
057:
058: public void setConnection(WbConnection con) {
059: this .conn = con;
060: }
061:
062: public void setObjectTable(TableIdentifier tbl) {
063: this .table = tbl;
064: }
065:
066: public List<? extends DbObject> getObjects() {
067: return columns;
068: }
069:
070: public void setObjects(List<? extends DbObject> toDrop) {
071: this .columns = new ArrayList<ColumnIdentifier>();
072: if (toDrop == null)
073: return;
074: for (DbObject dbo : toDrop) {
075: if (dbo instanceof ColumnIdentifier) {
076: columns.add((ColumnIdentifier) dbo);
077: }
078: }
079: }
080:
081: public void dropObjects() throws SQLException {
082: if (this .conn == null)
083: return;
084: if (this .table == null)
085: return;
086: if (this .columns == null || this .columns.size() == 0)
087: return;
088:
089: List<String> statements = getSql();
090:
091: try {
092: this .currentStatement = this .conn.createStatement();
093:
094: for (String sql : statements) {
095: if (cancelDrop)
096: break;
097: LogMgr.logDebug("ColumnDropper.dropObjects()",
098: "Using sql: " + sql);
099: this .currentStatement.executeUpdate(sql);
100: }
101:
102: if (conn.shouldCommitDDL()) {
103: if (cancelDrop) {
104: conn.rollback();
105: } else {
106: conn.commit();
107: }
108: }
109: } catch (SQLException e) {
110: if (conn.shouldCommitDDL()) {
111: conn.rollback();
112: }
113: throw e;
114: } finally {
115: SqlUtil.closeStatement(currentStatement);
116: currentStatement = null;
117: }
118: }
119:
120: private List<String> getSql() {
121: String multiSql = conn.getDbSettings()
122: .getDropMultipleColumnSql();
123: String singleSql = conn.getDbSettings()
124: .getDropSingleColumnSql();
125: List<String> result = new ArrayList<String>(columns.size());
126:
127: if (this .columns.size() == 1
128: || StringUtil.isEmptyString(multiSql)) {
129: singleSql = StringUtil.replace(singleSql,
130: MetaDataSqlManager.TABLE_NAME_PLACEHOLDER, table
131: .getTableExpression(conn));
132:
133: for (ColumnIdentifier col : columns) {
134: result.add(StringUtil.replace(singleSql,
135: MetaDataSqlManager.COLUMN_NAME_PLACEHOLDER, col
136: .getColumnName(this .conn)));
137: }
138: } else {
139: multiSql = StringUtil.replace(multiSql,
140: MetaDataSqlManager.TABLE_NAME_PLACEHOLDER, table
141: .getTableExpression(conn));
142:
143: StringBuilder cols = new StringBuilder(columns.size());
144: int nr = 0;
145: for (ColumnIdentifier col : columns) {
146: if (nr > 0)
147: cols.append(", ");
148: cols.append(col.getColumnName(this.conn));
149: nr++;
150: }
151: result.add(StringUtil.replace(multiSql,
152: MetaDataSqlManager.COLUMN_LIST_PLACEHOLDER, cols
153: .toString()));
154: }
155:
156: return result;
157: }
158: }
|