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.

280 lines
7.9 KiB

2 years ago
  1. using System;
  2. using System.Data;
  3. namespace SqlSugar
  4. {
  5. public static partial class IDataRecordExtensions
  6. {
  7. #region Common Extensions
  8. public static Guid GetStringGuid(this IDataRecord dr, int i)
  9. {
  10. var result = Guid.Parse(dr.GetValue(i).ToString());
  11. return result;
  12. }
  13. public static Guid? GetConvertStringGuid(this IDataRecord dr, int i)
  14. {
  15. if (dr.IsDBNull(i))
  16. {
  17. return Guid.Empty;
  18. }
  19. var result = Guid.Parse(dr.GetValue(i).ToString());
  20. return result;
  21. }
  22. public static bool? GetConvertBoolean(this IDataRecord dr, int i)
  23. {
  24. var result = dr.GetBoolean(i);
  25. return result;
  26. }
  27. public static byte? GetConvertByte(this IDataRecord dr, int i)
  28. {
  29. if (dr.IsDBNull(i))
  30. {
  31. return null;
  32. }
  33. var result = dr.GetByte(i);
  34. return result;
  35. }
  36. public static char? GetConvertChar(this IDataRecord dr, int i)
  37. {
  38. if (dr.IsDBNull(i))
  39. {
  40. return null;
  41. }
  42. var result = dr.GetChar(i);
  43. return result;
  44. }
  45. public static DateTime? GetConvertDateTime(this IDataRecord dr, int i)
  46. {
  47. var result = dr.GetDateTime(i);
  48. if (result == DateTime.MinValue)
  49. {
  50. return null; ;
  51. }
  52. return result;
  53. }
  54. public static decimal? GetConvertDecimal(this IDataRecord dr, int i)
  55. {
  56. if (dr.IsDBNull(i))
  57. {
  58. return null;
  59. }
  60. var result = dr.GetDecimal(i);
  61. return result;
  62. }
  63. public static double? GetConvertDouble(this IDataRecord dr, int i)
  64. {
  65. if (dr.IsDBNull(i))
  66. {
  67. return null;
  68. }
  69. var result = dr.GetDouble(i);
  70. return result;
  71. }
  72. public static Guid? GetConvertGuid(this IDataRecord dr, int i)
  73. {
  74. if (dr.IsDBNull(i))
  75. {
  76. return null;
  77. }
  78. var result = dr.GetGuid(i);
  79. return result;
  80. }
  81. public static short? GetConvertInt16(this IDataRecord dr, int i)
  82. {
  83. if (dr.IsDBNull(i))
  84. {
  85. return null;
  86. }
  87. var result = dr.GetInt16(i);
  88. return result;
  89. }
  90. public static Int32? GetConvertInt32(this IDataRecord dr, int i)
  91. {
  92. if (dr.IsDBNull(i))
  93. {
  94. return null;
  95. }
  96. var result = dr.GetInt32(i);
  97. return result;
  98. }
  99. public static long? GetConvetInt64(this IDataRecord dr, int i)
  100. {
  101. if (dr.IsDBNull(i))
  102. {
  103. return null;
  104. }
  105. var result = dr.GetInt64(i);
  106. return result;
  107. }
  108. public static float? GetConvertFloat(this IDataRecord dr, int i)
  109. {
  110. if (dr.IsDBNull(i))
  111. {
  112. return null;
  113. }
  114. var result = dr.GetFloat(i);
  115. return result;
  116. }
  117. public static DateTime GetdatetimeoffsetDate(this IDataRecord dr, int i)
  118. {
  119. if (dr.IsDBNull(i))
  120. {
  121. return DateTime.MinValue;
  122. }
  123. var offsetValue = (DateTimeOffset)dr.GetValue(i);
  124. var result = offsetValue.DateTime;
  125. return result;
  126. }
  127. public static DateTime? GetConvertdatetimeoffsetDate(this IDataRecord dr, int i)
  128. {
  129. if (dr.IsDBNull(i))
  130. {
  131. return DateTime.MinValue;
  132. }
  133. var offsetValue = (DateTimeOffset)dr.GetValue(i);
  134. var result = offsetValue.DateTime;
  135. return result;
  136. }
  137. public static DateTimeOffset Getdatetimeoffset(this IDataRecord dr, int i)
  138. {
  139. if (dr.IsDBNull(i))
  140. {
  141. return default(DateTimeOffset);
  142. }
  143. var result = (DateTimeOffset)dr.GetValue(i);
  144. return result;
  145. }
  146. public static DateTimeOffset? GetConvertdatetimeoffset(this IDataRecord dr, int i)
  147. {
  148. if (dr.IsDBNull(i))
  149. {
  150. return default(DateTimeOffset);
  151. }
  152. var result = (DateTimeOffset)dr.GetValue(i);
  153. return result;
  154. }
  155. public static string GetConvertString(this IDataRecord dr, int i)
  156. {
  157. if (dr.IsDBNull(i))
  158. {
  159. return null;
  160. }
  161. var result = Convert.ToString(dr.GetValue(i));
  162. return result;
  163. }
  164. public static Nullable<T> GetOtherNull<T>(this IDataReader dr, int i) where T : struct
  165. {
  166. if (dr.IsDBNull(i))
  167. {
  168. return null;
  169. }
  170. return (T)Convert.ChangeType(dr.GetValue(i), typeof(T));
  171. }
  172. public static T GetOther<T>(this IDataReader dr, int i)
  173. {
  174. return (T)Convert.ChangeType(dr.GetValue(i), typeof(T));
  175. }
  176. public static Nullable<T> GetConvertEnum_Null<T>(this IDataReader dr, int i) where T : struct
  177. {
  178. if (dr.IsDBNull(i))
  179. {
  180. return null;
  181. }
  182. var value = dr.GetValue(i);
  183. var t = (T)Enum.ToObject(typeof(T), value);
  184. return t;
  185. }
  186. public static T GetEnum<T>(this IDataReader dr, int i) where T : struct
  187. {
  188. var value = dr.GetValue(i);
  189. var t = (T)Enum.ToObject(typeof(T), value);
  190. return t;
  191. }
  192. public static object GetEntity(this IDataReader dr, SqlSugarClient context)
  193. {
  194. return null;
  195. }
  196. #endregion Common Extensions
  197. #region Sqlite Extensions
  198. public static Nullable<T> GetSqliteTypeNull<T>(this IDataReader dr, int i) where T : struct
  199. {
  200. var type = UtilMethods.GetUnderType(typeof(T));
  201. if (dr.IsDBNull(i))
  202. {
  203. return null;
  204. }
  205. return SqliteTypeConvert<T>(dr, i, type);
  206. }
  207. public static T GetSqliteType<T>(this IDataReader dr, int i) where T : struct
  208. {
  209. var type = typeof(T);
  210. return SqliteTypeConvert<T>(dr, i, type);
  211. }
  212. private static T SqliteTypeConvert<T>(IDataReader dr, int i, Type type) where T : struct
  213. {
  214. if (type.IsIn(UtilConstants.IntType))
  215. {
  216. return (T)((object)(dr.GetInt32(i)));
  217. }
  218. else if (type == UtilConstants.DateType)
  219. {
  220. return (T)Convert.ChangeType(Convert.ToDateTime(dr.GetString(i)), type);
  221. }
  222. else if (type == UtilConstants.DecType)
  223. {
  224. return (T)Convert.ChangeType(dr.GetDecimal(i), type);
  225. }
  226. else if (type == UtilConstants.DobType)
  227. {
  228. return (T)Convert.ChangeType(dr.GetDouble(i), type);
  229. }
  230. else if (type == UtilConstants.BoolType)
  231. {
  232. return (T)Convert.ChangeType(dr.GetBoolean(i), type);
  233. }
  234. else if (type == UtilConstants.LongType)
  235. {
  236. return (T)Convert.ChangeType(dr.GetInt64(i), type);
  237. }
  238. else if (type == UtilConstants.GuidType)
  239. {
  240. var guidString = dr.GetString(i);
  241. var changeValue = guidString.IsNullOrEmpty() ? Guid.Empty.ToString() : guidString;
  242. return (T)Convert.ChangeType(Guid.Parse(changeValue), type);
  243. }
  244. else
  245. {
  246. return (T)Convert.ChangeType((dr.GetString(i)), type);
  247. }
  248. }
  249. #endregion Sqlite Extensions
  250. }
  251. }