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.

57 lines
2.7 KiB

2 years ago
  1. using System.Linq.Expressions;
  2. namespace SqlSugar
  3. {
  4. public class BinaryExpressionResolve : BaseResolve
  5. {
  6. public BinaryExpressionResolve(ExpressionParameter parameter) : base(parameter)
  7. {
  8. var expression = this.Expression as BinaryExpression;
  9. var operatorValue = parameter.OperatorValue = ExpressionTool.GetOperator(expression.NodeType);
  10. var isEqual = expression.NodeType == ExpressionType.Equal;
  11. var isComparisonOperator = ExpressionTool.IsComparisonOperator(expression);
  12. base.ExactExpression = expression;
  13. var leftExpression = expression.Left;
  14. var rightExpression = expression.Right;
  15. var leftIsBinary = leftExpression is BinaryExpression;
  16. var rightBinary = rightExpression is BinaryExpression;
  17. var lbrs = leftIsBinary && !rightBinary;
  18. var lsrb = !leftIsBinary && rightBinary;
  19. var lbrb = rightBinary && leftIsBinary;
  20. var lsbs = !leftIsBinary && !rightBinary;
  21. var isAppend = !base.Context.Result.Contains(ExpressionConst.FormatSymbol);
  22. if (isAppend)
  23. {
  24. base.Context.Result.Append(ExpressionConst.LeftParenthesis);
  25. base.Context.Result.Append(ExpressionConst.FormatSymbol);
  26. }
  27. else
  28. {
  29. base.Context.Result.Replace(ExpressionConst.FormatSymbol, ExpressionConst.LeftParenthesis + ExpressionConst.FormatSymbol);
  30. }
  31. parameter.LeftExpression = leftExpression;
  32. parameter.RightExpression = rightExpression;
  33. base.Expression = leftExpression;
  34. base.IsLeft = true;
  35. base.Start();
  36. base.IsLeft = false;
  37. base.Expression = rightExpression;
  38. base.Start();
  39. base.IsLeft = null;
  40. if (lsbs && parameter.ValueIsNull)
  41. {
  42. base.Context.Result.Replace(ExpressionConst.ExpressionReplace + parameter.Index, isEqual ? "IS" : "IS NOT");
  43. base.Context.Result.Replace(ExpressionConst.ExpressionReplace + (parameter.Index+1), isEqual ? "IS" : "IS NOT");
  44. }
  45. else
  46. {
  47. base.Context.Result.Replace(ExpressionConst.ExpressionReplace + parameter.Index, operatorValue);
  48. base.Context.Result.Replace(ExpressionConst.ExpressionReplace + (parameter.Index + 1), operatorValue);
  49. }
  50. base.Context.Result.Append(ExpressionConst.RightParenthesis);
  51. if (parameter.BaseExpression is BinaryExpression && parameter.IsLeft == true)
  52. {
  53. base.Context.Result.Append(" " + ExpressionConst.ExpressionReplace + parameter.BaseParameter.Index + " ");
  54. }
  55. }
  56. }
  57. }