KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2
3    Derby - Class org.apache.derby.impl.jdbc.EmbedParameterSetMetaData
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.Activation;
25 import org.apache.derby.iapi.sql.ParameterValueSet;
26 import org.apache.derby.iapi.types.DataTypeDescriptor;
27 import org.apache.derby.iapi.types.DataTypeUtilities;
28 import org.apache.derby.iapi.reference.JDBC30Translation;
29 import org.apache.derby.iapi.reference.SQLState;
30 import org.apache.derby.iapi.jdbc.EngineParameterMetaData;
31
32 import java.sql.SQLException JavaDoc;
33 import java.sql.Types JavaDoc;
34
35 /**
36  * This class immitates to implement the ParameterMetaData interface from JDBC3.0
37  * We want to provide the functionality to JDKs before JDBC3.0. We put it here
38  * instead of in Local20 because we want to make it available for CallableStatement.
39  * It provides the parameter meta data for callable & prepared statements.
40  * The subclass in Local30 actually implements ParameterMetaData interface.
41  *
42  * For use of ParameterMetaData functionality in network server, please do not use
43  * this class directly. Instead use the method available on EnginePreparedStatement
44  * @see org.apache.derby.iapi.jdbc.EngineParameterMetaData
45  * @see org.apache.derby.iapi.jdbc.EnginePreparedStatement
46  */

47 public class EmbedParameterSetMetaData implements EngineParameterMetaData
48     {
49
50     private final ParameterValueSet pvs;
51     private final DataTypeDescriptor[] types;
52     private final int paramCount;
53
54     //////////////////////////////////////////////////////////////
55
//
56
// CONSTRUCTORS
57
//
58
//////////////////////////////////////////////////////////////
59
protected EmbedParameterSetMetaData(ParameterValueSet pvs, DataTypeDescriptor[] types) {
60         int paramCount;
61         paramCount = pvs.getParameterCount();
62         this.pvs = pvs;
63         this.paramCount = paramCount;
64         this.types = types;
65     }
66     /**
67     *
68     * Retrieves the number of parameters in the PreparedStatement object for which
69     * this ParameterMetaData object contains information.
70     *
71     * @return the number of parameters
72     */

73     public int getParameterCount() {
74         return paramCount;
75     }
76
77     /**
78     *
79     * Retrieves whether null values are allowed in the designated parameter.
80     *
81     * @param param - the first parameter is 1, the second is 2, ...
82     * @return the nullability status of the given parameter; one of
83     * ParameterMetaData.parameterNoNulls, ParameterMetaData.parameterNullable, or
84     * ParameterMetaData.parameterNullableUnknown
85     * @exception SQLException if a database access error occurs
86     */

87     public int isNullable(int param) throws SQLException JavaDoc {
88         checkPosition(param);
89
90         if (types[param - 1].isNullable())
91             return JDBC30Translation.PARAMETER_NULLABLE;
92         else
93             return JDBC30Translation.PARAMETER_NO_NULLS;
94     }
95
96     /**
97     *
98     * Retrieves whether values for the designated parameter can be signed numbers.
99     *
100     * @param param - the first parameter is 1, the second is 2, ...
101     * @return true if it can be signed numbers
102     * @exception SQLException if a database access error occurs
103     */

104     public boolean isSigned(int param) throws SQLException JavaDoc {
105         checkPosition(param);
106
107         return types[param - 1].getTypeId().isNumericTypeId();
108     }
109
110     /**
111     *
112     * Retrieves the designated parameter's number of decimal digits.
113     *
114     * @param param - the first parameter is 1, the second is 2, ...
115     * @return precision
116     * @exception SQLException if a database access error occurs
117     */

118     public int getPrecision(int param) throws SQLException JavaDoc {
119         checkPosition(param);
120
121         int outparamPrecision = -1;
122         int precision = DataTypeUtilities.getPrecision(types[param - 1]);
123        
124         if (((param == 1) && pvs.hasReturnOutputParameter()))
125         {
126             outparamPrecision = pvs.getPrecision(param);
127         }
128
129         return (outparamPrecision == -1) ? precision : outparamPrecision;
130
131     }
132         
133     /**
134     *
135     * Retrieves the designated parameter's number of digits to right of the decimal point.
136     *
137     * @param param - the first parameter is 1, the second is 2, ...
138     * @return scale
139     * @exception SQLException if a database access error occurs
140     */

141     public int getScale(int param) throws SQLException JavaDoc {
142         checkPosition(param);
143
144         if (((param == 1) && pvs.hasReturnOutputParameter()))
145             return pvs.getScale(param);
146         return types[param - 1].getScale();
147
148     }
149
150     /**
151     *
152     * Retrieves the designated parameter's SQL type.
153     *
154     * @param param - the first parameter is 1, the second is 2, ...
155     * @return SQL type from java.sql.Types
156     * @exception SQLException if a database access error occurs
157     */

158     public int getParameterType(int param) throws SQLException JavaDoc {
159         checkPosition(param);
160
161         return types[param - 1].getTypeId().getJDBCTypeId();
162     }
163
164     /**
165     *
166     * Retrieves the designated parameter's database-specific type name.
167     *
168     * @param param - the first parameter is 1, the second is 2, ...
169     * @return type the name used by the database. If the parameter
170     * type is a user-defined type, then a fully-qualified type name is returned.
171     * @exception SQLException if a database access error occurs
172     */

173     public String JavaDoc getParameterTypeName(int param) throws SQLException JavaDoc {
174         checkPosition(param);
175
176         return types[param - 1].getTypeId().getSQLTypeName();
177     }
178
179     /**
180     *
181     * Retrieves the fully-qualified name of the Java class whose instances should be
182     * passed to the method PreparedStatement.setObject.
183     *
184     * @param param - the first parameter is 1, the second is 2, ...
185     * @return the fully-qualified name of the class in the Java
186     * programming language that would be used by the method
187     * PreparedStatement.setObject to set the value in the specified parameter.
188     * This is the class name used for custom mapping.
189     * @exception SQLException if a database access error occurs
190     */

191     public String JavaDoc getParameterClassName(int param) throws SQLException JavaDoc {
192         checkPosition(param);
193
194         return types[param - 1].getTypeId().getResultSetMetaDataTypeName();
195     }
196
197     /**
198     *
199     * Retrieves the designated parameter's mode.
200     *
201     * @param param - the first parameter is 1, the second is 2, ...
202     * @return mode of the parameter; one of ParameterMetaData.parameterModeIn,
203     * ParameterMetaData.parameterModeOut, or ParameterMetaData.parameterModeInOut
204     * ParameterMetaData.parameterModeUnknown.
205     * @exception SQLException if a database access error occurs
206     */

207     public int getParameterMode(int param) throws SQLException JavaDoc {
208         checkPosition(param);
209
210         //bug 4857 - only the return parameter is of type OUT. All the other output
211
//parameter are IN_OUT (it doesn't matter if their value is set or not).
212
if ((param == 1) && pvs.hasReturnOutputParameter())//only the first parameter can be of return type
213
return JDBC30Translation.PARAMETER_MODE_OUT;
214         return pvs.getParameterMode(param);
215     }
216
217
218     // Check the position number for a parameter and throw an exception if
219
// it is out of range.
220
private void checkPosition(int parameterIndex) throws SQLException JavaDoc {
221         /* Check that the parameterIndex is in range. */
222         if (parameterIndex < 1 ||
223                 parameterIndex > paramCount) {
224
225             /* This message matches the one used by the DBMS */
226             throw Util.generateCsSQLException(
227             SQLState.LANG_INVALID_PARAM_POSITION,
228             new Integer JavaDoc(parameterIndex), new Integer JavaDoc(paramCount));
229         }
230     }
231 }
232
233
Popular Tags