KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > derby > impl > jdbc > EmbedCallableStatement


1 /*
2
3    Derby - Class org.apache.derby.impl.jdbc.EmbedCallableStatement
4
5    Licensed to the Apache Software Foundation (ASF) under one or more
6    contributor license agreements. See the NOTICE file distributed with
7    this work for additional information regarding copyright ownership.
8    The ASF licenses this file to you under the Apache License, Version 2.0
9    (the "License"); you may not use this file except in compliance with
10    the License. You may obtain a copy of the License at
11
12       http://www.apache.org/licenses/LICENSE-2.0
13
14    Unless required by applicable law or agreed to in writing, software
15    distributed under the License is distributed on an "AS IS" BASIS,
16    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17    See the License for the specific language governing permissions and
18    limitations under the License.
19
20  */

21
22 package org.apache.derby.impl.jdbc;
23
24 import org.apache.derby.iapi.sql.ParameterValueSet;
25 import org.apache.derby.iapi.sql.Activation;
26 import org.apache.derby.iapi.error.StandardException;
27 import org.apache.derby.iapi.types.DataValueDescriptor;
28
29 import org.apache.derby.iapi.services.sanity.SanityManager;
30
31 import org.apache.derby.iapi.reference.JDBC30Translation;
32 import org.apache.derby.iapi.reference.SQLState;
33
34 import java.net.URL JavaDoc;
35 import java.sql.Blob JavaDoc;
36 import java.sql.CallableStatement JavaDoc;
37 import java.sql.Clob JavaDoc;
38 import java.sql.SQLException JavaDoc;
39 import java.sql.Date JavaDoc;
40 import java.sql.Time JavaDoc;
41 import java.sql.Timestamp JavaDoc;
42 import java.util.Calendar JavaDoc;
43
44 /**
45  * Local implementation.
46  *
47  * @author ames
48  */

49 public abstract class EmbedCallableStatement extends EmbedPreparedStatement
50     implements CallableStatement JavaDoc
51 {
52     /*
53     ** True if we are of the form ? = CALL() -- i.e. true
54     ** if we have a return output parameter.
55     */

56     private boolean hasReturnOutputParameter;
57
58     protected boolean wasNull;
59
60     /**
61      * @exception SQLException thrown on failure
62      */

63     public EmbedCallableStatement (EmbedConnection conn, String JavaDoc sql,
64                                    int resultSetType,
65                                    int resultSetConcurrency,
66                                    int resultSetHoldability)
67         throws SQLException JavaDoc
68     {
69         super(conn, sql, false,
70               resultSetType,
71               resultSetConcurrency,
72               resultSetHoldability,
73               JDBC30Translation.NO_GENERATED_KEYS,
74               null,
75               null);
76
77         // mark our parameters as for a callable statement
78
ParameterValueSet pvs = getParms();
79
80         // do we have a return parameter?
81
hasReturnOutputParameter = pvs.hasReturnOutputParameter();
82     }
83
84     protected void checkRequiresCallableStatement(Activation activation) {
85     }
86
87     protected final boolean executeStatement(Activation a,
88                      boolean executeQuery, boolean executeUpdate)
89         throws SQLException JavaDoc
90     {
91         // need this additional check (it's also in the super.executeStatement
92
// to ensure we have an activation for the getParams
93
checkExecStatus();
94         synchronized (getConnectionSynchronization())
95         {
96             wasNull = false;
97             ParameterValueSet pvs = getParms();
98             try
99             {
100                 pvs.validate();
101
102             } catch (StandardException e)
103             {
104                 throw EmbedResultSet.noStateChangeException(e);
105             }
106
107             /* KLUDGE - ? = CALL ... returns a ResultSet(). We
108              * need executeUpdate to be false in that case.
109              */

110             boolean execResult = super.executeStatement(a, executeQuery,
111                 (executeUpdate && (! hasReturnOutputParameter)));
112
113             /*
114             ** If we have a return parameter, then we
115             ** consume it from the returned ResultSet
116             ** reset the ResultSet set to null.
117             */

118             if (hasReturnOutputParameter)
119             {
120                 if (SanityManager.DEBUG)
121                 {
122                     SanityManager.ASSERT(results!=null, "null results even though we are supposed to have a return parameter");
123                 }
124                 boolean gotRow = results.next();
125                 if (SanityManager.DEBUG)
126                 {
127                     SanityManager.ASSERT(gotRow, "the return resultSet didn't have any rows");
128                 }
129
130                 try
131                 {
132                     DataValueDescriptor returnValue = pvs.getReturnValueForSet();
133                     returnValue.setValueFromResultSet(results, 1, true);
134                 } catch (StandardException e)
135                 {
136                     throw EmbedResultSet.noStateChangeException(e);
137                 }
138                 finally {
139                     results = null;
140                 }
141
142                 // This is a form of ? = CALL which current is not a procedure call.
143
// Thus there cannot be any user result sets, so return false. execResult
144
// is set to true since a result set was returned, for the return parameter.
145
execResult = false;
146             }
147             return execResult;
148         }
149     }
150
151     /*
152     * CallableStatement interface
153     * (the PreparedStatement part implemented by EmbedPreparedStatement)
154     */

155
156     /**
157      * @see CallableStatement#registerOutParameter
158      * @exception SQLException NoOutputParameters thrown.
159      */

160     public final void registerOutParameter(int parameterIndex, int sqlType)
161         throws SQLException JavaDoc
162     {
163         checkStatus();
164
165         try {
166             getParms().registerOutParameter(parameterIndex-1, sqlType, -1);
167         } catch (StandardException e)
168         {
169             throw EmbedResultSet.noStateChangeException(e);
170         }
171     }
172
173     /**
174      * @see CallableStatement#registerOutParameter
175      * @exception SQLException NoOutputParameters thrown.
176      */

177     public final void registerOutParameter(int parameterIndex, int sqlType, int scale)
178         throws SQLException JavaDoc
179     {
180         checkStatus();
181
182         if (scale < 0)
183             throw newSQLException(SQLState.BAD_SCALE_VALUE, new Integer JavaDoc(scale));
184         try {
185             getParms().registerOutParameter(parameterIndex-1, sqlType, scale);
186         } catch (StandardException e)
187         {
188             throw EmbedResultSet.noStateChangeException(e);
189         }
190
191     }
192
193
194     /**
195      * JDBC 2.0
196      *
197      * Registers the designated output parameter
198      *
199      * @exception SQLException if a database-access error occurs.
200      */

201     public void registerOutParameter(int parameterIndex, int sqlType,
202                                      String JavaDoc typeName)
203          throws SQLException JavaDoc
204     {
205         throw Util.notImplemented("registerOutParameter");
206     }
207          
208  
209
210     /**
211      * @see CallableStatement#wasNull
212      * @exception SQLException NoOutputParameters thrown.
213      */

214     public boolean wasNull() throws SQLException JavaDoc
215     {
216         checkStatus();
217         return wasNull;
218     }
219
220     /**
221      * @see CallableStatement#getString
222      * @exception SQLException NoOutputParameters thrown.
223      */

224     public String JavaDoc getString(int parameterIndex) throws SQLException JavaDoc
225     {
226         checkStatus();
227         try {
228             String JavaDoc v = getParms().getParameterForGet(parameterIndex-1).getString();
229             wasNull = (v == null);
230             return v;
231
232         } catch (StandardException e)
233         {
234             throw EmbedResultSet.noStateChangeException(e);
235         }
236     }
237
238     /**
239      * @see CallableStatement#getBoolean
240      * @exception SQLException NoOutputParameters thrown.
241      */

242     public boolean getBoolean(int parameterIndex) throws SQLException JavaDoc
243     {
244         checkStatus();
245         try {
246             DataValueDescriptor param = getParms().getParameterForGet(parameterIndex-1);
247             boolean v = param.getBoolean();
248             wasNull = (!v) && param.isNull();
249             return v;
250         } catch (StandardException e)
251         {
252             throw EmbedResultSet.noStateChangeException(e);
253         }
254
255     }
256
257     /**
258      * @see CallableStatement#getByte
259      * @exception SQLException NoOutputParameters thrown.
260      */

261     public byte getByte(int parameterIndex) throws SQLException JavaDoc
262     {
263         checkStatus();
264         try {
265             DataValueDescriptor param = getParms().getParameterForGet(parameterIndex-1);
266             byte b = param.getByte();
267             wasNull = (b == 0) && param.isNull();
268             return b;
269         } catch (StandardException e)
270         {
271             throw EmbedResultSet.noStateChangeException(e);
272         }
273
274     }
275
276     /**
277      * @see CallableStatement#getShort
278      * @exception SQLException NoOutputParameters thrown.
279      */

280     public short getShort(int parameterIndex) throws SQLException JavaDoc
281     {
282         checkStatus();
283         try {
284             DataValueDescriptor param = getParms().getParameterForGet(parameterIndex-1);
285             short s = param.getShort();
286             wasNull = (s == 0) && param.isNull();
287             return s;
288         } catch (StandardException e)
289         {
290             throw EmbedResultSet.noStateChangeException(e);
291         }
292
293     }
294
295     /**
296      * @see CallableStatement#getInt
297      * @exception SQLException NoOutputParameters thrown.
298      */

299     public int getInt(int parameterIndex) throws SQLException JavaDoc
300     {
301         checkStatus();
302
303         try {
304             DataValueDescriptor param = getParms().getParameterForGet(parameterIndex-1);
305             int v = param.getInt();
306             wasNull = (v == 0) && param.isNull();
307             return v;
308
309         } catch (StandardException e)
310         {
311             throw EmbedResultSet.noStateChangeException(e);
312         }
313     }
314
315     /**
316      * @see CallableStatement#getLong
317      * @exception SQLException NoOutputParameters thrown.
318      */

319     public long getLong(int parameterIndex) throws SQLException JavaDoc
320     {
321         checkStatus();
322         try {
323             DataValueDescriptor param = getParms().getParameterForGet(parameterIndex-1);
324             long v = param.getLong();
325             wasNull = (v == 0L) && param.isNull();
326             return v;
327         } catch (StandardException e)
328         {
329             throw EmbedResultSet.noStateChangeException(e);
330         }
331
332     }
333
334     /**
335      * @see CallableStatement#getFloat
336      * @exception SQLException NoOutputParameters thrown.
337      */

338     public float getFloat(int parameterIndex) throws SQLException JavaDoc
339     {
340         checkStatus();
341         try {
342             DataValueDescriptor param = getParms().getParameterForGet(parameterIndex-1);
343             float v = param.getFloat();
344             wasNull = (v == 0.0) && param.isNull();
345             return v;
346         } catch (StandardException e)
347         {
348             throw EmbedResultSet.noStateChangeException(e);
349         }
350     }
351
352     /**
353      * @see CallableStatement#getDouble
354      * @exception SQLException NoOutputParameters thrown.
355      */

356     public double getDouble(int parameterIndex) throws SQLException JavaDoc
357     {
358         checkStatus();
359         try {
360             DataValueDescriptor param = getParms().getParameterForGet(parameterIndex-1);
361             double v = param.getDouble();
362             wasNull = (v == 0.0) && param.isNull();
363             return v;
364         } catch (StandardException e)
365         {
366             throw EmbedResultSet.noStateChangeException(e);
367         }
368
369     }
370
371     /**
372      * @see CallableStatement#getBytes
373      * @exception SQLException NoOutputParameters thrown.
374      */

375     public byte[] getBytes(int parameterIndex) throws SQLException JavaDoc
376     {
377         checkStatus();
378         try {
379             byte[] v = getParms().getParameterForGet(parameterIndex-1).getBytes();
380             wasNull = (v == null);
381             return v;
382         } catch (StandardException e)
383         {
384             throw EmbedResultSet.noStateChangeException(e);
385         }
386
387     }
388
389     /**
390      * @see CallableStatement#getDate
391      * @exception SQLException NoOutputParameters thrown.
392      */

393     public Date JavaDoc getDate(int parameterIndex) throws SQLException JavaDoc
394     {
395         checkStatus();
396         try {
397             Date JavaDoc v = getParms().getParameterForGet(parameterIndex-1).getDate(getCal());
398             wasNull = (v == null);
399             return v;
400         } catch (StandardException e)
401         {
402             throw EmbedResultSet.noStateChangeException(e);
403         }
404
405     }
406
407     /**
408      * @see CallableStatement#getTime
409      * @exception SQLException NoOutputParameters thrown.
410      */

411     public Time JavaDoc getTime(int parameterIndex) throws SQLException JavaDoc
412     {
413         checkStatus();
414         try {
415             Time JavaDoc v = getParms().getParameterForGet(parameterIndex-1).getTime(getCal());
416             wasNull = (v == null);
417             return v;
418         } catch (StandardException e)
419         {
420             throw EmbedResultSet.noStateChangeException(e);
421         }
422
423     }
424
425     /**
426      * @see CallableStatement#getTimestamp
427      * @exception SQLException NoOutputParameters thrown.
428      */

429     public Timestamp JavaDoc getTimestamp(int parameterIndex)
430         throws SQLException JavaDoc
431     {
432         checkStatus();
433         try {
434             Timestamp JavaDoc v = getParms().getParameterForGet(parameterIndex-1).getTimestamp(getCal());
435             wasNull = (v == null);
436             return v;
437         } catch (StandardException e)
438         {
439             throw EmbedResultSet.noStateChangeException(e);
440         }
441     }
442     /**
443      * Get the value of a SQL DATE parameter as a java.sql.Date object
444      *
445      * @param parameterIndex the first parameter is 1, the second is 2, ...
446      * @return the parameter value; if the value is SQL NULL, the result is
447      * null
448      * @exception SQLException if a database-access error occurs.
449      */

450     public java.sql.Date JavaDoc getDate(int parameterIndex, Calendar JavaDoc cal)
451       throws SQLException JavaDoc
452     {
453         return getDate(parameterIndex);
454     }
455
456     /**
457      * Get the value of a SQL TIME parameter as a java.sql.Time object.
458      *
459      * @param parameterIndex the first parameter is 1, the second is 2, ...
460      * @return the parameter value; if the value is SQL NULL, the result is
461      * null
462      * @exception SQLException if a database-access error occurs.
463      */

464     public java.sql.Time JavaDoc getTime(int parameterIndex, Calendar JavaDoc cal)
465       throws SQLException JavaDoc
466     {
467         return getTime(parameterIndex);
468     }
469
470     /**
471      * Get the value of a SQL TIMESTAMP parameter as a java.sql.Timestamp
472      * object.
473      *
474      * @param parameterIndex the first parameter is 1, the second is 2, ...
475      * @return the parameter value; if the value is SQL NULL, the result is
476      * null
477      * @exception SQLException if a database-access error occurs.
478      */

479     public java.sql.Timestamp JavaDoc getTimestamp(int parameterIndex, Calendar JavaDoc cal)
480       throws SQLException JavaDoc
481     {
482         return getTimestamp(parameterIndex);
483     }
484
485     /**
486      * @see CallableStatement#getObject
487      * @exception SQLException NoOutputParameters thrown.
488      */

489     public final Object JavaDoc getObject(int parameterIndex) throws SQLException JavaDoc
490     {
491         checkStatus();
492         try {
493             Object JavaDoc v = getParms().getParameterForGet(parameterIndex-1).getObject();
494             wasNull = (v == null);
495             return v;
496
497         } catch (StandardException e)
498         {
499             throw EmbedResultSet.noStateChangeException(e);
500         }
501     }
502     /**
503         * JDBC 3.0
504         *
505         * Retrieve the value of the designated JDBC DATALINK parameter as a java.net.URL object
506         *
507         * @param parameterIndex - the first parameter is 1, the second is 2
508         * @return a java.net.URL object that represents the JDBC DATALINK value used as
509         * the designated parameter
510         * @exception SQLException Feature not implemented for now.
511         */

512         public URL JavaDoc getURL(int parameterIndex)
513         throws SQLException JavaDoc
514         {
515             throw Util.notImplemented();
516         }
517
518         /**
519         * JDBC 3.0
520         *
521         * Sets the designated parameter to the given java.net.URL object. The driver
522         * converts this to an SQL DATALINK value when it sends it to the database.
523         *
524         * @param parameterName - the name of the parameter
525         * @param val - the parameter value
526         * @exception SQLException Feature not implemented for now.
527         */

528         public void setURL(String JavaDoc parameterName, URL JavaDoc val)
529         throws SQLException JavaDoc
530         {
531             throw Util.notImplemented();
532         }
533
534         /**
535         * JDBC 3.0
536         *
537         * Retrieves the value of a JDBC DATALINK parameter as a java.net.URL object
538         *
539         * @param parameterName - the name of the parameter
540         * @return the parameter value. If the value is SQL NULL, the result is null.
541         * @exception SQLException Feature not implemented for now.
542         */

543         public URL JavaDoc getURL(String JavaDoc parameterName)
544         throws SQLException JavaDoc
545         {
546             throw Util.notImplemented();
547         }
548
549         /**
550          * JDBC 2.0
551          *
552          * Get a BLOB OUT parameter.
553          *
554          * @param i the first parameter is 1, the second is 2, ...
555          * @return an object representing a BLOB
556          * @exception SQLException if a database-access error occurs.
557          */

558         public Blob JavaDoc getBlob (int i) throws SQLException JavaDoc {
559             throw Util.notImplemented();
560         }
561
562         /**
563          * JDBC 2.0
564          *
565          * Get a CLOB OUT parameter.
566          *
567          * @param i the first parameter is 1, the second is 2, ...
568          * @return an object representing a CLOB
569          * @exception SQLException if a database-access error occurs.
570          */

571         public Clob JavaDoc getClob (int i) throws SQLException JavaDoc {
572             throw Util.notImplemented();
573         }
574     public void addBatch() throws SQLException JavaDoc {
575
576         checkStatus();
577         ParameterValueSet pvs = getParms();
578
579         int numberOfParameters = pvs.getParameterCount();
580
581         for (int j=1; j<=numberOfParameters; j++) {
582
583             switch (pvs.getParameterMode(j)) {
584             case JDBC30Translation.PARAMETER_MODE_IN:
585             case JDBC30Translation.PARAMETER_MODE_UNKNOWN:
586                 break;
587             case JDBC30Translation.PARAMETER_MODE_OUT:
588             case JDBC30Translation.PARAMETER_MODE_IN_OUT:
589                 throw newSQLException(SQLState.OUTPUT_PARAMS_NOT_ALLOWED);
590             }
591         }
592
593         super.addBatch();
594     }
595 }
596
597
Popular Tags