From a4c9cb94550bbaf1c15d57f42955f51a77d59fa4 Mon Sep 17 00:00:00 2001 From: xiaochanghai Date: Thu, 18 Apr 2024 10:09:04 +0800 Subject: [PATCH] =?UTF-8?q?=E5=88=A0=E9=99=A4=E6=97=A0=E6=95=88=E4=BB=A3?= =?UTF-8?q?=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../DB/Dapper/DBManager/BaseDBConfig.cs | 151 ------------------ .../DB/Dapper/SimpleDapper.csproj | 29 ---- .../DB/Dapper/Utilities/StringHelper.cs | 15 -- 3 files changed, 195 deletions(-) delete mode 100644 Tiobon.Core.Common/DB/Dapper/DBManager/BaseDBConfig.cs delete mode 100644 Tiobon.Core.Common/DB/Dapper/SimpleDapper.csproj delete mode 100644 Tiobon.Core.Common/DB/Dapper/Utilities/StringHelper.cs diff --git a/Tiobon.Core.Common/DB/Dapper/DBManager/BaseDBConfig.cs b/Tiobon.Core.Common/DB/Dapper/DBManager/BaseDBConfig.cs deleted file mode 100644 index b5c17303..00000000 --- a/Tiobon.Core.Common/DB/Dapper/DBManager/BaseDBConfig.cs +++ /dev/null @@ -1,151 +0,0 @@ -using System; -using System.Collections.Generic; -using System.IO; -using System.Linq; -using SqlSugar; - -namespace Tiobon.Core.Common.DB.Dapper.DBManager; -public class BaseDBConfig -{ - /* 之前的单库操作已经删除,如果想要之前的代码,可以查看我的GitHub的历史记录 - * 目前是多库操作,默认加载的是AppSetting.json设置为true的第一个db连接。 - */ - public static (List allDbs, List slaveDbs) MutiConnectionString => MutiInitConn(); - - private static string DifDBConnOfSecurity(params string[] conn) - { - foreach (var item in conn) - { - try - { - if (File.Exists(item)) - { - return File.ReadAllText(item).Trim(); - } - } - catch (System.Exception) - { - } - } - - return conn[conn.Length - 1]; - } - - - public static (List, List) MutiInitConn() - { - List listdatabase = AppSetting.app("DBS") - .Where(i => i.Enabled).ToList(); - foreach (var i in listdatabase) - { - SpecialDbString(i); - } - - List listdatabaseSimpleDB = new List(); //单库 - List listdatabaseSlaveDB = new List(); //从库 - - // 单库,且不开启读写分离,只保留一个 - if (!AppSetting.app(new string[] { "CQRSEnabled" }).ObjToBool() && !AppSetting.app(new string[] { "MutiDBEnabled" }).ObjToBool()) - { - if (listdatabase.Count == 1) - { - return (listdatabase, listdatabaseSlaveDB); - } - else - { - var dbFirst = listdatabase.FirstOrDefault(d => d.ConnId == AppSetting.app(new string[] { "MainDB" }).ObjToString()); - if (dbFirst == null) - { - dbFirst = listdatabase.FirstOrDefault(); - } - - listdatabaseSimpleDB.Add(dbFirst); - return (listdatabaseSimpleDB, listdatabaseSlaveDB); - } - } - - - // 读写分离,且必须是单库模式,获取从库 - if (AppSetting.app(new string[] { "CQRSEnabled" }).ObjToBool() && !AppSetting.app(new string[] { "MutiDBEnabled" }).ObjToBool()) - { - if (listdatabase.Count > 1) - { - listdatabaseSlaveDB = listdatabase.Where(d => d.ConnId != AppSetting.app(new string[] { "MainDB" }).ObjToString()).ToList(); - } - } - - - return (listdatabase, listdatabaseSlaveDB); - //} - } - - /// - /// 定制Db字符串 - /// 目的是保证安全:优先从本地txt文件获取,若没有文件则从AppSetting.json中获取 - /// - /// - /// - private static MutiDBOperate SpecialDbString(MutiDBOperate mutiDBOperate) - { - if (mutiDBOperate.DbType == DataBaseType.Sqlite) - { - mutiDBOperate.Connection = $"DataSource=" + Path.Combine(Environment.CurrentDirectory, mutiDBOperate.Connection); - } - else if (mutiDBOperate.DbType == DataBaseType.SqlServer) - { - mutiDBOperate.Connection = DifDBConnOfSecurity(@"D:\my-file\dbCountPsw1_SqlserverConn.txt", mutiDBOperate.Connection); - } - else if (mutiDBOperate.DbType == DataBaseType.MySql) - { - mutiDBOperate.Connection = DifDBConnOfSecurity(@"D:\my-file\dbCountPsw1_MySqlConn.txt", mutiDBOperate.Connection); - } - else if (mutiDBOperate.DbType == DataBaseType.Oracle) - { - mutiDBOperate.Connection = DifDBConnOfSecurity(@"D:\my-file\dbCountPsw1_OracleConn.txt", mutiDBOperate.Connection); - } - - return mutiDBOperate; - } - - -} - - -public enum DataBaseType -{ - MySql = 0, - SqlServer = 1, - Sqlite = 2, - Oracle = 3, - PostgreSQL = 4, - Dm = 5, - Kdbndp = 6, -} - -public class MutiDBOperate -{ - /// - /// 连接启用开关 - /// - public bool Enabled { get; set; } - - /// - /// 连接ID - /// - public string ConnId { get; set; } - - /// - /// 从库执行级别,越大越先执行 - /// - public int HitRate { get; set; } - - /// - /// 连接字符串 - /// - public string Connection { get; set; } - - /// - /// 数据库类型 - /// - public DataBaseType DbType { get; set; } -} \ No newline at end of file diff --git a/Tiobon.Core.Common/DB/Dapper/SimpleDapper.csproj b/Tiobon.Core.Common/DB/Dapper/SimpleDapper.csproj deleted file mode 100644 index 9a7ca487..00000000 --- a/Tiobon.Core.Common/DB/Dapper/SimpleDapper.csproj +++ /dev/null @@ -1,29 +0,0 @@ - - - - net8.0 - True - - - - - - - - false - - - - - - - - - - - - - - - - diff --git a/Tiobon.Core.Common/DB/Dapper/Utilities/StringHelper.cs b/Tiobon.Core.Common/DB/Dapper/Utilities/StringHelper.cs deleted file mode 100644 index bea94565..00000000 --- a/Tiobon.Core.Common/DB/Dapper/Utilities/StringHelper.cs +++ /dev/null @@ -1,15 +0,0 @@ -using System; - -namespace Tiobon.Core.Common.DB.Dapper.Utilities; -public class StringHelper -{ - public static string Id - { - get - { - Guid id = Guid.NewGuid(); - return id.ToString(); - //return GuidRandomGenerator.Instance.Generate(); - } - } -}