RelationalDataReaderExtensions.cs 9.1 KB

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