public SqlToken GenerateSqlToken(SelectCommandContext sqlCommandContext)
        {
            var result = new OrderByToken(GenerateOrderByIndex(sqlCommandContext));

            foreach (var orderByItem in sqlCommandContext.GetOrderByContext().GetItems())
            {
                string columnLabel;
                if (orderByItem.GetSegment() is ColumnOrderByItemSegment columnOrderByItemSegment)
                {
                    var            quoteCharacterEnum = columnOrderByItemSegment.GetColumn().GetIdentifier().GetQuoteCharacter();
                    QuoteCharacter quoteCharacter     = QuoteCharacter.Get(quoteCharacterEnum);
                    columnLabel = quoteCharacter.GetStartDelimiter() + columnOrderByItemSegment.GetText() + quoteCharacter.GetEndDelimiter();
                }
                else if (orderByItem.GetSegment() is ExpressionOrderByItemSegment expressionOrderByItemSegment)
                {
                    columnLabel = expressionOrderByItemSegment.GetText();
                }
                else
                {
                    columnLabel = $"{orderByItem.GetIndex()}"; // String.valueOf(each.getIndex());
                }
                result.ColumnLabels.Add(columnLabel);
                result.OrderDirections.Add(orderByItem.GetSegment().GetOrderDirection());
            }
            return(result);
        }
 public void OrderByQueryTokenDefaultTest()
 {
     var expression = new InnerPathToken(string.Empty, null, null);
     OrderByToken orderby = new OrderByToken(expression, OrderByDirection.Ascending);
     this.Assert.AreEqual(QueryTokenKind.OrderBy, orderby.Kind, "The InternalKind property has an unexpected value.");
     this.Assert.AreEqual(OrderByDirection.Ascending, orderby.Direction, "The Direction property should be Ascending.");
     this.Assert.AreEqual(expression, orderby.Expression, "The Expression property has an unexpected value.");
 }
Exemplo n.º 3
0
        public void OrderByQueryTokenDefaultTest()
        {
            var          expression = new InnerPathToken(string.Empty, null, null);
            OrderByToken orderby    = new OrderByToken(expression, OrderByDirection.Ascending);

            this.Assert.AreEqual(QueryTokenKind.OrderBy, orderby.Kind, "The InternalKind property has an unexpected value.");
            this.Assert.AreEqual(OrderByDirection.Ascending, orderby.Direction, "The Direction property should be Ascending.");
            this.Assert.AreEqual(expression, orderby.Expression, "The Expression property has an unexpected value.");
        }
Exemplo n.º 4
0
        /// <summary>
        /// Write the orderby token as URI part to this builder.
        /// </summary>
        /// <param name="orderBy">To write as URI part.</param>
        private void WriteOrderBy(OrderByToken orderBy)
        {
            ExceptionUtils.CheckArgumentNotNull(orderBy, "orderBy");

            this.WriteQuery(orderBy.Expression);
            if (orderBy.Direction == OrderByDirection.Descending)
            {
                this.builder.Append(ExpressionConstants.SymbolEscapedSpace);
                this.builder.Append(ExpressionConstants.KeywordDescending);
            }
        }
Exemplo n.º 5
0
        public void NestedOrderbyInSelectIsAllowed(string optionsText)
        {
            // Arrange & Act
            SelectTermToken selectTerm = this.ParseSelectOptions(optionsText);

            // Assert
            Assert.NotNull(selectTerm);
            Assert.NotNull(selectTerm.OrderByOptions);
            OrderByToken orderBy = Assert.Single(selectTerm.OrderByOptions);

            orderBy.Expression.ShouldBeEndPathToken("two");
        }
Exemplo n.º 6
0
        public void ParseOrderByThisInSelectWorks(string queryString, OrderByDirection orderByDirection)
        {
            // Arrange & Act
            SelectToken selectToken = ParseSelectClause(queryString);

            // Assert
            Assert.NotNull(selectToken);
            SelectTermToken selectTermToken = Assert.Single(selectToken.SelectTerms);

            selectTermToken.PathToProperty.ShouldBeNonSystemToken("Emails");
            Assert.NotNull(selectTermToken.OrderByOptions);
            OrderByToken orderBy = Assert.Single(selectTermToken.OrderByOptions);

            orderBy.Expression.ShouldBeRangeVariableToken("$this");
            Assert.Equal(orderByDirection, orderBy.Direction);
        }
Exemplo n.º 7
0
        public void OrderBySetCorrectly()
        {
            OrderByToken    orderBy    = new OrderByToken(new LiteralToken(1), OrderByDirection.Descending);
            ExpandTermToken expandTerm = new ExpandTermToken(new NonSystemToken("stuff", null, null),
                                                             null /*filterOption*/,
                                                             new OrderByToken[] { orderBy },
                                                             null /*topOption*/,
                                                             null /*skipOption*/,
                                                             null /*countQueryOption*/,
                                                             null /*levelsOption*/,
                                                             null /*searchOption*/,
                                                             null /*selectOption*/,
                                                             null /*expandOption*/);

            expandTerm.OrderByOptions.Single().Kind.Should().Be(QueryTokenKind.OrderBy);
            expandTerm.OrderByOptions.Single().Expression.ShouldBeLiteralQueryToken(1);
        }
Exemplo n.º 8
0
        public void ParseNestedOrderByAndSearchInSelectWorks()
        {
            // Arrange & Act
            SelectToken selectToken = ParseSelectClause("Address($orderby=abc;$search=xyz)");

            // Assert
            Assert.NotNull(selectToken);
            SelectTermToken selectTermToken = Assert.Single(selectToken.SelectTerms);

            selectTermToken.PathToProperty.ShouldBeNonSystemToken("Address");
            Assert.NotNull(selectTermToken.OrderByOptions);
            OrderByToken orderBy = Assert.Single(selectTermToken.OrderByOptions);

            orderBy.Expression.ShouldBeEndPathToken("abc");

            Assert.NotNull(selectTermToken.SearchOption);
            selectTermToken.SearchOption.ShouldBeStringLiteralToken("xyz");
        }
Exemplo n.º 9
0
        public void InnerOrderBySetCorrectly()
        {
            // Arrange & Act
            OrderByToken    orderBy    = new OrderByToken(new LiteralToken(42), OrderByDirection.Descending);
            SelectTermToken selectTerm = new SelectTermToken(new NonSystemToken("stuff", null, null),
                                                             null, new OrderByToken[] { orderBy }, null, null, null, null, null, null);

            // Assert
            Assert.NotNull(selectTerm.OrderByOptions);
            OrderByToken expectedOrderBy = Assert.Single(selectTerm.OrderByOptions);

            Assert.Equal(QueryTokenKind.OrderBy, expectedOrderBy.Kind);

            Assert.NotNull(expectedOrderBy.Expression);
            LiteralToken literalToken = Assert.IsType <LiteralToken>(expectedOrderBy.Expression);

            Assert.Equal(QueryTokenKind.Literal, literalToken.Kind);
            Assert.Equal(42, literalToken.Value);
        }
Exemplo n.º 10
0
        public void OrderBySetCorrectly()
        {
            // Arrange & Act
            OrderByToken    orderBy    = new OrderByToken(new LiteralToken(1), OrderByDirection.Descending);
            ExpandTermToken expandTerm = new ExpandTermToken(new NonSystemToken("stuff", null, null),
                                                             null /*filterOption*/,
                                                             new OrderByToken[] { orderBy },
                                                             null /*topOption*/,
                                                             null /*skipOption*/,
                                                             null /*countQueryOption*/,
                                                             null /*levelsOption*/,
                                                             null /*searchOption*/,
                                                             null /*selectOption*/,
                                                             null /*expandOption*/);

            // Assert
            Assert.NotNull(expandTerm.OrderByOptions);
            OrderByToken orderByToken = Assert.Single(expandTerm.OrderByOptions);

            Assert.Equal(QueryTokenKind.OrderBy, orderByToken.Kind);
            orderByToken.Expression.ShouldBeLiteralQueryToken(1);
        }
Exemplo n.º 11
0
        /// <summary>
        /// Parses the $orderby expression.
        /// </summary>
        /// <param name="orderBy">The $orderby expression string to parse.</param>
        /// <returns>The enumeraion of lexical tokens representing order by tokens.</returns>
        internal IEnumerable <OrderByToken> ParseOrderBy(string orderBy)
        {
            DebugUtils.CheckNoExternalCallers();
            Debug.Assert(orderBy != null, "orderBy != null");

            this.recursionDepth = 0;
            this.lexer          = CreateLexerForFilterOrOrderByExpression(orderBy);

            List <OrderByToken> orderByTokens = new List <OrderByToken>();

            while (true)
            {
                QueryToken expression = this.ParseExpression();
                bool       ascending  = true;
                if (this.TokenIdentifierIs(ExpressionConstants.KeywordAscending))
                {
                    this.lexer.NextToken();
                }
                else if (this.TokenIdentifierIs(ExpressionConstants.KeywordDescending))
                {
                    this.lexer.NextToken();
                    ascending = false;
                }

                OrderByToken orderByToken = new OrderByToken(expression, ascending ? OrderByDirection.Ascending : OrderByDirection.Descending);
                orderByTokens.Add(orderByToken);
                if (this.lexer.CurrentToken.Kind != ExpressionTokenKind.Comma)
                {
                    break;
                }

                this.lexer.NextToken();
            }

            this.lexer.ValidateToken(ExpressionTokenKind.End);

            return(new ReadOnlyCollection <OrderByToken>(orderByTokens));
        }
Exemplo n.º 12
0
        /// <summary>
        /// Processes the specified order-by token.
        /// </summary>
        /// <param name="state">State to use for binding.</param>
        /// <param name="thenBy"> The next OrderBy node, or null if there is no orderby after this.</param>
        /// <param name="orderByToken">The order-by token to bind.</param>
        /// <returns>Returns the combined entityCollection including the ordering.</returns>
        private OrderByClause ProcessSingleOrderBy(BindingState state, OrderByClause thenBy, OrderByToken orderByToken)
        {
            ExceptionUtils.CheckArgumentNotNull(state, "state");
            ExceptionUtils.CheckArgumentNotNull(orderByToken, "orderByToken");

            QueryNode expressionNode = this.bindMethod(orderByToken.Expression);

            // The order-by expressions need to be primitive / enumeration types
            SingleValueNode expressionResultNode = expressionNode as SingleValueNode;
            if (expressionResultNode == null ||
                (expressionResultNode.TypeReference != null && !expressionResultNode.TypeReference.IsODataPrimitiveTypeKind() && !expressionResultNode.TypeReference.IsODataEnumTypeKind() && !expressionResultNode.TypeReference.IsODataTypeDefinitionTypeKind()))
            {
                throw new ODataException(ODataErrorStrings.MetadataBinder_OrderByExpressionNotSingleValue);
            }

            OrderByClause orderByNode = new OrderByClause(
                thenBy,
                expressionResultNode,
                orderByToken.Direction,
                state.ImplicitRangeVariable);

            return orderByNode;
        }
 /// <inheritdoc />
 public void OrderByDistanceDescending(string fieldName, double latitude, double longitude)
 {
     OrderByTokens.AddLast(OrderByToken.CreateDistanceDescending(fieldName, AddQueryParameter(latitude), AddQueryParameter(longitude)));
 }
 /// <inheritdoc />
 public void OrderByDistanceDescending(string fieldName, string shapeWkt)
 {
     OrderByTokens.AddLast(OrderByToken.CreateDistanceDescending(fieldName, AddQueryParameter(shapeWkt)));
 }
Exemplo n.º 15
0
 public void OrderBySetCorrectly()
 {
     OrderByToken orderBy = new OrderByToken(new LiteralToken(1), OrderByDirection.Descending);
     ExpandTermToken expandTerm = new ExpandTermToken(new NonSystemToken("stuff", null, null),
                                                      null /*filterOption*/,
                                                      new OrderByToken[] { orderBy },
                                                      null /*topOption*/,
                                                      null /*skipOption*/,
                                                      null /*countQueryOption*/,
                                                      null /*levelsOption*/,
                                                      null /*searchOption*/,
                                                      null /*selectOption*/,
                                                      null /*expandOption*/);
     expandTerm.OrderByOptions.Single().Kind.Should().Be(QueryTokenKind.OrderBy);
     expandTerm.OrderByOptions.Single().Expression.ShouldBeLiteralQueryToken(1);
 }
Exemplo n.º 16
0
 private static void VerifyOrderByQueryTokensAreEqual(OrderByToken expected, OrderByToken actual, AssertionHandler assert)
 {
     assert.AreEqual(expected.Direction, actual.Direction, "The Direction of the order by doesn't match the expected one.");
     VerifyQueryTokensAreEqual(expected.Expression, actual.Expression, assert);
 }
Exemplo n.º 17
0
        /// <summary>
        /// Processes the specified order-by token.
        /// </summary>
        /// <param name="state">State to use for binding.</param>
        /// <param name="thenBy"> The next OrderBy node, or null if there is no orderby after this.</param>
        /// <param name="orderByToken">The order-by token to bind.</param>
        /// <returns>Returns the combined entityCollection including the ordering.</returns>
        private OrderByClause ProcessSingleOrderBy(BindingState state, OrderByClause thenBy, OrderByToken orderByToken)
        {
            ExceptionUtils.CheckArgumentNotNull(state, "state");
            ExceptionUtils.CheckArgumentNotNull(orderByToken, "orderByToken");

            QueryNode expressionNode = this.bindMethod(orderByToken.Expression);

            // The order-by expressions need to be primitive / enumeration types
            SingleValueNode expressionResultNode = expressionNode as SingleValueNode;

            if (expressionResultNode == null ||
                (expressionResultNode.TypeReference != null && !expressionResultNode.TypeReference.IsODataPrimitiveTypeKind() && !expressionResultNode.TypeReference.IsODataEnumTypeKind()))
            {
                throw new ODataException(ODataErrorStrings.MetadataBinder_OrderByExpressionNotSingleValue);
            }

            OrderByClause orderByNode = new OrderByClause(
                thenBy,
                expressionResultNode,
                orderByToken.Direction,
                state.ImplicitRangeVariable);

            return(orderByNode);
        }
Exemplo n.º 18
0
 /// <summary>
 /// Visits an OrderByToken
 /// </summary>
 /// <param name="tokenIn">The OrderByToken to bind</param>
 /// <returns>An OrderByClause bound to this OrderByToken</returns>
 public virtual T Visit(OrderByToken tokenIn)
 {
     throw new NotImplementedException();
 }
Exemplo n.º 19
0
 private static void VerifyOrderByQueryTokensAreEqual(OrderByToken expected, OrderByToken actual, AssertionHandler assert)
 {
     assert.AreEqual(expected.Direction, actual.Direction, "The Direction of the order by doesn't match the expected one.");
     VerifyQueryTokensAreEqual(expected.Expression, actual.Expression, assert);
 }
Exemplo n.º 20
0
        /// <inheritdoc />
        public void OrderByDistanceDescending(string fieldName, double latitude, double longitude, double roundFactor)
        {
            var roundFactorParameterName = roundFactor == 0 ? null : AddQueryParameter(roundFactor);

            OrderByTokens.AddLast(OrderByToken.CreateDistanceDescending(fieldName, AddQueryParameter(latitude), AddQueryParameter(longitude), roundFactorParameterName));
        }
        /// <summary>
        /// Build the list of expand options
        /// Depends on whether options are allowed or not.
        /// </summary>
        /// <param name="isInnerTerm">is this an inner expand term</param>
        /// <param name="pathToken">the current level token, as a PathToken</param>
        /// <returns>An expand term token based on the path token, and all available expand options.</returns>
        internal override ExpandTermToken BuildExpandTermToken(bool isInnerTerm, PathSegmentToken pathToken)
        {
            DebugUtils.CheckNoExternalCallers();

            QueryToken      filterOption      = null;
            OrderByToken    orderByOption     = null;
            long?           topOption         = null;
            long?           skipOption        = null;
            InlineCountKind?inlineCountOption = null;
            SelectToken     selectOption      = null;
            ExpandToken     expandOption      = null;

            if (this.Lexer.CurrentToken.Kind == ExpressionTokenKind.OpenParen)
            {
                // could have a filter, orderby, etc. or another select and expand.
                while (this.Lexer.PeekNextToken().Kind != ExpressionTokenKind.CloseParen)
                {
                    switch (this.Lexer.NextToken().Text)
                    {
                    case ExpressionConstants.QueryOptionFilter:
                    {
                        // advance to the equal sign
                        this.Lexer.NextToken();
                        string filterText = this.ReadQueryOption();
                        UriQueryExpressionParser filterParser = new UriQueryExpressionParser(this.MaxDepth);
                        filterOption = filterParser.ParseFilter(filterText);
                        break;
                    }

                    case ExpressionConstants.QueryOptionOrderby:
                    {
                        // advance to the equal sign
                        this.Lexer.NextToken();
                        string orderByText = this.ReadQueryOption();
                        UriQueryExpressionParser orderbyParser = new UriQueryExpressionParser(this.MaxDepth);
                        orderByOption = orderbyParser.ParseOrderBy(orderByText).Single();
                        break;
                    }

                    case ExpressionConstants.QueryOptionTop:
                    {
                        // advance to the equal sign
                        this.Lexer.NextToken();
                        string topText = this.ReadQueryOption();

                        // TryParse requires a non-nullable long.
                        long top;
                        if (!long.TryParse(topText, out top))
                        {
                            throw new ODataException(ODataErrorStrings.UriSelectParser_InvalidTopOption(topText));
                        }

                        topOption = top;

                        break;
                    }

                    case ExpressionConstants.QueryOptionSkip:
                    {
                        // advance to the equal sign
                        this.Lexer.NextToken();
                        string skipText = this.ReadQueryOption();

                        // TryParse requires a non-nullable long.
                        long skip;
                        if (!long.TryParse(skipText, out skip))
                        {
                            throw new ODataException(ODataErrorStrings.UriSelectParser_InvalidSkipOption(skipText));
                        }

                        skipOption = skip;

                        break;
                    }

                    case ExpressionConstants.QueryOptionInlineCount:
                    {
                        // advance to the equal sign
                        this.Lexer.NextToken();
                        string inlineCountText = this.ReadQueryOption();
                        switch (inlineCountText)
                        {
                        case ExpressionConstants.InlineCountNone:
                        {
                            inlineCountOption = InlineCountKind.None;
                            break;
                        }

                        case ExpressionConstants.InlineCountAllPages:
                        {
                            inlineCountOption = InlineCountKind.AllPages;
                            break;
                        }

                        default:
                        {
                            throw new ODataException(ODataErrorStrings.UriSelectParser_TermIsNotValid(this.Lexer.ExpressionText));
                        }
                        }

                        break;
                    }

                    case ExpressionConstants.QueryOptionSelect:
                    {
                        // advance to the equal sign
                        this.Lexer.NextToken();
                        selectOption = this.ParseSelect();
                        break;
                    }

                    case ExpressionConstants.QueryOptionExpand:
                    {
                        // advance to the equal sign
                        this.Lexer.NextToken();
                        expandOption = this.ParseExpand();
                        break;
                    }

                    default:
                    {
                        throw new ODataException(ODataErrorStrings.UriSelectParser_TermIsNotValid(this.Lexer.ExpressionText));
                    }
                    }
                }
            }
            else if (this.IsNotEndOfTerm(isInnerTerm))
            {
                throw new ODataException(ODataErrorStrings.UriSelectParser_TermIsNotValid(this.Lexer.ExpressionText));
            }

            return(new ExpandTermToken(pathToken, filterOption, orderByOption, topOption, skipOption, inlineCountOption, selectOption, expandOption));
        }
Exemplo n.º 22
0
        /// <inheritdoc />
        public void OrderByDistanceDescending(string fieldName, string shapeWkt, double roundFactor)
        {
            string factorParamName = roundFactor == 0 ? null : AddQueryParameter(roundFactor);

            OrderByTokens.AddLast(OrderByToken.CreateDistanceDescending(fieldName, AddQueryParameter(shapeWkt), factorParamName));
        }
Exemplo n.º 23
0
        /// <summary>
        /// Parses the $orderby expression.
        /// </summary>
        /// <param name="orderBy">The $orderby expression string to parse.</param>
        /// <returns>The enumeraion of lexical tokens representing order by tokens.</returns>
        internal IEnumerable<OrderByToken> ParseOrderBy(string orderBy)
        {
            Debug.Assert(orderBy != null, "orderBy != null");

            this.recursionDepth = 0;
            this.lexer = CreateLexerForFilterOrOrderByOrApplyExpression(orderBy);

            List<OrderByToken> orderByTokens = new List<OrderByToken>();
            while (true)
            {
                QueryToken expression = this.ParseExpression();
                bool ascending = true;
                if (this.TokenIdentifierIs(ExpressionConstants.KeywordAscending))
                {
                    this.lexer.NextToken();
                }
                else if (this.TokenIdentifierIs(ExpressionConstants.KeywordDescending))
                {
                    this.lexer.NextToken();
                    ascending = false;
                }

                OrderByToken orderByToken = new OrderByToken(expression, ascending ? OrderByDirection.Ascending : OrderByDirection.Descending);
                orderByTokens.Add(orderByToken);
                if (this.lexer.CurrentToken.Kind != ExpressionTokenKind.Comma)
                {
                    break;
                }

                this.lexer.NextToken();
            }

            this.lexer.ValidateToken(ExpressionTokenKind.End);

            return new ReadOnlyCollection<OrderByToken>(orderByTokens);
        }
 public Expression Visit(OrderByToken tokenIn)
 {
     throw new NotImplementedException();
 }
 public bool Visit(OrderByToken tokenIn)
 {
     throw new NotSupportedException(string.Format(CultureInfo.CurrentCulture, "QueryToken of type '{0}' is not supported.", tokenIn.Kind));
 }