1 19 package com.mysql.jdbc; 20 21 import java.sql.SQLException ; 22 import java.sql.Types ; 23 24 25 33 public class ResultSetMetaData implements java.sql.ResultSetMetaData { 34 Field[] fields; 35 36 43 public ResultSetMetaData(Field[] fields) { 44 this.fields = fields; 45 } 46 47 57 public boolean isAutoIncrement(int column) throws java.sql.SQLException { 58 Field f = getField(column); 59 60 return f.isAutoIncrement(); 61 } 62 63 71 public boolean isCaseSensitive(int column) throws java.sql.SQLException { 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 107 public String getCatalogName(int column) throws java.sql.SQLException { 108 Field f = getField(column); 109 110 String database = f.getDatabaseName(); 111 112 return (database == null) ? "" : database; 113 } 114 115 117 132 public String getColumnClassName(int column) throws SQLException { 133 Field f = getField(column); 134 135 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 249 public int getColumnCount() throws java.sql.SQLException { 250 return fields.length; 251 } 252 253 260 public int getColumnDisplaySize(int column) throws java.sql.SQLException { 261 return getField(column).getLength(); 262 } 263 264 272 public String getColumnLabel(int column) throws java.sql.SQLException { 273 return getColumnName(column); 274 } 275 276 283 public String getColumnName(int column) throws java.sql.SQLException { 284 return getField(column).getName(); 285 } 286 287 295 public int getColumnType(int column) throws java.sql.SQLException { 296 return getField(column).getSQLType(); 297 } 298 299 306 public String getColumnTypeName(int column) throws java.sql.SQLException { 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 393 public boolean isCurrency(int column) throws java.sql.SQLException { 394 return false; 395 } 396 397 404 public boolean isDefinitelyWritable(int column) 405 throws java.sql.SQLException { 406 return isWritable(column); 407 } 408 409 416 public int isNullable(int column) throws java.sql.SQLException { 417 if (!getField(column).isNotNull()) { 418 return java.sql.ResultSetMetaData.columnNullable; 419 } else { 420 return java.sql.ResultSetMetaData.columnNoNulls; 421 } 422 } 423 424 431 public int getPrecision(int column) throws java.sql.SQLException { 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 452 public boolean isReadOnly(int column) throws java.sql.SQLException { 453 return false; 454 } 455 456 464 public int getScale(int column) throws java.sql.SQLException { 465 Field f = getField(column); 466 467 if (isDecimalType(f.getSQLType())) { 468 return f.getDecimals(); 469 } 470 471 return 0; 472 } 473 474 485 public String getSchemaName(int column) throws java.sql.SQLException { 486 return ""; 487 } 488 489 501 public boolean isSearchable(int column) throws java.sql.SQLException { 502 return true; 503 } 504 505 512 public boolean isSigned(int column) throws java.sql.SQLException { 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 545 public String getTableName(int column) throws java.sql.SQLException { 546 return getField(column).getTableName(); 547 } 548 549 556 public boolean isWritable(int column) throws java.sql.SQLException { 557 return !isReadOnly(column); 558 } 559 560 566 574 protected Field getField(int columnIndex) throws java.sql.SQLException { 575 if ((columnIndex < 1) || (columnIndex > fields.length)) { 576 throw new java.sql.SQLException ("Column index out of range.", 577 SQLError.SQL_STATE_INVALID_COLUMN_NUMBER); 578 } 579 580 return fields[columnIndex - 1]; 581 } 582 583 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 |