internal static ICriterion IsDescendantOf(MethodCallExpression methodCallExpression)
        {
            var projection = ExpressionProcessor.FindMemberProjection(methodCallExpression.Arguments[0]).AsProjection();
            var value      = (string)ExpressionProcessor.FindValue(methodCallExpression.Arguments[1]);

            return(new IsDescendantOfExpression(projection, value));
        }
示例#2
0
        internal static IProjection ProcessCoalesce(MethodCallExpression methodCallExpression)
        {
            IProjection property             = ExpressionProcessor.FindMemberProjection(methodCallExpression.Arguments[0]).AsProjection();
            object      replaceValueIfIsNull = ExpressionProcessor.FindValue(methodCallExpression.Arguments[1]);

            return(Projections.SqlFunction("coalesce", NHibernateUtil.Object, property, Projections.Constant(replaceValueIfIsNull)));
        }
        public static ICriterion ProcessIsLike(MethodCallExpression methodCallExpression)
        {
            string property = ExpressionProcessor.FindMemberExpression(methodCallExpression.Arguments[0]);
            object value    = ExpressionProcessor.FindValue(methodCallExpression.Arguments[1]);

            return(Restrictions.Like(property, value));
        }
        public static ICriterion ProcessIsInCollection(MethodCallExpression methodCallExpression)
        {
            ExpressionProcessor.ProjectionInfo projection = ExpressionProcessor.FindMemberProjection(methodCallExpression.Arguments[0]);
            ICollection values = (ICollection)ExpressionProcessor.FindValue(methodCallExpression.Arguments[1]);

            return(projection.Create <ICriterion>(s => Restrictions.In(s, values), p => Restrictions.In(p, values)));
        }
        public static ICriterion ProcessIsInCollection(MethodCallExpression methodCallExpression)
        {
            string      property = ExpressionProcessor.FindMemberExpression(methodCallExpression.Arguments[0]);
            ICollection values   = (ICollection)ExpressionProcessor.FindValue(methodCallExpression.Arguments[1]);

            return(Restrictions.In(property, values));
        }
        public static ICriterion ProcessIsLike(MethodCallExpression methodCallExpression)
        {
            ExpressionProcessor.ProjectionInfo projection = ExpressionProcessor.FindMemberProjection(methodCallExpression.Arguments[0]);
            object value = ExpressionProcessor.FindValue(methodCallExpression.Arguments[1]);

            return(projection.CreateCriterion(Restrictions.Like, Restrictions.Like, value));
        }
示例#7
0
        internal static IProjection ProcessMod(MethodCallExpression methodCallExpression)
        {
            IProjection property = ExpressionProcessor.FindMemberProjection(methodCallExpression.Arguments[0]).AsProjection();
            object      divisor  = ExpressionProcessor.FindValue(methodCallExpression.Arguments[1]);

            return(Projections.SqlFunction("mod", NHibernateUtil.Int32, property, Projections.Constant(divisor)));
        }
示例#8
0
        internal static IProjection ProcessCharIndex(MethodCallExpression methodCallExpression)
        {
            IProjection property      = ExpressionProcessor.FindMemberProjection(methodCallExpression.Arguments[0]).AsProjection();
            object      theChar       = ExpressionProcessor.FindValue(methodCallExpression.Arguments[1]);
            object      startLocation = ExpressionProcessor.FindValue(methodCallExpression.Arguments[2]);

            return(Projections.SqlFunction("locate", NHibernateUtil.String, Projections.Constant(theChar), property, Projections.Constant(startLocation)));
        }
示例#9
0
        internal static IProjection ProcessSubstr(MethodCallExpression methodCallExpression)
        {
            IProjection property   = ExpressionProcessor.FindMemberProjection(methodCallExpression.Arguments[0]).AsProjection();
            object      startIndex = ExpressionProcessor.FindValue(methodCallExpression.Arguments[1]);
            object      length     = ExpressionProcessor.FindValue(methodCallExpression.Arguments[2]);

            return(Projections.SqlFunction("substring", NHibernateUtil.String, property, Projections.Constant(startIndex), Projections.Constant(length)));
        }
        public static ICriterion ProcessIsLikeMatchMode(MethodCallExpression methodCallExpression)
        {
            string    property  = ExpressionProcessor.FindMemberExpression(methodCallExpression.Arguments[0]);
            string    value     = (string)ExpressionProcessor.FindValue(methodCallExpression.Arguments[1]);
            MatchMode matchMode = (MatchMode)ExpressionProcessor.FindValue(methodCallExpression.Arguments[2]);

            return(Restrictions.Like(property, value, matchMode));
        }
        public static ICriterion ProcessIsLikeMatchMode(MethodCallExpression methodCallExpression)
        {
            ExpressionProcessor.ProjectionInfo projection = ExpressionProcessor.FindMemberProjection(methodCallExpression.Arguments[0]);
            string    value     = (string)ExpressionProcessor.FindValue(methodCallExpression.Arguments[1]);
            MatchMode matchMode = (MatchMode)ExpressionProcessor.FindValue(methodCallExpression.Arguments[2]);

            return(projection.Create <ICriterion>(s => Restrictions.Like(s, value, matchMode), p => Restrictions.Like(p, value, matchMode)));
        }
        public static ICriterion ProcessIsBetween(MethodCallExpression methodCallExpression)
        {
            MethodCallExpression betweenFunction = (MethodCallExpression)methodCallExpression.Object;
            string property = ExpressionProcessor.FindMemberExpression(betweenFunction.Arguments[0]);
            object lo       = ExpressionProcessor.FindValue(betweenFunction.Arguments[1]);
            object hi       = ExpressionProcessor.FindValue(methodCallExpression.Arguments[0]);

            return(Restrictions.Between(property, lo, hi));
        }
        public static ICriterion ProcessIsBetween(MethodCallExpression methodCallExpression)
        {
            MethodCallExpression betweenFunction = (MethodCallExpression)methodCallExpression.Object;

            ExpressionProcessor.ProjectionInfo projection = ExpressionProcessor.FindMemberProjection(betweenFunction.Arguments[0]);
            object lo = ExpressionProcessor.FindValue(betweenFunction.Arguments[1]);
            object hi = ExpressionProcessor.FindValue(methodCallExpression.Arguments[0]);

            return(projection.Create <ICriterion>(s => Restrictions.Between(s, lo, hi), p => Restrictions.Between(p, lo, hi)));
        }
 private static object GetValue <T>(Expression <Func <T> > expression)
 {
     try
     {
         return(ExpressionProcessor.FindValue(expression.Body));
     }
     catch (TargetInvocationException e)
     {
         throw e.InnerException;
     }
 }
示例#15
0
        public static ICriterion ProcessCompare <T>(MethodCallExpression expression, string op, Func <object, object> normalizeValue)
        {
            var leftExpression  = expression.Object;
            var rightExpression = expression.Arguments[0];

            if (leftExpression == null)
            {
                leftExpression  = expression.Arguments[0];
                rightExpression = expression.Arguments[1];
            }

            var leftProjectionInfo  = ExpressionProcessor.FindMemberProjection(leftExpression);
            var rightProjectionInfo = ExpressionProcessor.FindMemberProjection(rightExpression);

            var rightProjection = rightProjectionInfo.AsProjection();

            if (rightProjection is ConstantProjection)
            {
                var value = ExpressionProcessor.FindValue(rightExpression);
                value = (normalizeValue == null) ? value : normalizeValue(value);
                return(leftProjectionInfo.CreateCriterion(
                           (propertyName) => Compare(propertyName, value, op),
                           (projection) => Compare(projection, value, op)));
            }

            var leftProjection = leftProjectionInfo.AsProjection();

            if (leftProjection is ConstantProjection)
            {
                var value = ExpressionProcessor.FindValue(leftExpression);
                value = (normalizeValue == null) ? value : normalizeValue(value);
                return(rightProjectionInfo.CreateCriterion(
                           (propertyName) => Compare(propertyName, value, op, true),
                           (projection) => Compare(projection, value, op, true)));
            }

            return(leftProjectionInfo.CreateCriterion(
                       rightProjectionInfo,
                       (propertyName, otherPropertyName) => CompareProperty(propertyName, otherPropertyName, op),
                       (propertyName, otherProjection) => CompareProperty(propertyName, otherProjection, op),
                       (projection, otherPropertyName) => CompareProperty(projection, otherPropertyName, op),
                       (projection, otherProjection) => CompareProperty(projection, otherProjection, op)));
        }
示例#16
0
        public static IProjection GetAncestor(MethodCallExpression methodCallExpression)
        {
            var         ishift = 0;
            IProjection property;

            if (methodCallExpression.Object == null)
            {
                property = ExpressionProcessor.FindMemberProjection(methodCallExpression.Arguments[0]).AsProjection();
            }
            else
            {
                property = ExpressionProcessor.FindMemberProjection(methodCallExpression.Object).AsProjection();
                ishift   = -1;
            }

            var level = (int)ExpressionProcessor.FindValue(methodCallExpression.Arguments[ishift + 1]);

            return(new GetAncestorProjection(property, level));
        }
示例#17
0
        public static IProjection GetReparentedValue(MethodCallExpression methodCallExpression)
        {
            var         ishift = 0;
            IProjection property;

            if (methodCallExpression.Object == null)
            {
                property = ExpressionProcessor.FindMemberProjection(methodCallExpression.Arguments[0]).AsProjection();
            }
            else
            {
                property = ExpressionProcessor.FindMemberProjection(methodCallExpression.Object).AsProjection();
                ishift   = -1;
            }

            var oldRoot = (string)ExpressionProcessor.FindValue(methodCallExpression.Arguments[ishift + 1]);
            var newRoot = (string)ExpressionProcessor.FindValue(methodCallExpression.Arguments[ishift + 2]);

            return(new GetReparentedValueProjection(property, oldRoot, newRoot));
        }
示例#18
0
        public static IProjection GetDescendant(MethodCallExpression methodCallExpression)
        {
            var         ishift = 0;
            IProjection property;

            if (methodCallExpression.Object == null)
            {
                property = ExpressionProcessor.FindMemberProjection(methodCallExpression.Arguments[0]).AsProjection();
            }
            else
            {
                property = ExpressionProcessor.FindMemberProjection(methodCallExpression.Object).AsProjection();
                ishift   = -1;
            }

            var child1 = (string)ExpressionProcessor.FindValue(methodCallExpression.Arguments[ishift + 1]);
            var child2 = (string)ExpressionProcessor.FindValue(methodCallExpression.Arguments[ishift + 2]);

            return(new GetDescendantProjection(property, child1, child2));
        }
示例#19
0
        /// <summary>
        /// Evaluates an evaluatable <see cref="Expression"/> subtree, i.e. an independent expression tree that is compilable and executable
        /// without any data being passed in. The result of the evaluation is returned as a <see cref="ConstantExpression"/>; if the subtree
        /// is already a <see cref="ConstantExpression"/>, no evaluation is performed.
        /// </summary>
        /// <param name="subtree">The subtree to be evaluated.</param>
        /// <returns>A <see cref="ConstantExpression"/> holding the result of the evaluation.</returns>
        private Expression EvaluateSubtree(Expression subtree)
        {
            if (subtree.NodeType == ExpressionType.Constant)
            {
                var constantExpression = (ConstantExpression)subtree;
                var valueAsIQueryable  = constantExpression.Value as IQueryable;
                if (valueAsIQueryable != null && valueAsIQueryable.Expression != constantExpression)
                {
                    return(valueAsIQueryable.Expression);
                }

                return(constantExpression);
            }

            try
            {
                return(Expression.Constant(ExpressionProcessor.FindValue(subtree), subtree.Type));
            }
            catch (TargetInvocationException ex)
            {
                throw ReflectHelper.UnwrapTargetInvocationException(ex);
            }
        }
示例#20
0
        public static IProjection CreateConstantProjection(Expression expression)
        {
            var value = ExpressionProcessor.FindValue(expression);

            return(new ConstantProjection(value, TypeUtil.GuessType(value)));
        }