From aa3e6a4d8f9918b872e16798f9a104cde3f26814 Mon Sep 17 00:00:00 2001 From: sunkaixuan <610262374@qq.com> Date: Wed, 31 May 2023 19:04:13 +0800 Subject: [PATCH] Update sqlsugarcustom db --- .../SugarProvider/SqlSugarAccessory.cs | 2 +- .../Infrastructure/InstanceFactory.cs | 97 ++++++++++++++++++- 2 files changed, 97 insertions(+), 2 deletions(-) diff --git a/Src/Asp.NetCore2/SqlSugar/Abstract/SugarProvider/SqlSugarAccessory.cs b/Src/Asp.NetCore2/SqlSugar/Abstract/SugarProvider/SqlSugarAccessory.cs index 7af144790..081ba2b24 100644 --- a/Src/Asp.NetCore2/SqlSugar/Abstract/SugarProvider/SqlSugarAccessory.cs +++ b/Src/Asp.NetCore2/SqlSugar/Abstract/SugarProvider/SqlSugarAccessory.cs @@ -442,7 +442,7 @@ namespace SqlSugar Check.ExceptionEasy("Use DbType.PostgreSQL , ConnectionString add No Reset On Close=true", "OpenGausso数据库请使用DbType.PostgreSQL 并且连接字符串加上 No Reset On Close=true"); break; case DbType.HG: - Check.ExceptionEasy("Use DbType.PostgreSQL", "瀚高数据库请使用DbType.PostgreSQL"); + InstanceFactory.CustomDllName = SugarCompatible.IsFramework ? throw new Exception("Only.NET CORE is supported") : "SqlSugar.HGCore"; break; case DbType.Kdbndp: DependencyManagement.TryKdbndb(); diff --git a/Src/Asp.NetCore2/SqlSugar/Infrastructure/InstanceFactory.cs b/Src/Asp.NetCore2/SqlSugar/Infrastructure/InstanceFactory.cs index b4fcdd6fd..878e7de56 100644 --- a/Src/Asp.NetCore2/SqlSugar/Infrastructure/InstanceFactory.cs +++ b/Src/Asp.NetCore2/SqlSugar/Infrastructure/InstanceFactory.cs @@ -249,12 +249,40 @@ namespace SqlSugar { return new KdbndpInserttable(); } + else if (IsCustomDb(currentConnectionConfig)) + { + var name = + "SqlSugar." + currentConnectionConfig.DbType + + "." + currentConnectionConfig.DbType + + "Insertable`1"; + var type = GetCustomTypeByClass(name); + if (type == null) + { + return new InsertableProvider(); + } + else + { + return (InsertableProvider)Activator.CreateInstance(type, true); + } + } else { return new InsertableProvider(); } } + private static bool IsCustomDb(ConnectionConfig currentConnectionConfig) + { + return + currentConnectionConfig.DbType != DbType.SqlServer && + currentConnectionConfig.DbType != DbType.Dm && + currentConnectionConfig.DbType != DbType.Oscar && + currentConnectionConfig.DbType != DbType.Access && + currentConnectionConfig.DbType != DbType.QuestDB && + currentConnectionConfig.DbType != DbType.MySql && + GetCustomTypeByClass("SqlSugar." + currentConnectionConfig.DbType + "." + currentConnectionConfig.DbType + "Provider") != null; + } + public static IDbBind GetDbBind(ConnectionConfig currentConnectionConfig) { if (currentConnectionConfig.DbType == DbType.SqlServer) @@ -467,7 +495,11 @@ namespace SqlSugar } if (type == null) { - type = Type.GetType(className + "`" + types.Length, true).MakeGenericType(types); + type = Type.GetType(className + "`" + types.Length)?.MakeGenericType(types); + if (type == null) + { + type = GetCustomDbType(className + "`" + types.Length, type).MakeGenericType(types); + } } } var result = (Restult)Activator.CreateInstance(type, true); @@ -537,10 +569,35 @@ namespace SqlSugar { type = GetCustomTypeByClass(className); } + if (type == null) + { + type = GetCustomDbType(className, type); + } var result = (T)Activator.CreateInstance(type, true); return result; } + private static Type GetCustomDbType(string className, Type type) + { + if (className.Replace(".", "").Length + 1 == className.Length) + { + var array = className.Split('.'); + foreach (var item in UtilMethods.EnumToDictionary()) + { + if (array.Last().StartsWith(item.Value.ToString())) + { + + var newName = array.First() + "." + item.Value.ToString() + "." + array.Last(); + type = GetCustomTypeByClass(newName); + break; + } + } + + } + + return type; + } + internal static Type GetCustomTypeByClass(string className) { var key = "Assembly_"+ CustomDllName+assembly.GetHashCode(); @@ -576,6 +633,44 @@ namespace SqlSugar Type type = newAssembly.GetType(className); return type; } + + internal static Type GetCustomTypeByClass(string className) + { + var key = "Assembly_" + CustomDllName + assembly.GetHashCode(); + var newAssembly = new ReflectionInoCacheService().GetOrCreate(key, () => { + try + { + var path = Assembly.GetExecutingAssembly().Location; + if (path.HasValue()) + { + path = System.IO.Path.Combine(System.IO.Path.GetDirectoryName(path), CustomDllName + ".dll"); + } + if (path.HasValue() && FileHelper.IsExistFile(path)) + { + return Assembly.LoadFrom(path); + } + else + { + if (IsWebFrom) + { + string newpath = (System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().GetName().CodeBase) + "\\" + CustomDllName + ".dll").Replace("file:\\", ""); + return Assembly.LoadFrom(newpath); + } + return Assembly.LoadFrom(CustomDllName + ".dll"); + } + } + catch + { + var message = "Not Found " + CustomDllName + ".dll"; + Check.Exception(true, message); + return null; + } + }); + Type typeArgument = typeof(T); + string fullTypeName = className + "[[" + typeArgument.FullName+","+ typeArgument.Assembly.FullName+ "]]"; + Type type = newAssembly.GetType(fullTypeName); + return type; + } #endregion } }