1 | /* |
2 | |
3 | Derby - Class org.apache.derby.impl.jdbc.EmbedParameterSetMetaData |
4 | |
5 | Copyright 2002, 2004 The Apache Software Foundation or its licensors, as applicable. |
6 | |
7 | Licensed under the Apache License, Version 2.0 (the "License"); |
8 | you may not use this file except in compliance with the License. |
9 | You may obtain a copy of the License at |
10 | |
11 | http://www.apache.org/licenses/LICENSE-2.0 |
12 | |
13 | Unless required by applicable law or agreed to in writing, software |
14 | distributed under the License is distributed on an "AS IS" BASIS, |
15 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
16 | See the License for the specific language governing permissions and |
17 | limitations under the License. |
18 | |
19 | */ |
20 | |
21 | package org.apache.derby.impl.jdbc; |
22 | |
23 | import org.apache.derby.iapi.sql.Activation; |
24 | import org.apache.derby.iapi.sql.ParameterValueSet; |
25 | import org.apache.derby.iapi.types.DataTypeDescriptor; |
26 | import org.apache.derby.iapi.types.DataTypeUtilities; |
27 | import org.apache.derby.iapi.reference.JDBC30Translation; |
28 | import org.apache.derby.iapi.reference.SQLState; |
29 | |
30 | import java.sql.SQLException; |
31 | import java.sql.Types; |
32 | |
33 | /** |
34 | * This class immitates to implement the ParameterMetaData interface from JDBC3.0 |
35 | * We want to provide the functionality to JDKs before JDBC3.0. We put it here |
36 | * instead of in Local20 because we want to make it available for CallableStatement. |
37 | * It provides the parameter meta data for callable & prepared statements. |
38 | * The subclass in Local30 actually implements ParameterMetaData interface. |
39 | * |
40 | * Our middle-tier servers or tools (eg. drda network server) can use it this way: |
41 | * import org.apache.derby.impl.jdbc.EmbedPreparedStatement; |
42 | * import org.apache.derby.impl.jdbc.EmbedParameterSetMetaData; |
43 | * |
44 | * EmbedParameterSetMetaData pmeta = ((EmbedPreparedStatement) ps).getEmbedParameterSetMetaData(); |
45 | */ |
46 | public class EmbedParameterSetMetaData |
47 | { |
48 | |
49 | private final ParameterValueSet pvs; |
50 | private final DataTypeDescriptor[] types; |
51 | private final int paramCount; |
52 | |
53 | ////////////////////////////////////////////////////////////// |
54 | // |
55 | // CONSTRUCTORS |
56 | // |
57 | ////////////////////////////////////////////////////////////// |
58 | protected EmbedParameterSetMetaData(ParameterValueSet pvs, DataTypeDescriptor[] types) { |
59 | int paramCount; |
60 | paramCount = pvs.getParameterCount(); |
61 | this.pvs = pvs; |
62 | this.paramCount = paramCount; |
63 | this.types = types; |
64 | } |
65 | /** |
66 | * |
67 | * Retrieves the number of parameters in the PreparedStatement object for which |
68 | * this ParameterMetaData object contains information. |
69 | * |
70 | * @return the number of parameters |
71 | */ |
72 | public int getParameterCount() { |
73 | return paramCount; |
74 | } |
75 | |
76 | /** |
77 | * |
78 | * Retrieves whether null values are allowed in the designated parameter. |
79 | * |
80 | * @param param - the first parameter is 1, the second is 2, ... |
81 | * @return the nullability status of the given parameter; one of |
82 | * ParameterMetaData.parameterNoNulls, ParameterMetaData.parameterNullable, or |
83 | * ParameterMetaData.parameterNullableUnknown |
84 | * @exception SQLException if a database access error occurs |
85 | */ |
86 | public int isNullable(int param) throws SQLException { |
87 | checkPosition(param); |
88 | |
89 | if (types[param - 1].isNullable()) |
90 | return JDBC30Translation.PARAMETER_NULLABLE; |
91 | else |
92 | return JDBC30Translation.PARAMETER_NO_NULLS; |
93 | } |
94 | |
95 | /** |
96 | * |
97 | * Retrieves whether values for the designated parameter can be signed numbers. |
98 | * |
99 | * @param param - the first parameter is 1, the second is 2, ... |
100 | * @return true if it can be signed numbers |
101 | * @exception SQLException if a database access error occurs |
102 | */ |
103 | public boolean isSigned(int param) throws SQLException { |
104 | checkPosition(param); |
105 | |
106 | return types[param - 1].getTypeId().isNumericTypeId(); |
107 | } |
108 | |
109 | /** |
110 | * |
111 | * Retrieves the designated parameter's number of decimal digits. |
112 | * |
113 | * @param param - the first parameter is 1, the second is 2, ... |
114 | * @return precision |
115 | * @exception SQLException if a database access error occurs |
116 | */ |
117 | public int getPrecision(int param) throws SQLException { |
118 | checkPosition(param); |
119 | |
120 | int outparamPrecision = -1; |
121 | int precision = DataTypeUtilities.getPrecision(types[param - 1]); |
122 | |
123 | if (((param == 1) && pvs.hasReturnOutputParameter())) |
124 | { |
125 | outparamPrecision = pvs.getPrecision(param); |
126 | } |
127 | |
128 | return (outparamPrecision == -1) ? precision : outparamPrecision; |
129 | |
130 | } |
131 | |
132 | /** |
133 | * |
134 | * Retrieves the designated parameter's number of digits to right of the decimal point. |
135 | * |
136 | * @param param - the first parameter is 1, the second is 2, ... |
137 | * @return scale |
138 | * @exception SQLException if a database access error occurs |
139 | */ |
140 | public int getScale(int param) throws SQLException { |
141 | checkPosition(param); |
142 | |
143 | if (((param == 1) && pvs.hasReturnOutputParameter())) |
144 | return pvs.getScale(param); |
145 | return types[param - 1].getScale(); |
146 | |
147 | } |
148 | |
149 | /** |
150 | * |
151 | * Retrieves the designated parameter's SQL type. |
152 | * |
153 | * @param param - the first parameter is 1, the second is 2, ... |
154 | * @return SQL type from java.sql.Types |
155 | * @exception SQLException if a database access error occurs |
156 | */ |
157 | public int getParameterType(int param) throws SQLException { |
158 | checkPosition(param); |
159 | |
160 | return types[param - 1].getTypeId().getJDBCTypeId(); |
161 | } |
162 | |
163 | /** |
164 | * |
165 | * Retrieves the designated parameter's database-specific type name. |
166 | * |
167 | * @param param - the first parameter is 1, the second is 2, ... |
168 | * @return type the name used by the database. If the parameter |
169 | * type is a user-defined type, then a fully-qualified type name is returned. |
170 | * @exception SQLException if a database access error occurs |
171 | */ |
172 | public String getParameterTypeName(int param) throws SQLException { |
173 | checkPosition(param); |
174 | |
175 | return types[param - 1].getTypeId().getSQLTypeName(); |
176 | } |
177 | |
178 | /** |
179 | * |
180 | * Retrieves the fully-qualified name of the Java class whose instances should be |
181 | * passed to the method PreparedStatement.setObject. |
182 | * |
183 | * @param param - the first parameter is 1, the second is 2, ... |
184 | * @return the fully-qualified name of the class in the Java |
185 | * programming language that would be used by the method |
186 | * PreparedStatement.setObject to set the value in the specified parameter. |
187 | * This is the class name used for custom mapping. |
188 | * @exception SQLException if a database access error occurs |
189 | */ |
190 | public String getParameterClassName(int param) throws SQLException { |
191 | checkPosition(param); |
192 | |
193 | return types[param - 1].getTypeId().getResultSetMetaDataTypeName(); |
194 | } |
195 | |
196 | /** |
197 | * |
198 | * Retrieves the designated parameter's mode. |
199 | * |
200 | * @param param - the first parameter is 1, the second is 2, ... |
201 | * @return mode of the parameter; one of ParameterMetaData.parameterModeIn, |
202 | * ParameterMetaData.parameterModeOut, or ParameterMetaData.parameterModeInOut |
203 | * ParameterMetaData.parameterModeUnknown. |
204 | * @exception SQLException if a database access error occurs |
205 | */ |
206 | public int getParameterMode(int param) throws SQLException { |
207 | checkPosition(param); |
208 | |
209 | //bug 4857 - only the return parameter is of type OUT. All the other output |
210 | //parameter are IN_OUT (it doesn't matter if their value is set or not). |
211 | if ((param == 1) && pvs.hasReturnOutputParameter())//only the first parameter can be of return type |
212 | return JDBC30Translation.PARAMETER_MODE_OUT; |
213 | return pvs.getParameterMode(param); |
214 | } |
215 | |
216 | |
217 | // Check the position number for a parameter and throw an exception if |
218 | // it is out of range. |
219 | private void checkPosition(int parameterIndex) throws SQLException { |
220 | /* Check that the parameterIndex is in range. */ |
221 | if (parameterIndex < 1 || |
222 | parameterIndex > paramCount) { |
223 | |
224 | /* This message matches the one used by the DBMS */ |
225 | throw Util.generateCsSQLException( |
226 | SQLState.LANG_INVALID_PARAM_POSITION, |
227 | new Integer(parameterIndex), new Integer(paramCount)); |
228 | } |
229 | } |
230 | } |
231 | |