MongoQueryProviderImpl.cs 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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.Linq;
  17. using System.Linq.Expressions;
  18. using System.Reflection;
  19. using System.Threading;
  20. using System.Threading.Tasks;
  21. using MongoDB.Bson.Serialization;
  22. using MongoDB.Driver.Core.Misc;
  23. using MongoDB.Driver.Linq.Processors;
  24. using MongoDB.Driver.Linq.Processors.Pipeline;
  25. using MongoDB.Driver.Linq.Translators;
  26. using MongoDB.Driver.Support;
  27. namespace MongoDB.Driver.Linq
  28. {
  29. internal sealed class MongoQueryProviderImpl<TDocument> : IMongoQueryProvider
  30. {
  31. private readonly IMongoCollection<TDocument> _collection;
  32. private readonly AggregateOptions _options;
  33. public MongoQueryProviderImpl(IMongoCollection<TDocument> collection, AggregateOptions options)
  34. {
  35. _collection = Ensure.IsNotNull(collection, nameof(collection));
  36. _options = Ensure.IsNotNull(options, nameof(options));
  37. }
  38. public CollectionNamespace CollectionNamespace => _collection.CollectionNamespace;
  39. public IBsonSerializer CollectionDocumentSerializer => _collection.DocumentSerializer;
  40. public IQueryable<TElement> CreateQuery<TElement>(Expression expression)
  41. {
  42. return new MongoQueryableImpl<TDocument, TElement>(this, expression);
  43. }
  44. public IQueryable CreateQuery(Expression expression)
  45. {
  46. Ensure.IsNotNull(expression, nameof(expression));
  47. var elementType = expression.Type.GetSequenceElementType();
  48. try
  49. {
  50. return (IQueryable)Activator.CreateInstance(
  51. typeof(MongoQueryableImpl<,>).MakeGenericType(typeof(TDocument), elementType),
  52. new object[] { this, expression });
  53. }
  54. catch (TargetInvocationException tie)
  55. {
  56. throw tie.InnerException;
  57. }
  58. }
  59. public TResult Execute<TResult>(Expression expression)
  60. {
  61. var result = Execute(expression);
  62. return (TResult)result;
  63. }
  64. public object Execute(Expression expression)
  65. {
  66. var executionPlan = ExecutionPlanBuilder.BuildPlan(
  67. Expression.Constant(this),
  68. Translate(expression));
  69. var lambda = Expression.Lambda(executionPlan);
  70. try
  71. {
  72. return lambda.Compile().DynamicInvoke(null);
  73. }
  74. catch (TargetInvocationException tie)
  75. {
  76. throw tie.InnerException;
  77. }
  78. }
  79. public Task<TResult> ExecuteAsync<TResult>(Expression expression, CancellationToken cancellationToken = default(CancellationToken))
  80. {
  81. var executionPlan = ExecutionPlanBuilder.BuildAsyncPlan(
  82. Expression.Constant(this),
  83. Translate(expression),
  84. Expression.Constant(cancellationToken));
  85. var lambda = Expression.Lambda(executionPlan);
  86. return (Task<TResult>)lambda.Compile().DynamicInvoke(null);
  87. }
  88. public QueryableExecutionModel GetExecutionModel(Expression expression)
  89. {
  90. return Translate(expression).Model;
  91. }
  92. internal object ExecuteModel(QueryableExecutionModel model)
  93. {
  94. return model.Execute(_collection, _options);
  95. }
  96. private Task ExecuteModelAsync(QueryableExecutionModel model, CancellationToken cancellationToken)
  97. {
  98. return model.ExecuteAsync(_collection, _options, cancellationToken);
  99. }
  100. private Expression Prepare(Expression expression)
  101. {
  102. expression = PartialEvaluator.Evaluate(expression);
  103. expression = Transformer.Transform(expression);
  104. expression = PipelineBinder.Bind(expression, _collection.Settings.SerializerRegistry);
  105. return expression;
  106. }
  107. private QueryableTranslation Translate(Expression expression)
  108. {
  109. var pipelineExpression = Prepare(expression);
  110. return QueryableTranslator.Translate(pipelineExpression, _collection.Settings.SerializerRegistry, _options.TranslationOptions);
  111. }
  112. }
  113. }