SubAnd.cs 4.1 KB

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