diff --git a/OrmTest/ExpressionTest/Method.cs b/OrmTest/ExpressionTest/Method.cs index 5aa74f4eb..62b5f724a 100644 --- a/OrmTest/ExpressionTest/Method.cs +++ b/OrmTest/ExpressionTest/Method.cs @@ -27,6 +27,7 @@ namespace OrmTest.ExpressionTest StringIsNullOrEmpty3(); StringIsNullOrEmpty4(); ToUpper(); + ToLower(); #endregion } base.End("Method Test"); @@ -34,15 +35,26 @@ namespace OrmTest.ExpressionTest private void ToUpper() { - Expression> exp = it =>"a"== NBORM.ToLower(it.Id) ; + Expression> exp = it =>"a"== NBORM.ToUpper(it.Id) ; SqlServerExpressionContext expContext = new SqlServerExpressionContext(exp, ResolveExpressType.WhereSingle); expContext.Resolve(); var value = expContext.Result.GetString(); var pars = expContext.Parameters; - base.Check(value, pars, "(( Id > @Id0 ) OR ( Id='' OR Id IS NULL ))", new List() { - new SugarParameter("@Id0",2) + base.Check(value, pars, "( @Const0 = (UPPER(Id)) )", new List() { + new SugarParameter("@Const0","a") }, "ToUpper"); } + private void ToLower() + { + Expression> exp = it => "a" == NBORM.ToLower(it.Id); + SqlServerExpressionContext expContext = new SqlServerExpressionContext(exp, ResolveExpressType.WhereSingle); + expContext.Resolve(); + var value = expContext.Result.GetString(); + var pars = expContext.Parameters; + base.Check(value, pars, "( @Const0 = (LOWER(Id)) )", new List() { + new SugarParameter("@Const0","a") + }, "ToLower"); + } #region StringIsNullOrEmpty private void StringIsNullOrEmpty() @@ -63,8 +75,8 @@ namespace OrmTest.ExpressionTest expContext.Resolve(); var value = expContext.Result.GetString(); var pars = expContext.Parameters; - base.Check(value, pars, "(( @Id0 = Id ) OR ( @MethodCost1='' OR @MethodCost1 IS NULL ))", new List() { - new SugarParameter("@MethodCost1",true), + base.Check(value, pars, "(( @Id0 = Id ) OR ( @MethodConst1='' OR @MethodConst1 IS NULL ))", new List() { + new SugarParameter("@MethodConst1",true), new SugarParameter("@Id0",2) }, "StringIsNullOrEmpty2"); } @@ -76,8 +88,8 @@ namespace OrmTest.ExpressionTest expContext.Resolve(); var value = expContext.Result.GetString(); var pars = expContext.Parameters; - base.Check(value, pars, "(( @Id0 = Id ) OR ( @MethodCost1='' OR @MethodCost1 IS NULL ))", new List() { - new SugarParameter("@MethodCost1",1), + base.Check(value, pars, "(( @Id0 = Id ) OR ( @MethodConst1='' OR @MethodConst1 IS NULL ))", new List() { + new SugarParameter("@MethodConst1",1), new SugarParameter("@Id0",2) }, "StringIsNullOrEmpty3"); } @@ -89,8 +101,8 @@ namespace OrmTest.ExpressionTest expContext.Resolve(); var value = expContext.Result.GetString(); var pars = expContext.Parameters; - base.Check(value, pars, "(( @Id0 = Id ) OR ( @MethodCost1='' OR @MethodCost1 IS NULL ))", new List() { - new SugarParameter("@MethodCost1","xx"), + base.Check(value, pars, "(( @Id0 = Id ) OR ( @MethodConst1='' OR @MethodConst1 IS NULL ))", new List() { + new SugarParameter("@MethodConst1","xx"), new SugarParameter("@Id0",2) }, "StringIsNullOrEmpty4"); } diff --git a/SqlSugar/ExpressionsToSql/Common/ExpressionConst.cs b/SqlSugar/ExpressionsToSql/Common/ExpressionConst.cs index b2863b868..94f2210b5 100644 --- a/SqlSugar/ExpressionsToSql/Common/ExpressionConst.cs +++ b/SqlSugar/ExpressionsToSql/Common/ExpressionConst.cs @@ -14,7 +14,8 @@ namespace SqlSugar public const string Format3 = "("; public const string Format4 = ")"; public const string NBORMFULLNAME = "SqlSugar.NBORM"; - public const string METHODCOST = "MethodCost"; + public const string METHODCONST = "MethodConst"; + public const string CONST = "Const"; 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/Method/DefaultDbMethod.cs b/SqlSugar/ExpressionsToSql/Method/DefaultDbMethod.cs index 6f5a94f92..f5b42f152 100644 --- a/SqlSugar/ExpressionsToSql/Method/DefaultDbMethod.cs +++ b/SqlSugar/ExpressionsToSql/Method/DefaultDbMethod.cs @@ -13,13 +13,13 @@ namespace SqlSugar return string.Format("( {0}='' OR {0} IS NULL )", parameter.Value); } - public object ToUpper(MethodCallExpressionModel model) + public virtual object ToUpper(MethodCallExpressionModel model) { var parameter = model.Args[0]; return string.Format(" (UPPER({0})) ", parameter.Value); } - public object ToLower(MethodCallExpressionModel model) + public virtual object ToLower(MethodCallExpressionModel model) { var parameter = model.Args[0]; return string.Format(" (LOWER({0})) ", parameter.Value); diff --git a/SqlSugar/ExpressionsToSql/ResolveItems/BaseResolve.cs b/SqlSugar/ExpressionsToSql/ResolveItems/BaseResolve.cs index e0cc35250..f361bab6e 100644 --- a/SqlSugar/ExpressionsToSql/ResolveItems/BaseResolve.cs +++ b/SqlSugar/ExpressionsToSql/ResolveItems/BaseResolve.cs @@ -124,9 +124,24 @@ namespace SqlSugar this.Context.Result.Append(parameterName); } } - else + else { - + var parameterName =this.Context.SqlParameterKeyWord+ExpressionConst.CONST +Context.ParameterIndex; + Context.ParameterIndex++; + this.Context.Parameters.Add(new SugarParameter(parameterName,value)); + parameterName = string.Format(" {0} ", parameterName); + if (isLeft == true) + { + parameterName += ExpressionConst.Format1 + parameter.BaseParameter.Index; + } + if (this.Context.Result.Contains(ExpressionConst.Format0)) + { + this.Context.Result.Replace(ExpressionConst.Format0, parameterName); + } + else + { + this.Context.Result.Append(parameterName); + } } } } diff --git a/SqlSugar/ExpressionsToSql/ResolveItems/ConstantExpressionResolve.cs b/SqlSugar/ExpressionsToSql/ResolveItems/ConstantExpressionResolve.cs index c6065c81a..cb15a54ff 100644 --- a/SqlSugar/ExpressionsToSql/ResolveItems/ConstantExpressionResolve.cs +++ b/SqlSugar/ExpressionsToSql/ResolveItems/ConstantExpressionResolve.cs @@ -31,6 +31,7 @@ namespace SqlSugar { AppendParameter(parameter, isLeft, value); } + break; case ResolveExpressType.FieldSingle: case ResolveExpressType.FieldMultiple: diff --git a/SqlSugar/ExpressionsToSql/ResolveItems/MethodCallExpressionResolve.cs b/SqlSugar/ExpressionsToSql/ResolveItems/MethodCallExpressionResolve.cs index 6acf8ccf6..4a94030ec 100644 --- a/SqlSugar/ExpressionsToSql/ResolveItems/MethodCallExpressionResolve.cs +++ b/SqlSugar/ExpressionsToSql/ResolveItems/MethodCallExpressionResolve.cs @@ -42,7 +42,7 @@ namespace SqlSugar } if (methodCallExpressionArgs.IsMember == false) { - var parameterName = this.Context.SqlParameterKeyWord + ExpressionConst.METHODCOST + this.Context.ParameterIndex; + var parameterName = this.Context.SqlParameterKeyWord + ExpressionConst.METHODCONST + this.Context.ParameterIndex; this.Context.ParameterIndex++; methodCallExpressionArgs.Value = parameterName; this.Context.Parameters.Add(new SugarParameter(parameterName, value)); @@ -69,6 +69,8 @@ namespace SqlSugar return this.Context.DbMehtods.IsNullOrEmpty(model); case "ToLower": return this.Context.DbMehtods.ToLower(model); + case "ToUpper": + return this.Context.DbMehtods.ToUpper(model); default: break; }