| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342 |
- 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<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
- {
- return false;
- }
- return true;
- }
- public bool Insert<T>(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<T>(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<bool> 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
- {
- return false;
- }
- return true;
- });
- }
- public async Task<bool> 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
- {
- return false;
- }
- return true;
- });
- }
- public bool Query<T>(string tableName, out List<T>? results)
- {
- results = null;
- if (this.Client is null)
- return false;
- try
- {
- results = string.IsNullOrEmpty(tableName) ?
- this.Client.Queryable<T>().ToList() :
- this.Client.Queryable<T>().AS(tableName).ToList();
- }
- catch
- {
- return false;
- }
- return true;
- }
- public bool Query<T>(out List<T>? results)
- {
- results = null;
- if (this.Client is null)
- return false;
- try
- {
- results = this.Client.Queryable<T>().ToList();
- }
- catch
- {
- return false;
- }
- return true;
- }
- public bool Query<T>(string tableName, Expression<Func<T, bool>> expression, out List<T>? results)
- {
- results = default;
- if (this.Client is null)
- return false;
- try
- {
- results = string.IsNullOrEmpty(tableName) ?
- this.Client.Queryable<T>().Where(expression).ToList() :
- this.Client.Queryable<T>().AS(tableName).Where(expression).ToList();
- }
- catch
- {
- return false;
- }
- return true;
- }
- public async Task<bool> 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
- {
- return false;
- }
- return true;
- });
- }
- public async Task<bool> 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
- {
- return false;
- }
- return true;
- });
- }
- public bool Delete<T>(string tableName, Expression<Func<T, bool>> expression) where T : class, new()
- {
- if (this.Client is null)
- return false;
- try
- {
- this.Client.Deleteable<T>().AS(tableName).Where(expression).ExecuteCommand();
- }
- catch
- {
- return false;
- }
- return true;
- }
- public bool InsertOrUpdate<T>(string tableName, T data, Expression<Func<T, bool>> 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
- }
|