01: package org.firebirdsql.squirrel.act;
02:
03: import net.sourceforge.squirrel_sql.client.plugin.IPlugin;
04: import net.sourceforge.squirrel_sql.client.session.IObjectTreeAPI;
05: import net.sourceforge.squirrel_sql.client.session.ISession;
06: import net.sourceforge.squirrel_sql.fw.sql.IDatabaseObjectInfo;
07: import net.sourceforge.squirrel_sql.fw.util.ICommand;
08:
09: /**
10: * This abstract command is a command that takes a table
11: * as a parameter.
12: *
13: * @author <A HREF="mailto:colbell@users.sourceforge.net">Colin Bell</A>
14: */
15: abstract class AbstractMultipleSQLCommand implements ICommand {
16: /** Current session. */
17: private ISession _session;
18:
19: /** Current plugin. */
20: @SuppressWarnings("unused")
21: private final IPlugin _plugin;
22:
23: /**
24: * Ctor specifying the current session.
25: *
26: * @throws IllegalArgumentException
27: * Thrown if a?<TT>null</TT> <TT>ISession</TT>,
28: * <TT>Resources</TT> or <TT>IPlugin</TT> passed.
29: */
30: public AbstractMultipleSQLCommand(ISession session, IPlugin plugin) {
31: super ();
32: if (session == null) {
33: throw new IllegalArgumentException("ISession == null");
34: }
35: if (plugin == null) {
36: throw new IllegalArgumentException("IPlugin == null");
37: }
38:
39: _session = session;
40: _plugin = plugin;
41: }
42:
43: /**
44: * Execute this command.
45: */
46: public void execute() {
47: final StringBuffer buf = new StringBuffer(2048);
48: final String sep = "\n"
49: + _session.getQueryTokenizer()
50: .getSQLStatementSeparator();
51:
52: final IObjectTreeAPI api = _session.getSessionInternalFrame()
53: .getObjectTreeAPI();
54: final IDatabaseObjectInfo[] dbObjs = api
55: .getSelectedDatabaseObjects();
56:
57: for (int i = 0; i < dbObjs.length; ++i) {
58: final String cmd = getSQL(dbObjs[i]);
59: if (cmd != null && cmd.length() > 0) {
60: buf.append(cmd).append(sep).append('\n');
61: }
62: }
63:
64: // Execute the SQL command in the SQL tab and then display the SQL tab.
65: if (buf.length() > 0) {
66: _session.getSessionInternalFrame().getSQLPanelAPI()
67: .appendSQLScript(buf.toString(), true);
68: _session.getSessionInternalFrame().getSQLPanelAPI()
69: .executeCurrentSQL();
70: _session
71: .selectMainTab(ISession.IMainPanelTabIndexes.SQL_TAB);
72: }
73: }
74:
75: /**
76: * Retrieve the SQL to run.
77: *
78: * @return the SQL to run.
79: */
80: protected abstract String getSQL(IDatabaseObjectInfo dbObj);
81: }
|