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.

147 lines
6.4 KiB

2 years ago
  1. using System;
  2. using System.Linq;
  3. using System.Linq.Expressions;
  4. using System.Text.RegularExpressions;
  5. namespace SqlSugar
  6. {
  7. public class MemberInitExpressionResolve : BaseResolve
  8. {
  9. public MemberInitExpressionResolve(ExpressionParameter parameter) : base(parameter)
  10. {
  11. var expression = base.Expression as MemberInitExpression;
  12. switch (parameter.Context.ResolveType)
  13. {
  14. case ResolveExpressType.WhereSingle:
  15. break;
  16. case ResolveExpressType.WhereMultiple:
  17. break;
  18. case ResolveExpressType.SelectSingle:
  19. Select(expression, parameter, true);
  20. break;
  21. case ResolveExpressType.SelectMultiple:
  22. Select(expression, parameter, false);
  23. break;
  24. case ResolveExpressType.FieldSingle:
  25. break;
  26. case ResolveExpressType.FieldMultiple:
  27. break;
  28. case ResolveExpressType.Update:
  29. Update(expression, parameter);
  30. break;
  31. default:
  32. break;
  33. }
  34. }
  35. private void Update(MemberInitExpression expression, ExpressionParameter parameter)
  36. {
  37. var i = 0;
  38. foreach (MemberBinding binding in expression.Bindings)
  39. {
  40. ++i;
  41. if (binding.BindingType != MemberBindingType.Assignment)
  42. {
  43. throw new NotSupportedException();
  44. }
  45. var memberAssignment = (MemberAssignment)binding;
  46. var type = memberAssignment.Member.ReflectedType;
  47. var memberName = this.Context.GetDbColumnName(type.Name, memberAssignment.Member.Name);
  48. var item = memberAssignment.Expression;
  49. if ((item is MemberExpression) && ((MemberExpression)item).Expression == null)
  50. {
  51. var paramterValue = ExpressionTool.DynamicInvoke(item);
  52. var parameterName = AppendParameter(paramterValue);
  53. this.Context.Result.Append(base.Context.GetEqString(memberName, parameterName));
  54. }
  55. else if (IsMethod(item))
  56. {
  57. if (item is UnaryExpression)
  58. item = (item as UnaryExpression).Operand;
  59. MethodCall(parameter, memberName, item);
  60. }
  61. else if (IsConst(item))
  62. {
  63. base.Expression = item;
  64. base.Start();
  65. var parameterName = this.Context.SqlParameterKeyWord + ExpressionConst.Const + this.Context.ParameterIndex;
  66. parameter.Context.Result.Append(base.Context.GetEqString(memberName, parameterName));
  67. this.Context.Parameters.Add(new SugarParameter(parameterName, parameter.CommonTempData));
  68. this.Context.ParameterIndex++;
  69. }
  70. else if (item is MemberExpression)
  71. {
  72. if (!base.Context.Result.IsLockCurrentParameter)
  73. {
  74. base.Context.Result.CurrentParameter = parameter;
  75. base.Context.Result.IsLockCurrentParameter = true;
  76. parameter.IsAppendTempDate();
  77. base.Expression = item;
  78. base.Start();
  79. parameter.IsAppendResult();
  80. parameter.Context.Result.Append(base.Context.GetEqString(memberName, parameter.CommonTempData.ObjToString()));
  81. base.Context.Result.CurrentParameter = null;
  82. }
  83. }
  84. else if (item is BinaryExpression)
  85. {
  86. var result = GetNewExpressionValue(item);
  87. this.Context.Result.Append(base.Context.GetEqString(memberName, result));
  88. }
  89. }
  90. }
  91. private static bool IsConst(Expression item)
  92. {
  93. return item is UnaryExpression || item.NodeType == ExpressionType.Constant || (item is MemberExpression) && ((MemberExpression)item).Expression.NodeType == ExpressionType.Constant;
  94. }
  95. private static bool IsMethod(Expression item)
  96. {
  97. return item is MethodCallExpression || (item is UnaryExpression && (item as UnaryExpression).Operand is MethodCallExpression);
  98. }
  99. private void MethodCall(ExpressionParameter parameter, string memberName, Expression item)
  100. {
  101. if (IsSubMethod(item as MethodCallExpression))
  102. {
  103. UtilMethods.GetOldValue(parameter.CommonTempData, () =>
  104. {
  105. parameter.CommonTempData = CommonTempDataType.Result;
  106. base.Expression = item;
  107. base.Start();
  108. var subSql = base.Context.GetEqString(memberName, parameter.CommonTempData.ObjToString());
  109. if (ResolveExpressType.Update == this.Context.ResolveType) {
  110. subSql = Regex.Replace(subSql,@" \[\w+?\]\.",this.Context.GetTranslationTableName(parameter.CurrentExpression.Type.Name,true) +".");
  111. }
  112. parameter.Context.Result.Append(subSql);
  113. });
  114. }
  115. else
  116. {
  117. base.Expression = item;
  118. base.Start();
  119. parameter.Context.Result.Append(base.Context.GetEqString(memberName, parameter.CommonTempData.ObjToString()));
  120. }
  121. }
  122. private void Select(MemberInitExpression expression, ExpressionParameter parameter, bool isSingle)
  123. {
  124. foreach (MemberBinding binding in expression.Bindings)
  125. {
  126. if (binding.BindingType != MemberBindingType.Assignment)
  127. {
  128. throw new NotSupportedException();
  129. }
  130. var memberAssignment = (MemberAssignment)binding;
  131. var memberName = memberAssignment.Member.Name;
  132. var item = memberAssignment.Expression;
  133. ResolveNewExpressions(parameter, item, memberName);
  134. }
  135. }
  136. private bool IsSubMethod(MethodCallExpression express)
  137. {
  138. return SubTools.SubItemsConst.Any(it =>express.Object != null && express.Object.Type.Name == "Subqueryable`1");
  139. }
  140. }
  141. }