public static RenderedProjectionDefinition <TResult> TranslateGroup <TKey, TDocument, TResult>(Expression <Func <TDocument, TKey> > idProjector, Expression <Func <IGrouping <TKey, TDocument>, TResult> > groupProjector, IBsonSerializer <TDocument> parameterSerializer, IBsonSerializerRegistry serializerRegistry) { var keyBinder = new SerializationInfoBinder(serializerRegistry); var boundKeyExpression = BindSerializationInfo(keyBinder, idProjector, parameterSerializer); if (!(boundKeyExpression is ISerializationExpression)) { var keySerializer = SerializerBuilder.Build(boundKeyExpression, serializerRegistry); boundKeyExpression = new SerializationExpression( boundKeyExpression, new BsonSerializationInfo(null, keySerializer, typeof(TKey))); } var idExpression = new IdExpression(boundKeyExpression, ((ISerializationExpression)boundKeyExpression).SerializationInfo); var groupBinder = new GroupSerializationInfoBinder(BsonSerializer.SerializerRegistry); groupBinder.RegisterMemberReplacement(typeof(IGrouping <TKey, TDocument>).GetProperty("Key"), idExpression); var groupSerializer = new ArraySerializer <TDocument>(parameterSerializer); var boundGroupExpression = BindSerializationInfo(groupBinder, groupProjector, groupSerializer); var projectionSerializer = (IBsonSerializer <TResult>)SerializerBuilder.Build(boundGroupExpression, serializerRegistry); var projection = ProjectionBuilder.Build(boundGroupExpression).AsBsonDocument; // must have an "_id" in a group document if (!projection.Contains("_id")) { var idProjection = ProjectionBuilder.Build(boundKeyExpression); projection.InsertAt(0, new BsonElement("_id", idProjection)); } return(new RenderedProjectionDefinition <TResult>(projection, projectionSerializer)); }
public static ProjectionInfo <TResult> TranslateGroup <TKey, TDocument, TResult>(Expression <Func <TDocument, TKey> > idProjector, Expression <Func <IGrouping <TKey, TDocument>, TResult> > groupProjector, IBsonSerializer <TDocument> parameterSerializer, IBsonSerializerRegistry serializerRegistry) { if (groupProjector.Body.NodeType != ExpressionType.New) { throw new NotSupportedException("Must use an anonymous type for constructing $group pipeline operators."); } var keyBinder = new SerializationInfoBinder(serializerRegistry); var boundKeyExpression = BindSerializationInfo(keyBinder, idProjector, parameterSerializer); var groupBinder = new GroupSerializationInfoBinder(BsonSerializer.SerializerRegistry); groupBinder.RegisterMemberReplacement(typeof(IGrouping <TKey, TDocument>).GetProperty("Key"), boundKeyExpression); var groupSerializer = new ArraySerializer <TDocument>(parameterSerializer); var boundGroupExpression = BindSerializationInfo(groupBinder, groupProjector, groupSerializer); var projectionSerializer = (IBsonSerializer <TResult>)SerializerBuilder.Build(boundGroupExpression, serializerRegistry); var projection = ProjectionBuilder.Build(boundGroupExpression).AsBsonDocument; // must have an "_id" in a group document if (!projection.Contains("_id")) { var idProjection = ProjectionBuilder.Build(boundKeyExpression); projection.InsertAt(0, new BsonElement("_id", idProjection)); } return(new ProjectionInfo <TResult>(projection, projectionSerializer)); }
static IChildProjection EagerChild <K, V>(Expression projector, Scope scope) { return(new EagerChildProjection <K, V> { ProjectorExpression = ProjectionBuilder.Build <KeyValuePair <K, V> >(projector, scope), }); }
static TranslateResult <T> BuildPrivate <T>(ProjectionExpression proj) { var eagerChildProjections = EagerChildProjectionGatherer.Gatherer(proj).Select(cp => BuildChild(cp)).ToList(); var lazyChildProjections = LazyChildProjectionGatherer.Gatherer(proj).Select(cp => BuildChild(cp)).ToList(); Scope scope = new Scope { Alias = proj.Select.Alias, Positions = proj.Select.Columns.Select((c, i) => new { c.Name, i }).ToDictionary(p => p.Name, p => p.i), }; Expression <Func <IProjectionRow, T> > lambda = ProjectionBuilder.Build <T>(proj.Projector, scope); var command = QueryFormatter.Format(proj.Select); var result = new TranslateResult <T> { EagerProjections = eagerChildProjections, LazyChildProjections = lazyChildProjections, MainCommand = command, ProjectorExpression = lambda, Unique = proj.UniqueFunction, }; return(result); }
static IChildProjection LazyChild <K, V>(Expression projector, Scope scope, LookupToken token, SqlPreCommandSimple command) where K : notnull { var proj = ProjectionBuilder.Build <KeyValuePair <K, MList <V> .RowIdElement> >(projector, scope); return(new LazyChildProjection <K, V>(token, command, proj)); }
static IChildProjection LazyChild <K, V>(Expression projector, Scope scope) { return(new LazyChildProjection <K, V> { ProjectorExpression = ProjectionBuilder.Build <KeyValuePair <K, MList <V> .RowIdValue> >(projector, scope), }); }
static TranslateResult <T> BuildPrivate <T>(ProjectionExpression proj) { var eagerChildProjections = EagerChildProjectionGatherer.Gatherer(proj).Select(cp => BuildChild(cp)).ToList(); var lazyChildProjections = LazyChildProjectionGatherer.Gatherer(proj).Select(cp => BuildChild(cp)).ToList(); Scope scope = new Scope( alias: proj.Select.Alias, positions: proj.Select.Columns.Select((c, i) => new { c.Name, i }).ToDictionary(p => p.Name !, p => p.i) /*CSBUG*/ ); Expression <Func <IProjectionRow, T> > lambda = ProjectionBuilder.Build <T>(proj.Projector, scope); var command = QueryFormatter.Format(proj.Select); var result = new TranslateResult <T>( eagerProjections: eagerChildProjections, lazyChildProjections: lazyChildProjections, mainCommand: command, projectorExpression: lambda, unique: proj.UniqueFunction ); return(result); }
public void Mapping_Fails_For_Duplicate_Projection() { MemberMappingDescriptor desc = new MemberMappingDescriptor(); desc.OwnerType = typeof(AttributedUserDTO); desc.Type = typeof(string); desc.Member = "FullName"; desc.IsArray = false; desc.IsList = false; try { List <ProjectionItem> items = new List <ProjectionItem>(); items.Add(new ProjectionItem("source", "target_1")); items.Add(new ProjectionItem("source", "target_2")); ProjectionBuilder.Build(desc, items); } catch (OtisException e) { if (e.Message.Contains("Invalid projection 'source => target_2'. 'source' is already mapped to '\"target_1\"'")) { return; // success } } Assert.Fail("Tested method didn't throw an exception!"); }
public override object Execute(Expression expression) { ProjectionExpression projection = this.Translate(expression); var projector = ProjectionBuilder.Build(Context, projection.Projector); DirectorySearcher searcher = CreateSearcher(projection.Searcher); var reader = GetReader(projector, projection.Aggregator, true, (projection.Searcher == null) ? null : projection.Searcher.Skip); return(((Func <DirectorySearcher, object>)reader.Compile())(searcher)); }
public static RenderedProjectionDefinition <TResult> TranslateProject <TDocument, TResult>(Expression <Func <TDocument, TResult> > projector, IBsonSerializer <TDocument> parameterSerializer, IBsonSerializerRegistry serializerRegistry) { var binder = new SerializationInfoBinder(BsonSerializer.SerializerRegistry); var boundExpression = BindSerializationInfo(binder, projector, parameterSerializer); var projectionSerializer = (IBsonSerializer <TResult>)SerializerBuilder.Build(boundExpression, serializerRegistry); var projection = ProjectionBuilder.Build(boundExpression).AsBsonDocument; if (!projection.Contains("_id")) { projection.Add("_id", 0); // we don't want the id back unless we asked for it... } return(new RenderedProjectionDefinition <TResult>(projection, projectionSerializer)); }
public static ProjectionInfo <TResult> TranslateProject <TDocument, TResult>(Expression <Func <TDocument, TResult> > projector, IBsonSerializer <TDocument> parameterSerializer, IBsonSerializerRegistry serializerRegistry) { if (projector.Body.NodeType != ExpressionType.New) { throw new NotSupportedException("Must use an anonymous type for constructing $project pipeline operators."); } var binder = new SerializationInfoBinder(BsonSerializer.SerializerRegistry); var boundExpression = BindSerializationInfo(binder, projector, parameterSerializer); var projectionSerializer = (IBsonSerializer <TResult>)SerializerBuilder.Build(boundExpression, serializerRegistry); var projection = ProjectionBuilder.Build(boundExpression).AsBsonDocument; if (!projection.Contains("_id")) { projection.Add("_id", 0); // we don't want the id back unless we asked for it... } return(new ProjectionInfo <TResult>(projection, projectionSerializer)); }
internal IEnumerable <T> ExecuteQuery <T>(ProjectionExpression projection) { List <object> args = new List <object>(); string sql = QueryFormatter.Format(projection.Source, args); Database.Log?.Invoke(sql); using (SQLiteQuery query = Database.ExecuteQuery(sql, args.ToArray())) { LambdaExpression projectorExpr = ProjectionBuilder.Build( projection.Projector, projection.Source.Alias, name => string.IsNullOrEmpty(name) ? 0 : query.GetColumnIndex(name) ); var projector = (Func <SQLiteQueryProvider, SQLiteQuery, T>)projectorExpr.Compile(); while (query.Step()) { yield return(projector(this, query)); } } }
public void Projection_Mapping_Fails_For_Nonexisting_Enum_Value() { MemberMappingDescriptor desc = new MemberMappingDescriptor(); desc.OwnerType = typeof(AttributedUserDTO); desc.Type = typeof(Gender); desc.Member = "Gender"; desc.IsArray = false; desc.IsList = false; try { List <ProjectionItem> items = new List <ProjectionItem>(); items.Add(new ProjectionItem("X", "Undefined")); ProjectionBuilder.Build(desc, items); } catch (OtisException e) { if (e.Message.Contains("Invalid projection 'X => Undefined'. Value 'Undefined' is not defined in 'Otis.Tests.Gender' enumeration")) { return; // success } } Assert.Fail("Tested method didn't throw an exception!"); }
static IChildProjection EagerChild <K, V>(Expression projector, Scope scope, LookupToken token, SqlPreCommandSimple command) { var proj = ProjectionBuilder.Build <KeyValuePair <K, V> >(projector, scope); return(new EagerChildProjection <K, V>(token, command, proj)); }