コード例 #1
0
 // TODO: Debugging
 private void VerifySelectExpression(ProjectionBindingExpression projectionBindingExpression)
 {
     if (projectionBindingExpression.QueryExpression != _selectExpression)
     {
         throw new InvalidOperationException(CoreStrings.QueryFailed(projectionBindingExpression.Print(), GetType().Name));
     }
 }
 // TODO: Debugging
 private void VerifyQueryExpression(ProjectionBindingExpression projectionBindingExpression)
 {
     if (projectionBindingExpression.QueryExpression != _queryExpression)
     {
         throw new InvalidOperationException();
     }
 }
コード例 #3
0
 private object GetProjectionIndex(ProjectionBindingExpression projectionBindingExpression)
 {
     return(projectionBindingExpression.ProjectionMember != null
         ? ((ConstantExpression)_selectExpression.GetProjectionExpression(projectionBindingExpression.ProjectionMember)).Value
         : (projectionBindingExpression.Index != null
             ? (object)projectionBindingExpression.Index
             : projectionBindingExpression.IndexMap));
 }
コード例 #4
0
 private object GetProjectionIndex(ProjectionBindingExpression projectionBindingExpression)
 {
     return(projectionBindingExpression.ProjectionMember != null
         ? ((ConstantExpression)_selectExpression.GetMappedProjection(projectionBindingExpression.ProjectionMember)).Value
         : (projectionBindingExpression.Index != null
             ? projectionBindingExpression.Index
             : throw new InvalidOperationException()));
 }
コード例 #5
0
 protected override Expression VisitExtension(Expression extensionExpression)
 {
     return(extensionExpression switch
     {
         ProjectionBindingExpression projectionBindingExpression => Remap(projectionBindingExpression),
         CollectionShaperExpression collectionShaperExpression => Remap(collectionShaperExpression),
         _ => base.VisitExtension(extensionExpression)
     });
コード例 #6
0
        private ShapedQueryExpression AggregateResultShaper(
            ShapedQueryExpression source, Expression projection, bool throwOnNullResult, Type resultType)
        {
            var selectExpression = (SelectExpression)source.QueryExpression;

            selectExpression.ReplaceProjectionMapping(
                new Dictionary <ProjectionMember, Expression>
            {
                { new ProjectionMember(), projection }
            });

            selectExpression.ClearOrdering();

            Expression shaper = new ProjectionBindingExpression(source.QueryExpression, new ProjectionMember(), projection.Type);

            if (throwOnNullResult &&
                resultType.IsNullableType())
            {
                var resultVariable = Expression.Variable(projection.Type, "result");

                shaper = Expression.Block(
                    new[] { resultVariable },
                    Expression.Assign(resultVariable, shaper),
                    Expression.Condition(
                        Expression.Equal(resultVariable, Expression.Default(projection.Type)),
                        Expression.Constant(null, resultType),
                        resultType != resultVariable.Type
                            ? Expression.Convert(resultVariable, resultType)
                            : (Expression)resultVariable));
            }
            else if (throwOnNullResult)
            {
                var resultVariable = Expression.Variable(projection.Type, "result");

                shaper = Expression.Block(
                    new[] { resultVariable },
                    Expression.Assign(resultVariable, shaper),
                    Expression.Condition(
                        Expression.Equal(resultVariable, Expression.Default(projection.Type)),
                        Expression.Throw(
                            Expression.New(
                                typeof(InvalidOperationException).GetConstructors()
                                .Single(ci => ci.GetParameters().Length == 1),
                                Expression.Constant(CoreStrings.NoElements)),
                            resultType),
                        resultType != resultVariable.Type
                            ? Expression.Convert(resultVariable, resultType)
                            : (Expression)resultVariable));
            }
            else if (resultType.IsNullableType())
            {
                shaper = Expression.Convert(shaper, resultType);
            }

            source.ShaperExpression = shaper;

            return(source);
        }
コード例 #7
0
        private ShapedQueryExpression AggregateResultShaper(
            ShapedQueryExpression source, Expression projection, bool throwWhenEmpty, Type resultType)
        {
            var selectExpression = (SelectExpression)source.QueryExpression;

            selectExpression.ReplaceProjectionMapping(
                new Dictionary <ProjectionMember, Expression> {
                { new ProjectionMember(), projection }
            });

            selectExpression.ClearOrdering();
            Expression shaper;

            if (throwWhenEmpty)
            {
                // Avg/Max/Min case.
                // We always read nullable value
                // If resultType is nullable then we always return null. Only non-null result shows throwing behavior.
                // otherwise, if projection.Type is nullable then server result is passed through DefaultIfEmpty, hence we return default
                // otherwise, server would return null only if it is empty, and we throw
                var nullableResultType = resultType.MakeNullable();
                shaper = new ProjectionBindingExpression(source.QueryExpression, new ProjectionMember(), nullableResultType);
                var resultVariable     = Expression.Variable(nullableResultType, "result");
                var returnValueForNull = resultType.IsNullableType()
                    ? Expression.Constant(null, resultType)
                    : projection.Type.IsNullableType()
                        ? (Expression)Expression.Default(resultType)
                        : Expression.Throw(
                    Expression.New(
                        typeof(InvalidOperationException).GetConstructors()
                        .Single(ci => ci.GetParameters().Length == 1),
                        Expression.Constant(CoreStrings.NoElements)),
                    resultType);

                shaper = Expression.Block(
                    new[] { resultVariable },
                    Expression.Assign(resultVariable, shaper),
                    Expression.Condition(
                        Expression.Equal(resultVariable, Expression.Default(nullableResultType)),
                        returnValueForNull,
                        resultType != resultVariable.Type
                            ? Expression.Convert(resultVariable, resultType)
                            : (Expression)resultVariable));
            }
            else
            {
                // Sum case. Projection is always non-null. We read non-nullable value (0 if empty)
                shaper = new ProjectionBindingExpression(source.QueryExpression, new ProjectionMember(), projection.Type);
                // Cast to nullable type if required
                if (resultType != shaper.Type)
                {
                    shaper = Expression.Convert(shaper, resultType);
                }
            }

            return(source.UpdateShaperExpression(shaper));
        }
 private object GetProjectionIndex(
     InMemoryQueryExpression queryExpression, ProjectionBindingExpression projectionBindingExpression)
 {
     return(projectionBindingExpression.ProjectionMember != null
         ? ((ConstantExpression)queryExpression.GetMappedProjection(projectionBindingExpression.ProjectionMember)).Value
         : (projectionBindingExpression.Index != null
             ? (object)projectionBindingExpression.Index
             : projectionBindingExpression.IndexMap));
 }
コード例 #9
0
 /// <summary>
 ///     Creates a new instance of the <see cref="CollectionResultExpression" /> class.
 /// </summary>
 /// <param name="projectionBindingExpression">An expression reprensenting how to get the subquery from SelectExpression to get the elements.</param>
 /// <param name="navigation">A navigation associated with this collection, if any.</param>
 /// <param name="elementType">The clr type of individual elements in the collection.</param>
 public CollectionResultExpression(
     ProjectionBindingExpression projectionBindingExpression,
     INavigationBase?navigation,
     Type elementType)
 {
     ProjectionBindingExpression = projectionBindingExpression;
     Navigation  = navigation;
     ElementType = elementType;
 }
コード例 #10
0
        public ProjectionBindingExpression AddToProjection(SqlExpression sqlExpression, Type type)
        {
            if (!_projectionCache.TryGetValue(sqlExpression, out var result))
            {
                _projection.Add(new ProjectionExpression(sqlExpression, alias: ""));
                result = new ProjectionBindingExpression(this, _projection.Count - 1, type);
                _projectionCache[sqlExpression] = result;
            }

            return(result);
        }
コード例 #11
0
        public ProjectionBindingExpression AddToProjection(ProjectionBindingExpression projectionBindingExpression)
        {
            var entityProjection = (EntityProjectionExpression)_projectionMapping[projectionBindingExpression.ProjectionMember];
            var index            = _projection.Count;

            foreach (var property in entityProjection.EntityType.GetProperties())
            {
                var columnExpression = entityProjection.GetProperty(property);
                _projection.Add(new ProjectionExpression(columnExpression, alias: ""));
            }

            return(new ProjectionBindingExpression(this, index, typeof(ValueBuffer)));
        }
コード例 #12
0
        public ProjectionBindingExpression AddToProjection(ProjectionBindingExpression projectionBindingExpression)
        {
            var entityProjection = (EntityProjectionExpression)_projectionMapping[projectionBindingExpression.ProjectionMember];

            if (!_projectionCache.TryGetValue(entityProjection, out var result))
            {
                var map = new Dictionary <IProperty, int>();
                foreach (var property in entityProjection.EntityType
                         .GetDerivedTypesInclusive().SelectMany(e => e.GetDeclaredProperties()))
                {
                    var columnExpression = entityProjection.GetProperty(property);
                    map[property] = _projection.Count;
                    _projection.Add(new ProjectionExpression(columnExpression, alias: ""));
                }

                result = new ProjectionBindingExpression(this, map);
                _projectionCache[entityProjection] = result;
            }

            return(result);
        }
 private Expression GenerateKey(ProjectionBindingExpression projectionBindingExpression)
 => projectionBindingExpression.ProjectionMember != null
         ? _selectExpression.GetMappedProjection(projectionBindingExpression.ProjectionMember)
         : projectionBindingExpression;
コード例 #14
0
 protected override ProjectionExpression GetProjection(ProjectionBindingExpression _)
 => _readItemExpression.ProjectionExpression;
コード例 #15
0
 private int GetProjectionIndex(ProjectionBindingExpression projectionBindingExpression)
 {
     return(projectionBindingExpression.ProjectionMember != null
         ? (int)((ConstantExpression)_selectExpression.GetProjectionExpression(projectionBindingExpression.ProjectionMember)).Value
         : projectionBindingExpression.Index);
 }
コード例 #16
0
            private ProjectionExpression GetProjection(ProjectionBindingExpression projectionBindingExpression)
            {
                var index = GetProjectionIndex(projectionBindingExpression);

                return(_selectExpression.Projection[index]);
            }
コード例 #17
0
 private int GetProjectionIndex(ProjectionBindingExpression projectionBindingExpression)
 => projectionBindingExpression.ProjectionMember != null
         ? (int)((ConstantExpression)_selectExpression.GetMappedProjection(projectionBindingExpression.ProjectionMember)).Value
         : projectionBindingExpression.Index
 ?? throw new InvalidOperationException(CoreStrings.QueryFailed(projectionBindingExpression.Print(), GetType().Name));
コード例 #18
0
 private int GetProjectionIndex(ProjectionBindingExpression projectionBindingExpression)
 => projectionBindingExpression.ProjectionMember != null
         ? _selectExpression.GetMappedProjection(projectionBindingExpression.ProjectionMember).GetConstantValue <int>()
         : projectionBindingExpression.Index
 ?? throw new InvalidOperationException(CoreStrings.TranslationFailed(projectionBindingExpression.Print()));
 private int GetProjectionIndex(ProjectionBindingExpression projectionBindingExpression)
 => projectionBindingExpression.ProjectionMember != null
         ? _selectExpression.GetMappedProjection(projectionBindingExpression.ProjectionMember).GetConstantValue <int>()
         : (projectionBindingExpression.Index
コード例 #20
0
 /// <summary>
 ///     Creates a new expression that is like this one, but using the supplied children. If all of the children are the same, it will
 ///     return this expression.
 /// </summary>
 /// <param name="projectionBindingExpression">The <see cref="ProjectionBindingExpression" /> property of the result.</param>
 /// <returns>This expression if no children changed, or an expression with the updated children.</returns>
 public virtual CollectionResultExpression Update(ProjectionBindingExpression projectionBindingExpression)
 => projectionBindingExpression != ProjectionBindingExpression
         ? new CollectionResultExpression(projectionBindingExpression, Navigation, ElementType)
         : this;
 private int GetProjectionIndex(ProjectionBindingExpression projectionBindingExpression)
 => projectionBindingExpression.ProjectionMember != null
         ? (int)((ConstantExpression)_selectExpression.GetMappedProjection(projectionBindingExpression.ProjectionMember)).Value
         : projectionBindingExpression.Index ?? throw new InvalidOperationException();
コード例 #22
0
 public SqlExpression BindNavigation(INavigation navigation, ProjectionBindingExpression projectionBindingExpression)
 => ((EntityProjectionExpression)_projectionMapping[projectionBindingExpression.ProjectionMember])
 .GetNavigation(navigation);
コード例 #23
0
 private Expression GenerateKey(ProjectionBindingExpression projectionBindingExpression)
 => _queryExpression != null &&
 projectionBindingExpression.ProjectionMember != null
             ? _queryExpression.GetMappedProjection(projectionBindingExpression.ProjectionMember)
             : projectionBindingExpression;
コード例 #24
0
 public SqlExpression BindProperty(IProperty property, ProjectionBindingExpression projectionBindingExpression)
 => ((EntityProjectionExpression)_projectionMapping[projectionBindingExpression.ProjectionMember])
 .GetProperty(property);
 protected override ProjectionExpression GetProjection(ProjectionBindingExpression projectionBindingExpression)
 => _selectExpression.Projection[GetProjectionIndex(projectionBindingExpression)];
 private object GetProjectionIndex(ProjectionBindingExpression projectionBindingExpression)
 => projectionBindingExpression.ProjectionMember != null
         ? ((ConstantExpression)_selectExpression.GetMappedProjection(projectionBindingExpression.ProjectionMember)).Value
         : (projectionBindingExpression.Index != null
コード例 #27
0
 public SqlExpression BindProperty(ProjectionBindingExpression projectionBindingExpression, IProperty property)
 {
     return(((EntityProjectionExpression)_projectionMapping[projectionBindingExpression.ProjectionMember])
            .GetProperty(property));
 }
コード例 #28
0
 private Expression GenerateKey(ProjectionBindingExpression projectionBindingExpression)
 {
     return(projectionBindingExpression.ProjectionMember != null
         ? _selectExpression.GetProjectionExpression(projectionBindingExpression.ProjectionMember)
         : projectionBindingExpression);
 }
 protected abstract ProjectionExpression GetProjection(ProjectionBindingExpression projectionBindingExpression);