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