001: package net.sourceforge.squirrel_sql.plugins.sqlscript.table_script;
002:
003: /*
004: * Copyright (C) 2006 Rob Manning
005: * manningr@sourceforge.net
006: *
007: * This program is free software; you can redistribute it and/or
008: * modify it under the terms of the GNU General Public License
009: * as published by the Free Software Foundation; either version 2
010: * of the License, or any later version.
011: *
012: * This program is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
015: * GNU General Public License for more details.
016: *
017: * You should have received a copy of the GNU General Public License
018: * along with this program; if not, write to the Free Software
019: * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
020: */
021:
022: import net.sourceforge.squirrel_sql.client.session.IObjectTreeAPI;
023: import net.sourceforge.squirrel_sql.client.session.ISQLPanelAPI;
024: import net.sourceforge.squirrel_sql.client.session.ISession;
025: import net.sourceforge.squirrel_sql.fw.gui.GUIUtils;
026: import net.sourceforge.squirrel_sql.fw.sql.IDatabaseObjectInfo;
027: import net.sourceforge.squirrel_sql.fw.sql.ITableInfo;
028: import net.sourceforge.squirrel_sql.fw.util.ICommand;
029: import net.sourceforge.squirrel_sql.fw.util.StringManager;
030: import net.sourceforge.squirrel_sql.fw.util.StringManagerFactory;
031: import net.sourceforge.squirrel_sql.fw.util.log.ILogger;
032: import net.sourceforge.squirrel_sql.fw.util.log.LoggerController;
033: import net.sourceforge.squirrel_sql.plugins.sqlscript.FrameWorkAcessor;
034: import net.sourceforge.squirrel_sql.plugins.sqlscript.SQLScriptPlugin;
035:
036: public class DropTableScriptCommand implements ICommand {
037: /**
038: * Current session.
039: */
040: private ISession _session;
041:
042: /**
043: * Current plugin.
044: */
045: private final SQLScriptPlugin _plugin;
046:
047: /** Logger for this class. */
048: private static ILogger s_log = LoggerController
049: .createLogger(DropTableScriptCommand.class);
050:
051: /** i18n strings for this class */
052: private static final StringManager s_stringMgr = StringManagerFactory
053: .getStringManager(DropTableScriptCommand.class);
054:
055: /**
056: * Ctor specifying the current session.
057: */
058: public DropTableScriptCommand(ISession session,
059: SQLScriptPlugin plugin) {
060: super ();
061: _session = session;
062: _plugin = plugin;
063: }
064:
065: /**
066: * Execute this command. Use the database meta data to construct a Create
067: * Table SQL script and place it in the SQL entry panel.
068: */
069: public void execute() {
070: IObjectTreeAPI api = FrameWorkAcessor.getObjectTreeAPI(
071: _session, _plugin);
072: IDatabaseObjectInfo[] dbObjs = api.getSelectedDatabaseObjects();
073: scriptTablesToSQLEntryArea(dbObjs);
074: }
075:
076: public void scriptTablesToSQLEntryArea(
077: final IDatabaseObjectInfo[] dbObjs) {
078: _session.getApplication().getThreadPool().addTask(
079: new Runnable() {
080: public void run() {
081: final String script = dropTableScriptString(dbObjs);
082: if (script != null) {
083: GUIUtils
084: .processOnSwingEventThread(new Runnable() {
085: public void run() {
086: ISQLPanelAPI api = FrameWorkAcessor
087: .getSQLPanelAPI(
088: _session,
089: _plugin);
090: api.appendSQLScript(script,
091: true);
092: int tabIdx = ISession.IMainPanelTabIndexes.SQL_TAB;
093: _session
094: .selectMainTab(tabIdx);
095: }
096: });
097: }
098: }
099: });
100: }
101:
102: public String dropTableScriptString(IDatabaseObjectInfo[] dbObjs) {
103: StringBuffer sbScript = new StringBuffer(1000);
104: try {
105: for (int k = 0; k < dbObjs.length; k++) {
106: if (false == dbObjs[k] instanceof ITableInfo) {
107: continue;
108:
109: }
110: ITableInfo ti = (ITableInfo) dbObjs[k];
111:
112: String sTable = ScriptUtil.getTableName(ti);
113: sbScript.append("DROP TABLE ");
114: sbScript.append(sTable);
115: sbScript.append(ScriptUtil
116: .getStatementSeparator(_session));
117: sbScript.append("\n");
118: }
119: } catch (Exception e) {
120: _session.showErrorMessage(e);
121: }
122: return sbScript.toString();
123: }
124: }
|