StorageablePage.cs 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Linq.Expressions;
  5. using System.Text;
  6. using System.Threading;
  7. using System.Threading.Tasks;
  8. namespace SqlSugar
  9. {
  10. public class StorageablePage<T> where T : class,new()
  11. {
  12. internal DbLockType? lockType { get; set; }
  13. public SqlSugarProvider Context { get; set; }
  14. public List<T> Data { get; set; }
  15. public int PageSize { get; internal set; }
  16. public Action<int> ActionCallBack { get; internal set; }
  17. public string TableName { get; internal set; }
  18. public Expression<Func<T, object>> whereExpression { get; internal set; }
  19. public int ExecuteCommand()
  20. {
  21. if (Data.Count() == 1 && Data.First() == null)
  22. {
  23. return 0;
  24. }
  25. if (PageSize == 0) { PageSize = 1000; }
  26. var result = 0;
  27. var isNoTran = this.Context.Ado.IsNoTran();
  28. try
  29. {
  30. if (isNoTran)
  31. {
  32. this.Context.Ado.BeginTran();
  33. }
  34. this.Context.Utilities.PageEach(Data, PageSize, pageItem =>
  35. {
  36. result += this.Context.Storageable(pageItem).As(TableName).TranLock(lockType).WhereColumns(whereExpression).ExecuteCommand();
  37. if (ActionCallBack != null)
  38. {
  39. ActionCallBack(result);
  40. }
  41. });
  42. if (isNoTran)
  43. {
  44. this.Context.Ado.CommitTran();
  45. }
  46. }
  47. catch (Exception)
  48. {
  49. if (isNoTran)
  50. {
  51. this.Context.Ado.RollbackTran();
  52. }
  53. throw;
  54. }
  55. return result;
  56. }
  57. public async Task<int> ExecuteCommandAsync(CancellationToken? cancellationToken=null)
  58. {
  59. if (cancellationToken != null)
  60. this.Context.Ado.CancellationToken = cancellationToken.Value;
  61. if (Data.Count() == 1 && Data.First() == null)
  62. {
  63. return 0;
  64. }
  65. if (PageSize == 0) { PageSize = 1000; }
  66. var result = 0;
  67. var isNoTran = this.Context.Ado.IsNoTran();
  68. try
  69. {
  70. if (isNoTran)
  71. {
  72. await this.Context.Ado.BeginTranAsync();
  73. }
  74. await this.Context.Utilities.PageEachAsync(Data, PageSize, async pageItem =>
  75. {
  76. result += await this.Context.Storageable(pageItem).As(TableName).TranLock(lockType).WhereColumns(whereExpression).ExecuteCommandAsync();
  77. if (ActionCallBack != null)
  78. {
  79. ActionCallBack(result);
  80. }
  81. });
  82. if (isNoTran)
  83. {
  84. await this.Context.Ado.CommitTranAsync();
  85. }
  86. }
  87. catch (Exception)
  88. {
  89. if (isNoTran)
  90. {
  91. await this.Context.Ado.RollbackTranAsync();
  92. }
  93. throw;
  94. }
  95. return result;
  96. }
  97. public int ExecuteSqlBulkCopy()
  98. {
  99. if (Data.Count() == 1 && Data.First() == null)
  100. {
  101. return 0;
  102. }
  103. if (PageSize == 0) { PageSize = 1000; }
  104. var result = 0;
  105. var isNoTran = this.Context.Ado.IsNoTran();
  106. try
  107. {
  108. this.Context.Utilities.PageEach(Data, PageSize, pageItem =>
  109. {
  110. result += this.Context.Storageable(pageItem).As(TableName).TranLock(lockType).WhereColumns(whereExpression).ExecuteSqlBulkCopy();
  111. if (ActionCallBack != null)
  112. {
  113. ActionCallBack(result);
  114. }
  115. });
  116. }
  117. catch (Exception)
  118. {
  119. throw;
  120. }
  121. return result;
  122. }
  123. public async Task<int> ExecuteSqlBulkCopyAsync(CancellationToken? cancellationToken = null)
  124. {
  125. if(cancellationToken!=null)
  126. this.Context.Ado.CancellationToken = cancellationToken.Value;
  127. if (Data.Count() == 1 && Data.First() == null)
  128. {
  129. return 0;
  130. }
  131. if (PageSize == 0) { PageSize = 1000; }
  132. var result = 0;
  133. var isNoTran = this.Context.Ado.IsNoTran();
  134. try
  135. {
  136. await this.Context.Utilities.PageEachAsync(Data, PageSize, async pageItem =>
  137. {
  138. result += await this.Context.Storageable(pageItem).As(TableName).TranLock(lockType).WhereColumns(whereExpression).ExecuteSqlBulkCopyAsync();
  139. if (ActionCallBack != null)
  140. {
  141. ActionCallBack(result);
  142. }
  143. });
  144. }
  145. catch (Exception)
  146. {
  147. throw;
  148. }
  149. return result;
  150. }
  151. }
  152. }