| 1 | /* |
| 2 | Copyright (C) 2002-2004 MySQL AB |
| 3 | |
| 4 | This program is free software; you can redistribute it and/or modify |
| 5 | it under the terms of version 2 of the GNU General Public License as |
| 6 | published by the Free Software Foundation. |
| 7 | |
| 8 | There are special exceptions to the terms and conditions of the GPL |
| 9 | as it is applied to this software. View the full text of the |
| 10 | exception in file EXCEPTIONS-CONNECTOR-J in the directory of this |
| 11 | software distribution. |
| 12 | |
| 13 | This program is distributed in the hope that it will be useful, |
| 14 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 16 | GNU General Public License for more details. |
| 17 | |
| 18 | You should have received a copy of the GNU General Public License |
| 19 | along with this program; if not, write to the Free Software |
| 20 | Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 21 | |
| 22 | |
| 23 | |
| 24 | */ |
| 25 | package com.mysql.jdbc; |
| 26 | |
| 27 | import java.io.UnsupportedEncodingException; |
| 28 | import java.sql.SQLException; |
| 29 | import java.sql.Types; |
| 30 | |
| 31 | /** |
| 32 | * Field is a class used to describe fields in a ResultSet |
| 33 | * |
| 34 | * @author Mark Matthews |
| 35 | * @version $Id: Field.java 5187 2006-04-20 16:00:49 -0500 (Thu, 20 Apr 2006) mmatthews $ |
| 36 | */ |
| 37 | public class Field { |
| 38 | // ~ Static fields/initializers |
| 39 | // --------------------------------------------- |
| 40 | |
| 41 | private static final int AUTO_INCREMENT_FLAG = 512; |
| 42 | |
| 43 | private static final int NO_CHARSET_INFO = -1; |
| 44 | |
| 45 | // ~ Instance fields |
| 46 | // -------------------------------------------------------- |
| 47 | |
| 48 | private byte[] buffer; |
| 49 | |
| 50 | private int charsetIndex = 0; |
| 51 | |
| 52 | private String charsetName = null; |
| 53 | |
| 54 | private int colDecimals; |
| 55 | |
| 56 | private short colFlag; |
| 57 | |
| 58 | private String collationName = null; |
| 59 | |
| 60 | private Connection connection = null; |
| 61 | |
| 62 | private String databaseName = null; |
| 63 | |
| 64 | private int databaseNameLength = -1; |
| 65 | |
| 66 | // database name info |
| 67 | private int databaseNameStart = -1; |
| 68 | |
| 69 | private int defaultValueLength = -1; |
| 70 | |
| 71 | // default value info - from COM_LIST_FIELDS execution |
| 72 | private int defaultValueStart = -1; |
| 73 | |
| 74 | private String fullName = null; |
| 75 | |
| 76 | private String fullOriginalName = null; |
| 77 | |
| 78 | private boolean isImplicitTempTable = false; |
| 79 | |
| 80 | private long length; // Internal length of the field; |
| 81 | |
| 82 | private int mysqlType = -1; // the MySQL type |
| 83 | |
| 84 | private String name; // The Field name |
| 85 | |
| 86 | private int nameLength; |
| 87 | |
| 88 | private int nameStart; |
| 89 | |
| 90 | private String originalColumnName = null; |
| 91 | |
| 92 | private int originalColumnNameLength = -1; |
| 93 | |
| 94 | // column name info (before aliasing) |
| 95 | private int originalColumnNameStart = -1; |
| 96 | |
| 97 | private String originalTableName = null; |
| 98 | |
| 99 | private int originalTableNameLength = -1; |
| 100 | |
| 101 | // table name info (before aliasing) |
| 102 | private int originalTableNameStart = -1; |
| 103 | |
| 104 | private int precisionAdjustFactor = 0; |
| 105 | |
| 106 | private int sqlType = -1; // the java.sql.Type |
| 107 | |
| 108 | private String tableName; // The Name of the Table |
| 109 | |
| 110 | private int tableNameLength; |
| 111 | |
| 112 | private int tableNameStart; |
| 113 | |
| 114 | private boolean useOldNameMetadata = false; |
| 115 | |
| 116 | private boolean isSingleBit; |
| 117 | |
| 118 | // ~ Constructors |
| 119 | // ----------------------------------------------------------- |
| 120 | |
| 121 | /** |
| 122 | * Constructor used when communicating with 4.1 and newer servers |
| 123 | */ |
| 124 | Field(Connection conn, byte[] buffer, int databaseNameStart, |
| 125 | int databaseNameLength, int tableNameStart, int tableNameLength, |
| 126 | int originalTableNameStart, int originalTableNameLength, |
| 127 | int nameStart, int nameLength, int originalColumnNameStart, |
| 128 | int originalColumnNameLength, long length, int mysqlType, |
| 129 | short colFlag, int colDecimals, int defaultValueStart, |
| 130 | int defaultValueLength, int charsetIndex) throws SQLException { |
| 131 | this.connection = conn; |
| 132 | this.buffer = buffer; |
| 133 | this.nameStart = nameStart; |
| 134 | this.nameLength = nameLength; |
| 135 | this.tableNameStart = tableNameStart; |
| 136 | this.tableNameLength = tableNameLength; |
| 137 | this.length = length; |
| 138 | this.colFlag = colFlag; |
| 139 | this.colDecimals = colDecimals; |
| 140 | this.mysqlType = mysqlType; |
| 141 | |
| 142 | // 4.1 field info... |
| 143 | this.databaseNameStart = databaseNameStart; |
| 144 | this.databaseNameLength = databaseNameLength; |
| 145 | |
| 146 | this.originalTableNameStart = originalTableNameStart; |
| 147 | this.originalTableNameLength = originalTableNameLength; |
| 148 | |
| 149 | this.originalColumnNameStart = originalColumnNameStart; |
| 150 | this.originalColumnNameLength = originalColumnNameLength; |
| 151 | |
| 152 | this.defaultValueStart = defaultValueStart; |
| 153 | this.defaultValueLength = defaultValueLength; |
| 154 | |
| 155 | // If we're not running 4.1 or newer, use the connection's |
| 156 | // charset |
| 157 | this.charsetIndex = charsetIndex; |
| 158 | |
| 159 | |
| 160 | // Map MySqlTypes to java.sql Types |
| 161 | this.sqlType = MysqlDefs.mysqlToJavaType(this.mysqlType); |
| 162 | |
| 163 | // Re-map to 'real' blob type, if we're a BLOB |
| 164 | |
| 165 | if (this.mysqlType == MysqlDefs.FIELD_TYPE_BLOB) { |
| 166 | if (this.charsetIndex == 63 || |
| 167 | !this.connection.versionMeetsMinimum(4, 1, 0)) { |
| 168 | setBlobTypeBasedOnLength(); |
| 169 | this.sqlType = MysqlDefs.mysqlToJavaType(this.mysqlType); |
| 170 | } else { |
| 171 | // *TEXT masquerading as blob |
| 172 | this.mysqlType = MysqlDefs.FIELD_TYPE_VAR_STRING; |
| 173 | this.sqlType = Types.LONGVARCHAR; |
| 174 | } |
| 175 | } |
| 176 | |
| 177 | if (this.sqlType == Types.TINYINT && this.length == 1 |
| 178 | && this.connection.getTinyInt1isBit()) { |
| 179 | // Adjust for pseudo-boolean |
| 180 | if (conn.getTinyInt1isBit()) { |
| 181 | if (conn.getTransformedBitIsBoolean()) { |
| 182 | this.sqlType = Types.BOOLEAN; |
| 183 | } else { |
| 184 | this.sqlType = Types.BIT; |
| 185 | } |
| 186 | } |
| 187 | |
| 188 | } |
| 189 | |
| 190 | if (!isNativeNumericType() && !isNativeDateTimeType()) { |
| 191 | this.charsetName = this.connection |
| 192 | .getCharsetNameForIndex(this.charsetIndex); |
| 193 | |
| 194 | |
| 195 | // Handle VARBINARY/BINARY (server doesn't have a different type |
| 196 | // for this |
| 197 | |
| 198 | boolean isBinary = isBinary(); |
| 199 | |
| 200 | if (this.connection.versionMeetsMinimum(4, 1, 0) && |
| 201 | this.mysqlType == MysqlDefs.FIELD_TYPE_VAR_STRING && |
| 202 | isBinary && |
| 203 | this.charsetIndex == 63) { |
| 204 | if (this.isOpaqueBinary()) { |
| 205 | this.sqlType = Types.VARBINARY; |
| 206 | } |
| 207 | } |
| 208 | |
| 209 | if (this.connection.versionMeetsMinimum(4, 1, 0) && |
| 210 | this.mysqlType == MysqlDefs.FIELD_TYPE_STRING && |
| 211 | isBinary && this.charsetIndex == 63) { |
| 212 | // |
| 213 | // Okay, this is a hack, but there's currently no way |
| 214 | // to easily distinguish something like DATE_FORMAT( ..) |
| 215 | // from the "BINARY" column type, other than looking |
| 216 | // at the original column name. |
| 217 | // |
| 218 | |
| 219 | if (isOpaqueBinary()) { |
| 220 | this.sqlType = Types.BINARY; |
| 221 | } |
| 222 | } |
| 223 | |
| 224 | |
| 225 | |
| 226 | if (this.mysqlType == MysqlDefs.FIELD_TYPE_BIT) { |
| 227 | this.isSingleBit = (this.length == 0); |
| 228 | |
| 229 | if (this.connection != null && (this.connection.versionMeetsMinimum(5, 0, 21) || |
| 230 | this.connection.versionMeetsMinimum(5, 1, 10)) && this.length == 1) { |
| 231 | this.isSingleBit = true; |
| 232 | } |
| 233 | |
| 234 | if (this.isSingleBit) { |
| 235 | this.sqlType = Types.BIT; |
| 236 | } else { |
| 237 | this.sqlType = Types.VARBINARY; |
| 238 | this.colFlag |= 128; // we need to pretend this is a full |
| 239 | this.colFlag |= 16; // binary blob |
| 240 | } |
| 241 | } |
| 242 | |
| 243 | // |
| 244 | // Handle TEXT type (special case), Fix proposed by Peter McKeown |
| 245 | // |
| 246 | if ((this.sqlType == java.sql.Types.LONGVARBINARY) && !isBinary) { |
| 247 | this.sqlType = java.sql.Types.LONGVARCHAR; |
| 248 | } else if ((this.sqlType == java.sql.Types.VARBINARY) && !isBinary) { |
| 249 | this.sqlType = java.sql.Types.VARCHAR; |
| 250 | } |
| 251 | |
| 252 | checkForImplicitTemporaryTable(); |
| 253 | } else { |
| 254 | this.charsetName = "US-ASCII"; |
| 255 | } |
| 256 | |
| 257 | // |
| 258 | // Handle odd values for 'M' for floating point/decimal numbers |
| 259 | // |
| 260 | if (!isUnsigned()) { |
| 261 | switch (this.mysqlType) { |
| 262 | case MysqlDefs.FIELD_TYPE_DECIMAL: |
| 263 | case MysqlDefs.FIELD_TYPE_NEW_DECIMAL: |
| 264 | this.precisionAdjustFactor = -1; |
| 265 | |
| 266 | break; |
| 267 | case MysqlDefs.FIELD_TYPE_DOUBLE: |
| 268 | case MysqlDefs.FIELD_TYPE_FLOAT: |
| 269 | this.precisionAdjustFactor = 1; |
| 270 | |
| 271 | break; |
| 272 | } |
| 273 | } else { |
| 274 | switch (this.mysqlType) { |
| 275 | case MysqlDefs.FIELD_TYPE_DOUBLE: |
| 276 | case MysqlDefs.FIELD_TYPE_FLOAT: |
| 277 | this.precisionAdjustFactor = 1; |
| 278 | |
| 279 | break; |
| 280 | } |
| 281 | } |
| 282 | } |
| 283 | |
| 284 | /** |
| 285 | * Constructor used when communicating with pre 4.1 servers |
| 286 | */ |
| 287 | Field(Connection conn, byte[] buffer, int nameStart, int nameLength, |
| 288 | int tableNameStart, int tableNameLength, int length, int mysqlType, |
| 289 | short colFlag, int colDecimals) throws SQLException { |
| 290 | this(conn, buffer, -1, -1, tableNameStart, tableNameLength, -1, -1, |
| 291 | nameStart, nameLength, -1, -1, length, mysqlType, colFlag, |
| 292 | colDecimals, -1, -1, NO_CHARSET_INFO); |
| 293 | } |
| 294 | |
| 295 | /** |
| 296 | * Constructor used by DatabaseMetaData methods. |
| 297 | */ |
| 298 | Field(String tableName, String columnName, int jdbcType, int length) { |
| 299 | this.tableName = tableName; |
| 300 | this.name = columnName; |
| 301 | this.length = length; |
| 302 | this.sqlType = jdbcType; |
| 303 | this.colFlag = 0; |
| 304 | this.colDecimals = 0; |
| 305 | } |
| 306 | |
| 307 | // ~ Methods |
| 308 | // ---------------------------------------------------------------- |
| 309 | |
| 310 | private void checkForImplicitTemporaryTable() { |
| 311 | this.isImplicitTempTable = this.tableNameLength > 5 |
| 312 | && this.buffer[tableNameStart] == (byte) '#' |
| 313 | && this.buffer[tableNameStart + 1] == (byte) 's' |
| 314 | && this.buffer[tableNameStart + 2] == (byte) 'q' |
| 315 | && this.buffer[tableNameStart + 3] == (byte) 'l' |
| 316 | && this.buffer[tableNameStart + 4] == (byte) '_'; |
| 317 | } |
| 318 | |
| 319 | /** |
| 320 | * Returns the character set (if known) for this field. |
| 321 | * |
| 322 | * @return the character set |
| 323 | */ |
| 324 | public String getCharacterSet() throws SQLException { |
| 325 | return this.charsetName; |
| 326 | } |
| 327 | |
| 328 | public synchronized String getCollation() throws SQLException { |
| 329 | if (this.collationName == null) { |
| 330 | if (this.connection != null) { |
| 331 | if (this.connection.versionMeetsMinimum(4, 1, 0)) { |
| 332 | java.sql.DatabaseMetaData dbmd = this.connection |
| 333 | .getMetaData(); |
| 334 | |
| 335 | String quotedIdStr = dbmd.getIdentifierQuoteString(); |
| 336 | |
| 337 | if (" ".equals(quotedIdStr)) { //$NON-NLS-1$ |
| 338 | quotedIdStr = ""; //$NON-NLS-1$ |
| 339 | } |
| 340 | |
| 341 | String csCatalogName = getDatabaseName(); |
| 342 | String csTableName = getOriginalTableName(); |
| 343 | String csColumnName = getOriginalName(); |
| 344 | |
| 345 | if (csCatalogName != null && csCatalogName.length() != 0 |
| 346 | && csTableName != null && csTableName.length() != 0 |
| 347 | && csColumnName != null |
| 348 | && csColumnName.length() != 0) { |
| 349 | StringBuffer queryBuf = new StringBuffer(csCatalogName |
| 350 | .length() |
| 351 | + csTableName.length() + 28); |
| 352 | queryBuf.append("SHOW FULL COLUMNS FROM "); //$NON-NLS-1$ |
| 353 | queryBuf.append(quotedIdStr); |
| 354 | queryBuf.append(csCatalogName); |
| 355 | queryBuf.append(quotedIdStr); |
| 356 | queryBuf.append("."); //$NON-NLS-1$ |
| 357 | queryBuf.append(quotedIdStr); |
| 358 | queryBuf.append(csTableName); |
| 359 | queryBuf.append(quotedIdStr); |
| 360 | |
| 361 | java.sql.Statement collationStmt = null; |
| 362 | java.sql.ResultSet collationRs = null; |
| 363 | |
| 364 | try { |
| 365 | collationStmt = this.connection.createStatement(); |
| 366 | |
| 367 | collationRs = collationStmt.executeQuery(queryBuf |
| 368 | .toString()); |
| 369 | |
| 370 | while (collationRs.next()) { |
| 371 | if (csColumnName.equals(collationRs |
| 372 | .getString("Field"))) { //$NON-NLS-1$ |
| 373 | this.collationName = collationRs |
| 374 | .getString("Collation"); //$NON-NLS-1$ |
| 375 | |
| 376 | break; |
| 377 | } |
| 378 | } |
| 379 | } finally { |
| 380 | if (collationRs != null) { |
| 381 | collationRs.close(); |
| 382 | collationRs = null; |
| 383 | } |
| 384 | |
| 385 | if (collationStmt != null) { |
| 386 | collationStmt.close(); |
| 387 | collationStmt = null; |
| 388 | } |
| 389 | } |
| 390 | |
| 391 | } |
| 392 | } |
| 393 | |
| 394 | } |
| 395 | |
| 396 | } |
| 397 | |
| 398 | return this.collationName; |
| 399 | } |
| 400 | |
| 401 | public String getColumnLabel() throws SQLException { |
| 402 | return getName(); // column name if not aliased, alias if used |
| 403 | } |
| 404 | |
| 405 | /** |
| 406 | * DOCUMENT ME! |
| 407 | * |
| 408 | * @return DOCUMENT ME! |
| 409 | */ |
| 410 | public String getDatabaseName() throws SQLException { |
| 411 | if ((this.databaseName == null) && (this.databaseNameStart != -1) |
| 412 | && (this.databaseNameLength != -1)) { |
| 413 | this.databaseName = getStringFromBytes(this.databaseNameStart, |
| 414 | this.databaseNameLength); |
| 415 | } |
| 416 | |
| 417 | return this.databaseName; |
| 418 | } |
| 419 | |
| 420 | int getDecimals() { |
| 421 | return this.colDecimals; |
| 422 | } |
| 423 | |
| 424 | /** |
| 425 | * DOCUMENT ME! |
| 426 | * |
| 427 | * @return DOCUMENT ME! |
| 428 | */ |
| 429 | public String getFullName() throws SQLException { |
| 430 | if (this.fullName == null) { |
| 431 | StringBuffer fullNameBuf = new StringBuffer(getTableName().length() |
| 432 | + 1 + getName().length()); |
| 433 | fullNameBuf.append(this.tableName); |
| 434 | |
| 435 | // much faster to append a char than a String |
| 436 | fullNameBuf.append('.'); |
| 437 | fullNameBuf.append(this.name); |
| 438 | this.fullName = fullNameBuf.toString(); |
| 439 | fullNameBuf = null; |
| 440 | } |
| 441 | |
| 442 | return this.fullName; |
| 443 | } |
| 444 | |
| 445 | /** |
| 446 | * DOCUMENT ME! |
| 447 | * |
| 448 | * @return DOCUMENT ME! |
| 449 | */ |
| 450 | public String getFullOriginalName() throws SQLException { |
| 451 | getOriginalName(); |
| 452 | |
| 453 | if (this.originalColumnName == null) { |
| 454 | return null; // we don't have this information |
| 455 | } |
| 456 | |
| 457 | if (this.fullName == null) { |
| 458 | StringBuffer fullOriginalNameBuf = new StringBuffer( |
| 459 | getOriginalTableName().length() + 1 |
| 460 | + getOriginalName().length()); |
| 461 | fullOriginalNameBuf.append(this.originalTableName); |
| 462 | |
| 463 | // much faster to append a char than a String |
| 464 | fullOriginalNameBuf.append('.'); |
| 465 | fullOriginalNameBuf.append(this.originalColumnName); |
| 466 | this.fullOriginalName = fullOriginalNameBuf.toString(); |
| 467 | fullOriginalNameBuf = null; |
| 468 | } |
| 469 | |
| 470 | return this.fullOriginalName; |
| 471 | } |
| 472 | |
| 473 | /** |
| 474 | * DOCUMENT ME! |
| 475 | * |
| 476 | * @return DOCUMENT ME! |
| 477 | */ |
| 478 | public long getLength() { |
| 479 | return this.length; |
| 480 | } |
| 481 | |
| 482 | public int getMaxBytesPerCharacter() throws SQLException { |
| 483 | return this.connection.getMaxBytesPerChar(getCharacterSet()); |
| 484 | } |
| 485 | |
| 486 | /** |
| 487 | * DOCUMENT ME! |
| 488 | * |
| 489 | * @return DOCUMENT ME! |
| 490 | */ |
| 491 | public int getMysqlType() { |
| 492 | return this.mysqlType; |
| 493 | } |
| 494 | |
| 495 | /** |
| 496 | * DOCUMENT ME! |
| 497 | * |
| 498 | * @return DOCUMENT ME! |
| 499 | */ |
| 500 | public String getName() throws SQLException { |
| 501 | if (this.name == null) { |
| 502 | this.name = getStringFromBytes(this.nameStart, this.nameLength); |
| 503 | } |
| 504 | |
| 505 | return this.name; |
| 506 | } |
| 507 | |
| 508 | public String getNameNoAliases() throws SQLException { |
| 509 | if (this.useOldNameMetadata) { |
| 510 | return getName(); |
| 511 | } |
| 512 | |
| 513 | if (this.connection != null && |
| 514 | this.connection.versionMeetsMinimum(4, 1, 0)) { |
| 515 | return getOriginalName(); |
| 516 | } |
| 517 | |
| 518 | return getName(); |
| 519 | } |
| 520 | |
| 521 | /** |
| 522 | * DOCUMENT ME! |
| 523 | * |
| 524 | * @return DOCUMENT ME! |
| 525 | */ |
| 526 | public String getOriginalName() throws SQLException { |
| 527 | if ((this.originalColumnName == null) |
| 528 | && (this.originalColumnNameStart != -1) |
| 529 | && (this.originalColumnNameLength != -1)) { |
| 530 | this.originalColumnName = getStringFromBytes( |
| 531 | this.originalColumnNameStart, this.originalColumnNameLength); |
| 532 | } |
| 533 | |
| 534 | return this.originalColumnName; |
| 535 | } |
| 536 | |
| 537 | /** |
| 538 | * DOCUMENT ME! |
| 539 | * |
| 540 | * @return DOCUMENT ME! |
| 541 | */ |
| 542 | public String getOriginalTableName() throws SQLException { |
| 543 | if ((this.originalTableName == null) |
| 544 | && (this.originalTableNameStart != -1) |
| 545 | && (this.originalTableNameLength != -1)) { |
| 546 | this.originalTableName = getStringFromBytes( |
| 547 | this.originalTableNameStart, this.originalTableNameLength); |
| 548 | } |
| 549 | |
| 550 | return this.originalTableName; |
| 551 | } |
| 552 | |
| 553 | /** |
| 554 | * Returns amount of correction that should be applied to the precision |
| 555 | * value. |
| 556 | * |
| 557 | * Different versions of MySQL report different precision values. |
| 558 | * |
| 559 | * @return the amount to adjust precision value by. |
| 560 | */ |
| 561 | public int getPrecisionAdjustFactor() { |
| 562 | return this.precisionAdjustFactor; |
| 563 | } |
| 564 | |
| 565 | /** |
| 566 | * DOCUMENT ME! |
| 567 | * |
| 568 | * @return DOCUMENT ME! |
| 569 | */ |
| 570 | public int getSQLType() { |
| 571 | return this.sqlType; |
| 572 | } |
| 573 | |
| 574 | /** |
| 575 | * Create a string with the correct charset encoding from the byte-buffer |
| 576 | * that contains the data for this field |
| 577 | */ |
| 578 | private String getStringFromBytes(int stringStart, int stringLength) |
| 579 | throws SQLException { |
| 580 | if ((stringStart == -1) || (stringLength == -1)) { |
| 581 | return null; |
| 582 | } |
| 583 | |
| 584 | String stringVal = null; |
| 585 | |
| 586 | if (this.connection != null) { |
| 587 | if (this.connection.getUseUnicode()) { |
| 588 | String encoding = this.connection.getCharacterSetMetadata(); |
| 589 | |
| 590 | if (encoding == null) { |
| 591 | encoding = connection.getEncoding(); |
| 592 | } |
| 593 | |
| 594 | if (encoding != null) { |
| 595 | SingleByteCharsetConverter converter = null; |
| 596 | |
| 597 | if (this.connection != null) { |
| 598 | converter = this.connection |
| 599 | .getCharsetConverter(encoding); |
| 600 | } |
| 601 | |
| 602 | if (converter != null) { // we have a converter |
| 603 | stringVal = converter.toString(this.buffer, |
| 604 | stringStart, stringLength); |
| 605 | } else { |
| 606 | // we have no converter, use JVM converter |
| 607 | byte[] stringBytes = new byte[stringLength]; |
| 608 | |
| 609 | int endIndex = stringStart + stringLength; |
| 610 | int pos = 0; |
| 611 | |
| 612 | for (int i = stringStart; i < endIndex; i++) { |
| 613 | stringBytes[pos++] = this.buffer[i]; |
| 614 | } |
| 615 | |
| 616 | try { |
| 617 | stringVal = new String(stringBytes, encoding); |
| 618 | } catch (UnsupportedEncodingException ue) { |
| 619 | throw new RuntimeException(Messages |
| 620 | .getString("Field.12") + encoding //$NON-NLS-1$ |
| 621 | + Messages.getString("Field.13")); //$NON-NLS-1$ |
| 622 | } |
| 623 | } |
| 624 | } else { |
| 625 | // we have no encoding, use JVM standard charset |
| 626 | stringVal = StringUtils.toAsciiString(this.buffer, |
| 627 | stringStart, stringLength); |
| 628 | } |
| 629 | } else { |
| 630 | // we are not using unicode, so use JVM standard charset |
| 631 | stringVal = StringUtils.toAsciiString(this.buffer, stringStart, |
| 632 | stringLength); |
| 633 | } |
| 634 | } else { |
| 635 | // we don't have a connection, so punt |
| 636 | stringVal = StringUtils.toAsciiString(this.buffer, stringStart, |
| 637 | stringLength); |
| 638 | } |
| 639 | |
| 640 | return stringVal; |
| 641 | } |
| 642 | |
| 643 | /** |
| 644 | * DOCUMENT ME! |
| 645 | * |
| 646 | * @return DOCUMENT ME! |
| 647 | */ |
| 648 | public String getTable() throws SQLException { |
| 649 | return getTableName(); |
| 650 | } |
| 651 | |
| 652 | /** |
| 653 | * DOCUMENT ME! |
| 654 | * |
| 655 | * @return DOCUMENT ME! |
| 656 | */ |
| 657 | public String getTableName() throws SQLException { |
| 658 | if (this.tableName == null) { |
| 659 | this.tableName = getStringFromBytes(this.tableNameStart, |
| 660 | this.tableNameLength); |
| 661 | } |
| 662 | |
| 663 | return this.tableName; |
| 664 | } |
| 665 | |
| 666 | public String getTableNameNoAliases() throws SQLException { |
| 667 | if (this.connection.versionMeetsMinimum(4, 1, 0)) { |
| 668 | return getOriginalTableName(); |
| 669 | } |
| 670 | |
| 671 | return getTableName(); // pre-4.1, no aliases returned |
| 672 | } |
| 673 | |
| 674 | /** |
| 675 | * DOCUMENT ME! |
| 676 | * |
| 677 | * @return DOCUMENT ME! |
| 678 | */ |
| 679 | public boolean isAutoIncrement() { |
| 680 | return ((this.colFlag & AUTO_INCREMENT_FLAG) > 0); |
| 681 | } |
| 682 | |
| 683 | /** |
| 684 | * DOCUMENT ME! |
| 685 | * |
| 686 | * @return DOCUMENT ME! |
| 687 | */ |
| 688 | public boolean isBinary() { |
| 689 | return ((this.colFlag & 128) > 0); |
| 690 | } |
| 691 | |
| 692 | /** |
| 693 | * DOCUMENT ME! |
| 694 | * |
| 695 | * @return DOCUMENT ME! |
| 696 | */ |
| 697 | public boolean isBlob() { |
| 698 | return ((this.colFlag & 16) > 0); |
| 699 | } |
| 700 | |
| 701 | /** |
| 702 | * Is this field owned by a server-created temporary table? |
| 703 | * |
| 704 | * @return |
| 705 | */ |
| 706 | private boolean isImplicitTemporaryTable() { |
| 707 | return this.isImplicitTempTable; |
| 708 | } |
| 709 | |
| 710 | /** |
| 711 | * DOCUMENT ME! |
| 712 | * |
| 713 | * @return DOCUMENT ME! |
| 714 | */ |
| 715 | public boolean isMultipleKey() { |
| 716 | return ((this.colFlag & 8) > 0); |
| 717 | } |
| 718 | |
| 719 | boolean isNotNull() { |
| 720 | return ((this.colFlag & 1) > 0); |
| 721 | } |
| 722 | |
| 723 | boolean isOpaqueBinary() throws SQLException { |
| 724 | |
| 725 | // |
| 726 | // Detect CHAR(n) CHARACTER SET BINARY which is a synonym for |
| 727 | // fixed-length binary types |
| 728 | // |
| 729 | |
| 730 | if (this.charsetIndex == 63 && isBinary() |
| 731 | && (this.getMysqlType() == MysqlDefs.FIELD_TYPE_STRING || |
| 732 | this.getMysqlType() == MysqlDefs.FIELD_TYPE_VAR_STRING)) { |
| 733 | |
| 734 | if (this.originalTableNameLength == 0) { |
| 735 | return false; // Probably from function |
| 736 | } |
| 737 | |
| 738 | // Okay, queries resolved by temp tables also have this 'signature', |
| 739 | // check for that |
| 740 | |
| 741 | return !isImplicitTemporaryTable(); |
| 742 | } |
| 743 | |
| 744 | return (this.connection.versionMeetsMinimum(4, 1, 0) && "binary" |
| 745 | .equalsIgnoreCase(getCharacterSet())); |
| 746 | |
| 747 | } |
| 748 | |
| 749 | /** |
| 750 | * DOCUMENT ME! |
| 751 | * |
| 752 | * @return DOCUMENT ME! |
| 753 | */ |
| 754 | public boolean isPrimaryKey() { |
| 755 | return ((this.colFlag & 2) > 0); |
| 756 | } |
| 757 | |
| 758 | /** |
| 759 | * Is this field _definitely_ not writable? |
| 760 | * |
| 761 | * @return true if this field can not be written to in an INSERT/UPDATE |
| 762 | * statement. |
| 763 | */ |
| 764 | boolean isReadOnly() throws SQLException { |
| 765 | if (this.connection.versionMeetsMinimum(4, 1, 0)) { |
| 766 | String orgColumnName = getOriginalName(); |
| 767 | String orgTableName = getOriginalTableName(); |
| 768 | |
| 769 | return !(orgColumnName != null && orgColumnName.length() > 0 |
| 770 | && orgTableName != null && orgTableName.length() > 0); |
| 771 | } |
| 772 | |
| 773 | return false; // we can't tell definitively in this case. |
| 774 | } |
| 775 | |
| 776 | /** |
| 777 | * DOCUMENT ME! |
| 778 | * |
| 779 | * @return DOCUMENT ME! |
| 780 | */ |
| 781 | public boolean isUniqueKey() { |
| 782 | return ((this.colFlag & 4) > 0); |
| 783 | } |
| 784 | |
| 785 | /** |
| 786 | * DOCUMENT ME! |
| 787 | * |
| 788 | * @return DOCUMENT ME! |
| 789 | */ |
| 790 | public boolean isUnsigned() { |
| 791 | return ((this.colFlag & 32) > 0); |
| 792 | } |
| 793 | |
| 794 | /** |
| 795 | * DOCUMENT ME! |
| 796 | * |
| 797 | * @return DOCUMENT ME! |
| 798 | */ |
| 799 | public boolean isZeroFill() { |
| 800 | return ((this.colFlag & 64) > 0); |
| 801 | } |
| 802 | |
| 803 | // |
| 804 | // MySQL only has one protocol-level BLOB type that it exposes |
| 805 | // which is FIELD_TYPE_BLOB, although we can divine what the |
| 806 | // actual type is by the length reported ... |
| 807 | // |
| 808 | private void setBlobTypeBasedOnLength() { |
| 809 | if (this.length == MysqlDefs.LENGTH_TINYBLOB) { |
| 810 | this.mysqlType = MysqlDefs.FIELD_TYPE_TINY_BLOB; |
| 811 | } else if (this.length == MysqlDefs.LENGTH_BLOB) { |
| 812 | this.mysqlType = MysqlDefs.FIELD_TYPE_BLOB; |
| 813 | } else if (this.length == MysqlDefs.LENGTH_MEDIUMBLOB) { |
| 814 | this.mysqlType = MysqlDefs.FIELD_TYPE_MEDIUM_BLOB; |
| 815 | } else if (this.length == MysqlDefs.LENGTH_LONGBLOB) { |
| 816 | this.mysqlType = MysqlDefs.FIELD_TYPE_LONG_BLOB; |
| 817 | } |
| 818 | } |
| 819 | |
| 820 | private boolean isNativeNumericType() { |
| 821 | return ((this.mysqlType >= MysqlDefs.FIELD_TYPE_TINY && |
| 822 | this.mysqlType <= MysqlDefs.FIELD_TYPE_DOUBLE) || |
| 823 | this.mysqlType == MysqlDefs.FIELD_TYPE_LONGLONG || |
| 824 | this.mysqlType == MysqlDefs.FIELD_TYPE_YEAR); |
| 825 | } |
| 826 | |
| 827 | private boolean isNativeDateTimeType() { |
| 828 | return (this.mysqlType == MysqlDefs.FIELD_TYPE_DATE || |
| 829 | this.mysqlType == MysqlDefs.FIELD_TYPE_NEWDATE || |
| 830 | this.mysqlType == MysqlDefs.FIELD_TYPE_DATETIME || |
| 831 | this.mysqlType == MysqlDefs.FIELD_TYPE_TIME || |
| 832 | this.mysqlType == MysqlDefs.FIELD_TYPE_TIMESTAMP); |
| 833 | } |
| 834 | |
| 835 | /** |
| 836 | * DOCUMENT ME! |
| 837 | * |
| 838 | * @param conn |
| 839 | * DOCUMENT ME! |
| 840 | */ |
| 841 | public void setConnection(Connection conn) { |
| 842 | this.connection = conn; |
| 843 | |
| 844 | this.charsetName = this.connection.getEncoding(); |
| 845 | } |
| 846 | |
| 847 | void setMysqlType(int type) { |
| 848 | this.mysqlType = type; |
| 849 | this.sqlType = MysqlDefs.mysqlToJavaType(this.mysqlType); |
| 850 | } |
| 851 | |
| 852 | protected void setUseOldNameMetadata(boolean useOldNameMetadata) { |
| 853 | this.useOldNameMetadata = useOldNameMetadata; |
| 854 | } |
| 855 | |
| 856 | /** |
| 857 | * DOCUMENT ME! |
| 858 | * |
| 859 | * @return DOCUMENT ME! |
| 860 | */ |
| 861 | public String toString() { |
| 862 | try { |
| 863 | StringBuffer asString = new StringBuffer(); |
| 864 | asString.append(super.toString()); |
| 865 | |
| 866 | asString.append("\n catalog: "); |
| 867 | asString.append(this.getDatabaseName()); |
| 868 | asString.append("\n table name: "); |
| 869 | asString.append(this.getTableName()); |
| 870 | asString.append("\n original table name: "); |
| 871 | asString.append(this.getOriginalTableName()); |
| 872 | asString.append("\n column name: "); |
| 873 | asString.append(this.getName()); |
| 874 | asString.append("\n original column name: "); |
| 875 | asString.append(this.getOriginalName()); |
| 876 | asString.append("\n MySQL data type: "); |
| 877 | asString.append(getMysqlType()); |
| 878 | asString.append("\n\nData as received from server:\n\n"); |
| 879 | asString.append(StringUtils.dumpAsHex(this.buffer, this.buffer.length)); |
| 880 | |
| 881 | return asString.toString(); |
| 882 | } catch (SQLException sqlEx) { |
| 883 | return super.toString(); |
| 884 | } |
| 885 | |
| 886 | } |
| 887 | |
| 888 | protected boolean isSingleBit() { |
| 889 | return this.isSingleBit; |
| 890 | } |
| 891 | } |