SqlSugarCustom.cs 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374
  1. using ORM;
  2. using SqlSugar;
  3. using System.Linq.Expressions;
  4. using Universal;
  5. namespace SqlSugarORM;
  6. public class SqlSugarCustom : IORM
  7. {
  8. public SqlSugarCustom()
  9. {
  10. this._logQueue = new(LogQueueHandler);
  11. }
  12. #region Internal
  13. private IOrmProvider? _provider;
  14. private 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. bool IORM.Initialize(IOrmProvider? notify)
  32. {
  33. if (_provider is not null)
  34. return false;
  35. _provider = notify;
  36. return true;
  37. }
  38. bool IORM.Open(string connectionString, ORM.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 = (SqlSugar.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. bool IORM.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. bool IORM.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. bool IORM.Insert<T>(T data)
  96. {
  97. if (this._Client is null)
  98. return false;
  99. try
  100. {
  101. this._Client.Insertable(data).ExecuteCommand();
  102. }
  103. catch
  104. {
  105. _logQueue?.Enqueue(new($"Insert {data.ToString} Failed", DateTime.Now, LogLevel.Error));
  106. return false;
  107. }
  108. return true;
  109. }
  110. bool IORM.Insert<T>(string tablename, T data)
  111. {
  112. if (this._Client is null)
  113. return false;
  114. try
  115. {
  116. if (string.IsNullOrEmpty(tablename))
  117. this._Client.Insertable(data).ExecuteCommand();
  118. else
  119. this._Client.Insertable(data).AS(tablename).ExecuteCommand();
  120. }
  121. catch
  122. {
  123. if (string.IsNullOrEmpty(tablename))
  124. _logQueue?.Enqueue(new($"Insert {data.ToString} Failed", DateTime.Now, LogLevel.Error));
  125. else
  126. _logQueue?.Enqueue(new($"Insert {data.ToString} to table {tablename} Failed", DateTime.Now, LogLevel.Error));
  127. return false;
  128. }
  129. return true;
  130. }
  131. async Task<bool> IORM.Query<T>(Action<List<T>> results)
  132. {
  133. if (this._Client is null)
  134. return false;
  135. return await Task<bool>.Factory.StartNew(() =>
  136. {
  137. try
  138. {
  139. results?.Invoke(this._Client.Queryable<T>().ToList());
  140. }
  141. catch
  142. {
  143. _logQueue?.Enqueue(new($"Query {typeof(T)} Failed", DateTime.Now, LogLevel.Error));
  144. return false;
  145. }
  146. return true;
  147. });
  148. }
  149. async Task<bool> IORM.Query<T>(string tableName, Action<List<T>> results)
  150. {
  151. if (this._Client is null)
  152. return false;
  153. return await Task<bool>.Factory.StartNew(() =>
  154. {
  155. try
  156. {
  157. if (string.IsNullOrEmpty(tableName))
  158. results?.Invoke(this._Client.Queryable<T>().ToList());
  159. else
  160. results?.Invoke(this._Client.Queryable<T>().AS(tableName).ToList());
  161. }
  162. catch
  163. {
  164. _logQueue?.Enqueue(new($"Query {typeof(T)} Failed", DateTime.Now, LogLevel.Error));
  165. return false;
  166. }
  167. return true;
  168. });
  169. }
  170. bool IORM.Query<T>(string tableName, out List<T>? results)
  171. {
  172. results = null;
  173. if (this._Client is null)
  174. return false;
  175. try
  176. {
  177. results = string.IsNullOrEmpty(tableName) ?
  178. this._Client.Queryable<T>().ToList() :
  179. this._Client.Queryable<T>().AS(tableName).ToList();
  180. }
  181. catch
  182. {
  183. return false;
  184. }
  185. return true;
  186. }
  187. bool IORM.Query<T>(out List<T>? results)
  188. {
  189. results = null;
  190. if (this._Client is null)
  191. return false;
  192. try
  193. {
  194. results = this._Client.Queryable<T>().ToList();
  195. }
  196. catch
  197. {
  198. return false;
  199. }
  200. return true;
  201. }
  202. bool IORM.Query<T>(string tableName, Expression<Func<T, bool>> expression, out List<T>? results)
  203. {
  204. results = default;
  205. if (this._Client is null)
  206. return false;
  207. try
  208. {
  209. results = string.IsNullOrEmpty(tableName) ?
  210. this._Client.Queryable<T>().Where(expression).ToList() :
  211. this._Client.Queryable<T>().AS(tableName).Where(expression).ToList();
  212. }
  213. catch
  214. {
  215. return false;
  216. }
  217. return true;
  218. }
  219. async Task<bool> IORM.Query<T>(Expression<Func<T, bool>> expression, Action<List<T>> results)
  220. {
  221. if (this._Client is null)
  222. return false;
  223. return await Task<bool>.Factory.StartNew(() =>
  224. {
  225. try
  226. {
  227. results?.Invoke(this._Client.Queryable<T>().Where(expression).ToList());
  228. }
  229. catch
  230. {
  231. _logQueue?.Enqueue(new($"Query {typeof(T)} Failed", DateTime.Now, LogLevel.Error));
  232. return false;
  233. }
  234. return true;
  235. });
  236. }
  237. async Task<bool> IORM.Query<T>(string tableName, Expression<Func<T, bool>> expression, Action<List<T>> results)
  238. {
  239. if (this._Client is null)
  240. return false;
  241. return await Task<bool>.Factory.StartNew(() =>
  242. {
  243. try
  244. {
  245. if (string.IsNullOrEmpty(tableName))
  246. results?.Invoke(this._Client.Queryable<T>().Where(expression).ToList());
  247. else
  248. results?.Invoke(this._Client.Queryable<T>().AS(tableName).Where(expression).ToList());
  249. }
  250. catch
  251. {
  252. if (string.IsNullOrEmpty(tableName))
  253. _logQueue?.Enqueue(new($"Query {typeof(T)} Failed", DateTime.Now, LogLevel.Error));
  254. else
  255. _logQueue?.Enqueue(new($"Query {typeof(T)} from Table {tableName} Failed", DateTime.Now, LogLevel.Error));
  256. return false;
  257. }
  258. return true;
  259. });
  260. }
  261. bool IORM.Delete<T>(string tableName, Expression<Func<T, bool>> expression)
  262. {
  263. if (this._Client is null)
  264. return false;
  265. try
  266. {
  267. this._Client.Deleteable<T>().AS(tableName).Where(expression).ExecuteCommand();
  268. }
  269. catch
  270. {
  271. return false;
  272. }
  273. return true;
  274. }
  275. bool IORM.AddOrUpdate<T>(string tableName, T data, Expression<Func<T, bool>> expression)
  276. {
  277. if (this._Client is null)
  278. return false;
  279. try
  280. {
  281. if (string.IsNullOrEmpty(tableName))
  282. {
  283. if (this._Client.Updateable(data).Where(expression).ExecuteCommand() == 0)
  284. return (this as IORM).Insert(data);
  285. }
  286. else
  287. {
  288. if (this._Client.Updateable(data).Where(expression).AS(tableName).ExecuteCommand() == 0)
  289. return (this as IORM).Insert(tableName, data);
  290. }
  291. }
  292. catch
  293. {
  294. if (string.IsNullOrEmpty(tableName))
  295. _logQueue?.Enqueue(new($"Update {data.ToString} Failed", DateTime.Now, LogLevel.Error));
  296. else
  297. _logQueue?.Enqueue(new($"Update {data.ToString} to table {tableName} Failed", DateTime.Now, LogLevel.Error));
  298. return false;
  299. }
  300. return true;
  301. }
  302. #region Disopse
  303. protected virtual void Dispose(bool disposing)
  304. {
  305. if (!disposedValue)
  306. {
  307. if (disposing)
  308. {
  309. while (_logQueue is not null && _logQueue.Count != 0)
  310. Thread.Sleep(200);
  311. this._Client?.Dispose();
  312. this._Client = null;
  313. this._logQueue?.Dispose();
  314. this._provider = null;
  315. }
  316. disposedValue = true;
  317. }
  318. }
  319. public void Dispose()
  320. {
  321. Dispose(disposing: true);
  322. GC.SuppressFinalize(this);
  323. }
  324. #endregion
  325. }