88 lines
2.7 KiB
C#
88 lines
2.7 KiB
C#
using ce.autofac.extension;
|
|
using Infrastructure.Helpers;
|
|
using Microsoft.Extensions.Configuration;
|
|
using OpenAuth.Auth.Interface;
|
|
using SqlSugar;
|
|
|
|
namespace OpenAuth.BaseApp
|
|
{
|
|
public abstract class SqlSugarBaseApp<T, TDbContext> : BLL
|
|
where T : class, new()
|
|
where TDbContext : SugarUnitOfWork, new()
|
|
{
|
|
|
|
/// <summary>
|
|
/// 用于普通的数据库操作
|
|
/// </summary>
|
|
protected ISimpleClient<T> Repository;
|
|
/// <summary>
|
|
/// 用于事务操作
|
|
/// </summary>
|
|
protected ISugarUnitOfWork<TDbContext> UnitWork;
|
|
|
|
protected IAuth _auth;
|
|
|
|
|
|
public SqlSugarBaseApp(ISugarUnitOfWork<TDbContext> unitWork, ISimpleClient<T> repository, IAuth auth)
|
|
{
|
|
UnitWork = unitWork;
|
|
Repository = repository;
|
|
_auth = auth;
|
|
}
|
|
|
|
public SqlSugarClient CodeClient(string dbConnectionString, string dbType = "PostgreSQL")
|
|
{
|
|
if (string.IsNullOrEmpty(dbConnectionString))
|
|
{
|
|
dbConnectionString = UnitWork.Db.CurrentConnectionConfig.ConnectionString;
|
|
}
|
|
return new SqlSugarClient(new ConnectionConfig()
|
|
{
|
|
DbType = GetDbType(dbType),
|
|
ConnectionString = dbConnectionString,
|
|
IsAutoCloseConnection = true,
|
|
MoreSettings = new SqlSugar.ConnMoreSettings()
|
|
{
|
|
IsAutoToUpper = false,
|
|
DatabaseModel = DbType.PostgreSQL
|
|
}
|
|
},
|
|
db =>
|
|
{
|
|
//单例参数配置,所有上下文生效
|
|
db.Aop.OnLogExecuting = (sql, pars) =>
|
|
{
|
|
//Console.WriteLine(sql + "\r\n" +
|
|
//db.Utilities.SerializeObject(pars.ToDictionary(it => it.ParameterName, it => it.Value)));
|
|
//Console.WriteLine();
|
|
};
|
|
});
|
|
|
|
}
|
|
|
|
public static DbType GetDbType(string dbtype)
|
|
{
|
|
switch (dbtype)
|
|
{
|
|
case "SqlServer":
|
|
return DbType.SqlServer;
|
|
case "Oracle":
|
|
return DbType.Oracle;
|
|
case "MySql":
|
|
return DbType.MySql;
|
|
case "PostgreSQL":
|
|
return DbType.PostgreSQL;
|
|
case "Dameng":
|
|
return DbType.Dm;//达梦
|
|
case "Kdbndp":
|
|
return DbType.Kdbndp;//人大金仓
|
|
case "Sqlite":
|
|
return DbType.Sqlite;//Sqlite
|
|
default:
|
|
return DbType.MySql;
|
|
}
|
|
}
|
|
|
|
}
|
|
}
|