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);

            return(new RenderedFieldDefinition <TField>(field.FieldName, fieldSerializer, valueSerializer, underlyingSerializer));
        }
Exemplo n.º 2
0
        public Expression Bind(PipelineExpression pipeline, PipelineBindingContext bindingContext, MethodCallExpression node, IEnumerable <Expression> arguments)
        {
            var newType = node.Method.GetGenericArguments()[0];

            var serializer = bindingContext.GetSerializer(newType, pipeline.Projector);

            var projector      = pipeline.Projector;
            var fieldProjector = projector as IFieldExpression;

            if (fieldProjector != null)
            {
                projector = new FieldExpression(
                    fieldProjector.FieldName,
                    serializer);
            }
            else
            {
                projector = new DocumentExpression(serializer);
            }

            return(new PipelineExpression(
                       new WhereExpression(
                           pipeline.Source,
                           "__p",
                           Expression.TypeIs(pipeline.Projector, newType)),
                       projector));
        }
Exemplo n.º 3
0
        public override object EditValue(ITypeDescriptorContext context, IServiceProvider provider, object value)
        {
            try
            {
                if (((string)value) == String.Empty)
                {
                    if (context != null)
                    {
                        DocumentExpression documentExpression = new DocumentExpression();
                        documentExpression.Type = DocumentType.Document;

                        DocumentExpressionOperatorAttribute attribute = (DocumentExpressionOperatorAttribute)context.PropertyDescriptor.Attributes[typeof(DocumentExpressionOperatorAttribute)];
                        if (attribute != null)
                        {
                            documentExpression.DocumentArgs.OperatorName = attribute.OperatorName;
                        }
                        value = DocumentExpressionForm.Execute(documentExpression);
                    }
                }
                else
                {
                    value = DocumentExpressionForm.Execute((string)value);
                }
            }
            catch (Exception exception)
            {
                Frontend.Client.Windows.Session.HandleException(exception);
            }
            return(value);
        }
Exemplo n.º 4
0
        public static string Execute()
        {
            DocumentExpression documentExpression = new DocumentExpression();

            documentExpression.Type = DocumentType.None;
            return(Execute(documentExpression));
        }
Exemplo n.º 5
0
        public Expression Bind(PipelineExpression pipeline, EmbeddedPipelineBindingContext bindingContext, MethodCallExpression node, IEnumerable <Expression> arguments)
        {
            var source = new ZipExpression(
                node.Type,
                pipeline.Source,
                arguments.First());

            var lambda = ExpressionHelper.GetLambda(arguments.Last());
            var input  = new DocumentExpression(bindingContext.GetSerializer(typeof(object[]), null));

            var itemA = new ArrayIndexExpression(
                input,
                Expression.Constant(0),
                bindingContext.GetSerializer(lambda.Parameters[0].Type, source.Source));
            var itemB = new ArrayIndexExpression(
                input,
                Expression.Constant(1),
                bindingContext.GetSerializer(lambda.Parameters[1].Type, source.Other));

            bindingContext.AddExpressionMapping(lambda.Parameters[0], itemA);
            bindingContext.AddExpressionMapping(lambda.Parameters[1], itemB);

            var selector = bindingContext.Bind(lambda.Body);

            var serializer = bindingContext.GetSerializer(selector.Type, selector);

            return(new PipelineExpression(
                       new SelectExpression(
                           source,
                           lambda.Parameters[0].Name + "_" + lambda.Parameters[1].Name,
                           selector),
                       new DocumentExpression(serializer)));
        }
        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);

            if (!(boundKeyExpression is IBsonSerializationInfoExpression))
            {
                var keySerializer = SerializerBuilder.Build(boundKeyExpression, serializerRegistry);
                boundKeyExpression = new DocumentExpression(
                    boundKeyExpression,
                    new BsonSerializationInfo(null, keySerializer, typeof(TKey)));
            }

            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));
        }
Exemplo n.º 7
0
        public SerializationExpression BindProjector(ref Expression selector)
        {
            var projector = selector as SerializationExpression;

            if (selector.NodeType == ExpressionType.MemberInit || selector.NodeType == ExpressionType.New)
            {
                var serializer = SerializerBuilder.Build(selector, _serializerRegistry);

                projector = new DocumentExpression(serializer);
            }
            else if (projector == null || projector is PipelineExpression || projector is IFieldExpression || projector is ArrayIndexExpression)
            {
                var newFieldName = "__fld0";
                if (projector is IFieldExpression)
                {
                    // We don't have to do this, but it makes the output a little nicer.
                    newFieldName = ((IFieldExpression)projector).FieldName;
                }

                // the output of a $project stage must be a document, so
                // if this isn't already a serialization expression and it's not
                // a new expression or member init, then we need to create an
                // artificial field to project the computation into.
                var serializer = GetSerializer(selector.Type, selector);
                selector  = new FieldAsDocumentExpression(selector, newFieldName, serializer);
                projector = new FieldExpression(newFieldName, serializer);
            }

            return(projector);
        }
Exemplo n.º 8
0
        private static Expression BindSerializationInfo(SerializationInfoBinder binder, LambdaExpression node, IBsonSerializer parameterSerializer)
        {
            var parameterSerializationInfo = new BsonSerializationInfo(null, parameterSerializer, parameterSerializer.ValueType);
            var parameterExpression        = new DocumentExpression(node.Parameters[0], parameterSerializationInfo, false);

            binder.RegisterParameterReplacement(node.Parameters[0], parameterExpression);
            return(binder.Bind(node.Body));
        }
        protected internal override Expression VisitDocument(DocumentExpression node)
        {
            if (node == _documentExpression)
            {
                _fullDocument = true;
            }

            return(base.VisitDocument(node));
        }
Exemplo n.º 10
0
        /// <summary> Gets a new document expression for a given expression string. </summary>
        public static DocumentExpression GetDocumentExpression(string expression)
        {
            var localExpression = new DocumentExpression(expression);

            if (localExpression.Type != DocumentType.Document)
            {
                throw new DataphoriaException(DataphoriaException.Codes.CanOnlyLiveEditDocuments);
            }
            return(localExpression);
        }
Exemplo n.º 11
0
        public override void New()
        {
            var ancestors          = new Ancestors();
            var documentExpression = new DocumentExpression();

            documentExpression.Type = DocumentType.Document;
            documentExpression.DocumentArgs.OperatorName = ".Frontend.Form";
            ancestors.Add(DocumentExpressionForm.Execute(documentExpression));
            New(ancestors);
        }
            protected override Expression VisitDocument(DocumentExpression node)
            {
                if (node.SerializationInfo.ElementName.StartsWith(_oldName))
                {
                    return(new DocumentExpression(
                               node.Expression,
                               node.SerializationInfo.WithNewName(GetReplacementName(node.SerializationInfo.ElementName))));
                }

                return(base.VisitDocument(node));
            }
Exemplo n.º 13
0
        protected override Expression VisitMember(MemberExpression node)
        {
            Expression newNode;

            if (_bindingContext.TryGetMemberMapping(node.Member, out newNode))
            {
                if (newNode is ISerializationExpression)
                {
                    return(newNode);
                }

                var message = string.Format("Could not determine serialization information for member {0} in the expression tree {1}.",
                                            node.Member,
                                            node.ToString());
                throw new MongoInternalException(message);
            }

            newNode = base.VisitMember(node);
            var mex = newNode as MemberExpression;

            if (mex != null)
            {
                var serializationExpression = mex.Expression as ISerializationExpression;
                if (serializationExpression != null)
                {
                    var documentSerializer = serializationExpression.Serializer as IBsonDocumentSerializer;
                    BsonSerializationInfo memberSerializationInfo;
                    if (documentSerializer != null && documentSerializer.TryGetMemberSerializationInfo(node.Member.Name, out memberSerializationInfo))
                    {
                        if (memberSerializationInfo.ElementName == null)
                        {
                            newNode = new DocumentExpression(memberSerializationInfo.Serializer);
                        }
                        else
                        {
                            newNode = new FieldExpression(
                                mex.Expression,
                                memberSerializationInfo.ElementName,
                                memberSerializationInfo.Serializer,
                                mex);
                        }
                    }

                    var parameterExpression = (node.Expression as ParameterExpression);
                    if (_isOutOfCurrentScope && parameterExpression != null)
                    {
                        SetOutOfCurrentScopePrefixIfPossible(newNode, parameterExpression.Name);
                    }
                }
            }

            return(newNode);
        }
Exemplo n.º 14
0
        protected DocumentDesignBuffer BufferFromHost(IHost host)
        {
            DocumentExpression expression = Program.GetDocumentExpression(host.Document);
            var buffer =
                new DocumentDesignBuffer
                (
                    Dataphoria,
                    expression.DocumentArgs.LibraryName,
                    expression.DocumentArgs.DocumentName
                );

            return(buffer);
        }
        private static Expression BindKeySelector <TKey, TDocument>(PipelineBindingContext bindingContext, Expression <Func <TDocument, TKey> > keySelector, IBsonSerializer <TDocument> parameterSerializer)
        {
            var parameterExpression = new DocumentExpression(parameterSerializer);

            bindingContext.AddExpressionMapping(keySelector.Parameters[0], parameterExpression);
            var node = PartialEvaluator.Evaluate(keySelector.Body);

            node = Transformer.Transform(node);
            node = bindingContext.Bind(node);

            var keySerializer = bindingContext.GetSerializer(node.Type, node);

            return(new GroupingKeyExpression(node, keySerializer));
        }
Exemplo n.º 16
0
        private static Expression BindResult<TSource,TResult>(
            PipelineBindingContext bindingContext,
            Expression<Func<TSource, TResult>> expression,
            IBsonSerializer<TSource> sourceSerializer)
        {
            var parameterExpression = new DocumentExpression(sourceSerializer);
            bindingContext.AddExpressionMapping(expression.Parameters[0], parameterExpression);
            var node = PartialEvaluator.Evaluate(expression.Body);
            node = Transformer.Transform(node);
            node = bindingContext.Bind(node);

            var resultSerializer = bindingContext.GetSerializer(node.Type, node);
            return new AggregateExpressionExpression(node, resultSerializer);
        }
        public static RenderedProjectionDefinition <TProjection> Translate <TDocument, TProjection>(Expression <Func <TDocument, TProjection> > projector, IBsonSerializer <TDocument> parameterSerializer, IBsonSerializerRegistry serializerRegistry)
        {
            var bindingContext     = new PipelineBindingContext(serializerRegistry);
            var documentExpression = new DocumentExpression(parameterSerializer);

            bindingContext.AddExpressionMapping(projector.Parameters[0], documentExpression);

            var node = PartialEvaluator.Evaluate(projector.Body);

            node = Transformer.Transform(node);
            node = bindingContext.Bind(node, isClientSideProjection: true);
            node = FieldExpressionFlattener.FlattenFields(node);

            BsonDocument projectionDocument = null;
            IBsonSerializer <TProjection> serializer;

            if (node is DocumentExpression)
            {
                serializer = new ProjectingDeserializer <TDocument, TProjection>(parameterSerializer, projector.Compile());
            }
            else
            {
                var candidateFields   = SerializationExpressionGatherer.Gather(node);
                var fields            = GetUniqueFieldsByHierarchy(candidateFields);
                var serializationInfo = fields.Select(x => new BsonSerializationInfo(x.FieldName, x.Serializer, x.Serializer.ValueType)).ToList();

                var projectedObjectExpression = Expression.Parameter(typeof(ProjectedObject), "document");
                var translator = new FindProjectionTranslator(documentExpression, projectedObjectExpression, fields);
                var translated = translator.Visit(node);
                if (translator._fullDocument)
                {
                    serializer = new ProjectingDeserializer <TDocument, TProjection>(parameterSerializer, projector.Compile());
                }
                else
                {
                    var newProjector = Expression.Lambda <Func <ProjectedObject, TProjection> >(
                        translated,
                        projectedObjectExpression);

                    projectionDocument = GetProjectionDocument(serializationInfo);
                    var projectedObjectSerializer = new ProjectedObjectDeserializer(serializationInfo);
                    serializer = new ProjectingDeserializer <ProjectedObject, TProjection>(projectedObjectSerializer, newProjector.Compile());
                }
            }

            return(new RenderedProjectionDefinition <TProjection>(projectionDocument, serializer));
        }
        public static RenderedProjectionDefinition <TResult> Translate <TDocument, TResult>(Expression <Func <TDocument, TResult> > projector, IBsonSerializer <TDocument> parameterSerializer, IBsonSerializerRegistry serializerRegistry)
        {
            var bindingContext      = new PipelineBindingContext(serializerRegistry);
            var parameterExpression = new DocumentExpression(parameterSerializer);

            bindingContext.AddExpressionMapping(projector.Parameters[0], parameterExpression);

            var node = PartialEvaluator.Evaluate(projector.Body);

            node = Transformer.Transform(node);
            node = bindingContext.Bind(node);

            var projectionSerializer = bindingContext.GetSerializer(node.Type, node);
            var projection           = TranslateProject(node);

            return(new RenderedProjectionDefinition <TResult>(projection, (IBsonSerializer <TResult>)projectionSerializer));
        }
        private static Expression BindGroup <TKey, TDocument, TResult>(PipelineBindingContext bindingContext, Expression <Func <IGrouping <TKey, TDocument>, TResult> > groupProjector, IBsonSerializer <TDocument> parameterSerializer, Expression keySelector)
        {
            var groupSerializer = new ArraySerializer <TDocument>(parameterSerializer);
            var groupExpression = new DocumentExpression(groupSerializer);

            var correlationId = Guid.NewGuid();

            bindingContext.AddCorrelatingId(groupExpression, correlationId);
            bindingContext.AddExpressionMapping(groupProjector.Parameters[0], groupExpression);
            bindingContext.AddMemberMapping(typeof(IGrouping <TKey, TDocument>).GetProperty("Key"), keySelector);

            var node = PartialEvaluator.Evaluate(groupProjector.Body);

            node = Transformer.Transform(node);
            node = bindingContext.Bind(node);

            return(CorrelatedAccumulatorRemover.Remove(node, correlationId));
        }
Exemplo n.º 20
0
        /// <inheritdoc />
        public override RenderedFieldDefinition Render(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);
            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));
        }
Exemplo n.º 21
0
        private Expression BindSelector(PipelineExpression pipeline, PipelineBindingContext bindingContext, Expression keySelector, Expression node)
        {
            var lambda = ExpressionHelper.GetLambda(node);

            var serializer         = bindingContext.GetSerializer(pipeline.Projector.Type, pipeline.Projector);
            var sequenceSerializer = (IBsonSerializer)Activator.CreateInstance(
                typeof(ArraySerializer <>).MakeGenericType(pipeline.Projector.Type),
                new object[] { serializer });
            var sequence = new DocumentExpression(sequenceSerializer);

            var keySerializer = bindingContext.GetSerializer(keySelector.Type, keySelector);

            Guid correlationId = Guid.NewGuid();

            bindingContext.AddCorrelatingId(sequence, correlationId);
            bindingContext.AddExpressionMapping(lambda.Parameters[0], new GroupingKeyExpression(keySelector, keySerializer));
            bindingContext.AddExpressionMapping(lambda.Parameters[1], sequence);
            var bound = bindingContext.Bind(lambda.Body);

            return(CorrelatedAccumulatorRemover.Remove(bound, correlationId));
        }
Exemplo n.º 22
0
        public Expression Bind(PipelineExpression pipeline, PipelineBindingContext bindingContext, MethodCallExpression node, IEnumerable <Expression> arguments)
        {
            var lambda = ExpressionHelper.GetLambda(arguments.Single());

            bindingContext.AddExpressionMapping(lambda.Parameters[0], pipeline.Projector);

            var selector = bindingContext.Bind(lambda.Body);

            if (selector == pipeline.Projector)
            {
                return(pipeline);
            }

            var        serializationExpression = selector as ISerializationExpression;
            Expression projector = selector;

            if (serializationExpression == null && selector.NodeType != ExpressionType.MemberInit && selector.NodeType != ExpressionType.New)
            {
                // the output of a $project stage must be a document, so
                // if this isn't already a serialization expression and it's not
                // a new expression or member init, then we need to create an
                // artificial field to project the computation into.
                var wrapped = bindingContext.WrapField(selector, "__fld0");
                selector  = wrapped;
                projector = new FieldExpression(wrapped.FieldName, wrapped.Serializer);
            }
            else if (selector.NodeType == ExpressionType.MemberInit || selector.NodeType == ExpressionType.New)
            {
                var serializer = bindingContext.GetSerializer(selector.Type, selector);
                projector = new DocumentExpression(serializer);
            }

            return(new PipelineExpression(
                       new SelectExpression(
                           pipeline.Source,
                           lambda.Parameters[0].Name,
                           selector),
                       projector));
        }
Exemplo n.º 23
0
        public static ProjectionInfo <TResult> Translate <TDocument, TResult>(Expression <Func <TDocument, TResult> > projector, IBsonSerializer <TDocument> parameterSerializer)
        {
            var parameterSerializationInfo = new BsonSerializationInfo(null, parameterSerializer, parameterSerializer.ValueType);
            var parameterExpression        = new DocumentExpression(projector.Parameters[0], parameterSerializationInfo, false);
            var binder = new SerializationInfoBinder(BsonSerializer.SerializerRegistry);

            binder.RegisterParameterReplacement(projector.Parameters[0], parameterExpression);
            var boundExpression = binder.Bind(projector.Body);
            var candidateFields = FieldExpressionGatherer.Gather(boundExpression, true);

            var fields = GetUniqueFieldsByHierarchy(candidateFields);

            var serializationInfo = fields.Select(x => x.SerializationInfo).ToList();

            var replacementParameter = Expression.Parameter(typeof(ProjectedObject), "document");

            var translator   = new FindProjectionTranslator(projector.Parameters[0], replacementParameter, fields);
            var newProjector = Expression.Lambda <Func <ProjectedObject, TResult> >(
                translator.Visit(boundExpression),
                replacementParameter);

            BsonDocument projectionDocument;
            IBsonSerializer <TResult> serializer;

            if (translator._fullDocument)
            {
                projectionDocument = null;
                serializer         = new ProjectingDeserializer <TDocument, TResult>(parameterSerializer, projector.Compile());
            }
            else
            {
                projectionDocument = GetProjectionDocument(serializationInfo);
                var projectedObjectSerializer = new ProjectedObjectDeserializer(serializationInfo);
                serializer = new ProjectingDeserializer <ProjectedObject, TResult>(projectedObjectSerializer, newProjector.Compile());
            }

            return(new ProjectionInfo <TResult>(projectionDocument, serializer));
        }
Exemplo n.º 24
0
 protected internal override Expression VisitDocument(DocumentExpression node)
 {
     return(new FieldExpression(
                _prefix,
                node.Serializer));
 }
Exemplo n.º 25
0
        public Expression Bind(PipelineExpression pipeline, PipelineBindingContext bindingContext, MethodCallExpression node, IEnumerable <Expression> arguments)
        {
            var collectionSelectorLambda = ExpressionHelper.GetLambda(arguments.First());

            bindingContext.AddExpressionMapping(collectionSelectorLambda.Parameters[0], pipeline.Projector);
            var    collectionSelector = bindingContext.Bind(collectionSelectorLambda.Body) as IFieldExpression;
            string collectionItemName = collectionSelectorLambda.Parameters[0].Name;

            if (collectionSelector == null)
            {
                var message = string.Format("Unable to determine the serialization information for the collection selector in the tree: {0}", node.ToString());
                throw new NotSupportedException(message);
            }

            var collectionSerializer = collectionSelector.Serializer as IBsonArraySerializer;
            BsonSerializationInfo itemSerializationInfo;

            if (collectionSerializer == null || !collectionSerializer.TryGetItemSerializationInfo(out itemSerializationInfo))
            {
                var message = string.Format("The collection selector's serializer must implement IBsonArraySerializer: {0}", node.ToString());
                throw new NotSupportedException(message);
            }

            string     resultItemName;
            Expression resultSelector;

            if (arguments.Count() == 2)
            {
                var resultLambda = ExpressionHelper.GetLambda(arguments.Last());
                bindingContext.AddExpressionMapping(resultLambda.Parameters[0], pipeline.Projector);
                bindingContext.AddExpressionMapping(
                    resultLambda.Parameters[1],
                    new FieldExpression(collectionSelector.FieldName, itemSerializationInfo.Serializer));

                resultItemName = resultLambda.Parameters[1].Name;
                resultSelector = bindingContext.Bind(resultLambda.Body);
            }
            else
            {
                resultItemName = "__p";
                resultSelector = new FieldExpression(collectionSelector.FieldName, itemSerializationInfo.Serializer);
            }

            var        serializationExpression = resultSelector as ISerializationExpression;
            Expression projector = resultSelector;

            if (serializationExpression == null && resultSelector.NodeType != ExpressionType.MemberInit && resultSelector.NodeType != ExpressionType.New)
            {
                // the output of a $project stage must be a document, so
                // if this isn't already a serialization expression and it's not
                // a new expression or member init, then we need to create an
                // artificial field to project the computation into.
                var wrapped = bindingContext.WrapField(resultSelector, "__fld0");
                resultSelector = wrapped;
                projector      = new FieldExpression(wrapped.FieldName, wrapped.Serializer);
            }
            else if (resultSelector.NodeType == ExpressionType.MemberInit || resultSelector.NodeType == ExpressionType.New)
            {
                var serializer = bindingContext.GetSerializer(resultSelector.Type, resultSelector);
                projector = new DocumentExpression(serializer);
            }

            return(new PipelineExpression(
                       new SelectManyExpression(
                           pipeline.Source,
                           collectionItemName,
                           (Expression)collectionSelector,
                           resultItemName,
                           resultSelector),
                       projector));
        }
Exemplo n.º 26
0
        public static string Execute(DocumentExpression documentExpression)
        {
            IDataphoria dataphoria = Program.DataphoriaInstance;

            Frontend.Client.Windows.IWindowsFormInterface form = dataphoria.FrontendSession.LoadForm(null, ".Frontend.Form('Frontend', 'DocumentExpressionEditor')", new Frontend.Client.FormInterfaceHandler(SetEditOpenState));
            try
            {
                Frontend.Client.INotebook notebook = (Frontend.Client.INotebook)form.FindNode("Notebook");
                Frontend.Client.ISource   source   = form.MainSource;
                switch (documentExpression.Type)
                {
                case DocumentType.Document:
                    notebook.ActivePage = (Frontend.Client.INotebookPage)form.FindNode("nbpLoad");
                    source.DataView.Fields["Library_Name"].AsString  = documentExpression.DocumentArgs.LibraryName;
                    source.DataView.Fields["Document_Name"].AsString = documentExpression.DocumentArgs.DocumentName;
                    break;

                case DocumentType.Derive:
                    notebook.ActivePage = (Frontend.Client.INotebookPage)form.FindNode("nbpDerive");
                    form.MainSource.DataView["Query"].AsString      = documentExpression.DeriveArgs.Query;
                    form.MainSource.DataView["PageType"].AsString   = documentExpression.DeriveArgs.PageType;
                    form.MainSource.DataView["MKN"].AsString        = documentExpression.DeriveArgs.MasterKeyNames;
                    form.MainSource.DataView["DKN"].AsString        = documentExpression.DeriveArgs.DetailKeyNames;
                    form.MainSource.DataView["Elaborate"].AsBoolean = documentExpression.DeriveArgs.Elaborate;
                    break;

                default:
                    notebook.ActivePage = (Frontend.Client.INotebookPage)form.FindNode("nbpOther");
                    form.MainSource.DataView["Expression"].AsString = documentExpression.Expression;
                    break;
                }
                if (form.ShowModal(Frontend.Client.FormMode.Edit) != DialogResult.OK)
                {
                    throw new AbortException();
                }
                if ((notebook.ActivePage.Name) == "nbpLoad")
                {
                    documentExpression.Type = DocumentType.Document;
                    documentExpression.DocumentArgs.LibraryName  = form.MainSource.DataView["Library_Name"].AsString;
                    documentExpression.DocumentArgs.DocumentName = form.MainSource.DataView["Document_Name"].AsString;
                }
                else if ((notebook.ActivePage.Name) == "nbpDerive")
                {
                    documentExpression.Type                      = DocumentType.Derive;
                    documentExpression.DeriveArgs.Query          = form.MainSource.DataView["Query"].AsString;
                    documentExpression.DeriveArgs.PageType       = form.MainSource.DataView["PageType"].AsString;
                    documentExpression.DeriveArgs.MasterKeyNames = form.MainSource.DataView["MKN"].AsString;
                    documentExpression.DeriveArgs.DetailKeyNames = form.MainSource.DataView["DKN"].AsString;
                    documentExpression.DeriveArgs.Elaborate      = form.MainSource.DataView["Elaborate"].AsBoolean;
                }
                else if ((notebook.ActivePage.Name) == "nbpOther")
                {
                    documentExpression.Type       = DocumentType.Other;
                    documentExpression.Expression = form.MainSource.DataView["Expression"].AsString;
                }
                return(documentExpression.Expression);
            }
            finally
            {
                form.Dispose();
            }
        }
Exemplo n.º 27
0
        public static string Execute(string expression)
        {
            DocumentExpression documentExpression = new DocumentExpression(expression);

            return(Execute(documentExpression));
        }
 private FindProjectionTranslator(DocumentExpression documentExpression, ParameterExpression parameterExpression, IReadOnlyList <IFieldExpression> fields)
 {
     _documentExpression  = Ensure.IsNotNull(documentExpression, nameof(documentExpression));
     _parameterExpression = Ensure.IsNotNull(parameterExpression, nameof(parameterExpression));
     _fields = fields;
 }
Exemplo n.º 29
0
 protected override Expression VisitDocument(DocumentExpression node)
 {
     return(Visit(node.Expression));
 }
Exemplo n.º 30
0
        public Expression Bind(PipelineExpression pipeline, PipelineBindingContext bindingContext, MethodCallExpression node, IEnumerable <Expression> arguments)
        {
            var args   = arguments.ToList();
            var joined = bindingContext.Bind(args[0]) as CollectionExpression;

            if (joined == null)
            {
                throw new NotSupportedException("The joined collection cannot have any qualifiers.");
            }

            var sourceKeySelectorLambda = ExpressionHelper.GetLambda(args[1]);

            bindingContext.AddExpressionMapping(sourceKeySelectorLambda.Parameters[0], pipeline.Projector);
            var sourceKeySelector = bindingContext.Bind(sourceKeySelectorLambda.Body) as IFieldExpression;

            if (sourceKeySelector == null)
            {
                var message = string.Format("Unable to determine the serialization information for the outer key selector in the tree: {0}", node.ToString());
                throw new NotSupportedException(message);
            }

            var joinedArraySerializer = joined.Serializer as IBsonArraySerializer;
            BsonSerializationInfo joinedItemSerializationInfo;

            if (joinedArraySerializer == null || !joinedArraySerializer.TryGetItemSerializationInfo(out joinedItemSerializationInfo))
            {
                var message = string.Format("Unable to determine the serialization information for the inner collection: {0}", node.ToString());
                throw new NotSupportedException(message);
            }

            var joinedKeySelectorLambda = ExpressionHelper.GetLambda(args[2]);
            var joinedDocument          = new DocumentExpression(joinedItemSerializationInfo.Serializer);

            bindingContext.AddExpressionMapping(joinedKeySelectorLambda.Parameters[0], joinedDocument);
            var joinedKeySelector = bindingContext.Bind(joinedKeySelectorLambda.Body) as IFieldExpression;

            if (joinedKeySelector == null)
            {
                var message = string.Format("Unable to determine the serialization information for the inner key selector in the tree: {0}", node.ToString());
                throw new NotSupportedException(message);
            }

            var resultSelectorLambda = ExpressionHelper.GetLambda(args[3]);

            var sourceSerializer = pipeline.Projector.Serializer;
            var joinedSerializer = node.Method.Name == "GroupJoin" ?
                                   joined.Serializer :
                                   joinedItemSerializationInfo.Serializer;
            var sourceDocument = new DocumentExpression(sourceSerializer);
            var joinedField    = new FieldExpression(
                resultSelectorLambda.Parameters[1].Name,
                joinedSerializer);

            bindingContext.AddExpressionMapping(
                resultSelectorLambda.Parameters[0],
                sourceDocument);
            bindingContext.AddExpressionMapping(
                resultSelectorLambda.Parameters[1],
                joinedField);
            var resultSelector = bindingContext.Bind(resultSelectorLambda.Body);

            Expression source;

            if (node.Method.Name == "GroupJoin")
            {
                source = new GroupJoinExpression(
                    node.Type,
                    pipeline.Source,
                    joined,
                    (Expression)sourceKeySelector,
                    (Expression)joinedKeySelector,
                    resultSelectorLambda.Parameters[1].Name);
            }
            else
            {
                source = new JoinExpression(
                    node.Type,
                    pipeline.Source,
                    joined,
                    (Expression)sourceKeySelector,
                    (Expression)joinedKeySelector,
                    resultSelectorLambda.Parameters[1].Name);
            }

            SerializationExpression projector;
            var newResultSelector = resultSelector as NewExpression;

            if (newResultSelector != null &&
                newResultSelector.Arguments[0] == sourceDocument &&
                newResultSelector.Arguments[1] == joinedField)
            {
                Func <object, object, object> creator = (s, j) => Activator.CreateInstance(resultSelector.Type, s, j);
                var serializer = (IBsonSerializer)Activator.CreateInstance(
                    typeof(JoinSerializer <>).MakeGenericType(resultSelector.Type),
                    sourceSerializer,
                    newResultSelector.Members[0].Name,
                    joinedSerializer,
                    newResultSelector.Members[1].Name,
                    resultSelectorLambda.Parameters[1].Name,
                    creator);

                projector = new DocumentExpression(serializer);
            }
            else
            {
                projector = bindingContext.BindProjector(ref resultSelector);
                source    = new SelectExpression(
                    source,
                    "__i", // since this is a top-level pipeline, this doesn't matter
                    resultSelector);
            }

            return(new PipelineExpression(
                       source,
                       projector));
        }