KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > cjdbc > common > sql > StoredProcedure


1 /**
2  * C-JDBC: Clustered JDBC.
3  * Copyright (C) 2002-2004 French National Institute For Research In Computer
4  * Science And Control (INRIA).
5  * Contact: c-jdbc@objectweb.org
6  *
7  * This library is free software; you can redistribute it and/or modify it
8  * under the terms of the GNU Lesser General Public License as published by the
9  * Free Software Foundation; either version 2.1 of the License, or any later
10  * version.
11  *
12  * This library is distributed in the hope that it will be useful, but WITHOUT
13  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14  * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
15  * for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public License
18  * along with this library; if not, write to the Free Software Foundation,
19  * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
20  *
21  * Initial developer(s): Emmanuel Cecchet.
22  * Contributor(s): ______________________.
23  */

24
25 package org.objectweb.cjdbc.common.sql;
26
27 import java.io.IOException JavaDoc;
28 import java.sql.SQLException JavaDoc;
29
30 import org.objectweb.cjdbc.common.sql.schema.DatabaseSchema;
31 import org.objectweb.cjdbc.common.stream.CJDBCInputStream;
32 import org.objectweb.cjdbc.common.stream.CJDBCOutputStream;
33
34 /**
35  * A <code>StoredProcedure</code> is a SQL request with the following syntax:
36  *
37  * <pre>
38  *
39  * {call &lt;procedure-name&gt;[&lt;arg1&gt;,&lt;arg2&gt;, ...]}
40  *
41  * </pre>
42  *
43  * @author <a HREF="mailto:Emmanuel.Cecchet@inria.fr">Emmanuel Cecchet </a>
44  * @version 1.0
45  */

46 public class StoredProcedure extends AbstractRequest
47 {
48   private static final long serialVersionUID = 8425933724676827694L;
49
50   /** <code>true</code> if this request might block. */
51   private transient boolean blocking = true;
52
53   private transient String JavaDoc procedureName = null;
54   // blank final for safety
55
private final boolean returnsRS;
56
57   /**
58    * Creates a new <code>StoredProcedure</code> instance.
59    *
60    * @param sqlQuery the SQL request
61    * @param escapeProcessing should the driver to escape processing before
62    * sending to the database ?
63    * @param timeout an <code>int</code> value
64    * @param lineSeparator the line separator used in the query
65    * @param isRead does the request expects a ResultSet ?
66    * @see #parse
67    */

68   public StoredProcedure(String JavaDoc sqlQuery, boolean escapeProcessing,
69       int timeout, String JavaDoc lineSeparator, boolean isRead)
70   {
71     super(sqlQuery, escapeProcessing, timeout, lineSeparator,
72         RequestType.STORED_PROCEDURE);
73     this.returnsRS = isRead;
74   }
75
76   /**
77    * @see AbstractRequest
78    */

79   public StoredProcedure(CJDBCInputStream in) throws IOException JavaDoc
80   {
81     super(in, RequestType.STORED_PROCEDURE);
82     this.returnsRS = in.readBoolean();
83     if (returnsRS)
84       receiveResultSetParams(in);
85
86   }
87
88   /**
89    * @see org.objectweb.cjdbc.common.sql.AbstractRequest#sendToStream(org.objectweb.cjdbc.common.stream.CJDBCOutputStream,
90    * boolean)
91    */

92   public void sendToStream(CJDBCOutputStream out, boolean needSkeleton)
93       throws IOException JavaDoc
94   {
95     super.sendToStream(out, needSkeleton);
96     out.writeBoolean(returnsRS);
97     if (returnsRS)
98       sendResultSetParams(out);
99   }
100
101   /**
102    * @return <code>false</code>
103    * @see org.objectweb.cjdbc.common.sql.AbstractRequest#needsMacroProcessing()
104    */

105   public boolean needsMacroProcessing()
106   {
107     return true;
108   }
109
110   /**
111    * @see org.objectweb.cjdbc.common.sql.AbstractRequest#returnsResultSet()
112    */

113   public boolean returnsResultSet()
114   {
115     return returnsRS;
116   }
117
118   /**
119    * Get the stored procedure name
120    *
121    * @return the stored procedure name
122    */

123   public String JavaDoc getProcedureName()
124   {
125     if (procedureName == null)
126       try
127       {
128         parse(null, 0, true);
129       }
130       catch (SQLException JavaDoc e)
131       {
132         return null;
133       }
134     return procedureName;
135   }
136
137   /**
138    * Tests if this request might block.
139    *
140    * @return <code>true</code> if this request might block
141    */

142   public boolean mightBlock()
143   {
144     return blocking;
145   }
146
147   /**
148    * Sets if this request might block.
149    *
150    * @param blocking a <code>boolean</code> value
151    */

152   public void setBlocking(boolean blocking)
153   {
154     this.blocking = blocking;
155   }
156
157   /**
158    * Just get the stored procedure name.
159    *
160    * @see org.objectweb.cjdbc.common.sql.AbstractRequest#parse(org.objectweb.cjdbc.common.sql.schema.DatabaseSchema,
161    * int, boolean)
162    */

163   public void parse(DatabaseSchema schema, int granularity,
164       boolean isCaseSensitive) throws SQLException JavaDoc
165   {
166     sqlQuery = sqlQuery.trim();
167     if (sqlQuery.length() < 6) // 6='call x'
168
throw new SQLException JavaDoc("Malformed stored procedure call '" + sqlQuery
169           + "'");
170
171     int parenthesis = sqlQuery.indexOf('(');
172     if (parenthesis == -1)
173       procedureName = sqlQuery.substring(5); // 5 = 'call '
174
else
175       procedureName = sqlQuery.substring(5, parenthesis); // 5 = 'call '
176
// Remove possible extra spaces between call and procedure name
177
procedureName = procedureName.trim();
178   }
179
180   /**
181    * Always throws a <code>SQLException</code>: it is useless to parse a
182    * stored procedure call since we can't know which tables are affected by this
183    * procedure.
184    *
185    * @see AbstractRequest#cloneParsing(AbstractRequest)
186    */

187   public void cloneParsing(AbstractRequest request)
188   {
189     throw new RuntimeException JavaDoc(
190         "Unable to clone the parsing of a stored procedure call");
191   }
192
193 }
Popular Tags