01: package net.sourceforge.squirrel_sql.plugins.SybaseASE;
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.ITableInfo;
13: import net.sourceforge.squirrel_sql.fw.util.Resources;
14: import net.sourceforge.squirrel_sql.fw.util.log.ILogger;
15: import net.sourceforge.squirrel_sql.fw.util.log.LoggerController;
16:
17: public class ScriptSybaseASEViewAction extends SquirrelAction {
18: private static final long serialVersionUID = 1L;
19:
20: transient private ISession _session;
21:
22: /** Logger for this class. */
23: transient private final ILogger s_log = LoggerController
24: .createLogger(ScriptSybaseASEViewAction.class);
25:
26: ///////////////////////////////////////////////////////////
27: // Sybase ASE specific code to read view definitions.
28: private static final String sql = "Select text "
29: + "from sysobjects inner join syscomments on syscomments.id = sysobjects.id "
30: + "where name = ?";
31:
32: public ScriptSybaseASEViewAction(IApplication app, Resources rsrc,
33: ISession session) {
34: super (app, rsrc);
35: _session = session;
36: }
37:
38: public void actionPerformed(ActionEvent evt) {
39: try {
40: PreparedStatement stat = _session.getSQLConnection()
41: .prepareStatement(sql);
42:
43: SessionInternalFrame sessMainFrm = _session
44: .getSessionInternalFrame();
45: IDatabaseObjectInfo[] dbObjs = sessMainFrm
46: .getObjectTreeAPI().getSelectedDatabaseObjects();
47:
48: StringBuffer script = new StringBuffer();
49: for (int i = 0; i < dbObjs.length; i++) {
50: ITableInfo ti = (ITableInfo) dbObjs[i];
51:
52: stat.setString(1, ti.getSimpleName());
53: if (s_log.isDebugEnabled()) {
54: s_log.debug("Running SQL: " + sql);
55: s_log.debug("Bind var value is: "
56: + ti.getSimpleName());
57: }
58: ResultSet res = stat.executeQuery(sql);
59:
60: while (res.next()) {
61: script.append(res.getString("text"));
62: }
63: script.append(getStatementSeparator());
64: res.close();
65: //
66: ///////////////////////////////////////////////////////////
67: }
68:
69: stat.close();
70:
71: sessMainFrm.getSQLPanelAPI().appendSQLScript(
72: script.toString());
73: sessMainFrm.getSessionPanel().selectMainTab(
74: ISession.IMainPanelTabIndexes.SQL_TAB);
75: } catch (Exception e) {
76: s_log.error(
77: "Unexpected exception while attempting to get source for view: "
78: + e.getMessage(), e);
79:
80: throw new RuntimeException(e);
81: }
82: }
83:
84: private String getStatementSeparator() {
85: String statementSeparator = _session.getQueryTokenizer()
86: .getSQLStatementSeparator();
87:
88: if (1 < statementSeparator.length()) {
89: statementSeparator = "\n" + statementSeparator + "\n";
90: }
91:
92: return statementSeparator;
93: }
94:
95: }
|