diff --git a/.vs/SqlSugar/v14/.suo b/.vs/SqlSugar/v14/.suo index 63a493758..68b1f44cf 100644 Binary files a/.vs/SqlSugar/v14/.suo and b/.vs/SqlSugar/v14/.suo differ diff --git a/OrmTest/ExpressionTest/Field.cs b/OrmTest/ExpressionTest/Field.cs index dd0315667..f8634ba60 100644 --- a/OrmTest/ExpressionTest/Field.cs +++ b/OrmTest/ExpressionTest/Field.cs @@ -9,26 +9,38 @@ using System.Threading.Tasks; namespace OrmTest.ExpressionTest { - public class Field + public class Field:ExpTestBase { - internal static void Init() + private Field() { } + public Field(int eachCount) { - FieldSingle(); - FieldMultiple(); + this.Count = eachCount; } - private static void FieldSingle() + internal void Init() + { + base.Begin(); + for (int i = 0; i < base.Count; i++) + { + FieldSingle(); + FieldMultiple(); + } + base.End("Filed Test"); + } + private void FieldSingle() { Expression> exp = it => it.Name; ExpressionContext expContext = new ExpressionContext(exp, ResolveExpressType.FieldSingle); expContext.Resolve(); var selectorValue = expContext.Result.GetString(); + Check(selectorValue, null, "Name", null, "FieldSingle"); } - private static void FieldMultiple() + private void FieldMultiple() { Expression> exp = it => it.Name; ExpressionContext expContext = new ExpressionContext(exp, ResolveExpressType.FieldMultiple); expContext.Resolve(); var selectorValue = expContext.Result.GetString(); + Check(selectorValue, null, "it.Name", null, "FieldMultiple"); } } } diff --git a/OrmTest/ExpressionTest/Where.cs b/OrmTest/ExpressionTest/Where.cs index 5f8f197bd..4e265d199 100644 --- a/OrmTest/ExpressionTest/Where.cs +++ b/OrmTest/ExpressionTest/Where.cs @@ -1,12 +1,80 @@ -using System; +using OrmTest.Models; +using SqlSugar; +using System; using System.Collections.Generic; using System.Linq; +using System.Linq.Expressions; using System.Text; using System.Threading.Tasks; namespace OrmTest.ExpressionTest { - public class Where + public class Where : ExpTestBase { + private Where() { } + public Where(int eachCount) + { + this.Count = eachCount; + } + internal void Init() + { + base.Begin(); + for (int i = 0; i < base.Count; i++) + { + whereSingle1(); + whereSingle2(); + whereSingle3(); + whereSingle4(); + } + base.End("Where Test"); + } + + private void whereSingle1() + { + Expression> exp = it => it.Id > 1; + ExpressionContext expContext = new ExpressionContext(exp, ResolveExpressType.WhereSingle); + expContext.Resolve(); + var value = expContext.Result.GetString(); + var pars = expContext.Parameters; + base.Check(value, pars, " ( Id > @Id1 ) ", new List() { + new SugarParameter("@Id1",1) + }, "whereSingle1"); + } + private void whereSingle2() + { + Expression> exp = it => it.Id > 1 || it.Name == "a"; + 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() { + new SugarParameter("@Id1",1), + new SugarParameter("@Name2","a") + }, "whereSingle2"); + } + private void whereSingle3() + { + Expression> exp = it => it.Id > 1 || it.Name == "a"; + 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() { + new SugarParameter("@Id1",1), + new SugarParameter("@Name2","a") + }, "whereSingle2"); + } + private void whereSingle4() + { + Expression> exp = it =>( it.Id > 1 &&it.Name!="a")|| it.Name == "a"; + 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() { + new SugarParameter("@Id1",1), + new SugarParameter("@Name2","a") + }, "whereSingle4"); + } } } diff --git a/OrmTest/Program.cs b/OrmTest/Program.cs index 4dda4aa50..6a4968c9f 100644 --- a/OrmTest/Program.cs +++ b/OrmTest/Program.cs @@ -18,7 +18,8 @@ namespace OrmTest { int eachCount = 1; new OrmTest.ExpressionTest.Select(eachCount).Init(); - OrmTest.ExpressionTest.Field.Init(); + new OrmTest.ExpressionTest.Field(eachCount).Init(); + new OrmTest.ExpressionTest.Where(eachCount).Init(); // Program.id = "xx"; diff --git a/OrmTest/bin/Debug/OrmTest.exe b/OrmTest/bin/Debug/OrmTest.exe index 25ebdfe4b..ea14578e7 100644 Binary files a/OrmTest/bin/Debug/OrmTest.exe and b/OrmTest/bin/Debug/OrmTest.exe differ diff --git a/OrmTest/bin/Debug/OrmTest.pdb b/OrmTest/bin/Debug/OrmTest.pdb index 3b2933bd2..7c9dd49ad 100644 Binary files a/OrmTest/bin/Debug/OrmTest.pdb and b/OrmTest/bin/Debug/OrmTest.pdb differ diff --git a/OrmTest/bin/Debug/SqlSugar.dll b/OrmTest/bin/Debug/SqlSugar.dll index d272f7d5c..8b6f9e747 100644 Binary files a/OrmTest/bin/Debug/SqlSugar.dll and b/OrmTest/bin/Debug/SqlSugar.dll differ diff --git a/OrmTest/bin/Debug/SqlSugar.pdb b/OrmTest/bin/Debug/SqlSugar.pdb index f326250db..48c8cbbe0 100644 Binary files a/OrmTest/bin/Debug/SqlSugar.pdb and b/OrmTest/bin/Debug/SqlSugar.pdb differ diff --git a/SqlSugar/ExpressionsToSql/Common/ExpressionConst.cs b/SqlSugar/ExpressionsToSql/Common/ExpressionConst.cs index 81b2164de..5bc199ee1 100644 --- a/SqlSugar/ExpressionsToSql/Common/ExpressionConst.cs +++ b/SqlSugar/ExpressionsToSql/Common/ExpressionConst.cs @@ -11,6 +11,7 @@ namespace SqlSugar public const string BinaryFormatString = " ( {0} {1} {2} ) "; public const string Format0 = "{0}"; public const string Format1 = "{1}"; + public const string Format2 = "{2}"; public readonly static Type MemberExpressionType = typeof(MemberExpression); public readonly static Type ConstantExpressionType = typeof(ConstantExpression); public readonly static Type StringType = typeof(string); diff --git a/SqlSugar/ExpressionsToSql/Common/ExpressionResult.cs b/SqlSugar/ExpressionsToSql/Common/ExpressionResult.cs index 236650772..ac9531e49 100644 --- a/SqlSugar/ExpressionsToSql/Common/ExpressionResult.cs +++ b/SqlSugar/ExpressionsToSql/Common/ExpressionResult.cs @@ -73,7 +73,13 @@ namespace SqlSugar public void TrimEnd() { if (this._Result == null) return; - this.Result=this.Result.Remove(this.Result.Length-1,1); + this.Result = this.Result.Remove(this.Result.Length - 1, 1); + } + + public bool Contains(string value) + { + if (this.Result.Equals(value)) return true; + return (this.Result.ToString().Contains(value)); } public void Append(object parameter) diff --git a/SqlSugar/ExpressionsToSql/ResolveItems/BinaryExpressionResolve.cs b/SqlSugar/ExpressionsToSql/ResolveItems/BinaryExpressionResolve.cs index e6ec7e379..978602695 100644 --- a/SqlSugar/ExpressionsToSql/ResolveItems/BinaryExpressionResolve.cs +++ b/SqlSugar/ExpressionsToSql/ResolveItems/BinaryExpressionResolve.cs @@ -26,18 +26,53 @@ namespace SqlSugar expression.NodeType != ExpressionType.Or && expression.NodeType != ExpressionType.OrElse; base.BaseExpression = expression; + var leftExpression = expression.Left; + 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) + { + base.Context.Result.Append("{" + i + "}"); + ++i; + } + 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}"); + } + base.Expression = leftExpression; base.IsLeft = true; - base.Expression = expression.Left; base.Start(); base.IsLeft = false; - base.Expression = expression.Right; + base.Expression = rightExpression; base.Start(); base.IsLeft = null; - string leftString = GetLeftString(parameter); - string rightString = GetRightString(parameter); - string binarySql = string.Format(ExpressionConst.BinaryFormatString, leftString, operatorValue, rightString); - string sqlWhereString = base.Context.Result.GetResultString(); - if (sqlWhereString.Contains(ExpressionConst.Format0)) + string leftString = null; + if (!leftIsBinary) + leftString = GetLeftString(parameter); + string rightString = null; + if (!rightBinary) + rightString = GetRightString(parameter); + string binarySql = null; + if (lsbs) + { + 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); } @@ -45,9 +80,10 @@ namespace SqlSugar { base.Context.Result.Append(binarySql); } - if (sqlWhereString.Contains(ExpressionConst.Format1)) + if (Context.Result.Contains(ExpressionConst.Format1)) { base.Context.Result.Replace(ExpressionConst.Format1, ExpressionConst.Format0); + base.Context.Result.Replace(ExpressionConst.Format2, ExpressionConst.Format1); } } } @@ -60,7 +96,8 @@ namespace SqlSugar { var sqlParameterKeyWord = parameter.Context.SqlParameterKeyWord; var reval = string.Format("{0}{1}{2}", sqlParameterKeyWord, leftInfo.Value, parameter.Context.Index + parameter.Index); - if (parameter.Context.Parameter