예제 #1
0
        private string BindOperator(UnaryOperatorQueryNode unaryOperatorNode)
        {
            var operand = unaryOperatorNode.Operand is ConstantQueryNode
                              ? MakeSparqlConstant((unaryOperatorNode.Operand as ConstantQueryNode).Value)
                              : ProcessNode(unaryOperatorNode.Operand);

            switch (unaryOperatorNode.OperatorKind)
            {
            case UnaryOperatorKind.Not:
                return("!(" + operand + ")");

            case UnaryOperatorKind.Negate:
                return("-" + operand + "");

            default:
                throw new NotImplementedException("No support for unary operator " + unaryOperatorNode.OperatorKind);
            }
        }
예제 #2
0
        private Expression BindUnaryOperatorQueryNode(UnaryOperatorQueryNode unaryOperatorQueryNode)
        {
            // No need to handle null-propagation here as CLR already handles it.
            // !(null) = null
            // -(null) = null
            Expression inner = Bind(unaryOperatorQueryNode.Operand);

            switch (unaryOperatorQueryNode.OperatorKind)
            {
            case UnaryOperatorKind.Negate:
                return(Expression.Negate(inner));

            case UnaryOperatorKind.Not:
                return(Expression.Not(inner));

            default:
                throw Error.NotSupported(SRResources.QueryNodeBindingNotSupported, unaryOperatorQueryNode.Kind, typeof(FilterBinder).Name);
            }
        }
        /// <summary>
        /// Translates a unary operator node.
        /// </summary>
        /// <param name="unaryOperatorNode">The unary operator node to translate.</param>
        /// <returns>Expression which evaluates to the result of the unary operator.</returns>
        protected virtual Expression TranslateUnaryOperator(UnaryOperatorQueryNode unaryOperatorNode)
        {
            ExceptionUtils.CheckArgumentNotNull(unaryOperatorNode, "unaryOperatorNode");
            ExceptionUtils.CheckArgumentNotNull(unaryOperatorNode.Operand, "unaryOperatorNode.Operand");

            Expression operand = this.Translate(unaryOperatorNode.Operand);

            // If the operand statically is the null literal we optimize the operator to result in the 
            // null literal as well (per specification) because we otherwise cannot execute such an expression tree
            if (operand == nullLiteralExpression)
            {
                return nullLiteralExpression;
            }

            // throw if no type is available
            if (unaryOperatorNode.TypeReference == null)
            {
                throw new ODataException(Strings.QueryExpressionTranslator_SingleValueQueryNodeWithoutTypeReference(unaryOperatorNode.Kind));
            }

            // TODO: deal with null propagation if necessary
            // TODO: deal with open types
            switch (unaryOperatorNode.OperatorKind)
            {
                case UnaryOperatorKind.Negate:
                    return Expression.Negate(operand);

                case UnaryOperatorKind.Not:
                    if (operand.Type == typeof(bool) || operand.Type == typeof(bool?))
                    {
                        // Expression.Not will take numerics and apply '~' to them, thus the extra check here.
                        return Expression.Not(operand);
                    }
                    else
                    {
                        throw new ODataException(Strings.QueryExpressionTranslator_UnaryNotOperandNotBoolean(operand.Type));
                    }

                default:
                    throw new ODataException(Strings.General_InternalError(InternalErrorCodes.QueryExpressionTranslator_TranslateUnaryOperator_UnreachableCodepath));
            }
        }
예제 #4
0
 private string BindOperator(UnaryOperatorQueryNode unaryOperatorNode)
 {
     var operand = unaryOperatorNode.Operand is ConstantQueryNode
                       ? MakeSparqlConstant((unaryOperatorNode.Operand as ConstantQueryNode).Value)
                       : ProcessNode(unaryOperatorNode.Operand);
     switch (unaryOperatorNode.OperatorKind)
     {
         case UnaryOperatorKind.Not:
             return "!(" + operand + ")";
         case UnaryOperatorKind.Negate:
             return "-" + operand + "";
         default:
             throw new NotImplementedException("No support for unary operator " + unaryOperatorNode.OperatorKind);
     }
 }