01: package net.sourceforge.squirrel_sql.plugins.mysql.action;
02:
03: /*
04: * Copyright (C) 2003 Colin Bell
05: * colbell@users.sourceforge.net
06: *
07: * This library is free software; you can redistribute it and/or
08: * modify it under the terms of the GNU Lesser General Public
09: * License as published by the Free Software Foundation; either
10: * version 2.1 of the License, or (at your option) any later version.
11: *
12: * This library is distributed in the hope that it will be useful,
13: * but WITHOUT ANY WARRANTY; without even the implied warranty of
14: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15: * Lesser General Public License for more details.
16: *
17: * You should have received a copy of the GNU Lesser General Public
18: * License along with this library; if not, write to the Free Software
19: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20: */
21: import net.sourceforge.squirrel_sql.client.session.IObjectTreeAPI;
22: import net.sourceforge.squirrel_sql.client.session.ISession;
23: import net.sourceforge.squirrel_sql.fw.sql.IDatabaseObjectInfo;
24: import net.sourceforge.squirrel_sql.fw.util.ICommand;
25: import net.sourceforge.squirrel_sql.plugins.mysql.MysqlPlugin;
26:
27: /**
28: * This abstract command is a MySQL command that takes a table
29: * as a parameter.
30: *
31: * @author <A HREF="mailto:colbell@users.sourceforge.net">Colin Bell</A>
32: */
33: abstract class AbstractMultipleSQLCommand implements ICommand {
34: /** Current session. */
35: private ISession _session;
36:
37: /** Current plugin. */
38: private final MysqlPlugin _plugin;
39:
40: /**
41: * Ctor specifying the current session.
42: *
43: * @throws IllegalArgumentException
44: * Thrown if a?<TT>null</TT> <TT>ISession</TT>,
45: * <TT>Resources</TT> or <TT>MysqlPlugin</TT> passed.
46: */
47: public AbstractMultipleSQLCommand(ISession session,
48: MysqlPlugin plugin) {
49: super ();
50: if (session == null) {
51: throw new IllegalArgumentException("ISession == null");
52: }
53: if (plugin == null) {
54: throw new IllegalArgumentException("MysqlPlugin == null");
55: }
56:
57: _session = session;
58: _plugin = plugin;
59: }
60:
61: /**
62: * Execute this command.
63: */
64: public void execute() {
65: final StringBuffer buf = new StringBuffer(2048);
66: final String sep = " "
67: + _session.getQueryTokenizer()
68: .getSQLStatementSeparator();
69:
70: final IObjectTreeAPI api = _session.getSessionInternalFrame()
71: .getObjectTreeAPI();
72: final IDatabaseObjectInfo[] dbObjs = api
73: .getSelectedDatabaseObjects();
74:
75: for (int i = 0; i < dbObjs.length; ++i) {
76: final String cmd = getMySQLCommand(dbObjs[i]);
77: if (cmd != null && cmd.length() > 0) {
78: buf.append(cmd).append(" ").append(sep).append('\n');
79: }
80: }
81:
82: // Execute the SQL command in the SQL tab and then display the SQL tab.
83: if (buf.length() > 0) {
84: _session.getSessionInternalFrame().getSQLPanelAPI()
85: .appendSQLScript(buf.toString(), true);
86: _session.getSessionInternalFrame().getSQLPanelAPI()
87: .executeCurrentSQL();
88: _session
89: .selectMainTab(ISession.IMainPanelTabIndexes.SQL_TAB);
90: }
91: }
92:
93: /**
94: * Retrieve the MySQL command to run.
95: *
96: * @return the MySQL command to run.
97: */
98: protected abstract String getMySQLCommand(IDatabaseObjectInfo dbObj);
99: }
|