Esempio n. 1
0
        private static IEnumerable <ODataExpandAssociation> MergeExpandAssociations(ODataExpandAssociation first, ODataExpandAssociation second)
        {
            if (first.Name != second.Name && first.Name != "*")
            {
                return new [] { first, second }
            }
            ;

            var result = first.Clone();

            result.OrderByColumns.AddRange(second.OrderByColumns.Except(first.OrderByColumns));
            result.ExpandAssociations.Clear();
            var groupedExpandAssociations = first.ExpandAssociations
                                            .Concat(second.ExpandAssociations)
                                            .GroupBy(x => x);
            var mergedExpandAssociations = groupedExpandAssociations
                                           .Select(x =>
            {
                var mainAssociation = x.Key;
                foreach (var association in x.Where(a => a != mainAssociation))
                {
                    mainAssociation = MergeExpandAssociations(mainAssociation, association).First();
                    mainAssociation.OrderByColumns.AddRange(association.OrderByColumns.Except(mainAssociation.OrderByColumns));
                }
                return(mainAssociation);
            });

            result.ExpandAssociations.AddRange(mergedExpandAssociations);

            return(new [] { result });
        }
Esempio n. 2
0
        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 (association.TypeName != null)
            {
                text = association.TypeName + "/" + text;
            }

            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. 3
0
 private static ODataExpandAssociation MergeExpandAssociations(ODataExpandAssociation expandAssociation, string path)
 {
     return(MergeExpandAssociations(expandAssociation, ODataExpandAssociation.From(path)).First());
 }
Esempio n. 4
0
 public FT Expand(ODataExpandOptions expandOptions, params ODataExpression[] associations)
 {
     Command.Expand(expandOptions, associations.Select(a => ODataExpandAssociation.From(a.Reference)));
     return(this as FT);
 }
    public void CreateExpandAssociationFromString()
    {
        var association = ODataExpandAssociation.From("Products");

        Assert.Equal("Products", association.Name);
    }
 public void CreateExpandAssociationFromEmptyStringThrowsArgumentException()
 {
     Assert.Throws <ArgumentException>(() => ODataExpandAssociation.From(string.Empty));
 }
 public void CreateExpandAssociationFromNullStringThrowsArgumentException()
 {
     Assert.Throws <ArgumentException>(() => ODataExpandAssociation.From(null));
 }