KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > derby > impl > sql > GenericParameter


1 /*
2
3    Derby - Class org.apache.derby.impl.sql.GenericParameter
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.sql;
23
24 import org.apache.derby.iapi.services.loader.ClassInspector;
25
26 import org.apache.derby.iapi.sql.ParameterValueSet;
27
28 import org.apache.derby.iapi.types.DataValueDescriptor;
29 import org.apache.derby.iapi.types.BooleanDataValue;
30 import org.apache.derby.iapi.types.BitDataValue;
31 import org.apache.derby.iapi.types.DateTimeDataValue;
32 import org.apache.derby.iapi.types.NumberDataValue;
33 import org.apache.derby.iapi.types.StringDataValue;
34 import org.apache.derby.iapi.types.UserDataValue;
35 import org.apache.derby.iapi.types.TypeId;
36 import org.apache.derby.iapi.types.DataTypeDescriptor;
37 import org.apache.derby.iapi.types.*;
38
39 import org.apache.derby.iapi.reference.SQLState;
40
41 import org.apache.derby.iapi.reference.JDBC30Translation;
42
43 import org.apache.derby.iapi.error.StandardException;
44
45 import org.apache.derby.iapi.services.sanity.SanityManager;
46
47 import org.apache.derby.iapi.types.*;
48 import org.apache.derby.iapi.types.*;
49
50 import java.sql.Types JavaDoc;
51
52 import java.lang.reflect.Array JavaDoc;
53
54 /**
55  * A parameter. Originally lifted from ParameterValueSet.
56  *
57  * @author jamie
58  */

59 final class GenericParameter
60 {
61
62     // These defaults match the Network Server/ JCC max precision and
63
// The JCC "guessed" scale. They are used as the defaults for
64
// Decimal out params.
65
private static int DECIMAL_PARAMETER_DEFAULT_PRECISION = 31;
66     private static int DECIMAL_PARAMETER_DEFAULT_SCALE = 15;
67
68
69     /*
70     ** The parameter set we are part of
71     */

72     private final GenericParameterValueSet pvs;
73
74     /**
75     ** Our value
76     */

77     private DataValueDescriptor value;
78
79     /**
80         Compile time JDBC type identifier.
81     */

82     int jdbcTypeId;
83
84     /**
85         Compile time Java class name.
86     */

87     String JavaDoc declaredClassName;
88
89     /**
90         Mode of the parameter, from ParameterMetaData
91     */

92     short parameterMode;
93
94     /*
95     ** If we are set
96     */

97     boolean isSet;
98
99     /*
100     ** Output parameter values
101     */

102     private final boolean isReturnOutputParameter;
103
104     /**
105         Type that has been registered.
106     */

107     int registerOutType = Types.NULL;
108     /**
109         Scale that has been registered.
110     */

111     int registerOutScale = -1;
112
113     /**
114      * When a decimal output parameter is registered we give it a
115      * precision
116      */

117
118     int registerOutPrecision = -1;
119
120     /**
121      * Constructor for a Parameter
122      *
123      * @param pvs the parameter set that this is part of
124      * @param isReturnOutputParameter true if this is a return output parameter
125      */

126     GenericParameter
127     (
128         GenericParameterValueSet pvs,
129         boolean isReturnOutputParameter
130     )
131     {
132         this.pvs = pvs;
133         parameterMode = (this.isReturnOutputParameter = isReturnOutputParameter)
134             ? (short) JDBC30Translation.PARAMETER_MODE_OUT : (short) JDBC30Translation.PARAMETER_MODE_IN;
135     }
136
137     /**
138      * Clone myself. It is a shallow copy for everything but
139      * the underlying data wrapper and its value -- e.g. for
140      * everything but the underlying SQLInt and its int.
141      *
142      * @param pvs the parameter value set
143      *
144      * @return a new generic parameter.
145      */

146     public GenericParameter getClone(GenericParameterValueSet pvs)
147     {
148         GenericParameter gpClone = new GenericParameter(pvs, isReturnOutputParameter);
149         gpClone.initialize(this.getValue().getClone(), jdbcTypeId, declaredClassName);
150         gpClone.isSet = true;
151
152         return gpClone;
153     }
154
155     /**
156      * Set the DataValueDescriptor and type information for this parameter
157      *
158      */

159     void initialize(DataValueDescriptor value, int jdbcTypeId, String JavaDoc className)
160     {
161         this.value = value;
162         this.jdbcTypeId = jdbcTypeId;
163         this.declaredClassName = className;
164     }
165
166
167     /**
168      * Clear the parameter, unless it is a return
169      * output parameter
170      */

171     void clear()
172     {
173         isSet = false;
174     }
175
176
177     /**
178      * Get the parameter value. Doesn't check to
179      * see if it has been initialized or not.
180      *
181      * @return the parameter value, may return null
182      */

183     DataValueDescriptor getValue()
184     {
185         return value;
186     }
187
188
189     //////////////////////////////////////////////////////////////////
190
//
191
// CALLABLE STATEMENT
192
//
193
//////////////////////////////////////////////////////////////////
194

195     /**
196      * Mark the parameter as an output parameter.
197      *
198      * @param sqlType A type from java.sql.Types
199      * @param scale scale, -1 if no scale arg
200      *
201      * @exception StandardException on error
202      */

203     void setOutParameter(int sqlType, int scale)
204         throws StandardException
205     {
206         // fast case duplicate registrations.
207
if (registerOutType == sqlType) {
208             if (scale == registerOutScale)
209                 return;
210         }
211
212         switch (parameterMode) {
213         case JDBC30Translation.PARAMETER_MODE_IN:
214         case JDBC30Translation.PARAMETER_MODE_UNKNOWN:
215         default:
216             throw StandardException.newException(SQLState.LANG_NOT_OUT_PARAM, getJDBCParameterNumberStr());
217
218         case JDBC30Translation.PARAMETER_MODE_IN_OUT:
219         case JDBC30Translation.PARAMETER_MODE_OUT:
220             // Declared/Java procedure parameter.
221
if (!DataTypeDescriptor.isJDBCTypeEquivalent(jdbcTypeId, sqlType))
222                 throw throwInvalidOutParamMap(sqlType);
223             break;
224
225         }
226
227         registerOutType = sqlType;
228         
229     }
230
231     private StandardException throwInvalidOutParamMap(int sqlType) {
232
233         //TypeId typeId = TypeId.getBuiltInTypeId(sqlType);
234
// String sqlTypeName = typeId == null ? "OTHER" : typeId.getSQLTypeName();
235

236
237         String JavaDoc jdbcTypesName = org.apache.derby.impl.jdbc.Util.typeName(sqlType);
238
239         TypeId typeId = TypeId.getBuiltInTypeId(jdbcTypeId);
240         String JavaDoc thisTypeName = typeId == null ? declaredClassName : typeId.getSQLTypeName();
241                 
242         StandardException e = StandardException.newException(SQLState.LANG_INVALID_OUT_PARAM_MAP,
243                     getJDBCParameterNumberStr(),
244                     jdbcTypesName, thisTypeName);
245
246         return e;
247     }
248
249
250
251     /**
252      * Validate the parameters. This is done for situations where
253      * we cannot validate everything in the setXXX() calls. In
254      * particular, before we do an execute() on a CallableStatement,
255      * we need to go through the parameters and make sure that
256      * all parameters are set up properly. The motivator for this
257      * is that setXXX() can be called either before or after
258      * registerOutputParamter(), we cannot be sure we have the types
259      * correct until we get to execute().
260      *
261      * @exception StandardException if the parameters aren't valid
262      */

263     void validate() throws StandardException
264     {
265         switch (parameterMode) {
266         case JDBC30Translation.PARAMETER_MODE_UNKNOWN:
267             break;
268         case JDBC30Translation.PARAMETER_MODE_IN:
269             break;
270         case JDBC30Translation.PARAMETER_MODE_IN_OUT:
271         case JDBC30Translation.PARAMETER_MODE_OUT:
272             if (registerOutType == Types.NULL) {
273                 throw StandardException.newException(SQLState.NEED_TO_REGISTER_PARAM,
274                     getJDBCParameterNumberStr(),
275                      org.apache.derby.catalog.types.RoutineAliasInfo.parameterMode(parameterMode));
276             }
277             break;
278         }
279     }
280
281     /**
282      * Return the scale of the parameter.
283      *
284      * @return scale
285      */

286     int getScale()
287     {
288         //when the user doesn't pass any scale, the registerOutScale gets set to -1
289
return (registerOutScale == -1 ? 0 : registerOutScale);
290     }
291
292
293     int getPrecision()
294     {
295         return registerOutPrecision;
296         
297     }
298
299     ////////////////////////////////////////////////////
300
//
301
// CLASS IMPLEMENTATION
302
//
303
////////////////////////////////////////////////////
304

305     /**
306      * get string for param number
307      */

308     String JavaDoc getJDBCParameterNumberStr()
309     {
310         return Integer.toString(pvs.getParameterNumber(this));
311     }
312
313     public String JavaDoc toString()
314     {
315         /* This method is used for debugging.
316          * It is called when derby.language.logStatementText=true,
317          * so there is no check of SanityManager.DEBUG.
318          * Anyway, we need to call value.getString() instead of
319          * value.toString() because the user may have done a
320          * a setStream() on the parameter. (toString() could get
321          * an assertion failure in that case as it would be in an
322          * unexpected state since this is a very weird codepath.)
323          * getString() can throw an exception which we eat and
324          * and reflect in the returned string.
325          */

326         if (value == null)
327         {
328             return "null";
329         }
330         else
331         {
332             try
333             {
334                 return value.getTraceString();
335             }
336             catch (StandardException se)
337             {
338                 return "unexpected exception from getTraceString() - " + se;
339             }
340         }
341     }
342 }
343
Popular Tags