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
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
/*
 Copyright  2002-2004 MySQL AB, 2008 Sun Microsystems

 This program is free software; you can redistribute it and/or modify
 it under the terms of version 2 of the GNU General Public License as
 published by the Free Software Foundation.

 There are special exceptions to the terms and conditions of the GPL
 as it is applied to this software. View the full text of the
 exception in file EXCEPTIONS-CONNECTOR-J in the directory of this
 software distribution.

 This program is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 GNU General Public License for more details.

 You should have received a copy of the GNU General Public License
 along with this program; if not, write to the Free Software
 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA



 */
package com.mysql.jdbc;

import java.io.UnsupportedEncodingException;
import java.sql.SQLException;
import java.sql.Types;
import java.util.regex.PatternSyntaxException;

/**
 * Field is a class used to describe fields in a ResultSet
 *
 * @author Mark Matthews
 * @version $Id$
 */
public class Field {

	private static final int AUTO_INCREMENT_FLAG = 512;

	private static final int NO_CHARSET_INFO = -1;

	private byte[] buffer;

	private int charsetIndex = 0;

	private String charsetName = null;

	private int colDecimals;

	private short colFlag;

	private String collationName = null;

	private ConnectionImpl connection = null;

	private String databaseName = null;

	private int databaseNameLength = -1;

	// database name info
	private int databaseNameStart = -1;

	private int defaultValueLength = -1;

	// default value info - from COM_LIST_FIELDS execution
	private int defaultValueStart = -1;

	private String fullName = null;

	private String fullOriginalName = null;

	private boolean isImplicitTempTable = false;

	private long length; // Internal length of the field;

	private int mysqlType = -1; // the MySQL type

	private String name; // The Field name

	private int nameLength;

	private int nameStart;

	private String originalColumnName = null;

	private int originalColumnNameLength = -1;

	// column name info (before aliasing)
	private int originalColumnNameStart = -1;

	private String originalTableName = null;

	private int originalTableNameLength = -1;

	// table name info (before aliasing)
	private int originalTableNameStart = -1;

	private int precisionAdjustFactor = 0;

	private int sqlType = -1; // the java.sql.Type

	private String tableName; // The Name of the Table

	private int tableNameLength;

	private int tableNameStart;

	private boolean useOldNameMetadata = false;

	private boolean isSingleBit;

	private int maxBytesPerChar;

	/**
	 * Constructor used when communicating with 4.1 and newer servers
	 */
	Field(ConnectionImpl conn, byte[] buffer, int databaseNameStart,
			int databaseNameLength, int tableNameStart, int tableNameLength,
			int originalTableNameStart, int originalTableNameLength,
			int nameStart, int nameLength, int originalColumnNameStart,
			int originalColumnNameLength, long length, int mysqlType,
			short colFlag, int colDecimals, int defaultValueStart,
			int defaultValueLength, int charsetIndex) throws SQLException {
		this.connection = conn;
		this.buffer = buffer;
		this.nameStart = nameStart;
		this.nameLength = nameLength;
		this.tableNameStart = tableNameStart;
		this.tableNameLength = tableNameLength;
		this.length = length;
		this.colFlag = colFlag;
		this.colDecimals = colDecimals;
		this.mysqlType = mysqlType;

		// 4.1 field info...
		this.databaseNameStart = databaseNameStart;
		this.databaseNameLength = databaseNameLength;

		this.originalTableNameStart = originalTableNameStart;
		this.originalTableNameLength = originalTableNameLength;

		this.originalColumnNameStart = originalColumnNameStart;
		this.originalColumnNameLength = originalColumnNameLength;

		this.defaultValueStart = defaultValueStart;
		this.defaultValueLength = defaultValueLength;

		// If we're not running 4.1 or newer, use the connection's
		// charset
		this.charsetIndex = charsetIndex;


		// Map MySqlTypes to java.sql Types
		this.sqlType = MysqlDefs.mysqlToJavaType(this.mysqlType);

        checkForImplicitTemporaryTable();
		// Re-map to 'real' blob type, if we're a BLOB
        boolean isFromFunction = this.originalTableNameLength == 0;
        
		if (this.mysqlType == MysqlDefs.FIELD_TYPE_BLOB) {
		    if (this.connection != null && this.connection.getBlobsAreStrings() ||
		            (this.connection.getFunctionsNeverReturnBlobs() && isFromFunction)) {
		        this.sqlType = Types.VARCHAR;
		        this.mysqlType = MysqlDefs.FIELD_TYPE_VARCHAR;
		    } else if (this.charsetIndex == 63 ||
					!this.connection.versionMeetsMinimum(4, 1, 0)) {
				if (this.connection.getUseBlobToStoreUTF8OutsideBMP() 
						&& shouldSetupForUtf8StringInBlob()) {
					setupForUtf8StringInBlob();
				} else {
					setBlobTypeBasedOnLength();
					this.sqlType = MysqlDefs.mysqlToJavaType(this.mysqlType);
				}
			} else {
				// *TEXT masquerading as blob
				this.mysqlType = MysqlDefs.FIELD_TYPE_VAR_STRING;
				this.sqlType = Types.LONGVARCHAR;
			}
		}

		if (this.sqlType == Types.TINYINT && this.length == 1
				&& this.connection.getTinyInt1isBit()) {
			// Adjust for pseudo-boolean
			if (conn.getTinyInt1isBit()) {
				if (conn.getTransformedBitIsBoolean()) {
					this.sqlType = Types.BOOLEAN;
				} else {
					this.sqlType = Types.BIT;
				}
			}

		}

		if (!isNativeNumericType() && !isNativeDateTimeType()) {
			this.charsetName = this.connection
				.getCharsetNameForIndex(this.charsetIndex);


			// Handle VARBINARY/BINARY (server doesn't have a different type
			// for this

			boolean isBinary = isBinary();

			if (this.connection.versionMeetsMinimum(4, 1, 0) &&
					this.mysqlType == MysqlDefs.FIELD_TYPE_VAR_STRING &&
					isBinary &&
					this.charsetIndex == 63) {
				if (this.connection != null && (this.connection.getFunctionsNeverReturnBlobs() && isFromFunction)) {
			        this.sqlType = Types.VARCHAR;
			        this.mysqlType = MysqlDefs.FIELD_TYPE_VARCHAR;
				} else if (this.isOpaqueBinary()) {
					this.sqlType = Types.VARBINARY;
				} 
			}

			if (this.connection.versionMeetsMinimum(4, 1, 0) &&
					this.mysqlType == MysqlDefs.FIELD_TYPE_STRING &&
					isBinary && this.charsetIndex == 63) {
				//
				// Okay, this is a hack, but there's currently no way
				// to easily distinguish something like DATE_FORMAT( ..)
				// from the "BINARY" column type, other than looking
				// at the original column name.
				//

				if (isOpaqueBinary() && !this.connection.getBlobsAreStrings()) {
					this.sqlType = Types.BINARY;
				}
			}



			if (this.mysqlType == MysqlDefs.FIELD_TYPE_BIT) {
				this.isSingleBit = (this.length == 0);

				if (this.connection != null && (this.connection.versionMeetsMinimum(5, 0, 21) ||
						this.connection.versionMeetsMinimum(5, 1, 10)) && this.length == 1) {
					this.isSingleBit = true;
				}

				if (this.isSingleBit) {
					this.sqlType = Types.BIT;
				} else {
					this.sqlType = Types.VARBINARY;
					this.colFlag |= 128; // we need to pretend this is a full
					this.colFlag |= 16; // binary blob
					isBinary = true;
				}
			}

			//
			// Handle TEXT type (special case), Fix proposed by Peter McKeown
			//
			if ((this.sqlType == java.sql.Types.LONGVARBINARY) && !isBinary) {
				this.sqlType = java.sql.Types.LONGVARCHAR;
			} else if ((this.sqlType == java.sql.Types.VARBINARY) && !isBinary) {
				this.sqlType = java.sql.Types.VARCHAR;
			}
		} else {
			this.charsetName = "US-ASCII";
		}

		//
		// Handle odd values for 'M' for floating point/decimal numbers
		//
		if (!isUnsigned()) {
			switch (this.mysqlType) {
			case MysqlDefs.FIELD_TYPE_DECIMAL:
			case MysqlDefs.FIELD_TYPE_NEW_DECIMAL:
				this.precisionAdjustFactor = -1;

				break;
			case MysqlDefs.FIELD_TYPE_DOUBLE:
			case MysqlDefs.FIELD_TYPE_FLOAT:
				this.precisionAdjustFactor = 1;

				break;
			}
		} else {
			switch (this.mysqlType) {
			case MysqlDefs.FIELD_TYPE_DOUBLE:
			case MysqlDefs.FIELD_TYPE_FLOAT:
				this.precisionAdjustFactor = 1;

				break;
			}
		}
	}

	private boolean shouldSetupForUtf8StringInBlob() throws SQLException {
		String includePattern = this.connection
				.getUtf8OutsideBmpIncludedColumnNamePattern();
		String excludePattern = this.connection
				.getUtf8OutsideBmpExcludedColumnNamePattern();

		if (excludePattern != null
				&& !StringUtils.isEmptyOrWhitespaceOnly(excludePattern)) {
			try {
				if (getOriginalName().matches(excludePattern)) {
					if (includePattern != null
							&& !StringUtils.isEmptyOrWhitespaceOnly(includePattern)) {
						try {
							if (getOriginalName().matches(includePattern)) {
								return true;
							}
						} catch (PatternSyntaxException pse) {
							SQLException sqlEx = SQLError
									.createSQLException(
											"Illegal regex specified for \"utf8OutsideBmpIncludedColumnNamePattern\"",
											SQLError.SQL_STATE_ILLEGAL_ARGUMENT, this.connection.getExceptionInterceptor());

							if (!this.connection.getParanoid()) {
								sqlEx.initCause(pse);
							}

							throw sqlEx;
						}
					}
					
					return false;
				}
			} catch (PatternSyntaxException pse) {
				SQLException sqlEx = SQLError
						.createSQLException(
								"Illegal regex specified for \"utf8OutsideBmpExcludedColumnNamePattern\"",
								SQLError.SQL_STATE_ILLEGAL_ARGUMENT, this.connection.getExceptionInterceptor());

				if (!this.connection.getParanoid()) {
					sqlEx.initCause(pse);
				}

				throw sqlEx;
			}
		}

		return true;
	}

	private void setupForUtf8StringInBlob() {
		if (this.length == MysqlDefs.LENGTH_TINYBLOB || this.length == MysqlDefs.LENGTH_BLOB) {
			this.mysqlType = MysqlDefs.FIELD_TYPE_VARCHAR;
			this.sqlType = Types.VARCHAR;
		}  else {
			this.mysqlType = MysqlDefs.FIELD_TYPE_VAR_STRING;
			this.sqlType = Types.LONGVARCHAR;
		}
		
		this.charsetIndex = 33;	
	}

	/**
	 * Constructor used when communicating with pre 4.1 servers
	 */
	Field(ConnectionImpl conn, byte[] buffer, int nameStart, int nameLength,
			int tableNameStart, int tableNameLength, int length, int mysqlType,
			short colFlag, int colDecimals) throws SQLException {
		this(conn, buffer, -1, -1, tableNameStart, tableNameLength, -1, -1,
				nameStart, nameLength, -1, -1, length, mysqlType, colFlag,
				colDecimals, -1, -1, NO_CHARSET_INFO);
	}

	/**
	 * Constructor used by DatabaseMetaData methods.
	 */
	Field(String tableName, String columnName, int jdbcType, int length) {
		this.tableName = tableName;
		this.name = columnName;
		this.length = length;
		this.sqlType = jdbcType;
		this.colFlag = 0;
		this.colDecimals = 0;
	}
	
	/**
	 * Used by prepared statements to re-use result set data conversion methods
	 * when generating bound parmeter retrieval instance for statement
	 * interceptors.
	 * 
	 * @param tableName
	 *            not used
	 * @param columnName
	 *            not used
	 * @param charsetIndex
	 *            the MySQL collation/character set index
	 * @param jdbcType
	 *            from java.sql.Types
	 * @param length
	 *            length in characters or bytes (for BINARY data).
	 */
	Field(String tableName, String columnName, int charsetIndex, int jdbcType,
			int length) {
		this.tableName = tableName;
		this.name = columnName;
		this.length = length;
		this.sqlType = jdbcType;
		this.colFlag = 0;
		this.colDecimals = 0;
		this.charsetIndex = charsetIndex;
		
		switch (this.sqlType) {
		case Types.BINARY:
		case Types.VARBINARY:
			this.colFlag |= 128;
			this.colFlag |= 16;
			break;
		}
	}
	
	private void checkForImplicitTemporaryTable() {
		this.isImplicitTempTable = this.tableNameLength > 5
				&& this.buffer[tableNameStart] == (byte) '#'
				&& this.buffer[tableNameStart + 1] == (byte) 's'
				&& this.buffer[tableNameStart + 2] == (byte) 'q'
				&& this.buffer[tableNameStart + 3] == (byte) 'l'
				&& this.buffer[tableNameStart + 4] == (byte) '_';
	}

	/**
	 * Returns the character set (if known) for this field.
	 *
	 * @return the character set
	 */
	public String getCharacterSet() throws SQLException {
		return this.charsetName;
	}

	public void setCharacterSet(String javaEncodingName) throws SQLException {
		this.charsetName = javaEncodingName;
		this.charsetIndex = CharsetMapping
				.getCharsetIndexForMysqlEncodingName(javaEncodingName);
	}
	
	public synchronized String getCollation() throws SQLException {
		if (this.collationName == null) {
			if (this.connection != null) {
				if (this.connection.versionMeetsMinimum(4, 1, 0)) {
					if (this.connection.getUseDynamicCharsetInfo()) {
						java.sql.DatabaseMetaData dbmd = this.connection
								.getMetaData();

						String quotedIdStr = dbmd.getIdentifierQuoteString();

						if (" ".equals(quotedIdStr)) { //$NON-NLS-1$
							quotedIdStr = ""; //$NON-NLS-1$
						}

						String csCatalogName = getDatabaseName();
						String csTableName = getOriginalTableName();
						String csColumnName = getOriginalName();

						if (csCatalogName != null && csCatalogName.length() != 0
								&& csTableName != null && csTableName.length() != 0
								&& csColumnName != null
								&& csColumnName.length() != 0) {
							StringBuffer queryBuf = new StringBuffer(csCatalogName
									.length()
									+ csTableName.length() + 28);
							queryBuf.append("SHOW FULL COLUMNS FROM "); //$NON-NLS-1$
							queryBuf.append(quotedIdStr);
							queryBuf.append(csCatalogName);
							queryBuf.append(quotedIdStr);
							queryBuf.append("."); //$NON-NLS-1$
							queryBuf.append(quotedIdStr);
							queryBuf.append(csTableName);
							queryBuf.append(quotedIdStr);

							java.sql.Statement collationStmt = null;
							java.sql.ResultSet collationRs = null;

							try {
								collationStmt = this.connection.createStatement();

								collationRs = collationStmt.executeQuery(queryBuf
										.toString());

								while (collationRs.next()) {
									if (csColumnName.equals(collationRs
											.getString("Field"))) { //$NON-NLS-1$
										this.collationName = collationRs
												.getString("Collation"); //$NON-NLS-1$

										break;
									}
								}
							} finally {
								if (collationRs != null) {
									collationRs.close();
									collationRs = null;
								}

								if (collationStmt != null) {
									collationStmt.close();
									collationStmt = null;
								}
							}
						}
					} else {
						this.collationName = CharsetMapping.INDEX_TO_COLLATION[charsetIndex];
					}
				}
			}
		}

		return this.collationName;
	}
	
	public String getColumnLabel() throws SQLException {
		return getName(); // column name if not aliased, alias if used
	}

	/**
	 * DOCUMENT ME!
	 *
	 * @return DOCUMENT ME!
	 */
	public String getDatabaseName() throws SQLException {
		if ((this.databaseName == null) && (this.databaseNameStart != -1)
				&& (this.databaseNameLength != -1)) {
			this.databaseName = getStringFromBytes(this.databaseNameStart,
					this.databaseNameLength);
		}

		return this.databaseName;
	}

	int getDecimals() {
		return this.colDecimals;
	}

	/**
	 * DOCUMENT ME!
	 *
	 * @return DOCUMENT ME!
	 */
	public String getFullName() throws SQLException {
		if (this.fullName == null) {
			StringBuffer fullNameBuf = new StringBuffer(getTableName().length()
					+ 1 + getName().length());
			fullNameBuf.append(this.tableName);

			// much faster to append a char than a String
			fullNameBuf.append('.');
			fullNameBuf.append(this.name);
			this.fullName = fullNameBuf.toString();
			fullNameBuf = null;
		}

		return this.fullName;
	}

	/**
	 * DOCUMENT ME!
	 *
	 * @return DOCUMENT ME!
	 */
	public String getFullOriginalName() throws SQLException {
		getOriginalName();

		if (this.originalColumnName == null) {
			return null; // we don't have this information
		}

		if (this.fullName == null) {
			StringBuffer fullOriginalNameBuf = new StringBuffer(
					getOriginalTableName().length() + 1
							+ getOriginalName().length());
			fullOriginalNameBuf.append(this.originalTableName);

			// much faster to append a char than a String
			fullOriginalNameBuf.append('.');
			fullOriginalNameBuf.append(this.originalColumnName);
			this.fullOriginalName = fullOriginalNameBuf.toString();
			fullOriginalNameBuf = null;
		}

		return this.fullOriginalName;
	}

	/**
	 * DOCUMENT ME!
	 *
	 * @return DOCUMENT ME!
	 */
	public long getLength() {
		return this.length;
	}

	public synchronized int getMaxBytesPerCharacter() throws SQLException {
		if (this.maxBytesPerChar == 0) {
			this.maxBytesPerChar = this.connection.getMaxBytesPerChar(getCharacterSet());
		}

		return this.maxBytesPerChar;
	}

	/**
	 * DOCUMENT ME!
	 *
	 * @return DOCUMENT ME!
	 */
	public int getMysqlType() {
		return this.mysqlType;
	}

	/**
	 * DOCUMENT ME!
	 *
	 * @return DOCUMENT ME!
	 */
	public String getName() throws SQLException {
		if (this.name == null) {
			this.name = getStringFromBytes(this.nameStart, this.nameLength);
		}

		return this.name;
	}

	public String getNameNoAliases() throws SQLException {
		if (this.useOldNameMetadata) {
			return getName();
		}

		if (this.connection != null &&
				this.connection.versionMeetsMinimum(4, 1, 0)) {
			return getOriginalName();
		}

		return getName();
	}

	/**
	 * DOCUMENT ME!
	 *
	 * @return DOCUMENT ME!
	 */
	public String getOriginalName() throws SQLException {
		if ((this.originalColumnName == null)
				&& (this.originalColumnNameStart != -1)
				&& (this.originalColumnNameLength != -1)) {
			this.originalColumnName = getStringFromBytes(
					this.originalColumnNameStart, this.originalColumnNameLength);
		}

		return this.originalColumnName;
	}

	/**
	 * DOCUMENT ME!
	 *
	 * @return DOCUMENT ME!
	 */
	public String getOriginalTableName() throws SQLException {
		if ((this.originalTableName == null)
				&& (this.originalTableNameStart != -1)
				&& (this.originalTableNameLength != -1)) {
			this.originalTableName = getStringFromBytes(
					this.originalTableNameStart, this.originalTableNameLength);
		}

		return this.originalTableName;
	}

	/**
	 * Returns amount of correction that should be applied to the precision
	 * value.
	 *
	 * Different versions of MySQL report different precision values.
	 *
	 * @return the amount to adjust precision value by.
	 */
	public int getPrecisionAdjustFactor() {
		return this.precisionAdjustFactor;
	}

	/**
	 * DOCUMENT ME!
	 *
	 * @return DOCUMENT ME!
	 */
	public int getSQLType() {
		return this.sqlType;
	}

	/**
	 * Create a string with the correct charset encoding from the byte-buffer
	 * that contains the data for this field
	 */
	private String getStringFromBytes(int stringStart, int stringLength)
			throws SQLException {
		if ((stringStart == -1) || (stringLength == -1)) {
			return null;
		}

		String stringVal = null;

		if (this.connection != null) {
			if (this.connection.getUseUnicode()) {
				String encoding = this.connection.getCharacterSetMetadata();

				if (encoding == null) {
					encoding = connection.getEncoding();
				}

				if (encoding != null) {
					SingleByteCharsetConverter converter = null;

					if (this.connection != null) {
						converter = this.connection
								.getCharsetConverter(encoding);
					}

					if (converter != null) { // we have a converter
						stringVal = converter.toString(this.buffer,
								stringStart, stringLength);
					} else {
						// we have no converter, use JVM converter
						byte[] stringBytes = new byte[stringLength];

						int endIndex = stringStart + stringLength;
						int pos = 0;

						for (int i = stringStart; i < endIndex; i++) {
							stringBytes[pos++] = this.buffer[i];
						}

						try {
							stringVal = new String(stringBytes, encoding);
						} catch (UnsupportedEncodingException ue) {
							throw new RuntimeException(Messages
									.getString("Field.12") + encoding //$NON-NLS-1$
									+ Messages.getString("Field.13")); //$NON-NLS-1$
						}
					}
				} else {
					// we have no encoding, use JVM standard charset
					stringVal = StringUtils.toAsciiString(this.buffer,
							stringStart, stringLength);
				}
			} else {
				// we are not using unicode, so use JVM standard charset
				stringVal = StringUtils.toAsciiString(this.buffer, stringStart,
						stringLength);
			}
		} else {
			// we don't have a connection, so punt
			stringVal = StringUtils.toAsciiString(this.buffer, stringStart,
					stringLength);
		}

		return stringVal;
	}

	/**
	 * DOCUMENT ME!
	 *
	 * @return DOCUMENT ME!
	 */
	public String getTable() throws SQLException {
		return getTableName();
	}

	/**
	 * DOCUMENT ME!
	 *
	 * @return DOCUMENT ME!
	 */
	public String getTableName() throws SQLException {
		if (this.tableName == null) {
			this.tableName = getStringFromBytes(this.tableNameStart,
					this.tableNameLength);
		}

		return this.tableName;
	}

	public String getTableNameNoAliases() throws SQLException {
		if (this.connection.versionMeetsMinimum(4, 1, 0)) {
			return getOriginalTableName();
		}

		return getTableName(); // pre-4.1, no aliases returned
	}

	/**
	 * DOCUMENT ME!
	 *
	 * @return DOCUMENT ME!
	 */
	public boolean isAutoIncrement() {
		return ((this.colFlag & AUTO_INCREMENT_FLAG) > 0);
	}

	/**
	 * DOCUMENT ME!
	 *
	 * @return DOCUMENT ME!
	 */
	public boolean isBinary() {
		return ((this.colFlag & 128) > 0);
	}

	/**
	 * DOCUMENT ME!
	 *
	 * @return DOCUMENT ME!
	 */
	public boolean isBlob() {
		return ((this.colFlag & 16) > 0);
	}

	/**
	 * Is this field owned by a server-created temporary table?
	 *
	 * @return
	 */
	private boolean isImplicitTemporaryTable() {
		return this.isImplicitTempTable;
	}

	/**
	 * DOCUMENT ME!
	 *
	 * @return DOCUMENT ME!
	 */
	public boolean isMultipleKey() {
		return ((this.colFlag & 8) > 0);
	}

	boolean isNotNull() {
		return ((this.colFlag & 1) > 0);
	}

	boolean isOpaqueBinary() throws SQLException {

		//
		// Detect CHAR(n) CHARACTER SET BINARY which is a synonym for
		// fixed-length binary types
		//

		if (this.charsetIndex == 63 && isBinary()
				&& (this.getMysqlType() == MysqlDefs.FIELD_TYPE_STRING ||
				this.getMysqlType() == MysqlDefs.FIELD_TYPE_VAR_STRING)) {

			if (this.originalTableNameLength == 0 && (
					this.connection != null && !this.connection.versionMeetsMinimum(5, 0, 25))) {
				return false; // Probably from function
			}

			// Okay, queries resolved by temp tables also have this 'signature',
			// check for that

			return !isImplicitTemporaryTable();
		}

		return (this.connection.versionMeetsMinimum(4, 1, 0) && "binary"
				.equalsIgnoreCase(getCharacterSet()));

	}

	/**
	 * DOCUMENT ME!
	 *
	 * @return DOCUMENT ME!
	 */
	public boolean isPrimaryKey() {
		return ((this.colFlag & 2) > 0);
	}

	/**
	 * Is this field _definitely_ not writable?
	 *
	 * @return true if this field can not be written to in an INSERT/UPDATE
	 *         statement.
	 */
	boolean isReadOnly() throws SQLException {
		if (this.connection.versionMeetsMinimum(4, 1, 0)) {
			String orgColumnName = getOriginalName();
			String orgTableName = getOriginalTableName();

			return !(orgColumnName != null && orgColumnName.length() > 0
					&& orgTableName != null && orgTableName.length() > 0);
		}

		return false; // we can't tell definitively in this case.
	}

	/**
	 * DOCUMENT ME!
	 *
	 * @return DOCUMENT ME!
	 */
	public boolean isUniqueKey() {
		return ((this.colFlag & 4) > 0);
	}

	/**
	 * DOCUMENT ME!
	 *
	 * @return DOCUMENT ME!
	 */
	public boolean isUnsigned() {
		return ((this.colFlag & 32) > 0);
	}

	public void setUnsigned() {
		this.colFlag |= 32;
	}
	
	/**
	 * DOCUMENT ME!
	 *
	 * @return DOCUMENT ME!
	 */
	public boolean isZeroFill() {
		return ((this.colFlag & 64) > 0);
	}

	//
	// MySQL only has one protocol-level BLOB type that it exposes
	// which is FIELD_TYPE_BLOB, although we can divine what the
	// actual type is by the length reported ...
	//
	private void setBlobTypeBasedOnLength() {
		if (this.length == MysqlDefs.LENGTH_TINYBLOB) {
			this.mysqlType = MysqlDefs.FIELD_TYPE_TINY_BLOB;
		} else if (this.length == MysqlDefs.LENGTH_BLOB) {
			this.mysqlType = MysqlDefs.FIELD_TYPE_BLOB;
		} else if (this.length == MysqlDefs.LENGTH_MEDIUMBLOB) {
			this.mysqlType = MysqlDefs.FIELD_TYPE_MEDIUM_BLOB;
		} else if (this.length == MysqlDefs.LENGTH_LONGBLOB) {
			this.mysqlType = MysqlDefs.FIELD_TYPE_LONG_BLOB;
		}
	}

	private boolean isNativeNumericType() {
		return ((this.mysqlType >= MysqlDefs.FIELD_TYPE_TINY &&
					this.mysqlType <= MysqlDefs.FIELD_TYPE_DOUBLE) ||
					this.mysqlType == MysqlDefs.FIELD_TYPE_LONGLONG ||
					this.mysqlType == MysqlDefs.FIELD_TYPE_YEAR);
	}

	private boolean isNativeDateTimeType() {
		return (this.mysqlType == MysqlDefs.FIELD_TYPE_DATE ||
				this.mysqlType == MysqlDefs.FIELD_TYPE_NEWDATE ||
				this.mysqlType == MysqlDefs.FIELD_TYPE_DATETIME ||
				this.mysqlType == MysqlDefs.FIELD_TYPE_TIME ||
				this.mysqlType == MysqlDefs.FIELD_TYPE_TIMESTAMP);
	}

	/**
	 * DOCUMENT ME!
	 *
	 * @param conn
	 *            DOCUMENT ME!
	 */
	public void setConnection(ConnectionImpl conn) {
		this.connection = conn;

		if (this.charsetName == null || this.charsetIndex == 0) {
			this.charsetName = this.connection.getEncoding();
		}
	}

	void setMysqlType(int type) {
		this.mysqlType = type;
		this.sqlType = MysqlDefs.mysqlToJavaType(this.mysqlType);
	}

	protected void setUseOldNameMetadata(boolean useOldNameMetadata) {
		this.useOldNameMetadata = useOldNameMetadata;
	}

	public String toString() {
		try {
			StringBuffer asString = new StringBuffer();
			asString.append(super.toString());
			asString.append("[");
			asString.append("catalog=");
			asString.append(this.getDatabaseName());
			asString.append(",tableName=");
			asString.append(this.getTableName());
			asString.append(",originalTableName=");
			asString.append(this.getOriginalTableName());
			asString.append(",columnName=");
			asString.append(this.getName());
			asString.append(",originalColumnName=");
			asString.append(this.getOriginalName());
			asString.append(",mysqlType=");
			asString.append(getMysqlType());
			asString.append("(");
			asString.append(MysqlDefs.typeToName(getMysqlType()));
			asString.append(")");
			asString.append(",flags=");
			
			if (isAutoIncrement()) {
				asString.append(" AUTO_INCREMENT");
			}
			
			if (isPrimaryKey()) {
				asString.append(" PRIMARY_KEY");
			}
			
			if (isUniqueKey()) {
				asString.append(" UNIQUE_KEY");
			}
			
			if (isBinary()) {
				asString.append(" BINARY");
			}
			
			if (isBlob()) {
				asString.append(" BLOB");
			}
			
			if (isMultipleKey()) {
				asString.append(" MULTI_KEY");
			}
			
			if (isUnsigned()) {
				asString.append(" UNSIGNED");
			}
			
			if (isZeroFill()) {
				asString.append(" ZEROFILL");
			}

			asString.append(", charsetIndex=");
			asString.append(this.charsetIndex);
			asString.append(", charsetName=");
			asString.append(this.charsetName);
			
			
			//if (this.buffer != null) {
			//	asString.append("\n\nData as received from server:\n\n");
			//	asString.append(StringUtils.dumpAsHex(this.buffer,
			//			this.buffer.length));
			//}

			asString.append("]");
			
			return asString.toString();
		} catch (Throwable t) {
			return super.toString();
		}
	}

	protected boolean isSingleBit() {
		return this.isSingleBit;
	}
}