From 30cf83149dd1072c06a09e722dad7ba5d3cb7199 Mon Sep 17 00:00:00 2001 From: sunkaixuan <610262374@qq.com> Date: Wed, 31 May 2023 19:12:05 +0800 Subject: [PATCH] Synchronization code --- .../Abstract/SqlBuilderProvider/Entities.cs | 4 +- .../SqlBuilderProvider/UpdateBuilder.cs | 2 +- .../SugarProvider/SqlSugarAccessory.cs | 4 +- Src/Asp.Net/SqlSugar/Entities/DbColumnInfo.cs | 2 +- .../Infrastructure/InstanceFactory.cs | 97 ++++++++++++++++++- 5 files changed, 102 insertions(+), 7 deletions(-) diff --git a/Src/Asp.Net/SqlSugar/Abstract/SqlBuilderProvider/Entities.cs b/Src/Asp.Net/SqlSugar/Abstract/SqlBuilderProvider/Entities.cs index c69a220f9..ff241cf2b 100644 --- a/Src/Asp.Net/SqlSugar/Abstract/SqlBuilderProvider/Entities.cs +++ b/Src/Asp.Net/SqlSugar/Abstract/SqlBuilderProvider/Entities.cs @@ -6,13 +6,13 @@ using System.Threading.Tasks; namespace SqlSugar { - internal class ReSetValueBySqlExpListModel + public class ReSetValueBySqlExpListModel { public string DbColumnName { get; set; } public string Sql { get; set; } public ReSetValueBySqlExpListModelType? Type { get; set; } } - internal enum ReSetValueBySqlExpListModelType + public enum ReSetValueBySqlExpListModelType { Default, List diff --git a/Src/Asp.Net/SqlSugar/Abstract/SqlBuilderProvider/UpdateBuilder.cs b/Src/Asp.Net/SqlSugar/Abstract/SqlBuilderProvider/UpdateBuilder.cs index 4f6fc4545..c8657c79f 100644 --- a/Src/Asp.Net/SqlSugar/Abstract/SqlBuilderProvider/UpdateBuilder.cs +++ b/Src/Asp.Net/SqlSugar/Abstract/SqlBuilderProvider/UpdateBuilder.cs @@ -36,7 +36,7 @@ namespace SqlSugar public List UpdateColumns { get; set; } public List JoinInfos { get; set; } public string ShortName { get; set; } - internal Dictionary ReSetValueBySqlExpList { get; set; } + public Dictionary ReSetValueBySqlExpList { get; set; } public virtual string ReSetValueBySqlExpListType { get; set; } public virtual string SqlTemplate { diff --git a/Src/Asp.Net/SqlSugar/Abstract/SugarProvider/SqlSugarAccessory.cs b/Src/Asp.Net/SqlSugar/Abstract/SugarProvider/SqlSugarAccessory.cs index e6ef7e500..081ba2b24 100644 --- a/Src/Asp.Net/SqlSugar/Abstract/SugarProvider/SqlSugarAccessory.cs +++ b/Src/Asp.Net/SqlSugar/Abstract/SugarProvider/SqlSugarAccessory.cs @@ -412,7 +412,7 @@ namespace SqlSugar } return N; } - internal bool IsVarchar() + public bool IsVarchar() { if (_Context.CurrentConnectionConfig.MoreSettings != null && _Context.CurrentConnectionConfig.MoreSettings.DisableNvarchar) { @@ -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.Net/SqlSugar/Entities/DbColumnInfo.cs b/Src/Asp.Net/SqlSugar/Entities/DbColumnInfo.cs index 03d545d99..2b6e1e314 100644 --- a/Src/Asp.Net/SqlSugar/Entities/DbColumnInfo.cs +++ b/Src/Asp.Net/SqlSugar/Entities/DbColumnInfo.cs @@ -30,6 +30,6 @@ namespace SqlSugar public string InsertSql { get; set; } public bool UpdateServerTime { get; set; } public string UpdateSql { get; set; } - internal object SqlParameterDbType { get; set; } + public object SqlParameterDbType { get; set; } } } diff --git a/Src/Asp.Net/SqlSugar/Infrastructure/InstanceFactory.cs b/Src/Asp.Net/SqlSugar/Infrastructure/InstanceFactory.cs index b4fcdd6fd..878e7de56 100644 --- a/Src/Asp.Net/SqlSugar/Infrastructure/InstanceFactory.cs +++ b/Src/Asp.Net/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 } }