Esempio n. 1
0
 public FluentCommand Expand(ODataExpandOptions expandOptions)
 {
     Details.ExpandAssociations.Add(new KeyValuePair <ODataExpandAssociation, ODataExpandOptions>(
                                        new ODataExpandAssociation("*"),
                                        expandOptions));
     return(this);
 }
Esempio n. 2
0
        public async Task ExpandSubordinates2LevelsByReference()
        {
            var client  = CreateClient("Northwind4.xml");
            var command = client
                          .For <Employee>()
                          .Expand(ODataExpandOptions.ByReference(2), x => x.Subordinates);

            string commandText = await command.GetCommandTextAsync();

            Assert.Equal("Employees?$expand=Subordinates/$ref($levels=2)", commandText);
        }
    public async Task ExpandSubordinates2LevelsByValue()
    {
        var client  = CreateClient("Northwind4.xml");
        var command = client
                      .For <Employee>()
                      .Expand(ODataExpandOptions.ByValue(2), x => x.Subordinates);

        var commandText = await command.GetCommandTextAsync().ConfigureAwait(false);

        Assert.Equal("Employees?$expand=Subordinates($levels=2)", commandText);
    }
        private string FormatExpansionSegment(string path, EntityCollection entityCollection,
            ODataExpandOptions expandOptions, IList<string> selectColumns, IList<KeyValuePair<string, bool>> orderbyColumns)
        {
            var items = path.Split('/');
            var associationName = _session.Metadata.GetNavigationPropertyExactName(entityCollection.Name, items.First());

            var clauses = new List<string>();
            var text = associationName;
            if (expandOptions.ExpandMode == ODataExpandMode.ByReference)
                text += "/" + ODataLiteral.Ref;

            if (items.Count() > 1)
            {
                path = path.Substring(items.First().Length + 1);
                entityCollection = _session.Metadata.GetEntityCollection(
                    _session.Metadata.GetNavigationPropertyPartnerTypeName(entityCollection.Name, associationName));

                clauses.Add(string.Format("{0}={1}", ODataLiteral.Expand, 
                    FormatExpansionSegment(path, entityCollection, expandOptions,
                        SelectExpansionSegmentColumns(selectColumns, path),
                        SelectExpansionSegmentColumns(orderbyColumns, path))));
            }

            if (expandOptions.Levels > 1)
            {
                clauses.Add(string.Format("{0}={1}", ODataLiteral.Levels, expandOptions.Levels));
            }
            else if (expandOptions.Levels == 0)
            {
                clauses.Add(string.Format("{0}={1}", ODataLiteral.Levels, ODataLiteral.Max));
            }

            if (selectColumns.Any())
            {
                var columns = string.Join(",", SelectExpansionSegmentColumns(selectColumns, null));
                if (!string.IsNullOrEmpty(columns))
                    clauses.Add(string.Format("{0}={1}", ODataLiteral.Select, columns));
            }

            if (orderbyColumns.Any())
            {
                var columns = string.Join(",", SelectExpansionSegmentColumns(orderbyColumns, null)
                    .Select(x => x.Key + (x.Value ? " desc" : string.Empty)).ToList());
                if (!string.IsNullOrEmpty(columns))
                    clauses.Add(string.Format("{0}={1}", ODataLiteral.OrderBy, columns));
            }

            if (clauses.Any())
                text += string.Format("({0})", string.Join(";", clauses));

            return text;
        }
Esempio n. 5
0
 public FluentCommand Expand(ODataExpandOptions expandOptions, IEnumerable <ODataExpandAssociation> associations)
 {
     Details.ExpandAssociations.AddRange(associations.Select(x =>
                                                             new KeyValuePair <ODataExpandAssociation, ODataExpandOptions>(x, expandOptions)));
     return(this);
 }
Esempio n. 6
0
        private string FormatExpansionSegment(string path, EntityCollection entityCollection,
                                              ODataExpandOptions expandOptions, IList <string> selectColumns, IList <KeyValuePair <string, bool> > orderbyColumns)
        {
            var  items               = path.Split('/');
            var  associationName     = _session.Metadata.GetNavigationPropertyExactName(entityCollection.Name, items.First());
            bool expandsToCollection = _session.Metadata.IsNavigationPropertyCollection(entityCollection.Name, associationName);

            var clauses = new List <string>();
            var text    = associationName;

            if (expandOptions.ExpandMode == ODataExpandMode.ByReference)
            {
                text += "/" + ODataLiteral.Ref;
            }

            if (items.Count() > 1)
            {
                path             = path.Substring(items.First().Length + 1);
                entityCollection = _session.Metadata.GetEntityCollection(
                    _session.Metadata.GetNavigationPropertyPartnerTypeName(entityCollection.Name, associationName));

                clauses.Add(string.Format("{0}={1}", ODataLiteral.Expand,
                                          FormatExpansionSegment(path, entityCollection, expandOptions,
                                                                 SelectPathSegmentColumns(selectColumns, path),
                                                                 SelectPathSegmentColumns(orderbyColumns, path))));
            }

            if (expandOptions.Levels > 1)
            {
                clauses.Add(string.Format("{0}={1}", ODataLiteral.Levels, expandOptions.Levels));
            }
            else if (expandOptions.Levels == 0)
            {
                clauses.Add(string.Format("{0}={1}", ODataLiteral.Levels, ODataLiteral.Max));
            }

            if (selectColumns.Any())
            {
                var columns = string.Join(",", SelectPathSegmentColumns(selectColumns, null));
                if (!string.IsNullOrEmpty(columns))
                {
                    clauses.Add(string.Format("{0}={1}", ODataLiteral.Select, columns));
                }
            }

            if (expandsToCollection && orderbyColumns.Any())
            {
                var columns = string.Join(",", SelectPathSegmentColumns(orderbyColumns, null)
                                          .Select(x => x.Key + (x.Value ? " desc" : string.Empty)).ToList());
                if (!string.IsNullOrEmpty(columns))
                {
                    clauses.Add(string.Format("{0}={1}", ODataLiteral.OrderBy, columns));
                }
            }

            if (clauses.Any())
            {
                text += string.Format("({0})", string.Join(";", clauses));
            }

            return(text);
        }
        private string FormatExpansionSegment(ODataExpandAssociation association, EntityCollection entityCollection,
                                              ODataExpandOptions expandOptions, ResolvedCommand command, bool rootLevel = true)
        {
            if (rootLevel)
            {
                association = command.Details.SelectColumns.Aggregate(association, MergeExpandAssociations);
                association = command.Details.OrderbyColumns.Aggregate(association, MergeOrderByColumns);
            }

            var associationName     = association.Name;
            var expandsToCollection = false;

            if (_session.Metadata.HasNavigationProperty(entityCollection.Name, associationName))
            {
                associationName     = _session.Metadata.GetNavigationPropertyExactName(entityCollection.Name, associationName);
                expandsToCollection = _session.Metadata.IsNavigationPropertyCollection(entityCollection.Name, associationName);
            }

            var clauses = new List <string>();
            var text    = associationName;

            if (expandOptions.ExpandMode == ODataExpandMode.ByReference)
            {
                text += "/" + ODataLiteral.Ref;
            }

            if (expandOptions.Levels > 1)
            {
                clauses.Add($"{ODataLiteral.Levels}={expandOptions.Levels}");
            }
            else if (expandOptions.Levels == 0)
            {
                clauses.Add($"{ODataLiteral.Levels}={ODataLiteral.Max}");
            }

            if (associationName != StarString)
            {
                if (expandsToCollection && !ReferenceEquals(association.FilterExpression, null))
                {
                    var associatedEntityCollection = _session.Metadata.GetEntityCollection(
                        _session.Metadata.GetNavigationPropertyPartnerTypeName(entityCollection.Name, associationName));
                    clauses.Add(
                        $"{ODataLiteral.Filter}={EscapeUnescapedString(association.FilterExpression.Format(new ExpressionContext(_session, associatedEntityCollection, null, command.DynamicPropertiesContainerName)))}");
                }

                if (association.ExpandAssociations.Any())
                {
                    var associatedEntityCollection = _session.Metadata.GetEntityCollection(
                        _session.Metadata.GetNavigationPropertyPartnerTypeName(entityCollection.Name, associationName));
                    var expandAll = association.ExpandAssociations.FirstOrDefault(a => a.Name == StarString);
                    if (expandAll != null)
                    {
                        clauses.Add($"{ODataLiteral.Expand}=*");
                    }
                    else
                    {
                        var expandedProperties = string.Join(",", association.ExpandAssociations
                                                             .Where(
                                                                 a => _session.Metadata.HasNavigationProperty(associatedEntityCollection.Name, a.Name))
                                                             .Select(a =>
                                                                     FormatExpansionSegment(a, associatedEntityCollection, ODataExpandOptions.ByValue(),
                                                                                            command,
                                                                                            false)));
                        if (!string.IsNullOrEmpty(expandedProperties))
                        {
                            clauses.Add($"{ODataLiteral.Expand}={expandedProperties}");
                        }
                    }

                    var selectColumns = string.Join(",", association.ExpandAssociations
                                                    .Where(a => a.Name != StarString &&
                                                           !_session.Metadata.HasNavigationProperty(associatedEntityCollection.Name, a.Name))
                                                    .Select(a => a.Name));
                    if (!string.IsNullOrEmpty(selectColumns))
                    {
                        clauses.Add($"{ODataLiteral.Select}={selectColumns}");
                    }
                }

                if (expandsToCollection && association.OrderByColumns.Any())
                {
                    var columns = string.Join(",", association.OrderByColumns
                                              .Select(o => o.Name + (o.Descending ? " desc" : string.Empty)));
                    if (!string.IsNullOrEmpty(columns))
                    {
                        clauses.Add($"{ODataLiteral.OrderBy}={columns}");
                    }
                }
            }

            if (clauses.Any())
            {
                text += $"({string.Join(";", clauses)})";
            }

            return(text);
        }
Esempio n. 8
0
 protected static IEnumerable <KeyValuePair <string, ODataExpandOptions> > FlatExpandAssociations(
     IEnumerable <KeyValuePair <ODataExpandAssociation, ODataExpandOptions> > associations)
 {
     return(associations
            .SelectMany(a => a.Key.ExpandAssociations.Any()
                             ? FlatExpandAssociations(a.Key.ExpandAssociations.Select(x =>
                                                                                      new KeyValuePair <ODataExpandAssociation, ODataExpandOptions>(x,
                                                                                                                                                    ODataExpandOptions.ByValue())))
                        .Select(x => new KeyValuePair <string, ODataExpandOptions>(a.Key.Name + "/" + x.Key, x.Value))
                             : new[] { new KeyValuePair <string, ODataExpandOptions>(a.Key.Name, a.Value) })
            .ToList());
 }
Esempio n. 9
0
 public FT Expand(ODataExpandOptions expandOptions, Expression <Func <T, object> > expression)
 {
     Command.Expand(expandOptions, expression.ExtractExpandAssociations(_session.TypeCache));
     return(this as FT);
 }
Esempio n. 10
0
 public FT Expand(ODataExpandOptions expandOptions, params ODataExpression[] associations)
 {
     Command.Expand(expandOptions, associations.Select(a => ODataExpandAssociation.From(a.Reference)));
     return(this as FT);
 }
Esempio n. 11
0
 public FT Expand(ODataExpandOptions expandOptions, params string[] associations)
 {
     Command.Expand(expandOptions, associations.Select(ODataExpandAssociation.From));
     return(this as FT);
 }
Esempio n. 12
0
 public FT Expand(ODataExpandOptions expandOptions, IEnumerable <string> associations)
 {
     Command.Expand(expandOptions, associations.Select(ODataExpandAssociation.From));
     return(this as FT);
 }
Esempio n. 13
0
 public FT Expand(ODataExpandOptions expandOptions)
 {
     Command.Expand(expandOptions);
     return(this as FT);
 }