/nhibernate/src/NHibernate/SqlTypes/SqlTypeFactory.cs

https://bitbucket.org/fabiomaulo/nhibernate/ · C# · 115 lines · 96 code · 14 blank · 5 comment · 3 complexity · 75e4f047a74b2a0c4612aac2dff83976 MD5 · raw file

  1. using System;
  2. using System.Data;
  3. using System.Collections.Generic;
  4. using System.Runtime.CompilerServices;
  5. using NHibernate.Util;
  6. namespace NHibernate.SqlTypes
  7. {
  8. /// <summary>
  9. /// SqlTypeFactory provides Singleton access to the SqlTypes.
  10. /// </summary>
  11. [Serializable]
  12. public static class SqlTypeFactory
  13. {
  14. // key = typeof(sqlType).Name : ie - BinarySqlType(l), BooleanSqlType, DecimalSqlType(p,s)
  15. // value = SqlType
  16. private static readonly IDictionary<string, SqlType> SqlTypes =
  17. new ThreadSafeDictionary<string, SqlType>(new Dictionary<string, SqlType>(128));
  18. public static readonly SqlType Guid = new SqlType(DbType.Guid);
  19. public static readonly SqlType Boolean = new SqlType(DbType.Boolean);
  20. public static readonly SqlType Byte = new SqlType(DbType.Byte);
  21. public static readonly SqlType Currency = new SqlType(DbType.Currency);
  22. public static readonly SqlType Date = new SqlType(DbType.Date);
  23. public static readonly SqlType DateTime = new SqlType(DbType.DateTime);
  24. public static readonly SqlType DateTime2 = new SqlType(DbType.DateTime2);
  25. public static readonly SqlType DateTimeOffSet = new SqlType(DbType.DateTimeOffset);
  26. public static readonly SqlType Decimal = new SqlType(DbType.Decimal);
  27. public static readonly SqlType Double = new SqlType(DbType.Double);
  28. public static readonly SqlType Int16 = new SqlType(DbType.Int16);
  29. public static readonly SqlType Int32 = new SqlType(DbType.Int32);
  30. public static readonly SqlType Int64 = new SqlType(DbType.Int64);
  31. public static readonly SqlType SByte = new SqlType(DbType.SByte);
  32. public static readonly SqlType Single = new SqlType(DbType.Single);
  33. public static readonly SqlType Time = new SqlType(DbType.Time);
  34. public static readonly SqlType UInt16 = new SqlType(DbType.UInt16);
  35. public static readonly SqlType UInt32 = new SqlType(DbType.UInt32);
  36. public static readonly SqlType UInt64 = new SqlType(DbType.UInt64);
  37. public static readonly SqlType[] NoTypes = new SqlType[0];
  38. private delegate SqlType TypeWithLenCreateDelegate(int length); // Func<int, T>
  39. private static T GetTypeWithLen<T>(int length, TypeWithLenCreateDelegate createDelegate) where T : SqlType
  40. {
  41. string key = GetKeyForLengthBased(typeof (T).Name, length);
  42. SqlType result;
  43. if (!SqlTypes.TryGetValue(key, out result))
  44. {
  45. lock(SqlTypes)
  46. {
  47. if (!SqlTypes.TryGetValue(key, out result))
  48. {
  49. result = createDelegate(length);
  50. SqlTypes.Add(key, result);
  51. }
  52. }
  53. }
  54. return (T) result;
  55. }
  56. private static SqlType GetTypeWithPrecision(DbType dbType, byte precision, byte scale)
  57. {
  58. string key = GetKeyForPrecisionScaleBased(dbType.ToString(), precision, scale);
  59. SqlType result;
  60. if (!SqlTypes.TryGetValue(key, out result))
  61. {
  62. result = new SqlType(dbType, precision, scale);
  63. SqlTypes.Add(key, result);
  64. }
  65. return result;
  66. }
  67. public static AnsiStringSqlType GetAnsiString(int length)
  68. {
  69. return GetTypeWithLen<AnsiStringSqlType>(length, l => new AnsiStringSqlType(l));
  70. }
  71. public static BinarySqlType GetBinary(int length)
  72. {
  73. return GetTypeWithLen<BinarySqlType>(length, l => new BinarySqlType(l));
  74. }
  75. public static BinaryBlobSqlType GetBinaryBlob(int length)
  76. {
  77. return GetTypeWithLen<BinaryBlobSqlType>(length, l => new BinaryBlobSqlType(l));
  78. }
  79. public static StringSqlType GetString(int length)
  80. {
  81. return GetTypeWithLen<StringSqlType>(length, l => new StringSqlType(l));
  82. }
  83. public static StringClobSqlType GetStringClob(int length)
  84. {
  85. return GetTypeWithLen<StringClobSqlType>(length, l => new StringClobSqlType(l));
  86. }
  87. [MethodImpl(MethodImplOptions.Synchronized)]
  88. public static SqlType GetSqlType(DbType dbType, byte precision, byte scale)
  89. {
  90. return GetTypeWithPrecision(dbType, precision, scale);
  91. }
  92. private static string GetKeyForLengthBased(string name, int length)
  93. {
  94. return name + "(" + length + ")";
  95. }
  96. private static string GetKeyForPrecisionScaleBased(string name, byte precision, byte scale)
  97. {
  98. return name + "(" + precision + ", " + scale + ")";
  99. }
  100. }
  101. }