日常生活的交流与学习

首页 新随笔 联系 管理

using System.Linq.Expressions;
using Microsoft.Extensions.Configuration;
using SqlSugar;

namespace SqlSugarFrameworkCore;

public class DbSettings
{
public const string AppOne = "AppOne";
public const string AppDevOne = "AppDevOne";
public const string AppThreeOne = "AppThreeOne";
public const string Oracle = "Oracle";
}

public enum DbInfo
{
MesOne = 1,
MesDevOne = 2,
MesThree = 3,
Oracle = 4,
}

public class LongSugarClient
{
public static LongSugarClient sugarInstance;
public static DbInfo curDbInfo;
public static Dictionary<DbInfo, LongSugarClient> sugarInstances = new Dictionary<DbInfo, LongSugarClient>();

public static readonly object sugarInstanceLock = new object();
public SqlSugarClient _db;

private LongSugarClient(SqlSugarClient sqlSugarClient)
{
    _db = sqlSugarClient;
}

public static LongSugarClient GetInstance(DbInfo dbInfo, IConfigurationSection dbConfigurationSection)
{
    if (sugarInstances.ContainsKey(dbInfo))
    {
        sugarInstances.TryGetValue(dbInfo, out LongSugarClient sugarInstance);
        return sugarInstance;
    }

    lock (sugarInstanceLock)
    {
        if (sugarInstances.ContainsKey(dbInfo))
        {
            sugarInstances.TryGetValue(dbInfo, out LongSugarClient sugarInstance);
            return sugarInstance;
        }
        GetSugarClient(dbInfo, dbConfigurationSection);
    }
    return sugarInstance;
}

private static void GetSugarClient(DbInfo dbInfo, IConfigurationSection dbConfigurationSection)
{
    if (dbInfo == DbInfo.MesOne)
    {
        string connectStr = dbConfigurationSection.GetSection(DbSettings.AppOne).Value ?? throw new ArgumentNullException($"{DbSettings.AppOne} is misstion");
        CreateSugarClient(dbInfo, connectStr);
    }
    else if (dbInfo == DbInfo.MesDevOne)
    {
        string connectStr = dbConfigurationSection.GetSection(DbSettings.AppDevOne).Value ?? throw new ArgumentNullException($"{DbSettings.AppDevOne} is misstion");
        CreateSugarClient(dbInfo, connectStr);
    }
    else if (dbInfo == DbInfo.MesThree)
    {
        string connectStr = dbConfigurationSection.GetSection(DbSettings.AppThreeOne).Value ?? throw new ArgumentNullException($"{DbSettings.AppThreeOne} is misstion");
        CreateSugarClient(dbInfo, connectStr);
    }
    else if (dbInfo == DbInfo.Oracle)
    {
        string connectStr = dbConfigurationSection.GetSection(DbSettings.Oracle).Value ?? throw new ArgumentNullException($"{DbSettings.Oracle} is misstion");
        CreateSugarClient(dbInfo, connectStr);
    }
}

private static void CreateSugarClient(DbInfo dbInfo, string connectStr)
{
    SqlSugarClient sqlSugar = new SqlSugarClient(
        new ConnectionConfig()
        {
            DbType = SqlSugar.DbType.SqlServer,
            ConnectionString = connectStr,
            IsAutoCloseConnection = true,
        },
        db =>
        {
            //单例参数配置,所有上下文生效
            db.Aop.OnLogExecuting = (sql, pars) =>
            {
                //打印日志
                Console.WriteLine(sql + "\r\n" + db.Utilities.SerializeObject(pars.ToDictionary(it => it.ParameterName, it => it.Value)));
            };
        }
    );
    sugarInstance = new LongSugarClient(sqlSugar);
    sugarInstances.Add(dbInfo, sugarInstance);
}

private DbInfo? GetCurrentDbInfo()
{
    foreach (KeyValuePair<DbInfo, LongSugarClient> pair in sugarInstances)
    {
        if (pair.Value == this)
        {
            return pair.Key;
        }
    }
    return null;
}


public void Excute()
{
    dynamic dynamic = this._db.SqlQueryable<dynamic>("select count(*) as count FROM [dbo].[MBO_EqInfo]").First();
    System.Console.WriteLine(dynamic.ToSqlValue);
}

}

posted on 2024-08-29 19:23  lazycookie  阅读(32)  评论(0编辑  收藏  举报