SqlSugarCustom.cs 7.7 KB

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