/// <summary>
        ///     Adds the specified property to the projection list.
        /// </summary>
        /// <param name="list">
        ///     The projection list.
        /// </param>
        /// <param name="property">
        ///     The property.
        /// </param>
        /// <param name="data">
        ///     A <see cref="QueryHelperData" /> instance.
        /// </param>
        /// <typeparam name="TSource">
        ///     The <see cref="System.Type" /> of the source entity.
        /// </typeparam>
        /// <typeparam name="TProperty">
        ///     The <see cref="System.Type" /> of the property.
        /// </typeparam>
        /// <returns>
        ///     The <see cref="ProjectionList" /> instance.
        /// </returns>
        /// <exception cref="ArgumentNullException">
        ///     <paramref name="list" /> is null.
        /// </exception>
        public static ProjectionList AddProperty <TSource, TProperty>
        (
            this ProjectionList list,
            Expression <Func <TSource, TProperty> > property,
            QueryHelperData data
        )
        {
            if (list == null)
            {
                throw new ArgumentNullException("list");
            }

            IProjection projection = ProjectionHelper.GetProjection(property, data);

            string alias = null;

            var propertyProjection = projection as IPropertyProjection;

            if (propertyProjection != null)
            {
                alias = propertyProjection.PropertyName;
            }

            list.Add(projection, alias);

            return(list);
        }
Ejemplo n.º 2
0
        /// <inheritdoc />
        public virtual TQuery GroupBy(Expression <Func <TSource, object> > property)
        {
            if (property != null)
            {
                IProjection projection = ProjectionHelper
                                         .GetProjection
                                         (
                    property.Body,
                    new HelperContext(Data, property, HelperType.GroupBy)
                                         );

                if (projection != null)
                {
                    if (projection.IsGrouped || projection.IsAggregate)
                    {
                        throw new InvalidOperationException(
                                  "Cannot use an aggregate or grouped projection with GroupBy");
                    }

                    GroupBys.Add(new FqGroupByProjection(projection, false));
                }
            }

            return(Query);
        }
Ejemplo n.º 3
0
 /// <inheritdoc />
 public override IProjection Project
 (
     Expression expression,
     HelperContext context
 )
 {
     return(ProjectionHelper.GetProjection(((LambdaExpression)expression).Body, context));
 }
Ejemplo n.º 4
0
            public IProjection Project
            (
                Expression expression,
                HelperContext context
            )
            {
                var methodCall = (MethodCallExpression)expression;

                return(ProjectionHelper.GetProjection(methodCall.Object, context));
            }
Ejemplo n.º 5
0
        /// <inheritdoc />
        public override IProjection Project
        (
            Expression expression,
            HelperContext context
        )
        {
            var memberInit = (MemberInitExpression)expression;

            var list = Projections.ProjectionList();

            var newProjection = ProjectionHelper.GetProjection(memberInit.NewExpression, context)
                                as ProjectionList;

            if (newProjection != null)
            {
                for (int i = 0; i < newProjection.Length; i++)
                {
                    list.Add(newProjection[i]);
                }
            }

            foreach (MemberBinding memberBinding in memberInit.Bindings)
            {
                var memberAssigment = memberBinding as MemberAssignment;

                if (memberAssigment != null)
                {
                    IProjection projection = ProjectionHelper.GetProjection(memberAssigment.Expression, context);

                    var innerList = projection as ProjectionList;

                    if (innerList != null)
                    {
                        for (int i = 0; i < innerList.Length; i++)
                        {
                            list.Add(innerList[i]);
                        }
                    }
                    else
                    {
                        string member = memberAssigment.Member.Name;

                        list.Add(new FqAliasProjection(projection, member));

                        if (!context.Data.Mappings.ContainsKey(member))
                        {
                            context.Data.Mappings.Add(member, projection);
                        }
                    }
                }
            }

            return(list);
        }
        /// <inheritdoc />
        public override IProjection Project(Expression expression, HelperContext context)
        {
            var condition = (ConditionalExpression)expression;

            return(Projections
                   .Conditional
                   (
                       RestrictionHelper.GetCriterion(condition.Test, context),
                       ProjectionHelper.GetProjection(condition.IfTrue, context),
                       ProjectionHelper.GetProjection(condition.IfFalse, context)
                   ));
        }
Ejemplo n.º 7
0
        /// <inheritdoc />
        public override IProjection Project
        (
            Expression expression,
            HelperContext context
        )
        {
            var binary = (BinaryExpression)expression;

            IProjection original = ProjectionHelper.GetProjection(binary.Left, context);
            IProjection fallback = ProjectionHelper.GetProjection(binary.Right, context);

            return(Projections.Conditional(Restrictions.IsNull(original), fallback, original));
        }
        /// <inheritdoc />
        public override IProjection Project
        (
            Expression expression,
            HelperContext context
        )
        {
            var methodCall = (MethodCallExpression)expression;

            Expression subExpression = methodCall.Object ?? methodCall.Arguments[0];

            IProjection projection = ProjectionHelper.GetProjection(subExpression, context);

            return(ProjectCore(methodCall, subExpression, projection, context));
        }
Ejemplo n.º 9
0
        /// <inheritdoc />
        public virtual ISelectSetup <TSource, TDestination> Use <TProjection>
        (
            Expression <Func <TSource, TProjection> > expression
        )
        {
            if (expression == null)
            {
                throw new ArgumentNullException("expression");
            }

            IProjection projection = ProjectionHelper
                                     .GetProjection(expression, Data);

            return(Use(projection));
        }
Ejemplo n.º 10
0
        /// <inheritdoc />
        public virtual TQuery OrderBy
        (
            Expression <Func <TSource, object> > property,
            bool ascending = true
        )
        {
            IProjection projection = ProjectionHelper
                                     .GetProjection
                                     (
                property.Body,
                new HelperContext(Data, property, HelperType.Order)
                                     );

            return(OrderBy(projection, ascending));
        }
Ejemplo n.º 11
0
        /// <inheritdoc />
        public override IProjection Project
        (
            Expression expression,
            HelperContext context
        )
        {
            string operation = GetArithmeticOperation(expression.NodeType);

            var binary = (BinaryExpression)expression;

            return(new SqlFunctionProjection
                   (
                       new VarArgsSQLFunction("(", operation, ")"),
                       NHibernateUtil.GuessType(binary.Left.Type),
                       ProjectionHelper.GetProjection(binary.Left, context),
                       ProjectionHelper.GetProjection(binary.Right, context)
                   ));
        }
        /// <inheritdoc />
        public override IProjection Project
        (
            Expression expression,
            HelperContext context
        )
        {
            var projections = new List <IProjection>();

            var binary = (BinaryExpression)expression;

            foreach (Expression expressionPart in binary.Flatten())
            {
                IProjection projection = ProjectionHelper.GetProjection(expressionPart, context);

                projections.Add(projection);
            }

            return(new SqlFunctionProjection("concat", NHibernateUtil.String, projections.ToArray()));
        }
Ejemplo n.º 13
0
        /// <inheritdoc />
        public override IProjection Project
        (
            Expression expression,
            HelperContext context
        )
        {
            var unary = (UnaryExpression)expression;

            IProjection projection = ProjectionHelper.GetProjection(unary.Operand, context);

            if (!unary.IsLiftedToNull)
            {
                IType type = TypeHelper.GuessType(unary.Type, true);

                if (type != null)
                {
                    return(new FqCastProjection(type, projection));
                }
            }

            return(projection);
        }
Ejemplo n.º 14
0
        /// <inheritdoc />
        protected override IProjection ProjectCore
        (
            MethodCallExpression expression,
            Expression subExpression,
            IProjection projection,
            HelperContext context
        )
        {
            var fromExpression = ExpressionHelper.GetValue <LambdaExpression>(expression.Arguments[1]);

            if (fromExpression == null)
            {
                return(null);
            }

            string tempRoot = context.RootAlias;

            context.RootAlias = fromExpression.Parameters[0].Name;

            if (context.Data.Aliases.ContainsValue(context.RootAlias))
            {
                // if the parameter to the mapping expression is the same as a joined alias we do not want it to be
                // treated as the "expected root" as it would cause properties in the map to be resolved from the query
                // root entity, when the map might actually be for a joined alias.
                context.RootAlias = "null";
            }

            //// TODO: Might want to check if (original) rootName != alias (e.g. expression.Arguments[0])
            //// if that is the case we might have to change the parameter name in the mapping expression so that the
            //// projections can be properly resolved.
            IProjection result = ProjectionHelper
                                 .GetProjection(fromExpression, context);

            context.RootAlias = tempRoot;

            return(result);
        }
        public IProjection Project
        (
            Expression expression,
            HelperContext context
        )
        {
            var methodCall = (MethodCallExpression)expression;

            // return null if called statically
            if (methodCall.Object == null)
            {
                return(null);
            }

            // return null if called for non-ToString method
            if (methodCall.Method.Name != "ToString")
            {
                return(null);
            }

            // resolve a projection for the property
            IProjection property = ProjectionHelper
                                   .GetProjection(methodCall.Object, context);

            // return null if no projection could be resolved
            if (property == null)
            {
                return(null);
            }

            // create a cast projection property
            return(new CastProjection
                   (
                       NHibernateUtil.String,
                       property
                   ));
        }
Ejemplo n.º 16
0
        /// <summary>
        ///     Specifies an expression to project.
        /// </summary>
        /// <param name="expression">
        ///     The expression.
        /// </param>
        /// <typeparam name="TDestination">
        ///     The <see cref="System.Type" /> of the result.
        /// </typeparam>
        /// <returns>
        ///     The <see cref="T:TQuery" />.
        /// </returns>
        /// <exception cref="ArgumentNullException">
        ///     <paramref name="expression" /> is null.
        /// </exception>
        protected virtual TQuery Project <TDestination>(Expression <Func <TSource, TDestination> > expression)
        {
            if (expression == null)
            {
                throw new ArgumentNullException("expression");
            }

            IProjection list = ProjectionHelper
                               .GetProjection
                               (
                expression,
                Data
                               );

            if (list == null || (list is ProjectionList && ((ProjectionList)list).Length == 0))
            {
                throw new NotSupportedException
                      (
                          "The provided expression contains unsupported features please revise your code."
                      );
            }

            return(ProjectionBase <TDestination>(list, Data.Mappings, expression, false));
        }