KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > mysql > jdbc > ResultSetMetaData


1 /*
2    Copyright (C) 2002 MySQL AB
3
4       This program is free software; you can redistribute it and/or modify
5       it under the terms of the GNU General Public License as published by
6       the Free Software Foundation; either version 2 of the License, or
7       (at your option) any later version.
8
9       This program is distributed in the hope that it will be useful,
10       but WITHOUT ANY WARRANTY; without even the implied warranty of
11       MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12       GNU General Public License for more details.
13
14       You should have received a copy of the GNU General Public License
15       along with this program; if not, write to the Free Software
16       Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17
18  */

19 package com.mysql.jdbc;
20
21 import java.sql.SQLException JavaDoc;
22 import java.sql.Types JavaDoc;
23
24
25 /**
26  * A ResultSetMetaData object can be used to find out about the types and
27  * properties of the columns in a ResultSet
28  *
29  * @see java.sql.ResultSetMetaData
30  * @author Mark Matthews
31  * @version $Id: ResultSetMetaData.java,v 1.12.2.6 2004/02/18 16:04:58 mmatthew Exp $
32  */

33 public class ResultSetMetaData implements java.sql.ResultSetMetaData JavaDoc {
34     Field[] fields;
35
36     /**
37             * Initialise for a result with a tuple set and
38      * a field descriptor set
39      *
40      * @param fields the array of field descriptors
41
42      */

43     public ResultSetMetaData(Field[] fields) {
44         this.fields = fields;
45     }
46
47     /**
48      * Is the column automatically numbered (and thus read-only)
49      *
50      * MySQL Auto-increment columns are not read only,
51      * so to conform to the spec, this method returns false.
52      *
53      * @param column the first column is 1, the second is 2...
54      * @return true if so
55      * @throws java.sql.SQLException if a database access error occurs
56      */

57     public boolean isAutoIncrement(int column) throws java.sql.SQLException JavaDoc {
58         Field f = getField(column);
59
60         return f.isAutoIncrement();
61     }
62
63     /**
64      * Does a column's case matter? ASSUMPTION: Any field that is
65      * not obviously case insensitive is assumed to be case sensitive
66      *
67      * @param column the first column is 1, the second is 2...
68      * @return true if so
69      * @throws java.sql.SQLException if a database access error occurs
70      */

71     public boolean isCaseSensitive(int column) throws java.sql.SQLException JavaDoc {
72         Field field = getField(column);
73         
74         int sqlType = field.getSQLType();
75
76         switch (sqlType) {
77         case Types.BIT:
78         case Types.TINYINT:
79         case Types.SMALLINT:
80         case Types.INTEGER:
81         case Types.BIGINT:
82         case Types.FLOAT:
83         case Types.REAL:
84         case Types.DOUBLE:
85         case Types.DATE:
86         case Types.TIME:
87         case Types.TIMESTAMP:
88             return false;
89             
90         case Types.CHAR:
91         case Types.VARCHAR:
92             
93             return field.isBinary();
94                     
95         default:
96             return true;
97         }
98     }
99
100     /**
101      * What's a column's table's catalog name?
102      *
103      * @param column the first column is 1, the second is 2...
104      * @return catalog name, or "" if not applicable
105      * @throws java.sql.SQLException if a database access error occurs
106      */

107     public String JavaDoc getCatalogName(int column) throws java.sql.SQLException JavaDoc {
108         Field f = getField(column);
109
110         String JavaDoc database = f.getDatabaseName();
111
112         return (database == null) ? "" : database;
113     }
114
115     //--------------------------JDBC 2.0-----------------------------------
116

117     /**
118      * JDBC 2.0
119      *
120      * <p>Return the fully qualified name of the Java class whose instances
121      * are manufactured if ResultSet.getObject() is called to retrieve a value
122      * from the column. ResultSet.getObject() may return a subClass of the
123      * class returned by this method.
124      *
125      * @param column the column number to retrieve information for
126      * @return the fully qualified name of the Java class whose instances
127      * are manufactured if ResultSet.getObject() is called to retrieve a value
128      * from the column.
129      *
130      * @throws SQLException if an error occurs
131      */

132     public String JavaDoc getColumnClassName(int column) throws SQLException JavaDoc {
133         Field f = getField(column);
134
135         // From JDBC-3.0 spec
136
//
137
// JDBC Type Java Object Type
138
//
139
// CHAR String
140
// VARCHAR String
141
// LONGVARCHAR String
142
// NUMERIC java.math.BigDecimal
143
// DECIMAL java.math.BigDecimal
144
// BIT Boolean
145
// BOOLEAN Boolean
146
// TINYINT Integer
147
// SMALLINT Integer
148
// INTEGER Integer
149
// BIGINT Long
150
// REAL Float
151
// FLOAT Double
152
// DOUBLE Double
153
// BINARY byte[]
154
// VARBINARY byte[]
155
// LONGVARBINARY byte[]
156
// DATE java.sql.Date
157
// TIME java.sql.Time
158
// TIMESTAMP java.sql.Timestamp
159
// DISTINCT Object type of underlying type
160
// CLOB Clob
161
// BLOB Blob
162
// ARRAY Array
163
// STRUCT Struct or SQLData
164
// REF Ref
165
// DATALINK java.net.URL
166
// JAVA_OBJECT underlying Java class
167

168         switch (f.getSQLType()) {
169         case Types.BIT:
170         case Types.BOOLEAN:
171             return "java.lang.Boolean";
172
173         case Types.TINYINT:
174
175             return "java.lang.Integer";
176            
177         case Types.SMALLINT:
178
179             return "java.lang.Integer";
180
181         case Types.INTEGER:
182
183             if (f.isUnsigned()) {
184                 return "java.lang.Long";
185             } else {
186                 return "java.lang.Integer";
187             }
188
189         case Types.BIGINT:
190             
191             return "java.lang.Long";
192
193         case Types.DECIMAL:
194         case Types.NUMERIC:
195             
196             return "java.math.BigDecimal";
197
198         case Types.REAL:
199         
200             return "java.lang.Float";
201         
202         case Types.FLOAT:
203         case Types.DOUBLE:
204             
205             return "java.lang.Double";
206
207         case Types.CHAR:
208         case Types.VARCHAR:
209         case Types.LONGVARCHAR:
210             
211             return "java.lang.String";
212
213         case Types.BINARY:
214         case Types.VARBINARY:
215         case Types.LONGVARBINARY:
216
217             if (!f.isBlob()) {
218                 return "java.lang.String";
219             } else if (!f.isBinary()) {
220                 return "java.lang.String";
221             } else {
222                 return "[B";
223             }
224
225         case Types.DATE:
226             
227             return "java.sql.Date";
228
229         case Types.TIME:
230             
231             return "java.sql.Time";
232
233         case Types.TIMESTAMP:
234             
235             return "java.sql.Timestamp";
236
237         default:
238             
239             return "java.lang.Object";
240         }
241     }
242
243     /**
244      * Whats the number of columns in the ResultSet?
245      *
246      * @return the number
247      * @throws java.sql.SQLException if a database access error occurs
248      */

249     public int getColumnCount() throws java.sql.SQLException JavaDoc {
250         return fields.length;
251     }
252
253     /**
254      * What is the column's normal maximum width in characters?
255      *
256      * @param column the first column is 1, the second is 2, etc.
257      * @return the maximum width
258      * @throws java.sql.SQLException if a database access error occurs
259      */

260     public int getColumnDisplaySize(int column) throws java.sql.SQLException JavaDoc {
261         return getField(column).getLength();
262     }
263
264     /**
265      * What is the suggested column title for use in printouts and
266      * displays?
267      *
268      * @param column the first column is 1, the second is 2, etc.
269      * @return the column label
270      * @throws java.sql.SQLException if a database access error occurs
271      */

272     public String JavaDoc getColumnLabel(int column) throws java.sql.SQLException JavaDoc {
273         return getColumnName(column);
274     }
275
276     /**
277      * What's a column's name?
278      *
279      * @param column the first column is 1, the second is 2, etc.
280      * @return the column name
281      * @throws java.sql.SQLException if a databvase access error occurs
282      */

283     public String JavaDoc getColumnName(int column) throws java.sql.SQLException JavaDoc {
284         return getField(column).getName();
285     }
286
287     /**
288      * What is a column's SQL Type? (java.sql.Type int)
289      *
290      * @param column the first column is 1, the second is 2, etc.
291      * @return the java.sql.Type value
292      * @throws java.sql.SQLException if a database access error occurs
293      * @see java.sql.Types
294      */

295     public int getColumnType(int column) throws java.sql.SQLException JavaDoc {
296         return getField(column).getSQLType();
297     }
298
299     /**
300      * Whats is the column's data source specific type name?
301      *
302      * @param column the first column is 1, the second is 2, etc.
303      * @return the type name
304      * @throws java.sql.SQLException if a database access error occurs
305      */

306     public String JavaDoc getColumnTypeName(int column) throws java.sql.SQLException JavaDoc {
307         int mysqlType = getField(column).getMysqlType();
308
309         switch (mysqlType) {
310         case MysqlDefs.FIELD_TYPE_DECIMAL:
311             return "DECIMAL";
312
313         case MysqlDefs.FIELD_TYPE_TINY:
314             return "TINY";
315
316         case MysqlDefs.FIELD_TYPE_SHORT:
317             return "SHORT";
318
319         case MysqlDefs.FIELD_TYPE_LONG:
320             return "LONG";
321
322         case MysqlDefs.FIELD_TYPE_FLOAT:
323             return "FLOAT";
324
325         case MysqlDefs.FIELD_TYPE_DOUBLE:
326             return "DOUBLE";
327
328         case MysqlDefs.FIELD_TYPE_NULL:
329             return "NULL";
330
331         case MysqlDefs.FIELD_TYPE_TIMESTAMP:
332             return "TIMESTAMP";
333
334         case MysqlDefs.FIELD_TYPE_LONGLONG:
335             return "LONGLONG";
336
337         case MysqlDefs.FIELD_TYPE_INT24:
338             return "INT";
339
340         case MysqlDefs.FIELD_TYPE_DATE:
341             return "DATE";
342
343         case MysqlDefs.FIELD_TYPE_TIME:
344             return "TIME";
345
346         case MysqlDefs.FIELD_TYPE_DATETIME:
347             return "DATETIME";
348
349         case MysqlDefs.FIELD_TYPE_TINY_BLOB:
350             return "TINYBLOB";
351
352         case MysqlDefs.FIELD_TYPE_MEDIUM_BLOB:
353             return "MEDIUMBLOB";
354
355         case MysqlDefs.FIELD_TYPE_LONG_BLOB:
356             return "LONGBLOB";
357
358         case MysqlDefs.FIELD_TYPE_BLOB:
359
360             if (getField(column).isBinary()) {
361                 return "BLOB";
362             } else {
363                 return "TEXT";
364             }
365
366         case MysqlDefs.FIELD_TYPE_VAR_STRING:
367             return "VARCHAR";
368
369         case MysqlDefs.FIELD_TYPE_STRING:
370             return "CHAR";
371
372         case MysqlDefs.FIELD_TYPE_ENUM:
373             return "ENUM";
374
375         case MysqlDefs.FIELD_TYPE_SET:
376             return "SET";
377            
378         case MysqlDefs.FIELD_TYPE_YEAR:
379             return "YEAR";
380
381         default:
382             return "UNKNOWN";
383         }
384     }
385
386     /**
387      * Is the column a cash value?
388      *
389      * @param column the first column is 1, the second is 2...
390      * @return true if its a cash column
391      * @throws java.sql.SQLException if a database access error occurs
392      */

393     public boolean isCurrency(int column) throws java.sql.SQLException JavaDoc {
394         return false;
395     }
396
397     /**
398      * Will a write on this column definately succeed?
399      *
400      * @param column the first column is 1, the second is 2, etc..
401      * @return true if so
402      * @throws java.sql.SQLException if a database access error occurs
403      */

404     public boolean isDefinitelyWritable(int column)
405         throws java.sql.SQLException JavaDoc {
406         return isWritable(column);
407     }
408
409     /**
410      * Can you put a NULL in this column?
411      *
412      * @param column the first column is 1, the second is 2...
413      * @return one of the columnNullable values
414      * @throws java.sql.SQLException if a database access error occurs
415      */

416     public int isNullable(int column) throws java.sql.SQLException JavaDoc {
417         if (!getField(column).isNotNull()) {
418             return java.sql.ResultSetMetaData.columnNullable;
419         } else {
420             return java.sql.ResultSetMetaData.columnNoNulls;
421         }
422     }
423
424     /**
425      * What is a column's number of decimal digits.
426      *
427      * @param column the first column is 1, the second is 2...
428      * @return the precision
429      * @throws java.sql.SQLException if a database access error occurs
430      */

431     public int getPrecision(int column) throws java.sql.SQLException JavaDoc {
432         Field f = getField(column);
433
434         if (isDecimalType(f.getSQLType())) {
435             if (f.getDecimals() > 0) {
436                 return f.getLength() - 1 + f.getPrecisionAdjustFactor();
437             }
438
439             return f.getLength() + f.getPrecisionAdjustFactor();
440         }
441
442         return 0;
443     }
444
445     /**
446      * Is the column definitely not writable?
447      *
448      * @param column the first column is 1, the second is 2, etc.
449      * @return true if so
450      * @throws java.sql.SQLException if a database access error occurs
451      */

452     public boolean isReadOnly(int column) throws java.sql.SQLException JavaDoc {
453         return false;
454     }
455
456     /**
457      * What is a column's number of digits to the right of the
458      * decimal point?
459      *
460      * @param column the first column is 1, the second is 2...
461      * @return the scale
462      * @throws java.sql.SQLException if a database access error occurs
463      */

464     public int getScale(int column) throws java.sql.SQLException JavaDoc {
465         Field f = getField(column);
466
467         if (isDecimalType(f.getSQLType())) {
468             return f.getDecimals();
469         }
470
471         return 0;
472     }
473
474     /**
475      * What is a column's table's schema? This relies on us knowing
476      * the table name.
477      *
478      * The JDBC specification allows us to return "" if this is not
479      * applicable.
480      *
481      * @param column the first column is 1, the second is 2...
482      * @return the Schema
483      * @throws java.sql.SQLException if a database access error occurs
484      */

485     public String JavaDoc getSchemaName(int column) throws java.sql.SQLException JavaDoc {
486         return "";
487     }
488
489     /**
490      * Can the column be used in a WHERE clause? Basically for
491      * this, I split the functions into two types: recognised
492      * types (which are always useable), and OTHER types (which
493      * may or may not be useable). The OTHER types, for now, I
494      * will assume they are useable. We should really query the
495      * catalog to see if they are useable.
496      *
497      * @param column the first column is 1, the second is 2...
498      * @return true if they can be used in a WHERE clause
499      * @throws java.sql.SQLException if a database access error occurs
500      */

501     public boolean isSearchable(int column) throws java.sql.SQLException JavaDoc {
502         return true;
503     }
504
505     /**
506      * Is the column a signed number?
507      *
508      * @param column the first column is 1, the second is 2...
509      * @return true if so
510      * @throws java.sql.SQLException if a database access error occurs
511      */

512     public boolean isSigned(int column) throws java.sql.SQLException JavaDoc {
513         Field f = getField(column);
514         int sqlType = f.getSQLType();
515
516         switch (sqlType) {
517         case Types.TINYINT:
518         case Types.SMALLINT:
519         case Types.INTEGER:
520         case Types.BIGINT:
521         case Types.FLOAT:
522         case Types.REAL:
523         case Types.DOUBLE:
524         case Types.NUMERIC:
525         case Types.DECIMAL:
526             return !f.isUnsigned();
527
528         case Types.DATE:
529         case Types.TIME:
530         case Types.TIMESTAMP:
531             return false;
532
533         default:
534             return false;
535         }
536     }
537
538     /**
539      * Whats a column's table's name?
540      *
541      * @param column the first column is 1, the second is 2...
542      * @return column name, or "" if not applicable
543      * @throws java.sql.SQLException if a database access error occurs
544      */

545     public String JavaDoc getTableName(int column) throws java.sql.SQLException JavaDoc {
546         return getField(column).getTableName();
547     }
548
549     /**
550      * Is it possible for a write on the column to succeed?
551      *
552      * @param column the first column is 1, the second is 2, etc.
553      * @return true if so
554      * @throws java.sql.SQLException if a database access error occurs
555      */

556     public boolean isWritable(int column) throws java.sql.SQLException JavaDoc {
557         return !isReadOnly(column);
558     }
559
560     // *********************************************************************
561
//
562
// END OF PUBLIC INTERFACE
563
//
564
// *********************************************************************
565

566     /**
567      * Returns the field instance for the given column index
568      *
569      * @param columnIndex the column number to retrieve a field instance for
570      * @return the field instance for the given column index
571      *
572      * @throws java.sql.SQLException if an error occurs
573      */

574     protected Field getField(int columnIndex) throws java.sql.SQLException JavaDoc {
575         if ((columnIndex < 1) || (columnIndex > fields.length)) {
576             throw new java.sql.SQLException JavaDoc("Column index out of range.",
577                 SQLError.SQL_STATE_INVALID_COLUMN_NUMBER);
578         }
579
580         return fields[columnIndex - 1];
581     }
582
583     /**
584      * Checks if the SQL Type is a Decimal/Number Type
585      * @param type SQL Type
586      */

587     private static final boolean isDecimalType(int type) {
588         switch (type) {
589         case Types.BIT:
590         case Types.TINYINT:
591         case Types.SMALLINT:
592         case Types.INTEGER:
593         case Types.BIGINT:
594         case Types.FLOAT:
595         case Types.REAL:
596         case Types.DOUBLE:
597         case Types.NUMERIC:
598         case Types.DECIMAL:
599             return true;
600         }
601
602         return false;
603     }
604 }
605
Popular Tags