01: package net.sourceforge.squirrel_sql.plugins.mssql.action;
02:
03: /*
04: * Copyright (C) 2004 Ryan Walberg <generalpf@yahoo.com>
05: *
06: * This library is free software; you can redistribute it and/or
07: * modify it under the terms of the GNU Lesser General Public
08: * License as published by the Free Software Foundation; either
09: * version 2.1 of the License, or (at your option) any later version.
10: *
11: * This library is distributed in the hope that it will be useful,
12: * but WITHOUT ANY WARRANTY; without even the implied warranty of
13: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14: * Lesser General Public License for more details.
15: *
16: * You should have received a copy of the GNU Lesser General Public
17: * License along with this library; if not, write to the Free Software
18: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19: */
20:
21: import java.sql.Connection;
22:
23: import net.sourceforge.squirrel_sql.client.session.ISession;
24: import net.sourceforge.squirrel_sql.fw.sql.IDatabaseObjectInfo;
25: import net.sourceforge.squirrel_sql.fw.sql.WrappedSQLException;
26: import net.sourceforge.squirrel_sql.fw.util.BaseException;
27: import net.sourceforge.squirrel_sql.fw.util.ICommand;
28: import net.sourceforge.squirrel_sql.plugins.mssql.MssqlPlugin;
29: import net.sourceforge.squirrel_sql.plugins.mssql.util.MssqlIntrospector;
30:
31: public class ScriptProcedureCommand implements ICommand {
32: private ISession _session;
33: private final MssqlPlugin _plugin;
34:
35: private final IDatabaseObjectInfo[] _dbObjs;
36:
37: public ScriptProcedureCommand(ISession session, MssqlPlugin plugin,
38: IDatabaseObjectInfo[] dbObjs) {
39: super ();
40: if (session == null)
41: throw new IllegalArgumentException("ISession == null");
42: if (dbObjs == null)
43: throw new IllegalArgumentException(
44: "IDatabaseObjectInfo array is null");
45:
46: _session = session;
47: _plugin = plugin;
48: _dbObjs = dbObjs;
49: }
50:
51: public void execute() throws BaseException {
52: try {
53: if (_dbObjs.length > 0) {
54: Connection conn = _session.getSQLConnection()
55: .getConnection();
56: final String sqlSep = _session.getQueryTokenizer()
57: .getSQLStatementSeparator();
58: final StringBuffer buf = new StringBuffer();
59:
60: for (int i = 0; i < _dbObjs.length; i++) {
61: final IDatabaseObjectInfo ti = _dbObjs[i];
62: /* TODO: what I really want to do here is get the SQL script and replace
63: * "CREATE PROCEDURE" with "ALTER PROCEDURE", then put that in the SQL pane
64: * and *NOT* execute it. */
65:
66: /* NOTE: a procedure may also be a UDF! */
67:
68: if (!conn.getCatalog().equals(ti.getCatalogName()))
69: conn.setCatalog(ti.getCatalogName());
70:
71: buf.append(MssqlIntrospector.getHelpTextForObject(
72: MssqlIntrospector
73: .getFixedVersionedObjectName(ti
74: .getSimpleName()), conn));
75: buf.append("\n");
76: buf.append(sqlSep);
77: buf.append("\n\n");
78: }
79:
80: _session.getSessionInternalFrame().getSQLPanelAPI()
81: .appendSQLScript(buf.toString());
82: _session
83: .selectMainTab(ISession.IMainPanelTabIndexes.SQL_TAB);
84: }
85: } catch (java.sql.SQLException ex) {
86: ex.printStackTrace();
87: throw new WrappedSQLException(ex);
88: }
89: }
90: }
|