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.

50 lines
2.0 KiB

2 years ago
  1. using System.Collections.Generic;
  2. using System.Linq;
  3. using System.Linq.Expressions;
  4. namespace SqlSugar
  5. {
  6. public class CaseWhenResolve
  7. {
  8. List<MethodCallExpression> allMethods = new List<MethodCallExpression>();
  9. private ExpressionContext context = null;
  10. public CaseWhenResolve(MethodCallExpression expression, ExpressionContext context, Expression oppsiteExpression)
  11. {
  12. this.context = context;
  13. var currentExpression = expression;
  14. allMethods.Add(currentExpression);
  15. if (context.IsSingle && oppsiteExpression != null&& oppsiteExpression is MemberExpression)
  16. {
  17. var childExpression = (oppsiteExpression as MemberExpression).Expression;
  18. this.context.SingleTableNameSubqueryShortName = (childExpression as ParameterExpression).Name;
  19. }
  20. else if (context.IsSingle)
  21. {
  22. this.context.SingleTableNameSubqueryShortName = (context.Expression as LambdaExpression).Parameters.First().Name;
  23. }
  24. while (currentExpression != null)
  25. {
  26. var addItem = currentExpression.Object as MethodCallExpression;
  27. if (addItem != null)
  28. allMethods.Add(addItem);
  29. currentExpression = addItem;
  30. }
  31. }
  32. public string GetSql()
  33. {
  34. allMethods.Reverse();
  35. var sqls = new List<KeyValuePair<string, string>>();
  36. foreach (var methodExp in allMethods)
  37. {
  38. var isFirst = allMethods.First() == methodExp;
  39. var isLast= allMethods.Last() == methodExp;
  40. var sql= SubTools.GetMethodValue(this.context, methodExp.Arguments[0],this.context.IsSingle?ResolveExpressType.WhereSingle:ResolveExpressType.WhereMultiple);
  41. sqls.Add(new KeyValuePair<string, string>(methodExp.Method.Name, sql));
  42. }
  43. var result= this.context.DbMehtods.CaseWhen(sqls);
  44. return result;
  45. }
  46. }
  47. }