SqlSugarCustom.cs 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342
  1. using SqlSugar;
  2. using System.Linq.Expressions;
  3. namespace SqlSugarORM;
  4. public class SqlSugarCustom
  5. {
  6. public SqlSugarClient? Client { get; private set; }
  7. #region Internal
  8. private bool disposedValue;
  9. private void Config(SqlSugarClient client)
  10. {
  11. client.Aop.OnLogExecuting = SqlLog;
  12. }
  13. private void SqlLog(string sql, SugarParameter[] paras)
  14. {
  15. string log = UtilMethods.GetNativeSql(sql, paras);
  16. }
  17. #endregion
  18. public bool Initialize()
  19. {
  20. return true;
  21. }
  22. public bool Open(string connectionString, SqlSugar.DbType dbType, bool isAutoConnection)
  23. {
  24. if (this.Client is not null)
  25. return false;
  26. ConnectionConfig config = new()
  27. {
  28. ConnectionString = connectionString,
  29. DbType = dbType,
  30. IsAutoCloseConnection = isAutoConnection
  31. };
  32. try
  33. {
  34. SqlSugarClient Db = new(config, Config);
  35. Db.DbMaintenance.CreateDatabase();
  36. this.Client = Db;
  37. }
  38. catch
  39. {
  40. return false;
  41. }
  42. return true;
  43. }
  44. public bool CreateDataBase(string dbName)
  45. {
  46. if (this.Client is null)
  47. return false;
  48. if (string.IsNullOrEmpty(dbName))
  49. return false;
  50. try
  51. {
  52. this.Client.DbMaintenance.CreateDatabase(dbName);
  53. }
  54. catch
  55. {
  56. return false;
  57. }
  58. return true;
  59. }
  60. public bool CreateTable<T>(string? tableName)
  61. {
  62. if (this.Client is null)
  63. return false;
  64. try
  65. {
  66. if (string.IsNullOrEmpty(tableName))
  67. this.Client.CodeFirst.InitTables<T>();
  68. else
  69. this.Client.CodeFirst.As(typeof(T), tableName).InitTables<T>();
  70. }
  71. catch
  72. {
  73. return false;
  74. }
  75. return true;
  76. }
  77. public bool Insert<T>(T data) where T : class, new()
  78. {
  79. if (this.Client is null)
  80. return false;
  81. try
  82. {
  83. this.Client.Insertable(data).ExecuteCommand();
  84. }
  85. catch
  86. {
  87. return false;
  88. }
  89. return true;
  90. }
  91. public bool Insert<T>(string tablename, T data) where T : class, new()
  92. {
  93. if (this.Client is null)
  94. return false;
  95. try
  96. {
  97. if (string.IsNullOrEmpty(tablename))
  98. this.Client.Insertable(data).ExecuteCommand();
  99. else
  100. this.Client.Insertable(data).AS(tablename).ExecuteCommand();
  101. }
  102. catch
  103. {
  104. return false;
  105. }
  106. return true;
  107. }
  108. public async Task<bool> Query<T>(Action<List<T>> results)
  109. {
  110. if (this.Client is null)
  111. return false;
  112. return await Task<bool>.Factory.StartNew(() =>
  113. {
  114. try
  115. {
  116. results?.Invoke(this.Client.Queryable<T>().ToList());
  117. }
  118. catch
  119. {
  120. return false;
  121. }
  122. return true;
  123. });
  124. }
  125. public async Task<bool> Query<T>(string tableName, Action<List<T>> results)
  126. {
  127. if (this.Client is null)
  128. return false;
  129. return await Task<bool>.Factory.StartNew(() =>
  130. {
  131. try
  132. {
  133. if (string.IsNullOrEmpty(tableName))
  134. results?.Invoke(this.Client.Queryable<T>().ToList());
  135. else
  136. results?.Invoke(this.Client.Queryable<T>().AS(tableName).ToList());
  137. }
  138. catch
  139. {
  140. return false;
  141. }
  142. return true;
  143. });
  144. }
  145. public bool Query<T>(string tableName, out List<T>? results)
  146. {
  147. results = null;
  148. if (this.Client is null)
  149. return false;
  150. try
  151. {
  152. results = string.IsNullOrEmpty(tableName) ?
  153. this.Client.Queryable<T>().ToList() :
  154. this.Client.Queryable<T>().AS(tableName).ToList();
  155. }
  156. catch
  157. {
  158. return false;
  159. }
  160. return true;
  161. }
  162. public bool Query<T>(out List<T>? results)
  163. {
  164. results = null;
  165. if (this.Client is null)
  166. return false;
  167. try
  168. {
  169. results = this.Client.Queryable<T>().ToList();
  170. }
  171. catch
  172. {
  173. return false;
  174. }
  175. return true;
  176. }
  177. public bool Query<T>(string tableName, Expression<Func<T, bool>> expression, out List<T>? results)
  178. {
  179. results = default;
  180. if (this.Client is null)
  181. return false;
  182. try
  183. {
  184. results = string.IsNullOrEmpty(tableName) ?
  185. this.Client.Queryable<T>().Where(expression).ToList() :
  186. this.Client.Queryable<T>().AS(tableName).Where(expression).ToList();
  187. }
  188. catch
  189. {
  190. return false;
  191. }
  192. return true;
  193. }
  194. public async Task<bool> Query<T>(Expression<Func<T, bool>> expression, Action<List<T>> results)
  195. {
  196. if (this.Client is null)
  197. return false;
  198. return await Task<bool>.Factory.StartNew(() =>
  199. {
  200. try
  201. {
  202. results?.Invoke(this.Client.Queryable<T>().Where(expression).ToList());
  203. }
  204. catch
  205. {
  206. return false;
  207. }
  208. return true;
  209. });
  210. }
  211. public async Task<bool> Query<T>(string tableName, Expression<Func<T, bool>> expression, Action<List<T>> results)
  212. {
  213. if (this.Client is null)
  214. return false;
  215. return await Task<bool>.Factory.StartNew(() =>
  216. {
  217. try
  218. {
  219. if (string.IsNullOrEmpty(tableName))
  220. results?.Invoke(this.Client.Queryable<T>().Where(expression).ToList());
  221. else
  222. results?.Invoke(this.Client.Queryable<T>().AS(tableName).Where(expression).ToList());
  223. }
  224. catch
  225. {
  226. return false;
  227. }
  228. return true;
  229. });
  230. }
  231. public bool Delete<T>(string tableName, Expression<Func<T, bool>> expression) where T : class, new()
  232. {
  233. if (this.Client is null)
  234. return false;
  235. try
  236. {
  237. this.Client.Deleteable<T>().AS(tableName).Where(expression).ExecuteCommand();
  238. }
  239. catch
  240. {
  241. return false;
  242. }
  243. return true;
  244. }
  245. public bool InsertOrUpdate<T>(string tableName, T data, Expression<Func<T, bool>> expression, out bool isNewData) where T : class, new()
  246. {
  247. isNewData = false;
  248. if (this.Client is null)
  249. return false;
  250. try
  251. {
  252. if (string.IsNullOrEmpty(tableName))
  253. {
  254. if (this.Client.Updateable(data).Where(expression).ExecuteCommand() == 0)
  255. {
  256. isNewData = true;
  257. return this.Insert(data);
  258. }
  259. }
  260. else
  261. {
  262. if (this.Client.Updateable(data).Where(expression).AS(tableName).ExecuteCommand() == 0)
  263. {
  264. isNewData = true;
  265. return this.Insert(tableName, data);
  266. }
  267. }
  268. }
  269. catch
  270. {
  271. return false;
  272. }
  273. return true;
  274. }
  275. #region Disopse
  276. protected virtual void Dispose(bool disposing)
  277. {
  278. if (!disposedValue)
  279. {
  280. if (disposing)
  281. {
  282. this.Client?.Dispose();
  283. this.Client = null;
  284. }
  285. disposedValue = true;
  286. }
  287. }
  288. public void Dispose()
  289. {
  290. Dispose(disposing: true);
  291. GC.SuppressFinalize(this);
  292. }
  293. #endregion
  294. }