mirror of
https://github.com/nsnail/FreeSql.git
synced 2025-06-19 20:38:16 +08:00
- 增加 IInsertOrUpdate.UpdateSet 指定更新;
This commit is contained in:
@ -1921,6 +1921,16 @@
|
||||
<param name="columns">属性名,或者字段名</param>
|
||||
<returns></returns>
|
||||
</member>
|
||||
<member name="M:FreeSql.IInsertOrUpdate`1.UpdateSet``1(System.Linq.Expressions.Expression{System.Func{`0,`0,``0}})">
|
||||
<summary>
|
||||
设置列的联表值,格式:<para></para>
|
||||
UpdateSet((a, b) => a.Clicks == b.xxx)<para></para>
|
||||
UpdateSet((a, b) => a.Clicks == a.Clicks + 1)
|
||||
</summary>
|
||||
<typeparam name="TMember"></typeparam>
|
||||
<param name="exp"></param>
|
||||
<returns></returns>
|
||||
</member>
|
||||
<member name="M:FreeSql.IInsertOrUpdate`1.AsTable(System.Func{System.String,System.String})">
|
||||
<summary>
|
||||
设置表名规则,可用于分库/分表,参数1:默认表名;返回值:新表名;
|
||||
|
@ -86,6 +86,16 @@ namespace FreeSql
|
||||
/// <returns></returns>
|
||||
IInsertOrUpdate<T1> UpdateColumns(string[] columns);
|
||||
|
||||
/// <summary>
|
||||
/// 设置列的联表值,格式:<para></para>
|
||||
/// UpdateSet((a, b) => a.Clicks == b.xxx)<para></para>
|
||||
/// UpdateSet((a, b) => a.Clicks == a.Clicks + 1)
|
||||
/// </summary>
|
||||
/// <typeparam name="TMember"></typeparam>
|
||||
/// <param name="exp"></param>
|
||||
/// <returns></returns>
|
||||
IInsertOrUpdate<T1> UpdateSet<TMember>(Expression<Func<T1, T1, TMember>> exp);
|
||||
|
||||
/// <summary>
|
||||
/// 设置表名规则,可用于分库/分表,参数1:默认表名;返回值:新表名;
|
||||
/// </summary>
|
||||
|
@ -23,6 +23,7 @@ namespace FreeSql.Internal.CommonProvider
|
||||
public bool _doNothing = false;
|
||||
public Dictionary<string, bool> _updateIgnore = new Dictionary<string, bool>(StringComparer.CurrentCultureIgnoreCase);
|
||||
public Dictionary<string, bool> _auditValueChangedDict = new Dictionary<string, bool>(StringComparer.CurrentCultureIgnoreCase);
|
||||
public Dictionary<string, string> _updateSetDict = new Dictionary<string, string>();
|
||||
public TableInfo _table;
|
||||
public ColumnInfo[] _tempPrimarys;
|
||||
public Func<string, string> _tableRule;
|
||||
@ -87,6 +88,55 @@ namespace FreeSql.Internal.CommonProvider
|
||||
return this;
|
||||
}
|
||||
|
||||
public IInsertOrUpdate<T1> UpdateSet<TMember>(Expression<Func<T1, T1, TMember>> exp)
|
||||
{
|
||||
var body = exp?.Body;
|
||||
var nodeType = body?.NodeType;
|
||||
if (nodeType == ExpressionType.Convert)
|
||||
{
|
||||
body = (body as UnaryExpression)?.Operand;
|
||||
nodeType = body?.NodeType;
|
||||
}
|
||||
switch (nodeType)
|
||||
{
|
||||
case ExpressionType.Equal:
|
||||
break;
|
||||
default:
|
||||
throw new Exception("格式错了,请使用 .Set((a,b) => a.name == b.xname)");
|
||||
}
|
||||
|
||||
var equalBinaryExp = body as BinaryExpression;
|
||||
var cols = new List<SelectColumnInfo>();
|
||||
_commonExpression.ExpressionSelectColumn_MemberAccess(null, null, cols, SelectTableInfoType.From, equalBinaryExp.Left, true, null);
|
||||
if (cols.Count != 1) return this;
|
||||
var col = cols[0].Column;
|
||||
var valueSql = "";
|
||||
|
||||
if (equalBinaryExp.Right.IsParameter())
|
||||
{
|
||||
var tmpQuery = _orm.Select<T1, T1>();
|
||||
var tmpQueryProvider = tmpQuery as Select0Provider;
|
||||
tmpQueryProvider._tables[0].Alias = "t1";
|
||||
tmpQueryProvider._tables[0].Parameter = exp.Parameters[0];
|
||||
tmpQueryProvider._tables[1].Alias = "t2";
|
||||
tmpQueryProvider._tables[1].Parameter = exp.Parameters[1];
|
||||
var valueExp = Expression.Lambda<Func<T1, T1, object>>(Expression.Convert(equalBinaryExp.Right, typeof(object)), exp.Parameters);
|
||||
tmpQuery.GroupBy(valueExp);
|
||||
valueSql = tmpQueryProvider._groupby?.Remove(0, " \r\nGROUP BY ".Length);
|
||||
}
|
||||
else
|
||||
{
|
||||
valueSql = _commonExpression.ExpressionLambdaToSql(equalBinaryExp.Right, new CommonExpression.ExpTSC
|
||||
{
|
||||
isQuoteName = true,
|
||||
mapType = equalBinaryExp.Right is BinaryExpression ? null : col.Attribute.MapType
|
||||
});
|
||||
}
|
||||
if (string.IsNullOrEmpty(valueSql)) return this;
|
||||
_updateSetDict[col.Attribute.Name] = valueSql;
|
||||
return this;
|
||||
}
|
||||
|
||||
public static void AuditDataValue(object sender, IEnumerable<T1> data, IFreeSql orm, TableInfo table, Dictionary<string, bool> changedDict)
|
||||
{
|
||||
if (data?.Any() != true) return;
|
||||
|
@ -894,7 +894,9 @@ namespace FreeSql.Internal.CommonProvider
|
||||
var sb = new StringBuilder();
|
||||
sb.Append(_commonUtils.QuoteSqlName(col.Attribute.Name)).Append(" = ");
|
||||
|
||||
var valsameIf = col.Attribute.MapType.IsNumberType() || col.Attribute.MapType == typeof(string) || col.Attribute.MapType.NullableTypeOrThis().IsEnum;
|
||||
var valsameIf = col.Attribute.MapType.IsNumberType() ||
|
||||
new[] { typeof(string), typeof(DateTime), typeof(DateTime?) }.Contains(col.Attribute.MapType) ||
|
||||
col.Attribute.MapType.NullableTypeOrThis().IsEnum;
|
||||
var ds = _source.Select(a => col.GetDbValue(a)).ToArray();
|
||||
if (valsameIf && ds.All(a => object.Equals(a, ds[0])))
|
||||
{
|
||||
@ -1149,7 +1151,9 @@ namespace FreeSql.Internal.CommonProvider
|
||||
sb.Append(col.DbUpdateValue);
|
||||
else
|
||||
{
|
||||
var valsameIf = col.Attribute.MapType.IsNumberType() || col.Attribute.MapType == typeof(string) || col.Attribute.MapType.NullableTypeOrThis().IsEnum;
|
||||
var valsameIf = col.Attribute.MapType.IsNumberType() ||
|
||||
new[] { typeof(string), typeof(DateTime), typeof(DateTime?) }.Contains(col.Attribute.MapType) ||
|
||||
col.Attribute.MapType.NullableTypeOrThis().IsEnum;
|
||||
var ds = _source.Select(a => col.GetDbValue(a)).ToArray();
|
||||
if (valsameIf && ds.All(a => object.Equals(a, ds[0])))
|
||||
{
|
||||
|
Reference in New Issue
Block a user