-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
Copy pathLinqProviderAdapterV2.cs
172 lines (155 loc) · 8.61 KB
/
LinqProviderAdapterV2.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
/* Copyright 2010-present MongoDB Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
using System;
using System.Linq;
using System.Linq.Expressions;
using MongoDB.Bson;
using MongoDB.Bson.Serialization;
using MongoDB.Driver.Linq.Linq2Implementation.Expressions;
using MongoDB.Driver.Linq.Linq2Implementation.Processors;
using MongoDB.Driver.Linq.Linq2Implementation.Translators;
using MongoDB.Driver.Linq.Linq3Implementation.Translators;
namespace MongoDB.Driver.Linq.Linq2Implementation
{
internal sealed class LinqProviderAdapterV2 : LinqProviderAdapter
{
internal override IMongoQueryable<TDocument> AsQueryable<TDocument>(
IMongoCollection<TDocument> collection,
IClientSessionHandle session,
AggregateOptions options)
{
var provider = new MongoQueryProviderImpl<TDocument>(collection, session, options);
return new MongoQueryableImpl<TDocument, TDocument>(provider);
}
internal override IMongoQueryable<NoPipelineInput> AsQueryable(
IMongoDatabase database,
IClientSessionHandle session,
AggregateOptions options)
{
throw new InvalidOperationException("LINQ2 does not support AsQueryable against a database.");
}
public override string ToString() => "V2";
internal override BsonValue TranslateExpressionToAggregateExpression<TSource, TResult>(
Expression<Func<TSource, TResult>> expression,
IBsonSerializer<TSource> sourceSerializer,
IBsonSerializerRegistry serializerRegistry,
ExpressionTranslationOptions translationOptions,
TranslationContextData contextData = null)
{
if (contextData != null)
{
throw new InvalidOperationException("The LINQ2 provider does not support context data.");
}
return AggregateExpressionTranslator.Translate(expression, sourceSerializer, serializerRegistry, translationOptions);
}
internal override RenderedProjectionDefinition<TOutput> TranslateExpressionToBucketOutputProjection<TInput, TValue, TOutput>(
Expression<Func<TInput, TValue>> valueExpression,
Expression<Func<IGrouping<TValue, TInput>, TOutput>> outputExpression,
IBsonSerializer<TInput> documentSerializer,
IBsonSerializerRegistry serializerRegistry,
ExpressionTranslationOptions translationOptions)
{
var renderedOutput = AggregateGroupTranslator.Translate<TValue, TInput, TOutput>(valueExpression, outputExpression, documentSerializer, serializerRegistry, translationOptions);
var document = renderedOutput.Document;
document.Remove("_id");
return new RenderedProjectionDefinition<TOutput>(document, renderedOutput.ProjectionSerializer);
}
internal override RenderedFieldDefinition TranslateExpressionToField<TDocument>(
LambdaExpression expression,
IBsonSerializer<TDocument> documentSerializer,
IBsonSerializerRegistry serializerRegistry)
{
var bindingContext = new PipelineBindingContext(serializerRegistry);
var lambda = ExpressionHelper.GetLambda(PartialEvaluator.Evaluate(expression));
var parameterExpression = new DocumentExpression(documentSerializer);
bindingContext.AddExpressionMapping(lambda.Parameters[0], parameterExpression);
var bound = bindingContext.Bind(lambda.Body);
bound = FieldExpressionFlattener.FlattenFields(bound);
IFieldExpression field;
if (!ExpressionHelper.TryGetExpression(bound, out field))
{
var message = string.Format("Unable to determine the serialization information for {0}.", expression);
throw new InvalidOperationException(message);
}
return new RenderedFieldDefinition(field.FieldName, field.Serializer);
}
internal override RenderedFieldDefinition<TField> TranslateExpressionToField<TDocument, TField>(
Expression<Func<TDocument, TField>> expression,
IBsonSerializer<TDocument> documentSerializer,
IBsonSerializerRegistry serializerRegistry,
bool allowScalarValueForArrayField)
{
var lambda = (LambdaExpression)PartialEvaluator.Evaluate(expression);
var bindingContext = new PipelineBindingContext(serializerRegistry);
var parameterExpression = new DocumentExpression(documentSerializer);
bindingContext.AddExpressionMapping(lambda.Parameters[0], parameterExpression);
var bound = bindingContext.Bind(lambda.Body);
bound = FieldExpressionFlattener.FlattenFields(bound);
IFieldExpression field;
if (!ExpressionHelper.TryGetExpression(bound, out field))
{
var message = string.Format("Unable to determine the serialization information for {0}.", expression);
throw new InvalidOperationException(message);
}
var underlyingSerializer = field.Serializer;
var fieldSerializer = underlyingSerializer as IBsonSerializer<TField>;
var valueSerializer = (IBsonSerializer<TField>)FieldValueSerializerHelper.GetSerializerForValueType(underlyingSerializer, serializerRegistry, typeof(TField), allowScalarValueForArrayField, LinqProvider.V2);
return new RenderedFieldDefinition<TField>(field.FieldName, fieldSerializer, valueSerializer, underlyingSerializer);
}
internal override BsonDocument TranslateExpressionToFilter<TDocument>(
Expression<Func<TDocument, bool>> expression,
IBsonSerializer<TDocument> documentSerializer,
IBsonSerializerRegistry serializerRegistry)
{
return PredicateTranslator.Translate<TDocument>(expression, documentSerializer, serializerRegistry);
}
internal override RenderedProjectionDefinition<TProjection> TranslateExpressionToFindProjection<TSource, TProjection>(
Expression<Func<TSource, TProjection>> expression,
IBsonSerializer<TSource> sourceSerializer,
IBsonSerializerRegistry serializerRegistry)
{
return FindProjectionTranslator.Translate<TSource, TProjection>(expression, sourceSerializer, serializerRegistry);
}
internal override RenderedProjectionDefinition<TOutput> TranslateExpressionToGroupProjection<TInput, TKey, TOutput>(
Expression<Func<TInput, TKey>> idExpression,
Expression<Func<IGrouping<TKey, TInput>, TOutput>> groupExpression,
IBsonSerializer<TInput> documentSerializer,
IBsonSerializerRegistry serializerRegistry,
ExpressionTranslationOptions translationOptions)
{
return AggregateGroupTranslator.Translate<TKey, TInput, TOutput>(idExpression, groupExpression, documentSerializer, serializerRegistry, translationOptions);
}
internal override RenderedProjectionDefinition<TOutput> TranslateExpressionToProjection<TInput, TOutput>(
Expression<Func<TInput, TOutput>> expression,
IBsonSerializer<TInput> inputSerializer,
IBsonSerializerRegistry serializerRegistry,
ExpressionTranslationOptions translationOptions)
{
if (expression.Parameters.Count == 1 && expression.Body == expression.Parameters[0])
{
// handle x => x as a special case
return new RenderedProjectionDefinition<TOutput>(null, (IBsonSerializer<TOutput>)inputSerializer);
}
return AggregateProjectTranslator.Translate<TInput, TOutput>(expression, inputSerializer, serializerRegistry, translationOptions);
}
internal override BsonDocument TranslateExpressionToSetStage<TDocument, TFields>(
Expression<Func<TDocument, TFields>> expression,
IBsonSerializer<TDocument> documentSerializer,
IBsonSerializerRegistry serializerRegistry)
{
throw new NotSupportedException("Set with an Expression is only supported when using LINQ3.");
}
}
}