FastestProvider.cs 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Data;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using System.Linq;
  7. namespace SqlSugar
  8. {
  9. public partial class FastestProvider<T>:IFastest<T> where T:class,new()
  10. {
  11. internal SqlSugarProvider context;
  12. private ISugarQueryable<T> queryable;
  13. private EntityInfo entityInfo { get; set; }
  14. public bool isLog;
  15. public FastestProvider(SqlSugarProvider sqlSugarProvider)
  16. {
  17. this.context = sqlSugarProvider;
  18. this.queryable = this.context.Queryable<T>();
  19. entityInfo=this.context.EntityMaintenance.GetEntityInfo<T>();
  20. }
  21. #region BulkCopy
  22. public int BulkCopy(string tableName,DataTable dt)
  23. {
  24. return BulkCopyAsync(tableName,dt).ConfigureAwait(true).GetAwaiter().GetResult();
  25. }
  26. public int BulkCopy(DataTable dt)
  27. {
  28. Check.ExceptionEasy(this.AsName.IsNullOrEmpty(), "need .AS(tablaeName) ", "需要 .AS(tablaeName) 设置表名");
  29. return BulkCopyAsync(this.AsName, dt).ConfigureAwait(true).GetAwaiter().GetResult();
  30. }
  31. public Task<int> BulkCopyAsync(DataTable dt)
  32. {
  33. Check.ExceptionEasy(this.AsName.IsNullOrEmpty(), "need .AS(tablaeName) ", "需要 .AS(tablaeName) 设置表名");
  34. return BulkCopyAsync(this.AsName, dt);
  35. }
  36. public async Task<int> BulkCopyAsync(string tableName, DataTable dt)
  37. {
  38. if (Size > 0)
  39. {
  40. int resul = 0;
  41. await this.context.Utilities.PageEachAsync(dt.Rows.Cast<DataRow>().ToList(), Size, async item =>
  42. {
  43. resul += await _BulkCopy(tableName,item.CopyToDataTable());
  44. });
  45. return resul;
  46. }
  47. else
  48. {
  49. return await _BulkCopy(tableName,dt);
  50. }
  51. }
  52. public int BulkCopy(List<T> datas)
  53. {
  54. return BulkCopyAsync(datas).ConfigureAwait(true).GetAwaiter().GetResult();
  55. }
  56. public async Task<int> BulkCopyAsync(List<T> datas)
  57. {
  58. if (Size > 0)
  59. {
  60. int resul=0;
  61. await this.context.Utilities.PageEachAsync(datas, Size, async item =>
  62. {
  63. resul+= await _BulkCopy(item);
  64. });
  65. return resul;
  66. }
  67. else
  68. {
  69. return await _BulkCopy(datas);
  70. }
  71. }
  72. #endregion
  73. #region BulkUpdate
  74. public int BulkUpdate(List<T> datas)
  75. {
  76. return BulkUpdateAsync(datas).ConfigureAwait(true).GetAwaiter().GetResult();
  77. }
  78. public async Task<int> BulkUpdateAsync(List<T> datas)
  79. {
  80. var whereColumns=entityInfo.Columns.Where(it => it.IsPrimarykey).Select(it=>it.DbColumnName??it.PropertyName).ToArray();
  81. var updateColumns = entityInfo.Columns.Where(it => !it.IsPrimarykey&&!it.IsIdentity&&!it.IsOnlyIgnoreUpdate&&!it.IsIgnore).Select(it => it.DbColumnName ?? it.PropertyName).ToArray();
  82. return await BulkUpdateAsync(datas,whereColumns,updateColumns);
  83. }
  84. public int BulkUpdate(List<T> datas, string[] whereColumns, string[] updateColumns)
  85. {
  86. whereColumns = whereColumns.Select(x => this.entityInfo.Columns.FirstOrDefault(it => it.PropertyName.EqualCase(x) || it.DbColumnName.EqualCase(x))?.DbColumnName ?? x).ToArray();
  87. updateColumns = updateColumns.Select(x => this.entityInfo.Columns.FirstOrDefault(it => it.PropertyName.EqualCase(x) || it.DbColumnName.EqualCase(x))?.DbColumnName ?? x).ToArray();
  88. return BulkUpdateAsync(datas,whereColumns,updateColumns).ConfigureAwait(true).GetAwaiter().GetResult();
  89. }
  90. public int BulkUpdate(List<T> datas, string[] whereColumns)
  91. {
  92. return BulkUpdateAsync(datas, whereColumns).GetAwaiter().GetResult();
  93. }
  94. public async Task<int> BulkUpdateAsync(List<T> datas, string[] whereColumns)
  95. {
  96. whereColumns = whereColumns.Select(x => this.entityInfo.Columns.FirstOrDefault(it => it.PropertyName.EqualCase(x) || it.DbColumnName.EqualCase(x))?.DbColumnName ?? x).ToArray();
  97. var updateColumns = this.entityInfo.Columns
  98. .Where(it => !whereColumns.Any(z => z.EqualCase(it.DbColumnName)))
  99. .Where(it => !it.IsIdentity)
  100. .Where(it => !it.IsPrimarykey)
  101. .Where(it => !it.IsOnlyIgnoreUpdate)
  102. .Where(it => !it.IsIgnore)
  103. .Select(it => it.DbColumnName)
  104. .ToArray();
  105. return await BulkUpdateAsync(datas, whereColumns, updateColumns).ConfigureAwait(true);
  106. }
  107. public async Task<int> BulkUpdateAsync(List<T> datas,string [] whereColumns,string [] updateColumns)
  108. {
  109. if (Size > 0)
  110. {
  111. int resul = 0;
  112. await this.context.Utilities.PageEachAsync(datas, Size, async item =>
  113. {
  114. resul += await _BulkUpdate(item, whereColumns, updateColumns);
  115. });
  116. return resul;
  117. }
  118. else
  119. {
  120. return await _BulkUpdate(datas, whereColumns, updateColumns);
  121. }
  122. }
  123. public int BulkUpdate(string tableName,DataTable dataTable, string[] whereColumns, string[] updateColumns)
  124. {
  125. return BulkUpdateAsync(tableName,dataTable, whereColumns, updateColumns).ConfigureAwait(true).GetAwaiter().GetResult();
  126. }
  127. public int BulkUpdate(DataTable dataTable, string[] whereColumns, string[] updateColumns)
  128. {
  129. Check.ExceptionEasy(this.AsName.IsNullOrEmpty(), "need .AS(tablaeName) ", "需要 .AS(tablaeName) 设置表名");
  130. return BulkUpdateAsync(this.AsName, dataTable, whereColumns, updateColumns).ConfigureAwait(true).GetAwaiter().GetResult();
  131. }
  132. public int BulkUpdate(DataTable dataTable, string[] whereColumns)
  133. {
  134. string[] updateColumns = dataTable.Columns.Cast<DataColumn>().Select(it => it.ColumnName).Where(it => !whereColumns.Any(z => z.EqualCase(it))).ToArray();
  135. Check.ExceptionEasy(this.AsName.IsNullOrEmpty(), "need .AS(tablaeName) ", "需要 .AS(tablaeName) 设置表名");
  136. return BulkUpdateAsync(this.AsName, dataTable, whereColumns, updateColumns).ConfigureAwait(true).GetAwaiter().GetResult();
  137. }
  138. public Task<int> BulkUpdateAsync(DataTable dataTable, string[] whereColumns)
  139. {
  140. string[] updateColumns = dataTable.Columns.Cast<DataColumn>().Select(it => it.ColumnName).Where(it => !whereColumns.Any(z => z.EqualCase(it))).ToArray();
  141. Check.ExceptionEasy(this.AsName.IsNullOrEmpty(), "need .AS(tablaeName) ", "需要 .AS(tablaeName) 设置表名");
  142. return BulkUpdateAsync(this.AsName, dataTable, whereColumns, updateColumns);
  143. }
  144. public async Task<int> BulkUpdateAsync(string tableName, DataTable dataTable, string[] whereColumns, string[] updateColumns)
  145. {
  146. if (Size > 0)
  147. {
  148. int resul = 0;
  149. await this.context.Utilities.PageEachAsync(dataTable.Rows.Cast<DataRow>().ToList(), Size, async item =>
  150. {
  151. resul += await _BulkUpdate(tableName,item.CopyToDataTable(), whereColumns, updateColumns);
  152. });
  153. return resul;
  154. }
  155. else
  156. {
  157. return await _BulkUpdate(tableName,dataTable, whereColumns, updateColumns);
  158. }
  159. }
  160. #endregion
  161. #region BulkMerge
  162. public Task<int> BulkMergeAsync(List<T> datas)
  163. {
  164. var updateColumns = entityInfo.Columns.Where(it => !it.IsPrimarykey && !it.IsIdentity && !it.IsOnlyIgnoreUpdate && !it.IsIgnore).Select(it => it.DbColumnName ?? it.PropertyName).ToArray();
  165. var whereColumns = entityInfo.Columns.Where(it => it.IsPrimarykey).Select(it => it.DbColumnName ?? it.PropertyName).ToArray(); ;
  166. return BulkMergeAsync(datas, whereColumns, updateColumns);
  167. }
  168. public int BulkMerge(List<T> datas)
  169. {
  170. return BulkMergeAsync(datas).GetAwaiter().GetResult();
  171. }
  172. public Task<int> BulkMergeAsync(List<T> datas, string[] whereColumns)
  173. {
  174. var updateColumns = entityInfo.Columns.Where(it => !it.IsPrimarykey && !it.IsIdentity && !it.IsOnlyIgnoreUpdate && !it.IsIgnore).Select(it => it.DbColumnName ?? it.PropertyName).ToArray();
  175. return BulkMergeAsync(datas, whereColumns, updateColumns);
  176. }
  177. public int BulkMerge(List<T> datas, string[] whereColumns)
  178. {
  179. return BulkMergeAsync(datas, whereColumns).GetAwaiter().GetResult();
  180. }
  181. public async Task<int> BulkMergeAsync(List<T> datas, string[] whereColumns, string[] updateColumns)
  182. {
  183. if (Size > 0)
  184. {
  185. int resul = 0;
  186. await this.context.Utilities.PageEachAsync(datas, Size, async item =>
  187. {
  188. resul += await _BulkMerge(item, updateColumns, whereColumns);
  189. });
  190. return resul;
  191. }
  192. else
  193. {
  194. return await _BulkMerge(datas, updateColumns, whereColumns);
  195. }
  196. }
  197. public int BulkMerge(List<T> datas, string[] whereColumns, string[] updateColumns)
  198. {
  199. return BulkMergeAsync(datas, whereColumns, updateColumns).GetAwaiter().GetResult();
  200. }
  201. private async Task<int> _BulkMerge(List<T> datas, string[] updateColumns, string[] whereColumns)
  202. {
  203. try
  204. {
  205. Begin(datas, false, true);
  206. Check.Exception(whereColumns == null || whereColumns.Count() == 0, "where columns count=0 or need primary key");
  207. Check.Exception(whereColumns == null || whereColumns.Count() == 0, "where columns count=0 or need primary key");
  208. var isAuto = this.context.CurrentConnectionConfig.IsAutoCloseConnection;
  209. this.context.CurrentConnectionConfig.IsAutoCloseConnection = false;
  210. DataTable dt = ToDdateTable(datas);
  211. IFastBuilder buider = GetBuider();
  212. buider.Context = context;
  213. if (buider?.DbFastestProperties?.IsMerge == true)
  214. {
  215. await buider.CreateTempAsync<T>(dt);
  216. await buider.ExecuteBulkCopyAsync(dt);
  217. }
  218. var result = await buider.Merge(GetTableName(), dt, this.entityInfo, whereColumns, updateColumns, datas);
  219. //var queryTemp = this.context.Queryable<T>().AS(dt.TableName).ToList();//test
  220. //var result = await buider.UpdateByTempAsync(GetTableName(), dt.TableName, updateColumns, whereColumns);
  221. if (buider?.DbFastestProperties?.IsMerge == true && this.context.CurrentConnectionConfig.DbType != DbType.Sqlite)
  222. {
  223. this.context.DbMaintenance.DropTable(dt.TableName);
  224. }
  225. this.context.CurrentConnectionConfig.IsAutoCloseConnection = isAuto;
  226. buider.CloseDb();
  227. End(datas, false, true);
  228. return result;
  229. }
  230. catch (Exception)
  231. {
  232. this.context.Close();
  233. throw;
  234. }
  235. }
  236. #endregion
  237. #region Core
  238. private async Task<int> _BulkUpdate(List<T> datas, string[] whereColumns, string[] updateColumns)
  239. {
  240. try
  241. {
  242. Begin(datas, false);
  243. Check.Exception(whereColumns == null || whereColumns.Count() == 0, "where columns count=0 or need primary key");
  244. Check.Exception(updateColumns == null || updateColumns.Count() == 0, "set columns count=0");
  245. var isAuto = this.context.CurrentConnectionConfig.IsAutoCloseConnection;
  246. this.context.CurrentConnectionConfig.IsAutoCloseConnection = false;
  247. DataTable dt = ToDdateTable(datas);
  248. IFastBuilder buider = GetBuider();
  249. ActionIgnoreColums(whereColumns, updateColumns, dt, buider.IsActionUpdateColumns);
  250. buider.Context = context;
  251. await buider.CreateTempAsync<T>(dt);
  252. await buider.ExecuteBulkCopyAsync(dt);
  253. //var queryTemp = this.context.Queryable<T>().AS(dt.TableName).ToList();//test
  254. var result = await buider.UpdateByTempAsync(GetTableName(), dt.TableName, updateColumns, whereColumns);
  255. if (this.context.CurrentConnectionConfig.DbType != DbType.Sqlite)
  256. {
  257. this.context.DbMaintenance.DropTable(dt.TableName);
  258. }
  259. this.context.CurrentConnectionConfig.IsAutoCloseConnection = isAuto;
  260. buider.CloseDb();
  261. End(datas, false);
  262. return result;
  263. }
  264. catch (Exception)
  265. {
  266. this.context.Close();
  267. throw;
  268. }
  269. }
  270. private void ActionIgnoreColums(string[] whereColumns, string[] updateColumns, DataTable dt,bool IsActionUpdateColumns)
  271. {
  272. if (entityInfo.Columns.Where(it => it.IsIgnore == false).Count() > whereColumns.Length + updateColumns.Length &&IsActionUpdateColumns)
  273. {
  274. var ignoreColums = dt.Columns.Cast<DataColumn>()
  275. .Where(it => !whereColumns.Any(y => y.EqualCase(it.ColumnName)))
  276. .Where(it => !updateColumns.Any(y => y.EqualCase(it.ColumnName))).ToList();
  277. foreach (DataRow item in dt.Rows)
  278. {
  279. foreach (var col in ignoreColums)
  280. {
  281. if (item[col.ColumnName].IsNullOrEmpty())
  282. {
  283. if (col.DataType == UtilConstants.StringType)
  284. {
  285. item[col.ColumnName] = string.Empty;
  286. }
  287. else if (col.DataType == UtilConstants.DateType)
  288. {
  289. item[col.ColumnName] =UtilMethods.GetMinDate(this.context.CurrentConnectionConfig);
  290. }
  291. else
  292. {
  293. item[col.ColumnName] = Activator.CreateInstance(col.DataType);
  294. }
  295. }
  296. }
  297. }
  298. }
  299. }
  300. private async Task<int> _BulkUpdate(string tableName,DataTable dataTable, string[] whereColumns, string[] updateColumns)
  301. {
  302. var datas = new string[dataTable.Rows.Count].ToList();
  303. Begin(datas, false);
  304. Check.Exception(whereColumns == null || whereColumns.Count() == 0, "where columns count=0 or need primary key");
  305. Check.Exception(updateColumns == null || updateColumns.Count() == 0, "set columns count=0");
  306. var isAuto = this.context.CurrentConnectionConfig.IsAutoCloseConnection;
  307. this.context.CurrentConnectionConfig.IsAutoCloseConnection = false;
  308. dataTable.TableName = this.queryable.SqlBuilder.GetTranslationTableName(tableName);
  309. DataTable dt = GetCopyWriteDataTableUpdate(dataTable);
  310. IFastBuilder buider = GetBuider();
  311. if (dt.Columns.Count != dataTable.Columns.Count)
  312. {
  313. ActionIgnoreColums(whereColumns, updateColumns, dt, buider.IsActionUpdateColumns);
  314. }
  315. buider.Context = context;
  316. if (buider.DbFastestProperties == null)
  317. {
  318. buider.DbFastestProperties = new DbFastestProperties();
  319. }
  320. buider.DbFastestProperties.WhereColumns = whereColumns;
  321. await buider.CreateTempAsync<object>(dt);
  322. await buider.ExecuteBulkCopyAsync(dt);
  323. //var queryTemp = this.context.Queryable<T>().AS(dt.TableName).ToList();//test
  324. var result = await buider.UpdateByTempAsync(GetTableName(), dt.TableName, updateColumns, whereColumns);
  325. this.context.DbMaintenance.DropTable(dt.TableName);
  326. this.context.CurrentConnectionConfig.IsAutoCloseConnection = isAuto;
  327. buider.CloseDb();
  328. End(datas, false);
  329. return result;
  330. }
  331. private async Task<int> _BulkCopy(List<T> datas)
  332. {
  333. Begin(datas,true);
  334. DataTable dt = ToDdateTable(datas);
  335. IFastBuilder buider =GetBuider();
  336. buider.Context = context;
  337. var result = await buider.ExecuteBulkCopyAsync(dt);
  338. End(datas,true);
  339. return result;
  340. }
  341. private async Task<int> _BulkCopy(string tableName,DataTable dataTable)
  342. {
  343. var datas =new string[dataTable.Rows.Count].ToList();
  344. Begin(datas, true);
  345. DataTable dt = dataTable;
  346. dt.TableName =this.queryable.SqlBuilder.GetTranslationTableName(tableName);
  347. dt = GetCopyWriteDataTable(dt);
  348. IFastBuilder buider = GetBuider();
  349. buider.Context = context;
  350. var result = await buider.ExecuteBulkCopyAsync(dt);
  351. End(datas, true);
  352. return result;
  353. }
  354. #endregion
  355. #region AOP
  356. private void End<Type>(List<Type> datas,bool isAdd,bool isMerge=false)
  357. {
  358. var title = isAdd ? "BulkCopy" : "BulkUpdate";
  359. if (isMerge)
  360. {
  361. title = "BulkMerge";
  362. }
  363. this.context.Ado.IsEnableLogEvent = isLog;
  364. if (this.context.CurrentConnectionConfig?.AopEvents?.OnLogExecuted != null)
  365. {
  366. this.context.CurrentConnectionConfig?.AopEvents?.OnLogExecuted($"End {title} name:{GetTableName()} ,count: {datas.Count},current time: {DateTime.Now}", new SugarParameter[] { });
  367. }
  368. RemoveCache();
  369. }
  370. private void Begin<Type>(List<Type> datas,bool isAdd, bool isMerge = false)
  371. {
  372. var title = isAdd ? "BulkCopy" : "BulkUpdate";
  373. if (isMerge)
  374. {
  375. title = "BulkMerge";
  376. }
  377. isLog = this.context.Ado.IsEnableLogEvent;
  378. this.context.Ado.IsEnableLogEvent = false;
  379. if (this.context.CurrentConnectionConfig?.AopEvents?.OnLogExecuting != null)
  380. {
  381. this.context.CurrentConnectionConfig?.AopEvents?.OnLogExecuting($"Begin {title} name:{GetTableName()} ,count: {datas.Count},current time: {DateTime.Now} ", new SugarParameter[] { });
  382. }
  383. var dataEvent = this.context.CurrentConnectionConfig.AopEvents?.DataExecuting;
  384. if (IsDataAop&&dataEvent!=null)
  385. {
  386. var entity = this.context.EntityMaintenance.GetEntityInfo(typeof(Type));
  387. foreach (var item in datas)
  388. {
  389. DataAop(item, isAdd
  390. ?
  391. DataFilterType.InsertByObject:
  392. DataFilterType.UpdateByObject
  393. , entity);
  394. }
  395. }
  396. }
  397. private void DataAop<Type>(Type item, DataFilterType type,EntityInfo entity)
  398. {
  399. var dataEvent = this.context.CurrentConnectionConfig.AopEvents?.DataExecuting;
  400. if (dataEvent != null && item != null)
  401. {
  402. foreach (var columnInfo in entity.Columns)
  403. {
  404. dataEvent(columnInfo.PropertyInfo.GetValue(item, null), new DataFilterModel() { OperationType = type, EntityValue = item, EntityColumnInfo = columnInfo });
  405. }
  406. }
  407. }
  408. #endregion
  409. }
  410. }