private string ToSql(Expression expression, out ODataInlineCountOption inlineCount, out List <Parameter> parameters, out Type rootElementType, out LinqToODataTranslator.ResultTranslator resultTranslator) { // translate LINQ expression to OData var translator = new LinqToODataTranslator(); IQueryable rootQuery; var oDataExpression = translator.Translate(expression, out rootQuery, out resultTranslator); rootElementType = rootQuery.ElementType; var queryExpression = oDataExpression as ODataQueryExpression; Throw <InvalidOperationException> .If(oDataExpression == null, "A queryable expression must translate to a query ODataExpression"); inlineCount = queryExpression.InlineCount; // get the table SQL from the root query var tableQuery = rootQuery as ODataSqlQuery; Throw <InvalidOperationException> .If( tableQuery == null, () => "Translate: expected a root query query of type " + typeof(ODataSqlQuery) + "; found " + tableQuery ); Throw <InvalidOperationException> .If(tableQuery.tableSql == null, "Invalid root query"); // translate ODataExpression to SQL var sql = ODataToSqlTranslator.Translate(this.syntax, tableQuery.tableSql, queryExpression, out parameters); return(sql); }
internal static ODataQueryExpression Query( ODataExpression filter = null, IReadOnlyList <ODataSortKeyExpression> orderBy = null, int?top = null, int skip = 0, string format = null, ODataInlineCountOption inlineCount = ODataInlineCountOption.None, IReadOnlyList <ODataSelectColumnExpression> select = null) { Throw.If(filter != null && filter.Type != ODataExpressionType.Boolean && filter.Type != ODataExpressionType.Unknown, "filter: must be boolean-typed"); var finalFilter = filter != null && filter.Type == ODataExpressionType.Unknown ? ConvertFromUnknownType(filter, typeof(bool)) : filter; if (top.HasValue) { Throw.IfOutOfRange(top.Value, min: 0, paramName: "top"); } Throw.IfOutOfRange(skip, min: 0, paramName: "skip"); if (format != null) { Throw.If(string.IsNullOrWhiteSpace(format), "format: must not be empty or whitespace"); } return(new ODataQueryExpression( filter: finalFilter, orderBy: orderBy != null ? orderBy.ToImmutableList() : Empty <ODataSortKeyExpression> .Array, top: top, skip: skip, format: format, inlineCount: inlineCount, select: select != null ? select.ToImmutableList() : Empty <ODataSelectColumnExpression> .Array )); }
// TODO VNEXT expand internal ODataQueryExpression( ODataExpression filter, IReadOnlyList <ODataSortKeyExpression> orderBy, int?top, int skip, string format, ODataInlineCountOption inlineCount, IReadOnlyList <ODataSelectColumnExpression> select) : base(ODataExpressionKind.Query, ODataExpressionType.Complex, typeof(IQueryable)) { this.Filter = filter; this.OrderBy = orderBy; this.Top = top; this.Skip = skip; this.Format = format; this.InlineCount = inlineCount; this.Select = select; }
internal ODataQueryExpression Update(ODataExpression filter = null, IEnumerable<ODataSortKeyExpression> orderBy = null, int? top = -1, int? skip = null, string format = null, ODataInlineCountOption? inlineCount = null, IEnumerable<ODataSelectColumnExpression> select = null) { return Query( filter: filter ?? this.Filter, orderBy: orderBy.NullSafe(ob => ob.ToArray(), ifNullReturn: this.OrderBy), top: top == -1 ? this.Top : top, skip: skip ?? this.Skip, format: format ?? this.Format, inlineCount: inlineCount ?? this.InlineCount, select: select.NullSafe(sc => sc.ToArray(), ifNullReturn: this.Select) ); }
// TODO VNEXT expand internal ODataQueryExpression( ODataExpression filter, IReadOnlyList<ODataSortKeyExpression> orderBy, int? top, int skip, string format, ODataInlineCountOption inlineCount, IReadOnlyList<ODataSelectColumnExpression> select) : base(ODataExpressionKind.Query, ODataExpressionType.Complex, typeof(IQueryable)) { this.Filter = filter; this.OrderBy = orderBy; this.Top = top; this.Skip = skip; this.Format = format; this.InlineCount = inlineCount; this.Select = select; }
/// <summary> /// Constructs a new instance of <see cref="ODataQueryOptions"/> /// </summary> public ODataQueryOptions(string format = "json", ODataInlineCountOption? inlineCount = null) { this.Format = format; this.InlineCount = inlineCount; }
internal static ODataQueryExpression Query( ODataExpression filter = null, IReadOnlyList<ODataSortKeyExpression> orderBy = null, int? top = null, int skip = 0, string format = null, ODataInlineCountOption inlineCount = ODataInlineCountOption.None, IReadOnlyList<ODataSelectColumnExpression> select = null) { Throw.If(filter != null && filter.Type != ODataExpressionType.Boolean, "filter: must be boolean-typed"); if (top.HasValue) { Throw.IfOutOfRange(top.Value, min: 0, paramName: "top"); } Throw.IfOutOfRange(skip, min: 0, paramName: "skip"); if (format != null) { Throw.If(string.IsNullOrWhiteSpace(format), "format: must not be empty or whitespace"); } return new ODataQueryExpression( filter: filter, orderBy: orderBy != null ? orderBy.ToImmutableList() : Empty<ODataSortKeyExpression>.Array, top: top, skip: skip, format: format, inlineCount: inlineCount, select: select != null ? select.ToImmutableList() : Empty<ODataSelectColumnExpression>.Array ); }