01: package net.sourceforge.squirrel_sql.plugins.example;
02:
03: import java.awt.event.ActionEvent;
04: import java.sql.PreparedStatement;
05: import java.sql.ResultSet;
06:
07: import net.sourceforge.squirrel_sql.client.IApplication;
08: import net.sourceforge.squirrel_sql.client.action.SquirrelAction;
09: import net.sourceforge.squirrel_sql.client.gui.session.SessionInternalFrame;
10: import net.sourceforge.squirrel_sql.client.session.ISession;
11: import net.sourceforge.squirrel_sql.fw.sql.IDatabaseObjectInfo;
12: import net.sourceforge.squirrel_sql.fw.sql.IProcedureInfo;
13: import net.sourceforge.squirrel_sql.fw.sql.SQLUtilities;
14: import net.sourceforge.squirrel_sql.fw.util.Resources;
15:
16: public class ScriptDB2ProcedureAction extends SquirrelAction {
17: private static final long serialVersionUID = 1L;
18:
19: transient private ISession _session;
20:
21: // /////////////////////////////////////////////////////////
22: // IBM DB 2 specific code to read procedure definitions.
23: private static final String SQL = "SELECT TEXT "
24: + "FROM SYSIBM.SYSPROCEDURES " + "WHERE PROCNAME = ? ";
25:
26: public ScriptDB2ProcedureAction(IApplication app, Resources rsrc,
27: ISession session) {
28: super (app, rsrc);
29: _session = session;
30: }
31:
32: public void actionPerformed(ActionEvent evt) {
33: PreparedStatement stat = null;
34: ResultSet res = null;
35: try {
36:
37: IDatabaseObjectInfo[] dbObjs = _session
38: .getSessionInternalFrame().getObjectTreeAPI()
39: .getSelectedDatabaseObjects();
40:
41: stat = _session.getSQLConnection().prepareStatement(SQL);
42:
43: StringBuffer script = new StringBuffer();
44: for (int i = 0; i < dbObjs.length; i++) {
45: IProcedureInfo pi = (IProcedureInfo) dbObjs[i];
46: stat.setString(1, pi.getSimpleName());
47: res = stat.executeQuery();
48: res.next();
49: res.getString("TEXT");
50:
51: script.append(res.getString("TEXT"));
52: script.append(getStatementSeparator());
53: res.close();
54: }
55: stat.close();
56:
57: SessionInternalFrame sessMainFrm = _session
58: .getSessionInternalFrame();
59: sessMainFrm.getSQLPanelAPI().appendSQLScript(
60: script.toString());
61: sessMainFrm.getSessionPanel().selectMainTab(
62: ISession.IMainPanelTabIndexes.SQL_TAB);
63: } catch (Exception e) {
64: throw new RuntimeException(e);
65: } finally {
66: SQLUtilities.closeResultSet(res);
67: SQLUtilities.closeStatement(stat);
68: }
69: }
70:
71: private String getStatementSeparator() {
72: String statementSeparator = _session.getQueryTokenizer()
73: .getSQLStatementSeparator();
74:
75: if (1 < statementSeparator.length()) {
76: statementSeparator = "\n" + statementSeparator + "\n";
77: }
78:
79: return statementSeparator;
80: }
81:
82: }
|