Update Expression analysis

This commit is contained in:
sunkaixuan
2017-07-11 23:56:06 +08:00
parent 8c1a34b708
commit 6531d80459
3 changed files with 42 additions and 2 deletions

View File

@@ -124,6 +124,25 @@ namespace OrmTest.UnitTest
t10.Value,
"Update 10 error"
);
var t11 = db.Updateable<DataTestInfo>().UpdateColumns(it => new DataTestInfo() { Datetime1=DateTime.MaxValue }).Where(it => it.Int1 == 11).ToSql();
base.Check(@"UPDATE [DataTestInfo] SET
[Datetime1] = @constant0 WHERE ( [Int1] = @Int11 )", new List<SugarParameter>() {
new SugarParameter("@Int11",11),
new SugarParameter("@constant0",DateTime.MaxValue) },
t11.Key,
t11.Value,
"Update 11 error"
);
var t12 = db.Updateable<DataTestInfo>().UpdateColumns(it => new DataTestInfo() { Int2 = it.Int2+1 }).Where(it => it.Int1 == 11).ToSql();
base.Check(@"UPDATE [DataTestInfo] SET
[Int2] = ( [Int2] + @Const0 ) WHERE ( [Int1] = @Int11 )", new List<SugarParameter>() {
new SugarParameter("@Int11",11),
new SugarParameter("@Const0",1) },
t12.Key,
t12.Value,
"Update 12 error"
);
}
public SqlSugarClient GetInstance()

View File

@@ -235,6 +235,20 @@ namespace SqlSugar
};
return methodCallExpressionArgs;
}
protected string GetNewExpressionValue(Expression item)
{
var newContext = this.Context.GetCopyContext();
newContext.Resolve(item, this.Context.IsJoin ? ResolveExpressType.WhereMultiple : ResolveExpressType.WhereSingle);
this.Context.Index = newContext.Index;
this.Context.ParameterIndex = newContext.ParameterIndex;
if (newContext.Parameters.IsValuable())
{
this.Context.Parameters.AddRange(newContext.Parameters);
}
return newContext.Result.GetResultString();
}
protected void ResolveNewExpressions(ExpressionParameter parameter, Expression item, string asName)
{
if (item.NodeType == ExpressionType.Constant)

View File

@@ -49,7 +49,13 @@ namespace SqlSugar
MemberAssignment memberAssignment = (MemberAssignment)binding;
var memberName = memberAssignment.Member.Name;
var item = memberAssignment.Expression;
if (item is UnaryExpression||item.NodeType == ExpressionType.Constant || (item is MemberExpression) && ((MemberExpression)item).Expression.NodeType == ExpressionType.Constant)
if ((item is MemberExpression) && ((MemberExpression)item).Expression == null)
{
var paramterValue = ExpressionTool.DynamicInvoke(item);
string parameterName = AppendParameter(paramterValue);
this.Context.Result.Append(base.Context.GetEqString(memberName, parameterName));
}
else if (item is UnaryExpression || item.NodeType == ExpressionType.Constant || (item is MemberExpression) && ((MemberExpression)item).Expression.NodeType == ExpressionType.Constant)
{
base.Expression = item;
base.Start();
@@ -80,7 +86,8 @@ namespace SqlSugar
}
else if (item is BinaryExpression)
{
Check.ThrowNotSupportedException(item.GetType().Name);
var result=GetNewExpressionValue(item);
this.Context.Result.Append(base.Context.GetEqString(memberName, result));
}
}
}