001: package net.sourceforge.squirrel_sql.plugins.mysql.action;
002:
003: /*
004: * Copyright (C) 2003 Colin Bell
005: * colbell@users.sourceforge.net
006: *
007: * This library is free software; you can redistribute it and/or
008: * modify it under the terms of the GNU Lesser General Public
009: * License as published by the Free Software Foundation; either
010: * version 2.1 of the License, or (at your option) any later version.
011: *
012: * This library 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 GNU
015: * Lesser General Public License for more details.
016: *
017: * You should have received a copy of the GNU Lesser General Public
018: * License along with this library; if not, write to the Free Software
019: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
020: */
021: import net.sourceforge.squirrel_sql.fw.sql.IDatabaseObjectInfo;
022: import net.sourceforge.squirrel_sql.fw.util.ICommand;
023:
024: import net.sourceforge.squirrel_sql.plugins.mysql.MysqlPlugin;
025:
026: import net.sourceforge.squirrel_sql.client.session.IObjectTreeAPI;
027: import net.sourceforge.squirrel_sql.client.session.ISession;
028:
029: /**
030: * This abstract command is a MySQL command that takes a comma separated list
031: * of tables as a parameter.
032: *
033: * @author <A HREF="mailto:colbell@users.sourceforge.net">Colin Bell</A>
034: */
035: abstract class AbstractTableListCommand implements ICommand {
036: /** Current session. */
037: private ISession _session;
038:
039: /** Current plugin. */
040: private final MysqlPlugin _plugin;
041:
042: /**
043: * Ctor specifying the current session.
044: *
045: * @throws IllegalArgumentException
046: * Thrown if a?<TT>null</TT> <TT>ISession</TT>,
047: * <TT>Resources</TT> or <TT>MysqlPlugin</TT> passed.
048: */
049: public AbstractTableListCommand(ISession session, MysqlPlugin plugin) {
050: super ();
051: if (session == null) {
052: throw new IllegalArgumentException("ISession == null");
053: }
054: if (plugin == null) {
055: throw new IllegalArgumentException("MysqlPlugin == null");
056: }
057:
058: _session = session;
059: _plugin = plugin;
060: }
061:
062: /**
063: * Execute this command.
064: */
065: public void execute() {
066: final IObjectTreeAPI api = _session.getSessionInternalFrame()
067: .getObjectTreeAPI();
068: final IDatabaseObjectInfo[] dbObjs = api
069: .getSelectedDatabaseObjects();
070:
071: // Get the names of all the selected tables in a comma separated list,
072: StringBuffer tableList = new StringBuffer(512);
073: for (int i = 0; i < dbObjs.length; ++i) {
074: tableList.append(dbObjs[i].getQualifiedName()).append(",");
075: }
076: if (tableList.length() > 0) {
077: tableList.setLength(tableList.length() - 1); // Remove trailing ","
078: }
079:
080: // Execute the SQL command in the SQL tab and then display the SQL tab.
081: final StringBuffer cmd = new StringBuffer(512);
082: cmd.append(getMySQLCommand()).append(' ').append(tableList);
083: final String cmdStr = checkSQL(cmd.toString());
084: if (cmdStr != null && cmdStr.length() > 0) {
085: _session.getSessionInternalFrame().getSQLPanelAPI()
086: .appendSQLScript(cmdStr, true);
087: _session.getSessionInternalFrame().getSQLPanelAPI()
088: .executeCurrentSQL();
089: _session
090: .selectMainTab(ISession.IMainPanelTabIndexes.SQL_TAB);
091: }
092: }
093:
094: /**
095: * Last chance for subclass to modify the SQL command prior to executing
096: * it. The default behaviour is just to return the SQL string passed in.
097: *
098: * @return The SQL command to execute.
099: */
100: protected String checkSQL(String sql) {
101: return sql;
102: }
103:
104: /**
105: * Retrieve the MySQL command to run.
106: *
107: * @return the MySQL command to run.
108: */
109: protected abstract String getMySQLCommand();
110: }
|