using ORM; using SqlSugar; using System.Linq.Expressions; using Universal; namespace SqlSugarORM; public class SqlSugarCustom { public SqlSugarCustom() { this._logQueue = new(LogQueueHandler); } #region Internal private IOrmProvider? _provider; public 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 public bool Initialize(IOrmProvider? notify) { if (_provider is not null) return false; _provider = notify; return true; } public bool Open(string connectionString, SqlSugar.DbType dbType, bool isAutoConnection) { if (this._Client is not null) return false; ConnectionConfig config = new() { ConnectionString = connectionString, DbType = dbType, IsAutoCloseConnection = isAutoConnection }; try { SqlSugarClient Db = new(config, Config); Db.DbMaintenance.CreateDatabase(); this._Client = Db; } catch { return false; } return true; } public bool 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; } public bool CreateTable(string? tableName) { if (this._Client is null) return false; try { if (string.IsNullOrEmpty(tableName)) this._Client.CodeFirst.InitTables(); else this._Client.CodeFirst.As(typeof(T), tableName).InitTables(); } catch { _logQueue?.Enqueue(new($"Create Table {tableName} Failed", DateTime.Now, LogLevel.Error)); return false; } return true; } public bool Insert(T data) where T : class, new() { 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; } public bool Insert(string tablename, T data) where T : class, new() { 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; } public async Task Query(Action> results) { if (this._Client is null) return false; return await Task.Factory.StartNew(() => { try { results?.Invoke(this._Client.Queryable().ToList()); } catch { _logQueue?.Enqueue(new($"Query {typeof(T)} Failed", DateTime.Now, LogLevel.Error)); return false; } return true; }); } public async Task Query(string tableName, Action> results) { if (this._Client is null) return false; return await Task.Factory.StartNew(() => { try { if (string.IsNullOrEmpty(tableName)) results?.Invoke(this._Client.Queryable().ToList()); else results?.Invoke(this._Client.Queryable().AS(tableName).ToList()); } catch { _logQueue?.Enqueue(new($"Query {typeof(T)} Failed", DateTime.Now, LogLevel.Error)); return false; } return true; }); } public async Task Query(Expression> expression, Action> results) { if (this._Client is null) return false; return await Task.Factory.StartNew(() => { try { results?.Invoke(this._Client.Queryable().Where(expression).ToList()); } catch { _logQueue?.Enqueue(new($"Query {typeof(T)} Failed", DateTime.Now, LogLevel.Error)); return false; } return true; }); } public async Task Query(string tableName, Expression> expression, Action> results) { if (this._Client is null) return false; return await Task.Factory.StartNew(() => { try { if (string.IsNullOrEmpty(tableName)) results?.Invoke(this._Client.Queryable().Where(expression).ToList()); else results?.Invoke(this._Client.Queryable().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; }); } public bool Delete(string tableName, Expression> expression) where T : class, new() { if (this._Client is null) return false; try { this._Client.Deleteable().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 }