/// <summary>
        /// Create a new QueryDescriptor given its parts as arguments.
        /// </summary>
        /// <param name="path">The path for the query. Must not be null.</param>
        /// <param name="filter">The filter for the query. If the property is null, there's no filter for this query.</param>
        /// <param name="orderByTokens">Enumeration of order by tokens.</param>
        /// <param name="select">The select for the query. If the property is null, there's no select for this query.</param>
        /// <param name="expand">The expansions for the query. If the property is null, there are no expandsion for this query.</param>
        /// <param name="skip">The number of entities to skip in the result.</param>
        /// <param name="top">The (maximum) number of entities to include in the result.</param>
        /// <param name="inlineCount">Type of inlinecount in the response of the query.</param>
        /// <param name="format">The format for the query.</param>
        /// <param name="queryOptions">The query options for the query.</param>
        public QueryDescriptorQueryToken(
            QueryToken path,
            QueryToken filter,
            IEnumerable <OrderByQueryToken> orderByTokens,
            SelectQueryToken select,
            ExpandQueryToken expand,
            int?skip,
            int?top,
            InlineCountKind?inlineCount,
            string format,
            IEnumerable <QueryOptionQueryToken> queryOptions)
        {
            ExceptionUtils.CheckArgumentNotNull(path, "path");

            this.path          = path;
            this.filter        = filter;
            this.orderByTokens = new ReadOnlyEnumerable <OrderByQueryToken>(orderByTokens ?? new OrderByQueryToken[0]);
            this.select        = select;
            this.expand        = expand;
            this.skip          = skip;
            this.top           = top;
            this.inlineCount   = inlineCount;
            this.format        = format;
            this.queryOptions  = new ReadOnlyEnumerable <QueryOptionQueryToken>(queryOptions ?? new QueryOptionQueryToken[0]);
        }
Esempio n. 2
0
        /// <summary>
        /// Write the select token as URI part to this builder.
        /// </summary>
        /// <param name="selectQueryToken">To write as URI part.</param>
        protected virtual void WriteSelect(SelectQueryToken selectQueryToken)
        {
            ExceptionUtils.CheckArgumentNotNull(selectQueryToken, "selectQueryToken");

            this.builder.Append(UriQueryConstants.SelectQueryOption);
            this.builder.Append(ExpressionConstants.SymbolEqual);

            bool needComma = false;

            foreach (QueryToken property in selectQueryToken.Properties)
            {
                if (needComma)
                {
                    this.builder.Append(ExpressionConstants.SymbolComma);
                }

                this.WriteQuery(property);
                needComma = true;
            }
        }
        /// <summary>
        /// Parses the <paramref name="queryUri"/> and returns a new instance of <see cref="QueryDescriptorQueryToken"/>
        /// describing the query specified by the uri.
        /// </summary>
        /// <param name="queryUri">The absolute URI which holds the query to parse. This must be a path relative to the <paramref name="serviceBaseUri"/>.</param>
        /// <param name="serviceBaseUri">The base URI of the service.</param>
        /// <param name="maxDepth">The maximum depth of any single query part. Security setting to guard against DoS attacks causing stack overflows and such.</param>
        /// <returns>A new instance of <see cref="QueryDescriptorQueryToken"/> which represents the query specified in the <paramref name="queryUri"/>.</returns>
        public static QueryDescriptorQueryToken ParseUri(Uri queryUri, Uri serviceBaseUri, int maxDepth)
        {
            ExceptionUtils.CheckArgumentNotNull(queryUri, "queryUri");
            if (!queryUri.IsAbsoluteUri)
            {
                throw new ArgumentException(Strings.QueryDescriptorQueryToken_UriMustBeAbsolute(queryUri), "queryUri");
            }

            ExceptionUtils.CheckArgumentNotNull(serviceBaseUri, "serviceBaseUri");
            if (!serviceBaseUri.IsAbsoluteUri)
            {
                throw new ArgumentException(Strings.QueryDescriptorQueryToken_UriMustBeAbsolute(serviceBaseUri), "serviceBaseUri");
            }

            if (maxDepth <= 0)
            {
                throw new ArgumentException(Strings.QueryDescriptorQueryToken_MaxDepthInvalid, "maxDepth");
            }

            UriQueryPathParser pathParser = new UriQueryPathParser(maxDepth);
            QueryToken path = pathParser.ParseUri(queryUri, serviceBaseUri);

            List<QueryOptionQueryToken> queryOptions = UriUtils.ParseQueryOptions(queryUri);

            QueryToken filter = null;
            string filterQuery = queryOptions.GetQueryOptionValueAndRemove(UriQueryConstants.FilterQueryOption);
            if (filterQuery != null)
            {
                UriQueryExpressionParser expressionParser = new UriQueryExpressionParser(maxDepth);
                filter = expressionParser.ParseFilter(filterQuery);
            }

            IEnumerable<OrderByQueryToken> orderByTokens = null;
            string orderByQuery = queryOptions.GetQueryOptionValueAndRemove(UriQueryConstants.OrderByQueryOption);
            if (orderByQuery != null)
            {
                UriQueryExpressionParser expressionParser = new UriQueryExpressionParser(maxDepth);
                orderByTokens = expressionParser.ParseOrderBy(orderByQuery);
            }

            SelectQueryToken select = null;
            string selectQuery = queryOptions.GetQueryOptionValueAndRemove(UriQueryConstants.SelectQueryOption);
            if (selectQuery != null)
            {
                UriQueryExpressionParser expressionParser = new UriQueryExpressionParser(maxDepth);
                select = expressionParser.ParseSelect(selectQuery);
            }

            ExpandQueryToken expand = null;
            string expandQuery = queryOptions.GetQueryOptionValueAndRemove(UriQueryConstants.ExpandQueryOption);
            if (expandQuery != null)
            {
                UriQueryExpressionParser expressionParser = new UriQueryExpressionParser(maxDepth);
                expand = expressionParser.ParseExpand(expandQuery);
            }

            int? skip = null;
            string skipQuery = queryOptions.GetQueryOptionValueAndRemove(UriQueryConstants.SkipQueryOption);
            if (skipQuery != null)
            {
                int skipValue;
                if (!UriPrimitiveTypeParser.TryUriStringToNonNegativeInteger(skipQuery, out skipValue))
                {
                    throw new ODataException(Strings.QueryDescriptorQueryToken_InvalidSkipQueryOptionValue(skipQuery));
                }

                skip = skipValue;
            }

            int? top = null;
            string topQuery = queryOptions.GetQueryOptionValueAndRemove(UriQueryConstants.TopQueryOption);
            if (topQuery != null)
            {
                int topValue;
                if (!UriPrimitiveTypeParser.TryUriStringToNonNegativeInteger(topQuery, out topValue))
                {
                    throw new ODataException(Strings.QueryDescriptorQueryToken_InvalidTopQueryOptionValue(topQuery));
                }

                top = topValue;
            }

            string inlineCountQuery = queryOptions.GetQueryOptionValueAndRemove(UriQueryConstants.InlineCountQueryOption);
            InlineCountKind? inlineCount = QueryTokenUtils.ParseInlineCountKind(inlineCountQuery);

            string format = queryOptions.GetQueryOptionValueAndRemove(UriQueryConstants.FormatQueryOption);

            return new QueryDescriptorQueryToken(
                path,
                filter,
                orderByTokens, 
                select,
                expand,
                skip,
                top,
                inlineCount,
                format,
                queryOptions.Count == 0 ? null : new ReadOnlyCollection<QueryOptionQueryToken>(queryOptions));
        }
        /// <summary>
        /// Create a new QueryDescriptor given its parts as arguments.
        /// </summary>
        /// <param name="path">The path for the query. Must not be null.</param>
        /// <param name="filter">The filter for the query. If the property is null, there's no filter for this query.</param>
        /// <param name="orderByTokens">Enumeration of order by tokens.</param>
        /// <param name="select">The select for the query. If the property is null, there's no select for this query.</param>
        /// <param name="expand">The expansions for the query. If the property is null, there are no expandsion for this query.</param>
        /// <param name="skip">The number of entities to skip in the result.</param>
        /// <param name="top">The (maximum) number of entities to include in the result.</param>
        /// <param name="inlineCount">Type of inlinecount in the response of the query.</param>
        /// <param name="format">The format for the query.</param>
        /// <param name="queryOptions">The query options for the query.</param>
        public QueryDescriptorQueryToken(
            QueryToken path, 
            QueryToken filter, 
            IEnumerable<OrderByQueryToken> orderByTokens, 
            SelectQueryToken select, 
            ExpandQueryToken expand,
            int? skip, 
            int? top, 
            InlineCountKind? inlineCount,
            string format,
            IEnumerable<QueryOptionQueryToken> queryOptions)
        {
            ExceptionUtils.CheckArgumentNotNull(path, "path");

            this.path = path;
            this.filter = filter;
            this.orderByTokens = new ReadOnlyEnumerable<OrderByQueryToken>(orderByTokens ?? new OrderByQueryToken[0]);
            this.select = select;
            this.expand = expand;
            this.skip = skip;
            this.top = top;
            this.inlineCount = inlineCount;
            this.format = format;
            this.queryOptions = new ReadOnlyEnumerable<QueryOptionQueryToken>(queryOptions ?? new QueryOptionQueryToken[0]);
        }
        /// <summary>
        /// Parses the <paramref name="queryUri"/> and returns a new instance of <see cref="QueryDescriptorQueryToken"/>
        /// describing the query specified by the uri.
        /// </summary>
        /// <param name="queryUri">The absolute URI which holds the query to parse. This must be a path relative to the <paramref name="serviceBaseUri"/>.</param>
        /// <param name="serviceBaseUri">The base URI of the service.</param>
        /// <param name="maxDepth">The maximum depth of any single query part. Security setting to guard against DoS attacks causing stack overflows and such.</param>
        /// <returns>A new instance of <see cref="QueryDescriptorQueryToken"/> which represents the query specified in the <paramref name="queryUri"/>.</returns>
        public static QueryDescriptorQueryToken ParseUri(Uri queryUri, Uri serviceBaseUri, int maxDepth)
        {
            ExceptionUtils.CheckArgumentNotNull(queryUri, "queryUri");
            if (!queryUri.IsAbsoluteUri)
            {
                throw new ArgumentException(Strings.QueryDescriptorQueryToken_UriMustBeAbsolute(queryUri), "queryUri");
            }

            ExceptionUtils.CheckArgumentNotNull(serviceBaseUri, "serviceBaseUri");
            if (!serviceBaseUri.IsAbsoluteUri)
            {
                throw new ArgumentException(Strings.QueryDescriptorQueryToken_UriMustBeAbsolute(serviceBaseUri), "serviceBaseUri");
            }

            if (maxDepth <= 0)
            {
                throw new ArgumentException(Strings.QueryDescriptorQueryToken_MaxDepthInvalid, "maxDepth");
            }

            UriQueryPathParser pathParser = new UriQueryPathParser(maxDepth);
            QueryToken         path       = pathParser.ParseUri(queryUri, serviceBaseUri);

            List <QueryOptionQueryToken> queryOptions = UriUtils.ParseQueryOptions(queryUri);

            QueryToken filter      = null;
            string     filterQuery = queryOptions.GetQueryOptionValueAndRemove(UriQueryConstants.FilterQueryOption);

            if (filterQuery != null)
            {
                UriQueryExpressionParser expressionParser = new UriQueryExpressionParser(maxDepth);
                filter = expressionParser.ParseFilter(filterQuery);
            }

            IEnumerable <OrderByQueryToken> orderByTokens = null;
            string orderByQuery = queryOptions.GetQueryOptionValueAndRemove(UriQueryConstants.OrderByQueryOption);

            if (orderByQuery != null)
            {
                UriQueryExpressionParser expressionParser = new UriQueryExpressionParser(maxDepth);
                orderByTokens = expressionParser.ParseOrderBy(orderByQuery);
            }

            SelectQueryToken select      = null;
            string           selectQuery = queryOptions.GetQueryOptionValueAndRemove(UriQueryConstants.SelectQueryOption);

            if (selectQuery != null)
            {
                UriQueryExpressionParser expressionParser = new UriQueryExpressionParser(maxDepth);
                select = expressionParser.ParseSelect(selectQuery);
            }

            ExpandQueryToken expand      = null;
            string           expandQuery = queryOptions.GetQueryOptionValueAndRemove(UriQueryConstants.ExpandQueryOption);

            if (expandQuery != null)
            {
                UriQueryExpressionParser expressionParser = new UriQueryExpressionParser(maxDepth);
                expand = expressionParser.ParseExpand(expandQuery);
            }

            int?   skip      = null;
            string skipQuery = queryOptions.GetQueryOptionValueAndRemove(UriQueryConstants.SkipQueryOption);

            if (skipQuery != null)
            {
                int skipValue;
                if (!UriPrimitiveTypeParser.TryUriStringToNonNegativeInteger(skipQuery, out skipValue))
                {
                    throw new ODataException(Strings.QueryDescriptorQueryToken_InvalidSkipQueryOptionValue(skipQuery));
                }

                skip = skipValue;
            }

            int?   top      = null;
            string topQuery = queryOptions.GetQueryOptionValueAndRemove(UriQueryConstants.TopQueryOption);

            if (topQuery != null)
            {
                int topValue;
                if (!UriPrimitiveTypeParser.TryUriStringToNonNegativeInteger(topQuery, out topValue))
                {
                    throw new ODataException(Strings.QueryDescriptorQueryToken_InvalidTopQueryOptionValue(topQuery));
                }

                top = topValue;
            }

            string          inlineCountQuery = queryOptions.GetQueryOptionValueAndRemove(UriQueryConstants.InlineCountQueryOption);
            InlineCountKind?inlineCount      = QueryTokenUtils.ParseInlineCountKind(inlineCountQuery);

            string format = queryOptions.GetQueryOptionValueAndRemove(UriQueryConstants.FormatQueryOption);

            return(new QueryDescriptorQueryToken(
                       path,
                       filter,
                       orderByTokens,
                       select,
                       expand,
                       skip,
                       top,
                       inlineCount,
                       format,
                       queryOptions.Count == 0 ? null : new ReadOnlyCollection <QueryOptionQueryToken>(queryOptions)));
        }
Esempio n. 6
0
        /// <summary>
        /// Write the select token as URI part to this builder.
        /// </summary>
        /// <param name="selectQueryToken">To write as URI part.</param>
        protected virtual void WriteSelect(SelectQueryToken selectQueryToken)
        {
            ExceptionUtils.CheckArgumentNotNull(selectQueryToken, "selectQueryToken");

            this.builder.Append(UriQueryConstants.SelectQueryOption);
            this.builder.Append(ExpressionConstants.SymbolEqual);

            bool needComma = false;
            foreach (QueryToken property in selectQueryToken.Properties)
            {
                if (needComma)
                {
                    this.builder.Append(ExpressionConstants.SymbolComma);
                }

                this.WriteQuery(property);
                needComma = true;
            }
        }