RelationalDataReaderExtensions.cs 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. using EasyDevCore.Common;
  2. using Microsoft.EntityFrameworkCore.Storage;
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Data;
  6. using System.Data.Common;
  7. using System.Dynamic;
  8. using System.Linq;
  9. using System.Text;
  10. using System.Threading;
  11. using System.Threading.Tasks;
  12. using EasyDevCore.Database;
  13. namespace EasyDevCore.Database
  14. {
  15. /// <summary>
  16. ///
  17. /// </summary>
  18. public static class RelationalDataReaderExtensions
  19. {
  20. /// <summary>
  21. /// Gets the value.
  22. /// </summary>
  23. /// <typeparam name="T"></typeparam>
  24. /// <param name="reader">The reader.</param>
  25. /// <param name="fieldName">Name of the field.</param>
  26. /// <param name="valueIfNull">The value if null.</param>
  27. /// <returns></returns>
  28. public static T GetValue<T>(this RelationalDataReader reader, string fieldName, T valueIfNull) => reader.DbDataReader.GetValue<T>(fieldName, valueIfNull);
  29. /// <summary>
  30. /// Gets the row.
  31. /// </summary>
  32. /// <param name="reader">The reader.</param>
  33. /// <returns></returns>
  34. public static dynamic GetRow(this RelationalDataReader reader) => reader.DbDataReader.GetRow();
  35. /// <summary>
  36. /// Gets the row asynchronous.
  37. /// </summary>
  38. /// <param name="reader">The reader.</param>
  39. /// <param name="cancellationToken">The cancellation token.</param>
  40. /// <returns></returns>
  41. public static Task<dynamic> GetRowAsync(this RelationalDataReader reader, CancellationToken cancellationToken)
  42. => reader.DbDataReader.GetRowAsync(cancellationToken);
  43. /// <summary>
  44. /// Fetches to dynamic.
  45. /// </summary>
  46. /// <param name="reader">The reader.</param>
  47. /// <returns></returns>
  48. public static IList<dynamic> FetchToDynamic(this RelationalDataReader reader) => reader.DbDataReader.FetchToDynamic();
  49. /// <summary>
  50. /// Fetches to dynamic asynchronous.
  51. /// </summary>
  52. /// <param name="reader">The reader.</param>
  53. /// <param name="cancellationToken">The cancellation token.</param>
  54. /// <returns></returns>
  55. public static Task<IList<dynamic>> FetchToDynamicAsync(this RelationalDataReader reader, CancellationToken cancellationToken = default)
  56. => reader.DbDataReader.FetchToDynamicAsync(cancellationToken);
  57. /// <summary>
  58. /// Fetches the fill list.
  59. /// </summary>
  60. /// <typeparam name="T"></typeparam>
  61. /// <param name="reader">The reader.</param>
  62. /// <param name="list">The list.</param>
  63. /// <returns></returns>
  64. public static int FetchFillList<T>(this RelationalDataReader reader, IList<T> list) where T : class, new() => reader.DbDataReader.FetchFillList<T>(list);
  65. /// <summary>
  66. /// Fetches the fill list asynchronous.
  67. /// </summary>
  68. /// <typeparam name="T"></typeparam>
  69. /// <param name="reader">The reader.</param>
  70. /// <param name="list">The list.</param>
  71. /// <param name="cancellationToken">The cancellation token.</param>
  72. /// <returns></returns>
  73. public static Task<int> FetchFillListAsync<T>(this RelationalDataReader reader, IList<T> list, CancellationToken cancellationToken = default)
  74. where T : class, new() => reader.DbDataReader.FetchFillListAsync(list, cancellationToken);
  75. /// <summary>
  76. /// Fetches to list.
  77. /// </summary>
  78. /// <typeparam name="TResult">The type of the result.</typeparam>
  79. /// <param name="reader">The reader.</param>
  80. /// <param name="selector">The selector.</param>
  81. /// <returns></returns>
  82. public static IList<TResult> FetchToList<TResult>(this RelationalDataReader reader, Func<dynamic, TResult> selector) => reader.DbDataReader.FetchToList(selector);
  83. /// <summary>
  84. /// Fetches to list asynchronous.
  85. /// </summary>
  86. /// <typeparam name="TResult">The type of the result.</typeparam>
  87. /// <param name="reader">The reader.</param>
  88. /// <param name="selector">The selector.</param>
  89. /// <param name="cancellationToken">The cancellation token.</param>
  90. /// <returns></returns>
  91. public static Task<IList<TResult>> FetchToListAsync<TResult>(this RelationalDataReader reader, Func<dynamic, TResult> selector, CancellationToken cancellationToken = default)
  92. => reader.DbDataReader.FetchToListAsync(selector, cancellationToken);
  93. /// <summary>
  94. /// Fetches to list.
  95. /// </summary>
  96. /// <typeparam name="T"></typeparam>
  97. /// <param name="reader">The reader.</param>
  98. /// <param name="list">The list.</param>
  99. /// <returns></returns>
  100. public static IList<T> FetchToList<T>(this RelationalDataReader reader, IList<T> list = null) where T : class, new() => reader.DbDataReader.FetchToList(list);
  101. /// <summary>
  102. /// Fetches to list asynchronous.
  103. /// </summary>
  104. /// <typeparam name="T"></typeparam>
  105. /// <param name="reader">The reader.</param>
  106. /// <param name="list">The list.</param>
  107. /// <param name="cancellationToken">The cancellation token.</param>
  108. /// <returns></returns>
  109. public static Task<IList<T>> FetchToListAsync<T>(this RelationalDataReader reader, IList<T> list = null, CancellationToken cancellationToken = default)
  110. where T : class, new() => reader.DbDataReader.FetchToListAsync(list, cancellationToken);
  111. /// <summary>
  112. /// Fetches the fill data set.
  113. /// </summary>
  114. /// <param name="reader">The reader.</param>
  115. /// <param name="dataSet">The data set.</param>
  116. public static int FetchFillDataSet(this RelationalDataReader reader, DataSet dataSet) => reader.DbDataReader.FetchFillDataSet(dataSet);
  117. /// <summary>
  118. /// Fetches the fill data set asynchronous.
  119. /// </summary>
  120. /// <param name="reader">The reader.</param>
  121. /// <param name="dataSet">The data set.</param>
  122. /// <param name="cancellationToken">The cancellation token.</param>
  123. /// <returns></returns>
  124. public static Task<int> FetchFillDataSetAsync(this RelationalDataReader reader, DataSet dataSet, CancellationToken cancellationToken = default)
  125. => reader.DbDataReader.FetchFillDataSetAsync(dataSet, cancellationToken);
  126. // <summary>
  127. /// <summary>
  128. /// Fetches to data set.
  129. /// </summary>
  130. /// <param name="reader">The reader.</param>
  131. /// <param name="dataSet">The data set.</param>
  132. /// <param name="parameters">The parameters.</param>
  133. /// <returns></returns>
  134. public static DataSet FetchToDataSet(this RelationalDataReader reader, DataSet dataSet = null, EasyDbParameterCollection parameters = null) => reader.DbDataReader.FetchToDataSet(dataSet, parameters);
  135. /// <summary>
  136. /// Fetches to data set asynchronous.
  137. /// </summary>
  138. /// <param name="reader">The reader.</param>
  139. /// <param name="DataSet">The entity set.</param>
  140. /// <param name="cancellationToken">The cancellation token.</param>
  141. /// <returns></returns>
  142. public static Task<DataSet> FetchToDataSetAsync(this RelationalDataReader reader, DataSet DataSet = null, CancellationToken cancellationToken = default)
  143. => reader.DbDataReader.FetchToDataSetAsync(DataSet, cancellationToken);
  144. /// <summary>
  145. /// Fetches to table.
  146. /// </summary>
  147. /// <param name="reader">The reader.</param>
  148. /// <param name="table">The table.</param>
  149. /// <returns></returns>
  150. public static int FetchFillTable(this RelationalDataReader reader, DataTable table) => reader.DbDataReader.FetchFillTable(table);
  151. /// <summary>
  152. /// Fetches to table.
  153. /// </summary>
  154. /// <param name="reader">The reader.</param>
  155. /// <param name="table">The table.</param>
  156. /// <param name="cancellationToken">The cancellation token.</param>
  157. /// <returns></returns>
  158. public static Task<int> FetchFillTableAsync(this RelationalDataReader reader, DataTable table, CancellationToken cancellationToken = default)
  159. => reader.DbDataReader.FetchFillTableAsync(table, cancellationToken);
  160. /// <summary>
  161. /// Fetches to table.
  162. /// </summary>
  163. /// <param name="reader">The reader.</param>
  164. /// <param name="table">The table.</param>
  165. /// <returns></returns>
  166. public static DataTable FetchToTable(this RelationalDataReader reader, DataTable table = null) => reader.DbDataReader.FetchToTable(table);
  167. /// <summary>
  168. /// Fetches to table asynchronous.
  169. /// </summary>
  170. /// <param name="reader">The reader.</param>
  171. /// <param name="table">The table.</param>
  172. /// <param name="cancellationToken">The cancellation token.</param>
  173. /// <returns></returns>
  174. public static Task<DataTable> FetchToTableAsync(this RelationalDataReader reader, DataTable table = null, CancellationToken cancellationToken = default)
  175. => reader.DbDataReader.FetchToTableAsync(table, cancellationToken);
  176. }
  177. }