You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

129 lines
6.1 KiB

2 years ago
  1. using System.Collections.Generic;
  2. using System.Linq.Expressions;
  3. namespace SqlSugar
  4. {
  5. public class UnaryExpressionResolve : BaseResolve
  6. {
  7. public UnaryExpressionResolve(ExpressionParameter parameter) : base(parameter)
  8. {
  9. var expression = base.Expression as UnaryExpression;
  10. var baseParameter = parameter.BaseParameter;
  11. switch (this.Context.ResolveType)
  12. {
  13. case ResolveExpressType.WhereSingle:
  14. case ResolveExpressType.WhereMultiple:
  15. case ResolveExpressType.FieldSingle:
  16. case ResolveExpressType.FieldMultiple:
  17. case ResolveExpressType.SelectSingle:
  18. case ResolveExpressType.SelectMultiple:
  19. case ResolveExpressType.ArraySingle:
  20. case ResolveExpressType.ArrayMultiple:
  21. case ResolveExpressType.Update:
  22. var nodeType = expression.NodeType;
  23. base.Expression = expression.Operand;
  24. var isMember = expression.Operand is MemberExpression;
  25. var isConst = expression.Operand is ConstantExpression;
  26. var offsetIsNull = (parameter.OppsiteExpression is ConstantExpression)
  27. &&(parameter.OppsiteExpression as ConstantExpression).Value==null
  28. &&ExpressionTool.IsComparisonOperator(expression.Operand);
  29. if (isMember && offsetIsNull) {
  30. Append(parameter, nodeType);
  31. }
  32. else if (baseParameter.CurrentExpression is NewArrayExpression)
  33. {
  34. Result(parameter, nodeType);
  35. }
  36. else if (base.Expression is BinaryExpression || parameter.BaseExpression is BinaryExpression || baseParameter.CommonTempData.ObjToString() == CommonTempDataType.Append.ToString())
  37. {
  38. Append(parameter, nodeType);
  39. }
  40. else if (isMember)
  41. {
  42. MemberLogic(parameter, baseParameter, nodeType);
  43. }
  44. else if (isConst)
  45. {
  46. Result(parameter, nodeType);
  47. }
  48. else
  49. {
  50. Append(parameter, nodeType);
  51. }
  52. break;
  53. default:
  54. break;
  55. }
  56. }
  57. private void MemberLogic(ExpressionParameter parameter, ExpressionParameter baseParameter, ExpressionType nodeType)
  58. {
  59. var memberExpression = (base.Expression as MemberExpression);
  60. var isLogicOperator = ExpressionTool.IsLogicOperator(baseParameter.OperatorValue) || baseParameter.OperatorValue.IsNullOrEmpty();
  61. var isHasValue = isLogicOperator && memberExpression.Member.Name == "HasValue" && memberExpression.Expression != null && memberExpression.NodeType == ExpressionType.MemberAccess;
  62. if (isHasValue)
  63. {
  64. var member = memberExpression.Expression as MemberExpression;
  65. parameter.CommonTempData = CommonTempDataType.Result;
  66. var isConst = member.Expression != null && member.Expression is ConstantExpression;
  67. if (isConst)
  68. {
  69. var paramterValue = ExpressionTool.DynamicInvoke(member);
  70. var paramterName= base.AppendParameter(paramterValue);
  71. var result = this.Context.DbMehtods.HasValue(new MethodCallExpressionModel
  72. {
  73. Args = new List<MethodCallExpressionArgs> {
  74. new MethodCallExpressionArgs { IsMember=false, MemberName=paramterName, MemberValue=paramterValue } }
  75. });
  76. this.Context.Result.Append(result);
  77. }
  78. else
  79. {
  80. this.Expression = isConst ? member.Expression : member;
  81. this.Start();
  82. var methodParamter = isConst ? new MethodCallExpressionArgs { IsMember = false } : new MethodCallExpressionArgs { IsMember = true, MemberName = parameter.CommonTempData, MemberValue = null };
  83. var result = this.Context.DbMehtods.HasValue(new MethodCallExpressionModel
  84. {
  85. Args = new List<MethodCallExpressionArgs> {
  86. methodParamter
  87. }
  88. });
  89. this.Context.Result.Append(result);
  90. parameter.CommonTempData = null;
  91. }
  92. }
  93. else if (memberExpression.Type == UtilConstants.BoolType && isLogicOperator)
  94. {
  95. Append(parameter, nodeType);
  96. }
  97. else
  98. {
  99. Result(parameter, nodeType);
  100. }
  101. }
  102. private void Result(ExpressionParameter parameter, ExpressionType nodeType)
  103. {
  104. BaseParameter.ChildExpression = base.Expression;
  105. parameter.CommonTempData = CommonTempDataType.Result;
  106. if (nodeType == ExpressionType.Not)
  107. AppendNot(parameter.CommonTempData);
  108. base.Start();
  109. parameter.BaseParameter.CommonTempData = parameter.CommonTempData;
  110. parameter.BaseParameter.ChildExpression = base.Expression;
  111. parameter.CommonTempData = null;
  112. }
  113. private void Append(ExpressionParameter parameter, ExpressionType nodeType)
  114. {
  115. BaseParameter.ChildExpression = base.Expression;
  116. this.IsLeft = parameter.IsLeft;
  117. parameter.CommonTempData = CommonTempDataType.Append;
  118. if (nodeType == ExpressionType.Not)
  119. AppendNot(parameter.CommonTempData);
  120. base.Start();
  121. parameter.BaseParameter.CommonTempData = parameter.CommonTempData;
  122. parameter.BaseParameter.ChildExpression = base.Expression;
  123. parameter.CommonTempData = null;
  124. }
  125. }
  126. }