From d3de52b89fbfc6b182f5cb280a7988ad272b26d8 Mon Sep 17 00:00:00 2001 From: sunkaixuan <610262374@qq.com> Date: Tue, 11 Jul 2017 13:18:24 +0800 Subject: [PATCH] Support native HasValue --- Src/Asp.Net/SqlServerTest/Program.cs | 2 +- .../UnitTest/ExpressionTest/Select.cs | 7 +++- .../ResolveItems/BaseResolve.cs | 38 +++++++++++-------- .../ResolveItems/UnaryExpressionResolve.cs | 30 +++++++++++---- 4 files changed, 50 insertions(+), 27 deletions(-) diff --git a/Src/Asp.Net/SqlServerTest/Program.cs b/Src/Asp.Net/SqlServerTest/Program.cs index 9b47ae5dc..f636767fe 100644 --- a/Src/Asp.Net/SqlServerTest/Program.cs +++ b/Src/Asp.Net/SqlServerTest/Program.cs @@ -18,7 +18,7 @@ namespace OrmTest static void Main(string[] args) { // /***Unit Test***/ - //new Select(1).Init(); + new Select(1).Init(); new Field(1).Init(); new Where(1).Init(); new Method(1).Init(); diff --git a/Src/Asp.Net/SqlServerTest/UnitTest/ExpressionTest/Select.cs b/Src/Asp.Net/SqlServerTest/UnitTest/ExpressionTest/Select.cs index 9cae7d85b..2f1b7ddf1 100644 --- a/Src/Asp.Net/SqlServerTest/UnitTest/ExpressionTest/Select.cs +++ b/Src/Asp.Net/SqlServerTest/UnitTest/ExpressionTest/Select.cs @@ -129,8 +129,9 @@ namespace OrmTest.UnitTest "Select.single4 Error"); } - private void single5(DateTime? p = null) + private void single5() { + var p =(DateTime?) DateTime.Now; Expression> exp = it => p.HasValue; SqlServerExpressionContext expContext = new SqlServerExpressionContext(); expContext.IsSingle = false; @@ -138,7 +139,9 @@ namespace OrmTest.UnitTest var selectorValue = expContext.Result.GetString(); var pars = expContext.Parameters; base.Check( - @"( [it].[SchoolId]<>'' AND [it].[SchoolId] IS NOT NULL )", null, selectorValue, null, + @"( @constant0<>'' AND @constant0 IS NOT NULL )", new List() { + new SugarParameter("@constant0",p) + }, selectorValue, pars, "Select.single4 Error"); } diff --git a/Src/Asp.Net/SqlSugar/ExpressionsToSql/ResolveItems/BaseResolve.cs b/Src/Asp.Net/SqlSugar/ExpressionsToSql/ResolveItems/BaseResolve.cs index 0100e91da..bab23d6a2 100644 --- a/Src/Asp.Net/SqlSugar/ExpressionsToSql/ResolveItems/BaseResolve.cs +++ b/Src/Asp.Net/SqlSugar/ExpressionsToSql/ResolveItems/BaseResolve.cs @@ -106,6 +106,7 @@ namespace SqlSugar } return null; } + protected void AppendMember(ExpressionParameter parameter, bool? isLeft, object appendValue) { @@ -197,6 +198,25 @@ namespace SqlSugar this.Context.Result.Append(" " + ExpressionConst.Format1 + parameter.BaseParameter.Index); } } + protected string AppendParameter(object paramterValue) + { + string parameterName = this.Context.SqlParameterKeyWord + "constant" + this.Context.ParameterIndex; + this.Context.ParameterIndex++;; + this.Context.Parameters.Add(new SugarParameter(parameterName, paramterValue)); + return parameterName; + } + protected void AppendNot(object Value) + { + var isAppend = !this.Context.Result.Contains(ExpressionConst.Format0); + if (isAppend) + { + this.Context.Result.Append("NOT"); + } + else + { + this.Context.Result.Replace(ExpressionConst.Format0, "NOT"); + } + } protected MethodCallExpressionArgs GetMethodCallArgs(ExpressionParameter parameter, Expression item) { @@ -215,7 +235,6 @@ namespace SqlSugar }; return methodCallExpressionArgs; } - protected void ResolveNewExpressions(ExpressionParameter parameter, Expression item, string asName) { if (item.NodeType == ExpressionType.Constant) @@ -229,7 +248,7 @@ namespace SqlSugar } else if ((item is MemberExpression) && ((MemberExpression)item).Expression == null) { - var paramterValue= ExpressionTool.GetPropertyValue(item as MemberExpression); + var paramterValue = ExpressionTool.GetPropertyValue(item as MemberExpression); string parameterName = this.Context.SqlParameterKeyWord + "constant" + this.Context.ParameterIndex; this.Context.ParameterIndex++; parameter.Context.Result.Append(this.Context.GetAsString(asName, parameterName)); @@ -337,7 +356,7 @@ namespace SqlSugar var listProperties = item.Type.GetProperties().Cast().ToList(); foreach (var property in listProperties) { - var hasIgnore = this.Context.IgnoreComumnList != null && this.Context.IgnoreComumnList.Any(it => it.EntityName.Equals(item.Type.Name,StringComparison.CurrentCultureIgnoreCase) && it.PropertyName.Equals(property.Name,StringComparison.CurrentCultureIgnoreCase)); + var hasIgnore = this.Context.IgnoreComumnList != null && this.Context.IgnoreComumnList.Any(it => it.EntityName.Equals(item.Type.Name, StringComparison.CurrentCultureIgnoreCase) && it.PropertyName.Equals(property.Name, StringComparison.CurrentCultureIgnoreCase)); if (hasIgnore) { continue; @@ -366,18 +385,5 @@ namespace SqlSugar Check.ThrowNotSupportedException(item.GetType().Name); } } - - protected void AppendNot(object Value) - { - var isAppend = !this.Context.Result.Contains(ExpressionConst.Format0); - if (isAppend) - { - this.Context.Result.Append("NOT"); - } - else - { - this.Context.Result.Replace(ExpressionConst.Format0, "NOT"); - } - } } } diff --git a/Src/Asp.Net/SqlSugar/ExpressionsToSql/ResolveItems/UnaryExpressionResolve.cs b/Src/Asp.Net/SqlSugar/ExpressionsToSql/ResolveItems/UnaryExpressionResolve.cs index 75deb10b5..08fd3276d 100644 --- a/Src/Asp.Net/SqlSugar/ExpressionsToSql/ResolveItems/UnaryExpressionResolve.cs +++ b/Src/Asp.Net/SqlSugar/ExpressionsToSql/ResolveItems/UnaryExpressionResolve.cs @@ -60,17 +60,31 @@ namespace SqlSugar var member = memberExpression.Expression as MemberExpression; parameter.CommonTempData = CommonTempDataType.Result; var isConst = member.Expression != null && member.Expression is ConstantExpression; - this.Expression = isConst?member.Expression:member; - this.Start(); - var methodParamter = isConst ? new MethodCallExpressionArgs() { IsMember=false } : new MethodCallExpressionArgs() { IsMember = true, MemberName = parameter.CommonTempData, MemberValue = null }; - var result = this.Context.DbMehtods.HasValue(new MethodCallExpressionModel() + if (isConst) { - Args = new List() { + var paramterValue = ExpressionTool.DynamicInvoke(member); + var paramterName= base.AppendParameter(paramterValue); + var result = this.Context.DbMehtods.HasValue(new MethodCallExpressionModel() + { + Args = new List() { + new MethodCallExpressionArgs() { IsMember=false, MemberName=paramterName, MemberValue=paramterValue } } + }); + this.Context.Result.Append(result); + } + else + { + this.Expression = isConst ? member.Expression : member; + this.Start(); + var methodParamter = isConst ? new MethodCallExpressionArgs() { IsMember = false } : new MethodCallExpressionArgs() { IsMember = true, MemberName = parameter.CommonTempData, MemberValue = null }; + var result = this.Context.DbMehtods.HasValue(new MethodCallExpressionModel() + { + Args = new List() { methodParamter } - }); - this.Context.Result.Append(result); - parameter.CommonTempData = null; + }); + this.Context.Result.Append(result); + parameter.CommonTempData = null; + } } else if (memberExpression.Type == PubConst.BoolType && isLogicOperator) {