You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

211 lines
6.8 KiB

2 years ago
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq.Expressions;
  4. namespace SqlSugar
  5. {
  6. public partial class SimpleClient
  7. {
  8. protected SqlSugarClient Context { get; set; }
  9. public SqlSugarClient FullClient { get { return this.Context; } }
  10. private SimpleClient()
  11. {
  12. }
  13. public SimpleClient(SqlSugarClient context)
  14. {
  15. this.Context = context;
  16. }
  17. public T GetById<T>(dynamic id) where T : class, new()
  18. {
  19. return Context.Queryable<T>().InSingle(id);
  20. }
  21. public List<T> GetList<T>() where T : class, new()
  22. {
  23. return Context.Queryable<T>().ToList();
  24. }
  25. public List<T> GetList<T>(Expression<Func<T, bool>> whereExpression) where T : class, new()
  26. {
  27. return Context.Queryable<T>().Where(whereExpression).ToList();
  28. }
  29. public List<T> GetPageList<T>(Expression<Func<T, bool>> whereExpression, PageModel page) where T : class, new()
  30. {
  31. var count = 0;
  32. var result = Context.Queryable<T>().Where(whereExpression).ToPageList(page.PageIndex, page.PageSize, ref count);
  33. page.Total = count;
  34. page.DataList = result;
  35. return result;
  36. }
  37. public List<T> GetPageList<T>(List<IConditionalModel> conditionalList, PageModel page) where T : class, new()
  38. {
  39. var count = 0;
  40. var result = Context.Queryable<T>().Where(conditionalList).ToPageList(page.PageIndex, page.PageSize, ref count);
  41. page.Total = count;
  42. page.DataList = result;
  43. return result;
  44. }
  45. public bool IsAny<T>(Expression<Func<T, bool>> whereExpression) where T : class, new()
  46. {
  47. return Context.Queryable<T>().Where(whereExpression).Any();
  48. }
  49. public bool Insert<T>(T insertObj) where T : class, new()
  50. {
  51. return this.Context.Insertable(insertObj).ExecuteCommand() > 0;
  52. }
  53. public int InsertReturnIdentity<T>(T insertObj) where T : class, new()
  54. {
  55. return this.Context.Insertable(insertObj).ExecuteReturnIdentity();
  56. }
  57. public bool InsertRange<T>(T[] insertObjs) where T : class, new()
  58. {
  59. return this.Context.Insertable(insertObjs).ExecuteCommand() > 0;
  60. }
  61. public bool InsertRange<T>(List<T>[] insertObjs) where T : class, new()
  62. {
  63. return this.Context.Insertable(insertObjs).ExecuteCommand() > 0;
  64. }
  65. public bool Update<T>(T updateObj) where T : class, new()
  66. {
  67. return this.Context.Updateable(updateObj).ExecuteCommand() > 0;
  68. }
  69. public bool Update<T>(Expression<Func<T, T>> columns, Expression<Func<T, bool>> whereExpression) where T : class, new()
  70. {
  71. return this.Context.Updateable<T>().UpdateColumns(columns).Where(whereExpression).ExecuteCommand() > 0;
  72. }
  73. public bool Delete<T>(T deleteObj) where T : class, new()
  74. {
  75. return this.Context.Deleteable<T>().Where(deleteObj).ExecuteCommand() > 0;
  76. }
  77. public bool Delete<T>(Expression<Func<T, bool>> whereExpression) where T : class, new()
  78. {
  79. return this.Context.Deleteable<T>().Where(whereExpression).ExecuteCommand() > 0;
  80. }
  81. public bool DeleteById<T>(dynamic id) where T : class, new()
  82. {
  83. return this.Context.Deleteable<T>().In(id).ExecuteCommand() > 0;
  84. }
  85. public bool DeleteByIds<T>(dynamic[] ids) where T : class, new()
  86. {
  87. return this.Context.Deleteable<T>().In(ids).ExecuteCommand() > 0;
  88. }
  89. }
  90. public partial class SimpleClient<T> where T : class, new()
  91. {
  92. protected SqlSugarClient Context { get; set; }
  93. public SqlSugarClient FullClient { get { return this.Context; } }
  94. private SimpleClient()
  95. {
  96. }
  97. public SimpleClient(SqlSugarClient context)
  98. {
  99. this.Context = context;
  100. }
  101. public T GetById(dynamic id)
  102. {
  103. return Context.Queryable<T>().InSingle(id);
  104. }
  105. public List<T> GetList()
  106. {
  107. return Context.Queryable<T>().ToList();
  108. }
  109. public List<T> GetList(Expression<Func<T, bool>> whereExpression)
  110. {
  111. return Context.Queryable<T>().Where(whereExpression).ToList();
  112. }
  113. public List<T> GetPageList(Expression<Func<T, bool>> whereExpression, PageModel page)
  114. {
  115. var count = 0;
  116. var result = Context.Queryable<T>().Where(whereExpression).ToPageList(page.PageIndex, page.PageSize, ref count);
  117. page.Total = count;
  118. page.DataList = result;
  119. return result;
  120. }
  121. public List<T> GetPageList(List<IConditionalModel> conditionalList, PageModel page)
  122. {
  123. var count = 0;
  124. var result = Context.Queryable<T>().Where(conditionalList).ToPageList(page.PageIndex, page.PageSize, ref count);
  125. page.Total = count;
  126. page.DataList = result;
  127. return result;
  128. }
  129. public bool IsAny(Expression<Func<T, bool>> whereExpression)
  130. {
  131. return Context.Queryable<T>().Where(whereExpression).Any();
  132. }
  133. public bool Insert(T insertObj)
  134. {
  135. return this.Context.Insertable(insertObj).ExecuteCommand() > 0;
  136. }
  137. public int InsertReturnIdentity(T insertObj)
  138. {
  139. return this.Context.Insertable(insertObj).ExecuteReturnIdentity();
  140. }
  141. public bool InsertRange(T[] insertObjs)
  142. {
  143. return this.Context.Insertable(insertObjs).ExecuteCommand() > 0;
  144. }
  145. public bool InsertRange(List<T>[] insertObjs)
  146. {
  147. return this.Context.Insertable(insertObjs).ExecuteCommand() > 0;
  148. }
  149. public bool Update(T updateObj)
  150. {
  151. return this.Context.Updateable(updateObj).ExecuteCommand() > 0;
  152. }
  153. public bool Update(Expression<Func<T, T>> columns, Expression<Func<T, bool>> whereExpression)
  154. {
  155. return this.Context.Updateable<T>().UpdateColumns(columns).Where(whereExpression).ExecuteCommand() > 0;
  156. }
  157. public bool Delete(T deleteObj)
  158. {
  159. return this.Context.Deleteable<T>().Where(deleteObj).ExecuteCommand() > 0;
  160. }
  161. public bool Delete(Expression<Func<T, bool>> whereExpression)
  162. {
  163. return this.Context.Deleteable<T>().Where(whereExpression).ExecuteCommand() > 0;
  164. }
  165. public bool DeleteById(dynamic id)
  166. {
  167. return this.Context.Deleteable<T>().In(id).ExecuteCommand() > 0;
  168. }
  169. public bool DeleteByIds(dynamic[] ids)
  170. {
  171. return this.Context.Deleteable<T>().In(ids).ExecuteCommand() > 0;
  172. }
  173. }
  174. }