|
@@ -1,25 +1,14 @@
|
|
-using ORM;
|
|
|
|
-using SqlSugar;
|
|
|
|
|
|
+using SqlSugar;
|
|
using System.Linq.Expressions;
|
|
using System.Linq.Expressions;
|
|
-using Universal;
|
|
|
|
|
|
|
|
namespace SqlSugarORM;
|
|
namespace SqlSugarORM;
|
|
-public class SqlSugarCustom : IORM
|
|
|
|
|
|
+public class SqlSugarCustom
|
|
{
|
|
{
|
|
- public SqlSugarCustom()
|
|
|
|
- {
|
|
|
|
- this._logQueue = new(LogQueueHandler);
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
|
|
+ public SqlSugarClient? Client { get; private set; }
|
|
#region Internal
|
|
#region Internal
|
|
- private IOrmProvider? _provider;
|
|
|
|
- private SqlSugarClient? _Client;
|
|
|
|
|
|
+
|
|
private bool disposedValue;
|
|
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)
|
|
private void Config(SqlSugarClient client)
|
|
{
|
|
{
|
|
client.Aop.OnLogExecuting = SqlLog;
|
|
client.Aop.OnLogExecuting = SqlLog;
|
|
@@ -27,36 +16,31 @@ public class SqlSugarCustom : IORM
|
|
private void SqlLog(string sql, SugarParameter[] paras)
|
|
private void SqlLog(string sql, SugarParameter[] paras)
|
|
{
|
|
{
|
|
string log = UtilMethods.GetNativeSql(sql, paras);
|
|
string log = UtilMethods.GetNativeSql(sql, paras);
|
|
- _logQueue?.Enqueue(new(log, DateTime.Now, LogLevel.Original));
|
|
|
|
}
|
|
}
|
|
#endregion
|
|
#endregion
|
|
|
|
|
|
- bool IORM.Initialize(IOrmProvider? notify)
|
|
|
|
|
|
+ public bool Initialize()
|
|
{
|
|
{
|
|
- if (_provider is not null)
|
|
|
|
- return false;
|
|
|
|
-
|
|
|
|
- _provider = notify;
|
|
|
|
|
|
|
|
return true;
|
|
return true;
|
|
}
|
|
}
|
|
|
|
|
|
- bool IORM.Open(string connectionString, ORM.DbType dbType, bool isAutoConnection)
|
|
|
|
|
|
+ public bool Open(string connectionString, SqlSugar.DbType dbType, bool isAutoConnection)
|
|
{
|
|
{
|
|
- if (this._Client is not null)
|
|
|
|
|
|
+ if (this.Client is not null)
|
|
return false;
|
|
return false;
|
|
|
|
|
|
ConnectionConfig config = new()
|
|
ConnectionConfig config = new()
|
|
{
|
|
{
|
|
ConnectionString = connectionString,
|
|
ConnectionString = connectionString,
|
|
- DbType = (SqlSugar.DbType)dbType,
|
|
|
|
|
|
+ DbType = dbType,
|
|
IsAutoCloseConnection = isAutoConnection
|
|
IsAutoCloseConnection = isAutoConnection
|
|
};
|
|
};
|
|
try
|
|
try
|
|
{
|
|
{
|
|
SqlSugarClient Db = new(config, Config);
|
|
SqlSugarClient Db = new(config, Config);
|
|
Db.DbMaintenance.CreateDatabase();
|
|
Db.DbMaintenance.CreateDatabase();
|
|
- this._Client = Db;
|
|
|
|
|
|
+ this.Client = Db;
|
|
}
|
|
}
|
|
catch
|
|
catch
|
|
{
|
|
{
|
|
@@ -65,9 +49,9 @@ public class SqlSugarCustom : IORM
|
|
return true;
|
|
return true;
|
|
}
|
|
}
|
|
|
|
|
|
- bool IORM.CreateDataBase(string dbName)
|
|
|
|
|
|
+ public bool CreateDataBase(string dbName)
|
|
{
|
|
{
|
|
- if (this._Client is null)
|
|
|
|
|
|
+ if (this.Client is null)
|
|
return false;
|
|
return false;
|
|
|
|
|
|
if (string.IsNullOrEmpty(dbName))
|
|
if (string.IsNullOrEmpty(dbName))
|
|
@@ -75,32 +59,30 @@ public class SqlSugarCustom : IORM
|
|
|
|
|
|
try
|
|
try
|
|
{
|
|
{
|
|
- this._Client.DbMaintenance.CreateDatabase(dbName);
|
|
|
|
|
|
+ this.Client.DbMaintenance.CreateDatabase(dbName);
|
|
}
|
|
}
|
|
catch
|
|
catch
|
|
{
|
|
{
|
|
- _logQueue?.Enqueue(new($"Create DB {dbName} Failed", DateTime.Now, LogLevel.Error));
|
|
|
|
return false;
|
|
return false;
|
|
}
|
|
}
|
|
|
|
|
|
return true;
|
|
return true;
|
|
}
|
|
}
|
|
|
|
|
|
- bool IORM.CreateTable<T>(string? tableName)
|
|
|
|
|
|
+ public bool CreateTable<T>(string? tableName)
|
|
{
|
|
{
|
|
- if (this._Client is null)
|
|
|
|
|
|
+ if (this.Client is null)
|
|
return false;
|
|
return false;
|
|
|
|
|
|
try
|
|
try
|
|
{
|
|
{
|
|
if (string.IsNullOrEmpty(tableName))
|
|
if (string.IsNullOrEmpty(tableName))
|
|
- this._Client.CodeFirst.InitTables<T>();
|
|
|
|
|
|
+ this.Client.CodeFirst.InitTables<T>();
|
|
else
|
|
else
|
|
- this._Client.CodeFirst.As(typeof(T), tableName).InitTables<T>();
|
|
|
|
|
|
+ this.Client.CodeFirst.As(typeof(T), tableName).InitTables<T>();
|
|
}
|
|
}
|
|
catch
|
|
catch
|
|
{
|
|
{
|
|
- _logQueue?.Enqueue(new($"Create Table {tableName} Failed", DateTime.Now, LogLevel.Error));
|
|
|
|
return false;
|
|
return false;
|
|
}
|
|
}
|
|
|
|
|
|
@@ -109,17 +91,16 @@ public class SqlSugarCustom : IORM
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
- bool IORM.Insert<T>(T data)
|
|
|
|
|
|
+ public bool Insert<T>(T data) where T : class, new()
|
|
{
|
|
{
|
|
- if (this._Client is null)
|
|
|
|
|
|
+ if (this.Client is null)
|
|
return false;
|
|
return false;
|
|
try
|
|
try
|
|
{
|
|
{
|
|
- this._Client.Insertable(data).ExecuteCommand();
|
|
|
|
|
|
+ this.Client.Insertable(data).ExecuteCommand();
|
|
}
|
|
}
|
|
catch
|
|
catch
|
|
{
|
|
{
|
|
- _logQueue?.Enqueue(new($"Insert {data.ToString} Failed", DateTime.Now, LogLevel.Error));
|
|
|
|
return false;
|
|
return false;
|
|
}
|
|
}
|
|
|
|
|
|
@@ -127,23 +108,19 @@ public class SqlSugarCustom : IORM
|
|
return true;
|
|
return true;
|
|
}
|
|
}
|
|
|
|
|
|
- bool IORM.Insert<T>(string tablename, T data)
|
|
|
|
|
|
+ public bool Insert<T>(string tablename, T data) where T : class, new()
|
|
{
|
|
{
|
|
- if (this._Client is null)
|
|
|
|
|
|
+ if (this.Client is null)
|
|
return false;
|
|
return false;
|
|
try
|
|
try
|
|
{
|
|
{
|
|
if (string.IsNullOrEmpty(tablename))
|
|
if (string.IsNullOrEmpty(tablename))
|
|
- this._Client.Insertable(data).ExecuteCommand();
|
|
|
|
|
|
+ this.Client.Insertable(data).ExecuteCommand();
|
|
else
|
|
else
|
|
- this._Client.Insertable(data).AS(tablename).ExecuteCommand();
|
|
|
|
|
|
+ this.Client.Insertable(data).AS(tablename).ExecuteCommand();
|
|
}
|
|
}
|
|
catch
|
|
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 false;
|
|
}
|
|
}
|
|
|
|
|
|
@@ -151,29 +128,28 @@ public class SqlSugarCustom : IORM
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
- async Task<bool> IORM.Query<T>(Action<List<T>> results)
|
|
|
|
|
|
+ public async Task<bool> Query<T>(Action<List<T>> results)
|
|
{
|
|
{
|
|
- if (this._Client is null)
|
|
|
|
|
|
+ if (this.Client is null)
|
|
return false;
|
|
return false;
|
|
|
|
|
|
return await Task<bool>.Factory.StartNew(() =>
|
|
return await Task<bool>.Factory.StartNew(() =>
|
|
{
|
|
{
|
|
try
|
|
try
|
|
{
|
|
{
|
|
- results?.Invoke(this._Client.Queryable<T>().ToList());
|
|
|
|
|
|
+ results?.Invoke(this.Client.Queryable<T>().ToList());
|
|
}
|
|
}
|
|
catch
|
|
catch
|
|
{
|
|
{
|
|
- _logQueue?.Enqueue(new($"Query {typeof(T)} Failed", DateTime.Now, LogLevel.Error));
|
|
|
|
return false;
|
|
return false;
|
|
}
|
|
}
|
|
return true;
|
|
return true;
|
|
});
|
|
});
|
|
}
|
|
}
|
|
|
|
|
|
- async Task<bool> IORM.Query<T>(string tableName, Action<List<T>> results)
|
|
|
|
|
|
+ public async Task<bool> Query<T>(string tableName, Action<List<T>> results)
|
|
{
|
|
{
|
|
- if (this._Client is null)
|
|
|
|
|
|
+ if (this.Client is null)
|
|
return false;
|
|
return false;
|
|
|
|
|
|
return await Task<bool>.Factory.StartNew(() =>
|
|
return await Task<bool>.Factory.StartNew(() =>
|
|
@@ -181,30 +157,29 @@ public class SqlSugarCustom : IORM
|
|
try
|
|
try
|
|
{
|
|
{
|
|
if (string.IsNullOrEmpty(tableName))
|
|
if (string.IsNullOrEmpty(tableName))
|
|
- results?.Invoke(this._Client.Queryable<T>().ToList());
|
|
|
|
|
|
+ results?.Invoke(this.Client.Queryable<T>().ToList());
|
|
else
|
|
else
|
|
- results?.Invoke(this._Client.Queryable<T>().AS(tableName).ToList());
|
|
|
|
|
|
+ results?.Invoke(this.Client.Queryable<T>().AS(tableName).ToList());
|
|
}
|
|
}
|
|
catch
|
|
catch
|
|
{
|
|
{
|
|
- _logQueue?.Enqueue(new($"Query {typeof(T)} Failed", DateTime.Now, LogLevel.Error));
|
|
|
|
return false;
|
|
return false;
|
|
}
|
|
}
|
|
return true;
|
|
return true;
|
|
});
|
|
});
|
|
}
|
|
}
|
|
|
|
|
|
- bool IORM.Query<T>(string tableName, out List<T>? results)
|
|
|
|
|
|
+ public bool Query<T>(string tableName, out List<T>? results)
|
|
{
|
|
{
|
|
results = null;
|
|
results = null;
|
|
- if (this._Client is null)
|
|
|
|
|
|
+ if (this.Client is null)
|
|
return false;
|
|
return false;
|
|
|
|
|
|
try
|
|
try
|
|
{
|
|
{
|
|
results = string.IsNullOrEmpty(tableName) ?
|
|
results = string.IsNullOrEmpty(tableName) ?
|
|
- this._Client.Queryable<T>().ToList() :
|
|
|
|
- this._Client.Queryable<T>().AS(tableName).ToList();
|
|
|
|
|
|
+ this.Client.Queryable<T>().ToList() :
|
|
|
|
+ this.Client.Queryable<T>().AS(tableName).ToList();
|
|
}
|
|
}
|
|
catch
|
|
catch
|
|
{
|
|
{
|
|
@@ -214,15 +189,15 @@ public class SqlSugarCustom : IORM
|
|
return true;
|
|
return true;
|
|
}
|
|
}
|
|
|
|
|
|
- bool IORM.Query<T>(out List<T>? results)
|
|
|
|
|
|
+ public bool Query<T>(out List<T>? results)
|
|
{
|
|
{
|
|
results = null;
|
|
results = null;
|
|
- if (this._Client is null)
|
|
|
|
|
|
+ if (this.Client is null)
|
|
return false;
|
|
return false;
|
|
|
|
|
|
try
|
|
try
|
|
{
|
|
{
|
|
- results = this._Client.Queryable<T>().ToList();
|
|
|
|
|
|
+ results = this.Client.Queryable<T>().ToList();
|
|
}
|
|
}
|
|
catch
|
|
catch
|
|
{
|
|
{
|
|
@@ -232,17 +207,17 @@ public class SqlSugarCustom : IORM
|
|
return true;
|
|
return true;
|
|
}
|
|
}
|
|
|
|
|
|
- bool IORM.Query<T>(string tableName, Expression<Func<T, bool>> expression, out List<T>? results)
|
|
|
|
|
|
+ public bool Query<T>(string tableName, Expression<Func<T, bool>> expression, out List<T>? results)
|
|
{
|
|
{
|
|
results = default;
|
|
results = default;
|
|
- if (this._Client is null)
|
|
|
|
|
|
+ if (this.Client is null)
|
|
return false;
|
|
return false;
|
|
|
|
|
|
try
|
|
try
|
|
{
|
|
{
|
|
results = string.IsNullOrEmpty(tableName) ?
|
|
results = string.IsNullOrEmpty(tableName) ?
|
|
- this._Client.Queryable<T>().Where(expression).ToList() :
|
|
|
|
- this._Client.Queryable<T>().AS(tableName).Where(expression).ToList();
|
|
|
|
|
|
+ this.Client.Queryable<T>().Where(expression).ToList() :
|
|
|
|
+ this.Client.Queryable<T>().AS(tableName).Where(expression).ToList();
|
|
}
|
|
}
|
|
catch
|
|
catch
|
|
{
|
|
{
|
|
@@ -254,29 +229,28 @@ public class SqlSugarCustom : IORM
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
- async Task<bool> IORM.Query<T>(Expression<Func<T, bool>> expression, Action<List<T>> results)
|
|
|
|
|
|
+ public async Task<bool> Query<T>(Expression<Func<T, bool>> expression, Action<List<T>> results)
|
|
{
|
|
{
|
|
- if (this._Client is null)
|
|
|
|
|
|
+ if (this.Client is null)
|
|
return false;
|
|
return false;
|
|
|
|
|
|
return await Task<bool>.Factory.StartNew(() =>
|
|
return await Task<bool>.Factory.StartNew(() =>
|
|
{
|
|
{
|
|
try
|
|
try
|
|
{
|
|
{
|
|
- results?.Invoke(this._Client.Queryable<T>().Where(expression).ToList());
|
|
|
|
|
|
+ results?.Invoke(this.Client.Queryable<T>().Where(expression).ToList());
|
|
}
|
|
}
|
|
catch
|
|
catch
|
|
{
|
|
{
|
|
- _logQueue?.Enqueue(new($"Query {typeof(T)} Failed", DateTime.Now, LogLevel.Error));
|
|
|
|
return false;
|
|
return false;
|
|
}
|
|
}
|
|
return true;
|
|
return true;
|
|
});
|
|
});
|
|
}
|
|
}
|
|
|
|
|
|
- async Task<bool> IORM.Query<T>(string tableName, Expression<Func<T, bool>> expression, Action<List<T>> results)
|
|
|
|
|
|
+ public async Task<bool> Query<T>(string tableName, Expression<Func<T, bool>> expression, Action<List<T>> results)
|
|
{
|
|
{
|
|
- if (this._Client is null)
|
|
|
|
|
|
+ if (this.Client is null)
|
|
return false;
|
|
return false;
|
|
|
|
|
|
return await Task<bool>.Factory.StartNew(() =>
|
|
return await Task<bool>.Factory.StartNew(() =>
|
|
@@ -284,16 +258,12 @@ public class SqlSugarCustom : IORM
|
|
try
|
|
try
|
|
{
|
|
{
|
|
if (string.IsNullOrEmpty(tableName))
|
|
if (string.IsNullOrEmpty(tableName))
|
|
- results?.Invoke(this._Client.Queryable<T>().Where(expression).ToList());
|
|
|
|
|
|
+ results?.Invoke(this.Client.Queryable<T>().Where(expression).ToList());
|
|
else
|
|
else
|
|
- results?.Invoke(this._Client.Queryable<T>().AS(tableName).Where(expression).ToList());
|
|
|
|
|
|
+ results?.Invoke(this.Client.Queryable<T>().AS(tableName).Where(expression).ToList());
|
|
}
|
|
}
|
|
catch
|
|
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 false;
|
|
}
|
|
}
|
|
|
|
|
|
@@ -301,13 +271,13 @@ public class SqlSugarCustom : IORM
|
|
});
|
|
});
|
|
}
|
|
}
|
|
|
|
|
|
- bool IORM.Delete<T>(string tableName, Expression<Func<T, bool>> expression)
|
|
|
|
|
|
+ public bool Delete<T>(string tableName, Expression<Func<T, bool>> expression) where T : class, new()
|
|
{
|
|
{
|
|
- if (this._Client is null)
|
|
|
|
|
|
+ if (this.Client is null)
|
|
return false;
|
|
return false;
|
|
try
|
|
try
|
|
{
|
|
{
|
|
- this._Client.Deleteable<T>().AS(tableName).Where(expression).ExecuteCommand();
|
|
|
|
|
|
+ this.Client.Deleteable<T>().AS(tableName).Where(expression).ExecuteCommand();
|
|
}
|
|
}
|
|
catch
|
|
catch
|
|
{
|
|
{
|
|
@@ -316,30 +286,26 @@ public class SqlSugarCustom : IORM
|
|
return true;
|
|
return true;
|
|
}
|
|
}
|
|
|
|
|
|
- bool IORM.AddOrUpdate<T>(string tableName, T data, Expression<Func<T, bool>> expression)
|
|
|
|
|
|
+ public bool AddOrUpdate<T>(string tableName, T data, Expression<Func<T, bool>> expression) where T : class, new()
|
|
{
|
|
{
|
|
- if (this._Client is null)
|
|
|
|
|
|
+ if (this.Client is null)
|
|
return false;
|
|
return false;
|
|
|
|
|
|
try
|
|
try
|
|
{
|
|
{
|
|
if (string.IsNullOrEmpty(tableName))
|
|
if (string.IsNullOrEmpty(tableName))
|
|
{
|
|
{
|
|
- if (this._Client.Updateable(data).Where(expression).ExecuteCommand() == 0)
|
|
|
|
- return (this as IORM).Insert(data);
|
|
|
|
|
|
+ if (this.Client.Updateable(data).Where(expression).ExecuteCommand() == 0)
|
|
|
|
+ return this.Insert(data);
|
|
}
|
|
}
|
|
else
|
|
else
|
|
{
|
|
{
|
|
- if (this._Client.Updateable(data).Where(expression).AS(tableName).ExecuteCommand() == 0)
|
|
|
|
- return (this as IORM).Insert(tableName, data);
|
|
|
|
|
|
+ if (this.Client.Updateable(data).Where(expression).AS(tableName).ExecuteCommand() == 0)
|
|
|
|
+ return this.Insert(tableName, data);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
catch
|
|
catch
|
|
{
|
|
{
|
|
- if (string.IsNullOrEmpty(tableName))
|
|
|
|
- _logQueue?.Enqueue(new($"Update {data.ToString} Failed", DateTime.Now, LogLevel.Error));
|
|
|
|
- else
|
|
|
|
- _logQueue?.Enqueue(new($"Update {data.ToString} to table {tableName} Failed", DateTime.Now, LogLevel.Error));
|
|
|
|
return false;
|
|
return false;
|
|
}
|
|
}
|
|
return true;
|
|
return true;
|
|
@@ -352,13 +318,8 @@ public class SqlSugarCustom : IORM
|
|
{
|
|
{
|
|
if (disposing)
|
|
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;
|
|
|
|
|
|
+ this.Client?.Dispose();
|
|
|
|
+ this.Client = null;
|
|
}
|
|
}
|
|
|
|
|
|
disposedValue = true;
|
|
disposedValue = true;
|