diff --git a/Src/Asp.Net/SqlSugar/Abstract/DbBindProvider/IDataReaderEntityBuilder.cs b/Src/Asp.Net/SqlSugar/Abstract/DbBindProvider/IDataReaderEntityBuilder.cs index 79a552630..55fae090d 100644 --- a/Src/Asp.Net/SqlSugar/Abstract/DbBindProvider/IDataReaderEntityBuilder.cs +++ b/Src/Asp.Net/SqlSugar/Abstract/DbBindProvider/IDataReaderEntityBuilder.cs @@ -189,7 +189,7 @@ namespace SqlSugar generator.Emit(OpCodes.Callvirt, columnInfo.PropertyInfo.GetSetMethod(true)); generator.MarkLabel(endIfLabel); } - else if (columnInfo.UnderType == typeof(XElement)) + else if (columnInfo.UnderType == typeof(XElement)||columnInfo.SqlParameterDbType is Type) { int i = DataRecord.GetOrdinal(fieldName); Label endIfLabel = generator.DefineLabel(); @@ -203,7 +203,7 @@ namespace SqlSugar BindMethod(generator, columnInfo, i); generator.Emit(OpCodes.Callvirt, columnInfo.PropertyInfo.GetSetMethod(true)); generator.MarkLabel(endIfLabel); - } + } } private void BindField(ILGenerator generator, LocalBuilder result, EntityColumnInfo columnInfo, string fieldName) { @@ -259,7 +259,7 @@ namespace SqlSugar { method = isNullableType ? getOtherNull.MakeGenericMethod(bindPropertyType) : getOther.MakeGenericMethod(bindPropertyType); } - else if (dbTypeName.EqualCase("STRING")) + else if (dbTypeName.EqualCase("STRING")) { method = isNullableType ? getOtherNull.MakeGenericMethod(bindPropertyType) : getOther.MakeGenericMethod(bindPropertyType); } @@ -271,7 +271,7 @@ namespace SqlSugar { method = isNullableType ? getOtherNull.MakeGenericMethod(bindPropertyType) : getOther.MakeGenericMethod(bindPropertyType); } - + method = GetMethodConvert(columnInfo, method); if (method.IsVirtual) generator.Emit(OpCodes.Callvirt, method); else @@ -403,6 +403,8 @@ namespace SqlSugar if (method == null) method = isNullableType ? getOtherNull.MakeGenericMethod(bindPropertyType) : getOther.MakeGenericMethod(bindPropertyType); + method = GetMethodConvert(columnInfo, method); + if (method.IsVirtual) generator.Emit(OpCodes.Callvirt, method); else @@ -410,6 +412,17 @@ namespace SqlSugar #endregion } + private static MethodInfo GetMethodConvert(EntityColumnInfo columnInfo, MethodInfo method) + { + if (columnInfo.SqlParameterDbType is Type) + { + method = (columnInfo.SqlParameterDbType as Type).GetMethod("QueryConverter"); + method=method.MakeGenericMethod(new Type[] { columnInfo.PropertyInfo.PropertyType }); + } + + return method; + } + private void CheckType(List invalidTypes, string bindProperyTypeName, string validPropertyType, string propertyName) { var isAny = invalidTypes.Contains(bindProperyTypeName); diff --git a/Src/Asp.Net/SqlSugar/Abstract/InsertableProvider/InsertableHelper.cs b/Src/Asp.Net/SqlSugar/Abstract/InsertableProvider/InsertableHelper.cs index 55c1aee9f..ab6bf8d6f 100644 --- a/Src/Asp.Net/SqlSugar/Abstract/InsertableProvider/InsertableHelper.cs +++ b/Src/Asp.Net/SqlSugar/Abstract/InsertableProvider/InsertableHelper.cs @@ -112,6 +112,10 @@ namespace SqlSugar { continue; } + if (item.SqlParameterDbType is Type) + { + continue; + } if (item.IsJson) { paramters.IsJson = true; @@ -217,7 +221,8 @@ namespace SqlSugar TableId = i, InsertSql = column.InsertSql, InsertServerTime = column.InsertServerTime, - DataType=column.DataType + DataType=column.DataType, + SqlParameterDbType= column.SqlParameterDbType }; if (column.DbColumnName == null) diff --git a/Src/Asp.Net/SqlSugar/Abstract/SqlBuilderProvider/InsertBuilder.cs b/Src/Asp.Net/SqlSugar/Abstract/SqlBuilderProvider/InsertBuilder.cs index ad796724f..edb10f787 100644 --- a/Src/Asp.Net/SqlSugar/Abstract/SqlBuilderProvider/InsertBuilder.cs +++ b/Src/Asp.Net/SqlSugar/Abstract/SqlBuilderProvider/InsertBuilder.cs @@ -284,6 +284,17 @@ namespace SqlSugar { return columnInfo.InsertSql; } + else if (columnInfo.SqlParameterDbType is Type) + { + var type=columnInfo.SqlParameterDbType as Type; + var ParameterConverter=type.GetMethod("ParameterConverter"); + //var obj=Activator.CreateInstance(type); + var p = ParameterConverter.Invoke(null,new object[] {columnInfo.Value, GetDbColumnIndex }) as SugarParameter; + GetDbColumnIndex++; + //this.Parameters.RemoveAll(it => it.ParameterName == it.ParameterName); + this.Parameters.Add(p); + return p.ParameterName; + } else if (columnInfo.DataType?.Equals("nvarchar2")==true) { var pname = Builder.SqlParameterKeyWord + columnInfo.DbColumnName + "_ts" + GetDbColumnIndex; diff --git a/Src/Asp.Net/SqlSugar/Abstract/SqlBuilderProvider/UpdateBuilder.cs b/Src/Asp.Net/SqlSugar/Abstract/SqlBuilderProvider/UpdateBuilder.cs index a0c8c0188..3b06917bb 100644 --- a/Src/Asp.Net/SqlSugar/Abstract/SqlBuilderProvider/UpdateBuilder.cs +++ b/Src/Asp.Net/SqlSugar/Abstract/SqlBuilderProvider/UpdateBuilder.cs @@ -401,6 +401,17 @@ namespace SqlSugar { return columnInfo.UpdateSql; } + else if (columnInfo.SqlParameterDbType is Type) + { + var type = columnInfo.SqlParameterDbType as Type; + var ParameterConverter = type.GetMethod("ParameterConverter"); + //var obj = Activator.CreateInstance(type); + var p = ParameterConverter.Invoke(null,new object[] { columnInfo.Value, GetDbColumnIndex }) as SugarParameter; + GetDbColumnIndex++; + //this.Parameters.RemoveAll(it => it.ParameterName == it.ParameterName); + this.Parameters.Add(p); + return p.ParameterName; + } else if (columnInfo.PropertyType.Name == "TimeOnly" && name != null && !name.ObjToString().StartsWith(Builder.SqlParameterKeyWord)) { var timeSpan = UtilMethods.TimeOnlyToTimeSpan(columnInfo.Value); @@ -414,20 +425,20 @@ namespace SqlSugar return name + ""; } } - public virtual string GetDbColumn(DbColumnInfo columnInfo, string name) - { - if (columnInfo.UpdateServerTime) - { - return LambdaExpressions.DbMehtods.GetDate(); - } - else if (columnInfo.UpdateSql.HasValue()) - { - return columnInfo.UpdateSql; - } - else - { - return name + ""; - } - } + //public virtual string GetDbColumn(DbColumnInfo columnInfo, string name) + //{ + // if (columnInfo.UpdateServerTime) + // { + // return LambdaExpressions.DbMehtods.GetDate(); + // } + // else if (columnInfo.UpdateSql.HasValue()) + // { + // return columnInfo.UpdateSql; + // } + // else + // { + // return name + ""; + // } + //} } } diff --git a/Src/Asp.Net/SqlSugar/Abstract/UpdateProvider/UpdateableHelper.cs b/Src/Asp.Net/SqlSugar/Abstract/UpdateProvider/UpdateableHelper.cs index 5813ab7c6..c090d0ce9 100644 --- a/Src/Asp.Net/SqlSugar/Abstract/UpdateProvider/UpdateableHelper.cs +++ b/Src/Asp.Net/SqlSugar/Abstract/UpdateProvider/UpdateableHelper.cs @@ -278,6 +278,10 @@ namespace SqlSugar { continue; } + if (item.SqlParameterDbType is Type) + { + continue; + } var parameter = new SugarParameter(this.SqlBuilder.SqlParameterKeyWord + item.DbColumnName, item.Value, item.PropertyType); if (item.IsJson) { diff --git a/Src/Asp.Net/SqlSugar/Entities/DefaultCustom.cs b/Src/Asp.Net/SqlSugar/Entities/DefaultCustom.cs new file mode 100644 index 000000000..03d21ba90 --- /dev/null +++ b/Src/Asp.Net/SqlSugar/Entities/DefaultCustom.cs @@ -0,0 +1,27 @@ +using System; +using System.Collections.Generic; +using System.Data; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace SqlSugar.DbConvert +{ + public static class EnumToStringConvert + { + public static SugarParameter ParameterConverter(object value, int i) + { + var name = "@myenmu" + i; + var enumToString = value?.ToString(); + return new SugarParameter(name, enumToString); + } + + public static T QueryConverter(this IDataRecord dr, int i) + { + + var str = dr.GetString(i); + Type undertype = SqlSugar.UtilMethods.GetUnderType(typeof(T));//获取没有nullable的枚举类型 + return (T)Enum.Parse(undertype, str); + } + } +} diff --git a/Src/Asp.Net/SqlSugar/SqlSugar.csproj b/Src/Asp.Net/SqlSugar/SqlSugar.csproj index b3a6f6912..ed3905991 100644 --- a/Src/Asp.Net/SqlSugar/SqlSugar.csproj +++ b/Src/Asp.Net/SqlSugar/SqlSugar.csproj @@ -140,6 +140,7 @@ + diff --git a/Src/Asp.Net/SqlSugar/Utilities/UtilMethods.cs b/Src/Asp.Net/SqlSugar/Utilities/UtilMethods.cs index f53344a61..18551bae4 100644 --- a/Src/Asp.Net/SqlSugar/Utilities/UtilMethods.cs +++ b/Src/Asp.Net/SqlSugar/Utilities/UtilMethods.cs @@ -329,7 +329,7 @@ namespace SqlSugar } } - internal static Type GetUnderType(Type oldType) + public static Type GetUnderType(Type oldType) { Type type = Nullable.GetUnderlyingType(oldType); return type == null ? oldType : type;