Exemple #1
0
        private static bool IsCompare(Expression expression)
        {
            var methodCall = expression as MethodCallExpression;

            if (methodCall == null)
            {
                return(false);
            }

            return(CompareGenerator.IsCompareMethod(methodCall.Method));
        }
            protected override Expression VisitMethodCall(MethodCallExpression node)
            {
                if (VisitorUtil.IsMappedAs(node.Method))
                {
                    var rawParameter = Visit(node.Arguments[0]);
                    var parameter    = UnwrapUnary(rawParameter) as ConstantExpression;
                    var type         = node.Arguments[1] as ConstantExpression;
                    if (parameter == null)
                    {
                        throw new HibernateException(
                                  $"{nameof(LinqExtensionMethods.MappedAs)} must be called on an expression which can be evaluated as " +
                                  $"{nameof(ConstantExpression)}. It was call on {rawParameter?.GetType().Name ?? "null"} instead.");
                    }
                    if (type == null)
                    {
                        throw new HibernateException(
                                  $"{nameof(LinqExtensionMethods.MappedAs)} type must be supplied as {nameof(ConstantExpression)}. " +
                                  $"It was {node.Arguments[1]?.GetType().Name ?? "null"} instead.");
                    }

                    ConstantExpressions[parameter] = (IType)type.Value;

                    return(_removeMappedAsCalls
                                                ? rawParameter
                                                : node);
                }

                if (EqualsGenerator.Methods.Contains(node.Method) || CompareGenerator.IsCompareMethod(node.Method))
                {
                    node = (MethodCallExpression)base.VisitMethodCall(node);
                    var left  = UnwrapUnary(node.Method.IsStatic ? node.Arguments[0] : node.Object);
                    var right = UnwrapUnary(node.Method.IsStatic ? node.Arguments[1] : node.Arguments[0]);
                    AddRelatedExpression(node, left, right);
                    AddRelatedExpression(node, right, left);

                    return(node);
                }

                // For hql method generators we do not want to guess the parameter type here, let hql logic figure it out.
                if (_functionRegistry.TryGetGenerator(node.Method, out _))
                {
                    var origHqlGenerator = _hqlGenerator;
                    _hqlGenerator = true;
                    var expression = base.VisitMethodCall(node);
                    _hqlGenerator = origHqlGenerator;
                    return(expression);
                }

                return(base.VisitMethodCall(node));
            }
Exemple #3
0
            protected override Expression VisitMethodCall(MethodCallExpression node)
            {
                if (VisitorUtil.IsMappedAs(node.Method))
                {
                    var rawParameter = Visit(node.Arguments[0]);
                    var parameter    = rawParameter as ConstantExpression;
                    var type         = node.Arguments[1] as ConstantExpression;
                    if (parameter == null)
                    {
                        throw new HibernateException(
                                  $"{nameof(LinqExtensionMethods.MappedAs)} must be called on an expression which can be evaluated as " +
                                  $"{nameof(ConstantExpression)}. It was call on {rawParameter?.GetType().Name ?? "null"} instead.");
                    }
                    if (type == null)
                    {
                        throw new HibernateException(
                                  $"{nameof(LinqExtensionMethods.MappedAs)} type must be supplied as {nameof(ConstantExpression)}. " +
                                  $"It was {node.Arguments[1]?.GetType().Name ?? "null"} instead.");
                    }

                    ConstantExpressions[parameter] = (IType)type.Value;

                    return(_removeMappedAsCalls
                                                ? rawParameter
                                                : node);
                }

                if (EqualsGenerator.Methods.Contains(node.Method) || CompareGenerator.IsCompareMethod(node.Method))
                {
                    node = (MethodCallExpression)base.VisitMethodCall(node);
                    var left  = UnwrapUnary(node.Method.IsStatic ? node.Arguments[0] : node.Object);
                    var right = UnwrapUnary(node.Method.IsStatic ? node.Arguments[1] : node.Arguments[0]);
                    AddRelatedExpression(node, left, right);
                    AddRelatedExpression(node, right, left);

                    return(node);
                }

                return(base.VisitMethodCall(node));
            }