KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > hsqldb > jdbc > jdbcParameterMetaData


1 /* Copyright (c) 2001-2005, The HSQL Development Group
2  * All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions are met:
6  *
7  * Redistributions of source code must retain the above copyright notice, this
8  * list of conditions and the following disclaimer.
9  *
10  * Redistributions in binary form must reproduce the above copyright notice,
11  * this list of conditions and the following disclaimer in the documentation
12  * and/or other materials provided with the distribution.
13  *
14  * Neither the name of the HSQL Development Group nor the names of its
15  * contributors may be used to endorse or promote products derived from this
16  * software without specific prior written permission.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21  * ARE DISCLAIMED. IN NO EVENT SHALL HSQL DEVELOPMENT GROUP, HSQLDB.ORG,
22  * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
23  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
24  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
25  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
26  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
28  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29  */

30
31
32 package org.hsqldb.jdbc;
33
34 import java.lang.reflect.Method JavaDoc;
35 import java.lang.reflect.Modifier JavaDoc;
36 import java.sql.ParameterMetaData JavaDoc;
37 import java.sql.SQLException JavaDoc;
38
39 import org.hsqldb.Result;
40 import org.hsqldb.Trace;
41 import org.hsqldb.Types;
42
43 // fredt@users 20040412 - removed DITypeInfo dependencies
44
// TODO: implement internal support for at least OUT return parameter
45

46 /**
47  * An object that can be used to get information about the types and
48  * properties of the parameters in a PreparedStatement object.
49  *
50  * @author boucherb@users
51  * @version 1.7.2
52  * @since JDK 1.4, HSQLDB 1.7.2
53  */

54 public class jdbcParameterMetaData implements ParameterMetaData JavaDoc {
55
56     /** The metadata object with which this object is constructed */
57     Result.ResultMetaData rmd;
58
59     /** The numeric data type codes of the parameters. */
60     int[] types;
61
62     /** Parameter mode values */
63     int[] modes;
64
65     /** whether param is assigned directly to identity column */
66     boolean[] isIdentity;
67
68     /** nullability code for site to which param is bound */
69     int[] nullability;
70
71     /**
72      * The fully-qualified name of the Java class whose instances should
73      * be passed to the method PreparedStatement.setObject. <p>
74      *
75      * Note that changes to Function.java and Types.java allow passing
76      * objects of any class implementing java.io.Serializable and that,
77      * as such, the parameter expression resolution mechanism has been
78      * upgraded to provide the precise FQN for SQL function and stored
79      * procedure arguments, rather than the more generic
80      * org.hsqldb.JavaObject class that is used internally to represent
81      * and transport objects whose class is not in the standard mapping.
82      */

83     String JavaDoc[] classNames;
84
85     /** The number of parameters in the described statement */
86     int parameterCount;
87
88     /**
89      * Creates a new instance of jdbcParameterMetaData. <p>
90      *
91      * @param r A Result object describing the statement parameters
92      * @throws SQLException never - reserved for future use
93      */

94     jdbcParameterMetaData(Result r) throws SQLException JavaDoc {
95
96         if (r == null) {
97             parameterCount = 0;
98
99             return;
100         }
101
102         rmd = r.metaData;
103         types = rmd.colTypes;
104         parameterCount = types.length;
105         nullability = rmd.colNullable;
106         isIdentity = rmd.isIdentity;
107         classNames = rmd.classNames;
108         modes = rmd.paramMode;
109     }
110
111     /**
112      * Checks if the value of the param argument is a valid parameter
113      * position. <p>
114      *
115      * @param param position to check
116      * @throws SQLException if the value of the param argument is not a
117      * valid parameter position
118      */

119     void checkRange(int param) throws SQLException JavaDoc {
120
121         if (param < 1 || param > parameterCount) {
122             String JavaDoc msg = param + " is out of range";
123
124             throw Util.sqlException(Trace.INVALID_JDBC_ARGUMENT, msg);
125         }
126     }
127
128     /**
129      * Retrieves the fully-qualified name of the Java class whose instances
130      * should be passed to the method PreparedStatement.setObject. <p>
131      *
132      * @param param the first parameter is 1, the second is 2, ...
133      * @throws SQLException if a database access error occurs
134      * @return the fully-qualified name of the class in the
135      * Java programming language that would be
136      * used by the method PreparedStatement.setObject
137      * to set the value in the specified parameter.
138      * This is the class name used for custom mapping.
139      * @since JDK 1.4, HSQLDB 1.7.2
140      */

141     public String JavaDoc getParameterClassName(int param) throws SQLException JavaDoc {
142
143         checkRange(param);
144
145         return classNames[--param];
146     }
147
148     /**
149      * Retrieves the number of parameters in the PreparedStatement object for
150      * which this ParameterMetaData object provides information. <p>
151      *
152      * @throws SQLException if a database access error occurs
153      * @return the number of parameters
154      * @since JDK 1.4, HSQLDB 1.7.2
155      */

156     public int getParameterCount() throws SQLException JavaDoc {
157         return parameterCount;
158     }
159
160     /**
161      * Retrieves the designated parameter's mode. <p>
162      *
163      * @param param the first parameter is 1, the second is 2, ...
164      * @throws SQLException if a database access error occurs
165      * @return mode of the parameter; one of
166      * ParameterMetaData.parameterModeIn,
167      * ParameterMetaData.parameterModeOut,
168      * ParameterMetaData.parameterModeInOut,
169      * ParameterMetaData.parameterModeUnknown
170      * @since JDK 1.4, HSQLDB 1.7.2
171      */

172     public int getParameterMode(int param) throws SQLException JavaDoc {
173
174         checkRange(param);
175
176         return modes[--param];
177     }
178
179     /**
180      * Retrieves the designated parameter's SQL type. <p>
181      *
182      * @param param the first parameter is 1, the second is 2, ...
183      * @throws SQLException if a database access error occurs
184      * @return SQL type from java.sql.Types
185      * @since JDK 1.4, HSQLDB 1.7.2
186      * @see java.sql.Types
187      */

188     public int getParameterType(int param) throws SQLException JavaDoc {
189
190         int t;
191
192         checkRange(param);
193
194         t = types[--param];
195
196         return t == Types.VARCHAR_IGNORECASE ? Types.VARCHAR
197                                              : t;
198     }
199
200     /**
201      * Retrieves the designated parameter's database-specific type name. <p>
202      *
203      * @param param the first parameter is 1, the second is 2, ...
204      * @throws SQLException if a database access error occurs
205      * @return type the name used by the database.
206      * If the parameter type is a user-defined
207      * type, then a fully-qualified type name is
208      * returned.
209      * @since JDK 1.4, HSQLDB 1.7.2
210      */

211     public String JavaDoc getParameterTypeName(int param) throws SQLException JavaDoc {
212
213         int t;
214         int ts;
215
216         checkRange(param);
217
218         return Types.getTypeName(types[--param]);
219     }
220
221     /**
222      * Retrieves the designated parameter's number of decimal digits. <p>
223      *
224      * @param param the first parameter is 1, the second is 2, ...
225      * @throws SQLException if a database access error occurs
226      * @return precision
227      * @since JDK 1.4, HSQLDB 1.7.2
228      */

229     public int getPrecision(int param) throws SQLException JavaDoc {
230
231         checkRange(param);
232
233         // TODO:
234
// parameters assigned directly to table columns
235
// should report the precision of the column if it is
236
// defined, otherwise the default (intrinsic) precision
237
// of the undecorated type
238
return Types.getPrecision(types[--param]);
239     }
240
241     /**
242      * Retrieves the designated parameter's number of digits to right of
243      * the decimal point. <p>
244      *
245      * @param param the first parameter is 1, the second is 2, ...
246      * @throws SQLException if a database access error occurs
247      * @return scale
248      * @since JDK 1.4, HSQLDB 1.7.2
249      */

250     public int getScale(int param) throws SQLException JavaDoc {
251
252         checkRange(param);
253
254         // TODO:
255
// parameters assigned directly to DECIMAL/NUMERIC columns
256
// should report the declared scale of the column
257
// For now, to be taken as "default or unknown"
258
return 0;
259     }
260
261     /**
262      * Retrieves whether null values are allowed in the designated parameter. <p>
263      *
264      * @param param the first parameter is 1, the second is 2, ...
265      * @throws SQLException if a database access error occurs
266      * @return the nullability status of the given parameter; one of
267      * ParameterMetaData.parameterNoNulls,
268      * ParameterMetaData.parameterNullable or
269      * ParameterMetaData.parameterNullableUnknown
270      * @since JDK 1.4, HSQLDB 1.7.2
271      */

272     public int isNullable(int param) throws SQLException JavaDoc {
273
274         checkRange(param);
275
276         return nullability[--param];
277     }
278
279     /**
280      * Retrieves whether values for the designated parameter can be
281      * signed numbers. <p>
282      *
283      * @param param the first parameter is 1, the second is 2, ...
284      * @throws SQLException if a database access error occurs
285      * @return true if so; false otherwise
286      * @since JDK 1.4, HSQLDB 1.7.2
287      */

288     public boolean isSigned(int param) throws SQLException JavaDoc {
289
290         checkRange(param);
291
292         Boolean JavaDoc b = Types.isUnsignedAttribute(types[--param]);
293
294         return b != null &&!b.booleanValue() &&!isIdentity[param];
295     }
296
297     /**
298      * Retrieves a String repsentation of this object. <p>
299      *
300      * @return a String repsentation of this object
301      */

302     public String JavaDoc toString() {
303
304         try {
305             return toStringImpl();
306         } catch (Throwable JavaDoc t) {
307             return super.toString() + "[toStringImpl_exception=" + t + "]";
308         }
309     }
310
311     /**
312      * Provides the implementation of the toString() method. <p>
313      *
314      * @return a String representation of this object
315      * @throws Exception if a reflection error occurs
316      */

317     private String JavaDoc toStringImpl() throws Exception JavaDoc {
318
319         StringBuffer JavaDoc sb;
320         Method JavaDoc[] methods;
321         Method JavaDoc method;
322         int count;
323
324         sb = new StringBuffer JavaDoc();
325
326         sb.append(super.toString());
327
328         count = getParameterCount();
329
330         if (count == 0) {
331             sb.append("[parameterCount=0]");
332
333             return sb.toString();
334         }
335
336         methods = getClass().getDeclaredMethods();
337
338         sb.append('[');
339
340         int len = methods.length;
341
342         for (int i = 0; i < count; i++) {
343             sb.append('\n');
344             sb.append(" parameter_");
345             sb.append(i + 1);
346             sb.append('=');
347             sb.append('[');
348
349             for (int j = 0; j < len; j++) {
350                 method = methods[j];
351
352                 if (!Modifier.isPublic(method.getModifiers())) {
353                     continue;
354                 }
355
356                 if (method.getParameterTypes().length != 1) {
357                     continue;
358                 }
359
360                 sb.append(method.getName());
361                 sb.append('=');
362                 sb.append(method.invoke(this,
363                                         new Object JavaDoc[]{ new Integer JavaDoc(i + 1) }));
364
365                 if (j + 1 < len) {
366                     sb.append(',');
367                     sb.append(' ');
368                 }
369             }
370
371             sb.append(']');
372
373             if (i + 1 < count) {
374                 sb.append(',');
375                 sb.append(' ');
376             }
377         }
378
379         sb.append('\n');
380         sb.append(']');
381
382         return sb.toString();
383     }
384 }
385
Popular Tags