private void AssertHasKey(ResolvedCommand command) { if (!command.Details.HasKey && command.FilterAsKey == null) { throw new InvalidOperationException("No entry key specified."); } }
private async Task <int> IterateEntriesAsync(ResolvedCommand command, Func <string, IDictionary <string, object>, Task> funcAsync, CancellationToken cancellationToken) { var collectionName = command.QualifiedEntityCollectionName; var result = 0; var client = new ODataClient(_settings); var entries = await client.FindEntriesAsync(command.Format(), cancellationToken).ConfigureAwait(false); if (entries != null) { var entryList = entries.ToList(); foreach (var entry in entryList) { await funcAsync(collectionName, entry).ConfigureAwait(false); if (cancellationToken.IsCancellationRequested) { cancellationToken.ThrowIfCancellationRequested(); } ++result; } } return(result); }
private async Task <IEnumerable <IDictionary <string, object> > > IterateEntriesAsync( ResolvedCommand command, bool resultRequired, Func <string, IDictionary <string, object>, IDictionary <string, object>, bool, Task <IDictionary <string, object> > > funcAsync, CancellationToken cancellationToken) { var collectionName = command.QualifiedEntityCollectionName; var entryData = command.CommandData; IEnumerable <IDictionary <string, object> > result = null; var client = new ODataClient(_settings); var entries = await client.FindEntriesAsync(command.Format(), cancellationToken).ConfigureAwait(false); if (entries != null) { var entryList = entries.ToList(); var resultList = new List <IDictionary <string, object> >(); foreach (var entry in entryList) { resultList.Add(await funcAsync(collectionName, entry, entryData, resultRequired).ConfigureAwait(false)); if (cancellationToken.IsCancellationRequested) { cancellationToken.ThrowIfCancellationRequested(); } } result = resultList; } return(result); }
private string FormatEntryKey(ResolvedCommand command) { var entryIdent = command.Details.HasKey ? command.Format() : new FluentCommand(command).Key(command.FilterAsKey).Resolve(_session).Format(); return(entryIdent); }
private async Task <IDictionary <string, object> > GetUpdatedResult(ResolvedCommand command, CancellationToken cancellationToken) { var entryKey = command.Details.HasKey ? command.KeyValues : command.FilterAsKey; var entryData = command.CommandData; var updatedKey = entryKey.Where(x => !entryData.ContainsKey(x.Key)).ToIDictionary(); foreach (var item in entryData.Where(x => entryKey.ContainsKey(x.Key))) { updatedKey.Add(item); } var updatedCommand = new FluentCommand(command).Key(updatedKey); return(await FindEntryAsync(updatedCommand.Resolve(_session).Format(), cancellationToken).ConfigureAwait(false)); }
public RequestBuilder(ResolvedCommand command, Session session, Lazy <IBatchWriter> batchWriter) { _command = command; _session = session; _lazyBatchWriter = batchWriter; }
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 string FormatClauses(ResolvedCommand command, IList <string> queryClauses = null) { var text = string.Empty; queryClauses = queryClauses ?? new List <string>(); var aggregateClauses = new List <string>(); if (command.CommandData.Any() && !string.IsNullOrEmpty(command.Details.FunctionName) && FunctionFormat == FunctionFormat.Query) { queryClauses.Add(string.Join("&", command.CommandData.Select(x => $"{x.Key}={ConvertValueToUriLiteral(x.Value, true)}"))); } if (command.Details.Filter != null) { queryClauses.Add($"{ODataLiteral.Filter}={EscapeUnescapedString(command.Details.Filter)}"); } if (command.Details.Search != null) { queryClauses.Add($"{ODataLiteral.Search}={EscapeUnescapedString(command.Details.Search)}"); } if (command.Details.QueryOptions != null) { queryClauses.Add(command.Details.QueryOptions); } var details = command.Details; if (!ReferenceEquals(details.QueryOptionsExpression, null)) { queryClauses.Add(details.QueryOptionsExpression.Format(new ExpressionContext(_session, true))); } if (command.Details.QueryOptionsKeyValues != null) { foreach (var kv in command.Details.QueryOptionsKeyValues) { queryClauses.Add($"{kv.Key}={ODataExpression.FromValue(kv.Value).Format(new ExpressionContext(_session))}"); } } if (command.Details.SkipCount >= 0) { queryClauses.Add($"{ODataLiteral.Skip}={command.Details.SkipCount}"); } if (command.Details.TopCount >= 0) { queryClauses.Add($"{ODataLiteral.Top}={command.Details.TopCount}"); } EntityCollection resultCollection; if (command.Details.HasFunction) { resultCollection = _session.Adapter.GetMetadata().GetFunctionReturnCollection(command.Details.FunctionName); } else if (command.Details.HasAction) { resultCollection = _session.Adapter.GetMetadata().GetActionReturnCollection(command.Details.ActionName); } else { resultCollection = command.EntityCollection; } if (resultCollection != null) { FormatExpandSelectOrderby(queryClauses, resultCollection, command); } if (command.Details.IncludeCount) { FormatInlineCount(queryClauses); } if (command.Details.ComputeCount) { aggregateClauses.Add(ODataLiteral.Count); } if (aggregateClauses.Any()) { text += "/" + string.Join("/", aggregateClauses); } if (queryClauses.Any()) { text += "?" + string.Join("&", queryClauses); } return(text); }
protected abstract void FormatExpandSelectOrderby(IList <string> commandClauses, EntityCollection resultCollection, ResolvedCommand command);
internal FluentCommand(ResolvedCommand command) { Details = new FluentCommandDetails(command.Details); }
private async Task <IEnumerable <IDictionary <string, object> > > ExecuteActionAsync(ResolvedCommand command, CancellationToken cancellationToken) { var entityTypeName = command.EntityCollection != null ? _session.Metadata.GetQualifiedTypeName(command.EntityCollection.Name) : null; var request = await _session.Adapter.GetRequestWriter(_lazyBatchWriter) .CreateActionRequestAsync(command.Format(), command.Details.ActionName, entityTypeName, command.CommandData, true, command.Details.Headers).ConfigureAwait(false); return(await ExecuteRequestWithResultAsync(request, cancellationToken, x => x.AsEntries(_session.Settings.IncludeAnnotationsInResults), () => Array.Empty <IDictionary <string, object> >()).ConfigureAwait(false)); }
private async Task EnrichWithMediaPropertiesAsync(IEnumerable <AnnotatedEntry> entries, ResolvedCommand command, CancellationToken cancellationToken) { if (entries != null) { foreach (var entry in entries) { await EnrichWithMediaPropertiesAsync(entry, command.Details.MediaProperties, cancellationToken).ConfigureAwait(false); if (cancellationToken.IsCancellationRequested) { cancellationToken.ThrowIfCancellationRequested(); } } } }
private async Task <IEnumerable <IDictionary <string, object> > > ExecuteFunctionAsync(ResolvedCommand command, CancellationToken cancellationToken) { var request = await _session.Adapter.GetRequestWriter(_lazyBatchWriter) .CreateFunctionRequestAsync(command.Format(), command.Details.FunctionName, command.Details.Headers).ConfigureAwait(false); return(await ExecuteRequestWithResultAsync(request, cancellationToken, x => x.AsEntries(_session.Settings.IncludeAnnotationsInResults), () => Array.Empty <IDictionary <string, object> >()).ConfigureAwait(false)); }
protected abstract void FormatExtensions(IList <string> commandClauses, ResolvedCommand command);