using SqlSugar; using System.Linq.Expressions; namespace SqlSugarORM; public class SqlSugarCustom { public SqlSugarClient? Client { get; private set; } #region Internal private bool disposedValue; private void Config(SqlSugarClient client) { client.Aop.OnLogExecuting = SqlLog; } private void SqlLog(string sql, SugarParameter[] paras) { string log = UtilMethods.GetNativeSql(sql, paras); } #endregion public bool Initialize() { 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 { 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 { 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 { 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 { 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 { 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 { return false; } return true; }); } public bool Query(string tableName, out List? results) { results = null; if (this.Client is null) return false; try { results = string.IsNullOrEmpty(tableName) ? this.Client.Queryable().ToList() : this.Client.Queryable().AS(tableName).ToList(); } catch { return false; } return true; } public bool Query(out List? results) { results = null; if (this.Client is null) return false; try { results = this.Client.Queryable().ToList(); } catch { return false; } return true; } public bool Query(string tableName, Expression> expression, out List? results) { results = default; if (this.Client is null) return false; try { results = string.IsNullOrEmpty(tableName) ? this.Client.Queryable().Where(expression).ToList() : this.Client.Queryable().AS(tableName).Where(expression).ToList(); } catch { 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 { 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 { 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; } public bool InsertOrUpdate(string tableName, T data, Expression> expression, out bool isNewData) where T : class, new() { isNewData = false; if (this.Client is null) return false; try { if (string.IsNullOrEmpty(tableName)) { if (this.Client.Updateable(data).Where(expression).ExecuteCommand() == 0) { isNewData = true; return this.Insert(data); } } else { if (this.Client.Updateable(data).Where(expression).AS(tableName).ExecuteCommand() == 0) { isNewData = true; return this.Insert(tableName, data); } } } catch { return false; } return true; } #region Disopse protected virtual void Dispose(bool disposing) { if (!disposedValue) { if (disposing) { this.Client?.Dispose(); this.Client = null; } disposedValue = true; } } public void Dispose() { Dispose(disposing: true); GC.SuppressFinalize(this); } #endregion }