mirror of
https://gitee.com/dotnetchina/SqlSugar.git
synced 2026-02-27 16:50:33 +08:00
Supports user-defined types
This commit is contained in:
@@ -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();
|
||||||
@@ -203,7 +203,7 @@ namespace SqlSugar
|
|||||||
BindMethod(generator, columnInfo, i);
|
BindMethod(generator, columnInfo, i);
|
||||||
generator.Emit(OpCodes.Callvirt, columnInfo.PropertyInfo.GetSetMethod(true));
|
generator.Emit(OpCodes.Callvirt, columnInfo.PropertyInfo.GetSetMethod(true));
|
||||||
generator.MarkLabel(endIfLabel);
|
generator.MarkLabel(endIfLabel);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
private void BindField(ILGenerator generator, LocalBuilder result, EntityColumnInfo columnInfo, string fieldName)
|
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);
|
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);
|
method = isNullableType ? getOtherNull.MakeGenericMethod(bindPropertyType) : getOther.MakeGenericMethod(bindPropertyType);
|
||||||
}
|
}
|
||||||
@@ -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);
|
||||||
|
|||||||
@@ -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)
|
||||||
|
|||||||
@@ -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;
|
||||||
|
|||||||
@@ -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 + "";
|
||||||
}
|
// }
|
||||||
}
|
//}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -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
Normal file
27
Src/Asp.Net/SqlSugar/Entities/DefaultCustom.cs
Normal file
@@ -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);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -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" />
|
||||||
|
|||||||
@@ -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;
|
||||||
|
|||||||
Reference in New Issue
Block a user