SaveableProvider.cs 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  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.Tasks;
  7. namespace SqlSugar
  8. {
  9. public partial class SaveableProvider<T> : ISaveable<T> where T : class, new()
  10. {
  11. internal SaveableProvider(SqlSugarProvider context,List<T> saveObjects)
  12. {
  13. this.saveObjects = saveObjects;
  14. this.Context = context;
  15. this.Context.InitMappingInfo<T>();
  16. }
  17. internal SaveableProvider(SqlSugarProvider context, T saveObject)
  18. {
  19. this.saveObjects = new List<T>() { saveObject };
  20. this.Context = context;
  21. this.Context.InitMappingInfo<T>();
  22. }
  23. public SqlSugarProvider Context { get; set; }
  24. public List<T> saveObjects = new List<T>();
  25. public List<T> existsObjects = null;
  26. public List<T> insertObjects
  27. {
  28. get
  29. {
  30. var isDisableMasterSlaveSeparation = this.Context.Ado.IsDisableMasterSlaveSeparation;
  31. this.Context.Ado.IsDisableMasterSlaveSeparation = true;
  32. List<T> result = new List<T>();
  33. var pks = GetPrimaryKeys();
  34. Check.Exception(pks.IsNullOrEmpty(), "Need primary key");
  35. Check.Exception(pks.Count() > 1, "Multiple primary keys are not supported");
  36. var pkInfo = this.EntityInfo.Columns.Where(it=>it.IsIgnore==false).Where(it => it.DbColumnName.Equals(pks.First(), StringComparison.CurrentCultureIgnoreCase)).First();
  37. var pkValues = saveObjects.Select(it=>it.GetType().GetProperty(pkInfo.PropertyName).GetValue(it,null));
  38. if(existsObjects==null)
  39. existsObjects=this.Context.Queryable<T>().In(pkValues).ToList();
  40. this.Context.Ado.IsDisableMasterSlaveSeparation = isDisableMasterSlaveSeparation;
  41. return saveObjects.Where(it=>!
  42. existsObjects.Any(e=>
  43. e.GetType().GetProperty(pkInfo.PropertyName).GetValue(e,null).ObjToString()
  44. ==
  45. it.GetType().GetProperty(pkInfo.PropertyName).GetValue(it, null).ObjToString())).ToList();
  46. }
  47. }
  48. public List<T> updatObjects
  49. {
  50. get
  51. {
  52. var isDisableMasterSlaveSeparation = this.Context.Ado.IsDisableMasterSlaveSeparation;
  53. this.Context.Ado.IsDisableMasterSlaveSeparation = true;
  54. List<T> result = new List<T>();
  55. var pks = GetPrimaryKeys();
  56. Check.Exception(pks.IsNullOrEmpty(), "Need primary key");
  57. Check.Exception(pks.Count() > 1, "Multiple primary keys are not supported");
  58. var pkInfo = this.EntityInfo.Columns.Where(it => it.IsIgnore == false).Where(it => it.DbColumnName.Equals(pks.First(), StringComparison.CurrentCultureIgnoreCase)).First();
  59. var pkValues = saveObjects.Select(it => it.GetType().GetProperty(pkInfo.PropertyName).GetValue(it, null));
  60. if (existsObjects == null)
  61. existsObjects = this.Context.Queryable<T>().In(pkValues).ToList();
  62. this.Context.Ado.IsDisableMasterSlaveSeparation = isDisableMasterSlaveSeparation;
  63. return saveObjects.Where(it =>
  64. existsObjects.Any(e =>
  65. e.GetType().GetProperty(pkInfo.PropertyName).GetValue(e, null).ObjToString()
  66. ==
  67. it.GetType().GetProperty(pkInfo.PropertyName).GetValue(it, null).ObjToString())).ToList();
  68. }
  69. }
  70. public IInsertable<T> insertable { get; set; }
  71. public IUpdateable<T> updateable { get; set; }
  72. public EntityInfo EntityInfo
  73. {
  74. get
  75. {
  76. return this.Context.EntityMaintenance.GetEntityInfo<T>();
  77. }
  78. }
  79. #region Core
  80. public int ExecuteCommand()
  81. {
  82. LoadInsertable();
  83. LoadUpdateable();
  84. var insertCount = 0;
  85. var updateCount = 0;
  86. if (insertable != null)
  87. {
  88. insertCount = insertable.ExecuteCommand();
  89. }
  90. if (updateable != null)
  91. {
  92. updateCount = updateable.ExecuteCommand();
  93. }
  94. return updateCount + insertCount;
  95. }
  96. public T ExecuteReturnEntity()
  97. {
  98. LoadInsertable();
  99. LoadUpdateable();
  100. if (insertable != null)
  101. insertable.ExecuteCommandIdentityIntoEntity();
  102. if (updateable != null)
  103. updateable.ExecuteCommand();
  104. return saveObjects.First();
  105. }
  106. public List<T> ExecuteReturnList()
  107. {
  108. LoadInsertable();
  109. LoadUpdateable();
  110. if (insertable != null)
  111. insertable.ExecuteCommand();
  112. if (updateable != null)
  113. updateable.ExecuteCommand();
  114. return saveObjects;
  115. }
  116. #endregion
  117. #region Core Async
  118. public Task<int> ExecuteCommandAsync()
  119. {
  120. return Task.FromResult(ExecuteCommand());
  121. }
  122. public Task<T> ExecuteReturnEntityAsync()
  123. {
  124. return Task.FromResult(ExecuteReturnEntity());
  125. }
  126. public Task<List<T>> ExecuteReturnListAsync()
  127. {
  128. return Task.FromResult(ExecuteReturnList());
  129. }
  130. #endregion
  131. public ISaveable<T> InsertColumns(Expression<Func<T, object>> columns)
  132. {
  133. LoadInsertable();
  134. if (this.insertable != null)
  135. {
  136. this.insertable.InsertColumns(columns);
  137. }
  138. return this;
  139. }
  140. public ISaveable<T> EnableDiffLogEvent(object businessData = null)
  141. {
  142. LoadInsertable();
  143. LoadUpdateable();
  144. if (this.insertable != null)
  145. {
  146. this.insertable.EnableDiffLogEvent(businessData);
  147. }
  148. if (this.updateable != null)
  149. {
  150. this.updateable.EnableDiffLogEvent(businessData);
  151. }
  152. return this;
  153. }
  154. public ISaveable<T> RemoveDataCache()
  155. {
  156. if (this.insertable != null)
  157. {
  158. this.insertable.RemoveDataCache();
  159. }
  160. if (this.updateable != null)
  161. {
  162. this.updateable.RemoveDataCache();
  163. }
  164. return this;
  165. }
  166. public ISaveable<T> InsertIgnoreColumns(Expression<Func<T, object>> columns)
  167. {
  168. LoadInsertable();
  169. if (this.insertable != null)
  170. {
  171. this.insertable.IgnoreColumns(columns);
  172. }
  173. return this;
  174. }
  175. public ISaveable<T> UpdateColumns(Expression<Func<T, object>> columns)
  176. {
  177. LoadUpdateable();
  178. if (this.updateable != null)
  179. {
  180. this.updateable.UpdateColumns(columns);
  181. }
  182. return this;
  183. }
  184. public ISaveable<T> UpdateIgnoreColumns(Expression<Func<T, object>> columns)
  185. {
  186. LoadUpdateable();
  187. if (this.updateable != null)
  188. {
  189. this.updateable.IgnoreColumns(columns);
  190. }
  191. return this;
  192. }
  193. public ISaveable<T> UpdateWhereColumns(Expression<Func<T, object>> columns)
  194. {
  195. LoadUpdateable();
  196. if (this.updateable != null)
  197. {
  198. this.updateable.WhereColumns(columns);
  199. }
  200. return this;
  201. }
  202. protected virtual List<string> GetPrimaryKeys()
  203. {
  204. if (this.Context.IsSystemTablesConfig)
  205. {
  206. return this.Context.DbMaintenance.GetPrimaries(this.Context.EntityMaintenance.GetTableName(this.EntityInfo.EntityName));
  207. }
  208. else
  209. {
  210. return this.EntityInfo.Columns.Where(it => it.IsPrimarykey).Select(it => it.DbColumnName).ToList();
  211. }
  212. }
  213. private void LoadInsertable()
  214. {
  215. var temp = insertObjects;
  216. if (insertable == null && temp.HasValue())
  217. insertable = this.Context.Insertable<T>(temp);
  218. }
  219. private void LoadUpdateable()
  220. {
  221. var temp = updatObjects;
  222. if (updateable == null && temp.HasValue())
  223. updateable = this.Context.Updateable<T>(temp);
  224. }
  225. }
  226. }