You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

102 lines
3.8 KiB

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

using zzz.Common;
using Microsoft.Extensions.DependencyInjection;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using StackExchange.Profiling;
using zzz.Common.LogHelper;
namespace zzz.Extensions
{
public static class SqlsugarSetup
{
public static void AddSqlsugarSetup(this IServiceCollection services)
{
if (services == null) throw new ArgumentNullException(nameof(services));
SqlSugarConfigure(services);
}
private static void SqlSugarConfigure(IServiceCollection services)
{
#region
var dbtypeInt = Appsettings.app("DBType").ObjToInt();
DbType dbType = (DbType)dbtypeInt; //这里设置数据库类型
var conStr = Appsettings.app(new string[] { "ConnectionStrings", $"{dbType.ToString()}" });
if (string.IsNullOrWhiteSpace(conStr))
{
Console.WriteLine($"请正确配置appsettings.json中的连接字符串:ConnectionStrings.{dbType.ToString()}");
return;
}
services.AddScoped<ISqlSugarClient>(o =>
{
var client = new SqlSugarScope(new ConnectionConfig()
{
ConnectionString = conStr, //必填, 数据库连接字符串
DbType = dbType, //必填, 数据库类型
IsAutoCloseConnection = true,//默认false, 是否自动关闭数据库连接, 设置为true无需使用using或者Close操作
InitKeyType = InitKeyType.Attribute //默认SystemTable, 字段信息读取, 如:该属性是不是主键,标识列等等信息
});
//用来打印Sql方便你调式
client.Aop.OnLogExecuting = (sql, pars) =>
{
//这里的sql会显示到console中
//Console.WriteLine(sql + "\r\n" +
//client.Utilities.SerializeObject(pars.ToDictionary(it => it.ParameterName, it => it.Value)));
//Console.WriteLine();
if (Appsettings.app(new string[] { "Aop","SqlAOP", "OutToLogFile" }).ObjToBool())
{
Parallel.For(0, 1, e =>
{
MiniProfiler.Current.CustomTiming("SQL", GetParas(pars) + "【SQL语句】" + sql);
LogLock.OutSql2Log("SqlLog", new string[] { GetParas(pars), "【SQL语句】" + sql });
});
}
if (Appsettings.app(new string[] { "Aop", "SqlAOP", "OutToConsole" }).ObjToBool())
{
ConsoleHelper.WriteColorLine(string.Join("\r\n", new string[] { "--------", "【SQL语句】" + GetWholeSql(pars, sql) }), ConsoleColor.DarkCyan);
}
};
//// 配置加删除全局过滤器
//db.QueryFilter.Add(new TableFilterItem<SysUser>(it => it.IsDeleted == false));
//db.QueryFilter.Add(new TableFilterItem<SysUserRole>(it => it.IsDeleted == false));
return client;
});
#endregion
}
private static string GetWholeSql(SugarParameter[] paramArr, string sql)
{
foreach (var param in paramArr)
{
sql.Replace(param.ParameterName, param.Value.ObjToString());
}
return sql;
}
private static string GetParas(SugarParameter[] pars)
{
string key = "【SQL参数】";
foreach (var param in pars)
{
key += $"{param.ParameterName}:{param.Value}\n";
}
return key;
}
}
}