sdk/dotnet/test/Datasync.Common.Test/Mocks/MockOfflineStore.cs
/// <summary>
/// Selects the correct set of entities based on the provided OData string.
/// </summary>
/// <param name="query">The query.</param>
/// <param name="items">The items in the table.</param>
/// <returns>The filtered items list.</returns>
Selects the correct set of entities based on the provided OData string.
Selects the correct set of entities based on the provided OData string .
private static IEnumerable<JObject> FilterItemList(QueryDescription query, IEnumerable<JObject> items)
        {
            var odataString = query.ToODataString();
            var odataQuery = ODataQuery.Parse(odataString);
            if (!items.Any())
            {
                return items;
            }
            string filter = odataQuery.Filter;
            if (!string.IsNullOrEmpty(filter))
            {
                if (Regex.IsMatch(filter, "^\\(\\(sequence gt [0-9]+L\\) and \\(tableName eq '[^']+'\\)\\)$"))
                {
                    BinaryOperatorNode sequenceComparison = (BinaryOperatorNode)((BinaryOperatorNode)query.Filter).LeftOperand;
                    long sequence = GetFromConstantNode<long>(sequenceComparison.RightOperand);
                    BinaryOperatorNode tableComparison = (BinaryOperatorNode)((BinaryOperatorNode)query.Filter).RightOperand;
                    string tableName = GetFromConstantNode<string>(tableComparison.RightOperand);
                    items = items.Where(item => item.Value<long>("sequence") > sequence && item.Value<string>("tableName") == tableName);
                }
                else if (Regex.IsMatch(filter, "^\\(\\(tableName eq '[^']+'\\) and \\(itemId eq '[^']+'\\)\\)$"))
                {
                    BinaryOperatorNode tableComparison = (BinaryOperatorNode)((BinaryOperatorNode)query.Filter).LeftOperand;
                    string tableName = GetFromConstantNode<string>(tableComparison.RightOperand);
                    BinaryOperatorNode itemIdComparison = (BinaryOperatorNode)((BinaryOperatorNode)query.Filter).RightOperand;
                    string itemId = GetFromConstantNode<string>(itemIdComparison.RightOperand);
                    items = items.Where(item => item.Value<string>("tableName") == tableName && item.Value<string>("itemId") == itemId);
                }
                else if (Regex.IsMatch(filter, "^\\(tableName eq '[^']+'\\)$"))
                {
                    BinaryOperatorNode tableComparison = (BinaryOperatorNode)query.Filter;
                    string tableName = GetFromConstantNode<string>(tableComparison.RightOperand);
                    items = items.Where(item => item.Value<string>("tableName") == tableName);
                }
                else if (Regex.IsMatch(filter, "^\\(sequence gt [0-9]+L\\)$"))
                {
                    BinaryOperatorNode sequenceComparison = (BinaryOperatorNode)query.Filter;
                    long sequence = GetFromConstantNode<long>(sequenceComparison.RightOperand);
                    items = items.Where(item => item.Value<long>("sequence") > sequence);
                }
                else
                {
                    throw new NotImplementedException();
                }
            }
            if (odataQuery.OrderBy?.Length > 0)
            {
                foreach (var orderBy in odataQuery.OrderBy)
                {
                    bool descending = false;
                    string field = orderBy;
                    if (orderBy.EndsWith(" desc"))
                    {
                        descending = true;
                        field = orderBy.Replace(" desc", "");
                    }
                    items = field switch
                    {
                        "sequence" => descending ? items.OrderByDescending(i => i.Value<long>(field)) : items.OrderBy(i => i.Value<long>(field)),
                        _ => throw new NotImplementedException()
                    };
                }
            }
            return items;
        }
private static IEnumerable < JObject > FilterItemList ( QueryDescription query , IEnumerable < JObject > items ) { var odataString = query . ToODataString ( ) ; var odataQuery = ODataQuery . Parse ( odataString ) ; if ( ! items . Any ( ) ) { return items ; } string filter = odataQuery . Filter ; if ( ! string . IsNullOrEmpty ( filter ) ) { if ( Regex . IsMatch ( filter , " \\ \\ \\ \\ \\ \\ " ) ) { BinaryOperatorNode sequenceComparison = ( BinaryOperatorNode ) ( ( BinaryOperatorNode ) query . Filter ) . LeftOperand ; long sequence = GetFromConstantNode < long > ( sequenceComparison . RightOperand ) ; BinaryOperatorNode tableComparison = ( BinaryOperatorNode ) ( ( BinaryOperatorNode ) query . Filter ) . RightOperand ; string tableName = GetFromConstantNode < string > ( tableComparison . RightOperand ) ; items = items . Where ( item => item . Value < long > ( " " ) > sequence && item . Value < string > ( " " ) == tableName ) ; } else if ( Regex . IsMatch ( filter , " \\ \\ \\ \\ \\ \\ " ) ) { BinaryOperatorNode tableComparison = ( BinaryOperatorNode ) ( ( BinaryOperatorNode ) query . Filter ) . LeftOperand ; string tableName = GetFromConstantNode < string > ( tableComparison . RightOperand ) ; BinaryOperatorNode itemIdComparison = ( BinaryOperatorNode ) ( ( BinaryOperatorNode ) query . Filter ) . RightOperand ; string itemId = GetFromConstantNode < string > ( itemIdComparison . RightOperand ) ; items = items . Where ( item => item . Value < string > ( " " ) == tableName && item . Value < string > ( " " ) == itemId ) ; } else if ( Regex . IsMatch ( filter , " \\ \\ " ) ) { BinaryOperatorNode tableComparison = ( BinaryOperatorNode ) query . Filter ; string tableName = GetFromConstantNode < string > ( tableComparison . RightOperand ) ; items = items . Where ( item => item . Value < string > ( " " ) == tableName ) ; } else if ( Regex . IsMatch ( filter , " \\ \\ " ) ) { BinaryOperatorNode sequenceComparison = ( BinaryOperatorNode ) query . Filter ; long sequence = GetFromConstantNode < long > ( sequenceComparison . RightOperand ) ; items = items . Where ( item => item . Value < long > ( " " ) > sequence ) ; } else { throw new NotImplementedException ( ) ; } } if ( odataQuery . OrderBy ? . Length > 0 ) { foreach ( var orderBy in odataQuery . OrderBy ) { bool descending = false ; string field = orderBy ; if ( orderBy . EndsWith ( " " ) ) { descending = true ; field = orderBy . Replace ( " " , " " ) ; } items = field switch { " " => descending ? items . OrderByDescending ( i => i . Value < long > ( field ) ) : items . OrderBy ( i => i . Value < long > ( field ) ) , _ => throw new NotImplementedException ( ) } ; } } return items ; }
Selects the correct set of entities based on the provided OData string.
Selects the correct set of entities based on the provided OData string .
The filtered items list.
The filtered items list .
query
QueryDescription
None
items
IEnumerable<JObject>
The items in the table.
The items in the table .