PipelineExpression.cs 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. /* Copyright 2015-present MongoDB Inc.
  2. *
  3. * Licensed under the Apache License, Version 2.0 (the "License");
  4. * you may not use this file except in compliance with the License.
  5. * You may obtain a copy of the License at
  6. *
  7. * http://www.apache.org/licenses/LICENSE-2.0
  8. *
  9. * Unless required by applicable law or agreed to in writing, software
  10. * distributed under the License is distributed on an "AS IS" BASIS,
  11. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. * See the License for the specific language governing permissions and
  13. * limitations under the License.
  14. */
  15. using System;
  16. using System.Collections.Generic;
  17. using System.Linq.Expressions;
  18. using MongoDB.Bson.Serialization;
  19. using MongoDB.Driver.Core.Misc;
  20. namespace MongoDB.Driver.Linq.Expressions
  21. {
  22. internal sealed class PipelineExpression : SerializationExpression, ISourcedExpression
  23. {
  24. private readonly Expression _source;
  25. private readonly SerializationExpression _projector;
  26. private readonly ResultOperator _resultOperator;
  27. private readonly IBsonSerializer _serializer;
  28. private readonly Type _type;
  29. public PipelineExpression(Expression source, SerializationExpression projector)
  30. : this(source, projector, null)
  31. {
  32. }
  33. public PipelineExpression(Expression source, SerializationExpression projector, ResultOperator resultOperator)
  34. {
  35. _source = Ensure.IsNotNull(source, nameof(source));
  36. _projector = Ensure.IsNotNull(projector, nameof(projector));
  37. _resultOperator = resultOperator;
  38. if (_resultOperator == null)
  39. {
  40. _serializer = SerializerHelper.CreateEnumerableSerializer(_projector.Serializer);
  41. _type = typeof(IEnumerable<>).MakeGenericType(_projector.Type);
  42. }
  43. else
  44. {
  45. _serializer = _resultOperator.Serializer;
  46. _type = _resultOperator.Type;
  47. }
  48. }
  49. public SerializationExpression Projector
  50. {
  51. get { return _projector; }
  52. }
  53. public ResultOperator ResultOperator
  54. {
  55. get { return _resultOperator; }
  56. }
  57. public override IBsonSerializer Serializer
  58. {
  59. get { return _serializer; }
  60. }
  61. public Expression Source
  62. {
  63. get { return _source; }
  64. }
  65. public override ExtensionExpressionType ExtensionType
  66. {
  67. get { return ExtensionExpressionType.Pipeline; }
  68. }
  69. public override Type Type
  70. {
  71. get { return _type; }
  72. }
  73. public override string ToString()
  74. {
  75. var result = _source.ToString();
  76. if (_resultOperator != null)
  77. {
  78. return _resultOperator.Name + "(" + result + ")";
  79. }
  80. return result;
  81. }
  82. public PipelineExpression Update(Expression source, SerializationExpression projector, ResultOperator resultOperator)
  83. {
  84. if (source != _source ||
  85. projector != _projector ||
  86. resultOperator != _resultOperator)
  87. {
  88. return new PipelineExpression(source, projector, resultOperator);
  89. }
  90. return this;
  91. }
  92. protected internal override Expression Accept(ExtensionExpressionVisitor visitor)
  93. {
  94. return visitor.VisitPipeline(this);
  95. }
  96. }
  97. }