KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > h2 > jdbc > JdbcResultSetMetaData


1 /*
2  * Copyright 2004-2006 H2 Group. Licensed under the H2 License, Version 1.0 (http://h2database.com/html/license.html).
3  * Initial Developer: H2 Group
4  */

5 package org.h2.jdbc;
6
7 import java.sql.ResultSetMetaData JavaDoc;
8 import java.sql.SQLException JavaDoc;
9
10 import org.h2.message.*;
11 import org.h2.message.TraceObject;
12 import org.h2.result.ResultInterface;
13 import org.h2.util.MathUtils;
14 import org.h2.value.DataType;
15
16 /**
17  * Represents the meta data for a ResultSet.
18  */

19 public class JdbcResultSetMetaData extends TraceObject implements ResultSetMetaData JavaDoc {
20
21     private JdbcResultSet rs;
22     private ResultInterface result;
23
24     /**
25      * Returns the number of columns.
26      *
27      * @return the number of columns
28      * @throws SQLException if the result set is closed or invalid
29      */

30     public int getColumnCount() throws SQLException JavaDoc {
31         try {
32             debugCodeCall("getColumnCount");
33             checkClosed();
34             return result.getVisibleColumnCount();
35         } catch(Throwable JavaDoc e) {
36             throw logAndConvert(e);
37         }
38     }
39
40     /**
41      * Returns the column label.
42      *
43      * @param column the column index (1,2,...)
44      * @return the column label
45      * @throws SQLException if the result set is closed or invalid
46      */

47     public String JavaDoc getColumnLabel(int column) throws SQLException JavaDoc {
48         try {
49             debugCodeCall("getColumnLabel", column);
50             rs.checkColumnIndex(column);
51             return result.getAlias(--column);
52         } catch(Throwable JavaDoc e) {
53             throw logAndConvert(e);
54         }
55     }
56
57     /**
58      * Returns the column name.
59      *
60      * @param column the column index (1,2,...)
61      * @return the column name
62      * @throws SQLException if the result set is closed or invalid
63      */

64     public String JavaDoc getColumnName(int column) throws SQLException JavaDoc {
65         try {
66             debugCodeCall("getColumnName", column);
67             rs.checkColumnIndex(column);
68             return result.getColumnName(--column);
69         } catch(Throwable JavaDoc e) {
70             throw logAndConvert(e);
71         }
72     }
73
74     /**
75      * Returns the data type of a column.
76      *
77      * @param column the column index (1,2,...)
78      * @return the data type
79      * @throws SQLException if the result set is closed or invalid
80      */

81     public int getColumnType(int column) throws SQLException JavaDoc {
82         try {
83             debugCodeCall("getColumnType", column);
84             rs.checkColumnIndex(column);
85             int type = result.getColumnType(--column);
86             return DataType.convertTypeToSQLType(type);
87         } catch(Throwable JavaDoc e) {
88             throw logAndConvert(e);
89         }
90     }
91
92     /**
93      * Returns the data type name of a column.
94      *
95      * @param column the column index (1,2,...)
96      * @return the data type
97      * @throws SQLException if the result set is closed or invalid
98      */

99     public String JavaDoc getColumnTypeName(int column) throws SQLException JavaDoc {
100         try {
101             debugCodeCall("getColumnTypeName", column);
102             rs.checkColumnIndex(column);
103             int type = result.getColumnType(--column);
104             return DataType.getDataType(type).name;
105         } catch(Throwable JavaDoc e) {
106             throw logAndConvert(e);
107         }
108     }
109
110     /**
111      * Returns the schema name.
112      *
113      * @param column the column index (1,2,...)
114      * @return the schema name
115      * @throws SQLException if the result set is closed or invalid
116      */

117     public String JavaDoc getSchemaName(int column) throws SQLException JavaDoc {
118         try {
119             debugCodeCall("getSchemaName", column);
120             rs.checkColumnIndex(column);
121             return result.getSchemaName(--column);
122         } catch(Throwable JavaDoc e) {
123             throw logAndConvert(e);
124         }
125     }
126
127     /**
128      * Returns the table name.
129      *
130      * @param column the column index (1,2,...)
131      * @return the table name
132      * @throws SQLException if the result set is closed or invalid
133      */

134     public String JavaDoc getTableName(int column) throws SQLException JavaDoc {
135         try {
136             debugCodeCall("getTableName", column);
137             rs.checkColumnIndex(column);
138             return result.getTableName(--column);
139         } catch(Throwable JavaDoc e) {
140             throw logAndConvert(e);
141         }
142     }
143
144     /**
145      * Returns the catalog name.
146      *
147      * @param column the column index (1,2,...)
148      * @return the catalog name
149      * @throws SQLException if the result set is closed or invalid
150      */

151     public String JavaDoc getCatalogName(int column) throws SQLException JavaDoc {
152         try {
153             debugCodeCall("getCatalogName", column);
154             rs.checkColumnIndex(column);
155             return rs.getConnection().getCatalog();
156         } catch(Throwable JavaDoc e) {
157             throw logAndConvert(e);
158         }
159     }
160
161     /**
162      * Checks if this an autoincrement column.
163      * It always returns false.
164      *
165      * @param column the column index (1,2,...)
166      * @return false
167      * @throws SQLException if the result set is closed or invalid
168      */

169     public boolean isAutoIncrement(int column) throws SQLException JavaDoc {
170         try {
171             debugCodeCall("isAutoIncrement", column);
172             rs.checkColumnIndex(column);
173             return result.isAutoIncrement(--column);
174         } catch(Throwable JavaDoc e) {
175             throw logAndConvert(e);
176         }
177     }
178
179     /**
180      * Checks if this column is case sensitive.
181      * It always returns true.
182      *
183      * @param column the column index (1,2,...)
184      * @return true
185      * @throws SQLException if the result set is closed or invalid
186      */

187     public boolean isCaseSensitive(int column) throws SQLException JavaDoc {
188         try {
189             debugCodeCall("isCaseSensitive", column);
190             rs.checkColumnIndex(column);
191             return true;
192         } catch(Throwable JavaDoc e) {
193             throw logAndConvert(e);
194         }
195     }
196
197     /**
198      * Checks if this column is searchable.
199      * It always returns true.
200      *
201      * @param column the column index (1,2,...)
202      * @return true
203      * @throws SQLException if the result set is closed or invalid
204      */

205     public boolean isSearchable(int column) throws SQLException JavaDoc {
206         try {
207             debugCodeCall("isSearchable", column);
208             rs.checkColumnIndex(column);
209             return true;
210         } catch(Throwable JavaDoc e) {
211             throw logAndConvert(e);
212         }
213     }
214
215     /**
216      * Checks if this is a currency column.
217      * It always returns false.
218      *
219      * @param column the column index (1,2,...)
220      * @return false
221      * @throws SQLException if the result set is closed or invalid
222      */

223     public boolean isCurrency(int column) throws SQLException JavaDoc {
224         try {
225             debugCodeCall("isCurrency", column);
226             rs.checkColumnIndex(column);
227             return false;
228         } catch(Throwable JavaDoc e) {
229             throw logAndConvert(e);
230         }
231     }
232
233     /**
234      * Checks if this is nullable column.
235      * Returns ResultSetMetaData.columnNullableUnknown if this is not a column of a table.
236      * Otherwise, it returns ResultSetMetaData.columnNoNulls if the column is not nullable, and
237      * ResultSetMetaData.columnNullable if it is nullable.
238      *
239      * @param column the column index (1,2,...)
240      * @return ResultSetMetaData.column*
241      * @throws SQLException if the result set is closed or invalid
242      */

243     public int isNullable(int column) throws SQLException JavaDoc {
244         try {
245             debugCodeCall("isNullable", column);
246             rs.checkColumnIndex(column);
247             return result.getNullable(--column);
248         } catch(Throwable JavaDoc e) {
249             throw logAndConvert(e);
250         }
251     }
252
253     /**
254      * Checks if this column is signed.
255      * It always returns true.
256      *
257      * @param column the column index (1,2,...)
258      * @return true
259      * @throws SQLException if the result set is closed or invalid
260      */

261     public boolean isSigned(int column) throws SQLException JavaDoc {
262         try {
263             debugCodeCall("isSigned", column);
264             rs.checkColumnIndex(column);
265             return true;
266         } catch(Throwable JavaDoc e) {
267             throw logAndConvert(e);
268         }
269     }
270
271     /**
272      * Checks if this column is read only.
273      * It always returns false.
274      *
275      * @param column the column index (1,2,...)
276      * @return false
277      * @throws SQLException if the result set is closed or invalid
278      */

279     public boolean isReadOnly(int column) throws SQLException JavaDoc {
280         try {
281             debugCodeCall("isReadOnly", column);
282             rs.checkColumnIndex(column);
283             return false;
284         } catch(Throwable JavaDoc e) {
285             throw logAndConvert(e);
286         }
287     }
288
289     /**
290      * Checks whether it is possible for a write on this column to succeed.
291      * It always returns true.
292      *
293      * @param column the column index (1,2,...)
294      * @return true
295      * @throws SQLException if the result set is closed or invalid
296      */

297     public boolean isWritable(int column) throws SQLException JavaDoc {
298         try {
299             debugCodeCall("isWritable", column);
300             rs.checkColumnIndex(column);
301             return true;
302         } catch(Throwable JavaDoc e) {
303             throw logAndConvert(e);
304         }
305     }
306
307     /**
308      * Checks whether a write on this column will definitely succeed.
309      * It always returns false.
310      *
311      * @param column the column index (1,2,...)
312      * @return false
313      * @throws SQLException if the result set is closed or invalid
314      */

315     public boolean isDefinitelyWritable(int column) throws SQLException JavaDoc {
316         try {
317             debugCodeCall("isDefinitelyWritable", column);
318             rs.checkColumnIndex(column);
319             return false;
320         } catch(Throwable JavaDoc e) {
321             throw logAndConvert(e);
322         }
323     }
324
325     /**
326      * Gets the Java class name of the object that will be returned
327      * if ResultSet.getObject is called.
328      *
329      * @param column the column index (1,2,...)
330      * @return the Java class name
331      * @throws SQLException if the result set is closed or invalid
332      */

333     public String JavaDoc getColumnClassName(int column) throws SQLException JavaDoc {
334         try {
335             debugCodeCall("getColumnClassName", column);
336             rs.checkColumnIndex(column);
337             int type = result.getColumnType(--column);
338             return DataType.getTypeClassName(type);
339         } catch(Throwable JavaDoc e) {
340             throw logAndConvert(e);
341         }
342     }
343
344     /**
345      * Gets the precision for this column.
346      * This method always returns 0.
347      *
348      * @param column the column index (1,2,...)
349      * @return the precision
350      * @throws SQLException if the result set is closed or invalid
351      */

352     public int getPrecision(int column) throws SQLException JavaDoc {
353         try {
354             debugCodeCall("getPrecision", column);
355             rs.checkColumnIndex(column);
356             long prec = result.getColumnPrecision(--column);
357             return MathUtils.convertLongToInt(prec);
358         } catch(Throwable JavaDoc e) {
359             throw logAndConvert(e);
360         }
361     }
362
363     /**
364      * Gets the scale for this column.
365      * This method always returns 0.
366      *
367      * @param column the column index (1,2,...)
368      * @return the scale
369      * @throws SQLException if the result set is closed or invalid
370      */

371     public int getScale(int column) throws SQLException JavaDoc {
372         try {
373             debugCodeCall("getScale", column);
374             rs.checkColumnIndex(column);
375             return result.getColumnScale(--column);
376         } catch(Throwable JavaDoc e) {
377             throw logAndConvert(e);
378         }
379     }
380
381     /**
382      * Gets the maximum display size for this column.
383      *
384      * @param column the column index (1,2,...)
385      * @return the display size
386      * @throws SQLException if the result set is closed or invalid
387      */

388     public int getColumnDisplaySize(int column) throws SQLException JavaDoc {
389         try {
390             debugCodeCall("getColumnDisplaySize", column);
391             rs.checkColumnIndex(column);
392             return result.getDisplaySize(--column);
393         } catch(Throwable JavaDoc e) {
394             throw logAndConvert(e);
395         }
396     }
397
398     JdbcResultSetMetaData(JdbcResultSet rs, ResultInterface result, Trace trace, int id) {
399         setTrace(trace, TraceObject.RESULT_SET_META_DATA, id);
400         this.rs = rs;
401         this.result = result;
402     }
403
404     void checkClosed() throws SQLException JavaDoc {
405         rs.checkClosed();
406     }
407
408     /**
409      * Return an object of this class if possible.
410      * @throws SQLException Unsupported Feature (SQL State 0A000)
411      */

412     //#ifdef JDK16
413
/*
414     public <T> T unwrap(Class<T> iface) throws SQLException {
415         debugCodeCall("unwrap");
416         throw Message.getUnsupportedException();
417     }
418 */

419     //#endif
420

421     /**
422      * Checks if unwrap can return an object of this class.
423      * @throws SQLException Unsupported Feature (SQL State 0A000)
424      */

425     //#ifdef JDK16
426
/*
427     public boolean isWrapperFor(Class<?> iface) throws SQLException {
428         debugCodeCall("isWrapperFor");
429         throw Message.getUnsupportedException();
430     }
431 */

432     //#endif
433

434 }
435
Popular Tags