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.

65 lines
3.1 KiB

2 years ago
  1. using System.Linq.Expressions;
  2. namespace SqlSugar
  3. {
  4. public class ConstantExpressionResolve : BaseResolve
  5. {
  6. public ConstantExpressionResolve(ExpressionParameter parameter) : base(parameter)
  7. {
  8. var expression = base.Expression as ConstantExpression;
  9. var isLeft = parameter.IsLeft;
  10. var value = ExpressionTool.GetValue(expression.Value);
  11. var baseParameter = parameter.BaseParameter;
  12. baseParameter.ChildExpression = expression;
  13. var isSetTempData = baseParameter.CommonTempData.HasValue() && baseParameter.CommonTempData.Equals(CommonTempDataType.Result);
  14. switch (parameter.Context.ResolveType)
  15. {
  16. case ResolveExpressType.SelectSingle:
  17. case ResolveExpressType.Update:
  18. case ResolveExpressType.SelectMultiple:
  19. baseParameter.CommonTempData = value;
  20. break;
  21. case ResolveExpressType.WhereSingle:
  22. case ResolveExpressType.WhereMultiple:
  23. if (isSetTempData)
  24. {
  25. baseParameter.CommonTempData = value;
  26. }
  27. else
  28. {
  29. var parentIsBinary = parameter.BaseParameter.CurrentExpression is BinaryExpression;
  30. var parentIsRoot = parameter.BaseParameter.CurrentExpression is LambdaExpression;
  31. var isBool = value != null && value.GetType() == UtilConstants.BoolType;
  32. if (parentIsRoot && isBool)
  33. {
  34. this.Context.Result.Append(value.ObjToBool() ? this.Context.DbMehtods.True() : this.Context.DbMehtods.False());
  35. break;
  36. }
  37. if (parentIsBinary && isBool)
  38. {
  39. var isLogicOperator =
  40. parameter.BaseExpression.NodeType == ExpressionType.And ||
  41. parameter.BaseExpression.NodeType == ExpressionType.AndAlso ||
  42. parameter.BaseExpression.NodeType == ExpressionType.Or ||
  43. parameter.BaseExpression.NodeType == ExpressionType.OrElse;
  44. if (isLogicOperator)
  45. {
  46. AppendMember(parameter, isLeft, (value.ObjToBool() ? this.Context.DbMehtods.True() : this.Context.DbMehtods.False()));
  47. break;
  48. }
  49. }
  50. if (value == null && parentIsBinary)
  51. {
  52. parameter.BaseParameter.ValueIsNull = true;
  53. value = this.Context.DbMehtods.Null();
  54. }
  55. AppendValue(parameter, isLeft, value);
  56. }
  57. break;
  58. case ResolveExpressType.FieldSingle:
  59. case ResolveExpressType.FieldMultiple:
  60. default:
  61. break;
  62. }
  63. }
  64. }
  65. }