From e28f8b0ad7d2abc487e02134a2e00dc6a397ae23 Mon Sep 17 00:00:00 2001 From: sunkaixuan <610262374@qq.com> Date: Tue, 4 Jul 2017 01:00:39 +0800 Subject: [PATCH] Update Core --- .../QueryableProvider/QueryableProvider.cs | 100 +++++++++++++++++- .../ExpressionsToSql/Common/ExpressionTool.cs | 12 ++- .../Method/DefaultDbMethod.cs | 7 ++ .../ExpressionsToSql/Method/IDbMethods.cs | 1 + .../ExpressionsToSql/Method/SqlFunc.cs | 1 + .../ResolveItems/BinaryExpressionResolve.cs | 24 ++--- .../ResolveItems/MemberExpressionResolve.cs | 5 +- .../MethodCallExpressionResolve.cs | 6 ++ .../ResolveItems/UnaryExpressionResolve.cs | 15 ++- .../src/SqlSugar/Interface/IQueryable.cs | 23 +++- 10 files changed, 172 insertions(+), 22 deletions(-) diff --git a/Src/Asp.NetCore/SqlServerTest/src/SqlSugar/Abstract/QueryableProvider/QueryableProvider.cs b/Src/Asp.NetCore/SqlServerTest/src/SqlSugar/Abstract/QueryableProvider/QueryableProvider.cs index 8c2627d48..5b04a6de0 100644 --- a/Src/Asp.NetCore/SqlServerTest/src/SqlSugar/Abstract/QueryableProvider/QueryableProvider.cs +++ b/Src/Asp.NetCore/SqlServerTest/src/SqlSugar/Abstract/QueryableProvider/QueryableProvider.cs @@ -665,7 +665,7 @@ namespace SqlSugar _Where(expression); return this; } - public ISugarQueryable WhereIF(bool isWhere, Expression> expression) + public new ISugarQueryable WhereIF(bool isWhere, Expression> expression) { if (isWhere) _Where(expression); @@ -677,6 +677,18 @@ namespace SqlSugar _Where(expression); return this; } + public new ISugarQueryable Where(string whereString, object whereObj) + { + Where(whereString, whereObj); + return this; + } + + public new ISugarQueryable WhereIF(bool isWhere, string whereString, object whereObj) + { + if (!isWhere) return this; + this.Where(whereString, whereObj); + return this; + } #endregion #region Select @@ -802,12 +814,25 @@ namespace SqlSugar return this; } - public ISugarQueryable WhereIF(bool isWhere, Expression> expression) + public new ISugarQueryable WhereIF(bool isWhere, Expression> expression) { if (isWhere) _Where(expression); return this; } + + public new ISugarQueryable Where(string whereString, object whereObj) + { + Where(whereString, whereObj); + return this; + } + + public new ISugarQueryable WhereIF(bool isWhere, string whereString, object whereObj) + { + if (!isWhere) return this; + this.Where(whereString, whereObj); + return this; + } #endregion } @@ -837,7 +862,7 @@ namespace SqlSugar return this; } - public ISugarQueryable WhereIF(bool isWhere, Expression> expression) + public new ISugarQueryable WhereIF(bool isWhere, Expression> expression) { if (isWhere) _Where(expression); @@ -864,6 +889,19 @@ namespace SqlSugar _Where(expression); return this; } + + public new ISugarQueryable Where(string whereString, object whereObj) + { + Where(whereString, whereObj); + return this; + } + + public new ISugarQueryable WhereIF(bool isWhere, string whereString, object whereObj) + { + if (!isWhere) return this; + this.Where(whereString, whereObj); + return this; + } #endregion #region Select @@ -958,7 +996,7 @@ namespace SqlSugar return this; } - public ISugarQueryable WhereIF(bool isWhere, Expression> expression) + public new ISugarQueryable WhereIF(bool isWhere, Expression> expression) { if (isWhere) _Where(expression); @@ -992,6 +1030,19 @@ namespace SqlSugar _Where(expression); return this; } + + public new ISugarQueryable Where(string whereString, object whereObj) + { + Where(whereString, whereObj); + return this; + } + + public new ISugarQueryable WhereIF(bool isWhere, string whereString, object whereObj) + { + if (!isWhere) return this; + this.Where(whereString, whereObj); + return this; + } #endregion #region Select @@ -1105,7 +1156,7 @@ namespace SqlSugar return this; } - public ISugarQueryable WhereIF(bool isWhere, Expression> expression) + public new ISugarQueryable WhereIF(bool isWhere, Expression> expression) { if (isWhere) _Where(expression); @@ -1146,6 +1197,19 @@ namespace SqlSugar _Where(expression); return this; } + + public new ISugarQueryable Where(string whereString, object whereObj) + { + Where(whereString, whereObj); + return this; + } + + public new ISugarQueryable WhereIF(bool isWhere, string whereString, object whereObj) + { + if (!isWhere) return this; + this.Where(whereString, whereObj); + return this; + } #endregion #region Select @@ -1326,6 +1390,19 @@ namespace SqlSugar _Where(expression); return this; } + + public new ISugarQueryable Where(string whereString, object whereObj) + { + Where(whereString, whereObj); + return this; + } + + public new ISugarQueryable WhereIF(bool isWhere, string whereString, object whereObj) + { + if (!isWhere) return this; + this.Where(whereString, whereObj); + return this; + } #endregion #region Select @@ -1533,6 +1610,19 @@ namespace SqlSugar _Where(expression); return this; } + + public new ISugarQueryable Where(string whereString, object whereObj) + { + Where(whereString, whereObj); + return this; + } + + public new ISugarQueryable WhereIF(bool isWhere, string whereString, object whereObj) + { + if (!isWhere) return this; + this.Where(whereString, whereObj); + return this; + } #endregion #region Select diff --git a/Src/Asp.NetCore/SqlServerTest/src/SqlSugar/ExpressionsToSql/Common/ExpressionTool.cs b/Src/Asp.NetCore/SqlServerTest/src/SqlSugar/ExpressionsToSql/Common/ExpressionTool.cs index b0c436ad4..74098b19a 100644 --- a/Src/Asp.NetCore/SqlServerTest/src/SqlSugar/ExpressionsToSql/Common/ExpressionTool.cs +++ b/Src/Asp.NetCore/SqlServerTest/src/SqlSugar/ExpressionsToSql/Common/ExpressionTool.cs @@ -46,7 +46,17 @@ namespace SqlSugar return null; } } - + public static bool IsLogicOperator(string operatorValue) + { + return operatorValue=="&&"|| operatorValue=="||"; + } + public static bool IsComparisonOperator(BinaryExpression expression) + { + return expression.NodeType != ExpressionType.And && + expression.NodeType != ExpressionType.AndAlso && + expression.NodeType != ExpressionType.Or && + expression.NodeType != ExpressionType.OrElse; + } public static object GetMemberValue(MemberInfo member, Expression expression) { var memberInfos = new Stack(); diff --git a/Src/Asp.NetCore/SqlServerTest/src/SqlSugar/ExpressionsToSql/Method/DefaultDbMethod.cs b/Src/Asp.NetCore/SqlServerTest/src/SqlSugar/ExpressionsToSql/Method/DefaultDbMethod.cs index bdefb3693..283c39e2f 100644 --- a/Src/Asp.NetCore/SqlServerTest/src/SqlSugar/ExpressionsToSql/Method/DefaultDbMethod.cs +++ b/Src/Asp.NetCore/SqlServerTest/src/SqlSugar/ExpressionsToSql/Method/DefaultDbMethod.cs @@ -247,5 +247,12 @@ namespace SqlSugar var parameter = model.Args[0]; return string.Format("COUNT({0})", parameter.MemberName); } + + public virtual string MappingColumn(MethodCallExpressionModel model) + { + var parameter = model.Args[0]; + var parameter1 = model.Args[1]; + return string.Format("{0}", parameter1.MemberValue); + } } } diff --git a/Src/Asp.NetCore/SqlServerTest/src/SqlSugar/ExpressionsToSql/Method/IDbMethods.cs b/Src/Asp.NetCore/SqlServerTest/src/SqlSugar/ExpressionsToSql/Method/IDbMethods.cs index ffd4b8ad8..df69aed5e 100644 --- a/Src/Asp.NetCore/SqlServerTest/src/SqlSugar/ExpressionsToSql/Method/IDbMethods.cs +++ b/Src/Asp.NetCore/SqlServerTest/src/SqlSugar/ExpressionsToSql/Method/IDbMethods.cs @@ -45,5 +45,6 @@ namespace SqlSugar string AggregateMin(MethodCallExpressionModel model); string AggregateMax(MethodCallExpressionModel model); string AggregateCount(MethodCallExpressionModel model); + string MappingColumn(MethodCallExpressionModel model); } } diff --git a/Src/Asp.NetCore/SqlServerTest/src/SqlSugar/ExpressionsToSql/Method/SqlFunc.cs b/Src/Asp.NetCore/SqlServerTest/src/SqlSugar/ExpressionsToSql/Method/SqlFunc.cs index da31202a3..fab566031 100644 --- a/Src/Asp.NetCore/SqlServerTest/src/SqlSugar/ExpressionsToSql/Method/SqlFunc.cs +++ b/Src/Asp.NetCore/SqlServerTest/src/SqlSugar/ExpressionsToSql/Method/SqlFunc.cs @@ -83,5 +83,6 @@ namespace SqlSugar public static TResult AggregateMin(TResult thisValue) { throw new NotSupportedException("This method is not supported by the current parameter"); } public static TResult AggregateMax(TResult thisValue) { throw new NotSupportedException("This method is not supported by the current parameter"); } public static TResult AggregateCount(TResult thisValue) { throw new NotSupportedException("This method is not supported by the current parameter"); } + public static TResult MappingColumn(TResult oldColumnName,string newColumnName) { throw new NotSupportedException("This method is not supported by the current parameter"); } } } diff --git a/Src/Asp.NetCore/SqlServerTest/src/SqlSugar/ExpressionsToSql/ResolveItems/BinaryExpressionResolve.cs b/Src/Asp.NetCore/SqlServerTest/src/SqlSugar/ExpressionsToSql/ResolveItems/BinaryExpressionResolve.cs index 4c1fcacf5..517ba04ff 100644 --- a/Src/Asp.NetCore/SqlServerTest/src/SqlSugar/ExpressionsToSql/ResolveItems/BinaryExpressionResolve.cs +++ b/Src/Asp.NetCore/SqlServerTest/src/SqlSugar/ExpressionsToSql/ResolveItems/BinaryExpressionResolve.cs @@ -18,13 +18,9 @@ namespace SqlSugar else { var expression = this.Expression as BinaryExpression; - var operatorValue =parameter.OperatorValue=ExpressionTool.GetOperator(expression.NodeType); - var isEqual = expression.NodeType==ExpressionType.Equal; - var isComparisonOperator = - expression.NodeType != ExpressionType.And && - expression.NodeType != ExpressionType.AndAlso && - expression.NodeType != ExpressionType.Or && - expression.NodeType != ExpressionType.OrElse; + var operatorValue = parameter.OperatorValue = ExpressionTool.GetOperator(expression.NodeType); + var isEqual = expression.NodeType == ExpressionType.Equal; + var isComparisonOperator =ExpressionTool.IsComparisonOperator(expression); base.ExactExpression = expression; var leftExpression = expression.Left; var rightExpression = expression.Right; @@ -40,8 +36,9 @@ namespace SqlSugar base.Context.Result.Append(ExpressionConst.Format3); base.Context.Result.Append(ExpressionConst.Format0); } - else { - base.Context.Result.Replace(ExpressionConst.Format0,ExpressionConst.Format3+ ExpressionConst.Format0); + else + { + base.Context.Result.Replace(ExpressionConst.Format0, ExpressionConst.Format3 + ExpressionConst.Format0); } parameter.LeftExpression = leftExpression; parameter.RightExpression = rightExpression; @@ -54,16 +51,17 @@ namespace SqlSugar base.IsLeft = null; if (lsbs && parameter.ValueIsNull) { - base.Context.Result.Replace(ExpressionConst.Format1 + parameter.Index, isEqual?"IS":"IS NOT"); + base.Context.Result.Replace(ExpressionConst.Format1 + parameter.Index, isEqual ? "IS" : "IS NOT"); } - else { + else + { base.Context.Result.Replace(ExpressionConst.Format1 + parameter.Index, operatorValue); - base.Context.Result.Replace(ExpressionConst.Format1 +(parameter.Index+1), operatorValue); + base.Context.Result.Replace(ExpressionConst.Format1 + (parameter.Index + 1), operatorValue); } base.Context.Result.Append(ExpressionConst.Format4); if (parameter.BaseExpression is BinaryExpression && parameter.IsLeft == true) { - base.Context.Result.Append(" "+ExpressionConst.Format1 + parameter.BaseParameter.Index+" "); + base.Context.Result.Append(" " + ExpressionConst.Format1 + parameter.BaseParameter.Index + " "); } } } diff --git a/Src/Asp.NetCore/SqlServerTest/src/SqlSugar/ExpressionsToSql/ResolveItems/MemberExpressionResolve.cs b/Src/Asp.NetCore/SqlServerTest/src/SqlSugar/ExpressionsToSql/ResolveItems/MemberExpressionResolve.cs index f620b8c27..6a0cbc297 100644 --- a/Src/Asp.NetCore/SqlServerTest/src/SqlSugar/ExpressionsToSql/ResolveItems/MemberExpressionResolve.cs +++ b/Src/Asp.NetCore/SqlServerTest/src/SqlSugar/ExpressionsToSql/ResolveItems/MemberExpressionResolve.cs @@ -61,8 +61,11 @@ namespace SqlSugar else { fieldName = getSingleName(parameter, expression, isLeft); + if (expression.Type == PubConst.BoolType&&baseParameter.OperatorValue.IsNullOrEmpty()) { + fieldName= "( "+fieldName+"=1 )"; + } fieldName = AppendMember(parameter, isLeft, fieldName); - } + } break; case ResolveExpressType.WhereMultiple: if (isSetTempData) diff --git a/Src/Asp.NetCore/SqlServerTest/src/SqlSugar/ExpressionsToSql/ResolveItems/MethodCallExpressionResolve.cs b/Src/Asp.NetCore/SqlServerTest/src/SqlSugar/ExpressionsToSql/ResolveItems/MethodCallExpressionResolve.cs index 38bc0f33f..b40cd8e7b 100644 --- a/Src/Asp.NetCore/SqlServerTest/src/SqlSugar/ExpressionsToSql/ResolveItems/MethodCallExpressionResolve.cs +++ b/Src/Asp.NetCore/SqlServerTest/src/SqlSugar/ExpressionsToSql/ResolveItems/MethodCallExpressionResolve.cs @@ -234,6 +234,12 @@ namespace SqlSugar return this.Context.DbMehtods.AggregateMax(model); case "AggregateCount": return this.Context.DbMehtods.AggregateCount(model); + case "MappingColumn": + var mappingColumnResult = this.Context.DbMehtods.MappingColumn(model); + var isValid= model.Args[0].IsMember&&model.Args[1].IsMember==false; + Check.Exception(!isValid, "SqlFunc.MappingColumn parameters error, The property name on the left, string value on the right"); + this.Context.Parameters.RemoveAll(it => it.ParameterName == model.Args[1].MemberName.ObjToString()); + return mappingColumnResult; default: break; } diff --git a/Src/Asp.NetCore/SqlServerTest/src/SqlSugar/ExpressionsToSql/ResolveItems/UnaryExpressionResolve.cs b/Src/Asp.NetCore/SqlServerTest/src/SqlSugar/ExpressionsToSql/ResolveItems/UnaryExpressionResolve.cs index f1244092f..39535e8ed 100644 --- a/Src/Asp.NetCore/SqlServerTest/src/SqlSugar/ExpressionsToSql/ResolveItems/UnaryExpressionResolve.cs +++ b/Src/Asp.NetCore/SqlServerTest/src/SqlSugar/ExpressionsToSql/ResolveItems/UnaryExpressionResolve.cs @@ -32,7 +32,20 @@ namespace SqlSugar { Append(parameter, nodeType); } - else if (isMember || isConst) + else if (isMember) + { + var isComparisonOperator = ExpressionTool.IsLogicOperator(baseParameter.OperatorValue)||baseParameter.OperatorValue.IsNullOrEmpty(); + var memberExpression = (base.Expression as MemberExpression); + if (memberExpression.Type== PubConst.BoolType&& isComparisonOperator) + { + Append(parameter, nodeType); + } + else + { + Result(parameter, nodeType); + } + } + else if (isConst) { Result(parameter, nodeType); } diff --git a/Src/Asp.NetCore/SqlServerTest/src/SqlSugar/Interface/IQueryable.cs b/Src/Asp.NetCore/SqlServerTest/src/SqlSugar/Interface/IQueryable.cs index 23a7c3e52..5afc40d3c 100644 --- a/Src/Asp.NetCore/SqlServerTest/src/SqlSugar/Interface/IQueryable.cs +++ b/Src/Asp.NetCore/SqlServerTest/src/SqlSugar/Interface/IQueryable.cs @@ -99,6 +99,9 @@ namespace SqlSugar new ISugarQueryable WhereIF(bool isWhere,Expression> expression); ISugarQueryable WhereIF(bool isWhere, Expression> expression); + + new ISugarQueryable Where(string whereString, object whereObj = null); + new ISugarQueryable WhereIF(bool isWhere, string whereString, object whereObj = null); #endregion #region Select @@ -125,6 +128,9 @@ namespace SqlSugar new ISugarQueryable WhereIF(bool isWhere, Expression> expression); ISugarQueryable WhereIF(bool isWhere, Expression> expression); ISugarQueryable WhereIF(bool isWhere, Expression> expression); + + new ISugarQueryable Where(string whereString, object whereObj = null); + new ISugarQueryable WhereIF(bool isWhere, string whereString, object whereObj = null); #endregion #region Select @@ -156,6 +162,9 @@ namespace SqlSugar ISugarQueryable WhereIF(bool isWhere, Expression> expression); ISugarQueryable WhereIF(bool isWhere, Expression> expression); ISugarQueryable WhereIF(bool isWhere, Expression> expression); + + new ISugarQueryable Where(string whereString, object whereObj = null); + new ISugarQueryable WhereIF(bool isWhere, string whereString, object whereObj = null); #endregion #region Select @@ -193,6 +202,9 @@ namespace SqlSugar ISugarQueryable WhereIF(bool isWhere, Expression> expression); ISugarQueryable WhereIF(bool isWhere, Expression> expression); ISugarQueryable WhereIF(bool isWhere, Expression> expression); + + new ISugarQueryable Where(string whereString, object whereObj = null); + new ISugarQueryable WhereIF(bool isWhere, string whereString, object whereObj = null); #endregion #region Select @@ -234,6 +246,9 @@ namespace SqlSugar ISugarQueryable WhereIF(bool isWhere, Expression> expression); ISugarQueryable WhereIF(bool isWhere, Expression> expression); ISugarQueryable WhereIF(bool isWhere, Expression> expression); + + new ISugarQueryable Where(string whereString, object whereObj = null); + new ISugarQueryable WhereIF(bool isWhere, string whereString, object whereObj = null); #endregion #region Select @@ -273,13 +288,16 @@ namespace SqlSugar ISugarQueryable Where(Expression> expression); ISugarQueryable Where(Expression> expression); - ISugarQueryable WhereIF(bool isWhere, Expression> expression); + new ISugarQueryable WhereIF(bool isWhere, Expression> expression); ISugarQueryable WhereIF(bool isWhere, Expression> expression); ISugarQueryable WhereIF(bool isWhere, Expression> expression); ISugarQueryable WhereIF(bool isWhere, Expression> expression); ISugarQueryable WhereIF(bool isWhere, Expression> expression); ISugarQueryable WhereIF(bool isWhere, Expression> expression); ISugarQueryable WhereIF(bool isWhere, Expression> expression); + + new ISugarQueryable Where(string whereString, object whereObj = null); + new ISugarQueryable WhereIF(bool isWhere, string whereString, object whereObj = null); #endregion #region Select @@ -331,6 +349,9 @@ namespace SqlSugar ISugarQueryable WhereIF(bool isWhere, Expression> expression); ISugarQueryable WhereIF(bool isWhere, Expression> expression); ISugarQueryable WhereIF(bool isWhere, Expression> expression); + + new ISugarQueryable Where(string whereString, object whereObj = null); + new ISugarQueryable WhereIF(bool isWhere, string whereString, object whereObj = null); #endregion #region Select