~ubuntu-branches/ubuntu/karmic/mysql-connector-java/karmic

« back to all changes in this revision

Viewing changes to com/mysql/jdbc/ResultSetMetaData.java

  • Committer: Bazaar Package Importer
  • Author(s): Takashi Okamoto
  • Date: 2003-12-30 22:37:53 UTC
  • Revision ID: james.westby@ubuntu.com-20031230223753-5d9n2b0w2ms6puqa
Tags: upstream-3.0.9
ImportĀ upstreamĀ versionĀ 3.0.9

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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;
 
22
import java.sql.Types;
 
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.3 2003/03/10 13:02:48 mmatthew Exp $
 
32
 */
 
33
public class ResultSetMetaData implements java.sql.ResultSetMetaData {
 
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 {
 
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 {
 
72
        int sqlType = getField(column).getSQLType();
 
73
 
 
74
        switch (sqlType) {
 
75
        case Types.BIT:
 
76
        case Types.TINYINT:
 
77
        case Types.SMALLINT:
 
78
        case Types.INTEGER:
 
79
        case Types.BIGINT:
 
80
        case Types.FLOAT:
 
81
        case Types.REAL:
 
82
        case Types.DOUBLE:
 
83
        case Types.DATE:
 
84
        case Types.TIME:
 
85
        case Types.TIMESTAMP:
 
86
            return false;
 
87
 
 
88
        default:
 
89
            return true;
 
90
        }
 
91
    }
 
92
 
 
93
    /**
 
94
     * What's a column's table's catalog name?
 
95
     *
 
96
     * @param column the first column is 1, the second is 2...
 
97
     * @return catalog name, or "" if not applicable
 
98
     * @throws java.sql.SQLException if a database access error occurs
 
99
     */
 
100
    public String getCatalogName(int column) throws java.sql.SQLException {
 
101
        Field f = getField(column);
 
102
 
 
103
        String database = f.getDatabaseName();
 
104
 
 
105
        return (database == null) ? "" : database;
 
106
    }
 
107
 
 
108
    //--------------------------JDBC 2.0-----------------------------------
 
109
 
 
110
    /**
 
111
     * JDBC 2.0
 
112
     *
 
113
     * <p>Return the fully qualified name of the Java class whose instances
 
114
     * are manufactured if ResultSet.getObject() is called to retrieve a value
 
115
     * from the column.  ResultSet.getObject() may return a subClass of the
 
116
     * class returned by this method.
 
117
     *
 
118
     * @param column the column number to retrieve information for
 
119
     * @return the fully qualified name of the Java class whose instances
 
120
     * are manufactured if ResultSet.getObject() is called to retrieve a value
 
121
     * from the column.
 
122
     *
 
123
     * @throws SQLException if an error occurs
 
124
     */
 
125
    public String getColumnClassName(int column) throws SQLException {
 
126
        Field f = getField(column);
 
127
 
 
128
        switch (f.getSQLType()) {
 
129
        case Types.BIT:
 
130
            return "java.lang.Boolean";
 
131
 
 
132
        case Types.TINYINT:
 
133
 
 
134
            if (f.isUnsigned()) {
 
135
                return "java.lang.Integer";
 
136
            } else {
 
137
                return "java.lang.Byte";
 
138
            }
 
139
 
 
140
        case Types.SMALLINT:
 
141
 
 
142
            if (f.isUnsigned()) {
 
143
                return "java.lang.Integer";
 
144
            } else {
 
145
                return "java.lang.Short";
 
146
            }
 
147
 
 
148
        case Types.INTEGER:
 
149
 
 
150
            if (f.isUnsigned()) {
 
151
                return "java.lang.Long";
 
152
            } else {
 
153
                return "java.lang.Integer";
 
154
            }
 
155
 
 
156
        case Types.BIGINT:
 
157
            return "java.lang.Long";
 
158
 
 
159
        case Types.DECIMAL:
 
160
        case Types.NUMERIC:
 
161
            return "java.math.BigDecimal";
 
162
 
 
163
        case Types.REAL:
 
164
        case Types.FLOAT:
 
165
            return "java.lang.Float";
 
166
 
 
167
        case Types.DOUBLE:
 
168
            return "java.lang.Double";
 
169
 
 
170
        case Types.CHAR:
 
171
        case Types.VARCHAR:
 
172
        case Types.LONGVARCHAR:
 
173
            return "java.lang.String";
 
174
 
 
175
        case Types.BINARY:
 
176
        case Types.VARBINARY:
 
177
        case Types.LONGVARBINARY:
 
178
 
 
179
            if (!f.isBlob()) {
 
180
                return "java.lang.String";
 
181
            } else if (!f.isBinary()) {
 
182
                return "java.lang.String";
 
183
            } else {
 
184
                return "java.lang.Object";
 
185
            }
 
186
 
 
187
        case Types.DATE:
 
188
            return "java.sql.Date";
 
189
 
 
190
        case Types.TIME:
 
191
            return "java.sql.Time";
 
192
 
 
193
        case Types.TIMESTAMP:
 
194
            return "java.sql.Timestamp";
 
195
 
 
196
        default:
 
197
            return "java.lang.Object";
 
198
        }
 
199
    }
 
200
 
 
201
    /**
 
202
     * Whats the number of columns in the ResultSet?
 
203
     *
 
204
     * @return the number
 
205
     * @throws java.sql.SQLException if a database access error occurs
 
206
     */
 
207
    public int getColumnCount() throws java.sql.SQLException {
 
208
        return fields.length;
 
209
    }
 
210
 
 
211
    /**
 
212
     * What is the column's normal maximum width in characters?
 
213
     *
 
214
     * @param column the first column is 1, the second is 2, etc.
 
215
     * @return the maximum width
 
216
     * @throws java.sql.SQLException if a database access error occurs
 
217
     */
 
218
    public int getColumnDisplaySize(int column) throws java.sql.SQLException {
 
219
        return getField(column).getLength();
 
220
    }
 
221
 
 
222
    /**
 
223
     * What is the suggested column title for use in printouts and
 
224
     * displays?
 
225
     *
 
226
     * @param column the first column is 1, the second is 2, etc.
 
227
     * @return the column label
 
228
     * @throws java.sql.SQLException if a database access error occurs
 
229
     */
 
230
    public String getColumnLabel(int column) throws java.sql.SQLException {
 
231
        return getColumnName(column);
 
232
    }
 
233
 
 
234
    /**
 
235
     * What's a column's name?
 
236
     *
 
237
     * @param column the first column is 1, the second is 2, etc.
 
238
     * @return the column name
 
239
     * @throws java.sql.SQLException if a databvase access error occurs
 
240
     */
 
241
    public String getColumnName(int column) throws java.sql.SQLException {
 
242
        return getField(column).getName();
 
243
    }
 
244
 
 
245
    /**
 
246
     * What is a column's SQL Type? (java.sql.Type int)
 
247
     *
 
248
     * @param column the first column is 1, the second is 2, etc.
 
249
     * @return the java.sql.Type value
 
250
     * @throws java.sql.SQLException if a database access error occurs
 
251
     * @see java.sql.Types
 
252
     */
 
253
    public int getColumnType(int column) throws java.sql.SQLException {
 
254
        return getField(column).getSQLType();
 
255
    }
 
256
 
 
257
    /**
 
258
     * Whats is the column's data source specific type name?
 
259
     *
 
260
     * @param column the first column is 1, the second is 2, etc.
 
261
     * @return the type name
 
262
     * @throws java.sql.SQLException if a database access error occurs
 
263
     */
 
264
    public String getColumnTypeName(int column) throws java.sql.SQLException {
 
265
        int mysqlType = getField(column).getMysqlType();
 
266
 
 
267
        switch (mysqlType) {
 
268
        case MysqlDefs.FIELD_TYPE_DECIMAL:
 
269
            return "DECIMAL";
 
270
 
 
271
        case MysqlDefs.FIELD_TYPE_TINY:
 
272
            return "TINY";
 
273
 
 
274
        case MysqlDefs.FIELD_TYPE_SHORT:
 
275
            return "SHORT";
 
276
 
 
277
        case MysqlDefs.FIELD_TYPE_LONG:
 
278
            return "LONG";
 
279
 
 
280
        case MysqlDefs.FIELD_TYPE_FLOAT:
 
281
            return "FLOAT";
 
282
 
 
283
        case MysqlDefs.FIELD_TYPE_DOUBLE:
 
284
            return "DOUBLE";
 
285
 
 
286
        case MysqlDefs.FIELD_TYPE_NULL:
 
287
            return "NULL";
 
288
 
 
289
        case MysqlDefs.FIELD_TYPE_TIMESTAMP:
 
290
            return "TIMESTAMP";
 
291
 
 
292
        case MysqlDefs.FIELD_TYPE_LONGLONG:
 
293
            return "LONGLONG";
 
294
 
 
295
        case MysqlDefs.FIELD_TYPE_INT24:
 
296
            return "INT";
 
297
 
 
298
        case MysqlDefs.FIELD_TYPE_DATE:
 
299
            return "DATE";
 
300
 
 
301
        case MysqlDefs.FIELD_TYPE_TIME:
 
302
            return "TIME";
 
303
 
 
304
        case MysqlDefs.FIELD_TYPE_DATETIME:
 
305
            return "DATETIME";
 
306
 
 
307
        case MysqlDefs.FIELD_TYPE_TINY_BLOB:
 
308
            return "TINYBLOB";
 
309
 
 
310
        case MysqlDefs.FIELD_TYPE_MEDIUM_BLOB:
 
311
            return "MEDIUMBLOB";
 
312
 
 
313
        case MysqlDefs.FIELD_TYPE_LONG_BLOB:
 
314
            return "LONGBLOB";
 
315
 
 
316
        case MysqlDefs.FIELD_TYPE_BLOB:
 
317
 
 
318
            if (getField(column).isBinary()) {
 
319
                return "BLOB";
 
320
            } else {
 
321
                return "TEXT";
 
322
            }
 
323
 
 
324
        case MysqlDefs.FIELD_TYPE_VAR_STRING:
 
325
            return "VARCHAR";
 
326
 
 
327
        case MysqlDefs.FIELD_TYPE_STRING:
 
328
            return "CHAR";
 
329
 
 
330
        case MysqlDefs.FIELD_TYPE_ENUM:
 
331
            return "ENUM";
 
332
 
 
333
        case MysqlDefs.FIELD_TYPE_SET:
 
334
            return "SET";
 
335
           
 
336
        case MysqlDefs.FIELD_TYPE_YEAR:
 
337
                return "YEAR";
 
338
 
 
339
        default:
 
340
            return "UNKNOWN";
 
341
        }
 
342
    }
 
343
 
 
344
    /**
 
345
     * Is the column a cash value?
 
346
     *
 
347
     * @param column the first column is 1, the second is 2...
 
348
     * @return true if its a cash column
 
349
     * @throws java.sql.SQLException if a database access error occurs
 
350
     */
 
351
    public boolean isCurrency(int column) throws java.sql.SQLException {
 
352
        return false;
 
353
    }
 
354
 
 
355
    /**
 
356
     * Will a write on this column definately succeed?
 
357
     *
 
358
     * @param column the first column is 1, the second is 2, etc..
 
359
     * @return true if so
 
360
     * @throws java.sql.SQLException if a database access error occurs
 
361
     */
 
362
    public boolean isDefinitelyWritable(int column)
 
363
        throws java.sql.SQLException {
 
364
        return isWritable(column);
 
365
    }
 
366
 
 
367
    /**
 
368
     * Can you put a NULL in this column?
 
369
     *
 
370
     * @param column the first column is 1, the second is 2...
 
371
     * @return one of the columnNullable values
 
372
     * @throws java.sql.SQLException if a database access error occurs
 
373
     */
 
374
    public int isNullable(int column) throws java.sql.SQLException {
 
375
        if (!getField(column).isNotNull()) {
 
376
            return java.sql.ResultSetMetaData.columnNullable;
 
377
        } else {
 
378
            return java.sql.ResultSetMetaData.columnNoNulls;
 
379
        }
 
380
    }
 
381
 
 
382
    /**
 
383
     * What is a column's number of decimal digits.
 
384
     *
 
385
     * @param column the first column is 1, the second is 2...
 
386
     * @return the precision
 
387
     * @throws java.sql.SQLException if a database access error occurs
 
388
     */
 
389
    public int getPrecision(int column) throws java.sql.SQLException {
 
390
        Field f = getField(column);
 
391
 
 
392
        if (isDecimalType(f.getSQLType())) {
 
393
            if (f.getDecimals() > 0) {
 
394
                return f.getLength() - 1 + f.getPrecisionAdjustFactor();
 
395
            }
 
396
 
 
397
            return f.getLength() + f.getPrecisionAdjustFactor();
 
398
        }
 
399
 
 
400
        return 0;
 
401
    }
 
402
 
 
403
    /**
 
404
     * Is the column definitely not writable?
 
405
     *
 
406
     * @param column the first column is 1, the second is 2, etc.
 
407
     * @return true if so
 
408
     * @throws java.sql.SQLException if a database access error occurs
 
409
     */
 
410
    public boolean isReadOnly(int column) throws java.sql.SQLException {
 
411
        return false;
 
412
    }
 
413
 
 
414
    /**
 
415
     * What is a column's number of digits to the right of the
 
416
     * decimal point?
 
417
     *
 
418
     * @param column the first column is 1, the second is 2...
 
419
     * @return the scale
 
420
     * @throws java.sql.SQLException if a database access error occurs
 
421
     */
 
422
    public int getScale(int column) throws java.sql.SQLException {
 
423
        Field f = getField(column);
 
424
 
 
425
        if (isDecimalType(f.getSQLType())) {
 
426
            return f.getDecimals();
 
427
        }
 
428
 
 
429
        return 0;
 
430
    }
 
431
 
 
432
    /**
 
433
     * What is a column's table's schema?  This relies on us knowing
 
434
     * the table name.
 
435
     *
 
436
     * The JDBC specification allows us to return "" if this is not
 
437
     * applicable.
 
438
     *
 
439
     * @param column the first column is 1, the second is 2...
 
440
     * @return the Schema
 
441
     * @throws java.sql.SQLException if a database access error occurs
 
442
     */
 
443
    public String getSchemaName(int column) throws java.sql.SQLException {
 
444
        return "";
 
445
    }
 
446
 
 
447
    /**
 
448
     * Can the column be used in a WHERE clause?  Basically for
 
449
     * this, I split the functions into two types: recognised
 
450
     * types (which are always useable), and OTHER types (which
 
451
     * may or may not be useable).  The OTHER types, for now, I
 
452
     * will assume they are useable.  We should really query the
 
453
     * catalog to see if they are useable.
 
454
     *
 
455
     * @param column the first column is 1, the second is 2...
 
456
     * @return true if they can be used in a WHERE clause
 
457
     * @throws java.sql.SQLException if a database access error occurs
 
458
     */
 
459
    public boolean isSearchable(int column) throws java.sql.SQLException {
 
460
        return true;
 
461
    }
 
462
 
 
463
    /**
 
464
     * Is the column a signed number?
 
465
     *
 
466
     * @param column the first column is 1, the second is 2...
 
467
     * @return true if so
 
468
     * @throws java.sql.SQLException if a database access error occurs
 
469
     */
 
470
    public boolean isSigned(int column) throws java.sql.SQLException {
 
471
        Field f = getField(column);
 
472
        int sqlType = f.getSQLType();
 
473
 
 
474
        switch (sqlType) {
 
475
        case Types.TINYINT:
 
476
        case Types.SMALLINT:
 
477
        case Types.INTEGER:
 
478
        case Types.BIGINT:
 
479
        case Types.FLOAT:
 
480
        case Types.REAL:
 
481
        case Types.DOUBLE:
 
482
        case Types.NUMERIC:
 
483
        case Types.DECIMAL:
 
484
            return !f.isUnsigned();
 
485
 
 
486
        case Types.DATE:
 
487
        case Types.TIME:
 
488
        case Types.TIMESTAMP:
 
489
            return false;
 
490
 
 
491
        default:
 
492
            return false;
 
493
        }
 
494
    }
 
495
 
 
496
    /**
 
497
     * Whats a column's table's name?
 
498
     *
 
499
     * @param column the first column is 1, the second is 2...
 
500
     * @return column name, or "" if not applicable
 
501
     * @throws java.sql.SQLException if a database access error occurs
 
502
     */
 
503
    public String getTableName(int column) throws java.sql.SQLException {
 
504
        return getField(column).getTableName();
 
505
    }
 
506
 
 
507
    /**
 
508
     * Is it possible for a write on the column to succeed?
 
509
     *
 
510
     * @param column the first column is 1, the second is 2, etc.
 
511
     * @return true if so
 
512
     * @throws java.sql.SQLException if a database access error occurs
 
513
     */
 
514
    public boolean isWritable(int column) throws java.sql.SQLException {
 
515
        return !isReadOnly(column);
 
516
    }
 
517
 
 
518
    // *********************************************************************
 
519
    //
 
520
    //                END OF PUBLIC INTERFACE
 
521
    //
 
522
    // *********************************************************************
 
523
 
 
524
    /**
 
525
     * Returns the field instance for the given column index
 
526
     *
 
527
     * @param columnIndex the column number to retrieve a field instance for
 
528
     * @return the field instance for the given column index
 
529
     *
 
530
     * @throws java.sql.SQLException if an error occurs
 
531
     */
 
532
    protected Field getField(int columnIndex) throws java.sql.SQLException {
 
533
        if ((columnIndex < 1) || (columnIndex > fields.length)) {
 
534
            throw new java.sql.SQLException("Column index out of range.",
 
535
                "S1002");
 
536
        }
 
537
 
 
538
        return fields[columnIndex - 1];
 
539
    }
 
540
 
 
541
    /**
 
542
     * Checks if the SQL Type is a Decimal/Number Type
 
543
     * @param type SQL Type
 
544
     */
 
545
    private static final boolean isDecimalType(int type) {
 
546
        switch (type) {
 
547
        case Types.BIT:
 
548
        case Types.TINYINT:
 
549
        case Types.SMALLINT:
 
550
        case Types.INTEGER:
 
551
        case Types.BIGINT:
 
552
        case Types.FLOAT:
 
553
        case Types.REAL:
 
554
        case Types.DOUBLE:
 
555
        case Types.NUMERIC:
 
556
        case Types.DECIMAL:
 
557
            return true;
 
558
        }
 
559
 
 
560
        return false;
 
561
    }
 
562
}