SubWhere.cs 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Linq.Expressions;
  5. using System.Text;
  6. using System.Text.RegularExpressions;
  7. namespace SqlSugar
  8. {
  9. public class SubWhere : ISubOperation
  10. {
  11. public bool HasWhere
  12. {
  13. get; set;
  14. }
  15. public string Name
  16. {
  17. get { return "Where"; }
  18. }
  19. public Expression Expression
  20. {
  21. get; set;
  22. }
  23. public int Sort
  24. {
  25. get
  26. {
  27. return 400;
  28. }
  29. }
  30. public ExpressionContext Context
  31. {
  32. get; set;
  33. }
  34. public string GetValue(Expression expression)
  35. {
  36. var exp = expression as MethodCallExpression;
  37. if (Regex.Matches( expression.ToString(), "Subqueryable").Count >= 2)
  38. {
  39. new SubSelect() { Context = this.Context }.SetShortName(exp, "+");
  40. }
  41. var argExp = exp.Arguments[0];
  42. if (ExpressionTool.GetMethodName(argExp) == "ToExpression")
  43. {
  44. argExp= ExpressionTool.DynamicInvoke(argExp) as Expression;
  45. }
  46. var copyContext = this.Context;
  47. var pars=ExpressionTool.GetParameters(expression).Distinct();
  48. if (this.Context.JoinIndex > 0|| pars.Count()>1)
  49. {
  50. copyContext = this.Context.GetCopyContextWithMapping();
  51. copyContext.IsSingle = false;
  52. }
  53. var result = "WHERE " + SubTools.GetMethodValue(copyContext, argExp, ResolveExpressType.WhereMultiple);
  54. if (argExp.Type == typeof(List<IConditionalModel>))
  55. {
  56. var p= this.Context.Parameters.Last();
  57. this.Context.Parameters.Remove(p);
  58. var cols = p.Value as List<IConditionalModel>;
  59. var sqlObj=this.Context.SugarContext.QueryBuilder.Builder.ConditionalModelToSql(cols,this.Context.ParameterIndex*100);
  60. this.Context.ParameterIndex= this.Context.ParameterIndex+this.Context.ParameterIndex * 100;
  61. result = "WHERE " + sqlObj.Key;
  62. this.Context.Parameters.AddRange(sqlObj.Value);
  63. return result;
  64. }
  65. if (this.Context.JoinIndex > 0 ||pars.Count() > 1)
  66. {
  67. this.Context.Parameters.AddRange(copyContext.Parameters);
  68. this.Context.Index = copyContext.Index;
  69. this.Context.ParameterIndex = copyContext.ParameterIndex;
  70. }
  71. var regex = @"^WHERE (\@Const\d+) $";
  72. if (this.Context is OracleExpressionContext)
  73. {
  74. regex = @"^WHERE (\:Const\d+) $";
  75. }
  76. if (this.Context is DmExpressionContext)
  77. {
  78. regex = @"^WHERE (\:Const\d+) $";
  79. }
  80. if (Regex.IsMatch(result, regex))
  81. {
  82. var value = GetValue(result, regex);
  83. if (value is Expression)
  84. {
  85. var p = this.Context.Parameters.First(it => it.ParameterName == Regex.Match(result, regex).Groups[1].Value);
  86. result = "WHERE " + SubTools.GetMethodValue(Context, value as Expression, ResolveExpressType.WhereMultiple);
  87. argExp = value as Expression;
  88. this.Context.Parameters.Remove(p);
  89. }
  90. else
  91. {
  92. result = "WHERE " + value;
  93. return result;
  94. }
  95. }
  96. var selfParameterName = Context.GetTranslationColumnName((argExp as LambdaExpression).Parameters.First().Name) + UtilConstants.Dot;
  97. if (this.Context.JoinIndex == 0&&result.Contains(" FROM "))
  98. {
  99. this.Context.CurrentShortName= selfParameterName.ObjToString().TrimEnd('.');
  100. }
  101. else if (this.Context.JoinIndex == 0&& this.Context.CurrentShortName!= selfParameterName.TrimEnd('.'))
  102. result = result.Replace(selfParameterName, SubTools.GetSubReplace(this.Context));
  103. if (!string.IsNullOrEmpty(selfParameterName) && this.Context.IsSingle&& this.Context.JoinIndex == 0)
  104. {
  105. this.Context.CurrentShortName = selfParameterName.TrimEnd('.');
  106. }
  107. return result;
  108. }
  109. private object GetValue(string result, string regex)
  110. {
  111. return this.Context.Parameters.First(it => it.ParameterName == Regex.Match(result, regex).Groups[1].Value).Value;
  112. }
  113. }
  114. }