You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

295 lines
7.9 KiB

2 years ago
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Data;
  4. using System.Linq;
  5. namespace SqlSugar
  6. {
  7. public abstract partial class DbBindProvider : DbBindAccessory, IDbBind
  8. {
  9. #region Properties
  10. public virtual SqlSugarClient Context { get; set; }
  11. public abstract List<KeyValuePair<string, CSharpDataType>> MappingTypes { get; }
  12. #endregion Properties
  13. #region Public methods
  14. public virtual string GetDbTypeName(string csharpTypeName)
  15. {
  16. if (csharpTypeName == UtilConstants.ByteArrayType.Name)
  17. return "varbinary";
  18. if (csharpTypeName.ToLower() == "int32")
  19. csharpTypeName = "int";
  20. if (csharpTypeName.ToLower() == "int16")
  21. csharpTypeName = "short";
  22. if (csharpTypeName.ToLower() == "int64")
  23. csharpTypeName = "long";
  24. if (csharpTypeName.ToLower().IsIn("boolean", "bool"))
  25. csharpTypeName = "bool";
  26. var mappings = this.MappingTypes.Where(it => it.Value.ToString().Equals(csharpTypeName, StringComparison.CurrentCultureIgnoreCase));
  27. return mappings.HasValue() ? mappings.First().Key : "varchar";
  28. }
  29. public string GetCsharpTypeName(string dbTypeName)
  30. {
  31. var mappings = this.MappingTypes.Where(it => it.Key == dbTypeName);
  32. return mappings.HasValue() ? mappings.First().Key : "string";
  33. }
  34. public virtual string GetConvertString(string dbTypeName)
  35. {
  36. var result = string.Empty;
  37. switch (dbTypeName.ToLower())
  38. {
  39. #region Int
  40. case "int":
  41. result = "Convert.ToInt32";
  42. break;
  43. #endregion Int
  44. #region String
  45. case "nchar":
  46. case "char":
  47. case "ntext":
  48. case "nvarchar":
  49. case "varchar":
  50. case "text":
  51. result = "Convert.ToString";
  52. break;
  53. #endregion String
  54. #region Long
  55. case "bigint":
  56. result = "Convert.ToInt64";
  57. break;
  58. #endregion Long
  59. #region Bool
  60. case "bit":
  61. result = "Convert.ToBoolean";
  62. break;
  63. #endregion Bool
  64. #region Datetime
  65. case "timestamp":
  66. case "smalldatetime":
  67. case "datetime":
  68. case "date":
  69. case "datetime2":
  70. result = "Convert.ToDateTime";
  71. break;
  72. #endregion Datetime
  73. #region Decimal
  74. case "smallmoney":
  75. case "single":
  76. case "numeric":
  77. case "money":
  78. case "decimal":
  79. result = "Convert.ToDecimal";
  80. break;
  81. #endregion Decimal
  82. #region Double
  83. case "float":
  84. result = "Convert.ToDouble";
  85. break;
  86. #endregion Double
  87. #region Byte[]
  88. case "varbinary":
  89. case "binary":
  90. case "image":
  91. result = "byte[]";
  92. break;
  93. #endregion Byte[]
  94. #region Float
  95. case "real":
  96. result = "Convert.ToSingle";
  97. break;
  98. #endregion Float
  99. #region Short
  100. case "smallint":
  101. result = "Convert.ToInt16";
  102. break;
  103. #endregion Short
  104. #region Byte
  105. case "tinyint":
  106. result = "Convert.ToByte";
  107. break;
  108. #endregion Byte
  109. #region Guid
  110. case "uniqueidentifier":
  111. result = "Guid.Parse";
  112. break;
  113. #endregion Guid
  114. #region Null
  115. default:
  116. result = null;
  117. break;
  118. #endregion Null
  119. }
  120. return result;
  121. }
  122. public virtual string GetPropertyTypeName(string dbTypeName)
  123. {
  124. dbTypeName = dbTypeName.ToLower();
  125. var propertyTypes = MappingTypes.Where(it => it.Key.Equals(dbTypeName, StringComparison.CurrentCultureIgnoreCase));
  126. if (dbTypeName == "int32")
  127. {
  128. return "int";
  129. }
  130. else if (dbTypeName == "int64")
  131. {
  132. return "long";
  133. }
  134. else if (dbTypeName == "int16")
  135. {
  136. return "short";
  137. }
  138. else if (propertyTypes == null)
  139. {
  140. return "other";
  141. }
  142. else if (dbTypeName.IsContainsIn("xml", "string", "String"))
  143. {
  144. return "string";
  145. }
  146. else if (dbTypeName.IsContainsIn("boolean", "bool"))
  147. {
  148. return "bool";
  149. }
  150. else if (propertyTypes == null || propertyTypes.Count() == 0)
  151. {
  152. Check.ThrowNotSupportedException(string.Format(" \"{0}\" Type NotSupported, DbBindProvider.GetPropertyTypeName error.", dbTypeName));
  153. return null;
  154. }
  155. else if (propertyTypes.First().Value == CSharpDataType.byteArray)
  156. {
  157. return "byte[]";
  158. }
  159. else
  160. {
  161. return propertyTypes.First().Value.ToString();
  162. }
  163. }
  164. public virtual List<T> DataReaderToList<T>(Type type, IDataReader dataReader)
  165. {
  166. using (dataReader)
  167. {
  168. if (type.Name.Contains("KeyValuePair"))
  169. {
  170. return GetKeyValueList<T>(type, dataReader);
  171. }
  172. else if (type.IsValueType() || type == UtilConstants.StringType || type == UtilConstants.ByteArrayType)
  173. {
  174. return GetValueTypeList<T>(type, dataReader);
  175. }
  176. else if (type.IsArray)
  177. {
  178. return GetArrayList<T>(type, dataReader);
  179. }
  180. else
  181. {
  182. return GetEntityList<T>(Context, dataReader);
  183. }
  184. }
  185. }
  186. #endregion Public methods
  187. #region Throw rule
  188. public virtual List<string> IntThrow
  189. {
  190. get
  191. {
  192. return new List<string> { "datetime", "byte" };
  193. }
  194. }
  195. public virtual List<string> ShortThrow
  196. {
  197. get
  198. {
  199. return new List<string> { "datetime", "guid" };
  200. }
  201. }
  202. public virtual List<string> DecimalThrow
  203. {
  204. get
  205. {
  206. return new List<string> { "datetime", "byte", "guid" };
  207. }
  208. }
  209. public virtual List<string> DoubleThrow
  210. {
  211. get
  212. {
  213. return new List<string> { "datetime", "byte", "guid" };
  214. }
  215. }
  216. public virtual List<string> DateThrow
  217. {
  218. get
  219. {
  220. return new List<string> { "int32", "decimal", "double", "byte", "guid" };
  221. }
  222. }
  223. public virtual List<string> GuidThrow
  224. {
  225. get
  226. {
  227. return new List<string> { "int32", "datetime", "decimal", "double", "byte" };
  228. }
  229. }
  230. public virtual List<string> StringThrow
  231. {
  232. get
  233. {
  234. return new List<string> { "int32", "datetime", "decimal", "double", "byte", "guid" };
  235. }
  236. }
  237. #endregion Throw rule
  238. }
  239. }