调整项目依赖
This commit is contained in:
37
common/Tnb.SqlSugar/Extensions/JsonSqlExtFunc.cs
Normal file
37
common/Tnb.SqlSugar/Extensions/JsonSqlExtFunc.cs
Normal file
@@ -0,0 +1,37 @@
|
||||
using SqlSugar;
|
||||
|
||||
namespace JNPF.Extras.DatabaseAccessor.SqlSugar.Extensions;
|
||||
|
||||
/// <summary>
|
||||
/// Newtonsoft.Json 序列化拓展.
|
||||
/// </summary>
|
||||
public static class JsonSqlExtFunc
|
||||
{
|
||||
/// <summary>
|
||||
///
|
||||
/// </summary>
|
||||
public static List<SqlFuncExternal> ExpMethods = new List<SqlFuncExternal>
|
||||
{
|
||||
new SqlFuncExternal(){
|
||||
UniqueMethodName = "ToObject",
|
||||
MethodValue = (expInfo, dbType, expContext) =>
|
||||
{
|
||||
//var value = string.Empty;
|
||||
return null;
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
/// <summary>
|
||||
///
|
||||
/// </summary>
|
||||
/// <typeparam name="T"></typeparam>
|
||||
/// <param name="value"></param>
|
||||
/// <returns></returns>
|
||||
/// <exception cref="NotSupportedException"></exception>
|
||||
public static T ToObject<T>(string value)
|
||||
{
|
||||
//这里不能写任何实现代码,需要在上面的配置中实现
|
||||
throw new NotSupportedException("Can only be used in expressions");
|
||||
}
|
||||
}
|
||||
84
common/Tnb.SqlSugar/Extensions/PagedQueryableExtensions.cs
Normal file
84
common/Tnb.SqlSugar/Extensions/PagedQueryableExtensions.cs
Normal file
@@ -0,0 +1,84 @@
|
||||
using System.Linq.Expressions;
|
||||
|
||||
namespace SqlSugar;
|
||||
|
||||
/// <summary>
|
||||
/// 分页拓展类
|
||||
/// </summary>
|
||||
public static class PagedQueryableExtensions
|
||||
{
|
||||
/// <summary>
|
||||
/// 分页拓展
|
||||
/// </summary>
|
||||
/// <param name="entity"></param>
|
||||
/// <param name="pageIndex"></param>
|
||||
/// <param name="pageSize"></param>
|
||||
/// <returns></returns>
|
||||
public static SqlSugarPagedList<TEntity> ToPagedList<TEntity>(this ISugarQueryable<TEntity> entity, int pageIndex, int pageSize)
|
||||
where TEntity : new()
|
||||
{
|
||||
var totalCount = 0;
|
||||
var items = entity.ToPageList(pageIndex, pageSize, ref totalCount);
|
||||
var pagination = new Pagination()
|
||||
{
|
||||
CurrentPage = pageIndex,
|
||||
PageSize = pageSize,
|
||||
Total = (int)totalCount
|
||||
};
|
||||
return new SqlSugarPagedList<TEntity>
|
||||
{
|
||||
pagination = pagination,
|
||||
list = items
|
||||
};
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 分页拓展
|
||||
/// </summary>
|
||||
/// <param name="entity"></param>
|
||||
/// <param name="pageIndex"></param>
|
||||
/// <param name="pageSize"></param>
|
||||
/// <returns></returns>
|
||||
public static async Task<SqlSugarPagedList<TEntity>> ToPagedListAsync<TEntity>(this ISugarQueryable<TEntity> entity, int pageIndex, int pageSize)
|
||||
where TEntity : new()
|
||||
{
|
||||
RefAsync<int> totalCount = 0;
|
||||
var items = await entity.ToPageListAsync(pageIndex, pageSize, totalCount);
|
||||
var pagination = new Pagination()
|
||||
{
|
||||
CurrentPage = pageIndex,
|
||||
PageSize = pageSize,
|
||||
Total = (int)totalCount
|
||||
};
|
||||
return new SqlSugarPagedList<TEntity>
|
||||
{
|
||||
pagination = pagination,
|
||||
list = items
|
||||
};
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 分页拓展
|
||||
/// </summary>
|
||||
/// <param name="entity"></param>
|
||||
/// <param name="pageIndex"></param>
|
||||
/// <param name="pageSize"></param>
|
||||
/// <param name="expression"></param>
|
||||
/// <returns></returns>
|
||||
public static async Task<SqlSugarPagedList<TEntity>> ToPagedListAsync<T, TEntity>(this ISugarQueryable<T> entity, int pageIndex, int pageSize, Expression<Func<T, TEntity>> expression)
|
||||
{
|
||||
RefAsync<int> totalCount = 0;
|
||||
var items = await entity.ToPageListAsync(pageIndex, pageSize, totalCount, expression);
|
||||
var pagination = new Pagination()
|
||||
{
|
||||
CurrentPage = pageIndex,
|
||||
PageSize = pageSize,
|
||||
Total = (int)totalCount
|
||||
};
|
||||
return new SqlSugarPagedList<TEntity>
|
||||
{
|
||||
pagination = pagination,
|
||||
list = items
|
||||
};
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,48 @@
|
||||
using SqlSugar;
|
||||
|
||||
namespace Microsoft.Extensions.DependencyInjection;
|
||||
|
||||
/// <summary>
|
||||
/// SqlSugar 拓展类
|
||||
/// </summary>
|
||||
public static class SqlSugarServiceCollectionExtensions
|
||||
{
|
||||
/// <summary>
|
||||
/// 添加 SqlSugar 拓展
|
||||
/// </summary>
|
||||
/// <param name="services"></param>
|
||||
/// <param name="config"></param>
|
||||
/// <param name="buildAction"></param>
|
||||
/// <returns></returns>
|
||||
public static IServiceCollection AddSqlSugar(this IServiceCollection services, ConnectionConfig config, Action<ISqlSugarClient> buildAction = default)
|
||||
{
|
||||
var list = new List<ConnectionConfig>();
|
||||
list.Add(config);
|
||||
return services.AddSqlSugar(list, buildAction);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 添加 SqlSugar 拓展
|
||||
/// </summary>
|
||||
/// <param name="services"></param>
|
||||
/// <param name="configs"></param>
|
||||
/// <param name="buildAction"></param>
|
||||
/// <returns></returns>
|
||||
public static IServiceCollection AddSqlSugar(this IServiceCollection services, List<ConnectionConfig> configs, Action<ISqlSugarClient> buildAction = default)
|
||||
{
|
||||
// SqlSugarScope是线程安全,可使用单例注入
|
||||
// 注册 SqlSugar 客户端
|
||||
services.AddSingleton<ISqlSugarClient>(u =>
|
||||
{
|
||||
var sqlSugarClient = new SqlSugarScope(configs);
|
||||
buildAction?.Invoke(sqlSugarClient);
|
||||
|
||||
return sqlSugarClient;
|
||||
});
|
||||
|
||||
// 注册 SqlSugar 仓储
|
||||
services.AddScoped(typeof(ISqlSugarRepository<>), typeof(SqlSugarRepository<>));
|
||||
|
||||
return services;
|
||||
}
|
||||
}
|
||||
225
common/Tnb.SqlSugar/Extensions/TenantLinkExtensions.cs
Normal file
225
common/Tnb.SqlSugar/Extensions/TenantLinkExtensions.cs
Normal file
@@ -0,0 +1,225 @@
|
||||
using JNPF.DataEncryption;
|
||||
using JNPF.Extras.DatabaseAccessor.SqlSugar;
|
||||
using JNPF.Extras.DatabaseAccessor.SqlSugar.Extensions;
|
||||
using Microsoft.Extensions.Configuration;
|
||||
using Microsoft.Extensions.Configuration.Json;
|
||||
|
||||
namespace SqlSugar;
|
||||
|
||||
/// <summary>
|
||||
/// JNPF多租户拓展.
|
||||
/// </summary>
|
||||
public class JNPFTenantExtensions
|
||||
{
|
||||
//public static IConfiguration _config { get; set; }
|
||||
private static ConnectionStringsOptions _connOpt { get; }
|
||||
|
||||
public static string skey = "Vl4WTqna9aZCgswjieIP";
|
||||
|
||||
/// <summary>
|
||||
/// 构造函数.
|
||||
/// </summary>
|
||||
static JNPFTenantExtensions()
|
||||
{
|
||||
var config = new ConfigurationBuilder().AddJsonFile("Configurations/ConnectionStrings.json", true, true).Build();
|
||||
_connOpt = config.GetSection("ConnectionStrings").Get<ConnectionStringsOptions>();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取普通链接.
|
||||
/// </summary>
|
||||
/// <param name="configId">配置ID.</param>
|
||||
/// <param name="tableName">数据库名称.</param>
|
||||
/// <returns></returns>
|
||||
public static ConnectionConfigOptions GetLinkToOrdinary(string configId, string tableName)
|
||||
{
|
||||
List<DBConnectionConfig> configList = new List<DBConnectionConfig>();
|
||||
var connStr = string.Format(_connOpt.DefaultConnection, _connOpt.Host, _connOpt.Port, _connOpt.DBName, _connOpt.UserName, _connOpt.Password);
|
||||
configList.Add(new DBConnectionConfig()
|
||||
{
|
||||
IsMaster = true,
|
||||
ServiceName = tableName,
|
||||
dbType = ToDbType(_connOpt.DBType),
|
||||
connectionStr = DESCEncryption.Encrypt(connStr, skey)
|
||||
});
|
||||
return new ConnectionConfigOptions()
|
||||
{
|
||||
ConfigId = configId,
|
||||
IsCustom = false,
|
||||
IsMasterSlaveSeparation = false,
|
||||
ConfigList = configList
|
||||
};
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取自定义链接
|
||||
/// </summary>
|
||||
/// <param name="configId">配置ID.</param>
|
||||
/// <param name="tenantLinkModels">数据库连接列表.</param>
|
||||
/// <returns></returns>
|
||||
public static ConnectionConfigOptions GetLinkToCustom(string configId, List<TenantLinkModel> tenantLinkModels)
|
||||
{
|
||||
List<DBConnectionConfig> configList = new List<DBConnectionConfig>();
|
||||
foreach (var item in tenantLinkModels)
|
||||
{
|
||||
if (item.configType == 0)
|
||||
{
|
||||
if (!string.IsNullOrEmpty(item.connectionStr))
|
||||
{
|
||||
configList.Add(new DBConnectionConfig()
|
||||
{
|
||||
IsMaster = true,
|
||||
dbType = ToDbType(item.dbType),
|
||||
ServiceName = item.serviceName,
|
||||
connectionStr = item.connectionStr,
|
||||
});
|
||||
}
|
||||
else
|
||||
{
|
||||
configList.Add(new DBConnectionConfig()
|
||||
{
|
||||
IsMaster = true,
|
||||
dbType = ToDbType(item.dbType),
|
||||
connectionStr = DESCEncryption.Encrypt(ToConnectionString(ToDbType(item.dbType), item.host, Convert.ToInt32(item.port), item.serviceName, item.userName, item.password, item.dbSchema), skey)
|
||||
});
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
if (!string.IsNullOrEmpty(item.connectionStr))
|
||||
{
|
||||
configList.Add(new DBConnectionConfig()
|
||||
{
|
||||
IsMaster = false,
|
||||
dbType = ToDbType(item.dbType),
|
||||
ServiceName = item.serviceName,
|
||||
connectionStr = item.connectionStr,
|
||||
});
|
||||
}
|
||||
else
|
||||
{
|
||||
configList.Add(new DBConnectionConfig()
|
||||
{
|
||||
IsMaster = false,
|
||||
dbType = ToDbType(item.dbType),
|
||||
connectionStr = DESCEncryption.Encrypt(ToConnectionString(ToDbType(item.dbType), item.host, Convert.ToInt32(item.port), item.serviceName, item.userName, item.password, item.dbSchema), skey)
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
return new ConnectionConfigOptions()
|
||||
{
|
||||
ConfigId = configId,
|
||||
IsCustom = true,
|
||||
IsMasterSlaveSeparation = tenantLinkModels.Any(it => it.configType.Equals(1)),
|
||||
ConfigList = configList
|
||||
};
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取配置.
|
||||
/// </summary>
|
||||
/// <param name="options"></param>
|
||||
/// <returns></returns>
|
||||
public static ConnectionConfig GetConfig(ConnectionConfigOptions options)
|
||||
{
|
||||
if (!options.IsCustom)
|
||||
{
|
||||
DBConnectionConfig config = options.ConfigList.FirstOrDefault();
|
||||
return new ConnectionConfig()
|
||||
{
|
||||
DbType = config.dbType,
|
||||
ConfigId = options.ConfigId,
|
||||
IsAutoCloseConnection = true,
|
||||
ConnectionString = DESCEncryption.Decrypt(config.connectionStr, skey),
|
||||
ConfigureExternalServices = new ConfigureExternalServices()
|
||||
{
|
||||
SqlFuncServices = JsonSqlExtFunc.ExpMethods
|
||||
}
|
||||
};
|
||||
}
|
||||
else
|
||||
{
|
||||
var slaveConnection = new List<SlaveConnectionConfig>();
|
||||
foreach (var item in options.ConfigList.FindAll(it => it.IsMaster.Equals(false)))
|
||||
{
|
||||
slaveConnection.Add(new SlaveConnectionConfig()
|
||||
{
|
||||
HitRate = 10,
|
||||
ConnectionString = DESCEncryption.Decrypt(item.connectionStr, skey)
|
||||
});
|
||||
}
|
||||
return new ConnectionConfig()
|
||||
{
|
||||
DbType = options.ConfigList.Find(it => it.IsMaster.Equals(true)).dbType,
|
||||
ConfigId = options.ConfigId,
|
||||
IsAutoCloseConnection = true,
|
||||
ConnectionString = DESCEncryption.Decrypt(options.ConfigList.Find(it => it.IsMaster.Equals(true)).connectionStr, skey),
|
||||
SlaveConnectionConfigs = slaveConnection,
|
||||
ConfigureExternalServices = new ConfigureExternalServices()
|
||||
{
|
||||
SqlFuncServices = JsonSqlExtFunc.ExpMethods
|
||||
}
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 转换数据库类型.
|
||||
/// </summary>
|
||||
/// <param name="dbType">数据库类型.</param>
|
||||
/// <returns></returns>
|
||||
public static DbType ToDbType(string dbType)
|
||||
{
|
||||
switch (dbType.ToLower())
|
||||
{
|
||||
case "mysql":
|
||||
return SqlSugar.DbType.MySql;
|
||||
case "oracle":
|
||||
return SqlSugar.DbType.Oracle;
|
||||
case "dm8":
|
||||
case "dm":
|
||||
return SqlSugar.DbType.Dm;
|
||||
case "kdbndp":
|
||||
case "kingbasees":
|
||||
return SqlSugar.DbType.Kdbndp;
|
||||
case "postgresql":
|
||||
return SqlSugar.DbType.PostgreSQL;
|
||||
default:
|
||||
return SqlSugar.DbType.SqlServer;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 转换连接字符串.
|
||||
/// </summary>
|
||||
/// <param name="dbType">数据库类型.</param>
|
||||
/// <param name="host">主机地址.</param>
|
||||
/// <param name="port">端口.</param>
|
||||
/// <param name="tableName">数据库名.</param>
|
||||
/// <param name="userName">用户名.</param>
|
||||
/// <param name="password">密码.</param>
|
||||
/// <param name="dbSchema">模式.</param>
|
||||
/// <returns></returns>
|
||||
public static string ToConnectionString(DbType dbType, string host, int port, string tableName, string userName, string password, string dbSchema)
|
||||
{
|
||||
switch (dbType)
|
||||
{
|
||||
case DbType.SqlServer:
|
||||
return string.Format("Data Source={0},{1};Initial Catalog={2};User ID={3};Password={4};Connection Timeout=5;MultipleActiveResultSets=true", host, port, tableName, userName, password);
|
||||
case DbType.Oracle:
|
||||
return string.Format("Data Source={0}:{1}/{2};User ID={3};Password={4};", host, port, dbSchema, userName, password);
|
||||
case DbType.MySql:
|
||||
return string.Format("server={0};port={1};database={2};user={3};password={4};AllowLoadLocalInfile=true", host, port, tableName, userName, password);
|
||||
case DbType.Dm:
|
||||
return string.Format("server={0};port={1};database={2};User Id={3};PWD={4}", host, port, tableName, userName, password);
|
||||
case DbType.Kdbndp:
|
||||
return string.Format("server={0};port={1};database={2};UID={3};PWD={4}", host, port, tableName, userName, password);
|
||||
case DbType.PostgreSQL:
|
||||
return string.Format("server={0};port={1};Database={2};User Id={3};Password={4}", host, port, tableName, userName, password);
|
||||
default:
|
||||
return string.Format("Data Source={0},{4};Initial Catalog={1};User ID={2};Password={3};Connection Timeout=5;MultipleActiveResultSets=true", host, tableName, userName, password, port);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user