File D:\maven-qcr-plugin-testsunitaires\qcr4java\h2-1.3.170-sources\target\site\qc\lecode\jdbc\JdbcParameterMetaData.java.html

1 /*
2 * Copyright 2004-2011 H2 Group. Multiple-Licensed under the H2 License,
3 * Version 1.0, and under the Eclipse Public License, Version 1.0
4 * (http://h2database.com/html/license.html).
5 * Initial Developer: H2 Group
6 */
7 package org.h2.jdbc;
8
9 import java.sql.ParameterMetaData;
10 import java.sql.SQLException;
11 import java.util.ArrayList;
12 import org.h2.command.CommandInterface;
13 import org.h2.expression.ParameterInterface;
14 import org.h2.message.DbException;
15 import org.h2.message.Trace;
16 import org.h2.message.TraceObject;
17 import org.h2.util.MathUtils;
18 import org.h2.value.DataType;
19 import org.h2.value.Value;
20
21 /**
22 * Information about the parameters of a prepared statement.
23 */
24 public class JdbcParameterMetaData extends TraceObject implements ParameterMetaData {
25
26 private final JdbcPreparedStatement prep;
27 private final int paramCount;
28 private final ArrayList<? extends ParameterInterface> parameters;
29
30 JdbcParameterMetaData(Trace trace, JdbcPreparedStatement prep, CommandInterface command, int id) {
31 setTrace(trace, TraceObject.PARAMETER_META_DATA, id);
32 this.prep = prep;
33 this.parameters = command.getParameters();
34 this.paramCount = parameters.size();
35 }
36
37 /**
38 * Returns the number of parameters.
39 *
40 * @return the number
41 */
42 public int getParameterCount() throws SQLException {
43 try {
44 debugCodeCall("getParameterCount");
45 checkClosed();
46 return paramCount;
47 } catch (Exception e) {
48 throw logAndConvert(e);
49 }
50 }
51
52 /**
53 * Returns the parameter mode.
54 * Always returns parameterModeIn.
55 *
56 * @param param the column index (1,2,...)
57 * @return parameterModeIn
58 */
59 public int getParameterMode(int param) throws SQLException {
60 try {
61 debugCodeCall("getParameterMode", param);
62 getParameter(param);
63 return parameterModeIn;
64 } catch (Exception e) {
65 throw logAndConvert(e);
66 }
67 }
68
69 /**
70 * Returns the parameter type.
71 * java.sql.Types.VARCHAR is returned if the data type is not known.
72 *
73 * @param param the column index (1,2,...)
74 * @return the data type
75 */
76 public int getParameterType(int param) throws SQLException {
77 try {
78 debugCodeCall("getParameterType", param);
79 ParameterInterface p = getParameter(param);
80 int type = p.getType();
81 if (type == Value.UNKNOWN) {
82 type = Value.STRING;
83 }
84 return DataType.getDataType(type).sqlType;
85 } catch (Exception e) {
86 throw logAndConvert(e);
87 }
88 }
89
90 /**
91 * Returns the parameter precision.
92 * The value 0 is returned if the precision is not known.
93 *
94 * @param param the column index (1,2,...)
95 * @return the precision
96 */
97 public int getPrecision(int param) throws SQLException {
98 try {
99 debugCodeCall("getPrecision", param);
100 ParameterInterface p = getParameter(param);
101 return MathUtils.convertLongToInt(p.getPrecision());
102 } catch (Exception e) {
103 throw logAndConvert(e);
104 }
105 }
106
107 /**
108 * Returns the parameter scale.
109 * The value 0 is returned if the scale is not known.
110 *
111 * @param param the column index (1,2,...)
112 * @return the scale
113 */
114 public int getScale(int param) throws SQLException {
115 try {
116 debugCodeCall("getScale", param);
117 ParameterInterface p = getParameter(param);
118 return p.getScale();
119 } catch (Exception e) {
120 throw logAndConvert(e);
121 }
122 }
123
124 /**
125 * Checks if this is nullable parameter.
126 * Returns ResultSetMetaData.columnNullableUnknown..
127 *
128 * @param param the column index (1,2,...)
129 * @return ResultSetMetaData.columnNullableUnknown
130 */
131 public int isNullable(int param) throws SQLException {
132 try {
133 debugCodeCall("isNullable", param);
134 return getParameter(param).getNullable();
135 } catch (Exception e) {
136 throw logAndConvert(e);
137 }
138 }
139
140 /**
141 * Checks if this parameter is signed.
142 * It always returns true.
143 *
144 * @param param the column index (1,2,...)
145 * @return true
146 */
147 public boolean isSigned(int param) throws SQLException {
148 try {
149 debugCodeCall("isSigned", param);
150 getParameter(param);
151 return true;
152 } catch (Exception e) {
153 throw logAndConvert(e);
154 }
155 }
156
157 /**
158 * Returns the Java class name of the parameter.
159 * "java.lang.String" is returned if the type is not known.
160 *
161 * @param param the column index (1,2,...)
162 * @return the Java class name
163 */
164 public String getParameterClassName(int param) throws SQLException {
165 try {
166 debugCodeCall("getParameterClassName", param);
167 ParameterInterface p = getParameter(param);
168 int type = p.getType();
169 if (type == Value.UNKNOWN) {
170 type = Value.STRING;
171 }
172 return DataType.getTypeClassName(type);
173 } catch (Exception e) {
174 throw logAndConvert(e);
175 }
176 }
177
178 /**
179 * Returns the parameter type name.
180 * "VARCHAR" is returned if the type is not known.
181 *
182 * @param param the column index (1,2,...)
183 * @return the type name
184 */
185 public String getParameterTypeName(int param) throws SQLException {
186 try {
187 debugCodeCall("getParameterTypeName", param);
188 ParameterInterface p = getParameter(param);
189 int type = p.getType();
190 if (type == Value.UNKNOWN) {
191 type = Value.STRING;
192 }
193 return DataType.getDataType(type).name;
194 } catch (Exception e) {
195 throw logAndConvert(e);
196 }
197 }
198
199 private ParameterInterface getParameter(int param) {
200 checkClosed();
201 if (param < 1 || param > paramCount) {
202 throw DbException.getInvalidValueException("param", param);
203 }
204 return parameters.get(param - 1);
205 }
206
207 private void checkClosed() {
208 prep.checkClosed();
209 }
210
211 /**
212 * [Not supported] Return an object of this class if possible.
213 */
214 //## Java 1.6 ##
215 public <T> T unwrap(Class<T> iface) throws SQLException {
216 throw unsupported("unwrap");
217 }
218 //*/
219
220 /**
221 * [Not supported] Checks if unwrap can return an object of this class.
222 */
223 //## Java 1.6 ##
224 public boolean isWrapperFor(Class<?> iface) throws SQLException {
225 throw unsupported("isWrapperFor");
226 }
227 //*/
228
229 /**
230 * INTERNAL
231 */
232 public String toString() {
233 return getTraceObjectName() + ": parameterCount=" + paramCount;
234 }
235
236 }
237