SugarParameter.cs 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Data;
  4. using System.Data.Common;
  5. using System.Linq;
  6. using System.Text;
  7. using System.Threading.Tasks;
  8. namespace SqlSugar
  9. {
  10. public class SugarParameter : DbParameter
  11. {
  12. public bool IsRefCursor { get; set; }
  13. public bool IsClob { get; set; }
  14. public bool IsNClob { get; set; }
  15. public bool IsNvarchar2 { get; set; }
  16. public SugarParameter(string name, object value)
  17. {
  18. this.Value = value;
  19. this.ParameterName = name;
  20. if (value != null)
  21. {
  22. SettingDataType(value.GetType());
  23. }
  24. }
  25. public SugarParameter(string name, object value, Type type)
  26. {
  27. this.Value = value;
  28. this.ParameterName = name;
  29. SettingDataType(type);
  30. }
  31. public SugarParameter(string name, object value, Type type, ParameterDirection direction)
  32. {
  33. this.Value = value;
  34. this.ParameterName = name;
  35. this.Direction = direction;
  36. SettingDataType(type);
  37. }
  38. public SugarParameter(string name, object value, Type type, ParameterDirection direction, int size)
  39. {
  40. this.Value = value;
  41. this.ParameterName = name;
  42. this.Direction = direction;
  43. this.Size = size;
  44. SettingDataType(type);
  45. }
  46. public SugarParameter(string name, object value, System.Data.DbType type)
  47. {
  48. this.Value = value;
  49. this.ParameterName = name;
  50. this.DbType = type;
  51. }
  52. public SugarParameter(string name, DataTable value, string SqlServerTypeName)
  53. {
  54. this.Value = value;
  55. this.ParameterName = name;
  56. this.TypeName = SqlServerTypeName;
  57. }
  58. public SugarParameter(string name, object value, System.Data.DbType type, ParameterDirection direction)
  59. {
  60. this.Value = value;
  61. this.ParameterName = name;
  62. this.Direction = direction;
  63. this.DbType = type;
  64. }
  65. public SugarParameter(string name, object value, System.Data.DbType type, ParameterDirection direction, int size)
  66. {
  67. this.Value = value;
  68. this.ParameterName = name;
  69. this.Direction = direction;
  70. this.Size = size;
  71. this.DbType = type;
  72. }
  73. private void SettingDataType(Type type)
  74. {
  75. if (type == UtilConstants.ByteArrayType)
  76. {
  77. this.DbType = System.Data.DbType.Binary;
  78. }
  79. else if (type == UtilConstants.GuidType)
  80. {
  81. this.DbType = System.Data.DbType.Guid;
  82. }
  83. else if (type == UtilConstants.IntType)
  84. {
  85. this.DbType = System.Data.DbType.Int32;
  86. }
  87. else if (type == UtilConstants.ShortType)
  88. {
  89. this.DbType = System.Data.DbType.Int16;
  90. }
  91. else if (type == UtilConstants.LongType)
  92. {
  93. this.DbType = System.Data.DbType.Int64;
  94. }
  95. else if (type == UtilConstants.DateType)
  96. {
  97. this.DbType = System.Data.DbType.DateTime;
  98. }
  99. else if (type == UtilConstants.DobType)
  100. {
  101. this.DbType = System.Data.DbType.Double;
  102. }
  103. else if (type == UtilConstants.DecType)
  104. {
  105. this.DbType = System.Data.DbType.Decimal;
  106. }
  107. else if (type == UtilConstants.ByteType)
  108. {
  109. this.DbType = System.Data.DbType.Byte;
  110. }
  111. else if (type == UtilConstants.SByteType)
  112. {
  113. this.DbType = System.Data.DbType.SByte;
  114. }
  115. else if (type == UtilConstants.FloatType)
  116. {
  117. this.DbType = System.Data.DbType.Single;
  118. }
  119. else if (type == UtilConstants.BoolType)
  120. {
  121. this.DbType = System.Data.DbType.Boolean;
  122. }
  123. else if (type == UtilConstants.StringType)
  124. {
  125. this.DbType = System.Data.DbType.String;
  126. }
  127. else if (type == UtilConstants.DateTimeOffsetType)
  128. {
  129. this.DbType = System.Data.DbType.DateTimeOffset;
  130. }
  131. else if (type == UtilConstants.TimeSpanType)
  132. {
  133. this.DbType = System.Data.DbType.Time;
  134. }
  135. else if (type?.Name=="Geometry")
  136. {
  137. this.DbType = System.Data.DbType.Object;
  138. }
  139. else if (type!=null&&type.IsEnum())
  140. {
  141. this.DbType = System.Data.DbType.Int64;
  142. if (Value != null)
  143. {
  144. this.Value = Convert.ToInt64(Value);
  145. }
  146. }
  147. else if (type==UtilConstants.UIntType)
  148. {
  149. this.DbType = System.Data.DbType.UInt32;
  150. }
  151. else if (type == UtilConstants.ULongType)
  152. {
  153. this.DbType = System.Data.DbType.UInt64;
  154. }
  155. else if (type == UtilConstants.UShortType)
  156. {
  157. this.DbType = System.Data.DbType.UInt16;
  158. }
  159. else if (type == UtilConstants.ShortType)
  160. {
  161. this.DbType = System.Data.DbType.UInt16;
  162. }
  163. else if (type?.Name == "TimeOnly")
  164. {
  165. this.DbType = System.Data.DbType.Time;
  166. this.Value =UtilMethods.TimeOnlyToTimeSpan(this.Value);
  167. }
  168. else if (type?.Name == "DateOnly")
  169. {
  170. this.DbType = System.Data.DbType.Date;
  171. this.Value =Convert.ToDateTime(UtilMethods.DateOnlyToDateTime(this.Value));
  172. }
  173. else if (type?.FullName == "Newtonsoft.Json.Linq.JObject" || type?.FullName == "Newtonsoft.Json.Linq.JArray" || type?.FullName == "Newtonsoft.Json.Linq.JValue")
  174. {
  175. this.Value =this.Value==null?default(string):this.Value.ObjToString() ;
  176. }
  177. }
  178. public SugarParameter(string name, object value, bool isOutput)
  179. {
  180. this.Value = value;
  181. this.ParameterName = name;
  182. if (isOutput)
  183. {
  184. this.Direction = ParameterDirection.Output;
  185. }
  186. }
  187. public override System.Data.DbType DbType
  188. {
  189. get; set;
  190. }
  191. public override ParameterDirection Direction
  192. {
  193. get; set;
  194. }
  195. public override bool IsNullable
  196. {
  197. get; set;
  198. }
  199. public override string ParameterName
  200. {
  201. get; set;
  202. }
  203. public int _Size;
  204. public override int Size
  205. {
  206. get
  207. {
  208. if (_Size == 0 && Value != null)
  209. {
  210. var isByteArray = Value.GetType() == UtilConstants.ByteArrayType;
  211. if (isByteArray)
  212. _Size = -1;
  213. else
  214. {
  215. var length = Value.ToString().Length;
  216. _Size = length < 4000 ? 4000 : -1;
  217. }
  218. }
  219. if (_Size == 0)
  220. _Size = 4000;
  221. return _Size;
  222. }
  223. set
  224. {
  225. _Size = value;
  226. }
  227. }
  228. public override string SourceColumn
  229. {
  230. get; set;
  231. }
  232. public override bool SourceColumnNullMapping
  233. {
  234. get; set;
  235. }
  236. public string UdtTypeName
  237. {
  238. get;
  239. set;
  240. }
  241. public override object Value
  242. {
  243. get; set;
  244. }
  245. public Dictionary<string, object> TempDate
  246. {
  247. get; set;
  248. }
  249. /// <summary>
  250. /// 如果类库是.NET 4.5请删除该属性
  251. /// If the SqlSugar library is.NET 4.5, delete the property
  252. /// </summary>
  253. public override DataRowVersion SourceVersion
  254. {
  255. get; set;
  256. }
  257. public override void ResetDbType()
  258. {
  259. this.DbType = System.Data.DbType.String;
  260. }
  261. public string TypeName { get; set; }
  262. public bool IsJson { get; set; }
  263. public bool IsArray { get; set; }
  264. public object CustomDbType { get; set; }
  265. }
  266. }