KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > continuent > sequoia > console > text > commands > sqlconsole > CallStoredProcedure


1 /**
2  * Sequoia: Database clustering technology.
3  * Copyright (C) 2002-2004 French National Institute For Research In Computer
4  * Science And Control (INRIA).
5  * Contact: sequoia@continuent.org
6  *
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  * http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  *
19  * Initial developer(s): Jeff Mesnil.
20  * Contributor(s): ______________________.
21  */

22 /**
23  * Sequoia: Database clustering technology.
24  * Copyright (C) 2002-2004 French National Institute For Research In Computer
25  * Science And Control (INRIA).
26  * Contact: sequoia@continuent.org
27  *
28  * Licensed under the Apache License, Version 2.0 (the "License");
29  * you may not use this file except in compliance with the License.
30  * You may obtain a copy of the License at
31  *
32  * http://www.apache.org/licenses/LICENSE-2.0
33  *
34  * Unless required by applicable law or agreed to in writing, software
35  * distributed under the License is distributed on an "AS IS" BASIS,
36  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
37  * See the License for the specific language governing permissions and
38  * limitations under the License.
39  *
40  * Initial developer(s): Jeff Mesnil
41  * Contributor(s): ______________________.
42  */

43
44 package org.continuent.sequoia.console.text.commands.sqlconsole;
45
46 import java.io.IOException JavaDoc;
47 import java.sql.CallableStatement JavaDoc;
48 import java.sql.Connection JavaDoc;
49 import java.sql.ResultSet JavaDoc;
50
51 import org.continuent.sequoia.common.i18n.ConsoleTranslate;
52 import org.continuent.sequoia.console.text.ConsoleException;
53 import org.continuent.sequoia.console.text.commands.ConsoleCommand;
54 import org.continuent.sequoia.console.text.formatter.ResultSetFormatter;
55 import org.continuent.sequoia.console.text.module.VirtualDatabaseConsole;
56
57 /**
58  * This class defines a "timeout" sql command
59  *
60  * @author <a HREF="mailto:jeff.mesnil@emicnetworks.com">Jeff Mesnil</a>
61  */

62 public class CallStoredProcedure extends ConsoleCommand
63 {
64
65   /**
66    * Creates a new <code>SetTimeout</code> object
67    *
68    * @param module the command is attached to
69    */

70   public CallStoredProcedure(VirtualDatabaseConsole module)
71   {
72     super(module);
73   }
74
75   /**
76    * @see org.continuent.sequoia.console.text.commands.ConsoleCommand#parse(java.lang.String)
77    */

78   public void parse(String JavaDoc commandText) throws IOException JavaDoc, ConsoleException
79   {
80     callStoredProcedure(getCommandName() + " " + commandText); //$NON-NLS-1$
81
}
82
83   /**
84    * @see org.continuent.sequoia.console.text.commands.ConsoleCommand#getCommandName()
85    */

86   public String JavaDoc getCommandName()
87   {
88     return "{call"; //$NON-NLS-1$
89
}
90
91   /**
92    * @see org.continuent.sequoia.console.text.commands.ConsoleCommand#getCommandParameters()
93    */

94   public String JavaDoc getCommandParameters()
95   {
96     return ConsoleTranslate.get("sql.command.call.stored.procedure.params"); //$NON-NLS-1$
97
}
98
99   /**
100    * @see org.continuent.sequoia.console.text.commands.ConsoleCommand#getCommandDescription()
101    */

102   public String JavaDoc getCommandDescription()
103   {
104     return ConsoleTranslate
105         .get("sql.command.call.stored.procedure.description"); //$NON-NLS-1$
106
}
107
108   /**
109    * Call a store procedure.
110    *
111    * @param proc the stored procedure to call
112    */

113   private synchronized void callStoredProcedure(String JavaDoc proc)
114   {
115     Connection JavaDoc connection = ((VirtualDatabaseConsole) module).getConnection();
116     int fetchsize = ((VirtualDatabaseConsole) module).getFetchsize();
117     CallableStatement JavaDoc cs = null;
118     try
119     {
120       cs = connection.prepareCall(proc);
121       cs.setQueryTimeout(((VirtualDatabaseConsole) module).getTimeout());
122
123       long start = System.currentTimeMillis();
124       long end;
125       ResultSet JavaDoc rs = cs.executeQuery();
126       end = System.currentTimeMillis();
127       ResultSetFormatter.formatAndDisplay(rs, fetchsize, console);
128       console.printInfo(ConsoleTranslate.get("sql.display.query.time", //$NON-NLS-1$
129
new Long JavaDoc[]{new Long JavaDoc((end - start) / 1000),
130               new Long JavaDoc((end - start) % 1000)}));
131     }
132     catch (Exception JavaDoc e)
133     {
134       console.printError(ConsoleTranslate.get(
135           "sql.command.call.stored.procedure.failed", e), e); //$NON-NLS-1$
136
}
137     finally
138     {
139       try
140       {
141         cs.close();
142       }
143       catch (Exception JavaDoc ignore)
144       {
145       }
146     }
147   }
148 }
Popular Tags