mirror of
https://gitee.com/dotnetchina/SqlSugar.git
synced 2025-06-28 13:34:32 +08:00
-
This commit is contained in:
parent
3805d5291f
commit
a3cdbad4fd
Binary file not shown.
@ -36,20 +36,19 @@ namespace OrmTest.ExpressionTest
|
||||
expContext.Resolve();
|
||||
var value = expContext.Result.GetString();
|
||||
var pars = expContext.Parameters;
|
||||
base.Check(value, pars, " ( Id > @Id1 ) ", new List<SugarParameter>() {
|
||||
new SugarParameter("@Id1",1)
|
||||
base.Check(value, pars, "( Id > @Id0 )", new List<SugarParameter>() {
|
||||
new SugarParameter("@Id0",1)
|
||||
}, "whereSingle1");
|
||||
}
|
||||
private void whereSingle2()
|
||||
{
|
||||
Expression<Func<Student, bool>> exp = it => it.Id > 1 || it.Name == "a";
|
||||
Expression<Func<Student, bool>> exp = it => 1 > it.Id;
|
||||
ExpressionContext expContext = new ExpressionContext(exp, ResolveExpressType.WhereSingle);
|
||||
expContext.Resolve();
|
||||
var value = expContext.Result.GetString();
|
||||
var pars = expContext.Parameters;
|
||||
base.Check(value, pars, " ( Id > @Id1 ) OR ( Name = @Name2 ) ", new List<SugarParameter>() {
|
||||
new SugarParameter("@Id1",1),
|
||||
new SugarParameter("@Name2","a")
|
||||
base.Check(value, pars, "( @Id0 > Id )", new List<SugarParameter>() {
|
||||
new SugarParameter("@Id0",1)
|
||||
}, "whereSingle2");
|
||||
}
|
||||
private void whereSingle3()
|
||||
@ -59,21 +58,22 @@ namespace OrmTest.ExpressionTest
|
||||
expContext.Resolve();
|
||||
var value = expContext.Result.GetString();
|
||||
var pars = expContext.Parameters;
|
||||
base.Check(value, pars, " ( Id > @Id1 ) OR ( Name = @Name2 ) ", new List<SugarParameter>() {
|
||||
new SugarParameter("@Id1",1),
|
||||
new SugarParameter("@Name2","a")
|
||||
}, "whereSingle2");
|
||||
base.Check(value, pars, " (( Id > @Id0 ) OR ( Name = @Name1 ))", new List<SugarParameter>() {
|
||||
new SugarParameter("@Id0",1),
|
||||
new SugarParameter("@Name1","a")
|
||||
}, "whereSingle3");
|
||||
}
|
||||
private void whereSingle4()
|
||||
{
|
||||
Expression<Func<Student, bool>> exp = it =>( it.Id > 1 &&it.Name!="a")|| it.Name == "a";
|
||||
Expression<Func<Student, bool>> exp = it => (it.Id > 1 && it.Name != "a") || it.Name == "a1";
|
||||
ExpressionContext expContext = new ExpressionContext(exp, ResolveExpressType.WhereSingle);
|
||||
expContext.Resolve();
|
||||
var value = expContext.Result.GetString();
|
||||
var pars = expContext.Parameters;
|
||||
base.Check(value, pars, " ( Id > @Id1 ) OR ( Name = @Name2 ) ", new List<SugarParameter>() {
|
||||
new SugarParameter("@Id1",1),
|
||||
new SugarParameter("@Name2","a")
|
||||
base.Check(value, pars, " ((( Id > @Id0 ) AND ( Name <> @Name1 )) OR ( Name = @Name2 )) ", new List<SugarParameter>() {
|
||||
new SugarParameter("@Id0",1),
|
||||
new SugarParameter("@Name1","a"),
|
||||
new SugarParameter("@Name2","a1")
|
||||
}, "whereSingle4");
|
||||
}
|
||||
}
|
||||
|
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
@ -10,8 +10,10 @@ namespace SqlSugar
|
||||
public const string BinaryExpressionInfoListKey = "BinaryExpressionInfoListKey";
|
||||
public const string BinaryFormatString = " ( {0} {1} {2} ) ";
|
||||
public const string Format0 = "{0}";
|
||||
public const string Format1 = "{1}";
|
||||
public const string Format2 = "{2}";
|
||||
public const string Format1 = "$__$";
|
||||
public const string Format2 = "o__o";
|
||||
public const string Format3 = "(";
|
||||
public const string Format4 = ")";
|
||||
public readonly static Type MemberExpressionType = typeof(MemberExpression);
|
||||
public readonly static Type ConstantExpressionType = typeof(ConstantExpression);
|
||||
public readonly static Type StringType = typeof(string);
|
||||
|
@ -11,11 +11,12 @@ namespace SqlSugar
|
||||
public ExpressionContext Context { get; set; }
|
||||
public ExpressionParameter BaseParameter { get; set; }
|
||||
public Expression BaseExpression { get; set; }
|
||||
public Expression LeftExpression { get; set; }
|
||||
public Expression RightExpression { get; set; }
|
||||
public Expression Expression { get; set; }
|
||||
public bool? IsLeft { get; set; }
|
||||
public int Index { get; set; }
|
||||
public object CommonTempData { get; set; }
|
||||
public List<KeyValuePair<string, BinaryExpressionInfo>> BinaryTempData { get; set; }
|
||||
public ExpressionResultAppendType AppendType { get; set; }
|
||||
public void IsAppendResult()
|
||||
{
|
||||
|
@ -82,6 +82,12 @@ namespace SqlSugar
|
||||
return (this.Result.ToString().Contains(value));
|
||||
}
|
||||
|
||||
internal void Insert(int index, string value)
|
||||
{
|
||||
if (this.Result == null) this.Result.Append(value);
|
||||
this.Result.Insert(index, value);
|
||||
}
|
||||
|
||||
public void Append(object parameter)
|
||||
{
|
||||
if (this.CurrentParameter.IsValuable() && this.CurrentParameter.AppendType.IsIn(ExpressionResultAppendType.AppendTempDate))
|
||||
|
@ -29,6 +29,7 @@ namespace SqlSugar
|
||||
#region properties
|
||||
public IDbMethods DbMehtods { get; set; }
|
||||
public int Index { get; set; }
|
||||
public int ParameterIndex { get; set; }
|
||||
internal ResolveExpressType ResolveType { get; set; }
|
||||
public Expression Expression { get; set; }
|
||||
public ExpressionResult Result
|
||||
|
@ -28,7 +28,7 @@ namespace SqlSugar
|
||||
|
||||
public BaseResolve Start()
|
||||
{
|
||||
this.Index++;
|
||||
Context.Index++;
|
||||
Expression exp = this.Expression;
|
||||
ExpressionParameter parameter = new ExpressionParameter()
|
||||
{
|
||||
@ -37,7 +37,7 @@ namespace SqlSugar
|
||||
IsLeft = this.IsLeft,
|
||||
BaseExpression = this.BaseExpression,
|
||||
BaseParameter = this.BaseParameter,
|
||||
Index = this.Index
|
||||
Index = Context.Index
|
||||
};
|
||||
if (exp is LambdaExpression)
|
||||
{
|
||||
|
@ -17,7 +17,6 @@ namespace SqlSugar
|
||||
}
|
||||
else
|
||||
{
|
||||
parameter.BinaryTempData = new List<KeyValuePair<string, BinaryExpressionInfo>>();
|
||||
var expression = this.Expression as BinaryExpression;
|
||||
var operatorValue = ExpressionTool.GetOperator(expression.NodeType);
|
||||
var isComparisonOperator =
|
||||
@ -30,94 +29,34 @@ namespace SqlSugar
|
||||
var rightExpression = expression.Right;
|
||||
var leftIsBinary = leftExpression is BinaryExpression;
|
||||
var rightBinary = rightExpression is BinaryExpression;
|
||||
int i = 0;
|
||||
var lbrs = leftIsBinary && !rightBinary;
|
||||
var lsrb = !leftIsBinary && rightBinary;
|
||||
var lbrb = rightBinary && leftIsBinary;
|
||||
var lsbs = !leftIsBinary && !rightBinary;
|
||||
if (lbrs)
|
||||
if (!base.Context.Result.Contains(ExpressionConst.Format0))
|
||||
{
|
||||
base.Context.Result.Append("{" + i + "}");
|
||||
++i;
|
||||
base.Context.Result.Append(ExpressionConst.Format3);
|
||||
base.Context.Result.Append(ExpressionConst.Format0);
|
||||
}
|
||||
else if (lsrb)
|
||||
{
|
||||
base.Context.Result.Append("{" + i + "}");
|
||||
}
|
||||
else if (lbrb)
|
||||
{
|
||||
base.Context.Result.Append("{0}");
|
||||
base.Context.Result.Append("{2}");
|
||||
base.Context.Result.Append("{1}");
|
||||
else {
|
||||
base.Context.Result.Replace(ExpressionConst.Format0,ExpressionConst.Format3+ ExpressionConst.Format0);
|
||||
}
|
||||
parameter.LeftExpression = leftExpression;
|
||||
parameter.RightExpression = rightExpression;
|
||||
base.Expression = leftExpression;
|
||||
base.IsLeft = true;
|
||||
base.Start();
|
||||
base.Context.Result.Replace(ExpressionConst.Format1+parameter.Index,operatorValue);
|
||||
base.IsLeft = false;
|
||||
base.Expression = rightExpression;
|
||||
base.Start();
|
||||
base.IsLeft = null;
|
||||
string leftString = null;
|
||||
if (!leftIsBinary)
|
||||
leftString = GetLeftString(parameter);
|
||||
string rightString = null;
|
||||
if (!rightBinary)
|
||||
rightString = GetRightString(parameter);
|
||||
string binarySql = null;
|
||||
if (lsbs)
|
||||
base.Context.Result.Append(ExpressionConst.Format4);
|
||||
if (parameter.BaseExpression is BinaryExpression && parameter.IsLeft == true)
|
||||
{
|
||||
binarySql = string.Format(ExpressionConst.BinaryFormatString, leftString, operatorValue, rightString);
|
||||
}
|
||||
else if (lbrb)
|
||||
{
|
||||
binarySql = operatorValue;
|
||||
}
|
||||
if (Context.Result.Contains(ExpressionConst.Format0))
|
||||
{
|
||||
base.Context.Result.Replace(ExpressionConst.Format0, binarySql);
|
||||
}
|
||||
else
|
||||
{
|
||||
base.Context.Result.Append(binarySql);
|
||||
}
|
||||
if (Context.Result.Contains(ExpressionConst.Format1))
|
||||
{
|
||||
base.Context.Result.Replace(ExpressionConst.Format1, ExpressionConst.Format0);
|
||||
base.Context.Result.Replace(ExpressionConst.Format2, ExpressionConst.Format1);
|
||||
base.Context.Result.Append(" "+ExpressionConst.Format1 + parameter.BaseParameter.Index+" ");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private string GetRightString(ExpressionParameter parameter)
|
||||
{
|
||||
var leftInfo = parameter.BinaryTempData.Single(it => it.Value.IsLeft).Value;
|
||||
var rightInfo = parameter.BinaryTempData.Single(it => !it.Value.IsLeft).Value;
|
||||
if (rightInfo.ExpressionType == ExpressionConst.ConstantExpressionType)
|
||||
{
|
||||
var sqlParameterKeyWord = parameter.Context.SqlParameterKeyWord;
|
||||
var reval = string.Format("{0}{1}{2}", sqlParameterKeyWord, leftInfo.Value, parameter.Context.Index + parameter.Index);
|
||||
if (parameter.Context.Parameters == null)
|
||||
{
|
||||
parameter.Context.Parameters = new List<SugarParameter>();
|
||||
}
|
||||
parameter.Context.Parameters.Add(new SugarParameter(reval, rightInfo.Value));
|
||||
return reval;
|
||||
}
|
||||
return rightInfo.Value.ObjToString();
|
||||
}
|
||||
|
||||
private string GetLeftString(ExpressionParameter parameter)
|
||||
{
|
||||
var leftInfo = parameter.BinaryTempData.Single(it => it.Value.IsLeft).Value;
|
||||
var rightInfo = parameter.BinaryTempData.Single(it => !it.Value.IsLeft).Value;
|
||||
if (leftInfo.ExpressionType == ExpressionConst.ConstantExpressionType)
|
||||
{
|
||||
var sqlParameterKeyWord = parameter.Context.SqlParameterKeyWord;
|
||||
var reval = string.Format("{0}{1}{2}", sqlParameterKeyWord, leftInfo.Value, parameter.Context.Index + parameter.Index);
|
||||
parameter.Context.Parameters.Add(new SugarParameter(reval, leftInfo.Value));
|
||||
return reval;
|
||||
}
|
||||
return leftInfo.Value.ObjToString();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -20,19 +20,44 @@ namespace SqlSugar
|
||||
parameter.BaseParameter.CommonTempData = value;
|
||||
break;
|
||||
case ResolveExpressType.WhereSingle:
|
||||
if (parameter.BaseExpression is BinaryExpression)
|
||||
{
|
||||
var otherExpression = isLeft == true ? parameter.BaseParameter.RightExpression : parameter.BaseParameter.LeftExpression;
|
||||
if (otherExpression is MemberExpression)
|
||||
{
|
||||
string parameterName = Context.SqlParameterKeyWord
|
||||
+ ((MemberExpression)otherExpression).Member.Name
|
||||
+ Context.ParameterIndex;
|
||||
base.Context.Parameters.Add(new SugarParameter(parameterName, value));
|
||||
Context.ParameterIndex++;
|
||||
parameterName = string.Format(" {0} ", parameterName);
|
||||
if (isLeft == true)
|
||||
{
|
||||
parameterName += ExpressionConst.Format1 + parameter.BaseParameter.Index;
|
||||
}
|
||||
if (base.Context.Result.Contains(ExpressionConst.Format0))
|
||||
{
|
||||
base.Context.Result.Replace(ExpressionConst.Format0, parameterName);
|
||||
}
|
||||
else {
|
||||
base.Context.Result.Append(parameterName);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
|
||||
}
|
||||
}
|
||||
break;
|
||||
case ResolveExpressType.WhereMultiple:
|
||||
if (parameter.BaseExpression is BinaryExpression)
|
||||
{
|
||||
|
||||
}
|
||||
break;
|
||||
case ResolveExpressType.FieldSingle:
|
||||
case ResolveExpressType.FieldMultiple:
|
||||
default:
|
||||
if (parameter.BaseParameter.BinaryTempData != null)
|
||||
{
|
||||
parameter.BaseParameter.BinaryTempData.Add(new KeyValuePair<string, BinaryExpressionInfo>(ExpressionConst.BinaryExpressionInfoListKey, new BinaryExpressionInfo()
|
||||
{
|
||||
IsLeft = Convert.ToBoolean(isLeft),
|
||||
Value = value,
|
||||
ExpressionType = expression.GetType()
|
||||
}));
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
@ -24,16 +24,6 @@ namespace SqlSugar
|
||||
case ResolveExpressType.WhereMultiple:
|
||||
case ResolveExpressType.FieldSingle:
|
||||
case ResolveExpressType.FieldMultiple:
|
||||
default:
|
||||
if (parameter.BaseParameter.BinaryTempData != null)
|
||||
{
|
||||
parameter.BaseParameter.BinaryTempData.Add(new KeyValuePair<string, BinaryExpressionInfo>(ExpressionConst.BinaryExpressionInfoListKey, new BinaryExpressionInfo()
|
||||
{
|
||||
IsLeft = Convert.ToBoolean(isLeft),
|
||||
Value = value,
|
||||
ExpressionType = expression.Expression.GetType()
|
||||
}));
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
@ -24,13 +24,25 @@ namespace SqlSugar
|
||||
base.Context.Result.Append(fieldName);
|
||||
break;
|
||||
case ResolveExpressType.WhereSingle:
|
||||
fieldName = getSingleName(parameter, expression, isLeft);
|
||||
if (parameter.BaseExpression is BinaryExpression)
|
||||
{
|
||||
fieldName = getSingleName(parameter, expression, isLeft);
|
||||
fieldName = string.Format(" {0} ", fieldName);
|
||||
if (isLeft == true)
|
||||
{
|
||||
fieldName += ExpressionConst.Format1 + parameter.BaseParameter.Index;
|
||||
}
|
||||
if (base.Context.Result.Contains(ExpressionConst.Format0))
|
||||
{
|
||||
base.Context.Result.Replace(ExpressionConst.Format0, fieldName);
|
||||
}
|
||||
else
|
||||
{
|
||||
base.Context.Result.Append(fieldName);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
fieldName = getSingleName(parameter, expression, isLeft);
|
||||
base.Context.Result.Append(fieldName);
|
||||
}
|
||||
break;
|
||||
@ -56,26 +68,12 @@ namespace SqlSugar
|
||||
string shortName = expression.Expression.ToString();
|
||||
string fieldName = expression.Member.Name;
|
||||
fieldName = shortName + "." + fieldName;
|
||||
if (parameter.BaseParameter.BinaryTempData != null)
|
||||
parameter.BaseParameter.BinaryTempData.Add(new KeyValuePair<string, BinaryExpressionInfo>(ExpressionConst.BinaryExpressionInfoListKey, new BinaryExpressionInfo()
|
||||
{
|
||||
IsLeft = Convert.ToBoolean(isLeft),
|
||||
Value = fieldName,
|
||||
ExpressionType = expression.GetType()
|
||||
}));
|
||||
return fieldName;
|
||||
}
|
||||
|
||||
private string getSingleName(ExpressionParameter parameter, MemberExpression expression, bool? isLeft)
|
||||
{
|
||||
string fieldName = expression.Member.Name;
|
||||
if (parameter.BaseParameter.BinaryTempData != null)
|
||||
parameter.BaseParameter.BinaryTempData.Add(new KeyValuePair<string, BinaryExpressionInfo>(ExpressionConst.BinaryExpressionInfoListKey, new BinaryExpressionInfo()
|
||||
{
|
||||
IsLeft = Convert.ToBoolean(isLeft),
|
||||
Value = fieldName,
|
||||
ExpressionType = expression.GetType()
|
||||
}));
|
||||
return fieldName;
|
||||
}
|
||||
}
|
||||
|
@ -14,15 +14,6 @@ namespace SqlSugar
|
||||
var isLeft = parameter.IsLeft;
|
||||
object value = null;
|
||||
value = ExpressionTool.DynamicInvoke(expression);
|
||||
if (parameter.BaseParameter.BinaryTempData != null)
|
||||
{
|
||||
parameter.BaseParameter.BinaryTempData.Add(new KeyValuePair<string, BinaryExpressionInfo>(ExpressionConst.BinaryExpressionInfoListKey, new BinaryExpressionInfo()
|
||||
{
|
||||
IsLeft = Convert.ToBoolean(isLeft),
|
||||
Value = value,
|
||||
ExpressionType = ExpressionConst.ConstantExpressionType
|
||||
}));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -23,15 +23,6 @@ namespace SqlSugar
|
||||
{
|
||||
value = ExpressionTool.GetPropertyValue(expression);
|
||||
}
|
||||
if (parameter.BaseParameter.BinaryTempData != null)
|
||||
{
|
||||
parameter.BaseParameter.BinaryTempData.Add(new KeyValuePair<string, BinaryExpressionInfo>(ExpressionConst.BinaryExpressionInfoListKey, new BinaryExpressionInfo()
|
||||
{
|
||||
IsLeft = Convert.ToBoolean(isLeft),
|
||||
Value = value,
|
||||
ExpressionType = ExpressionConst.ConstantExpressionType
|
||||
}));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user