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 | this.charsetName = this.connection |
160 | .getCharsetNameForIndex(this.charsetIndex); |
161 | |
162 | // Map MySqlTypes to java.sql Types |
163 | this.sqlType = MysqlDefs.mysqlToJavaType(this.mysqlType); |
164 | |
165 | // Re-map to 'real' blob type, if we're a BLOB |
166 | |
167 | if (this.mysqlType == MysqlDefs.FIELD_TYPE_BLOB) { |
168 | if (this.charsetIndex == 63 || |
169 | !this.connection.versionMeetsMinimum(4, 1, 0)) { |
170 | setBlobTypeBasedOnLength(); |
171 | this.sqlType = MysqlDefs.mysqlToJavaType(this.mysqlType); |
172 | } else { |
173 | // *TEXT masquerading as blob |
174 | this.mysqlType = MysqlDefs.FIELD_TYPE_VAR_STRING; |
175 | this.sqlType = Types.LONGVARCHAR; |
176 | } |
177 | } |
178 | |
179 | // Handle VARBINARY/BINARY (server doesn't have a different type |
180 | // for this |
181 | |
182 | boolean isBinary = isBinary(); |
183 | |
184 | if (this.connection.versionMeetsMinimum(4, 1, 0) && |
185 | this.mysqlType == MysqlDefs.FIELD_TYPE_VAR_STRING && |
186 | isBinary && |
187 | this.charsetIndex == 63) { |
188 | if (this.isOpaqueBinary()) { |
189 | this.sqlType = Types.VARBINARY; |
190 | } |
191 | } |
192 | |
193 | if (this.connection.versionMeetsMinimum(4, 1, 0) && |
194 | this.mysqlType == MysqlDefs.FIELD_TYPE_STRING && |
195 | isBinary && this.charsetIndex == 63) { |
196 | // |
197 | // Okay, this is a hack, but there's currently no way |
198 | // to easily distinguish something like DATE_FORMAT( ..) |
199 | // from the "BINARY" column type, other than looking |
200 | // at the original column name. |
201 | // |
202 | |
203 | if (isOpaqueBinary()) { |
204 | this.sqlType = Types.BINARY; |
205 | } |
206 | } |
207 | |
208 | if (this.sqlType == Types.TINYINT && this.length == 1 |
209 | && this.connection.getTinyInt1isBit()) { |
210 | // Adjust for pseudo-boolean |
211 | if (conn.getTinyInt1isBit()) { |
212 | if (conn.getTransformedBitIsBoolean()) { |
213 | this.sqlType = Types.BOOLEAN; |
214 | } else { |
215 | this.sqlType = Types.BIT; |
216 | } |
217 | } |
218 | |
219 | } |
220 | |
221 | if (this.mysqlType == MysqlDefs.FIELD_TYPE_BIT) { |
222 | this.isSingleBit = (this.length == 0); |
223 | |
224 | if (this.connection != null && (this.connection.versionMeetsMinimum(5, 0, 21) || |
225 | this.connection.versionMeetsMinimum(5, 1, 10)) && this.length == 1) { |
226 | this.isSingleBit = true; |
227 | } |
228 | |
229 | if (this.isSingleBit) { |
230 | this.sqlType = Types.BIT; |
231 | } else { |
232 | this.sqlType = Types.VARBINARY; |
233 | this.colFlag |= 128; // we need to pretend this is a full |
234 | this.colFlag |= 16; // binary blob |
235 | } |
236 | } |
237 | |
238 | |
239 | |
240 | // |
241 | // Handle TEXT type (special case), Fix proposed by Peter McKeown |
242 | // |
243 | if ((this.sqlType == java.sql.Types.LONGVARBINARY) && !isBinary) { |
244 | this.sqlType = java.sql.Types.LONGVARCHAR; |
245 | } else if ((this.sqlType == java.sql.Types.VARBINARY) && !isBinary) { |
246 | this.sqlType = java.sql.Types.VARCHAR; |
247 | } |
248 | |
249 | // |
250 | // Handle odd values for 'M' for floating point/decimal numbers |
251 | // |
252 | if (!isUnsigned()) { |
253 | switch (this.mysqlType) { |
254 | case MysqlDefs.FIELD_TYPE_DECIMAL: |
255 | case MysqlDefs.FIELD_TYPE_NEW_DECIMAL: |
256 | this.precisionAdjustFactor = -1; |
257 | |
258 | break; |
259 | case MysqlDefs.FIELD_TYPE_DOUBLE: |
260 | case MysqlDefs.FIELD_TYPE_FLOAT: |
261 | this.precisionAdjustFactor = 1; |
262 | |
263 | break; |
264 | } |
265 | } else { |
266 | switch (this.mysqlType) { |
267 | case MysqlDefs.FIELD_TYPE_DOUBLE: |
268 | case MysqlDefs.FIELD_TYPE_FLOAT: |
269 | this.precisionAdjustFactor = 1; |
270 | |
271 | break; |
272 | } |
273 | } |
274 | |
275 | checkForImplicitTemporaryTable(); |
276 | } |
277 | |
278 | /** |
279 | * Constructor used when communicating with pre 4.1 servers |
280 | */ |
281 | Field(Connection conn, byte[] buffer, int nameStart, int nameLength, |
282 | int tableNameStart, int tableNameLength, int length, int mysqlType, |
283 | short colFlag, int colDecimals) throws SQLException { |
284 | this(conn, buffer, -1, -1, tableNameStart, tableNameLength, -1, -1, |
285 | nameStart, nameLength, -1, -1, length, mysqlType, colFlag, |
286 | colDecimals, -1, -1, NO_CHARSET_INFO); |
287 | } |
288 | |
289 | /** |
290 | * Constructor used by DatabaseMetaData methods. |
291 | */ |
292 | Field(String tableName, String columnName, int jdbcType, int length) { |
293 | this.tableName = tableName; |
294 | this.name = columnName; |
295 | this.length = length; |
296 | this.sqlType = jdbcType; |
297 | this.colFlag = 0; |
298 | this.colDecimals = 0; |
299 | } |
300 | |
301 | // ~ Methods |
302 | // ---------------------------------------------------------------- |
303 | |
304 | private void checkForImplicitTemporaryTable() { |
305 | this.isImplicitTempTable = this.tableNameLength > 5 |
306 | && this.buffer[tableNameStart] == (byte) '#' |
307 | && this.buffer[tableNameStart + 1] == (byte) 's' |
308 | && this.buffer[tableNameStart + 2] == (byte) 'q' |
309 | && this.buffer[tableNameStart + 3] == (byte) 'l' |
310 | && this.buffer[tableNameStart + 4] == (byte) '_'; |
311 | } |
312 | |
313 | /** |
314 | * Returns the character set (if known) for this field. |
315 | * |
316 | * @return the character set |
317 | */ |
318 | public String getCharacterSet() { |
319 | return this.charsetName; |
320 | } |
321 | |
322 | public synchronized String getCollation() throws SQLException { |
323 | if (this.collationName == null) { |
324 | if (this.connection != null) { |
325 | if (this.connection.versionMeetsMinimum(4, 1, 0)) { |
326 | java.sql.DatabaseMetaData dbmd = this.connection |
327 | .getMetaData(); |
328 | |
329 | String quotedIdStr = dbmd.getIdentifierQuoteString(); |
330 | |
331 | if (" ".equals(quotedIdStr)) { //$NON-NLS-1$ |
332 | quotedIdStr = ""; //$NON-NLS-1$ |
333 | } |
334 | |
335 | String csCatalogName = getDatabaseName(); |
336 | String csTableName = getOriginalTableName(); |
337 | String csColumnName = getOriginalName(); |
338 | |
339 | if (csCatalogName != null && csCatalogName.length() != 0 |
340 | && csTableName != null && csTableName.length() != 0 |
341 | && csColumnName != null |
342 | && csColumnName.length() != 0) { |
343 | StringBuffer queryBuf = new StringBuffer(csCatalogName |
344 | .length() |
345 | + csTableName.length() + 28); |
346 | queryBuf.append("SHOW FULL COLUMNS FROM "); //$NON-NLS-1$ |
347 | queryBuf.append(quotedIdStr); |
348 | queryBuf.append(csCatalogName); |
349 | queryBuf.append(quotedIdStr); |
350 | queryBuf.append("."); //$NON-NLS-1$ |
351 | queryBuf.append(quotedIdStr); |
352 | queryBuf.append(csTableName); |
353 | queryBuf.append(quotedIdStr); |
354 | |
355 | java.sql.Statement collationStmt = null; |
356 | java.sql.ResultSet collationRs = null; |
357 | |
358 | try { |
359 | collationStmt = this.connection.createStatement(); |
360 | |
361 | collationRs = collationStmt.executeQuery(queryBuf |
362 | .toString()); |
363 | |
364 | while (collationRs.next()) { |
365 | if (csColumnName.equals(collationRs |
366 | .getString("Field"))) { //$NON-NLS-1$ |
367 | this.collationName = collationRs |
368 | .getString("Collation"); //$NON-NLS-1$ |
369 | |
370 | break; |
371 | } |
372 | } |
373 | } finally { |
374 | if (collationRs != null) { |
375 | collationRs.close(); |
376 | collationRs = null; |
377 | } |
378 | |
379 | if (collationStmt != null) { |
380 | collationStmt.close(); |
381 | collationStmt = null; |
382 | } |
383 | } |
384 | |
385 | } |
386 | } |
387 | |
388 | } |
389 | |
390 | } |
391 | |
392 | return this.collationName; |
393 | } |
394 | |
395 | public String getColumnLabel() throws SQLException { |
396 | return getName(); // column name if not aliased, alias if used |
397 | } |
398 | |
399 | /** |
400 | * DOCUMENT ME! |
401 | * |
402 | * @return DOCUMENT ME! |
403 | */ |
404 | public String getDatabaseName() throws SQLException { |
405 | if ((this.databaseName == null) && (this.databaseNameStart != -1) |
406 | && (this.databaseNameLength != -1)) { |
407 | this.databaseName = getStringFromBytes(this.databaseNameStart, |
408 | this.databaseNameLength); |
409 | } |
410 | |
411 | return this.databaseName; |
412 | } |
413 | |
414 | int getDecimals() { |
415 | return this.colDecimals; |
416 | } |
417 | |
418 | /** |
419 | * DOCUMENT ME! |
420 | * |
421 | * @return DOCUMENT ME! |
422 | */ |
423 | public String getFullName() throws SQLException { |
424 | if (this.fullName == null) { |
425 | StringBuffer fullNameBuf = new StringBuffer(getTableName().length() |
426 | + 1 + getName().length()); |
427 | fullNameBuf.append(this.tableName); |
428 | |
429 | // much faster to append a char than a String |
430 | fullNameBuf.append('.'); |
431 | fullNameBuf.append(this.name); |
432 | this.fullName = fullNameBuf.toString(); |
433 | fullNameBuf = null; |
434 | } |
435 | |
436 | return this.fullName; |
437 | } |
438 | |
439 | /** |
440 | * DOCUMENT ME! |
441 | * |
442 | * @return DOCUMENT ME! |
443 | */ |
444 | public String getFullOriginalName() throws SQLException { |
445 | getOriginalName(); |
446 | |
447 | if (this.originalColumnName == null) { |
448 | return null; // we don't have this information |
449 | } |
450 | |
451 | if (this.fullName == null) { |
452 | StringBuffer fullOriginalNameBuf = new StringBuffer( |
453 | getOriginalTableName().length() + 1 |
454 | + getOriginalName().length()); |
455 | fullOriginalNameBuf.append(this.originalTableName); |
456 | |
457 | // much faster to append a char than a String |
458 | fullOriginalNameBuf.append('.'); |
459 | fullOriginalNameBuf.append(this.originalColumnName); |
460 | this.fullOriginalName = fullOriginalNameBuf.toString(); |
461 | fullOriginalNameBuf = null; |
462 | } |
463 | |
464 | return this.fullOriginalName; |
465 | } |
466 | |
467 | /** |
468 | * DOCUMENT ME! |
469 | * |
470 | * @return DOCUMENT ME! |
471 | */ |
472 | public long getLength() { |
473 | return this.length; |
474 | } |
475 | |
476 | public int getMaxBytesPerCharacter() throws SQLException { |
477 | return this.connection.getMaxBytesPerChar(getCharacterSet()); |
478 | } |
479 | |
480 | /** |
481 | * DOCUMENT ME! |
482 | * |
483 | * @return DOCUMENT ME! |
484 | */ |
485 | public int getMysqlType() { |
486 | return this.mysqlType; |
487 | } |
488 | |
489 | /** |
490 | * DOCUMENT ME! |
491 | * |
492 | * @return DOCUMENT ME! |
493 | */ |
494 | public String getName() throws SQLException { |
495 | if (this.name == null) { |
496 | this.name = getStringFromBytes(this.nameStart, this.nameLength); |
497 | } |
498 | |
499 | return this.name; |
500 | } |
501 | |
502 | public String getNameNoAliases() throws SQLException { |
503 | if (this.useOldNameMetadata) { |
504 | return getName(); |
505 | } |
506 | |
507 | if (this.connection != null && |
508 | this.connection.versionMeetsMinimum(4, 1, 0)) { |
509 | return getOriginalName(); |
510 | } |
511 | |
512 | return getName(); |
513 | } |
514 | |
515 | /** |
516 | * DOCUMENT ME! |
517 | * |
518 | * @return DOCUMENT ME! |
519 | */ |
520 | public String getOriginalName() throws SQLException { |
521 | if ((this.originalColumnName == null) |
522 | && (this.originalColumnNameStart != -1) |
523 | && (this.originalColumnNameLength != -1)) { |
524 | this.originalColumnName = getStringFromBytes( |
525 | this.originalColumnNameStart, this.originalColumnNameLength); |
526 | } |
527 | |
528 | return this.originalColumnName; |
529 | } |
530 | |
531 | /** |
532 | * DOCUMENT ME! |
533 | * |
534 | * @return DOCUMENT ME! |
535 | */ |
536 | public String getOriginalTableName() throws SQLException { |
537 | if ((this.originalTableName == null) |
538 | && (this.originalTableNameStart != -1) |
539 | && (this.originalTableNameLength != -1)) { |
540 | this.originalTableName = getStringFromBytes( |
541 | this.originalTableNameStart, this.originalTableNameLength); |
542 | } |
543 | |
544 | return this.originalTableName; |
545 | } |
546 | |
547 | /** |
548 | * Returns amount of correction that should be applied to the precision |
549 | * value. |
550 | * |
551 | * Different versions of MySQL report different precision values. |
552 | * |
553 | * @return the amount to adjust precision value by. |
554 | */ |
555 | public int getPrecisionAdjustFactor() { |
556 | return this.precisionAdjustFactor; |
557 | } |
558 | |
559 | /** |
560 | * DOCUMENT ME! |
561 | * |
562 | * @return DOCUMENT ME! |
563 | */ |
564 | public int getSQLType() { |
565 | return this.sqlType; |
566 | } |
567 | |
568 | /** |
569 | * Create a string with the correct charset encoding from the byte-buffer |
570 | * that contains the data for this field |
571 | */ |
572 | private String getStringFromBytes(int stringStart, int stringLength) |
573 | throws SQLException { |
574 | if ((stringStart == -1) || (stringLength == -1)) { |
575 | return null; |
576 | } |
577 | |
578 | String stringVal = null; |
579 | |
580 | if (this.connection != null) { |
581 | if (this.connection.getUseUnicode()) { |
582 | String encoding = this.connection.getCharacterSetMetadata(); |
583 | |
584 | if (encoding == null) { |
585 | encoding = connection.getEncoding(); |
586 | } |
587 | |
588 | if (encoding != null) { |
589 | SingleByteCharsetConverter converter = null; |
590 | |
591 | if (this.connection != null) { |
592 | converter = this.connection |
593 | .getCharsetConverter(encoding); |
594 | } |
595 | |
596 | if (converter != null) { // we have a converter |
597 | stringVal = converter.toString(this.buffer, |
598 | stringStart, stringLength); |
599 | } else { |
600 | // we have no converter, use JVM converter |
601 | byte[] stringBytes = new byte[stringLength]; |
602 | |
603 | int endIndex = stringStart + stringLength; |
604 | int pos = 0; |
605 | |
606 | for (int i = stringStart; i < endIndex; i++) { |
607 | stringBytes[pos++] = this.buffer[i]; |
608 | } |
609 | |
610 | try { |
611 | stringVal = new String(stringBytes, encoding); |
612 | } catch (UnsupportedEncodingException ue) { |
613 | throw new RuntimeException(Messages |
614 | .getString("Field.12") + encoding //$NON-NLS-1$ |
615 | + Messages.getString("Field.13")); //$NON-NLS-1$ |
616 | } |
617 | } |
618 | } else { |
619 | // we have no encoding, use JVM standard charset |
620 | stringVal = StringUtils.toAsciiString(this.buffer, |
621 | stringStart, stringLength); |
622 | } |
623 | } else { |
624 | // we are not using unicode, so use JVM standard charset |
625 | stringVal = StringUtils.toAsciiString(this.buffer, stringStart, |
626 | stringLength); |
627 | } |
628 | } else { |
629 | // we don't have a connection, so punt |
630 | stringVal = StringUtils.toAsciiString(this.buffer, stringStart, |
631 | stringLength); |
632 | } |
633 | |
634 | return stringVal; |
635 | } |
636 | |
637 | /** |
638 | * DOCUMENT ME! |
639 | * |
640 | * @return DOCUMENT ME! |
641 | */ |
642 | public String getTable() throws SQLException { |
643 | return getTableName(); |
644 | } |
645 | |
646 | /** |
647 | * DOCUMENT ME! |
648 | * |
649 | * @return DOCUMENT ME! |
650 | */ |
651 | public String getTableName() throws SQLException { |
652 | if (this.tableName == null) { |
653 | this.tableName = getStringFromBytes(this.tableNameStart, |
654 | this.tableNameLength); |
655 | } |
656 | |
657 | return this.tableName; |
658 | } |
659 | |
660 | public String getTableNameNoAliases() throws SQLException { |
661 | if (this.connection.versionMeetsMinimum(4, 1, 0)) { |
662 | return getOriginalTableName(); |
663 | } |
664 | |
665 | return getTableName(); // pre-4.1, no aliases returned |
666 | } |
667 | |
668 | /** |
669 | * DOCUMENT ME! |
670 | * |
671 | * @return DOCUMENT ME! |
672 | */ |
673 | public boolean isAutoIncrement() { |
674 | return ((this.colFlag & AUTO_INCREMENT_FLAG) > 0); |
675 | } |
676 | |
677 | /** |
678 | * DOCUMENT ME! |
679 | * |
680 | * @return DOCUMENT ME! |
681 | */ |
682 | public boolean isBinary() { |
683 | return ((this.colFlag & 128) > 0); |
684 | } |
685 | |
686 | /** |
687 | * DOCUMENT ME! |
688 | * |
689 | * @return DOCUMENT ME! |
690 | */ |
691 | public boolean isBlob() { |
692 | return ((this.colFlag & 16) > 0); |
693 | } |
694 | |
695 | /** |
696 | * Is this field owned by a server-created temporary table? |
697 | * |
698 | * @return |
699 | */ |
700 | private boolean isImplicitTemporaryTable() { |
701 | return this.isImplicitTempTable; |
702 | } |
703 | |
704 | /** |
705 | * DOCUMENT ME! |
706 | * |
707 | * @return DOCUMENT ME! |
708 | */ |
709 | public boolean isMultipleKey() { |
710 | return ((this.colFlag & 8) > 0); |
711 | } |
712 | |
713 | boolean isNotNull() { |
714 | return ((this.colFlag & 1) > 0); |
715 | } |
716 | |
717 | boolean isOpaqueBinary() throws SQLException { |
718 | |
719 | // |
720 | // Detect CHAR(n) CHARACTER SET BINARY which is a synonym for |
721 | // fixed-length binary types |
722 | // |
723 | |
724 | if (this.charsetIndex == 63 && isBinary() |
725 | && (this.getMysqlType() == MysqlDefs.FIELD_TYPE_STRING || |
726 | this.getMysqlType() == MysqlDefs.FIELD_TYPE_VAR_STRING)) { |
727 | |
728 | if (this.originalTableNameLength == 0) { |
729 | return false; // Probably from function |
730 | } |
731 | |
732 | // Okay, queries resolved by temp tables also have this 'signature', |
733 | // check for that |
734 | |
735 | return !isImplicitTemporaryTable(); |
736 | } |
737 | |
738 | return (this.connection.versionMeetsMinimum(4, 1, 0) && "binary" |
739 | .equalsIgnoreCase(getCharacterSet())); |
740 | |
741 | } |
742 | |
743 | /** |
744 | * DOCUMENT ME! |
745 | * |
746 | * @return DOCUMENT ME! |
747 | */ |
748 | public boolean isPrimaryKey() { |
749 | return ((this.colFlag & 2) > 0); |
750 | } |
751 | |
752 | /** |
753 | * Is this field _definitely_ not writable? |
754 | * |
755 | * @return true if this field can not be written to in an INSERT/UPDATE |
756 | * statement. |
757 | */ |
758 | boolean isReadOnly() throws SQLException { |
759 | if (this.connection.versionMeetsMinimum(4, 1, 0)) { |
760 | String orgColumnName = getOriginalName(); |
761 | String orgTableName = getOriginalTableName(); |
762 | |
763 | return !(orgColumnName != null && orgColumnName.length() > 0 |
764 | && orgTableName != null && orgTableName.length() > 0); |
765 | } |
766 | |
767 | return false; // we can't tell definitively in this case. |
768 | } |
769 | |
770 | /** |
771 | * DOCUMENT ME! |
772 | * |
773 | * @return DOCUMENT ME! |
774 | */ |
775 | public boolean isUniqueKey() { |
776 | return ((this.colFlag & 4) > 0); |
777 | } |
778 | |
779 | /** |
780 | * DOCUMENT ME! |
781 | * |
782 | * @return DOCUMENT ME! |
783 | */ |
784 | public boolean isUnsigned() { |
785 | return ((this.colFlag & 32) > 0); |
786 | } |
787 | |
788 | /** |
789 | * DOCUMENT ME! |
790 | * |
791 | * @return DOCUMENT ME! |
792 | */ |
793 | public boolean isZeroFill() { |
794 | return ((this.colFlag & 64) > 0); |
795 | } |
796 | |
797 | // |
798 | // MySQL only has one protocol-level BLOB type that it exposes |
799 | // which is FIELD_TYPE_BLOB, although we can divine what the |
800 | // actual type is by the length reported ... |
801 | // |
802 | private void setBlobTypeBasedOnLength() { |
803 | if (this.length == MysqlDefs.LENGTH_TINYBLOB) { |
804 | this.mysqlType = MysqlDefs.FIELD_TYPE_TINY_BLOB; |
805 | } else if (this.length == MysqlDefs.LENGTH_BLOB) { |
806 | this.mysqlType = MysqlDefs.FIELD_TYPE_BLOB; |
807 | } else if (this.length == MysqlDefs.LENGTH_MEDIUMBLOB) { |
808 | this.mysqlType = MysqlDefs.FIELD_TYPE_MEDIUM_BLOB; |
809 | } else if (this.length == MysqlDefs.LENGTH_LONGBLOB) { |
810 | this.mysqlType = MysqlDefs.FIELD_TYPE_LONG_BLOB; |
811 | } |
812 | } |
813 | |
814 | /** |
815 | * DOCUMENT ME! |
816 | * |
817 | * @param conn |
818 | * DOCUMENT ME! |
819 | */ |
820 | public void setConnection(Connection conn) { |
821 | this.connection = conn; |
822 | |
823 | this.charsetName = this.connection.getEncoding(); |
824 | } |
825 | |
826 | void setMysqlType(int type) { |
827 | this.mysqlType = type; |
828 | this.sqlType = MysqlDefs.mysqlToJavaType(this.mysqlType); |
829 | } |
830 | |
831 | protected void setUseOldNameMetadata(boolean useOldNameMetadata) { |
832 | this.useOldNameMetadata = useOldNameMetadata; |
833 | } |
834 | |
835 | /** |
836 | * DOCUMENT ME! |
837 | * |
838 | * @return DOCUMENT ME! |
839 | */ |
840 | public String toString() { |
841 | try { |
842 | StringBuffer asString = new StringBuffer(); |
843 | asString.append(super.toString()); |
844 | |
845 | asString.append("\n catalog: "); |
846 | asString.append(this.getDatabaseName()); |
847 | asString.append("\n table name: "); |
848 | asString.append(this.getTableName()); |
849 | asString.append("\n original table name: "); |
850 | asString.append(this.getOriginalTableName()); |
851 | asString.append("\n column name: "); |
852 | asString.append(this.getName()); |
853 | asString.append("\n original column name: "); |
854 | asString.append(this.getOriginalName()); |
855 | asString.append("\n MySQL data type: "); |
856 | asString.append(getMysqlType()); |
857 | asString.append("\n\nData as received from server:\n\n"); |
858 | asString.append(StringUtils.dumpAsHex(this.buffer, this.buffer.length)); |
859 | |
860 | return asString.toString(); |
861 | } catch (SQLException sqlEx) { |
862 | return super.toString(); |
863 | } |
864 | |
865 | } |
866 | |
867 | protected boolean isSingleBit() { |
868 | return this.isSingleBit; |
869 | } |
870 | } |