KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > in > co > daffodil > db > jdbc > DaffodilDBResultSetMetaData


1 package in.co.daffodil.db.jdbc;
2
3 import java.sql.*;
4 import com.daffodilwoods.database.resource.*;
5 import java.io.BufferedWriter JavaDoc;
6 import com.daffodilwoods.daffodildb.server.datadictionarysystem.*;
7 import com.daffodilwoods.daffodildb.server.datasystem.interfaces.Datatype;
8 import in.co.daffodil.db.general.*;
9 import com.daffodilwoods.database.utility.*;
10
11 /**
12  * An object that can be used to get information about the types
13  * and properties of the columns in a <code>ResultSet</code> object.
14  * The following code fragment creates the <code>ResultSet</code> object rs,
15  * creates the <code>ResultSetMetaData</code> object rsmd, and uses rsmd
16  * to find out how many columns rs has and whether the first column in rs
17  * can be used in a <code>WHERE</code> clause.
18  * <PRE>
19  *
20  * ResultSet rs = stmt.executeQuery("SELECT a, b, c FROM TABLE2");
21  * ResultSetMetaData rsmd = rs.getMetaData();
22  * int numberOfColumns = rsmd.getColumnCount();
23  * boolean b = rsmd.isSearchable(1);
24  *
25  * </PRE>
26  */

27
28 public class DaffodilDBResultSetMetaData
29     implements ResultSetMetaData {
30
31   static final int intType = 0;
32   static final int stringType = 1;
33   static final int imageType = 2;
34   static final int memoType = 3;
35   static final int realType = 4;
36
37   private _ColumnCharacteristics columnCharacteristics;
38   private DaffodilDBConnection connection;
39
40   DaffodilDBResultSetMetaData(DaffodilDBConnection con) {
41     connection = con;
42   }
43
44
45   void setColumnCharacteristics(_ColumnCharacteristics columnChar) {
46     columnCharacteristics = columnChar;
47   }
48
49   public int getColumnIndex(String JavaDoc name) throws SQLException {
50     try {
51       return columnCharacteristics.getColumnIndex(name) +
52           DaffodilDBDriver.COLUMN_OFFSET;
53     }
54     catch (DException e) {
55       throw e.getSqlException(connection.getLocale());
56     }
57   }
58
59   /**
60    * Returns the number of columns in this <code>ResultSet</code> object.
61    *
62    * @return the number of columns
63    * @exception SQLException if a database access error occurs
64    */

65   public int getColumnCount() throws SQLException {
66     try {
67       return columnCharacteristics.getColumnCount();
68     }
69     catch (DException e) {
70       throw e.getSqlException(connection.getLocale());
71     }
72   }
73
74   /**
75    * Indicates whether the designated column is automatically numbered, thus read-only.
76    *
77    * @param column the first column is 1, the second is 2, ...
78    * @return <code>true</code> if so; <code>false</code> otherwise
79    * @exception SQLException if a database access error occurs
80    */

81   public boolean isAutoIncrement(int index) throws SQLException {
82     checkValidColumn(index);
83     try {
84       return columnCharacteristics.isAutoIncrement(index -
85                                                   DaffodilDBDriver.COLUMN_OFFSET);
86     }
87     catch (DException e) {
88       throw e.getSqlException(connection.getLocale());
89     }
90   }
91
92   /**
93    * Indicates whether a column's case matters.
94    *
95    * @param column the first column is 1, the second is 2, ...
96    * @return <code>true</code> if so; <code>false</code> otherwise
97    * @exception SQLException if a database access error occurs
98    */

99   public boolean isCaseSensitive(int index) throws SQLException {
100     checkValidColumn(index);
101     return false;
102   }
103
104   /**
105    * Indicates whether the designated column can be used in a where clause.
106    *
107    * @param column the first column is 1, the second is 2, ...
108    * @return <code>true</code> if so; <code>false</code> otherwise
109    * @exception SQLException if a database access error occurs
110    */

111   public boolean isSearchable(int index) throws SQLException {
112     checkValidColumn(index);
113     try {
114       int type = columnCharacteristics.getColumnType(index -
115           DaffodilDBDriver.COLUMN_OFFSET);
116       int dataType = getDataType(type);
117       return! (dataType == memoType || dataType == imageType);
118     }
119     catch (DException e) {
120       throw e.getSqlException(connection.getLocale());
121     }
122   }
123
124   /**
125    * Indicates whether the designated column is a cash value.
126    *
127    * @param column the first column is 1, the second is 2, ...
128    * @return <code>true</code> if so; <code>false</code> otherwise
129    * @exception SQLException if a database access error occurs
130    */

131   public boolean isCurrency(int index) throws SQLException {
132     checkValidColumn(index);
133     return false;
134   }
135
136   /**
137    * Indicates the nullability of values in the designated column.
138    *
139    * @param column the first column is 1, the second is 2, ...
140    * @return the nullability status of the given column; one of <code>columnNoNulls</code>,
141    * <code>columnNullable</code> or <code>columnNullableUnknown</code>
142    * @exception SQLException if a database access error occurs
143    */

144   public int isNullable(int index) throws SQLException {
145     checkValidColumn(index);
146     try {
147       int nullableValue = columnCharacteristics.isNullable(index -
148                                               DaffodilDBDriver.COLUMN_OFFSET);
149        return nullableValue == -1 ? columnNullableUnknown : nullableValue;
150     }
151     catch (DException e) {
152       throw e.getSqlException(connection.getLocale());
153     }
154   }
155
156   /**
157    * The constant indicating that a
158    * column does not allow <code>NULL</code> values.
159    *
160    * int columnNoNulls = 0;
161    *
162    * The constant indicating that a
163    * column allows <code>NULL</code> values.
164    *
165    * int columnNullable = 1;
166    *
167    * The constant indicating that the
168    * nullability of a column's values is unknown.
169    *
170    * int columnNullableUnknown = 2;
171    *
172    * Indicates whether values in the designated column are signed numbers.
173    *
174    * @param column the first column is 1, the second is 2, ...
175    * @return <code>true</code> if so; <code>false</code> otherwise
176    * @exception SQLException if a database access error occurs
177    */

178
179   public boolean isSigned(int index) throws SQLException {
180     checkValidColumn(index);
181     try {
182       int type = columnCharacteristics.getColumnType(index -
183           DaffodilDBDriver.COLUMN_OFFSET);
184       int dataType = getDataType(type);
185       return dataType == realType || dataType == intType;
186     }
187     catch (DException e) {
188       throw e.getSqlException(connection.getLocale());
189     }
190   }
191
192   /**
193    * Indicates the designated column's normal maximum width in characters.
194    *
195    * @param column the first column is 1, the second is 2, ...
196    * @return the normal maximum number of characters allowed as the width
197    * of the designated column
198    * @exception SQLException if a database access error occurs
199    */

200   public int getColumnDisplaySize(int index) throws SQLException {
201     checkValidColumn(index);
202     try {
203       int size = columnCharacteristics.getSize(index -
204                                                DaffodilDBDriver.COLUMN_OFFSET);
205       int precision = columnCharacteristics.getPrecision(index -
206           DaffodilDBDriver.COLUMN_OFFSET);
207       return Utilities.getDispalySize(getColumnType(index), precision, size);
208     }
209     catch (DException e) {
210       throw e.getSqlException(connection.getLocale());
211     }
212   }
213
214   /**
215    * Gets the designated column's suggested title for use in printouts and
216    * displays.
217    *
218    * @param column the first column is 1, the second is 2, ...
219    * @return the suggested column title
220    * @exception SQLException if a database access error occurs
221    */

222   public String JavaDoc getColumnLabel(int index) throws SQLException {
223     checkValidColumn(index);
224     try {
225       return columnCharacteristics.getColumnLabel(index -
226                                                   DaffodilDBDriver.COLUMN_OFFSET);
227     }
228     catch (DException e) {
229       throw e.getSqlException(connection.getLocale());
230     }
231   }
232
233   /**
234    * Get the designated column's name.
235    *
236    * @param column the first column is 1, the second is 2, ...
237    * @return column name
238    * @exception SQLException if a database access error occurs
239    */

240   public String JavaDoc getColumnName(int index) throws SQLException {
241     checkValidColumn(index);
242     try {
243      return columnCharacteristics.getColumnName(index -
244                                                  DaffodilDBDriver.COLUMN_OFFSET);
245     }
246     catch (DException e) {
247       throw e.getSqlException(connection.getLocale());
248     }
249   }
250
251   /**
252    * Get the designated column's table's schema.
253    *
254    * @param column the first column is 1, the second is 2, ...
255    * @return schema name or "" if not applicable
256    * @exception SQLException if a database access error occurs
257    */

258   public String JavaDoc getSchemaName(int index) throws SQLException {
259     checkValidColumn(index);
260     try {
261       return columnCharacteristics.getSchemaName(index -
262                                                  DaffodilDBDriver.COLUMN_OFFSET);
263     }
264     catch (DException e) {
265       throw e.getSqlException(connection.getLocale());
266     }
267   }
268
269   /**
270    * Get the designated column's number of decimal digits.
271    *
272    * @param column the first column is 1, the second is 2, ...
273    * @return precision
274    * @exception SQLException if a database access error occurs
275    */

276   public int getPrecision(int index) throws SQLException {
277     checkValidColumn(index);
278     try {
279       return columnCharacteristics.getPrecision(index -
280                                                 DaffodilDBDriver.COLUMN_OFFSET);
281     }
282     catch (DException e) {
283       throw e.getSqlException(connection.getLocale());
284     }
285   }
286
287   /**
288    * Gets the designated column's number of digits to right of the decimal point.
289    *
290    * @param column the first column is 1, the second is 2, ...
291    * @return scale
292    * @exception SQLException if a database access error occurs
293    */

294   public int getScale(int index) throws SQLException {
295     checkValidColumn(index);
296     try {
297       return columnCharacteristics.getScale(index -
298                                             DaffodilDBDriver.COLUMN_OFFSET);
299     }
300     catch (DException e) {
301       throw e.getSqlException(connection.getLocale());
302     }
303   }
304
305   /**
306    * Gets the designated column's table name.
307    *
308    * @param column the first column is 1, the second is 2, ...
309    * @return table name or "" if not applicable
310    * @exception SQLException if a database access error occurs
311    */

312   public String JavaDoc getTableName(int index) throws SQLException {
313     checkValidColumn(index);
314     try {
315       return columnCharacteristics.getTableName(index -
316                                                 DaffodilDBDriver.COLUMN_OFFSET);
317     }
318     catch (DException e) {
319       throw e.getSqlException(connection.getLocale());
320     }
321   }
322
323   /**
324    * Gets the designated column's table's catalog name.
325    *
326    * @param column the first column is 1, the second is 2, ...
327    * @return the name of the catalog for the table in which the given column
328    * appears or "" if not applicable
329    * @exception SQLException if a database access error occurs
330    */

331   public String JavaDoc getCatalogName(int index) throws SQLException {
332     checkValidColumn(index);
333     try {
334       return columnCharacteristics.getCatalogName(index -
335                                                   DaffodilDBDriver.
336                                                   COLUMN_OFFSET);
337     }
338     catch (DException e) {
339       throw e.getSqlException(connection.getLocale());
340     }
341   }
342
343   /**
344    * Retrieves the designated column's SQL type.
345    *
346    * @param column the first column is 1, the second is 2, ...
347    * @return SQL type from java.sql.Types
348    * @exception SQLException if a database access error occurs
349    * @see Types
350    */

351   public int getColumnType(int index) throws SQLException {
352     checkValidColumn(index);
353     try {
354       int dataType = columnCharacteristics.getColumnType(index -
355           DaffodilDBDriver.COLUMN_OFFSET);
356       int type = Utilities.getCorrespondingSqlTypeOfDatabaseType(dataType);
357       return type;
358     }
359     catch (DException e) {
360       throw e.getSqlException(connection.getLocale());
361     }
362   }
363
364   /**
365    * Retrieves the designated column's database-specific type name.
366    *
367    * @param column the first column is 1, the second is 2, ...
368    * @return type name used by the database. If the column type is
369    * a user-defined type, then a fully-qualified type name is returned.
370    * @exception SQLException if a database access error occurs
371    */

372   public String JavaDoc getColumnTypeName(int index) throws SQLException {
373     checkValidColumn(index);
374     try {
375       int type = columnCharacteristics.getColumnType(index -
376           DaffodilDBDriver.COLUMN_OFFSET);
377       return Utilities.getDataBaseTypeName(type);
378     }
379     catch (DException e) {
380       throw e.getSqlException(connection.getLocale());
381     }
382   }
383
384   /**
385    * Indicates whether the designated column is definitely not writable.
386    *
387    * @param column the first column is 1, the second is 2, ...
388    * @return <code>true</code> if so; <code>false</code> otherwise
389    * @exception SQLException if a database access error occurs
390    */

391   public boolean isReadOnly(int index) throws SQLException {
392     checkValidColumn(index);
393     return false;
394   }
395
396   /**
397    * Indicates whether it is possible for a write on the designated column to succeed.
398    *
399    * @param column the first column is 1, the second is 2, ...
400    * @return <code>true</code> if so; <code>false</code> otherwise
401    * @exception SQLException if a database access error occurs
402    */

403   public boolean isWritable(int index) throws SQLException {
404     checkValidColumn(index);
405     return true;
406   }
407
408   /**
409    * Indicates whether a write on the designated column will definitely succeed.
410    *
411    * @param column the first column is 1, the second is 2, ...
412    * @return <code>true</code> if so; <code>false</code> otherwise
413    * @exception SQLException if a database access error occurs
414    */

415   public boolean isDefinitelyWritable(int index) throws SQLException {
416     checkValidColumn(index);
417     return true;
418   }
419
420   /** --------------------------
421    * JDBC 2.0
422    * -----------------------------------
423    */

424
425   /**
426    * <p>Returns the fully-qualified name of the Java class whose instances
427    * are manufactured if the method <code>ResultSet.getObject</code>
428    * is called to retrieve a value
429    * from the column. <code>ResultSet.getObject</code> may return a subclass of the
430    * class returned by this method.
431    *
432    * @param column the first column is 1, the second is 2, ...
433    * @return the fully-qualified name of the class in the Java programming
434    * language that would be used by the method
435    * <code>ResultSet.getObject</code> to retrieve the value in the specified
436    * column. This is the class name used for custom mapping.
437    * @exception SQLException if a database access error occurs
438    * @since 1.2
439    */

440
441   public String JavaDoc getColumnClassName(int index) throws SQLException {
442     checkValidColumn(index);
443       int sqlType = getColumnType(index);
444       return Utilities.getCorrespondingJavaClassesOfSqlTypes(sqlType);
445   }
446
447   /** --------------------------------------------------
448    * Methods specific to DaffodilDBResultSetMetaData
449    * ---------------------------------------------------
450    */

451
452   private int getDataType(int databaseType) {
453     switch (databaseType) {
454       case Datatype.TIME:
455       case Datatype.TIMESTAMP:
456       case Datatype.DATE:
457       case Datatype.BOOLEAN:
458       case Datatype.CHARACTER:
459         return stringType;
460
461       case Datatype.BYTE:
462       case Datatype.INTEGER:
463       case Datatype.MODULE:
464       case Datatype.RECORD:
465       case Datatype.LONG:
466       case Datatype.SHORT:
467         return intType;
468
469       case Datatype.BIGDECIMAL:
470       case Datatype.FLOAT:
471       case Datatype.SUM:
472       case Datatype.DOUBLE:
473         return realType;
474
475       case Datatype.BLOB:
476         return imageType;
477
478       case Datatype.ARRAY:
479       case Datatype.REF:
480       case Datatype.STRUCT:
481       case Datatype.CLOB:
482         return memoType;
483     }
484     return memoType;
485   }
486
487   private void checkValidColumn(int columnIndex) throws SQLException {
488     if (columnIndex < 1 || columnIndex > getColumnCount()) {
489       DException dex = new DException("DSE505",
490                                       new Object JavaDoc[] {new Integer JavaDoc(columnIndex)});
491       throw dex.getSqlException(connection.getLocale());
492     }
493   }
494 }
495
Popular Tags