|
@@ -4,7 +4,7 @@ using System.Linq.Expressions;
|
|
using Universal;
|
|
using Universal;
|
|
|
|
|
|
namespace SqlSugarORM;
|
|
namespace SqlSugarORM;
|
|
-public class SqlSugarCustom
|
|
|
|
|
|
+public class SqlSugarCustom : IORM
|
|
{
|
|
{
|
|
public SqlSugarCustom()
|
|
public SqlSugarCustom()
|
|
{
|
|
{
|
|
@@ -13,7 +13,7 @@ public class SqlSugarCustom
|
|
|
|
|
|
#region Internal
|
|
#region Internal
|
|
private IOrmProvider? _provider;
|
|
private IOrmProvider? _provider;
|
|
- public SqlSugarClient? Client;
|
|
|
|
|
|
+ private SqlSugarClient? _Client;
|
|
private bool disposedValue;
|
|
private bool disposedValue;
|
|
private readonly EventQueue<(string, DateTime, LogLevel)> _logQueue;
|
|
private readonly EventQueue<(string, DateTime, LogLevel)> _logQueue;
|
|
private void LogQueueHandler((string log, DateTime time, LogLevel level) logItem)
|
|
private void LogQueueHandler((string log, DateTime time, LogLevel level) logItem)
|
|
@@ -31,7 +31,7 @@ public class SqlSugarCustom
|
|
}
|
|
}
|
|
#endregion
|
|
#endregion
|
|
|
|
|
|
- public bool Initialize(IOrmProvider? notify)
|
|
|
|
|
|
+ bool IORM.Initialize(IOrmProvider? notify)
|
|
{
|
|
{
|
|
if (_provider is not null)
|
|
if (_provider is not null)
|
|
return false;
|
|
return false;
|
|
@@ -41,22 +41,22 @@ public class SqlSugarCustom
|
|
return true;
|
|
return true;
|
|
}
|
|
}
|
|
|
|
|
|
- public bool Open(string connectionString, SqlSugar.DbType dbType, bool isAutoConnection)
|
|
|
|
|
|
+ bool IORM.Open(string connectionString, ORM.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 = dbType,
|
|
|
|
|
|
+ DbType = (SqlSugar.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 +65,9 @@ public class SqlSugarCustom
|
|
return true;
|
|
return true;
|
|
}
|
|
}
|
|
|
|
|
|
- public bool CreateDataBase(string dbName)
|
|
|
|
|
|
+ bool IORM.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,7 +75,7 @@ public class SqlSugarCustom
|
|
|
|
|
|
try
|
|
try
|
|
{
|
|
{
|
|
- this.Client.DbMaintenance.CreateDatabase(dbName);
|
|
|
|
|
|
+ this._Client.DbMaintenance.CreateDatabase(dbName);
|
|
}
|
|
}
|
|
catch
|
|
catch
|
|
{
|
|
{
|
|
@@ -86,17 +86,17 @@ public class SqlSugarCustom
|
|
return true;
|
|
return true;
|
|
}
|
|
}
|
|
|
|
|
|
- public bool CreateTable<T>(string? tableName)
|
|
|
|
|
|
+ bool IORM.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
|
|
{
|
|
{
|
|
@@ -108,15 +108,14 @@ public class SqlSugarCustom
|
|
return true;
|
|
return true;
|
|
}
|
|
}
|
|
|
|
|
|
- public bool Insert<T>(T data) where T : class, new()
|
|
|
|
|
|
+
|
|
|
|
+ bool IORM.Insert<T>(T data)
|
|
{
|
|
{
|
|
- if (this.Client is null)
|
|
|
|
- return false;
|
|
|
|
- if (data 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
|
|
{
|
|
{
|
|
@@ -128,18 +127,16 @@ public class SqlSugarCustom
|
|
return true;
|
|
return true;
|
|
}
|
|
}
|
|
|
|
|
|
- public bool Insert<T>(string tablename, T? data) where T : class, new()
|
|
|
|
|
|
+ bool IORM.Insert<T>(string tablename, T data)
|
|
{
|
|
{
|
|
- if (this.Client is null)
|
|
|
|
- return false;
|
|
|
|
- if (data 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
|
|
{
|
|
{
|
|
@@ -154,16 +151,16 @@ public class SqlSugarCustom
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
- public async Task<bool> Query<T>(Action<List<T>> results)
|
|
|
|
|
|
+ async Task<bool> IORM.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
|
|
{
|
|
{
|
|
@@ -174,9 +171,9 @@ public class SqlSugarCustom
|
|
});
|
|
});
|
|
}
|
|
}
|
|
|
|
|
|
- public async Task<bool> Query<T>(string tableName, Action<List<T>> results)
|
|
|
|
|
|
+ async Task<bool> IORM.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(() =>
|
|
@@ -184,9 +181,9 @@ public class SqlSugarCustom
|
|
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
|
|
{
|
|
{
|
|
@@ -197,16 +194,76 @@ public class SqlSugarCustom
|
|
});
|
|
});
|
|
}
|
|
}
|
|
|
|
|
|
- public async Task<bool> Query<T>(Expression<Func<T, bool>> expression, Action<List<T>> results)
|
|
|
|
|
|
+ bool IORM.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;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ bool IORM.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;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ bool IORM.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;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+ async Task<bool> IORM.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
|
|
{
|
|
{
|
|
@@ -217,9 +274,9 @@ public class SqlSugarCustom
|
|
});
|
|
});
|
|
}
|
|
}
|
|
|
|
|
|
- public async Task<bool> Query<T>(string tableName, Expression<Func<T, bool>> expression, Action<List<T>> results)
|
|
|
|
|
|
+ async Task<bool> IORM.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(() =>
|
|
@@ -227,9 +284,9 @@ public class SqlSugarCustom
|
|
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
|
|
{
|
|
{
|
|
@@ -244,16 +301,45 @@ public class SqlSugarCustom
|
|
});
|
|
});
|
|
}
|
|
}
|
|
|
|
|
|
- public bool Delete<T>(string tableName, Expression<Func<T, bool>> expression) where T : class, new()
|
|
|
|
|
|
+ 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;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ bool IORM.AddOrUpdate<T>(string tableName, T data, Expression<Func<T, bool>> expression)
|
|
{
|
|
{
|
|
- 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();
|
|
|
|
|
|
+ if (string.IsNullOrEmpty(tableName))
|
|
|
|
+ {
|
|
|
|
+ if (this._Client.Updateable(data).Where(expression).ExecuteCommand() == 0)
|
|
|
|
+ return (this as IORM).Insert(data);
|
|
|
|
+ }
|
|
|
|
+ else
|
|
|
|
+ {
|
|
|
|
+ if (this._Client.Updateable(data).Where(expression).AS(tableName).ExecuteCommand() == 0)
|
|
|
|
+ return (this as IORM).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;
|
|
@@ -269,8 +355,8 @@ public class SqlSugarCustom
|
|
while (_logQueue is not null && _logQueue.Count != 0)
|
|
while (_logQueue is not null && _logQueue.Count != 0)
|
|
Thread.Sleep(200);
|
|
Thread.Sleep(200);
|
|
|
|
|
|
- this.Client?.Dispose();
|
|
|
|
- this.Client = null;
|
|
|
|
|
|
+ this._Client?.Dispose();
|
|
|
|
+ this._Client = null;
|
|
this._logQueue?.Dispose();
|
|
this._logQueue?.Dispose();
|
|
this._provider = null;
|
|
this._provider = null;
|
|
}
|
|
}
|