123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286 |
- using ORM;
- using SqlSugar;
- using System.Linq.Expressions;
- using Universal;
- namespace SqlSugarORM;
- public class SqlSugarCustom : IORM
- {
- public SqlSugarCustom()
- {
- this._logQueue = new(LogQueueHandler);
- }
- #region Internal
- private IOrmProvider? _provider;
- private SqlSugarClient? _Client;
- private bool disposedValue;
- private readonly EventQueue<(string, DateTime, LogLevel)> _logQueue;
- private void LogQueueHandler((string log, DateTime time, LogLevel level) logItem)
- {
- _provider?.Log(logItem.log, logItem.time, logItem.level);
- }
- private void Config(SqlSugarClient client)
- {
- client.Aop.OnLogExecuting = SqlLog;
- }
- private void SqlLog(string sql, SugarParameter[] paras)
- {
- string log = UtilMethods.GetNativeSql(sql, paras);
- _logQueue?.Enqueue(new(log, DateTime.Now, LogLevel.Original));
- }
- #endregion
- bool IORM.Initialize(IOrmProvider? notify)
- {
- if (_provider is not null)
- return false;
- _provider = notify;
- return true;
- }
- bool IORM.Open(string connectionString, ORM.DbType dbType, bool isAutoConnection)
- {
- if (this._Client is not null)
- return false;
- ConnectionConfig config = new()
- {
- ConnectionString = connectionString,
- DbType = (SqlSugar.DbType)dbType,
- IsAutoCloseConnection = isAutoConnection
- };
- try
- {
- SqlSugarClient Db = new(config, Config);
- Db.DbMaintenance.CreateDatabase();
- this._Client = Db;
- }
- catch
- {
- return false;
- }
- return true;
- }
- bool IORM.CreateDataBase(string dbName)
- {
- if (this._Client is null)
- return false;
- if (string.IsNullOrEmpty(dbName))
- return false;
- try
- {
- this._Client.DbMaintenance.CreateDatabase(dbName);
- }
- catch
- {
- _logQueue?.Enqueue(new($"Create DB {dbName} Failed", DateTime.Now, LogLevel.Error));
- return false;
- }
- return true;
- }
- bool IORM.CreateTable<T>(string? tableName)
- {
- if (this._Client is null)
- return false;
- try
- {
- if (string.IsNullOrEmpty(tableName))
- this._Client.CodeFirst.InitTables<T>();
- else
- this._Client.CodeFirst.As(typeof(T), tableName).InitTables<T>();
- }
- catch
- {
- _logQueue?.Enqueue(new($"Create Table {tableName} Failed", DateTime.Now, LogLevel.Error));
- return false;
- }
- return true;
- }
- bool IORM.Insert<T>(T data)
- {
- if (this._Client is null)
- return false;
- try
- {
- this._Client.Insertable(data).ExecuteCommand();
- }
- catch
- {
- _logQueue?.Enqueue(new($"Insert {data.ToString} Failed", DateTime.Now, LogLevel.Error));
- return false;
- }
- return true;
- }
- bool IORM.Insert<T>(string tablename, T data)
- {
- if (this._Client is null)
- return false;
- try
- {
- if (string.IsNullOrEmpty(tablename))
- this._Client.Insertable(data).ExecuteCommand();
- else
- this._Client.Insertable(data).AS(tablename).ExecuteCommand();
- }
- catch
- {
- if (string.IsNullOrEmpty(tablename))
- _logQueue?.Enqueue(new($"Insert {data.ToString} Failed", DateTime.Now, LogLevel.Error));
- else
- _logQueue?.Enqueue(new($"Insert {data.ToString} to table {tablename} Failed", DateTime.Now, LogLevel.Error));
- return false;
- }
- return true;
- }
- async Task<bool> IORM.Query<T>(Action<List<T>> results)
- {
- if (this._Client is null)
- return false;
- return await Task<bool>.Factory.StartNew(() =>
- {
- try
- {
- results?.Invoke(this._Client.Queryable<T>().ToList());
- }
- catch
- {
- _logQueue?.Enqueue(new($"Query {typeof(T)} Failed", DateTime.Now, LogLevel.Error));
- return false;
- }
- return true;
- });
- }
- async Task<bool> IORM.Query<T>(string tableName, Action<List<T>> results)
- {
- if (this._Client is null)
- return false;
- return await Task<bool>.Factory.StartNew(() =>
- {
- try
- {
- if (string.IsNullOrEmpty(tableName))
- results?.Invoke(this._Client.Queryable<T>().ToList());
- else
- results?.Invoke(this._Client.Queryable<T>().AS(tableName).ToList());
- }
- catch
- {
- _logQueue?.Enqueue(new($"Query {typeof(T)} Failed", DateTime.Now, LogLevel.Error));
- return false;
- }
- return true;
- });
- }
- async Task<bool> IORM.Query<T>(Expression<Func<T, bool>> expression, Action<List<T>> results)
- {
- if (this._Client is null)
- return false;
- return await Task<bool>.Factory.StartNew(() =>
- {
- try
- {
- results?.Invoke(this._Client.Queryable<T>().Where(expression).ToList());
- }
- catch
- {
- _logQueue?.Enqueue(new($"Query {typeof(T)} Failed", DateTime.Now, LogLevel.Error));
- return false;
- }
- return true;
- });
- }
- async Task<bool> IORM.Query<T>(string tableName, Expression<Func<T, bool>> expression, Action<List<T>> results)
- {
- if (this._Client is null)
- return false;
- return await Task<bool>.Factory.StartNew(() =>
- {
- try
- {
- if (string.IsNullOrEmpty(tableName))
- results?.Invoke(this._Client.Queryable<T>().Where(expression).ToList());
- else
- results?.Invoke(this._Client.Queryable<T>().AS(tableName).Where(expression).ToList());
- }
- catch
- {
- if (string.IsNullOrEmpty(tableName))
- _logQueue?.Enqueue(new($"Query {typeof(T)} Failed", DateTime.Now, LogLevel.Error));
- else
- _logQueue?.Enqueue(new($"Query {typeof(T)} from Table {tableName} Failed", DateTime.Now, LogLevel.Error));
- return false;
- }
- return true;
- });
- }
- bool IORM.Delete<T>(string tableName, Expression<Func<T, bool>> expression)
- {
- if (this._Client is null)
- return false;
- try
- {
- this._Client.Deleteable<T>().AS(tableName).Where(expression).ExecuteCommand();
- }
- catch
- {
- return false;
- }
- return true;
- }
- #region Disopse
- protected virtual void Dispose(bool disposing)
- {
- if (!disposedValue)
- {
- if (disposing)
- {
- while (_logQueue is not null && _logQueue.Count != 0)
- Thread.Sleep(200);
- this._Client?.Dispose();
- this._Client = null;
- this._logQueue?.Dispose();
- this._provider = null;
- }
- disposedValue = true;
- }
- }
- public void Dispose()
- {
- Dispose(disposing: true);
- GC.SuppressFinalize(this);
- }
- #endregion
- }
|