001: /**
002: * Sequoia: Database clustering technology.
003: * Copyright (C) 2002-2004 French National Institute For Research In Computer
004: * Science And Control (INRIA).
005: * Contact: sequoia@continuent.org
006: *
007: * Licensed under the Apache License, Version 2.0 (the "License");
008: * you may not use this file except in compliance with the License.
009: * You may obtain a copy of the License at
010: *
011: * http://www.apache.org/licenses/LICENSE-2.0
012: *
013: * Unless required by applicable law or agreed to in writing, software
014: * distributed under the License is distributed on an "AS IS" BASIS,
015: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
016: * See the License for the specific language governing permissions and
017: * limitations under the License.
018: *
019: * Initial developer(s): Jeff Mesnil.
020: * Contributor(s): ______________________.
021: */
022: /**
023: * Sequoia: Database clustering technology.
024: * Copyright (C) 2002-2004 French National Institute For Research In Computer
025: * Science And Control (INRIA).
026: * Contact: sequoia@continuent.org
027: *
028: * Licensed under the Apache License, Version 2.0 (the "License");
029: * you may not use this file except in compliance with the License.
030: * You may obtain a copy of the License at
031: *
032: * http://www.apache.org/licenses/LICENSE-2.0
033: *
034: * Unless required by applicable law or agreed to in writing, software
035: * distributed under the License is distributed on an "AS IS" BASIS,
036: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
037: * See the License for the specific language governing permissions and
038: * limitations under the License.
039: *
040: * Initial developer(s): Jeff Mesnil
041: * Contributor(s): ______________________.
042: */package org.continuent.sequoia.console.text.commands.sqlconsole;
043:
044: import java.io.IOException;
045: import java.sql.CallableStatement;
046: import java.sql.Connection;
047: import java.sql.ResultSet;
048:
049: import org.continuent.sequoia.common.i18n.ConsoleTranslate;
050: import org.continuent.sequoia.console.text.ConsoleException;
051: import org.continuent.sequoia.console.text.commands.ConsoleCommand;
052: import org.continuent.sequoia.console.text.formatter.ResultSetFormatter;
053: import org.continuent.sequoia.console.text.module.VirtualDatabaseConsole;
054:
055: /**
056: * This class defines a "timeout" sql command
057: *
058: * @author <a href="mailto:jeff.mesnil@emicnetworks.com">Jeff Mesnil</a>
059: */
060: public class CallStoredProcedure extends ConsoleCommand {
061:
062: /**
063: * Creates a new <code>SetTimeout</code> object
064: *
065: * @param module the command is attached to
066: */
067: public CallStoredProcedure(VirtualDatabaseConsole module) {
068: super (module);
069: }
070:
071: /**
072: * @see org.continuent.sequoia.console.text.commands.ConsoleCommand#parse(java.lang.String)
073: */
074: public void parse(String commandText) throws IOException,
075: ConsoleException {
076: callStoredProcedure(getCommandName() + " " + commandText); //$NON-NLS-1$
077: }
078:
079: /**
080: * @see org.continuent.sequoia.console.text.commands.ConsoleCommand#getCommandName()
081: */
082: public String getCommandName() {
083: return "{call"; //$NON-NLS-1$
084: }
085:
086: /**
087: * @see org.continuent.sequoia.console.text.commands.ConsoleCommand#getCommandParameters()
088: */
089: public String getCommandParameters() {
090: return ConsoleTranslate
091: .get("sql.command.call.stored.procedure.params"); //$NON-NLS-1$
092: }
093:
094: /**
095: * @see org.continuent.sequoia.console.text.commands.ConsoleCommand#getCommandDescription()
096: */
097: public String getCommandDescription() {
098: return ConsoleTranslate
099: .get("sql.command.call.stored.procedure.description"); //$NON-NLS-1$
100: }
101:
102: /**
103: * Call a store procedure.
104: *
105: * @param proc the stored procedure to call
106: */
107: private synchronized void callStoredProcedure(String proc) {
108: Connection connection = ((VirtualDatabaseConsole) module)
109: .getConnection();
110: int fetchsize = ((VirtualDatabaseConsole) module)
111: .getFetchsize();
112: CallableStatement cs = null;
113: try {
114: cs = connection.prepareCall(proc);
115: cs.setQueryTimeout(((VirtualDatabaseConsole) module)
116: .getTimeout());
117:
118: long start = System.currentTimeMillis();
119: long end;
120: ResultSet rs = cs.executeQuery();
121: end = System.currentTimeMillis();
122: ResultSetFormatter.formatAndDisplay(rs, fetchsize, console);
123: console.printInfo(ConsoleTranslate.get(
124: "sql.display.query.time", //$NON-NLS-1$
125: new Long[] { new Long((end - start) / 1000),
126: new Long((end - start) % 1000) }));
127: } catch (Exception e) {
128: console.printError(ConsoleTranslate.get(
129: "sql.command.call.stored.procedure.failed", e), e); //$NON-NLS-1$
130: } finally {
131: try {
132: cs.close();
133: } catch (Exception ignore) {
134: }
135: }
136: }
137: }
|