1 19 package com.mysql.jdbc; 20 21 import java.io.UnsupportedEncodingException ; 22 23 24 31 public class Field { 32 34 private static final int AUTO_INCREMENT_FLAG = 512; 35 private static final int NO_CHARSET_INFO = -1; 36 37 39 private Connection connection = null; 40 private String charsetName = null; 41 private String databaseName = null; 42 private String defaultValue = null; 43 private String fullName = null; 44 private String fullNameWithDatabase = null; 45 private String fullOriginalName = null; 46 private String fullOriginalNameWithDatabase = null; 47 private String name; private String originalColumnName = null; 49 private String originalTableName = null; 50 private String tableName; private byte[] buffer; 52 private int charsetIndex = 0; 53 private int colDecimals; 54 private int databaseNameLength = -1; 55 56 private int databaseNameStart = -1; 58 private int defaultValueLength = -1; 59 60 private int defaultValueStart = -1; 62 private int length; private int mysqlType = -1; private int nameLength; 65 private int nameStart; 66 private int originalColumnNameLength = -1; 67 68 private int originalColumnNameStart = -1; 70 private int originalTableNameLength = -1; 71 72 private int originalTableNameStart = -1; 74 private int precisionAdjustFactor = 0; 75 private int sqlType = -1; private int tableNameLength; 77 private int tableNameStart; 78 private short colFlag; 79 80 82 85 Field(String tableName, String columnName, int jdbcType, int length) { 86 this.tableName = tableName; 87 this.name = columnName; 88 this.length = length; 89 sqlType = jdbcType; 90 colFlag = 0; 91 colDecimals = 0; 92 } 93 94 97 Field(Connection conn, byte[] buffer, int nameStart, int nameLength, 98 int tableNameStart, int tableNameLength, int length, int mysqlType, 99 short colFlag, int colDecimals) { 100 this(conn, buffer, -1, -1, tableNameStart, tableNameLength, -1, -1, 101 nameStart, nameLength, -1, -1, length, mysqlType, colFlag, 102 colDecimals, -1, -1, NO_CHARSET_INFO); 103 } 104 105 109 Field(Connection conn, byte[] buffer, int databaseNameStart, 110 int databaseNameLength, int tableNameStart, int tableNameLength, 111 int originalTableNameStart, int originalTableNameLength, int nameStart, 112 int nameLength, int originalColumnNameStart, 113 int originalColumnNameLength, int length, int mysqlType, short colFlag, 114 int colDecimals, int defaultValueStart, int defaultValueLength, 115 int charsetIndex) { 116 this.connection = conn; 117 this.buffer = buffer; 118 this.nameStart = nameStart; 119 this.nameLength = nameLength; 120 this.tableNameStart = tableNameStart; 121 this.tableNameLength = tableNameLength; 122 this.length = length; 123 this.colFlag = colFlag; 124 this.colDecimals = colDecimals; 125 this.mysqlType = mysqlType; 126 127 this.databaseNameStart = databaseNameStart; 129 this.databaseNameLength = databaseNameLength; 130 131 this.originalTableNameStart = originalTableNameStart; 132 this.originalTableNameLength = originalTableNameLength; 133 134 this.originalColumnNameStart = originalColumnNameStart; 135 this.originalColumnNameLength = originalColumnNameLength; 136 137 this.defaultValueStart = defaultValueStart; 138 this.defaultValueLength = defaultValueLength; 139 140 sqlType = MysqlDefs.mysqlToJavaType(mysqlType); 142 143 if (charsetIndex != NO_CHARSET_INFO) { 146 this.charsetIndex = charsetIndex; 147 this.charsetName = CharsetMapping.INDEX_TO_CHARSET[this.charsetIndex]; 148 149 if (this.charsetName == null) { 151 this.charsetName = this.connection.getEncoding(); 152 } 153 } else { 154 this.charsetName = this.connection.getEncoding(); 155 } 156 157 boolean isBinary = isBinary(); 158 159 if ((sqlType == java.sql.Types.LONGVARBINARY) && !isBinary) { 163 sqlType = java.sql.Types.LONGVARCHAR; 164 } else if ((sqlType == java.sql.Types.VARBINARY) && !isBinary) { 165 sqlType = java.sql.Types.VARCHAR; 166 } 167 168 if (!isUnsigned()) { 172 switch (this.mysqlType) { 173 case MysqlDefs.FIELD_TYPE_DECIMAL: 174 this.precisionAdjustFactor = -1; 175 176 break; 177 178 case MysqlDefs.FIELD_TYPE_DOUBLE: 179 case MysqlDefs.FIELD_TYPE_FLOAT: 180 this.precisionAdjustFactor = 1; 181 182 break; 183 } 184 } else { 185 switch (this.mysqlType) { 186 case MysqlDefs.FIELD_TYPE_DOUBLE: 187 case MysqlDefs.FIELD_TYPE_FLOAT: 188 this.precisionAdjustFactor = 1; 189 190 break; 191 } 192 } 193 } 194 195 197 202 public boolean isAutoIncrement() { 203 return ((colFlag & AUTO_INCREMENT_FLAG) > 0); 204 } 205 206 211 public boolean isBinary() { 212 return ((colFlag & 128) > 0); 213 } 214 215 220 public boolean isBlob() { 221 return ((colFlag & 16) > 0); 222 } 223 224 230 public String getCharacterSet() { 231 return this.charsetName; 232 } 233 234 239 public void setConnection(Connection conn) { 240 this.connection = conn; 241 242 this.charsetName = this.connection.getEncoding(); 243 } 244 245 250 public String getDatabaseName() { 251 if ((this.databaseName == null) && (this.databaseNameStart != -1) 252 && (this.databaseNameLength != -1)) { 253 this.databaseName = getStringFromBytes(this.databaseNameStart, 254 this.databaseNameLength); 255 } 256 257 return this.databaseName; 258 } 259 260 265 public String getFullName() { 266 if (fullName == null) { 267 StringBuffer fullNameBuf = new StringBuffer (getTableName().length() 268 + 1 + getName().length()); 269 fullNameBuf.append(tableName); 270 271 fullNameBuf.append('.'); 273 fullNameBuf.append(name); 274 fullName = fullNameBuf.toString(); 275 fullNameBuf = null; 276 } 277 278 return fullName; 279 } 280 281 286 public String getFullOriginalName() { 287 getOriginalName(); 288 289 if (this.originalColumnName == null) { 290 return null; } 292 293 if (fullName == null) { 294 StringBuffer fullOriginalNameBuf = new StringBuffer (getOriginalTableName() 295 .length() 296 + 1 + getOriginalName().length()); 297 fullOriginalNameBuf.append(this.originalTableName); 298 299 fullOriginalNameBuf.append('.'); 301 fullOriginalNameBuf.append(this.originalColumnName); 302 this.fullOriginalName = fullOriginalNameBuf.toString(); 303 fullOriginalNameBuf = null; 304 } 305 306 return this.fullOriginalName; 307 } 308 309 314 public int getLength() { 315 return length; 316 } 317 318 323 public boolean isMultipleKey() { 324 return ((colFlag & 8) > 0); 325 } 326 327 332 public int getMysqlType() { 333 return mysqlType; 334 } 335 336 341 public String getName() { 342 if (this.name == null) { 343 this.name = getStringFromBytes(this.nameStart, this.nameLength); 344 } 345 346 return name; 347 } 348 349 354 public String getOriginalName() { 355 if ((this.originalColumnName == null) 356 && (this.originalColumnNameStart != -1) 357 && (this.originalColumnNameLength != -1)) { 358 this.originalColumnName = getStringFromBytes(this.originalColumnNameStart, 359 this.originalColumnNameLength); 360 } 361 362 return this.originalColumnName; 363 } 364 365 370 public String getOriginalTableName() { 371 if ((this.originalTableName == null) 372 && (this.originalTableNameStart != -1) 373 && (this.originalTableNameLength != -1)) { 374 this.originalTableName = getStringFromBytes(this.originalTableNameStart, 375 this.originalTableNameLength); 376 } 377 378 return this.originalTableName; 379 } 380 381 390 public int getPrecisionAdjustFactor() { 391 return this.precisionAdjustFactor; 392 } 393 394 399 public boolean isPrimaryKey() { 400 return ((colFlag & 2) > 0); 401 } 402 403 408 public int getSQLType() { 409 return sqlType; 410 } 411 412 417 public String getTable() { 418 return getTableName(); 419 } 420 421 426 public String getTableName() { 427 if (tableName == null) { 428 tableName = getStringFromBytes(tableNameStart, tableNameLength); 429 } 430 431 return tableName; 432 } 433 434 439 public boolean isUniqueKey() { 440 return ((colFlag & 4) > 0); 441 } 442 443 448 public boolean isUnsigned() { 449 return ((colFlag & 32) > 0); 450 } 451 452 457 public boolean isZeroFill() { 458 return ((colFlag & 64) > 0); 459 } 460 461 466 public String toString() { 467 return this.getDatabaseName() + " . " + this.getTableName() + "(" + this.getOriginalTableName() + ") . " + this.getName() + "(" + this.getOriginalName() + ")"; 468 469 } 470 471 int getDecimals() { 472 return colDecimals; 473 } 474 475 boolean isNotNull() { 476 return ((colFlag & 1) > 0); 477 } 478 479 483 private String getStringFromBytes(int stringStart, int stringLength) { 484 if ((stringStart == -1) || (stringLength == -1)) { 485 return null; 486 } 487 488 String stringVal = null; 489 490 if (connection != null) { 491 if (connection.useUnicode()) { 492 String encoding = connection.getEncoding(); 493 494 if (encoding != null) { 495 496 SingleByteCharsetConverter converter = null; 497 498 if (this.connection != null) { 499 converter = this.connection.getCharsetConverter(encoding); 500 } 501 502 if (converter != null) { stringVal = converter.toString(buffer, stringStart, 504 stringLength); 505 } else { 506 byte[] stringBytes = new byte[stringLength]; 508 509 int endIndex = stringStart + stringLength; 510 int pos = 0; 511 512 for (int i = stringStart; i < endIndex; i++) { 513 stringBytes[pos++] = buffer[i]; 514 } 515 516 try { 517 stringVal = new String (stringBytes, encoding); 518 } catch (UnsupportedEncodingException ue) { 519 throw new RuntimeException ( 520 "Unsupported character encoding '" + encoding 521 + "'"); 522 } 523 } 524 } else { 525 stringVal = StringUtils.toAsciiString(buffer, stringStart, 527 stringLength); 528 } 529 } else { 530 stringVal = StringUtils.toAsciiString(buffer, stringStart, 532 stringLength); 533 } 534 } else { 535 stringVal = StringUtils.toAsciiString(buffer, stringStart, 537 stringLength); 538 } 539 540 return stringVal; 541 } 542 } 543 | Popular Tags |