SqlSugarCustom.cs 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  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. async Task<bool> IORM.Query<T>(Expression<Func<T, bool>> expression, Action<List<T>> results)
  171. {
  172. if (this._Client is null)
  173. return false;
  174. return await Task<bool>.Factory.StartNew(() =>
  175. {
  176. try
  177. {
  178. results?.Invoke(this._Client.Queryable<T>().Where(expression).ToList());
  179. }
  180. catch
  181. {
  182. _logQueue?.Enqueue(new($"Query {typeof(T)} Failed", DateTime.Now, LogLevel.Error));
  183. return false;
  184. }
  185. return true;
  186. });
  187. }
  188. async Task<bool> IORM.Query<T>(string tableName, Expression<Func<T, bool>> expression, Action<List<T>> results)
  189. {
  190. if (this._Client is null)
  191. return false;
  192. return await Task<bool>.Factory.StartNew(() =>
  193. {
  194. try
  195. {
  196. if (string.IsNullOrEmpty(tableName))
  197. results?.Invoke(this._Client.Queryable<T>().Where(expression).ToList());
  198. else
  199. results?.Invoke(this._Client.Queryable<T>().AS(tableName).Where(expression).ToList());
  200. }
  201. catch
  202. {
  203. if (string.IsNullOrEmpty(tableName))
  204. _logQueue?.Enqueue(new($"Query {typeof(T)} Failed", DateTime.Now, LogLevel.Error));
  205. else
  206. _logQueue?.Enqueue(new($"Query {typeof(T)} from Table {tableName} Failed", DateTime.Now, LogLevel.Error));
  207. return false;
  208. }
  209. return true;
  210. });
  211. }
  212. bool IORM.Delete<T>(string tableName, Expression<Func<T, bool>> expression)
  213. {
  214. if (this._Client is null)
  215. return false;
  216. try
  217. {
  218. this._Client.Deleteable<T>().AS(tableName).Where(expression).ExecuteCommand();
  219. }
  220. catch
  221. {
  222. return false;
  223. }
  224. return true;
  225. }
  226. #region Disopse
  227. protected virtual void Dispose(bool disposing)
  228. {
  229. if (!disposedValue)
  230. {
  231. if (disposing)
  232. {
  233. while (_logQueue is not null && _logQueue.Count != 0)
  234. Thread.Sleep(200);
  235. this._Client?.Dispose();
  236. this._Client = null;
  237. this._logQueue?.Dispose();
  238. this._provider = null;
  239. }
  240. disposedValue = true;
  241. }
  242. }
  243. public void Dispose()
  244. {
  245. Dispose(disposing: true);
  246. GC.SuppressFinalize(this);
  247. }
  248. #endregion
  249. }