From bb23883a7d442bbda14fde28d91879d877bd2faa Mon Sep 17 00:00:00 2001 From: sunkaixuan <610262374@qq.com> Date: Fri, 28 Oct 2022 10:24:48 +0800 Subject: [PATCH] Support pgsql dictionary null --- .../Abstract/DbBindProvider/DbBindProvider.cs | 11 +++ .../InsertableProvider/InsertableHelper.cs | 10 ++ .../UpdateProvider/UpdateableHelper.cs | 9 ++ Src/Asp.Net/SqlSugar/Interface/IDbBind.cs | 1 + .../SqlBuilder/PostgreSQLBuilder.cs | 6 +- Src/Asp.Net/SqlSugar/Utilities/UtilMethods.cs | 97 +++++++++++++++++++ 6 files changed, 131 insertions(+), 3 deletions(-) diff --git a/Src/Asp.Net/SqlSugar/Abstract/DbBindProvider/DbBindProvider.cs b/Src/Asp.Net/SqlSugar/Abstract/DbBindProvider/DbBindProvider.cs index 2e3b34956..aa797e00c 100644 --- a/Src/Asp.Net/SqlSugar/Abstract/DbBindProvider/DbBindProvider.cs +++ b/Src/Asp.Net/SqlSugar/Abstract/DbBindProvider/DbBindProvider.cs @@ -41,6 +41,17 @@ namespace SqlSugar var mappings = this.MappingTypes.Where(it => it.Key == dbTypeName); return mappings.HasValue() ? mappings.First().Key : "string"; } + + public string GetCsharpTypeNameByDbTypeName(string dbTypeName) + { + var mappings = this.MappingTypes.Where(it => it.Key == dbTypeName); + if (mappings == null || mappings.Count() == 0) + { + return "string"; + } + var result = mappings.First().Value.ObjToString(); + return result; + } public virtual string GetConvertString(string dbTypeName) { string result = string.Empty; diff --git a/Src/Asp.Net/SqlSugar/Abstract/InsertableProvider/InsertableHelper.cs b/Src/Asp.Net/SqlSugar/Abstract/InsertableProvider/InsertableHelper.cs index 123821dff..9fd4f0d9e 100644 --- a/Src/Asp.Net/SqlSugar/Abstract/InsertableProvider/InsertableHelper.cs +++ b/Src/Asp.Net/SqlSugar/Abstract/InsertableProvider/InsertableHelper.cs @@ -103,6 +103,7 @@ namespace SqlSugar #endregion if (this.IsSingle) { + var isDic = this.EntityInfo.DbTableName.StartsWith("Dictionary`"); foreach (var item in this.InsertBuilder.DbColumnInfoList) { if (this.InsertBuilder.Parameters == null) this.InsertBuilder.Parameters = new List(); @@ -119,6 +120,15 @@ namespace SqlSugar { paramters.IsArray = true; } + if (item.Value == null && isDic) + { + var type = this.SqlBuilder.GetNullType(this.InsertBuilder.GetTableNameString, item.DbColumnName); + if (type != null) + { + paramters = new SugarParameter(this.SqlBuilder.SqlParameterKeyWord + item.DbColumnName, item.Value, type); + + } + } this.InsertBuilder.Parameters.Add(paramters); } } diff --git a/Src/Asp.Net/SqlSugar/Abstract/UpdateProvider/UpdateableHelper.cs b/Src/Asp.Net/SqlSugar/Abstract/UpdateProvider/UpdateableHelper.cs index f1f8a9f6a..059077283 100644 --- a/Src/Asp.Net/SqlSugar/Abstract/UpdateProvider/UpdateableHelper.cs +++ b/Src/Asp.Net/SqlSugar/Abstract/UpdateProvider/UpdateableHelper.cs @@ -268,6 +268,7 @@ namespace SqlSugar #endregion if (this.IsSingle) { + var isDic = this.EntityInfo.DbTableName.StartsWith("Dictionary`"); foreach (var item in this.UpdateBuilder.DbColumnInfoList) { if (this.UpdateBuilder.Parameters == null) this.UpdateBuilder.Parameters = new List(); @@ -284,6 +285,14 @@ namespace SqlSugar { parameter.IsArray = true; } + if (item.Value == null && isDic) + { + var type = this.SqlBuilder.GetNullType(this.UpdateBuilder.GetTableNameString, item.DbColumnName); + if (type != null) + { + parameter = new SugarParameter(this.SqlBuilder.SqlParameterKeyWord + item.DbColumnName, item.Value, type); + } + } this.UpdateBuilder.Parameters.Add(parameter); } } diff --git a/Src/Asp.Net/SqlSugar/Interface/IDbBind.cs b/Src/Asp.Net/SqlSugar/Interface/IDbBind.cs index 14e99c092..5c0ea1c73 100644 --- a/Src/Asp.Net/SqlSugar/Interface/IDbBind.cs +++ b/Src/Asp.Net/SqlSugar/Interface/IDbBind.cs @@ -21,6 +21,7 @@ namespace SqlSugar string GetConvertString(string dbTypeName); string GetDbTypeName(string csharpTypeName); string GetCsharpTypeName(string dbTypeName); + string GetCsharpTypeNameByDbTypeName(string dbTypeName); List> MappingTypes { get; } List DataReaderToList(Type type, IDataReader reader); Task> DataReaderToListAsync(Type entityType, IDataReader dataReader); diff --git a/Src/Asp.Net/SqlSugar/Realization/PostgreSQL/SqlBuilder/PostgreSQLBuilder.cs b/Src/Asp.Net/SqlSugar/Realization/PostgreSQL/SqlBuilder/PostgreSQLBuilder.cs index 53f8dd2eb..0590d6e0a 100644 --- a/Src/Asp.Net/SqlSugar/Realization/PostgreSQL/SqlBuilder/PostgreSQLBuilder.cs +++ b/Src/Asp.Net/SqlSugar/Realization/PostgreSQL/SqlBuilder/PostgreSQLBuilder.cs @@ -108,12 +108,12 @@ namespace SqlSugar var columnInfo=this.Context.DbMaintenance.GetColumnInfosByTableName(tableName).FirstOrDefault(z => z.DbColumnName.EqualCase(columnName)); if (columnInfo != null) { - var cTypeName=this.Context.Ado.DbBind.GetCsharpTypeName(UtilConstants.StringType.Name); - var value=UtilMethods.ConvertDataByTypeName(cTypeName, ""); + var cTypeName=this.Context.Ado.DbBind.GetCsharpTypeNameByDbTypeName(columnInfo.DataType); + var value=UtilMethods.GetTypeByTypeName(cTypeName); if (value != null) { var key = "GetNullType_" + tableName + columnName; - return new ReflectionInoCacheService().GetOrCreate(key, () => value.GetType()); + return new ReflectionInoCacheService().GetOrCreate(key, () => value); } } return null; diff --git a/Src/Asp.Net/SqlSugar/Utilities/UtilMethods.cs b/Src/Asp.Net/SqlSugar/Utilities/UtilMethods.cs index 55896d176..a642d3d7c 100644 --- a/Src/Asp.Net/SqlSugar/Utilities/UtilMethods.cs +++ b/Src/Asp.Net/SqlSugar/Utilities/UtilMethods.cs @@ -636,6 +636,103 @@ namespace SqlSugar } return dic; } + + public static Type GetTypeByTypeName(string ctypename) + { + + if (ctypename.EqualCase(UtilConstants.DecType.Name)) + { + return UtilConstants.DecType; + } + else if (ctypename.EqualCase(UtilConstants.DobType.Name)) + { + return UtilConstants.DobType; + } + else if (ctypename.EqualCase(UtilConstants.DateType.Name)) + { + return UtilConstants.DateType; + } + else if (ctypename.EqualCase(UtilConstants.IntType.Name)) + { + return UtilConstants.IntType; + } + else if (ctypename.EqualCase(UtilConstants.BoolType.Name)) + { + return UtilConstants.BoolType; + } + else if (ctypename.EqualCase(UtilConstants.LongType.Name)) + { + return UtilConstants.LongType; + } + else if (ctypename.EqualCase(UtilConstants.ShortType.Name)) + { + return UtilConstants.ShortType; + } + else if (ctypename.EqualCase(UtilConstants.DateTimeOffsetType.Name)) + { + return UtilConstants.DateTimeOffsetType; + } + else if (ctypename.EqualCase(UtilConstants.GuidType.Name)) + { + return UtilConstants.GuidType; + } + else if (ctypename.EqualCase("int")) + { + return UtilConstants.IntType; + } + else if (ctypename.EqualCase("long")) + { + return UtilConstants.LongType; + } + else if (ctypename.EqualCase("short")) + { + return UtilConstants.ShortType; + } + else if (ctypename.EqualCase("byte")) + { + return UtilConstants.ByteType; + } + else if (ctypename.EqualCase("uint")) + { + return UtilConstants.UIntType; + } + else if (ctypename.EqualCase("ulong")) + { + return UtilConstants.ULongType; + } + else if (ctypename.EqualCase("ushort")) + { + return UtilConstants.UShortType; + } + else if (ctypename.EqualCase("uint32")) + { + return UtilConstants.UIntType; + } + else if (ctypename.EqualCase("uint64")) + { + return UtilConstants.ULongType; + } + else if (ctypename.EqualCase("bool")) + { + return UtilConstants.BoolType; + } + else if (ctypename.EqualCase("ToBoolean")) + { + return UtilConstants.BoolType; + } + else if (ctypename.EqualCase("uint16")) + { + return UtilConstants.UShortType; + } + else if (ctypename.EqualCase(UtilConstants.ByteArrayType.Name)) + { + return UtilConstants.ByteArrayType; + } + else + { + return UtilConstants.StringType; + } + } public static object ConvertDataByTypeName(string ctypename,string value) { var item = new ConditionalModel() {