From cc9e47246e9dc402d9799ee3908fdf22a4144ba8 Mon Sep 17 00:00:00 2001 From: sunkaixuan <610262374@qq.com> Date: Tue, 13 Jun 2017 16:23:08 +0800 Subject: [PATCH] Expression Bug --- SqlServerTest/Demos/1_Query.cs | 2 ++ SqlServerTest/Program.cs | 24 +++++++++---------- .../UnitTest/ExpressionTest/Where.cs | 14 +++++++++++ .../ResolveItems/BaseResolve.cs | 15 ++++++++++++ .../ResolveItems/ConstantExpressionResolve.cs | 13 +++++++--- 5 files changed, 53 insertions(+), 15 deletions(-) diff --git a/SqlServerTest/Demos/1_Query.cs b/SqlServerTest/Demos/1_Query.cs index 0e53dcd3d..d96f0a571 100644 --- a/SqlServerTest/Demos/1_Query.cs +++ b/SqlServerTest/Demos/1_Query.cs @@ -119,9 +119,11 @@ namespace OrmTest.Demo private static void Ado() { var db = GetInstance(); + db.Ado.BeginTran(); var t1 = db.Ado.SqlQuery("select 'a'"); var t2 = db.Ado.GetInt("select 1"); var t3 = db.Ado.GetDataTable("select 1 as id"); + db.Ado.CommitTran(); //more //db.Ado.GetXXX... } diff --git a/SqlServerTest/Program.cs b/SqlServerTest/Program.cs index caafd709e..114e595ba 100644 --- a/SqlServerTest/Program.cs +++ b/SqlServerTest/Program.cs @@ -18,18 +18,18 @@ namespace OrmTest static void Main(string[] args) { /***Unit Test***/ - new Field(1).Init(); - new Where(1).Init(); - new Method(1).Init(); - new JoinQuery(1).Init(); - new SingleQuery(1).Init(); - new SelectQuery(1).Init(); - new AutoClose(1).Init(); - new Insert(1).Init(); - new Delete(1).Init(); - new Update(1).Init(); - new Mapping(1).Init(); - new DataTest(1).Init(); + //new Field(1).Init(); + //new Where(1).Init(); + //new Method(1).Init(); + //new JoinQuery(1).Init(); + //new SingleQuery(1).Init(); + //new SelectQuery(1).Init(); + //new AutoClose(1).Init(); + //new Insert(1).Init(); + //new Delete(1).Init(); + //new Update(1).Init(); + //new Mapping(1).Init(); + //new DataTest(1).Init(); /***Performance Test***/ new SqlSugarPerformance(100).Select(); diff --git a/SqlServerTest/UnitTest/ExpressionTest/Where.cs b/SqlServerTest/UnitTest/ExpressionTest/Where.cs index ce4965bb6..fc6f74d56 100644 --- a/SqlServerTest/UnitTest/ExpressionTest/Where.cs +++ b/SqlServerTest/UnitTest/ExpressionTest/Where.cs @@ -34,6 +34,7 @@ namespace OrmTest.UnitTest whereSingle11(); whereSingle12(); whereSingle13(); + whereSingle14(); WhereMultiple1(); WhereMultiple2(); @@ -235,6 +236,19 @@ namespace OrmTest.UnitTest }, "whereSingle13"); } + + private void whereSingle14() + { + Expression> exp = it =>true&& it.Name != null; + ExpressionContext expContext = new ExpressionContext(); + expContext.Resolve(exp, ResolveExpressType.WhereSingle); + var value = expContext.Result.GetString(); + var pars = expContext.Parameters; + base.Check(value, pars, "(( 1 = 1 ) AND( [Name] IS NOT NULL ))", new List() + { + + }, "whereSingle14"); + } } public class WhereConst diff --git a/SqlSugar/ExpressionsToSql/ResolveItems/BaseResolve.cs b/SqlSugar/ExpressionsToSql/ResolveItems/BaseResolve.cs index 69c47a37e..86c69f944 100644 --- a/SqlSugar/ExpressionsToSql/ResolveItems/BaseResolve.cs +++ b/SqlSugar/ExpressionsToSql/ResolveItems/BaseResolve.cs @@ -106,7 +106,22 @@ namespace SqlSugar } return null; } + protected void AppendMember(ExpressionParameter parameter, bool? isLeft, object appendValue) { + Context.ParameterIndex++; + if (isLeft == true) + { + appendValue += ExpressionConst.Format1 + parameter.BaseParameter.Index; + } + if (this.Context.Result.Contains(ExpressionConst.Format0)) + { + this.Context.Result.Replace(ExpressionConst.Format0, appendValue.ObjToString()); + } + else + { + this.Context.Result.Append(appendValue); + } + } protected void AppendValue(ExpressionParameter parameter, bool? isLeft, object value) { if (parameter.BaseExpression is BinaryExpression || parameter.BaseExpression == null) diff --git a/SqlSugar/ExpressionsToSql/ResolveItems/ConstantExpressionResolve.cs b/SqlSugar/ExpressionsToSql/ResolveItems/ConstantExpressionResolve.cs index 899503a41..cb9fb56b8 100644 --- a/SqlSugar/ExpressionsToSql/ResolveItems/ConstantExpressionResolve.cs +++ b/SqlSugar/ExpressionsToSql/ResolveItems/ConstantExpressionResolve.cs @@ -33,11 +33,18 @@ namespace SqlSugar { var parentIsBinary = parameter.BaseParameter.CurrentExpression is BinaryExpression; var parentIsRoot = parameter.BaseParameter.CurrentExpression is LambdaExpression; - if (parentIsRoot&& value!=null&&value.GetType()==PubConst.BoolType) { - this.Context.Result.Append(value.ObjToBool()?"( 1 = 1 ) ": "( 1 = 2 ) "); + if (parentIsRoot && value != null && value.GetType() == PubConst.BoolType) + { + this.Context.Result.Append(value.ObjToBool() ? "( 1 = 1 ) " : "( 1 = 2 ) "); break; } - if (value == null && parentIsBinary) { + if (parentIsBinary && value != null && value.GetType() == PubConst.BoolType && parameter.OppsiteExpression is BinaryExpression) + { + AppendMember(parameter,isLeft,(value.ObjToBool() ? "( 1 = 1 ) " : "( 1 = 2 ) ")); + break; + } + if (value == null && parentIsBinary) + { parameter.BaseParameter.ValueIsNull = true; value = "NULL"; }