¶Ô±ÈÐÂÎļþ |
| | |
| | | using SqlSugar; |
| | | using System; |
| | | using System.Collections.Generic; |
| | | using System.Dynamic; |
| | | using System.Linq; |
| | | using System.Text; |
| | | using System.Threading.Tasks; |
| | | using WIDESEA_Core.Const; |
| | | using WIDESEA_Core.Helper; |
| | | using WIDESEA_Core.Tenants; |
| | | |
| | | namespace WIDESEA_Core.DB |
| | | { |
| | | public class BaseDBConfig |
| | | { |
| | | /* ä¹åçååºæä½å·²ç»å é¤ï¼å¦ææ³è¦ä¹åç代ç ï¼å¯ä»¥æ¥çæçGitHubçåå²è®°å½ |
| | | * ç®åæ¯å¤åºæä½ï¼é»è®¤å è½½çæ¯appsettings.json设置为trueç第ä¸ä¸ªdbè¿æ¥ã |
| | | */ |
| | | public static List<MutiDBOperate> 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<MutiDBOperate> MutiInitConn() |
| | | { |
| | | SqlSugarClient sqlSugarClient = new SqlSugarClient(new ConnectionConfig |
| | | { |
| | | ConfigId = MainDb.CurrentDbConnId, |
| | | ConnectionString = AppSettings.app(MainDb.ConnectionString).DecryptDES(AppSecret.DB), |
| | | IsAutoCloseConnection = true, |
| | | DbType = MainDb.DbType, |
| | | AopEvents = new AopEvents |
| | | { |
| | | OnError = x => |
| | | { |
| | | Console.WriteLine(x.Sql); |
| | | } |
| | | } |
| | | }); |
| | | |
| | | List<ExpandoObject> list = sqlSugarClient.Queryable(MainDb.TenantTableName, "x").Where(MainDb.TenantStatus, "=", TenantStatus.Enable).Select(TenantUtil.GetTenantSelectModels()).ToList(); |
| | | List<MutiDBOperate> listdatabaseSlaveDB = new List<MutiDBOperate>(); |
| | | MutiDBOperate mainDb = new MutiDBOperate() |
| | | { |
| | | Connection = AppSettings.app(MainDb.ConnectionString).DecryptDES(AppSecret.DB), |
| | | ConnId = MainDb.CurrentDbConnId, |
| | | DbType = DataBaseType.SqlServer |
| | | }; |
| | | listdatabaseSlaveDB.Add(mainDb); |
| | | for (int i = 0; i < list.Count; i++) |
| | | { |
| | | dynamic data = list[i]; |
| | | MutiDBOperate mutiDBOperate = new MutiDBOperate() |
| | | { |
| | | Connection = data.ConnectionString, |
| | | ConnId = data.TenantId + "", |
| | | DbType = (DataBaseType)data.DbType, |
| | | }; |
| | | mutiDBOperate.Connection = mutiDBOperate.Connection.DecryptDES(AppSecret.DB); |
| | | listdatabaseSlaveDB.Add(mutiDBOperate); |
| | | } |
| | | |
| | | return listdatabaseSlaveDB; |
| | | } |
| | | } |
| | | |
| | | public enum DataBaseType |
| | | { |
| | | MySql = 0, |
| | | SqlServer = 1, |
| | | Sqlite = 2, |
| | | Oracle = 3, |
| | | PostgreSQL = 4, |
| | | Dm = 5, |
| | | Kdbndp = 6, |
| | | } |
| | | |
| | | public class MutiDBOperate |
| | | { |
| | | /// <summary> |
| | | /// è¿æ¥å¯ç¨å¼å
³ |
| | | /// </summary> |
| | | public bool Enabled { get; set; } |
| | | |
| | | /// <summary> |
| | | /// è¿æ¥ID |
| | | /// </summary> |
| | | public string ConnId { get; set; } |
| | | |
| | | /// <summary> |
| | | /// ä»åºæ§è¡çº§å«ï¼è¶å¤§è¶å
æ§è¡ |
| | | /// </summary> |
| | | public int HitRate { get; set; } |
| | | |
| | | /// <summary> |
| | | /// è¿æ¥å符串 |
| | | /// </summary> |
| | | public string Connection { get; set; } |
| | | |
| | | /// <summary> |
| | | /// æ°æ®åºç±»å |
| | | /// </summary> |
| | | public DataBaseType DbType { get; set; } |
| | | } |
| | | } |