File D:\maven-qcr-plugin-testsunitaires\qcr4java\h2-1.3.170-sources\target\site\qc\lecode\jdbc\JdbcResultSetMetaData.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.ResultSetMetaData;
10 import java.sql.SQLException;
11
12 import org.h2.message.DbException;
13 import org.h2.message.Trace;
14 import org.h2.message.TraceObject;
15 import org.h2.result.ResultInterface;
16 import org.h2.util.MathUtils;
17 import org.h2.value.DataType;
18
19 /**
20 * Represents the meta data for a ResultSet.
21 */
22 public class JdbcResultSetMetaData extends TraceObject implements ResultSetMetaData {
23
24 private final String catalog;
25 private final JdbcResultSet rs;
26 private final JdbcPreparedStatement prep;
27 private final ResultInterface result;
28 private final int columnCount;
29
30 JdbcResultSetMetaData(JdbcResultSet rs, JdbcPreparedStatement prep, ResultInterface result,
31 String catalog, Trace trace, int id) {
32 setTrace(trace, TraceObject.RESULT_SET_META_DATA, id);
33 this.catalog = catalog;
34 this.rs = rs;
35 this.prep = prep;
36 this.result = result;
37 this.columnCount = result.getVisibleColumnCount();
38 }
39
40 /**
41 * Returns the number of columns.
42 *
43 * @return the number of columns
44 * @throws SQLException if the result set is closed or invalid
45 */
46 public int getColumnCount() throws SQLException {
47 try {
48 debugCodeCall("getColumnCount");
49 checkClosed();
50 return columnCount;
51 } catch (Exception e) {
52 throw logAndConvert(e);
53 }
54 }
55
56 /**
57 * Returns the column label.
58 *
59 * @param column the column index (1,2,...)
60 * @return the column label
61 * @throws SQLException if the result set is closed or invalid
62 */
63 public String getColumnLabel(int column) throws SQLException {
64 try {
65 debugCodeCall("getColumnLabel", column);
66 checkColumnIndex(column);
67 return result.getAlias(--column);
68 } catch (Exception e) {
69 throw logAndConvert(e);
70 }
71 }
72
73 /**
74 * Returns the column name.
75 *
76 * @param column the column index (1,2,...)
77 * @return the column name
78 * @throws SQLException if the result set is closed or invalid
79 */
80 public String getColumnName(int column) throws SQLException {
81 try {
82 debugCodeCall("getColumnName", column);
83 checkColumnIndex(column);
84 return result.getColumnName(--column);
85 } catch (Exception e) {
86 throw logAndConvert(e);
87 }
88 }
89
90 /**
91 * Returns the data type of a column.
92 * See also java.sql.Type.
93 *
94 * @param column the column index (1,2,...)
95 * @return the data type
96 * @throws SQLException if the result set is closed or invalid
97 */
98 public int getColumnType(int column) throws SQLException {
99 try {
100 debugCodeCall("getColumnType", column);
101 checkColumnIndex(column);
102 int type = result.getColumnType(--column);
103 return DataType.convertTypeToSQLType(type);
104 } catch (Exception e) {
105 throw logAndConvert(e);
106 }
107 }
108
109 /**
110 * Returns the data type name of a column.
111 *
112 * @param column the column index (1,2,...)
113 * @return the data type name
114 * @throws SQLException if the result set is closed or invalid
115 */
116 public String getColumnTypeName(int column) throws SQLException {
117 try {
118 debugCodeCall("getColumnTypeName", column);
119 checkColumnIndex(column);
120 int type = result.getColumnType(--column);
121 return DataType.getDataType(type).name;
122 } catch (Exception e) {
123 throw logAndConvert(e);
124 }
125 }
126
127 /**
128 * Returns the schema name.
129 *
130 * @param column the column index (1,2,...)
131 * @return the schema name, or "" (an empty string) if not applicable
132 * @throws SQLException if the result set is closed or invalid
133 */
134 public String getSchemaName(int column) throws SQLException {
135 try {
136 debugCodeCall("getSchemaName", column);
137 checkColumnIndex(column);
138 String schema = result.getSchemaName(--column);
139 return schema == null ? "" : schema;
140 } catch (Exception e) {
141 throw logAndConvert(e);
142 }
143 }
144
145 /**
146 * Returns the table name.
147 *
148 * @param column the column index (1,2,...)
149 * @return the table name
150 * @throws SQLException if the result set is closed or invalid
151 */
152 public String getTableName(int column) throws SQLException {
153 try {
154 debugCodeCall("getTableName", column);
155 checkColumnIndex(column);
156 String table = result.getTableName(--column);
157 return table == null ? "" : table;
158 } catch (Exception e) {
159 throw logAndConvert(e);
160 }
161 }
162
163 /**
164 * Returns the catalog name.
165 *
166 * @param column the column index (1,2,...)
167 * @return the catalog name
168 * @throws SQLException if the result set is closed or invalid
169 */
170 public String getCatalogName(int column) throws SQLException {
171 try {
172 debugCodeCall("getCatalogName", column);
173 checkColumnIndex(column);
174 return catalog == null ? "" : catalog;
175 } catch (Exception e) {
176 throw logAndConvert(e);
177 }
178 }
179
180 /**
181 * Checks if this an autoincrement column.
182 * It always returns false.
183 *
184 * @param column the column index (1,2,...)
185 * @return false
186 * @throws SQLException if the result set is closed or invalid
187 */
188 public boolean isAutoIncrement(int column) throws SQLException {
189 try {
190 debugCodeCall("isAutoIncrement", column);
191 checkColumnIndex(column);
192 return result.isAutoIncrement(--column);
193 } catch (Exception e) {
194 throw logAndConvert(e);
195 }
196 }
197
198 /**
199 * Checks if this column is case sensitive.
200 * It always returns true.
201 *
202 * @param column the column index (1,2,...)
203 * @return true
204 * @throws SQLException if the result set is closed or invalid
205 */
206 public boolean isCaseSensitive(int column) throws SQLException {
207 try {
208 debugCodeCall("isCaseSensitive", column);
209 checkColumnIndex(column);
210 return true;
211 } catch (Exception e) {
212 throw logAndConvert(e);
213 }
214 }
215
216 /**
217 * Checks if this column is searchable.
218 * It always returns true.
219 *
220 * @param column the column index (1,2,...)
221 * @return true
222 * @throws SQLException if the result set is closed or invalid
223 */
224 public boolean isSearchable(int column) throws SQLException {
225 try {
226 debugCodeCall("isSearchable", column);
227 checkColumnIndex(column);
228 return true;
229 } catch (Exception e) {
230 throw logAndConvert(e);
231 }
232 }
233
234 /**
235 * Checks if this is a currency column.
236 * It always returns false.
237 *
238 * @param column the column index (1,2,...)
239 * @return false
240 * @throws SQLException if the result set is closed or invalid
241 */
242 public boolean isCurrency(int column) throws SQLException {
243 try {
244 debugCodeCall("isCurrency", column);
245 checkColumnIndex(column);
246 return false;
247 } catch (Exception e) {
248 throw logAndConvert(e);
249 }
250 }
251
252 /**
253 * Checks if this is nullable column. Returns
254 * ResultSetMetaData.columnNullableUnknown if this is not a column of a
255 * table. Otherwise, it returns ResultSetMetaData.columnNoNulls if the
256 * column is not nullable, and ResultSetMetaData.columnNullable if it is
257 * nullable.
258 *
259 * @param column the column index (1,2,...)
260 * @return ResultSetMetaData.column*
261 * @throws SQLException if the result set is closed or invalid
262 */
263 public int isNullable(int column) throws SQLException {
264 try {
265 debugCodeCall("isNullable", column);
266 checkColumnIndex(column);
267 return result.getNullable(--column);
268 } catch (Exception e) {
269 throw logAndConvert(e);
270 }
271 }
272
273 /**
274 * Checks if this column is signed.
275 * It always returns true.
276 *
277 * @param column the column index (1,2,...)
278 * @return true
279 * @throws SQLException if the result set is closed or invalid
280 */
281 public boolean isSigned(int column) throws SQLException {
282 try {
283 debugCodeCall("isSigned", column);
284 checkColumnIndex(column);
285 return true;
286 } catch (Exception e) {
287 throw logAndConvert(e);
288 }
289 }
290
291 /**
292 * Checks if this column is read only.
293 * It always returns false.
294 *
295 * @param column the column index (1,2,...)
296 * @return false
297 * @throws SQLException if the result set is closed or invalid
298 */
299 public boolean isReadOnly(int column) throws SQLException {
300 try {
301 debugCodeCall("isReadOnly", column);
302 checkColumnIndex(column);
303 return false;
304 } catch (Exception e) {
305 throw logAndConvert(e);
306 }
307 }
308
309 /**
310 * Checks whether it is possible for a write on this column to succeed.
311 * It always returns true.
312 *
313 * @param column the column index (1,2,...)
314 * @return true
315 * @throws SQLException if the result set is closed or invalid
316 */
317 public boolean isWritable(int column) throws SQLException {
318 try {
319 debugCodeCall("isWritable", column);
320 checkColumnIndex(column);
321 return true;
322 } catch (Exception e) {
323 throw logAndConvert(e);
324 }
325 }
326
327 /**
328 * Checks whether a write on this column will definitely succeed.
329 * It always returns false.
330 *
331 * @param column the column index (1,2,...)
332 * @return false
333 * @throws SQLException if the result set is closed or invalid
334 */
335 public boolean isDefinitelyWritable(int column) throws SQLException {
336 try {
337 debugCodeCall("isDefinitelyWritable", column);
338 checkColumnIndex(column);
339 return false;
340 } catch (Exception e) {
341 throw logAndConvert(e);
342 }
343 }
344
345 /**
346 * Gets the Java class name of the object that will be returned
347 * if ResultSet.getObject is called.
348 *
349 * @param column the column index (1,2,...)
350 * @return the Java class name
351 * @throws SQLException if the result set is closed or invalid
352 */
353 public String getColumnClassName(int column) throws SQLException {
354 try {
355 debugCodeCall("getColumnClassName", column);
356 checkColumnIndex(column);
357 int type = result.getColumnType(--column);
358 return DataType.getTypeClassName(type);
359 } catch (Exception e) {
360 throw logAndConvert(e);
361 }
362 }
363
364 /**
365 * Gets the precision for this column.
366 * This method always returns 0.
367 *
368 * @param column the column index (1,2,...)
369 * @return the precision
370 * @throws SQLException if the result set is closed or invalid
371 */
372 public int getPrecision(int column) throws SQLException {
373 try {
374 debugCodeCall("getPrecision", column);
375 checkColumnIndex(column);
376 long prec = result.getColumnPrecision(--column);
377 return MathUtils.convertLongToInt(prec);
378 } catch (Exception e) {
379 throw logAndConvert(e);
380 }
381 }
382
383 /**
384 * Gets the scale for this column.
385 * This method always returns 0.
386 *
387 * @param column the column index (1,2,...)
388 * @return the scale
389 * @throws SQLException if the result set is closed or invalid
390 */
391 public int getScale(int column) throws SQLException {
392 try {
393 debugCodeCall("getScale", column);
394 checkColumnIndex(column);
395 return result.getColumnScale(--column);
396 } catch (Exception e) {
397 throw logAndConvert(e);
398 }
399 }
400
401 /**
402 * Gets the maximum display size for this column.
403 *
404 * @param column the column index (1,2,...)
405 * @return the display size
406 * @throws SQLException if the result set is closed or invalid
407 */
408 public int getColumnDisplaySize(int column) throws SQLException {
409 try {
410 debugCodeCall("getColumnDisplaySize", column);
411 checkColumnIndex(column);
412 return result.getDisplaySize(--column);
413 } catch (Exception e) {
414 throw logAndConvert(e);
415 }
416 }
417
418 private void checkClosed() {
419 if (rs != null) {
420 rs.checkClosed();
421 }
422 if (prep != null) {
423 prep.checkClosed();
424 }
425 }
426
427 private void checkColumnIndex(int columnIndex) {
428 checkClosed();
429 if (columnIndex < 1 || columnIndex > columnCount) {
430 throw DbException.getInvalidValueException("columnIndex", columnIndex);
431 }
432 }
433
434 /**
435 * [Not supported] Return an object of this class if possible.
436 */
437 //## Java 1.6 ##
438 public <T> T unwrap(Class<T> iface) throws SQLException {
439 throw unsupported("unwrap");
440 }
441 //*/
442
443 /**
444 * [Not supported] Checks if unwrap can return an object of this class.
445 */
446 //## Java 1.6 ##
447 public boolean isWrapperFor(Class<?> iface) throws SQLException {
448 throw unsupported("isWrapperFor");
449 }
450 //*/
451
452 /**
453 * INTERNAL
454 */
455 public String toString() {
456 return getTraceObjectName() + ": columns=" + columnCount;
457 }
458
459 }
460