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
|
// Copyright (c) 2004-2008 MySQL AB, 2008-2009 Sun Microsystems, Inc.
//
// MySQL Connector/NET is licensed under the terms of the GPLv2
// <http://www.gnu.org/licenses/old-licenses/gpl-2.0.html>, like most
// MySQL Connectors. There are special exceptions to the terms and
// conditions of the GPLv2 as it is applied to this software, see the
// FLOSS License Exception
// <http://www.mysql.com/about/legal/licensing/foss-exception.html>.
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published
// by the Free Software Foundation; version 2 of the License.
//
// 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.,
// 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
using System.Text;
using MySql.Data.Common;
using MySql.Data.Types;
using System.Globalization;
using System.Text.RegularExpressions;
using System;
using System.Collections;
using System.Collections.Generic;
namespace MySql.Data.MySqlClient
{
internal enum ColumnFlags : int
{
NOT_NULL = 1,
PRIMARY_KEY = 2,
UNIQUE_KEY = 4,
MULTIPLE_KEY = 8,
BLOB = 16,
UNSIGNED = 32,
ZERO_FILL = 64,
BINARY = 128,
ENUM = 256,
AUTO_INCREMENT = 512,
TIMESTAMP = 1024,
SET = 2048,
NUMBER = 32768
} ;
/// <summary>
/// Summary description for Field.
/// </summary>
internal class MySqlField
{
#region Fields
// public fields
public string CatalogName;
public int ColumnLength;
public string ColumnName;
public string OriginalColumnName;
public string TableName;
public string RealTableName;
public string DatabaseName;
public Encoding Encoding;
public int maxLength;
// protected fields
protected ColumnFlags colFlags;
protected int charSetIndex;
protected byte precision;
protected byte scale;
protected MySqlDbType mySqlDbType;
protected DBVersion connVersion;
protected Driver driver;
protected bool binaryOk;
protected List<Type> typeConversions = new List<Type>();
#endregion
public MySqlField(Driver driver)
{
this.driver = driver;
connVersion = driver.Version;
maxLength = 1;
binaryOk = true;
}
#region Properties
public int CharacterSetIndex
{
get { return charSetIndex; }
set { charSetIndex = value; SetFieldEncoding(); }
}
public MySqlDbType Type
{
get { return mySqlDbType; }
}
public byte Precision
{
get { return precision; }
set { precision = value; }
}
public byte Scale
{
get { return scale; }
set { scale = value; }
}
public int MaxLength
{
get { return maxLength; }
set { maxLength = value; }
}
public ColumnFlags Flags
{
get { return colFlags; }
}
public bool IsAutoIncrement
{
get { return (colFlags & ColumnFlags.AUTO_INCREMENT) > 0; }
}
public bool IsNumeric
{
get { return (colFlags & ColumnFlags.NUMBER) > 0; }
}
public bool AllowsNull
{
get { return (colFlags & ColumnFlags.NOT_NULL) == 0; }
}
public bool IsUnique
{
get { return (colFlags & ColumnFlags.UNIQUE_KEY) > 0; }
}
public bool IsPrimaryKey
{
get { return (colFlags & ColumnFlags.PRIMARY_KEY) > 0; }
}
public bool IsBlob
{
get
{
return (mySqlDbType >= MySqlDbType.TinyBlob &&
mySqlDbType <= MySqlDbType.Blob) ||
(mySqlDbType >= MySqlDbType.TinyText &&
mySqlDbType <= MySqlDbType.Text) ||
(colFlags & ColumnFlags.BLOB) > 0;
}
}
public bool IsBinary
{
get
{
return binaryOk && (CharacterSetIndex == 63);
}
}
public bool IsUnsigned
{
get { return (colFlags & ColumnFlags.UNSIGNED) > 0; }
}
public bool IsTextField
{
get
{
return Type == MySqlDbType.VarString || Type == MySqlDbType.VarChar ||
(IsBlob && !IsBinary);
}
}
public int CharacterLength
{
get { return ColumnLength / MaxLength; }
}
public List<Type> TypeConversions
{
get { return typeConversions; }
}
#endregion
public void SetTypeAndFlags(MySqlDbType type, ColumnFlags flags)
{
colFlags = flags;
mySqlDbType = type;
if (String.IsNullOrEmpty(TableName) && String.IsNullOrEmpty(RealTableName) &&
IsBinary && driver.Settings.FunctionsReturnString)
{
CharacterSetIndex = driver.ConnectionCharSetIndex;
}
// if our type is an unsigned number, then we need
// to bump it up into our unsigned types
// we're trusting that the server is not going to set the UNSIGNED
// flag unless we are a number
if (IsUnsigned)
{
switch (type)
{
case MySqlDbType.Byte:
mySqlDbType = MySqlDbType.UByte;
return;
case MySqlDbType.Int16:
mySqlDbType = MySqlDbType.UInt16;
return;
case MySqlDbType.Int24:
mySqlDbType = MySqlDbType.UInt24;
return;
case MySqlDbType.Int32:
mySqlDbType = MySqlDbType.UInt32;
return;
case MySqlDbType.Int64:
mySqlDbType = MySqlDbType.UInt64;
return;
}
}
if (IsBlob)
{
// handle blob to UTF8 conversion if requested. This is only activated
// on binary blobs
if (IsBinary && driver.Settings.TreatBlobsAsUTF8)
{
bool convertBlob = false;
Regex includeRegex = driver.Settings.GetBlobAsUTF8IncludeRegex();
Regex excludeRegex = driver.Settings.GetBlobAsUTF8ExcludeRegex();
if (includeRegex != null && includeRegex.IsMatch(ColumnName))
convertBlob = true;
else if (includeRegex == null && excludeRegex != null &&
!excludeRegex.IsMatch(ColumnName))
convertBlob = true;
if (convertBlob)
{
binaryOk = false;
Encoding = System.Text.Encoding.GetEncoding("UTF-8");
charSetIndex = -1; // lets driver know we are in charge of encoding
maxLength = 4;
}
}
if (!IsBinary)
{
if (type == MySqlDbType.TinyBlob)
mySqlDbType = MySqlDbType.TinyText;
else if (type == MySqlDbType.MediumBlob)
mySqlDbType = MySqlDbType.MediumText;
else if (type == MySqlDbType.Blob)
mySqlDbType = MySqlDbType.Text;
else if (type == MySqlDbType.LongBlob)
mySqlDbType = MySqlDbType.LongText;
}
}
// now determine if we really should be binary
if (driver.Settings.RespectBinaryFlags)
CheckForExceptions();
if (Type == MySqlDbType.String && CharacterLength == 36 && !driver.Settings.OldGuids)
mySqlDbType = MySqlDbType.Guid;
if (!IsBinary) return;
if (driver.Settings.RespectBinaryFlags)
{
if (type == MySqlDbType.String)
mySqlDbType = MySqlDbType.Binary;
else if (type == MySqlDbType.VarChar ||
type == MySqlDbType.VarString)
mySqlDbType = MySqlDbType.VarBinary;
}
if (CharacterSetIndex == 63)
CharacterSetIndex = driver.ConnectionCharSetIndex;
if (Type == MySqlDbType.Binary && ColumnLength == 16 && driver.Settings.OldGuids)
mySqlDbType = MySqlDbType.Guid;
}
public void AddTypeConversion(Type t)
{
if (TypeConversions.Contains(t)) return;
TypeConversions.Add(t);
}
private void CheckForExceptions()
{
string colName = String.Empty;
if (OriginalColumnName != null)
colName = OriginalColumnName.ToUpper(CultureInfo.InvariantCulture);
if (colName.StartsWith("CHAR("))
binaryOk = false;
}
public IMySqlValue GetValueObject()
{
IMySqlValue v = GetIMySqlValue(Type);
if (v is MySqlByte && ColumnLength == 1 && driver.Settings.TreatTinyAsBoolean)
{
MySqlByte b = (MySqlByte)v;
b.TreatAsBoolean = true;
v = b;
}
else if (v is MySqlGuid)
{
MySqlGuid g = (MySqlGuid)v;
g.OldGuids = driver.Settings.OldGuids;
v = g;
}
return v;
}
public static IMySqlValue GetIMySqlValue(MySqlDbType type)
{
switch (type)
{
case MySqlDbType.Byte:
return new MySqlByte();
case MySqlDbType.UByte:
return new MySqlUByte();
case MySqlDbType.Int16:
return new MySqlInt16();
case MySqlDbType.UInt16:
return new MySqlUInt16();
case MySqlDbType.Int24:
case MySqlDbType.Int32:
case MySqlDbType.Year:
return new MySqlInt32(type, true);
case MySqlDbType.UInt24:
case MySqlDbType.UInt32:
return new MySqlUInt32(type, true);
case MySqlDbType.Bit:
return new MySqlBit();
case MySqlDbType.Int64:
return new MySqlInt64();
case MySqlDbType.UInt64:
return new MySqlUInt64();
case MySqlDbType.Time:
return new MySqlTimeSpan();
case MySqlDbType.Date:
case MySqlDbType.DateTime:
case MySqlDbType.Newdate:
case MySqlDbType.Timestamp:
return new MySqlDateTime(type, true);
case MySqlDbType.Decimal:
case MySqlDbType.NewDecimal:
return new MySqlDecimal();
case MySqlDbType.Float:
return new MySqlSingle();
case MySqlDbType.Double:
return new MySqlDouble();
case MySqlDbType.Set:
case MySqlDbType.Enum:
case MySqlDbType.String:
case MySqlDbType.VarString:
case MySqlDbType.VarChar:
case MySqlDbType.Text:
case MySqlDbType.TinyText:
case MySqlDbType.MediumText:
case MySqlDbType.LongText:
case (MySqlDbType) Field_Type.NULL:
return new MySqlString(type, true);
case MySqlDbType.Geometry:
case MySqlDbType.Blob:
case MySqlDbType.MediumBlob:
case MySqlDbType.LongBlob:
case MySqlDbType.TinyBlob:
case MySqlDbType.Binary:
case MySqlDbType.VarBinary:
return new MySqlBinary(type, true);
case MySqlDbType.Guid:
return new MySqlGuid();
default:
throw new MySqlException("Unknown data type");
}
}
private void SetFieldEncoding()
{
Hashtable charSets = driver.CharacterSets;
DBVersion version = driver.Version;
if (charSets == null || CharacterSetIndex == -1) return;
if (charSets[CharacterSetIndex] == null) return;
CharacterSet cs = CharSetMap.GetCharacterSet(version, (string)charSets[CharacterSetIndex]);
// starting with 6.0.4 utf8 has a maxlen of 4 instead of 3. The old
// 3 byte utf8 is utf8mb3
if (cs.name.ToLower(System.Globalization.CultureInfo.InvariantCulture) == "utf-8" &&
version.Major >= 6)
MaxLength = 4;
else
MaxLength = cs.byteCount;
Encoding = CharSetMap.GetEncoding(version, (string)charSets[CharacterSetIndex]);
}
}
}
|