内部 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

@@ -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 =>