Browse Source

Supports user-defined types

pull/31/head
sunkaixuan 2 years ago
parent
commit
d018de1a28
  1. 17
      Src/Asp.Net/SqlSugar/Abstract/DbBindProvider/IDataReaderEntityBuilder.cs
  2. 7
      Src/Asp.Net/SqlSugar/Abstract/InsertableProvider/InsertableHelper.cs
  3. 11
      Src/Asp.Net/SqlSugar/Abstract/SqlBuilderProvider/InsertBuilder.cs
  4. 41
      Src/Asp.Net/SqlSugar/Abstract/SqlBuilderProvider/UpdateBuilder.cs
  5. 4
      Src/Asp.Net/SqlSugar/Abstract/UpdateProvider/UpdateableHelper.cs
  6. 27
      Src/Asp.Net/SqlSugar/Entities/DefaultCustom.cs
  7. 1
      Src/Asp.Net/SqlSugar/SqlSugar.csproj
  8. 2
      Src/Asp.Net/SqlSugar/Utilities/UtilMethods.cs

17
Src/Asp.Net/SqlSugar/Abstract/DbBindProvider/IDataReaderEntityBuilder.cs

@ -189,7 +189,7 @@ namespace SqlSugar
generator.Emit(OpCodes.Callvirt, columnInfo.PropertyInfo.GetSetMethod(true)); generator.Emit(OpCodes.Callvirt, columnInfo.PropertyInfo.GetSetMethod(true));
generator.MarkLabel(endIfLabel); generator.MarkLabel(endIfLabel);
} }
else if (columnInfo.UnderType == typeof(XElement)) else if (columnInfo.UnderType == typeof(XElement)||columnInfo.SqlParameterDbType is Type)
{ {
int i = DataRecord.GetOrdinal(fieldName); int i = DataRecord.GetOrdinal(fieldName);
Label endIfLabel = generator.DefineLabel(); Label endIfLabel = generator.DefineLabel();
@ -271,7 +271,7 @@ namespace SqlSugar
{ {
method = isNullableType ? getOtherNull.MakeGenericMethod(bindPropertyType) : getOther.MakeGenericMethod(bindPropertyType); method = isNullableType ? getOtherNull.MakeGenericMethod(bindPropertyType) : getOther.MakeGenericMethod(bindPropertyType);
} }
method = GetMethodConvert(columnInfo, method);
if (method.IsVirtual) if (method.IsVirtual)
generator.Emit(OpCodes.Callvirt, method); generator.Emit(OpCodes.Callvirt, method);
else else
@ -403,6 +403,8 @@ namespace SqlSugar
if (method == null) if (method == null)
method = isNullableType ? getOtherNull.MakeGenericMethod(bindPropertyType) : getOther.MakeGenericMethod(bindPropertyType); method = isNullableType ? getOtherNull.MakeGenericMethod(bindPropertyType) : getOther.MakeGenericMethod(bindPropertyType);
method = GetMethodConvert(columnInfo, method);
if (method.IsVirtual) if (method.IsVirtual)
generator.Emit(OpCodes.Callvirt, method); generator.Emit(OpCodes.Callvirt, method);
else else
@ -410,6 +412,17 @@ namespace SqlSugar
#endregion #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<string> invalidTypes, string bindProperyTypeName, string validPropertyType, string propertyName) private void CheckType(List<string> invalidTypes, string bindProperyTypeName, string validPropertyType, string propertyName)
{ {
var isAny = invalidTypes.Contains(bindProperyTypeName); var isAny = invalidTypes.Contains(bindProperyTypeName);

7
Src/Asp.Net/SqlSugar/Abstract/InsertableProvider/InsertableHelper.cs

@ -112,6 +112,10 @@ namespace SqlSugar
{ {
continue; continue;
} }
if (item.SqlParameterDbType is Type)
{
continue;
}
if (item.IsJson) if (item.IsJson)
{ {
paramters.IsJson = true; paramters.IsJson = true;
@ -217,7 +221,8 @@ namespace SqlSugar
TableId = i, TableId = i,
InsertSql = column.InsertSql, InsertSql = column.InsertSql,
InsertServerTime = column.InsertServerTime, InsertServerTime = column.InsertServerTime,
DataType=column.DataType DataType=column.DataType,
SqlParameterDbType= column.SqlParameterDbType
}; };
if (column.DbColumnName == null) if (column.DbColumnName == null)

11
Src/Asp.Net/SqlSugar/Abstract/SqlBuilderProvider/InsertBuilder.cs

@ -284,6 +284,17 @@ namespace SqlSugar
{ {
return columnInfo.InsertSql; 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) else if (columnInfo.DataType?.Equals("nvarchar2")==true)
{ {
var pname = Builder.SqlParameterKeyWord + columnInfo.DbColumnName + "_ts" + GetDbColumnIndex; var pname = Builder.SqlParameterKeyWord + columnInfo.DbColumnName + "_ts" + GetDbColumnIndex;

41
Src/Asp.Net/SqlSugar/Abstract/SqlBuilderProvider/UpdateBuilder.cs

@ -401,6 +401,17 @@ namespace SqlSugar
{ {
return columnInfo.UpdateSql; 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)) else if (columnInfo.PropertyType.Name == "TimeOnly" && name != null && !name.ObjToString().StartsWith(Builder.SqlParameterKeyWord))
{ {
var timeSpan = UtilMethods.TimeOnlyToTimeSpan(columnInfo.Value); var timeSpan = UtilMethods.TimeOnlyToTimeSpan(columnInfo.Value);
@ -414,20 +425,20 @@ namespace SqlSugar
return name + ""; return name + "";
} }
} }
public virtual string GetDbColumn(DbColumnInfo columnInfo, string name) //public virtual string GetDbColumn(DbColumnInfo columnInfo, string name)
{ //{
if (columnInfo.UpdateServerTime) // if (columnInfo.UpdateServerTime)
{ // {
return LambdaExpressions.DbMehtods.GetDate(); // return LambdaExpressions.DbMehtods.GetDate();
} // }
else if (columnInfo.UpdateSql.HasValue()) // else if (columnInfo.UpdateSql.HasValue())
{ // {
return columnInfo.UpdateSql; // return columnInfo.UpdateSql;
} // }
else // else
{ // {
return name + ""; // return name + "";
} // }
} //}
} }
} }

4
Src/Asp.Net/SqlSugar/Abstract/UpdateProvider/UpdateableHelper.cs

@ -278,6 +278,10 @@ namespace SqlSugar
{ {
continue; continue;
} }
if (item.SqlParameterDbType is Type)
{
continue;
}
var parameter = new SugarParameter(this.SqlBuilder.SqlParameterKeyWord + item.DbColumnName, item.Value, item.PropertyType); var parameter = new SugarParameter(this.SqlBuilder.SqlParameterKeyWord + item.DbColumnName, item.Value, item.PropertyType);
if (item.IsJson) if (item.IsJson)
{ {

27
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<T>(this IDataRecord dr, int i)
{
var str = dr.GetString(i);
Type undertype = SqlSugar.UtilMethods.GetUnderType(typeof(T));//获取没有nullable的枚举类型
return (T)Enum.Parse(undertype, str);
}
}
}

1
Src/Asp.Net/SqlSugar/SqlSugar.csproj

@ -140,6 +140,7 @@
<Compile Include="Abstract\UpdateProvider\UpdateableHelper.cs" /> <Compile Include="Abstract\UpdateProvider\UpdateableHelper.cs" />
<Compile Include="Abstract\UpdateProvider\SplitTableUpdateByObjectProvider.cs" /> <Compile Include="Abstract\UpdateProvider\SplitTableUpdateByObjectProvider.cs" />
<Compile Include="Entities\DbFastestProperties.cs" /> <Compile Include="Entities\DbFastestProperties.cs" />
<Compile Include="Entities\DefaultCustom.cs" />
<Compile Include="Entities\DeleteNavOptions.cs" /> <Compile Include="Entities\DeleteNavOptions.cs" />
<Compile Include="Entities\JoinInfoParameter.cs" /> <Compile Include="Entities\JoinInfoParameter.cs" />
<Compile Include="ExpressionsToSql\Common\MapperSql.cs" /> <Compile Include="ExpressionsToSql\Common\MapperSql.cs" />

2
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); Type type = Nullable.GetUnderlyingType(oldType);
return type == null ? oldType : type; return type == null ? oldType : type;

Loading…
Cancel
Save