MongoQueryableImpl.cs 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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;
  18. using System.Linq.Expressions;
  19. using System.Threading;
  20. using System.Threading.Tasks;
  21. using MongoDB.Driver.Core.Misc;
  22. namespace MongoDB.Driver.Linq
  23. {
  24. internal sealed class MongoQueryableImpl<TInput, TOutput> : IOrderedMongoQueryable<TOutput>
  25. {
  26. private readonly IMongoQueryProvider _queryProvider;
  27. private readonly Expression _expression;
  28. public MongoQueryableImpl(IMongoQueryProvider queryProvider)
  29. {
  30. _queryProvider = Ensure.IsNotNull(queryProvider, nameof(queryProvider));
  31. _expression = Expression.Constant(this, typeof(IMongoQueryable<TOutput>));
  32. }
  33. public MongoQueryableImpl(IMongoQueryProvider queryProvider, Expression expression)
  34. {
  35. _queryProvider = Ensure.IsNotNull(queryProvider, nameof(queryProvider));
  36. _expression = Ensure.IsNotNull(expression, nameof(expression));
  37. }
  38. public Type ElementType
  39. {
  40. get { return typeof(TOutput); }
  41. }
  42. public Expression Expression
  43. {
  44. get { return _expression; }
  45. }
  46. public IQueryProvider Provider
  47. {
  48. get { return _queryProvider; }
  49. }
  50. public IEnumerator<TOutput> GetEnumerator()
  51. {
  52. var results = (IEnumerable<TOutput>)_queryProvider.Execute(_expression);
  53. return results.GetEnumerator();
  54. }
  55. System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
  56. {
  57. return GetEnumerator();
  58. }
  59. public QueryableExecutionModel GetExecutionModel()
  60. {
  61. return _queryProvider.GetExecutionModel(_expression);
  62. }
  63. public IAsyncCursor<TOutput> ToCursor(CancellationToken cancellationToken)
  64. {
  65. var model = _queryProvider.GetExecutionModel(_expression);
  66. var mongoQueryProvider = (MongoQueryProviderImpl<TInput>)_queryProvider;
  67. return (IAsyncCursor<TOutput>)mongoQueryProvider.ExecuteModel(model);
  68. }
  69. public Task<IAsyncCursor<TOutput>> ToCursorAsync(CancellationToken cancellationToken)
  70. {
  71. return _queryProvider.ExecuteAsync<IAsyncCursor<TOutput>>(_expression, cancellationToken);
  72. }
  73. public override string ToString()
  74. {
  75. var pipeline = GetExecutionModel();
  76. return pipeline.ToString();
  77. }
  78. }
  79. }