зеркало из https://github.com/microsoft/Power-Fx.git
Add OrderBy (#2530)
ODataParameters: Update OrderBy DelegationParameters: Add GetOrderBy()
This commit is contained in:
Родитель
d2201af57e
Коммит
a9622c06db
|
@ -1,14 +1,10 @@
|
|||
// Copyright (c) Microsoft Corporation.
|
||||
// Licensed under the MIT license.
|
||||
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Collections.Specialized;
|
||||
using System.Globalization;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Web;
|
||||
using Microsoft.PowerFx.Types;
|
||||
using System.Web;
|
||||
|
||||
namespace Microsoft.PowerFx.Connectors
|
||||
{
|
||||
|
@ -38,7 +34,7 @@ namespace Microsoft.PowerFx.Connectors
|
|||
public int Levels { get; init; } = 0;
|
||||
|
||||
// $orderby
|
||||
public string OrderBy { get; init; } = null;
|
||||
public IList<(string, bool)> OrderBy { get; init; } = null;
|
||||
|
||||
// $schemaversion
|
||||
public string SchemaVersion { get; init; } = null;
|
||||
|
@ -113,7 +109,11 @@ namespace Microsoft.PowerFx.Connectors
|
|||
|
||||
d2 = ",";
|
||||
}
|
||||
}
|
||||
}
|
||||
else if (value is IList<(string col, bool asc)> orderByList && orderByList.Any())
|
||||
{
|
||||
sb.Append(string.Join(",", orderByList.Select(x => $"{x.col}{(x.asc ? string.Empty : " desc")}")));
|
||||
}
|
||||
else
|
||||
{
|
||||
encoded = HttpUtility.UrlEncode(value.ToString());
|
||||
|
@ -160,17 +160,17 @@ namespace Microsoft.PowerFx.Connectors
|
|||
query["$format"] = Format;
|
||||
}
|
||||
|
||||
if (Index != 0)
|
||||
if (Index > 0)
|
||||
{
|
||||
query["$index"] = Index;
|
||||
}
|
||||
|
||||
if (Levels != 0)
|
||||
if (Levels > 0)
|
||||
{
|
||||
query["$levels"] = Levels;
|
||||
}
|
||||
|
||||
if (!string.IsNullOrEmpty(OrderBy))
|
||||
if (OrderBy != null && OrderBy.Any())
|
||||
{
|
||||
query["$orderby"] = OrderBy;
|
||||
}
|
||||
|
@ -190,12 +190,12 @@ namespace Microsoft.PowerFx.Connectors
|
|||
query["$select"] = Select;
|
||||
}
|
||||
|
||||
if (Skip != 0)
|
||||
if (Skip > 0)
|
||||
{
|
||||
query["$skip"] = Skip;
|
||||
}
|
||||
|
||||
if (Top != 0)
|
||||
if (Top > 0)
|
||||
{
|
||||
query["$top"] = Top;
|
||||
}
|
||||
|
|
|
@ -76,14 +76,17 @@ namespace Microsoft.PowerFx.Connectors
|
|||
DelegationParameterFeatures allowedFeatures =
|
||||
DelegationParameterFeatures.Filter |
|
||||
DelegationParameterFeatures.Top |
|
||||
DelegationParameterFeatures.Columns;
|
||||
DelegationParameterFeatures.Columns | // $select
|
||||
DelegationParameterFeatures.Sort; // $orderby
|
||||
|
||||
parameters.EnsureOnlyFeatures(allowedFeatures);
|
||||
|
||||
ODataParameters op = new ODataParameters()
|
||||
{
|
||||
Filter = parameters.GetOdataFilter(),
|
||||
Top = parameters.Top.GetValueOrDefault(),
|
||||
Select = parameters.GetColumns()
|
||||
Select = parameters.GetColumns(),
|
||||
OrderBy = parameters.GetOrderBy()
|
||||
};
|
||||
|
||||
return op;
|
||||
|
|
|
@ -3,16 +3,6 @@
|
|||
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Diagnostics.Contracts;
|
||||
using System.Linq;
|
||||
using System.Runtime.CompilerServices;
|
||||
using System.Text;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
using Microsoft.PowerFx.Core.IR;
|
||||
using Microsoft.PowerFx.Core.Localization;
|
||||
using Microsoft.PowerFx.Core.Utils;
|
||||
using Microsoft.PowerFx.Functions;
|
||||
|
||||
namespace Microsoft.PowerFx.Types
|
||||
{
|
||||
|
@ -44,6 +34,12 @@ namespace Microsoft.PowerFx.Types
|
|||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Returns the list of (column name, ascending/descending) where ascending=true.
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
public abstract IList<(string, bool)> GetOrderBy();
|
||||
|
||||
public abstract string GetOdataFilter();
|
||||
|
||||
// 0 columns means return all columns.
|
||||
|
@ -53,8 +49,6 @@ namespace Microsoft.PowerFx.Types
|
|||
}
|
||||
|
||||
public int? Top { get; set; }
|
||||
|
||||
// Other odata fetchers?
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
|
@ -64,9 +58,47 @@ namespace Microsoft.PowerFx.Types
|
|||
[Flags]
|
||||
public enum DelegationParameterFeatures
|
||||
{
|
||||
// $filter
|
||||
Filter = 1 << 0,
|
||||
|
||||
// $top
|
||||
Top = 1 << 1,
|
||||
|
||||
// $select
|
||||
Columns = 1 << 2,
|
||||
|
||||
// $orderBy
|
||||
Sort = 1 << 3,
|
||||
|
||||
/*
|
||||
To be implemented later when needed
|
||||
|
||||
// $compute
|
||||
Compute = 1 << 4,
|
||||
|
||||
// $count
|
||||
Count = 1 << 5,
|
||||
|
||||
// $expand
|
||||
Expand = 1 << 6,
|
||||
|
||||
// $format
|
||||
Format = 1 << 7,
|
||||
|
||||
// $index
|
||||
Index = 1 << 8,
|
||||
|
||||
// $levels
|
||||
Levels = 1 << 9,
|
||||
|
||||
// $schemaversion
|
||||
SchemaVersion = 1 << 10,
|
||||
|
||||
// $search
|
||||
Search = 1 << 11,
|
||||
|
||||
// $skip
|
||||
Skip = 1 << 12
|
||||
*/
|
||||
}
|
||||
}
|
||||
|
|
|
@ -3,18 +3,9 @@
|
|||
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Net.Http;
|
||||
using System.Text.RegularExpressions;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
using Microsoft.CodeAnalysis.Operations;
|
||||
using Microsoft.PowerFx.Connectors;
|
||||
using Microsoft.PowerFx.Connectors.Tests;
|
||||
using Microsoft.PowerFx.Core.Tests;
|
||||
using Microsoft.PowerFx.Types;
|
||||
using Xunit;
|
||||
using Xunit.Abstractions;
|
||||
|
||||
namespace Microsoft.PowerFx.Tests
|
||||
{
|
||||
|
@ -130,24 +121,58 @@ namespace Microsoft.PowerFx.Tests
|
|||
Assert.Equal("$filter=score+gt+5&$top=10", str);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void TestToOdataParametersFilterTopOrderByAsc()
|
||||
{
|
||||
var delegation = new TestDelegationParameters()
|
||||
{
|
||||
_columns = null,
|
||||
_features = DelegationParameterFeatures.Columns | DelegationParameterFeatures.Filter | DelegationParameterFeatures.Top | DelegationParameterFeatures.Sort,
|
||||
_filter = "score gt 5",
|
||||
_orderBy = new List<(string, bool)>() { ("score", true) },
|
||||
Top = 10
|
||||
};
|
||||
|
||||
var od = delegation.ToOdataParameters();
|
||||
|
||||
var str = od.ToQueryString();
|
||||
|
||||
Assert.Equal("$filter=score+gt+5&$orderby=score&$top=10", str);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void TestToOdataParametersFilterTopOrderByDesc()
|
||||
{
|
||||
var delegation = new TestDelegationParameters()
|
||||
{
|
||||
_columns = null,
|
||||
_features = DelegationParameterFeatures.Columns | DelegationParameterFeatures.Filter | DelegationParameterFeatures.Top | DelegationParameterFeatures.Sort,
|
||||
_filter = "score gt 5",
|
||||
_orderBy = new List<(string, bool)>() { ("score", false) },
|
||||
Top = 10
|
||||
};
|
||||
|
||||
var od = delegation.ToOdataParameters();
|
||||
|
||||
var str = od.ToQueryString();
|
||||
|
||||
Assert.Equal("$filter=score+gt+5&$orderby=score desc&$top=10", str);
|
||||
}
|
||||
|
||||
private class TestDelegationParameters : DelegationParameters
|
||||
{
|
||||
public string[] _columns;
|
||||
public string _filter;
|
||||
public DelegationParameterFeatures _features;
|
||||
public List<(string, bool)> _orderBy;
|
||||
|
||||
public override DelegationParameterFeatures Features =>
|
||||
_features;
|
||||
public override DelegationParameterFeatures Features => _features;
|
||||
|
||||
public override IReadOnlyCollection<string> GetColumns()
|
||||
{
|
||||
return _columns;
|
||||
}
|
||||
public override IReadOnlyCollection<string> GetColumns() => _columns;
|
||||
|
||||
public override string GetOdataFilter()
|
||||
{
|
||||
return _filter;
|
||||
}
|
||||
public override string GetOdataFilter() => _filter;
|
||||
|
||||
public override IList<(string, bool)> GetOrderBy() => _orderBy;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Загрузка…
Ссылка в новой задаче