Mercurial > pub > bltoolkit
comparison Source/Data/Sql/SqlDataType.cs @ 0:f990fcb411a9
Копия текущей версии из github
| author | cin |
|---|---|
| date | Thu, 27 Mar 2014 21:46:09 +0400 |
| parents | |
| children |
comparison
equal
deleted
inserted
replaced
| -1:000000000000 | 0:f990fcb411a9 |
|---|---|
| 1 using System; | |
| 2 using System.Collections.Generic; | |
| 3 using System.Data; | |
| 4 using System.Data.SqlTypes; | |
| 5 using System.Linq; | |
| 6 using System.Text; | |
| 7 | |
| 8 namespace BLToolkit.Data.Sql | |
| 9 { | |
| 10 using Reflection; | |
| 11 | |
| 12 public class SqlDataType : ISqlExpression | |
| 13 { | |
| 14 #region Init | |
| 15 | |
| 16 public SqlDataType(SqlDbType dbType) | |
| 17 { | |
| 18 var defaultType = GetDataType(dbType); | |
| 19 | |
| 20 SqlDbType = dbType; | |
| 21 Type = defaultType.Type; | |
| 22 Length = defaultType.Length; | |
| 23 Precision = defaultType.Precision; | |
| 24 Scale = defaultType.Scale; | |
| 25 } | |
| 26 | |
| 27 public SqlDataType(SqlDbType dbType, int length) | |
| 28 { | |
| 29 if (length <= 0) throw new ArgumentOutOfRangeException("length"); | |
| 30 | |
| 31 SqlDbType = dbType; | |
| 32 Type = GetDataType(dbType).Type; | |
| 33 Length = length; | |
| 34 } | |
| 35 | |
| 36 public SqlDataType(SqlDbType dbType, int precision, int scale) | |
| 37 { | |
| 38 if (precision <= 0) throw new ArgumentOutOfRangeException("precision"); | |
| 39 if (scale < 0) throw new ArgumentOutOfRangeException("scale"); | |
| 40 | |
| 41 SqlDbType = dbType; | |
| 42 Type = GetDataType(dbType).Type; | |
| 43 Precision = precision; | |
| 44 Scale = scale; | |
| 45 } | |
| 46 | |
| 47 public SqlDataType([JetBrains.Annotations.NotNull]Type type) | |
| 48 { | |
| 49 if (type == null) throw new ArgumentNullException("type"); | |
| 50 | |
| 51 var defaultType = GetDataType(type); | |
| 52 | |
| 53 SqlDbType = defaultType.SqlDbType; | |
| 54 Type = type; | |
| 55 Length = defaultType.Length; | |
| 56 Precision = defaultType.Precision; | |
| 57 Scale = defaultType.Scale; | |
| 58 } | |
| 59 | |
| 60 public SqlDataType([JetBrains.Annotations.NotNull] Type type, int length) | |
| 61 { | |
| 62 if (type == null) throw new ArgumentNullException ("type"); | |
| 63 if (length <= 0) throw new ArgumentOutOfRangeException("length"); | |
| 64 | |
| 65 SqlDbType = GetDataType(type).SqlDbType; | |
| 66 Type = type; | |
| 67 Length = length; | |
| 68 } | |
| 69 | |
| 70 public SqlDataType([JetBrains.Annotations.NotNull] Type type, int precision, int scale) | |
| 71 { | |
| 72 if (type == null) throw new ArgumentNullException ("type"); | |
| 73 if (precision <= 0) throw new ArgumentOutOfRangeException("precision"); | |
| 74 if (scale < 0) throw new ArgumentOutOfRangeException("scale"); | |
| 75 | |
| 76 SqlDbType = GetDataType(type).SqlDbType; | |
| 77 Type = type; | |
| 78 Precision = precision; | |
| 79 Scale = scale; | |
| 80 } | |
| 81 | |
| 82 public SqlDataType(SqlDbType dbType, [JetBrains.Annotations.NotNull]Type type) | |
| 83 { | |
| 84 if (type == null) throw new ArgumentNullException("type"); | |
| 85 | |
| 86 var defaultType = GetDataType(dbType); | |
| 87 | |
| 88 SqlDbType = dbType; | |
| 89 Type = type; | |
| 90 Length = defaultType.Length; | |
| 91 Precision = defaultType.Precision; | |
| 92 Scale = defaultType.Scale; | |
| 93 } | |
| 94 | |
| 95 public SqlDataType(SqlDbType dbType, [JetBrains.Annotations.NotNull] Type type, int length) | |
| 96 { | |
| 97 if (type == null) throw new ArgumentNullException ("type"); | |
| 98 if (length <= 0) throw new ArgumentOutOfRangeException("length"); | |
| 99 | |
| 100 SqlDbType = dbType; | |
| 101 Type = type; | |
| 102 Length = length; | |
| 103 } | |
| 104 | |
| 105 public SqlDataType(SqlDbType dbType, [JetBrains.Annotations.NotNull] Type type, int precision, int scale) | |
| 106 { | |
| 107 if (type == null) throw new ArgumentNullException ("type"); | |
| 108 if (precision <= 0) throw new ArgumentOutOfRangeException("precision"); | |
| 109 if (scale < 0) throw new ArgumentOutOfRangeException("scale"); | |
| 110 | |
| 111 SqlDbType = dbType; | |
| 112 Type = type; | |
| 113 Precision = precision; | |
| 114 Scale = scale; | |
| 115 } | |
| 116 | |
| 117 #endregion | |
| 118 | |
| 119 #region Public Members | |
| 120 | |
| 121 public SqlDbType SqlDbType { get; private set; } | |
| 122 public Type Type { get; private set; } | |
| 123 public int Length { get; private set; } | |
| 124 public int Precision { get; private set; } | |
| 125 public int Scale { get; private set; } | |
| 126 | |
| 127 #endregion | |
| 128 | |
| 129 #region Static Members | |
| 130 | |
| 131 struct TypeInfo | |
| 132 { | |
| 133 public TypeInfo(SqlDbType dbType, int maxLength, int maxPrecision, int maxScale, int maxDisplaySize) | |
| 134 { | |
| 135 SqlDbType = dbType; | |
| 136 MaxLength = maxLength; | |
| 137 MaxPrecision = maxPrecision; | |
| 138 MaxScale = maxScale; | |
| 139 MaxDisplaySize = maxDisplaySize; | |
| 140 } | |
| 141 | |
| 142 public readonly SqlDbType SqlDbType; | |
| 143 public readonly int MaxLength; | |
| 144 public readonly int MaxPrecision; | |
| 145 public readonly int MaxScale; | |
| 146 public readonly int MaxDisplaySize; | |
| 147 } | |
| 148 | |
| 149 static TypeInfo[] SortTypeInfo(params TypeInfo[] info) | |
| 150 { | |
| 151 var sortedInfo = new TypeInfo[info.Max(ti => (int)ti.SqlDbType) + 1]; | |
| 152 | |
| 153 foreach (var typeInfo in info) | |
| 154 sortedInfo[(int)typeInfo.SqlDbType] = typeInfo; | |
| 155 | |
| 156 return sortedInfo; | |
| 157 } | |
| 158 | |
| 159 static int Len(object obj) | |
| 160 { | |
| 161 return obj.ToString().Length; | |
| 162 } | |
| 163 | |
| 164 static readonly TypeInfo[] _typeInfo = SortTypeInfo | |
| 165 ( | |
| 166 // DbType MaxLength MaxPrecision MaxScale MaxDisplaySize | |
| 167 // | |
| 168 new TypeInfo(SqlDbType.BigInt, 8, Len( long.MaxValue), 0, Len( long.MinValue)), | |
| 169 new TypeInfo(SqlDbType.Int, 4, Len( int.MaxValue), 0, Len( int.MinValue)), | |
| 170 new TypeInfo(SqlDbType.SmallInt, 2, Len(short.MaxValue), 0, Len(short.MinValue)), | |
| 171 new TypeInfo(SqlDbType.TinyInt, 1, Len( byte.MaxValue), 0, Len( byte.MaxValue)), | |
| 172 new TypeInfo(SqlDbType.Bit, 1, 1, 0, 1), | |
| 173 | |
| 174 new TypeInfo(SqlDbType.Decimal, 17, Len(decimal.MaxValue), Len(decimal.MaxValue), Len(decimal.MinValue)+1), | |
| 175 new TypeInfo(SqlDbType.Money, 8, 19, 4, 19 + 2), | |
| 176 new TypeInfo(SqlDbType.SmallMoney, 4, 10, 4, 10 + 2), | |
| 177 new TypeInfo(SqlDbType.Float, 8, 15, 15, 15 + 2 + 5), | |
| 178 new TypeInfo(SqlDbType.Real, 4, 7, 7, 7 + 2 + 4), | |
| 179 | |
| 180 new TypeInfo(SqlDbType.DateTime, 8, -1, -1, 23), | |
| 181 #if !MONO | |
| 182 new TypeInfo(SqlDbType.DateTime2, 8, -1, -1, 27), | |
| 183 #endif | |
| 184 new TypeInfo(SqlDbType.SmallDateTime, 4, -1, -1, 19), | |
| 185 new TypeInfo(SqlDbType.Date, 3, -1, -1, 10), | |
| 186 new TypeInfo(SqlDbType.Time, 5, -1, -1, 16), | |
| 187 #if !MONO | |
| 188 new TypeInfo(SqlDbType.DateTimeOffset, 10, -1, -1, 34), | |
| 189 #endif | |
| 190 | |
| 191 new TypeInfo(SqlDbType.Char, 8000, -1, -1, 8000), | |
| 192 new TypeInfo(SqlDbType.VarChar, 8000, -1, -1, 8000), | |
| 193 new TypeInfo(SqlDbType.Text, int.MaxValue, -1, -1, int.MaxValue), | |
| 194 new TypeInfo(SqlDbType.NChar, 4000, -1, -1, 4000), | |
| 195 new TypeInfo(SqlDbType.NVarChar, 4000, -1, -1, 4000), | |
| 196 new TypeInfo(SqlDbType.NText, int.MaxValue, -1, -1, int.MaxValue / 2), | |
| 197 | |
| 198 new TypeInfo(SqlDbType.Binary, 8000, -1, -1, -1), | |
| 199 new TypeInfo(SqlDbType.VarBinary, 8000, -1, -1, -1), | |
| 200 new TypeInfo(SqlDbType.Image, int.MaxValue, -1, -1, -1), | |
| 201 | |
| 202 new TypeInfo(SqlDbType.Timestamp, 8, -1, -1, -1), | |
| 203 new TypeInfo(SqlDbType.UniqueIdentifier, 16, -1, -1, 36), | |
| 204 | |
| 205 new TypeInfo(SqlDbType.Variant, -1, -1, -1, -1), | |
| 206 new TypeInfo(SqlDbType.Xml, -1, -1, -1, -1), | |
| 207 #if !MONO | |
| 208 new TypeInfo(SqlDbType.Structured, -1, -1, -1, -1), | |
| 209 #endif | |
| 210 new TypeInfo(SqlDbType.Udt, -1, -1, -1, -1) | |
| 211 ); | |
| 212 | |
| 213 public static int GetMaxLength (SqlDbType dbType) { return _typeInfo[(int)dbType].MaxLength; } | |
| 214 public static int GetMaxPrecision (SqlDbType dbType) { return _typeInfo[(int)dbType].MaxPrecision; } | |
| 215 public static int GetMaxScale (SqlDbType dbType) { return _typeInfo[(int)dbType].MaxScale; } | |
| 216 public static int GetMaxDisplaySize(SqlDbType dbType) { return _typeInfo[(int)dbType].MaxDisplaySize; } | |
| 217 | |
| 218 public static SqlDataType GetDataType(Type type) | |
| 219 { | |
| 220 var underlyingType = type; | |
| 221 | |
| 222 if (underlyingType.IsGenericType && underlyingType.GetGenericTypeDefinition() == typeof(Nullable<>)) | |
| 223 underlyingType = underlyingType.GetGenericArguments()[0]; | |
| 224 | |
| 225 if (underlyingType.IsEnum) | |
| 226 underlyingType = Enum.GetUnderlyingType(underlyingType); | |
| 227 | |
| 228 switch (Type.GetTypeCode(underlyingType)) | |
| 229 { | |
| 230 case TypeCode.Boolean : return Boolean; | |
| 231 case TypeCode.Char : return Char; | |
| 232 case TypeCode.SByte : return SByte; | |
| 233 case TypeCode.Byte : return Byte; | |
| 234 case TypeCode.Int16 : return Int16; | |
| 235 case TypeCode.UInt16 : return UInt16; | |
| 236 case TypeCode.Int32 : return Int32; | |
| 237 case TypeCode.UInt32 : return UInt32; | |
| 238 case TypeCode.Int64 : return Int64; | |
| 239 case TypeCode.UInt64 : return UInt64; | |
| 240 case TypeCode.Single : return Single; | |
| 241 case TypeCode.Double : return Double; | |
| 242 case TypeCode.Decimal : return Decimal; | |
| 243 case TypeCode.DateTime : return DateTime; | |
| 244 case TypeCode.String : return String; | |
| 245 case TypeCode.Object : | |
| 246 if (underlyingType == typeof(Guid)) return Guid; | |
| 247 if (underlyingType == typeof(byte[])) return ByteArray; | |
| 248 if (underlyingType == typeof(System.Data.Linq.Binary)) return LinqBinary; | |
| 249 if (underlyingType == typeof(char[])) return CharArray; | |
| 250 #if !MONO | |
| 251 if (underlyingType == typeof(DateTimeOffset)) return DateTimeOffset; | |
| 252 #endif | |
| 253 if (underlyingType == typeof(TimeSpan)) return TimeSpan; | |
| 254 break; | |
| 255 | |
| 256 case TypeCode.DBNull : | |
| 257 case TypeCode.Empty : | |
| 258 default : break; | |
| 259 } | |
| 260 | |
| 261 #if !SILVERLIGHT | |
| 262 | |
| 263 if (underlyingType == typeof(SqlByte)) return SqlByte; | |
| 264 if (underlyingType == typeof(SqlInt16)) return SqlInt16; | |
| 265 if (underlyingType == typeof(SqlInt32)) return SqlInt32; | |
| 266 if (underlyingType == typeof(SqlInt64)) return SqlInt64; | |
| 267 if (underlyingType == typeof(SqlSingle)) return SqlSingle; | |
| 268 if (underlyingType == typeof(SqlBoolean)) return SqlBoolean; | |
| 269 if (underlyingType == typeof(SqlDouble)) return SqlDouble; | |
| 270 if (underlyingType == typeof(SqlDateTime)) return SqlDateTime; | |
| 271 if (underlyingType == typeof(SqlDecimal)) return SqlDecimal; | |
| 272 if (underlyingType == typeof(SqlMoney)) return SqlMoney; | |
| 273 if (underlyingType == typeof(SqlString)) return SqlString; | |
| 274 if (underlyingType == typeof(SqlBinary)) return SqlBinary; | |
| 275 if (underlyingType == typeof(SqlGuid)) return SqlGuid; | |
| 276 if (underlyingType == typeof(SqlBytes)) return SqlBytes; | |
| 277 if (underlyingType == typeof(SqlChars)) return SqlChars; | |
| 278 if (underlyingType == typeof(SqlXml)) return SqlXml; | |
| 279 | |
| 280 #endif | |
| 281 | |
| 282 return DbVariant; | |
| 283 } | |
| 284 | |
| 285 public static SqlDataType GetDataType(SqlDbType type) | |
| 286 { | |
| 287 switch (type) | |
| 288 { | |
| 289 case SqlDbType.BigInt : return DbBigInt; | |
| 290 case SqlDbType.Binary : return DbBinary; | |
| 291 case SqlDbType.Bit : return DbBit; | |
| 292 case SqlDbType.Char : return DbChar; | |
| 293 case SqlDbType.DateTime : return DbDateTime; | |
| 294 case SqlDbType.Decimal : return DbDecimal; | |
| 295 case SqlDbType.Float : return DbFloat; | |
| 296 case SqlDbType.Image : return DbImage; | |
| 297 case SqlDbType.Int : return DbInt; | |
| 298 case SqlDbType.Money : return DbMoney; | |
| 299 case SqlDbType.NChar : return DbNChar; | |
| 300 case SqlDbType.NText : return DbNText; | |
| 301 case SqlDbType.NVarChar : return DbNVarChar; | |
| 302 case SqlDbType.Real : return DbReal; | |
| 303 case SqlDbType.UniqueIdentifier : return DbUniqueIdentifier; | |
| 304 case SqlDbType.SmallDateTime : return DbSmallDateTime; | |
| 305 case SqlDbType.SmallInt : return DbSmallInt; | |
| 306 case SqlDbType.SmallMoney : return DbSmallMoney; | |
| 307 case SqlDbType.Text : return DbText; | |
| 308 case SqlDbType.Timestamp : return DbTimestamp; | |
| 309 case SqlDbType.TinyInt : return DbTinyInt; | |
| 310 case SqlDbType.VarBinary : return DbVarBinary; | |
| 311 case SqlDbType.VarChar : return DbVarChar; | |
| 312 case SqlDbType.Variant : return DbVariant; | |
| 313 #if !SILVERLIGHT | |
| 314 case SqlDbType.Xml : return DbXml; | |
| 315 #endif | |
| 316 case SqlDbType.Udt : return DbUdt; | |
| 317 #if !MONO | |
| 318 case SqlDbType.Structured : return DbStructured; | |
| 319 #endif | |
| 320 case SqlDbType.Date : return DbDate; | |
| 321 case SqlDbType.Time : return DbTime; | |
| 322 #if !MONO | |
| 323 case SqlDbType.DateTime2 : return DbDateTime2; | |
| 324 case SqlDbType.DateTimeOffset : return DbDateTimeOffset; | |
| 325 #endif | |
| 326 } | |
| 327 | |
| 328 throw new InvalidOperationException(); | |
| 329 } | |
| 330 | |
| 331 public static bool CanBeNull(Type type) | |
| 332 { | |
| 333 if (type.IsValueType == false || | |
| 334 type.IsGenericType && type.GetGenericTypeDefinition() == typeof(Nullable<>) || | |
| 335 TypeHelper.IsSameOrParent(typeof(INullable), type)) | |
| 336 return true; | |
| 337 | |
| 338 return false; | |
| 339 } | |
| 340 | |
| 341 #endregion | |
| 342 | |
| 343 #region Default Types | |
| 344 | |
| 345 internal SqlDataType(SqlDbType dbType, Type type, int length, int precision, int scale) | |
| 346 { | |
| 347 SqlDbType = dbType; | |
| 348 Type = type; | |
| 349 Length = length; | |
| 350 Precision = precision; | |
| 351 Scale = scale; | |
| 352 } | |
| 353 | |
| 354 SqlDataType(SqlDbType dbType, Type type, Func<SqlDbType,int> length, int precision, int scale) | |
| 355 : this(dbType, type, length(dbType), precision, scale) | |
| 356 { | |
| 357 } | |
| 358 | |
| 359 SqlDataType(SqlDbType dbType, Type type, int length, Func<SqlDbType,int> precision, int scale) | |
| 360 : this(dbType, type, length, precision(dbType), scale) | |
| 361 { | |
| 362 } | |
| 363 | |
| 364 public static readonly SqlDataType DbBigInt = new SqlDataType(SqlDbType.BigInt, typeof(Int64), 0, 0, 0); | |
| 365 public static readonly SqlDataType DbInt = new SqlDataType(SqlDbType.Int, typeof(Int32), 0, 0, 0); | |
| 366 public static readonly SqlDataType DbSmallInt = new SqlDataType(SqlDbType.SmallInt, typeof(Int16), 0, 0, 0); | |
| 367 public static readonly SqlDataType DbTinyInt = new SqlDataType(SqlDbType.TinyInt, typeof(Byte), 0, 0, 0); | |
| 368 public static readonly SqlDataType DbBit = new SqlDataType(SqlDbType.Bit, typeof(Boolean), 0, 0, 0); | |
| 369 | |
| 370 public static readonly SqlDataType DbDecimal = new SqlDataType(SqlDbType.Decimal, typeof(Decimal), 0, GetMaxPrecision, 10); | |
| 371 public static readonly SqlDataType DbMoney = new SqlDataType(SqlDbType.Money, typeof(Decimal), 0, GetMaxPrecision, 4); | |
| 372 public static readonly SqlDataType DbSmallMoney = new SqlDataType(SqlDbType.SmallMoney, typeof(Decimal), 0, GetMaxPrecision, 4); | |
| 373 public static readonly SqlDataType DbFloat = new SqlDataType(SqlDbType.Float, typeof(Double), 0, 0, 0); | |
| 374 public static readonly SqlDataType DbReal = new SqlDataType(SqlDbType.Real, typeof(Single), 0, 0, 0); | |
| 375 | |
| 376 public static readonly SqlDataType DbDateTime = new SqlDataType(SqlDbType.DateTime, typeof(DateTime), 0, 0, 0); | |
| 377 #if !MONO | |
| 378 public static readonly SqlDataType DbDateTime2 = new SqlDataType(SqlDbType.DateTime2, typeof(DateTime), 0, 0, 0); | |
| 379 #else | |
| 380 public static readonly SqlDataType DbDateTime2 = new SqlDataType(SqlDbType.DateTime, typeof(DateTime), 0, 0, 0); | |
| 381 #endif | |
| 382 public static readonly SqlDataType DbSmallDateTime = new SqlDataType(SqlDbType.SmallDateTime, typeof(DateTime), 0, 0, 0); | |
| 383 public static readonly SqlDataType DbDate = new SqlDataType(SqlDbType.Date, typeof(DateTime), 0, 0, 0); | |
| 384 public static readonly SqlDataType DbTime = new SqlDataType(SqlDbType.Time, typeof(TimeSpan), 0, 0, 0); | |
| 385 #if !MONO | |
| 386 public static readonly SqlDataType DbDateTimeOffset = new SqlDataType(SqlDbType.DateTimeOffset, typeof(DateTimeOffset), 0, 0, 0); | |
| 387 #endif | |
| 388 | |
| 389 public static readonly SqlDataType DbChar = new SqlDataType(SqlDbType.Char, typeof(String), GetMaxLength, 0, 0); | |
| 390 public static readonly SqlDataType DbVarChar = new SqlDataType(SqlDbType.VarChar, typeof(String), GetMaxLength, 0, 0); | |
| 391 public static readonly SqlDataType DbText = new SqlDataType(SqlDbType.Text, typeof(String), GetMaxLength, 0, 0); | |
| 392 public static readonly SqlDataType DbNChar = new SqlDataType(SqlDbType.NChar, typeof(String), GetMaxLength, 0, 0); | |
| 393 public static readonly SqlDataType DbNVarChar = new SqlDataType(SqlDbType.NVarChar, typeof(String), GetMaxLength, 0, 0); | |
| 394 public static readonly SqlDataType DbNText = new SqlDataType(SqlDbType.NText, typeof(String), GetMaxLength, 0, 0); | |
| 395 | |
| 396 public static readonly SqlDataType DbBinary = new SqlDataType(SqlDbType.Binary, typeof(Byte[]), GetMaxLength, 0, 0); | |
| 397 public static readonly SqlDataType DbVarBinary = new SqlDataType(SqlDbType.VarBinary, typeof(Byte[]), GetMaxLength, 0, 0); | |
| 398 public static readonly SqlDataType DbImage = new SqlDataType(SqlDbType.Image, typeof(Byte[]), GetMaxLength, 0, 0); | |
| 399 | |
| 400 public static readonly SqlDataType DbTimestamp = new SqlDataType(SqlDbType.Timestamp, typeof(Byte[]), 0, 0, 0); | |
| 401 public static readonly SqlDataType DbUniqueIdentifier = new SqlDataType(SqlDbType.UniqueIdentifier, typeof(Guid), 0, 0, 0); | |
| 402 | |
| 403 public static readonly SqlDataType DbVariant = new SqlDataType(SqlDbType.Variant, typeof(Object), 0, 0, 0); | |
| 404 #if !SILVERLIGHT | |
| 405 public static readonly SqlDataType DbXml = new SqlDataType(SqlDbType.Xml, typeof(SqlXml), 0, 0, 0); | |
| 406 #endif | |
| 407 public static readonly SqlDataType DbUdt = new SqlDataType(SqlDbType.Udt, typeof(Object), 0, 0, 0); | |
| 408 #if !MONO | |
| 409 public static readonly SqlDataType DbStructured = new SqlDataType(SqlDbType.Structured, typeof(Object), 0, 0, 0); | |
| 410 #endif | |
| 411 | |
| 412 public static readonly SqlDataType Boolean = DbBit; | |
| 413 public static readonly SqlDataType Char = new SqlDataType(SqlDbType.Char, typeof(Char), 1, 0, 0); | |
| 414 public static readonly SqlDataType SByte = new SqlDataType(SqlDbType.SmallInt, typeof(SByte), 0, 0, 0); | |
| 415 public static readonly SqlDataType Byte = DbTinyInt; | |
| 416 public static readonly SqlDataType Int16 = DbSmallInt; | |
| 417 public static readonly SqlDataType UInt16 = new SqlDataType(SqlDbType.Int, typeof(UInt16), 0, 0, 0); | |
| 418 public static readonly SqlDataType Int32 = DbInt; | |
| 419 public static readonly SqlDataType UInt32 = new SqlDataType(SqlDbType.BigInt, typeof(UInt32), 0, 0, 0); | |
| 420 public static readonly SqlDataType Int64 = DbBigInt; | |
| 421 public static readonly SqlDataType UInt64 = new SqlDataType(SqlDbType.Decimal, typeof(UInt64), 0, ulong.MaxValue.ToString().Length, 0); | |
| 422 public static readonly SqlDataType Single = DbReal; | |
| 423 public static readonly SqlDataType Double = DbFloat; | |
| 424 public static readonly SqlDataType Decimal = DbDecimal; | |
| 425 public static readonly SqlDataType DateTime = DbDateTime2; | |
| 426 public static readonly SqlDataType String = DbNVarChar; | |
| 427 public static readonly SqlDataType Guid = DbUniqueIdentifier; | |
| 428 public static readonly SqlDataType ByteArray = DbVarBinary; | |
| 429 public static readonly SqlDataType LinqBinary = DbVarBinary; | |
| 430 public static readonly SqlDataType CharArray = new SqlDataType(SqlDbType.NVarChar, typeof(Char[]), GetMaxLength, 0, 0); | |
| 431 #if !MONO | |
| 432 public static readonly SqlDataType DateTimeOffset = DbDateTimeOffset; | |
| 433 #endif | |
| 434 public static readonly SqlDataType TimeSpan = DbTime; | |
| 435 | |
| 436 #if !SILVERLIGHT | |
| 437 public static readonly SqlDataType SqlByte = new SqlDataType(SqlDbType.TinyInt, typeof(SqlByte), 0, 0, 0); | |
| 438 public static readonly SqlDataType SqlInt16 = new SqlDataType(SqlDbType.SmallInt, typeof(SqlInt16), 0, 0, 0); | |
| 439 public static readonly SqlDataType SqlInt32 = new SqlDataType(SqlDbType.Int, typeof(SqlInt32), 0, 0, 0); | |
| 440 public static readonly SqlDataType SqlInt64 = new SqlDataType(SqlDbType.BigInt, typeof(SqlInt64), 0, 0, 0); | |
| 441 public static readonly SqlDataType SqlSingle = new SqlDataType(SqlDbType.Real, typeof(SqlSingle), 0, 0, 0); | |
| 442 public static readonly SqlDataType SqlBoolean = new SqlDataType(SqlDbType.Bit, typeof(SqlBoolean), 0, 0, 0); | |
| 443 public static readonly SqlDataType SqlDouble = new SqlDataType(SqlDbType.Float, typeof(SqlDouble), 0, 0, 0); | |
| 444 public static readonly SqlDataType SqlDateTime = new SqlDataType(SqlDbType.DateTime, typeof(SqlDateTime), 0, 0, 0); | |
| 445 public static readonly SqlDataType SqlDecimal = new SqlDataType(SqlDbType.Decimal, typeof(SqlDecimal), 0, GetMaxPrecision, 10); | |
| 446 public static readonly SqlDataType SqlMoney = new SqlDataType(SqlDbType.Money, typeof(SqlMoney), 0, GetMaxPrecision, 4); | |
| 447 public static readonly SqlDataType SqlString = new SqlDataType(SqlDbType.NVarChar, typeof(SqlString), GetMaxLength, 0, 0); | |
| 448 public static readonly SqlDataType SqlBinary = new SqlDataType(SqlDbType.Binary, typeof(SqlBinary), GetMaxLength, 0, 0); | |
| 449 public static readonly SqlDataType SqlGuid = new SqlDataType(SqlDbType.UniqueIdentifier, typeof(SqlGuid), 0, 0, 0); | |
| 450 public static readonly SqlDataType SqlBytes = new SqlDataType(SqlDbType.Image, typeof(SqlBytes), GetMaxLength, 0, 0); | |
| 451 public static readonly SqlDataType SqlChars = new SqlDataType(SqlDbType.Text, typeof(SqlChars), GetMaxLength, 0, 0); | |
| 452 public static readonly SqlDataType SqlXml = new SqlDataType(SqlDbType.Xml, typeof(SqlXml), 0, 0, 0); | |
| 453 #endif | |
| 454 | |
| 455 #endregion | |
| 456 | |
| 457 #region Overrides | |
| 458 | |
| 459 #if OVERRIDETOSTRING | |
| 460 | |
| 461 public override string ToString() | |
| 462 { | |
| 463 return ((IQueryElement)this).ToString(new StringBuilder(), new Dictionary<IQueryElement,IQueryElement>()).ToString(); | |
| 464 } | |
| 465 | |
| 466 #endif | |
| 467 | |
| 468 #endregion | |
| 469 | |
| 470 #region ISqlExpression Members | |
| 471 | |
| 472 public int Precedence | |
| 473 { | |
| 474 get { return Sql.Precedence.Primary; } | |
| 475 } | |
| 476 | |
| 477 public Type SystemType | |
| 478 { | |
| 479 get { return typeof(Type); } | |
| 480 } | |
| 481 | |
| 482 #endregion | |
| 483 | |
| 484 #region ISqlExpressionWalkable Members | |
| 485 | |
| 486 ISqlExpression ISqlExpressionWalkable.Walk(bool skipColumns, Func<ISqlExpression,ISqlExpression> func) | |
| 487 { | |
| 488 return func(this); | |
| 489 } | |
| 490 | |
| 491 #endregion | |
| 492 | |
| 493 #region IEquatable<ISqlExpression> Members | |
| 494 | |
| 495 bool IEquatable<ISqlExpression>.Equals(ISqlExpression other) | |
| 496 { | |
| 497 if (this == other) | |
| 498 return true; | |
| 499 | |
| 500 var value = (SqlDataType)other; | |
| 501 return Type == value.Type && Length == value.Length && Precision == value.Precision && Scale == value.Scale; | |
| 502 } | |
| 503 | |
| 504 #endregion | |
| 505 | |
| 506 #region ISqlExpression Members | |
| 507 | |
| 508 public bool CanBeNull() | |
| 509 { | |
| 510 return false; | |
| 511 } | |
| 512 | |
| 513 public bool Equals(ISqlExpression other, Func<ISqlExpression,ISqlExpression,bool> comparer) | |
| 514 { | |
| 515 return ((ISqlExpression)this).Equals(other) && comparer(this, other); | |
| 516 } | |
| 517 | |
| 518 #endregion | |
| 519 | |
| 520 #region ICloneableElement Members | |
| 521 | |
| 522 public ICloneableElement Clone(Dictionary<ICloneableElement, ICloneableElement> objectTree, Predicate<ICloneableElement> doClone) | |
| 523 { | |
| 524 if (!doClone(this)) | |
| 525 return this; | |
| 526 | |
| 527 ICloneableElement clone; | |
| 528 | |
| 529 if (!objectTree.TryGetValue(this, out clone)) | |
| 530 objectTree.Add(this, clone = new SqlDataType(SqlDbType, Type, Length, Precision, Scale)); | |
| 531 | |
| 532 return clone; | |
| 533 } | |
| 534 | |
| 535 #endregion | |
| 536 | |
| 537 #region IQueryElement Members | |
| 538 | |
| 539 public QueryElementType ElementType { get { return QueryElementType.SqlDataType; } } | |
| 540 | |
| 541 StringBuilder IQueryElement.ToString(StringBuilder sb, Dictionary<IQueryElement,IQueryElement> dic) | |
| 542 { | |
| 543 sb.Append(SqlDbType); | |
| 544 | |
| 545 if (Length != 0) | |
| 546 sb.Append('(').Append(Length).Append(')'); | |
| 547 else if (Precision != 0) | |
| 548 sb.Append('(').Append(Precision).Append(',').Append(Scale).Append(')'); | |
| 549 | |
| 550 return sb; | |
| 551 } | |
| 552 | |
| 553 #endregion | |
| 554 } | |
| 555 } |
