internal FluentCommand(FluentCommand ancestor)
 {
     _schema = ancestor._schema;
     _parent = ancestor._parent;
     _collectionName = ancestor._collectionName;
     _collectionExpression = ancestor._collectionExpression;
     _derivedCollectionName = ancestor._derivedCollectionName;
     _derivedCollectionExpression = ancestor._derivedCollectionExpression;
     _functionName = ancestor._functionName;
     _keyValues = ancestor._keyValues;
     _namedKeyValues = ancestor._namedKeyValues;
     _entryData = ancestor._entryData;
     _parameters = ancestor._parameters;
     _filter = ancestor._filter;
     _filterExpression = ancestor._filterExpression;
     _filterExpression = ancestor._filterExpression;
     _skipCount = ancestor._skipCount;
     _topCount = ancestor._topCount;
     _expandAssociations = ancestor._expandAssociations;
     _selectColumns = ancestor._selectColumns;
     _orderbyColumns = ancestor._orderbyColumns;
     _computeCount = ancestor._computeCount;
     _inlineCount = ancestor._inlineCount;
     _linkName = ancestor._linkName;
     _linkExpression = ancestor._linkExpression;
 }
 public CommandDetails(Session session, FluentCommand parent, SimpleDictionary<object, IDictionary<string, object>> batchEntries)
 {
     this.Session = session;
     this.Parent = parent;
     this.SkipCount = -1;
     this.TopCount = -1;
     this.ExpandAssociations = new List<KeyValuePair<string, ODataExpandOptions>>();
     this.SelectColumns = new List<string>();
     this.OrderbyColumns = new List<KeyValuePair<string, bool>>();
     this.BatchEntries = batchEntries;
 }
        private async Task EnrichWithMediaPropertiesAsync(IEnumerable <AnnotatedEntry> entries, FluentCommand command, CancellationToken cancellationToken)
        {
            if (entries != null)
            {
                foreach (var entry in entries)
                {
                    await EnrichWithMediaPropertiesAsync(entry, command.Details.MediaProperties, cancellationToken);

                    if (cancellationToken.IsCancellationRequested)
                    {
                        cancellationToken.ThrowIfCancellationRequested();
                    }
                }
            }
        }
        private async Task <IEnumerable <IDictionary <string, object> > > ExecuteFunctionAsync(FluentCommand command, CancellationToken cancellationToken)
        {
            var commandText = await command.GetCommandTextAsync(cancellationToken).ConfigureAwait(false);

            if (cancellationToken.IsCancellationRequested)
            {
                cancellationToken.ThrowIfCancellationRequested();
            }

            var request = await _session.Adapter.GetRequestWriter(_lazyBatchWriter)
                          .CreateFunctionRequestAsync(commandText, command.FunctionName).ConfigureAwait(false);

            return(await ExecuteRequestWithResultAsync(request, cancellationToken,
                                                       x => x.AsEntries(_session.Settings.IncludeAnnotationsInResults),
                                                       () => new IDictionary <string, object>[] { }).ConfigureAwait(false));
        }
 public FluentCommand(ISchema schema, FluentCommand parent)
 {
     _schema = schema;
     _parent = parent;
 }
 internal FluentCommand(Session session, FluentCommand parent, SimpleDictionary<object, IDictionary<string, object>> batchEntries)
 {
     _details = new CommandDetails(session, parent, batchEntries);
 }
Example #7
0
        public string FormatCommand(FluentCommand command)
        {
            if (command.HasKey && command.HasFilter)
            {
                throw new InvalidOperationException("OData filter and key may not be combined.");
            }

            if (command.HasFunction && command.HasAction)
            {
                throw new InvalidOperationException("OData function and action may not be combined.");
            }

            var commandText = string.Empty;

            if (!string.IsNullOrEmpty(command.Details.CollectionName))
            {
                commandText += _session.Metadata.GetEntityCollectionExactName(command.Details.CollectionName);
            }
            else if (!string.IsNullOrEmpty(command.Details.LinkName))
            {
                var parent = new FluentCommand(command.Details.Parent).Resolve();
                commandText += string.Format("{0}/{1}",
                                             FormatCommand(parent),
                                             _session.Metadata.GetNavigationPropertyExactName(parent.EntityCollection.Name, command.Details.LinkName));
            }

            if (command.HasKey)
            {
                commandText += ConvertKeyValuesToUriLiteral(command.KeyValues, true);
            }

            if (!string.IsNullOrEmpty(command.Details.MediaName))
            {
                commandText += "/" + (command.Details.MediaName == FluentCommand.MediaEntityLiteral
                    ? ODataLiteral.Value
                    : command.Details.MediaName);
            }
            else
            {
                if (!string.IsNullOrEmpty(command.Details.FunctionName) || !string.IsNullOrEmpty(command.Details.ActionName))
                {
                    if (!string.IsNullOrEmpty(command.Details.CollectionName) || !string.IsNullOrEmpty(command.Details.LinkName))
                    {
                        commandText += "/";
                    }
                    if (!string.IsNullOrEmpty(command.Details.FunctionName))
                    {
                        commandText += _session.Metadata.GetFunctionFullName(command.Details.FunctionName);
                    }
                    else
                    {
                        commandText += _session.Metadata.GetActionFullName(command.Details.ActionName);
                    }
                }

                if (!string.IsNullOrEmpty(command.Details.FunctionName) && FunctionFormat == FunctionFormat.Key)
                {
                    commandText += ConvertKeyValuesToUriLiteral(command.CommandData, false);
                }

                if (!string.IsNullOrEmpty(command.Details.DerivedCollectionName))
                {
                    commandText += "/" + _session.Metadata.GetQualifiedTypeName(command.Details.DerivedCollectionName);
                }

                commandText += FormatClauses(command);
            }

            return(commandText);
        }
Example #8
0
 protected abstract void FormatExpandSelectOrderby(IList <string> commandClauses, EntityCollection resultCollection, FluentCommand command);
Example #9
0
 internal FluentCommand(FluentCommand ancestor)
 {
     _details = new CommandDetails(ancestor._details);
 }
Example #10
0
        internal async Task <IEnumerable <IDictionary <string, object> > > FindEntriesAsync(FluentCommand command, ODataFeedAnnotations annotations, CancellationToken cancellationToken)
        {
            if (IsBatchResponse)
            {
                annotations.CopyFrom(_batchResponse.Annotations);
                return(_batchResponse.AsEntries());
            }

            await _session.ResolveAdapterAsync(cancellationToken);

            if (cancellationToken.IsCancellationRequested)
            {
                cancellationToken.ThrowIfCancellationRequested();
            }

            var commandText = await command.GetCommandTextAsync(cancellationToken);

            if (cancellationToken.IsCancellationRequested)
            {
                cancellationToken.ThrowIfCancellationRequested();
            }

            return(await FindEntriesAsync(commandText, annotations, cancellationToken));
        }
Example #11
0
        internal async Task <IEnumerable <IDictionary <string, object> > > FindEntriesAsync(FluentCommand command, bool scalarResult, CancellationToken cancellationToken)
        {
            if (IsBatchResponse)
            {
                return(_batchResponse.AsEntries());
            }

            await _session.ResolveAdapterAsync(cancellationToken);

            if (cancellationToken.IsCancellationRequested)
            {
                cancellationToken.ThrowIfCancellationRequested();
            }

            var commandText = await command.GetCommandTextAsync(cancellationToken);

            if (cancellationToken.IsCancellationRequested)
            {
                cancellationToken.ThrowIfCancellationRequested();
            }

            return(await FindEntriesAsync(commandText, scalarResult, cancellationToken));
        }
Example #12
0
        internal async Task <IEnumerable <IDictionary <string, object> > > ExecuteAsEnumerableAsync(FluentCommand command, CancellationToken cancellationToken)
        {
            if (IsBatchResponse)
            {
                return(_batchResponse.AsEntries());
            }

            await _session.ResolveAdapterAsync(cancellationToken);

            if (cancellationToken.IsCancellationRequested)
            {
                cancellationToken.ThrowIfCancellationRequested();
            }

            if (command.HasFunction)
            {
                return(await ExecuteFunctionAsync(command, cancellationToken));
            }
            else if (command.HasAction)
            {
                return(await ExecuteActionAsync(command, cancellationToken));
            }
            else
            {
                throw new InvalidOperationException("Command is expected to be a function or an action.");
            }
        }
Example #13
0
        internal async Task <IEnumerable <IDictionary <string, object> > > UpdateEntriesAsync(FluentCommand command, IDictionary <string, object> entryData, bool resultRequired, CancellationToken cancellationToken)
        {
            if (IsBatchResponse)
            {
                return(_batchResponse.AsEntries());
            }

            await _session.ResolveAdapterAsync(cancellationToken);

            if (cancellationToken.IsCancellationRequested)
            {
                cancellationToken.ThrowIfCancellationRequested();
            }

            return(await ExecuteUpdateEntriesAsync(command, resultRequired, cancellationToken));
        }
        private async Task <IEnumerable <IDictionary <string, object> > > ExecuteActionAsync(FluentCommand command, CancellationToken cancellationToken)
        {
            var commandText = await command.GetCommandTextAsync(cancellationToken);

            if (cancellationToken.IsCancellationRequested)
            {
                cancellationToken.ThrowIfCancellationRequested();
            }

            var request = await _session.Adapter.GetRequestWriter(_lazyBatchWriter)
                          .CreateActionRequestAsync(commandText, command.ActionName, command.CommandData, true);

            return(await ExecuteRequestWithResultAsync(request, cancellationToken,
                                                       x => x.AsEntries(),
                                                       () => new[] { (IDictionary <string, object>)null }));
        }
        public string FormatCommand(ResolvedCommand command)
        {
            if (command.Details.HasFunction && command.Details.HasAction)
            {
                throw new InvalidOperationException("OData function and action may not be combined.");
            }

            var commandText = string.Empty;

            if (!string.IsNullOrEmpty(command.Details.CollectionName))
            {
                commandText += _session.Metadata.GetEntityCollectionExactName(command.Details.CollectionName);
            }
            else if (!string.IsNullOrEmpty(command.Details.LinkName))
            {
                var parent = new FluentCommand(command.Details.Parent).Resolve(_session);
                commandText += $"{FormatCommand(parent)}/{_session.Metadata.GetNavigationPropertyExactName(parent.EntityCollection.Name, command.Details.LinkName)}";
            }

            if (command.Details.HasKey)
            {
                commandText += ConvertKeyValuesToUriLiteral(command.KeyValues, !command.Details.IsAlternateKey);
            }

            var collectionValues = new List <string>();

            if (!string.IsNullOrEmpty(command.Details.MediaName))
            {
                commandText += "/" + (command.Details.MediaName == FluentCommand.MediaEntityLiteral
                    ? ODataLiteral.Value
                    : command.Details.MediaName);
            }
            else
            {
                if (!string.IsNullOrEmpty(command.Details.FunctionName) || !string.IsNullOrEmpty(command.Details.ActionName))
                {
                    if (!string.IsNullOrEmpty(command.Details.CollectionName) || !string.IsNullOrEmpty(command.Details.LinkName))
                    {
                        commandText += "/";
                    }
                    if (!string.IsNullOrEmpty(command.Details.FunctionName))
                    {
                        commandText += _session.Metadata.GetFunctionFullName(command.Details.FunctionName);
                    }
                    else
                    {
                        commandText += _session.Metadata.GetActionFullName(command.Details.ActionName);
                    }
                }

                if (!string.IsNullOrEmpty(command.Details.FunctionName) && FunctionFormat == FunctionFormat.Key)
                {
                    commandText += ConvertKeyValuesToUriLiteralExtractCollections(command.CommandData, collectionValues, false);
                }

                if (!string.IsNullOrEmpty(command.Details.DerivedCollectionName))
                {
                    commandText += "/" + _session.Metadata.GetQualifiedTypeName(command.Details.DerivedCollectionName);
                }
            }

            commandText += FormatClauses(command, collectionValues);
            return(commandText);
        }
        private async Task <IEnumerable <IDictionary <string, object> > > ExecuteActionAsync(FluentCommand command, CancellationToken cancellationToken)
        {
            var commandText = await command.GetCommandTextAsync(cancellationToken).ConfigureAwait(false);

            if (cancellationToken.IsCancellationRequested)
            {
                cancellationToken.ThrowIfCancellationRequested();
            }

            var entityTypeName = command.EntityCollection != null
                ? _session.Metadata.GetQualifiedTypeName(command.EntityCollection.Name)
                : null;

            var request = await _session.Adapter.GetRequestWriter(_lazyBatchWriter)
                          .CreateActionRequestAsync(commandText, command.ActionName, entityTypeName, command.CommandData, true).ConfigureAwait(false);

            return(await ExecuteRequestWithResultAsync(request, cancellationToken,
                                                       x => x.AsEntries(_session.Settings.IncludeAnnotationsInResults),
                                                       () => new IDictionary <string, object>[] { }).ConfigureAwait(false));
        }
Example #17
0
 internal FluentCommand(Session session, FluentCommand parent, ConcurrentDictionary <object, IDictionary <string, object> > batchEntries)
 {
     _details = new CommandDetails(session, parent, batchEntries);
 }
Example #18
0
 internal FluentCommand(FluentCommand parent, ConcurrentDictionary <object, IDictionary <string, object> > batchEntries)
 {
     Details = new FluentCommandDetails(parent?.Details, batchEntries);
 }
Example #19
0
 public RequestBuilder(FluentCommand command, Session session, Lazy <IBatchWriter> batchWriter)
 {
     _command         = command;
     _session         = session;
     _lazyBatchWriter = batchWriter;
 }
Example #20
0
 private async Task <IEnumerable <IDictionary <string, object> > > ExecuteUpdateEntriesAsync(FluentCommand command, bool resultRequired, CancellationToken cancellationToken)
 {
     return(await IterateEntriesAsync(
                command, resultRequired,
                async (x, y, z, w) => await UpdateEntryAsync(x, y, z, w, cancellationToken),
                cancellationToken));
 }
Example #21
0
        private string FormatClauses(FluentCommand command)
        {
            var text             = string.Empty;
            var extraClauses     = new List <string>();
            var aggregateClauses = new List <string>();

            if (command.CommandData.Any() && !string.IsNullOrEmpty(command.Details.FunctionName) &&
                FunctionFormat == FunctionFormat.Query)
            {
                extraClauses.Add(string.Join("&", command.CommandData.Select(x => string.Format("{0}={1}",
                                                                                                x.Key, ConvertValueToUriLiteral(x.Value, true)))));
            }

            if (command.Details.Filter != null)
            {
                extraClauses.Add(string.Format("{0}={1}", ODataLiteral.Filter, Uri.EscapeDataString(command.Details.Filter)));
            }

            if (command.Details.QueryOptions != null)
            {
                extraClauses.Add(command.Details.QueryOptions);
            }

            var details = command.Details;

            if (!ReferenceEquals(details.QueryOptionsExpression, null))
            {
                extraClauses.Add(details.QueryOptionsExpression.Format(new ExpressionContext(details.Session, true)));
            }
            if (command.Details.QueryOptionsKeyValues != null)
            {
                foreach (var kv in command.Details.QueryOptionsKeyValues)
                {
                    extraClauses.Add(string.Format("{0}={1}", kv.Key, ODataExpression.FromValue(kv.Value).Format(new ExpressionContext(details.Session))));
                }
            }

            if (command.Details.SkipCount >= 0)
            {
                extraClauses.Add(string.Format("{0}={1}", ODataLiteral.Skip, command.Details.SkipCount));
            }

            if (command.Details.TopCount >= 0)
            {
                extraClauses.Add(string.Format("{0}={1}", ODataLiteral.Top, command.Details.TopCount));
            }

            EntityCollection resultCollection;

            if (command.HasFunction)
            {
                resultCollection = _session.Adapter.GetMetadata().GetFunctionReturnCollection(command.Details.FunctionName);
            }
            else if (command.HasAction)
            {
                resultCollection = _session.Adapter.GetMetadata().GetActionReturnCollection(command.Details.ActionName);
            }
            else
            {
                resultCollection = command.EntityCollection;
            }
            if (resultCollection != null)
            {
                FormatExpandSelectOrderby(extraClauses, resultCollection, command);
            }

            if (command.Details.IncludeCount)
            {
                FormatInlineCount(extraClauses);
            }

            if (command.Details.ComputeCount)
            {
                aggregateClauses.Add(ODataLiteral.Count);
            }

            if (aggregateClauses.Any())
            {
                text += "/" + string.Join("/", aggregateClauses);
            }

            if (extraClauses.Any())
            {
                text += "?" + string.Join("&", extraClauses);
            }

            return(text);
        }
Example #22
0
        private async Task <IDictionary <string, object> > ExecuteUpdateEntryAsync(FluentCommand command, bool resultRequired, CancellationToken cancellationToken)
        {
            AssertHasKey(command);

            var collectionName = command.QualifiedEntityCollectionName;
            var entryKey       = command.HasKey ? command.KeyValues : command.FilterAsKey;
            var entryData      = command.CommandData;
            var entryIdent     = await FormatEntryKeyAsync(command, cancellationToken);

            var request = await _session.Adapter.GetRequestWriter(_lazyBatchWriter).CreateUpdateRequestAsync(collectionName, entryIdent, entryKey, entryData, resultRequired);

            if (cancellationToken.IsCancellationRequested)
            {
                cancellationToken.ThrowIfCancellationRequested();
            }

            var result = await ExecuteRequestWithResultAsync(request, cancellationToken,
                                                             x => x.AsEntry(_session.Settings.IncludeAnnotationsInResults), () => null, () => request.EntryData);

            if (cancellationToken.IsCancellationRequested)
            {
                cancellationToken.ThrowIfCancellationRequested();
            }

            if (result == null && resultRequired)
            {
                try
                {
                    result = await GetUpdatedResult(command, cancellationToken);

                    if (cancellationToken.IsCancellationRequested)
                    {
                        cancellationToken.ThrowIfCancellationRequested();
                    }
                }
                catch (Exception)
                {
                }
            }

            var entityCollection = _session.Metadata.GetEntityCollection(collectionName);
            var entryDetails     = _session.Metadata.ParseEntryDetails(entityCollection.Name, entryData);

            var removedLinks = entryDetails.Links
                               .SelectMany(x => x.Value.Where(y => y.LinkData == null))
                               .Select(x => _session.Metadata.GetNavigationPropertyExactName(entityCollection.Name, x.LinkName))
                               .ToList();

            foreach (var associationName in removedLinks)
            {
                try
                {
                    await UnlinkEntryAsync(collectionName, entryKey, associationName, cancellationToken);

                    if (cancellationToken.IsCancellationRequested)
                    {
                        cancellationToken.ThrowIfCancellationRequested();
                    }
                }
                catch (Exception)
                {
                }
            }

            return(result);
        }
Example #23
0
 internal BoundClient(ODataClient client, Session session, FluentCommand parentCommand = null, FluentCommand command = null, bool dynamicResults = false)
     : base(client, session, parentCommand, command, dynamicResults)
 {
 }
Example #24
0
 protected BoundClient <U> Link <U>(FluentCommand command, ODataExpression expression)
     where U : class
 {
     return(Link <U>(command, expression.Reference));
 }
 internal FluentCommand(FluentCommand ancestor)
 {
     _details = new CommandDetails(ancestor._details);
 }
        private string Format()
        {
            string commandText = string.Empty;
            if (!string.IsNullOrEmpty(_collectionName))
            {
                commandText += _schema.FindTable(_collectionName).ActualName;
                if (!string.IsNullOrEmpty(_derivedCollectionName))
                    commandText += "/" + string.Join(".",
                        _schema.TypesNamespace,
                        _schema.FindEntityType(_derivedCollectionName).Name);
            }
            else if (!string.IsNullOrEmpty(_linkName))
            {
                var parent = new FluentCommand(_parent).Resolve();
                commandText += parent.Format() + "/";
                commandText += parent.Table.FindAssociation(_linkName).ActualName;
            }
            else if (!string.IsNullOrEmpty(_functionName))
            {
                commandText += _schema.FindFunction(_functionName).ActualName;
            }

            if (HasKey && HasFilter)
                throw new InvalidOperationException("Filter may not be set when key is assigned");

            if (HasKey)
                commandText += FormatKey();

            commandText += FormatClauses();

            return commandText;
        }