内部 ColumnInfo 调整

This commit is contained in:
28810
2020-08-18 18:14:02 +08:00
parent 90b6d76510
commit 8cec3e396b
31 changed files with 223 additions and 102 deletions

View File

@ -1,4 +1,5 @@
using System;
using FreeSql.Internal.Model;
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Linq;
@ -27,7 +28,6 @@ namespace FreeSql.Extensions.EntityUtil
/// <param name="genGuid">当Guid无值时会生成有序的新值</param>
/// <param name="splitString"></param>
/// <returns></returns>
//public static string GetEntityKeyString<TEntity>(this IFreeSql orm, TEntity entity, string splitString = "*|_,[,_|*") => GetEntityKeyString(orm, typeof(TEntity), entity, splitString);
public static string GetEntityKeyString(this IFreeSql orm, Type entityType, object entity, bool genGuid, string splitString = "*|_,[,_|*")
{
if (entity == null) return null;
@ -141,7 +141,6 @@ namespace FreeSql.Extensions.EntityUtil
/// <param name="entityType"></param>
/// <param name="entity"></param>
/// <returns></returns>
//public static object[] GetEntityKeyValues<TEntity>(this IFreeSql orm, TEntity entity) => GetEntityKeyValues(orm, typeof(TEntity), entity);
public static object[] GetEntityKeyValues(this IFreeSql orm, Type entityType, object entity)
{
if (entity == null) return null;
@ -184,7 +183,6 @@ namespace FreeSql.Extensions.EntityUtil
/// <param name="entity"></param>
/// <param name="propertyName"></param>
/// <returns></returns>
//public static object GetEntityValueWithPropertyName<TEntity>(this IFreeSql orm, TEntity entity, string propertyName) => GetEntityKeyValues(orm, typeof(TEntity), entity, propertyName);
public static object GetEntityValueWithPropertyName(this IFreeSql orm, Type entityType, object entity, string propertyName)
{
if (entity == null) return null;
@ -194,8 +192,8 @@ namespace FreeSql.Extensions.EntityUtil
.GetOrAdd(entityType, et => new ConcurrentDictionary<string, Func<object, object>>())
.GetOrAdd(propertyName, pn =>
{
var _table = orm.CodeFirst.GetTableByEntity(entityType);
var pks = _table.Primarys;
var table = orm.CodeFirst.GetTableByEntity(entityType);
var pks = table.Primarys;
var returnTarget = Expression.Label(typeof(object));
var parm1 = Expression.Parameter(typeof(object));
var var1Parm = Expression.Variable(entityType);
@ -204,7 +202,35 @@ namespace FreeSql.Extensions.EntityUtil
Expression.Assign(var1Parm, Expression.TypeAs(parm1, entityType)),
Expression.Assign(
var2Ret,
Expression.Convert(Expression.MakeMemberAccess(var1Parm, _table.Properties[pn]), typeof(object))
Expression.Convert(Expression.MakeMemberAccess(var1Parm, table.Properties[pn]), typeof(object))
)
});
exps.AddRange(new Expression[] {
Expression.Return(returnTarget, var2Ret),
Expression.Label(returnTarget, Expression.Default(typeof(object)))
});
return Expression.Lambda<Func<object, object>>(Expression.Block(new[] { var1Parm, var2Ret }, exps), new[] { parm1 }).Compile();
});
return func(entity);
}
public static object GetPropertyValue(this TableInfo table, object entity, string propertyName)
{
if (table == null || entity == null) return null;
var func = _dicGetEntityValueWithPropertyName
.GetOrAdd(DataType.MsAccess, dt => new ConcurrentDictionary<Type, ConcurrentDictionary<string, Func<object, object>>>())
.GetOrAdd(table.Type, et => new ConcurrentDictionary<string, Func<object, object>>())
.GetOrAdd(propertyName, pn =>
{
var pks = table.Primarys;
var returnTarget = Expression.Label(typeof(object));
var parm1 = Expression.Parameter(typeof(object));
var var1Parm = Expression.Variable(table.Type);
var var2Ret = Expression.Variable(typeof(object));
var exps = new List<Expression>(new Expression[] {
Expression.Assign(var1Parm, Expression.TypeAs(parm1, table.Type)),
Expression.Assign(
var2Ret,
Expression.Convert(Expression.MakeMemberAccess(var1Parm, table.Properties[pn]), typeof(object))
)
});
exps.AddRange(new Expression[] {
@ -223,7 +249,6 @@ namespace FreeSql.Extensions.EntityUtil
/// <param name="entityType"></param>
/// <param name="entity"></param>
/// <returns></returns>
//public static string GetEntityString<TEntity>(this IFreeSql orm, TEntity entity) => GetEntityString(orm, typeof(TEntity), entity);
public static string GetEntityString(this IFreeSql orm, Type entityType, object entity)
{
if (entity == null) return null;
@ -270,7 +295,6 @@ namespace FreeSql.Extensions.EntityUtil
/// 使用新实体的值,复盖旧实体的值
/// </summary>
static ConcurrentDictionary<DataType, ConcurrentDictionary<Type, Action<object, object>>> _dicMapEntityValue = new ConcurrentDictionary<DataType, ConcurrentDictionary<Type, Action<object, object>>>();
//public static void MapEntityValue<TEntity>(this IFreeSql orm, TEntity entityFrom, TEntity entityTo) => MapEntityValue(orm, typeof(TEntity), entityFrom, entityTo);
public static void MapEntityValue(this IFreeSql orm, Type entityType, object entityFrom, object entityTo)
{
if (entityType == null) entityType = entityFrom?.GetType() ?? entityTo?.GetType();
@ -316,7 +340,6 @@ namespace FreeSql.Extensions.EntityUtil
/// <summary>
/// 使用新实体的主键值,复盖旧实体的主键值
/// </summary>
//public static void MapEntityKeyValue<TEntity>(this IFreeSql orm, TEntity entityFrom, TEntity entityTo) => MapEntityKeyValue(orm, typeof(TEntity), entityFrom, entityTo);
public static void MapEntityKeyValue(this IFreeSql orm, Type entityType, object entityFrom, object entityTo)
{
if (entityType == null) entityType = entityFrom?.GetType() ?? entityTo?.GetType();
@ -354,7 +377,6 @@ namespace FreeSql.Extensions.EntityUtil
/// <param name="entityType"></param>
/// <param name="entity"></param>
/// <param name="idtval"></param>
//public static void SetEntityIdentityValueWithPrimary<TEntity>(this IFreeSql orm, TEntity entity, long idtval) => SetEntityIdentityValueWithPrimary(orm, typeof(TEntity), entity, idtval);
public static void SetEntityIdentityValueWithPrimary(this IFreeSql orm, Type entityType, object entity, long idtval)
{
if (entity == null) return;
@ -390,7 +412,6 @@ namespace FreeSql.Extensions.EntityUtil
/// <param name="orm"></param>
/// <param name="entityType"></param>
/// <param name="entity"></param>
//public static long GetEntityIdentityValueWithPrimary<TEntity>(this IFreeSql orm, TEntity entity) => GetEntityIdentityValueWithPrimary(orm, typeof(TEntity), entity);
public static long GetEntityIdentityValueWithPrimary(this IFreeSql orm, Type entityType, object entity)
{
if (entity == null) return 0;
@ -440,7 +461,6 @@ namespace FreeSql.Extensions.EntityUtil
/// <param name="orm"></param>
/// <param name="entityType"></param>
/// <param name="entity"></param>
//public static void ClearEntityPrimaryValueWithIdentityAndGuid<TEntity>(this IFreeSql orm, TEntity entity) => ClearEntityPrimaryValueWithIdentityAndGuid(orm, typeof(TEntity), entity);
public static void ClearEntityPrimaryValueWithIdentityAndGuid(this IFreeSql orm, Type entityType, object entity)
{
if (entity == null) return;
@ -489,7 +509,6 @@ namespace FreeSql.Extensions.EntityUtil
/// <param name="orm"></param>
/// <param name="entityType"></param>
/// <param name="entity"></param>
//public static void ClearEntityPrimaryValueWithIdentity<TEntity>(this IFreeSql orm, TEntity entity) => ClearEntityPrimaryValueWithIdentity(orm, typeof(TEntity), entity);
public static void ClearEntityPrimaryValueWithIdentity(this IFreeSql orm, Type entityType, object entity)
{
if (entity == null) return;
@ -530,7 +549,6 @@ namespace FreeSql.Extensions.EntityUtil
/// <param name="entity2"></param>
/// <param name="isEqual"></param>
/// <returns></returns>
//public static string[] CompareEntityValueReturnColumns<TEntity>(this IFreeSql orm, TEntity entity1, TEntity entity2, bool isEqual) => CompareEntityValueReturnColumns(orm, typeof(TEntity), entity1, entity2, isEqual);
public static string[] CompareEntityValueReturnColumns(this IFreeSql orm, Type entityType, object entity1, object entity2, bool isEqual)
{
if (entityType == null) entityType = entity1?.GetType() ?? entity2?.GetType();
@ -598,7 +616,6 @@ namespace FreeSql.Extensions.EntityUtil
/// <param name="entity"></param>
/// <param name="propertyName"></param>
/// <param name="incrBy"></param>
//public static void SetEntityIncrByWithPropertyName<TEntity>(this IFreeSql orm, TEntity entity, string propertyName, int incrBy) => SetEntityIncrByWithPropertyName(orm, typeof(TEntity), entity, propertyName, incrBy);
public static void SetEntityIncrByWithPropertyName(this IFreeSql orm, Type entityType, object entity, string propertyName, int incrBy)
{
if (entity == null) return;
@ -643,7 +660,6 @@ namespace FreeSql.Extensions.EntityUtil
/// <param name="entity"></param>
/// <param name="propertyName"></param>
/// <param name="value"></param>
//public static void SetEntityValueWithPropertyName<TEntity>(this IFreeSql orm, TEntity entity, string propertyName, object value) => SetEntityValueWithPropertyName(orm, typeof(TEntity), entity, propertyName, value);
public static void SetEntityValueWithPropertyName(this IFreeSql orm, Type entityType, object entity, string propertyName, object value)
{
if (entity == null) return;
@ -694,6 +710,53 @@ namespace FreeSql.Extensions.EntityUtil
});
func(entity, propertyName, value);
}
public static void SetPropertyValue(this TableInfo table, object entity, string propertyName, object value)
{
if (table == null || entity == null) return;
var func = _dicSetEntityValueWithPropertyName.GetOrAdd(DataType.MsAccess, dt => new ConcurrentDictionary<Type, ConcurrentDictionary<string, Action<object, string, object>>>())
.GetOrAdd(table.Type, et => new ConcurrentDictionary<string, Action<object, string, object>>())
.GetOrAdd(propertyName, pn =>
{
var parm1 = Expression.Parameter(typeof(object));
var parm2 = Expression.Parameter(typeof(string));
var parm3 = Expression.Parameter(typeof(object));
var var1Parm = Expression.Variable(table.Type);
var exps = new List<Expression>(new Expression[] {
Expression.Assign(var1Parm, Expression.TypeAs(parm1, table.Type))
});
if (table.Properties.ContainsKey(pn))
{
var prop = table.Properties[pn];
if (table.ColumnsByCs.ContainsKey(pn))
{
exps.Add(
Expression.Assign(
Expression.MakeMemberAccess(var1Parm, prop),
Expression.Convert(
FreeSql.Internal.Utils.GetDataReaderValueBlockExpression(prop.PropertyType, parm3),
prop.PropertyType
)
)
);
}
else
{
exps.Add(
Expression.Assign(
Expression.MakeMemberAccess(var1Parm, prop),
Expression.Convert(
parm3,
prop.PropertyType
)
)
);
}
}
return Expression.Lambda<Action<object, string, object>>(Expression.Block(new[] { var1Parm }, exps), new[] { parm1, parm2, parm3 }).Compile();
});
func(entity, propertyName, value);
}
static ConcurrentDictionary<Type, MethodInfo[]> _dicAppendEntityUpdateSetWithColumnMethods = new ConcurrentDictionary<Type, MethodInfo[]>();
static ConcurrentDictionary<Type, ConcurrentDictionary<Type, MethodInfo>> _dicAppendEntityUpdateSetWithColumnMethod = new ConcurrentDictionary<Type, ConcurrentDictionary<Type, MethodInfo>>();

View File

@ -3307,6 +3307,26 @@
总批次数量
</summary>
</member>
<member name="M:FreeSql.Internal.Model.ColumnInfo.GetDbValue(System.Object)">
<summary>
获取 obj.CsName 属性值 MapType 之后的数据库值
</summary>
<param name="obj"></param>
<returns></returns>
</member>
<member name="M:FreeSql.Internal.Model.ColumnInfo.GetValue(System.Object)">
<summary>
获取 obj.CsName 属性原始值(不经过 MapType
</summary>
<param name="obj"></param>
</member>
<member name="M:FreeSql.Internal.Model.ColumnInfo.SetValue(System.Object,System.Object)">
<summary>
设置 obj.CsName 属性值
</summary>
<param name="obj"></param>
<param name="val"></param>
</member>
<member name="T:FreeSql.Internal.Model.DynamicFilterInfo">
<summary>
动态过滤条件

View File

@ -78,12 +78,12 @@ namespace FreeSql.Internal.CommonProvider
if (d == null) continue;
foreach (var col in table.Columns.Values)
{
object val = col.GetMapValue(d);
object val = col.GetValue(d);
var auditArgs = new Aop.AuditValueEventArgs(Aop.AuditValueType.InsertOrUpdate, col, table.Properties[col.CsName], val);
orm.Aop.AuditValueHandler(sender, auditArgs);
if (auditArgs.IsChanged)
{
col.SetMapValue(d, val = auditArgs.Value);
col.SetValue(d, val = auditArgs.Value);
if (changedDict != null && changedDict.ContainsKey(col.Attribute.Name) == false)
changedDict.Add(col.Attribute.Name, true);
}
@ -98,12 +98,12 @@ namespace FreeSql.Internal.CommonProvider
throw new Exception($"操作的数据类型({data.GetType().DisplayCsharp()}) 与 AsType({table.Type.DisplayCsharp()}) 不一致,请检查。");
foreach (var col in table.Columns.Values)
{
object val = col.GetMapValue(data);
object val = col.GetValue(data);
var auditArgs = new Aop.AuditValueEventArgs(Aop.AuditValueType.InsertOrUpdate, col, table.Properties[col.CsName], val);
orm.Aop.AuditValueHandler(sender, auditArgs);
if (auditArgs.IsChanged)
{
col.SetMapValue(data, val = auditArgs.Value);
col.SetValue(data, val = auditArgs.Value);
if (changedDict != null && changedDict.ContainsKey(col.Attribute.Name) == false)
changedDict.Add(col.Attribute.Name, true);
}
@ -166,7 +166,7 @@ namespace FreeSql.Internal.CommonProvider
sb.Append(col.DbInsertValue);
else
{
object val = col.GetMapValue(d);
object val = col.GetDbValue(d);
sb.Append(_commonUtils.GetNoneParamaterSqlValue(dbParams, "cu", col.Attribute.MapType, val));
}
if (didx == 0) sb.Append(" as ").Append(col.Attribute.Name);

View File

@ -147,30 +147,28 @@ namespace FreeSql.Internal.CommonProvider
throw new Exception($"操作的数据类型({data.GetType().DisplayCsharp()}) 与 AsType({table.Type.DisplayCsharp()}) 不一致,请检查。");
foreach (var col in table.Columns.Values)
{
object val = col.GetMapValue(data);
object val = col.GetValue(data);
if (orm.Aop.AuditValueHandler != null)
{
var auditArgs = new Aop.AuditValueEventArgs(Aop.AuditValueType.Insert, col, table.Properties[col.CsName], val);
orm.Aop.AuditValueHandler(sender, auditArgs);
if (auditArgs.IsChanged)
{
col.SetMapValue(data, val = auditArgs.Value);
col.SetValue(data, val = auditArgs.Value);
if (changedDict != null && changedDict.ContainsKey(col.Attribute.Name) == false)
changedDict.Add(col.Attribute.Name, true);
}
}
if (col.Attribute.IsPrimary)
{
val = col.GetDbValue(data);
if (col.Attribute.MapType.NullableTypeOrThis() == typeof(Guid) && (val == null || (Guid)val == Guid.Empty))
col.SetMapValue(data, val = FreeUtil.NewMongodbId());
col.SetValue(data, val = FreeUtil.NewMongodbId());
else if (col.CsType.NullableTypeOrThis() == typeof(Guid))
{
var guidVal = orm.GetEntityValueWithPropertyName(table.Type, data, col.CsName);
if (guidVal == null || (Guid)guidVal == Guid.Empty)
{
orm.SetEntityValueWithPropertyName(table.Type, data, col.CsName, FreeUtil.NewMongodbId());
val = col.GetMapValue(data);
}
val = col.GetValue(data);
if (val == null || (Guid)val == Guid.Empty)
col.SetValue(data, val = FreeUtil.NewMongodbId());
}
}
}
@ -548,7 +546,7 @@ namespace FreeSql.Internal.CommonProvider
sb.Append(col.DbInsertValue);
else
{
object val = col.GetMapValue(d);
object val = col.GetDbValue(d);
if (val == null && col.Attribute.IsNullable == false) val = col.CsType == typeof(string) ? "" : Utils.GetDataReaderValue(col.CsType.NullableTypeOrThis(), null);//#384
if (_noneParameter)
sb.Append(_commonUtils.GetNoneParamaterSqlValue(specialParams, _noneParameterFlag, col.Attribute.MapType, val));
@ -588,7 +586,7 @@ namespace FreeSql.Internal.CommonProvider
var rowIndex = 0;
foreach (var col in dtCols)
{
var val = col.Item1.GetMapValue(d);
var val = col.Item1.GetDbValue(d);
if (col.Item3 == true)
{
//if (val == null) throw new Exception($"[{didx}].{col.Item1.CsName} 值不可为 nullDataTable 限制不可使用 int?/long? 可空类型IInsert.ToDataTable 将映射成 int/long因此不可接受 null 值");

View File

@ -350,12 +350,12 @@ namespace FreeSql.Internal.CommonProvider
if (d == null) continue;
foreach (var col in table.Columns.Values)
{
object val = col.GetMapValue(d);
object val = col.GetValue(d);
var auditArgs = new Aop.AuditValueEventArgs(Aop.AuditValueType.Update, col, table.Properties[col.CsName], val);
orm.Aop.AuditValueHandler(sender, auditArgs);
if (auditArgs.IsChanged)
{
col.SetMapValue(d, val = auditArgs.Value);
col.SetValue(d, val = auditArgs.Value);
if (changedDict != null && changedDict.ContainsKey(col.Attribute.Name) == false)
changedDict.Add(col.Attribute.Name, true);
}
@ -370,12 +370,12 @@ namespace FreeSql.Internal.CommonProvider
throw new Exception($"操作的数据类型({data.GetType().DisplayCsharp()}) 与 AsType({table.Type.DisplayCsharp()}) 不一致,请检查。");
foreach (var col in table.Columns.Values)
{
object val = col.GetMapValue(data);
object val = col.GetValue(data);
var auditArgs = new Aop.AuditValueEventArgs(Aop.AuditValueType.Update, col, table.Properties[col.CsName], val);
orm.Aop.AuditValueHandler(sender, auditArgs);
if (auditArgs.IsChanged)
{
col.SetMapValue(data, val = auditArgs.Value);
col.SetValue(data, val = auditArgs.Value);
if (changedDict != null && changedDict.ContainsKey(col.Attribute.Name) == false)
changedDict.Add(col.Attribute.Name, true);
}
@ -403,18 +403,18 @@ namespace FreeSql.Internal.CommonProvider
protected void SetPriv(ColumnInfo col, object value)
{
object paramVal = null;
if (col.Attribute.MapType == col.CsType) paramVal = value;
else paramVal = Utils.GetDataReaderValue(col.Attribute.MapType, value);
object val = null;
if (col.Attribute.MapType == col.CsType) val = value;
else val = Utils.GetDataReaderValue(col.Attribute.MapType, value);
_set.Append(", ").Append(_commonUtils.QuoteSqlName(col.Attribute.Name)).Append(" = ");
if (_noneParameter)
{
_set.Append(_commonUtils.GetNoneParamaterSqlValue(_params, "u", col.Attribute.MapType, paramVal));
_set.Append(_commonUtils.GetNoneParamaterSqlValue(_params, "u", col.Attribute.MapType, val));
}
else
{
_set.Append(_commonUtils.QuoteWriteParamter(col.Attribute.MapType, $"{_commonUtils.QuoteParamterName("p_")}{_params.Count}"));
_commonUtils.AppendParamter(_params, null, col, col.Attribute.MapType, paramVal);
_commonUtils.AppendParamter(_params, null, col, col.Attribute.MapType, val);
}
}
public IUpdate<T1> Set<TMember>(Expression<Func<T1, TMember>> column, TMember value)
@ -565,7 +565,7 @@ namespace FreeSql.Internal.CommonProvider
var sb = new StringBuilder();
sb.Append(_commonUtils.QuoteSqlName(col.Attribute.Name)).Append(" = ");
sb.Append(thenValue(_commonUtils.GetNoneParamaterSqlValue(_paramsSource, "u", col.Attribute.MapType, col.GetMapValue(_source.First()))));
sb.Append(thenValue(_commonUtils.GetNoneParamaterSqlValue(_paramsSource, "u", col.Attribute.MapType, col.GetDbValue(_source.First()))));
return sb.ToString();
@ -588,7 +588,7 @@ namespace FreeSql.Internal.CommonProvider
cwsb.Append(" \r\nWHEN ");
ToSqlWhen(cwsb, _table.Primarys, d);
cwsb.Append(" THEN ");
var val = col.GetMapValue(d);
var val = col.GetDbValue(d);
cwsb.Append(thenValue(_commonUtils.GetNoneParamaterSqlValue(_paramsSource, "u", col.Attribute.MapType, val)));
if (val == null || val == DBNull.Value) nulls++;
}
@ -660,7 +660,7 @@ namespace FreeSql.Internal.CommonProvider
sb.Append(col.DbUpdateValue);
else
{
var val = col.GetMapValue(_source.First());
var val = col.GetDbValue(_source.First());
if (_noneParameter)
sb.Append(_commonUtils.GetNoneParamaterSqlValue(_paramsSource, "u", col.Attribute.MapType, val));
else
@ -705,7 +705,7 @@ namespace FreeSql.Internal.CommonProvider
cwsb.Append(" \r\nWHEN ");
ToSqlWhen(cwsb, _table.Primarys, d);
cwsb.Append(" THEN ");
var val = col.GetMapValue(d);
var val = col.GetDbValue(d);
if (_noneParameter)
cwsb.Append(_commonUtils.GetNoneParamaterSqlValue(_paramsSource, "u", col.Attribute.MapType, val));
else

View File

@ -277,7 +277,7 @@ namespace FreeSql.Internal
{
if (pkidx > 0) sb.Append(" AND ");
sb.Append(aliasAndDot).Append(this.QuoteSqlName(pk.Attribute.Name));
sb.Append(this.FormatSql(" = {0}", pk.GetMapValue(dywhere)));
sb.Append(this.FormatSql(" = {0}", pk.GetDbValue(dywhere)));
++pkidx;
}
return sb.ToString();
@ -301,7 +301,7 @@ namespace FreeSql.Internal
var itype = i.GetType();
isEntityType = (itype == table.Type || itype.BaseType == table.Type);
}
if (isEntityType) sb.Append(this.FormatSql("{0}", primarys[0].GetMapValue(i)));
if (isEntityType) sb.Append(this.FormatSql("{0}", primarys[0].GetDbValue(i)));
else sb.Append(this.FormatSql("{0}", Utils.GetDataReaderValue(pk1.Attribute.MapType, i)));
++ieidx;
}
@ -352,7 +352,7 @@ namespace FreeSql.Internal
{
var sbin = new StringBuilder();
sbin.Append(aliasAndDot).Append(this.QuoteSqlName(pk1.Attribute.Name));
var indt = its.Select(a => pk1.GetMapValue(a)).Where(a => a != null).ToArray();
var indt = its.Select(a => pk1.GetDbValue(a)).Where(a => a != null).ToArray();
if (indt.Any() == false) return null;
if (indt.Length == 1) sbin.Append(" = ").Append(this.FormatSql("{0}", indt.First()));
else sbin.Append(" IN (").Append(string.Join(",", indt.Select(a => this.FormatSql("{0}", a)))).Append(")");
@ -365,7 +365,7 @@ namespace FreeSql.Internal
{
var filter = "";
foreach (var pk in table.Primarys)
filter += $" AND {aliasAndDot}{this.QuoteSqlName(pk.Attribute.Name)} = {this.FormatSql("{0}", pk.GetMapValue(item))}";
filter += $" AND {aliasAndDot}{this.QuoteSqlName(pk.Attribute.Name)} = {this.FormatSql("{0}", pk.GetDbValue(item))}";
if (string.IsNullOrEmpty(filter)) continue;
if (sb != null)
{

View File

@ -1,4 +1,5 @@
using FreeSql.DataAnnotations;
using FreeSql.Extensions.EntityUtil;
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
@ -20,8 +21,37 @@ namespace FreeSql.Internal.Model
public int DbSize { get; internal set; }
public byte DbPrecision { get; internal set; }
public byte DbScale { get; internal set; }
//public Func<object, object> ConversionCsToDb { get; internal set; }
//public Func<object, object> ConversionDbToCs { get; internal set; }
/// <summary>
/// 获取 obj.CsName 属性值 MapType 之后的数据库值
/// </summary>
/// <param name="obj"></param>
/// <returns></returns>
public object GetDbValue(object obj)
{
var dbval = Table.GetPropertyValue(obj, CsName);
//if (ConversionCsToDb != null) dbval = ConversionCsToDb(dbval);
if (Attribute.MapType != CsType) dbval = Utils.GetDataReaderValue(Attribute.MapType, dbval);
return dbval;
}
/// <summary>
/// 获取 obj.CsName 属性原始值(不经过 MapType
/// </summary>
/// <param name="obj"></param>
public object GetValue(object obj) => Table.GetPropertyValue(obj, CsName);
/// <summary>
/// 设置 obj.CsName 属性值
/// </summary>
/// <param name="obj"></param>
/// <param name="val"></param>
public void SetValue(object obj, object val) => Table.SetPropertyValue(obj, CsName, Utils.GetDataReaderValue(CsType, val));
static ConcurrentDictionary<ColumnInfo, Func<object, object>> _dicGetMapValue = new ConcurrentDictionary<ColumnInfo, Func<object, object>>();
[Obsolete("请使用 GetDbValue 或者 GetValue")]
public object GetMapValue(object obj)
{
var func = _dicGetMapValue.GetOrAdd(this, col =>
@ -57,6 +87,7 @@ namespace FreeSql.Internal.Model
return func(obj);
}
static ConcurrentDictionary<ColumnInfo, Action<object, object>> _dicSetMapValue = new ConcurrentDictionary<ColumnInfo, Action<object, object>>();
[Obsolete("请使用 SetValue")]
public void SetMapValue(object obj, object val)
{
var func = _dicSetMapValue.GetOrAdd(this, col =>