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

« back to all changes in this revision

Viewing changes to .pc/0002-java6-compilation-compat.patch/src/com/mysql/jdbc/ResultSetMetaData.java

  • Committer: Package Import Robot
  • Author(s): Emmanuel Bourg, Miguel Landaeta
  • Date: 2013-07-02 17:07:51 UTC
  • mfrom: (1.1.8) (3.1.6 sid)
  • Revision ID: package-import@ubuntu.com-20130702170751-f4rszjabxg0391fr
Tags: 5.1.25-1
* New upstream release
* Refreshed the patches
* Added a patch to build with one JDK and removed the build
  dependency on java-gcj-compat-dev
* Updated Standards-Version to 3.9.4 (no changes)
* Use canonical URLs for the Vcs-* fields
* debian/rules: Improved the clean target to allow rebuilds
* Updated the watch file
* Renamed debian/README.Debian-source to README.source

[ Miguel Landaeta ] 
* Fix FTBFS with OpenJDK 7 (Closes: #706668)
* Remove Michael Koch from Uploaders list.
  Thanks for your work on this package. (Closes: #654122).

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 Copyright (c) 2002, 2012, Oracle and/or its affiliates. All rights reserved.
 
3
 
 
4
 
 
5
  The MySQL Connector/J is licensed under the terms of the GPLv2
 
6
  <http://www.gnu.org/licenses/old-licenses/gpl-2.0.html>, like most MySQL Connectors.
 
7
  There are special exceptions to the terms and conditions of the GPLv2 as it is applied to
 
8
  this software, see the FLOSS License Exception
 
9
  <http://www.mysql.com/about/legal/licensing/foss-exception.html>.
 
10
 
 
11
  This program is free software; you can redistribute it and/or modify it under the terms
 
12
  of the GNU General Public License as published by the Free Software Foundation; version 2
 
13
  of the License.
 
14
 
 
15
  This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
 
16
  without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 
17
  See the GNU General Public License for more details.
 
18
 
 
19
  You should have received a copy of the GNU General Public License along with this
 
20
  program; if not, write to the Free Software Foundation, Inc., 51 Franklin St, Fifth
 
21
  Floor, Boston, MA 02110-1301  USA
 
22
 
 
23
 */
 
24
package com.mysql.jdbc;
 
25
 
 
26
import java.sql.SQLException;
 
27
import java.sql.Types;
 
28
 
 
29
/**
 
30
 * A ResultSetMetaData object can be used to find out about the types and
 
31
 * properties of the columns in a ResultSet
 
32
 * 
 
33
 * @author Mark Matthews
 
34
 * @version $Id: ResultSetMetaData.java,v 1.1.2.1 2005/05/13 18:58:38 mmatthews
 
35
 *          Exp $
 
36
 * 
 
37
 * @see java.sql.ResultSetMetaData
 
38
 */
 
39
public class ResultSetMetaData implements java.sql.ResultSetMetaData {
 
40
        private static int clampedGetLength(Field f) {
 
41
                long fieldLength = f.getLength();
 
42
 
 
43
                if (fieldLength > Integer.MAX_VALUE) {
 
44
                        fieldLength = Integer.MAX_VALUE;
 
45
                }
 
46
 
 
47
                return (int) fieldLength;
 
48
        }
 
49
 
 
50
        /**
 
51
         * Checks if the SQL Type is a Decimal/Number Type
 
52
         * 
 
53
         * @param type
 
54
         *            SQL Type
 
55
         * 
 
56
         * @return ...
 
57
         */
 
58
        private static final boolean isDecimalType(int type) {
 
59
                switch (type) {
 
60
                case Types.BIT:
 
61
                case Types.TINYINT:
 
62
                case Types.SMALLINT:
 
63
                case Types.INTEGER:
 
64
                case Types.BIGINT:
 
65
                case Types.FLOAT:
 
66
                case Types.REAL:
 
67
                case Types.DOUBLE:
 
68
                case Types.NUMERIC:
 
69
                case Types.DECIMAL:
 
70
                        return true;
 
71
                }
 
72
 
 
73
                return false;
 
74
        }
 
75
 
 
76
        Field[] fields;
 
77
        boolean useOldAliasBehavior = false;
 
78
 
 
79
        private ExceptionInterceptor exceptionInterceptor;
 
80
        
 
81
        /**
 
82
         * Initialise for a result with a tuple set and a field descriptor set
 
83
         * 
 
84
         * @param fields
 
85
         *            the array of field descriptors
 
86
         */
 
87
        public ResultSetMetaData(Field[] fields, boolean useOldAliasBehavior, ExceptionInterceptor exceptionInterceptor) {
 
88
                this.fields = fields;
 
89
                this.useOldAliasBehavior = useOldAliasBehavior;
 
90
                this.exceptionInterceptor = exceptionInterceptor;
 
91
        }
 
92
 
 
93
        /**
 
94
         * What's a column's table's catalog name?
 
95
         * 
 
96
         * @param column
 
97
         *            the first column is 1, the second is 2...
 
98
         * 
 
99
         * @return catalog name, or "" if not applicable
 
100
         * 
 
101
         * @throws SQLException
 
102
         *             if a database access error occurs
 
103
         */
 
104
        public String getCatalogName(int column) throws SQLException {
 
105
                Field f = getField(column);
 
106
 
 
107
                String database = f.getDatabaseName();
 
108
 
 
109
                return (database == null) ? "" : database; //$NON-NLS-1$
 
110
        }
 
111
 
 
112
        /**
 
113
         * What's the Java character encoding name for the given column?
 
114
         * 
 
115
         * @param column
 
116
         *            the first column is 1, the second is 2, etc.
 
117
         * 
 
118
         * @return the Java character encoding name for the given column, or null if
 
119
         *         no Java character encoding maps to the MySQL character set for
 
120
         *         the given column.
 
121
         * 
 
122
         * @throws SQLException
 
123
         *             if an invalid column index is given.
 
124
         */
 
125
        public String getColumnCharacterEncoding(int column) throws SQLException {
 
126
                String mysqlName = getColumnCharacterSet(column);
 
127
 
 
128
                String javaName = null;
 
129
 
 
130
                if (mysqlName != null) {
 
131
                        try {
 
132
                                javaName = CharsetMapping.MYSQL_TO_JAVA_CHARSET_MAP.get(mysqlName);
 
133
                        } catch (RuntimeException ex) {
 
134
                                SQLException sqlEx = SQLError.createSQLException(ex.toString(), SQLError.SQL_STATE_ILLEGAL_ARGUMENT, null);
 
135
                                sqlEx.initCause(ex);
 
136
                                throw sqlEx;
 
137
                        }
 
138
                }
 
139
 
 
140
                return javaName;
 
141
        }
 
142
 
 
143
        /**
 
144
         * What's the MySQL character set name for the given column?
 
145
         * 
 
146
         * @param column
 
147
         *            the first column is 1, the second is 2, etc.
 
148
         * 
 
149
         * @return the MySQL character set name for the given column
 
150
         * 
 
151
         * @throws SQLException
 
152
         *             if an invalid column index is given.
 
153
         */
 
154
        public String getColumnCharacterSet(int column) throws SQLException {
 
155
                return getField(column).getCharacterSet();
 
156
        }
 
157
 
 
158
        // --------------------------JDBC 2.0-----------------------------------
 
159
 
 
160
        /**
 
161
         * JDBC 2.0
 
162
         * 
 
163
         * <p>
 
164
         * Return the fully qualified name of the Java class whose instances are
 
165
         * manufactured if ResultSet.getObject() is called to retrieve a value from
 
166
         * the column. ResultSet.getObject() may return a subClass of the class
 
167
         * returned by this method.
 
168
         * </p>
 
169
         * 
 
170
         * @param column
 
171
         *            the column number to retrieve information for
 
172
         * 
 
173
         * @return the fully qualified name of the Java class whose instances are
 
174
         *         manufactured if ResultSet.getObject() is called to retrieve a
 
175
         *         value from the column.
 
176
         * 
 
177
         * @throws SQLException
 
178
         *             if an error occurs
 
179
         */
 
180
        public String getColumnClassName(int column) throws SQLException {
 
181
                Field f = getField(column);
 
182
 
 
183
                return getClassNameForJavaType(f.getSQLType(), 
 
184
                                f.isUnsigned(), 
 
185
                                f.getMysqlType(), 
 
186
                                f.isBinary() || f.isBlob(),
 
187
                                f.isOpaqueBinary()); 
 
188
        }
 
189
 
 
190
        /**
 
191
         * Whats the number of columns in the ResultSet?
 
192
         * 
 
193
         * @return the number
 
194
         * 
 
195
         * @throws SQLException
 
196
         *             if a database access error occurs
 
197
         */
 
198
        public int getColumnCount() throws SQLException {
 
199
                return this.fields.length;
 
200
        }
 
201
 
 
202
        /**
 
203
         * What is the column's normal maximum width in characters?
 
204
         * 
 
205
         * @param column
 
206
         *            the first column is 1, the second is 2, etc.
 
207
         * 
 
208
         * @return the maximum width
 
209
         * 
 
210
         * @throws SQLException
 
211
         *             if a database access error occurs
 
212
         */
 
213
        public int getColumnDisplaySize(int column) throws SQLException {
 
214
                Field f = getField(column);
 
215
 
 
216
                int lengthInBytes = clampedGetLength(f);
 
217
 
 
218
                return lengthInBytes / f.getMaxBytesPerCharacter();
 
219
        }
 
220
 
 
221
        /**
 
222
         * What is the suggested column title for use in printouts and displays?
 
223
         * 
 
224
         * @param column
 
225
         *            the first column is 1, the second is 2, etc.
 
226
         * 
 
227
         * @return the column label
 
228
         * 
 
229
         * @throws SQLException
 
230
         *             if a database access error occurs
 
231
         */
 
232
        public String getColumnLabel(int column) throws SQLException {
 
233
                if (this.useOldAliasBehavior) {
 
234
                        return getColumnName(column);
 
235
                }
 
236
                
 
237
                return getField(column).getColumnLabel();
 
238
        }
 
239
 
 
240
        /**
 
241
         * What's a column's name?
 
242
         * 
 
243
         * @param column
 
244
         *            the first column is 1, the second is 2, etc.
 
245
         * 
 
246
         * @return the column name
 
247
         * 
 
248
         * @throws SQLException
 
249
         *             if a databvase access error occurs
 
250
         */
 
251
        public String getColumnName(int column) throws SQLException {
 
252
                if (this.useOldAliasBehavior) {
 
253
                        return getField(column).getName();
 
254
        }
 
255
 
 
256
                String name = getField(column).getNameNoAliases();
 
257
                
 
258
                if (name != null && name.length() == 0) {
 
259
                        return getField(column).getName();
 
260
                }
 
261
                
 
262
                return name;
 
263
        }
 
264
 
 
265
        /**
 
266
         * What is a column's SQL Type? (java.sql.Type int)
 
267
         * 
 
268
         * @param column
 
269
         *            the first column is 1, the second is 2, etc.
 
270
         * 
 
271
         * @return the java.sql.Type value
 
272
         * 
 
273
         * @throws SQLException
 
274
         *             if a database access error occurs
 
275
         * 
 
276
         * @see java.sql.Types
 
277
         */
 
278
        public int getColumnType(int column) throws SQLException {
 
279
                return getField(column).getSQLType();
 
280
        }
 
281
 
 
282
        /**
 
283
         * Whats is the column's data source specific type name?
 
284
         * 
 
285
         * @param column
 
286
         *            the first column is 1, the second is 2, etc.
 
287
         * 
 
288
         * @return the type name
 
289
         * 
 
290
         * @throws SQLException
 
291
         *             if a database access error occurs
 
292
         */
 
293
        public String getColumnTypeName(int column) throws java.sql.SQLException {
 
294
                Field field = getField(column);
 
295
 
 
296
                int mysqlType = field.getMysqlType();
 
297
                int jdbcType = field.getSQLType();
 
298
 
 
299
                switch (mysqlType) {
 
300
                case MysqlDefs.FIELD_TYPE_BIT:
 
301
                        return "BIT";
 
302
                case MysqlDefs.FIELD_TYPE_DECIMAL:
 
303
                case MysqlDefs.FIELD_TYPE_NEW_DECIMAL:
 
304
                        return field.isUnsigned() ? "DECIMAL UNSIGNED" : "DECIMAL";
 
305
 
 
306
                case MysqlDefs.FIELD_TYPE_TINY:
 
307
                        return field.isUnsigned() ? "TINYINT UNSIGNED" : "TINYINT";
 
308
 
 
309
                case MysqlDefs.FIELD_TYPE_SHORT:
 
310
                        return field.isUnsigned() ? "SMALLINT UNSIGNED" : "SMALLINT";
 
311
 
 
312
                case MysqlDefs.FIELD_TYPE_LONG:
 
313
                        return field.isUnsigned() ? "INT UNSIGNED" : "INT";
 
314
 
 
315
                case MysqlDefs.FIELD_TYPE_FLOAT:
 
316
                        return field.isUnsigned() ? "FLOAT UNSIGNED" : "FLOAT";
 
317
 
 
318
                case MysqlDefs.FIELD_TYPE_DOUBLE:
 
319
                        return field.isUnsigned() ? "DOUBLE UNSIGNED" : "DOUBLE";
 
320
 
 
321
                case MysqlDefs.FIELD_TYPE_NULL:
 
322
                        return "NULL"; //$NON-NLS-1$
 
323
 
 
324
                case MysqlDefs.FIELD_TYPE_TIMESTAMP:
 
325
                        return "TIMESTAMP"; //$NON-NLS-1$       
 
326
 
 
327
                case MysqlDefs.FIELD_TYPE_LONGLONG:
 
328
                        return field.isUnsigned() ? "BIGINT UNSIGNED" : "BIGINT";
 
329
 
 
330
                case MysqlDefs.FIELD_TYPE_INT24:
 
331
                        return field.isUnsigned() ? "MEDIUMINT UNSIGNED" : "MEDIUMINT";
 
332
 
 
333
                case MysqlDefs.FIELD_TYPE_DATE:
 
334
                        return "DATE"; //$NON-NLS-1$       
 
335
 
 
336
                case MysqlDefs.FIELD_TYPE_TIME:
 
337
                        return "TIME"; //$NON-NLS-1$       
 
338
 
 
339
                case MysqlDefs.FIELD_TYPE_DATETIME:
 
340
                        return "DATETIME"; //$NON-NLS-1$      
 
341
 
 
342
                case MysqlDefs.FIELD_TYPE_TINY_BLOB:
 
343
                        return "TINYBLOB"; //$NON-NLS-1$
 
344
 
 
345
                case MysqlDefs.FIELD_TYPE_MEDIUM_BLOB:
 
346
                        return "MEDIUMBLOB"; //$NON-NLS-1$
 
347
 
 
348
                case MysqlDefs.FIELD_TYPE_LONG_BLOB:
 
349
                        return "LONGBLOB"; //$NON-NLS-1$
 
350
 
 
351
                case MysqlDefs.FIELD_TYPE_BLOB:
 
352
                        if (getField(column).isBinary()) {
 
353
                                return "BLOB";//$NON-NLS-1$
 
354
                        }
 
355
 
 
356
                        return "TEXT";//$NON-NLS-1$
 
357
 
 
358
                case MysqlDefs.FIELD_TYPE_VARCHAR:
 
359
                        return "VARCHAR"; //$NON-NLS-1$
 
360
 
 
361
                case MysqlDefs.FIELD_TYPE_VAR_STRING:
 
362
                        if (jdbcType == Types.VARBINARY) {
 
363
                                return "VARBINARY";
 
364
                        }
 
365
                        
 
366
                        return "VARCHAR"; //$NON-NLS-1$
 
367
 
 
368
                case MysqlDefs.FIELD_TYPE_STRING:
 
369
                        if (jdbcType == Types.BINARY) {
 
370
                                return "BINARY";
 
371
                        }
 
372
                        
 
373
                        return "CHAR"; //$NON-NLS-1$
 
374
 
 
375
                case MysqlDefs.FIELD_TYPE_ENUM:
 
376
                        return "ENUM"; //$NON-NLS-1$
 
377
 
 
378
                case MysqlDefs.FIELD_TYPE_YEAR:
 
379
                        return "YEAR"; // $NON_NLS-1$
 
380
 
 
381
                case MysqlDefs.FIELD_TYPE_SET:
 
382
                        return "SET"; //$NON-NLS-1$
 
383
                        
 
384
                case MysqlDefs.FIELD_TYPE_GEOMETRY:
 
385
                        return "GEOMETRY"; //$NON-NLS-1$
 
386
                        
 
387
                default:
 
388
                        return "UNKNOWN"; //$NON-NLS-1$
 
389
                }
 
390
        }
 
391
 
 
392
        /**
 
393
         * Returns the field instance for the given column index
 
394
         * 
 
395
         * @param columnIndex
 
396
         *            the column number to retrieve a field instance for
 
397
         * 
 
398
         * @return the field instance for the given column index
 
399
         * 
 
400
         * @throws SQLException
 
401
         *             if an error occurs
 
402
         */
 
403
        protected Field getField(int columnIndex) throws SQLException {
 
404
                if ((columnIndex < 1) || (columnIndex > this.fields.length)) {
 
405
                        throw SQLError.createSQLException(Messages.getString("ResultSetMetaData.46"), //$NON-NLS-1$
 
406
                                        SQLError.SQL_STATE_INVALID_COLUMN_NUMBER, this.exceptionInterceptor);
 
407
                }
 
408
 
 
409
                return this.fields[columnIndex - 1];
 
410
        }
 
411
 
 
412
        /**
 
413
         * What is a column's number of decimal digits.
 
414
         * 
 
415
         * @param column
 
416
         *            the first column is 1, the second is 2...
 
417
         * 
 
418
         * @return the precision
 
419
         * 
 
420
         * @throws SQLException
 
421
         *             if a database access error occurs
 
422
         */
 
423
        public int getPrecision(int column) throws SQLException {
 
424
                Field f = getField(column);
 
425
 
 
426
                // if (f.getMysqlType() == MysqlDefs.FIELD_TYPE_NEW_DECIMAL) {
 
427
                // return f.getLength();
 
428
                // }
 
429
 
 
430
                if (isDecimalType(f.getSQLType())) {
 
431
                        if (f.getDecimals() > 0) {
 
432
                                return clampedGetLength(f) - 1 + f.getPrecisionAdjustFactor();
 
433
                        }
 
434
 
 
435
                        return clampedGetLength(f) + f.getPrecisionAdjustFactor();
 
436
                }
 
437
 
 
438
                switch (f.getMysqlType()) {
 
439
                case MysqlDefs.FIELD_TYPE_TINY_BLOB:
 
440
                case MysqlDefs.FIELD_TYPE_BLOB:
 
441
                case MysqlDefs.FIELD_TYPE_MEDIUM_BLOB:
 
442
                case MysqlDefs.FIELD_TYPE_LONG_BLOB:
 
443
                        return clampedGetLength(f); // this may change in the future
 
444
                // for now, the server only
 
445
                // returns FIELD_TYPE_BLOB for _all_
 
446
                // BLOB types, but varying lengths
 
447
                // indicating the _maximum_ size
 
448
                // for each BLOB type.
 
449
                default:
 
450
                        return clampedGetLength(f) / f.getMaxBytesPerCharacter();
 
451
 
 
452
                }
 
453
        }
 
454
 
 
455
        /**
 
456
         * What is a column's number of digits to the right of the decimal point?
 
457
         * 
 
458
         * @param column
 
459
         *            the first column is 1, the second is 2...
 
460
         * 
 
461
         * @return the scale
 
462
         * 
 
463
         * @throws SQLException
 
464
         *             if a database access error occurs
 
465
         */
 
466
        public int getScale(int column) throws SQLException {
 
467
                Field f = getField(column);
 
468
 
 
469
                if (isDecimalType(f.getSQLType())) {
 
470
                        return f.getDecimals();
 
471
                }
 
472
 
 
473
                return 0;
 
474
        }
 
475
 
 
476
        /**
 
477
         * What is a column's table's schema? This relies on us knowing the table
 
478
         * name. The JDBC specification allows us to return "" if this is not
 
479
         * applicable.
 
480
         * 
 
481
         * @param column
 
482
         *            the first column is 1, the second is 2...
 
483
         * 
 
484
         * @return the Schema
 
485
         * 
 
486
         * @throws SQLException
 
487
         *             if a database access error occurs
 
488
         */
 
489
        public String getSchemaName(int column) throws SQLException {
 
490
                return ""; //$NON-NLS-1$
 
491
        }
 
492
 
 
493
        /**
 
494
         * Whats a column's table's name?
 
495
         * 
 
496
         * @param column
 
497
         *            the first column is 1, the second is 2...
 
498
         * 
 
499
         * @return column name, or "" if not applicable
 
500
         * 
 
501
         * @throws SQLException
 
502
         *             if a database access error occurs
 
503
         */
 
504
        public String getTableName(int column) throws SQLException {
 
505
                if (this.useOldAliasBehavior) {
 
506
                        return getField(column).getTableName();
 
507
                }
 
508
                
 
509
                return getField(column).getTableNameNoAliases();
 
510
        }
 
511
 
 
512
        /**
 
513
         * Is the column automatically numbered (and thus read-only)
 
514
         * 
 
515
         * @param column
 
516
         *            the first column is 1, the second is 2...
 
517
         * 
 
518
         * @return true if so
 
519
         * 
 
520
         * @throws SQLException
 
521
         *             if a database access error occurs
 
522
         */
 
523
        public boolean isAutoIncrement(int column) throws SQLException {
 
524
                Field f = getField(column);
 
525
 
 
526
                return f.isAutoIncrement();
 
527
        }
 
528
 
 
529
        /**
 
530
         * Does a column's case matter?
 
531
         * 
 
532
         * @param column
 
533
         *            the first column is 1, the second is 2...
 
534
         * 
 
535
         * @return true if so
 
536
         * 
 
537
         * @throws java.sql.SQLException
 
538
         *             if a database access error occurs
 
539
         */
 
540
        public boolean isCaseSensitive(int column) throws java.sql.SQLException {
 
541
                Field field = getField(column);
 
542
 
 
543
                int sqlType = field.getSQLType();
 
544
 
 
545
                switch (sqlType) {
 
546
                case Types.BIT:
 
547
                case Types.TINYINT:
 
548
                case Types.SMALLINT:
 
549
                case Types.INTEGER:
 
550
                case Types.BIGINT:
 
551
                case Types.FLOAT:
 
552
                case Types.REAL:
 
553
                case Types.DOUBLE:
 
554
                case Types.DATE:
 
555
                case Types.TIME:
 
556
                case Types.TIMESTAMP:
 
557
                        return false;
 
558
 
 
559
                case Types.CHAR:
 
560
                case Types.VARCHAR:
 
561
                case Types.LONGVARCHAR:
 
562
 
 
563
                        if (field.isBinary()) {
 
564
                                return true;
 
565
                        }
 
566
 
 
567
                        String collationName = field.getCollation();
 
568
 
 
569
                        return ((collationName != null) && !collationName.endsWith("_ci"));
 
570
 
 
571
                default:
 
572
                        return true;
 
573
                }
 
574
        }
 
575
 
 
576
        /**
 
577
         * Is the column a cash value?
 
578
         * 
 
579
         * @param column
 
580
         *            the first column is 1, the second is 2...
 
581
         * 
 
582
         * @return true if its a cash column
 
583
         * 
 
584
         * @throws SQLException
 
585
         *             if a database access error occurs
 
586
         */
 
587
        public boolean isCurrency(int column) throws SQLException {
 
588
                return false;
 
589
        }
 
590
 
 
591
        /**
 
592
         * Will a write on this column definately succeed?
 
593
         * 
 
594
         * @param column
 
595
         *            the first column is 1, the second is 2, etc..
 
596
         * 
 
597
         * @return true if so
 
598
         * 
 
599
         * @throws SQLException
 
600
         *             if a database access error occurs
 
601
         */
 
602
        public boolean isDefinitelyWritable(int column) throws SQLException {
 
603
                return isWritable(column);
 
604
        }
 
605
 
 
606
        /**
 
607
         * Can you put a NULL in this column?
 
608
         * 
 
609
         * @param column
 
610
         *            the first column is 1, the second is 2...
 
611
         * 
 
612
         * @return one of the columnNullable values
 
613
         * 
 
614
         * @throws SQLException
 
615
         *             if a database access error occurs
 
616
         */
 
617
        public int isNullable(int column) throws SQLException {
 
618
                if (!getField(column).isNotNull()) {
 
619
                        return java.sql.ResultSetMetaData.columnNullable;
 
620
                }
 
621
 
 
622
                return java.sql.ResultSetMetaData.columnNoNulls;
 
623
        }
 
624
 
 
625
        /**
 
626
         * Is the column definitely not writable?
 
627
         * 
 
628
         * @param column
 
629
         *            the first column is 1, the second is 2, etc.
 
630
         * 
 
631
         * @return true if so
 
632
         * 
 
633
         * @throws SQLException
 
634
         *             if a database access error occurs
 
635
         */
 
636
        public boolean isReadOnly(int column) throws SQLException {
 
637
                return getField(column).isReadOnly();
 
638
        }
 
639
 
 
640
        /**
 
641
         * Can the column be used in a WHERE clause? Basically for this, I split the
 
642
         * functions into two types: recognised types (which are always useable),
 
643
         * and OTHER types (which may or may not be useable). The OTHER types, for
 
644
         * now, I will assume they are useable. We should really query the catalog
 
645
         * to see if they are useable.
 
646
         * 
 
647
         * @param column
 
648
         *            the first column is 1, the second is 2...
 
649
         * 
 
650
         * @return true if they can be used in a WHERE clause
 
651
         * 
 
652
         * @throws SQLException
 
653
         *             if a database access error occurs
 
654
         */
 
655
        public boolean isSearchable(int column) throws SQLException {
 
656
                return true;
 
657
        }
 
658
 
 
659
        /**
 
660
         * Is the column a signed number?
 
661
         * 
 
662
         * @param column
 
663
         *            the first column is 1, the second is 2...
 
664
         * 
 
665
         * @return true if so
 
666
         * 
 
667
         * @throws SQLException
 
668
         *             if a database access error occurs
 
669
         */
 
670
        public boolean isSigned(int column) throws SQLException {
 
671
                Field f = getField(column);
 
672
                int sqlType = f.getSQLType();
 
673
 
 
674
                switch (sqlType) {
 
675
                case Types.TINYINT:
 
676
                case Types.SMALLINT:
 
677
                case Types.INTEGER:
 
678
                case Types.BIGINT:
 
679
                case Types.FLOAT:
 
680
                case Types.REAL:
 
681
                case Types.DOUBLE:
 
682
                case Types.NUMERIC:
 
683
                case Types.DECIMAL:
 
684
                        return !f.isUnsigned();
 
685
 
 
686
                case Types.DATE:
 
687
                case Types.TIME:
 
688
                case Types.TIMESTAMP:
 
689
                        return false;
 
690
 
 
691
                default:
 
692
                        return false;
 
693
                }
 
694
        }
 
695
 
 
696
        /**
 
697
         * Is it possible for a write on the column to succeed?
 
698
         * 
 
699
         * @param column
 
700
         *            the first column is 1, the second is 2, etc.
 
701
         * 
 
702
         * @return true if so
 
703
         * 
 
704
         * @throws SQLException
 
705
         *             if a database access error occurs
 
706
         */
 
707
        public boolean isWritable(int column) throws SQLException {
 
708
                return !isReadOnly(column);
 
709
        }
 
710
 
 
711
        /**
 
712
         * Returns a string representation of this object
 
713
         * 
 
714
         * @return ...
 
715
         */
 
716
        public String toString() {
 
717
                StringBuffer toStringBuf = new StringBuffer();
 
718
                toStringBuf.append(super.toString());
 
719
                toStringBuf.append(" - Field level information: "); //$NON-NLS-1$
 
720
 
 
721
                for (int i = 0; i < this.fields.length; i++) {
 
722
                        toStringBuf.append("\n\t"); //$NON-NLS-1$
 
723
                        toStringBuf.append(this.fields[i].toString());
 
724
                }
 
725
 
 
726
                return toStringBuf.toString();
 
727
        }
 
728
        
 
729
        static String getClassNameForJavaType(int javaType, 
 
730
                        boolean isUnsigned, int mysqlTypeIfKnown, 
 
731
                        boolean isBinaryOrBlob,
 
732
                        boolean isOpaqueBinary) {
 
733
                switch (javaType) {
 
734
                case Types.BIT:
 
735
                case Types.BOOLEAN:
 
736
                        return "java.lang.Boolean"; //$NON-NLS-1$
 
737
 
 
738
                case Types.TINYINT:
 
739
 
 
740
                        if (isUnsigned) {
 
741
                                return "java.lang.Integer"; //$NON-NLS-1$
 
742
                        }
 
743
 
 
744
                        return "java.lang.Integer"; //$NON-NLS-1$
 
745
 
 
746
                case Types.SMALLINT:
 
747
 
 
748
                        if (isUnsigned) {
 
749
                                return "java.lang.Integer"; //$NON-NLS-1$
 
750
                        }
 
751
 
 
752
                        return "java.lang.Integer"; //$NON-NLS-1$
 
753
 
 
754
                case Types.INTEGER:
 
755
 
 
756
                        if (!isUnsigned || 
 
757
                                        mysqlTypeIfKnown == MysqlDefs.FIELD_TYPE_INT24) {
 
758
                                return "java.lang.Integer"; //$NON-NLS-1$
 
759
                        }
 
760
 
 
761
                        return "java.lang.Long"; //$NON-NLS-1$
 
762
 
 
763
                case Types.BIGINT:
 
764
 
 
765
                        if (!isUnsigned) {
 
766
                                return "java.lang.Long"; //$NON-NLS-1$
 
767
                        }
 
768
 
 
769
                        return "java.math.BigInteger"; //$NON-NLS-1$
 
770
 
 
771
                case Types.DECIMAL:
 
772
                case Types.NUMERIC:
 
773
                        return "java.math.BigDecimal"; //$NON-NLS-1$
 
774
 
 
775
                case Types.REAL:
 
776
                        return "java.lang.Float"; //$NON-NLS-1$
 
777
 
 
778
                case Types.FLOAT:
 
779
                case Types.DOUBLE:
 
780
                        return "java.lang.Double"; //$NON-NLS-1$
 
781
 
 
782
                case Types.CHAR:
 
783
                case Types.VARCHAR:
 
784
                case Types.LONGVARCHAR:
 
785
                        if (!isOpaqueBinary) {
 
786
                                return "java.lang.String"; //$NON-NLS-1$
 
787
                        }
 
788
 
 
789
                        return "[B";
 
790
 
 
791
                case Types.BINARY:
 
792
                case Types.VARBINARY:
 
793
                case Types.LONGVARBINARY:
 
794
 
 
795
                        if (mysqlTypeIfKnown == MysqlDefs.FIELD_TYPE_GEOMETRY) {
 
796
                                return "[B";
 
797
                        } else if (isBinaryOrBlob) {
 
798
                                return "[B";
 
799
                        } else {
 
800
                                return "java.lang.String";
 
801
                        }
 
802
 
 
803
                case Types.DATE:
 
804
                        return "java.sql.Date"; //$NON-NLS-1$
 
805
 
 
806
                case Types.TIME:
 
807
                        return "java.sql.Time"; //$NON-NLS-1$
 
808
 
 
809
                case Types.TIMESTAMP:
 
810
                        return "java.sql.Timestamp"; //$NON-NLS-1$
 
811
 
 
812
                default:
 
813
                        return "java.lang.Object"; //$NON-NLS-1$
 
814
                }
 
815
        }
 
816
        
 
817
        /**
 
818
     * Returns true if this either implements the interface argument or is directly or indirectly a wrapper
 
819
     * for an object that does. Returns false otherwise. If this implements the interface then return true,
 
820
     * else if this is a wrapper then return the result of recursively calling <code>isWrapperFor</code> on the wrapped
 
821
     * object. If this does not implement the interface and is not a wrapper, return false.
 
822
     * This method should be implemented as a low-cost operation compared to <code>unwrap</code> so that
 
823
     * callers can use this method to avoid expensive <code>unwrap</code> calls that may fail. If this method
 
824
     * returns true then calling <code>unwrap</code> with the same argument should succeed.
 
825
     *
 
826
     * @param interfaces a Class defining an interface.
 
827
     * @return true if this implements the interface or directly or indirectly wraps an object that does.
 
828
     * @throws java.sql.SQLException  if an error occurs while determining whether this is a wrapper
 
829
     * for an object with the given interface.
 
830
     * @since 1.6
 
831
     */
 
832
        public boolean isWrapperFor(Class<?> iface) throws SQLException {
 
833
                // This works for classes that aren't actually wrapping
 
834
                // anything
 
835
                return iface.isInstance(this);
 
836
        }
 
837
 
 
838
    /**
 
839
     * Returns an object that implements the given interface to allow access to non-standard methods,
 
840
     * or standard methods not exposed by the proxy.
 
841
     * The result may be either the object found to implement the interface or a proxy for that object.
 
842
     * If the receiver implements the interface then that is the object. If the receiver is a wrapper
 
843
     * and the wrapped object implements the interface then that is the object. Otherwise the object is
 
844
     *  the result of calling <code>unwrap</code> recursively on the wrapped object. If the receiver is not a
 
845
     * wrapper and does not implement the interface, then an <code>SQLException</code> is thrown.
 
846
     *
 
847
     * @param iface A Class defining an interface that the result must implement.
 
848
     * @return an object that implements the interface. May be a proxy for the actual implementing object.
 
849
     * @throws java.sql.SQLException If no object found that implements the interface 
 
850
     * @since 1.6
 
851
     */
 
852
        public Object unwrap(Class<?> iface) throws java.sql.SQLException {
 
853
        try {
 
854
                // This works for classes that aren't actually wrapping
 
855
                // anything
 
856
                return Util.cast(iface, this);
 
857
        } catch (ClassCastException cce) {
 
858
            throw SQLError.createSQLException("Unable to unwrap to " + iface.toString(), 
 
859
                        SQLError.SQL_STATE_ILLEGAL_ARGUMENT, this.exceptionInterceptor);
 
860
        }
 
861
    }
 
862
}