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; }
private static List<ODataExpression> MergeArguments(IEnumerable<object> arguments, ODataExpression argument) { var collection = new List<ODataExpression>(); collection.AddRange(arguments.Select(ODataExpression.FromValue)); collection.Add(argument); return collection; }
private static string FormatExpression(ODataExpression expr, ExpressionContext context) { if (ReferenceEquals(expr, null)) { return "null"; } else { return expr.Format(context); } }
protected internal ODataExpression(ODataExpression expression) { _functionCaller = expression._functionCaller; _left = expression._left; _right = expression._right; _operator = expression._operator; _conversionType = expression._conversionType; this.Reference = expression.Reference; this.Value = expression.Value; this.Function = expression.Function; }
internal string Format(ExpressionContext context) { if (_operator == ExpressionOperator.None && _conversionType == null) { return this.Reference != null ? FormatReference(context) : this.Function != null ? FormatFunction(context) : FormatValue(context); } else if (_conversionType != null) { var expr = _left; if (expr.Reference == null && expr.Function == null) { object result; if (Utils.TryConvert(expr.Value, _conversionType, out result)) { expr = new ODataExpression(result); } } return FormatExpression(expr, context); } else if (_operator == ExpressionOperator.NOT || _operator == ExpressionOperator.NEG) { var left = FormatExpression(_left, context); var op = FormatOperator(context); if (NeedsGrouping(_left)) return string.Format("{0}({1})", op, left); else return string.Format("{0} {1}", op, left); } else { var left = FormatExpression(_left, context); var right = FormatExpression(_right, context); var op = FormatOperator(context); if (NeedsGrouping(_left)) return string.Format("({0}) {1} {2}", left, op, right); else if (NeedsGrouping(_right)) return string.Format("{0} {1} ({2})", left, op, right); else return string.Format("{0} {1} {2}", left, op, right); } }
private static ODataExpression ParseBinaryExpression(ODataExpression leftExpression, ODataExpression rightExpression, Expression operandExpression) { switch (operandExpression.NodeType) { case ExpressionType.Equal: return(leftExpression == rightExpression); case ExpressionType.NotEqual: return(leftExpression != rightExpression); case ExpressionType.LessThan: return(leftExpression < rightExpression); case ExpressionType.LessThanOrEqual: return(leftExpression <= rightExpression); case ExpressionType.GreaterThan: return(leftExpression > rightExpression); case ExpressionType.GreaterThanOrEqual: return(leftExpression >= rightExpression); case ExpressionType.And: case ExpressionType.AndAlso: return(leftExpression && rightExpression); case ExpressionType.Or: case ExpressionType.OrElse: return(leftExpression || rightExpression); case ExpressionType.Add: case ExpressionType.AddChecked: return(leftExpression + rightExpression); case ExpressionType.Subtract: case ExpressionType.SubtractChecked: return(leftExpression - rightExpression); case ExpressionType.Multiply: case ExpressionType.MultiplyChecked: return(leftExpression * rightExpression); case ExpressionType.Divide: return(leftExpression / rightExpression); case ExpressionType.Modulo: return(leftExpression % rightExpression); } throw Utils.NotSupportedExpression(operandExpression); }
public Task UnlinkEntryAsync(ODataExpression expression, IDictionary <string, object> linkedEntryKey) { return(_client.UnlinkEntryAsync(_command, _command.KeyValues, expression.AsString(_session), linkedEntryKey != null ? linkedEntryKey.ToDictionary() : null, CancellationToken.None)); }
public void As(ODataExpression expression) { _derivedCollectionExpression = expression; }
public FluentCommand As(ODataExpression expression) { if (IsBatchResponse) return this; _details.DerivedCollectionExpression = expression; return this; }
public void Link(ODataExpression expression) { _linkExpression = expression; }
protected DynamicODataExpression(ODataExpression caller, ExpressionFunction function) : base(caller, function) { }
protected internal ODataExpression(ODataExpression expr, Type conversionType) { _conversionType = conversionType; this.Value = expr; }
protected DynamicODataExpression(ODataExpression caller, string reference) : base(caller, reference) { }
protected DynamicODataExpression(ODataExpression left, ODataExpression right, ExpressionType expressionOperator) : base(left, right, expressionOperator) { }
internal string Format(ExpressionContext context) { if (context.IsQueryOption && _operator != ExpressionType.Default && _operator != ExpressionType.And && _operator != ExpressionType.Equal) { throw new InvalidOperationException("Invalid custom query option"); } if (_operator == ExpressionType.Default && _conversionType == null) { return(this.Reference != null? FormatReference(context) : this.Function != null? FormatFunction(context) : FormatValue(context)); } else if (_conversionType != null) { var expr = _left; if (expr.Reference == null && expr.Function == null && expr._conversionType == null) { object result; if (expr.Value != null && expr.Value.GetType().IsEnumType()) { expr = new ODataExpression(expr.Value); } else if (Utils.TryConvert(expr.Value, _conversionType, out result)) { expr = new ODataExpression(result); } } return(FormatExpression(expr, context)); } else if (_operator == ExpressionType.Not || _operator == ExpressionType.Negate) { var left = FormatExpression(_left, context); var op = FormatOperator(context); if (NeedsGrouping(_left)) { return(string.Format("{0}({1})", op, left)); } else { return(string.Format("{0} {1}", op, left)); } } else { var left = FormatExpression(_left, context); var right = FormatExpression(_right, context); var op = FormatOperator(context); if (context.IsQueryOption) { return(string.Format("{0}{1}{2}", left, op, right)); } else { if (NeedsGrouping(_left)) { left = string.Format("({0})", left); } if (NeedsGrouping(_right)) { right = string.Format("({0})", right); } return(string.Format("{0} {1} {2}", left, op, right)); } } }
public FluentCommand For(ODataExpression expression) { Details.CollectionExpression = expression; return(this); }
public FluentCommand Link(ODataExpression expression) { Details.LinkExpression = expression; return(this); }
private FluentCommand Resolve() { if (!ReferenceEquals(_collectionExpression, null)) { For(_collectionExpression.AsString()); _collectionExpression = null; } if (!ReferenceEquals(_derivedCollectionExpression, null)) { As(_derivedCollectionExpression.AsString()); _derivedCollectionExpression = null; } if (!ReferenceEquals(_filterExpression, null)) { _namedKeyValues = TryInterpretFilterExpressionAsKey(_filterExpression); if (_namedKeyValues == null) { _filter = _filterExpression.Format(_schema, this.Table); } else { _topCount = -1; } _filterExpression = null; } if (!ReferenceEquals(_linkExpression, null)) { Link(_linkExpression.AsString()); _linkExpression = null; } return this; }
internal static ODataExpression FromFunction(string functionName, ODataExpression targetExpression, IEnumerable <Expression> arguments) { return(new ODataExpression( targetExpression, new ExpressionFunction(functionName, arguments))); }
protected internal ODataExpression(ODataExpression caller, string reference) { _functionCaller = caller; this.Reference = reference; }
protected DynamicODataExpression(ODataExpression caller, ExpressionFunction function) : base(caller, function) { }
protected DynamicODataExpression(ODataExpression left, ODataExpression right, ExpressionType expressionOperator) : base(left, right, expressionOperator) { }
public IBoundClient <ODataEntry> As(ODataExpression expression) { this.Command.As(expression); return(CreateClientForODataEntry()); }
public IBoundClient <ODataEntry> For(ODataExpression expression) { this.Command.For(expression.Reference); return(CreateClientForODataEntry()); }
public FT Filter(ODataExpression expression) { this.Command.Filter(expression); return(this as FT); }
protected internal ODataExpression(ODataExpression caller, ExpressionFunction function) { _functionCaller = caller; this.Function = function; }
public FT Filter(Expression <Func <T, bool> > expression) { this.Command.Filter(ODataExpression.FromLinqExpression(expression.Body)); return(this as FT); }
public FluentCommand Filter(ODataExpression expression) { if (IsBatchResponse) return this; if (ReferenceEquals(_details.FilterExpression, null)) _details.FilterExpression = expression; else _details.FilterExpression = _details.FilterExpression && expression; return this; }
public FT QueryOptions(ODataExpression expression) { this.Command.QueryOptions(expression); return(this as FT); }
public Task UnlinkEntryAsync(ODataExpression expression, CancellationToken cancellationToken) { return(_client.UnlinkEntryAsync(_command, _command.KeyValues, expression.AsString(_session), null, cancellationToken)); }
public FT QueryOptions <U>(Expression <Func <U, bool> > expression) { this.Command.QueryOptions(ODataExpression.FromLinqExpression(expression.Body)); return(this as FT); }
public Task UnlinkEntryAsync(ODataExpression expression, ODataEntry linkedEntryKey, CancellationToken cancellationToken) { return(_client.UnlinkEntryAsync(_command, _command.KeyValues, expression.AsString(_session), linkedEntryKey != null ? linkedEntryKey.ToDictionary() : null, cancellationToken)); }
public IMediaClient Media(ODataExpression expression) { this.Command.Media(expression); return(new MediaClient(_client, _session, this.Command, _dynamicResults)); }
protected override Expression VisitMethodCall(MethodCallExpression node) { if (node.Arguments.Count != 2) { throw Utils.NotSupportedExpression(node); } var association = AddNestedExpandAssociationAndGetDeepestChild(node.Arguments[0]); switch (node.Method.Name) { case "Select": { association.ExpandAssociations.AddRange(ExtractNestedExpandAssociations(node.Arguments[1])); return(node); } case "OrderBy": case "ThenBy": { if ((node.Arguments[0] as MethodCallExpression)?.Method.Name == "Select") { throw Utils.NotSupportedExpression(node); } association.OrderByColumns .AddRange(node.Arguments[1] .ExtractColumnNames(_typeCache).Select(c => new ODataOrderByColumn(c, false))); return(node); } case "OrderByDescending": case "ThenByDescending": { if ((node.Arguments[0] as MethodCallExpression)?.Method.Name == "Select") { throw Utils.NotSupportedExpression(node); } association.OrderByColumns .AddRange(node.Arguments[1] .ExtractColumnNames(_typeCache).Select(c => new ODataOrderByColumn(c, true))); return(node); } case "Where": { var filterExpression = ODataExpression.FromLinqExpression((node.Arguments[1] as LambdaExpression)?.Body); if (ReferenceEquals(association.FilterExpression, null)) { association.FilterExpression = filterExpression; } else { association.FilterExpression = association.FilterExpression && filterExpression; } return(node); } default: throw Utils.NotSupportedExpression(node); } }
protected BoundClient <U> Link <U>(FluentCommand command, ODataExpression expression) where U : class { return(Link <U>(command, expression.Reference)); }
protected internal ODataExpression(ODataExpression left, ODataExpression right, ExpressionType expressionOperator) { _left = left; _right = right; _operator = expressionOperator; }
/// <summary> /// Navigates to the linked entity. /// </summary> /// <param name="expression">The expression for the link.</param> /// <returns>Self.</returns> public IBoundClient <T> NavigateTo(ODataExpression expression) { return(this.Link <T>(this.Command, expression)); }
protected internal ODataExpression(ODataExpression caller, ExpressionFunction function) { _functionCaller = caller; this.Function = function; }
/// <summary> /// Returns an instance of a fluent OData client for the specified collection. /// </summary> /// <param name="expression">Collection expression.</param> /// <returns> /// The fluent OData client instance. /// </returns> public IBoundClient <ODataEntry> For(ODataExpression expression) { return(new BoundClient <ODataEntry>(this, _session).For(expression)); }
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, EscapeUnescapedString(command.Details.Filter))); } if (command.Details.Search != null) { extraClauses.Add(string.Format("{0}={1}", ODataLiteral.Search, EscapeUnescapedString(command.Details.Search))); } 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); }
private bool NeedsGrouping(ODataExpression expr) { if (_operator == ExpressionOperator.None) return false; if (ReferenceEquals(expr, null)) return false; if (expr._operator == ExpressionOperator.None) return false; int outerPrecedence = GetPrecedence(_operator); int innerPrecedence = GetPrecedence(expr._operator); return outerPrecedence < innerPrecedence; }
protected DynamicODataExpression(ODataExpression caller, string reference) : base(caller, reference) { }
public FluentCommand Media(ODataExpression expression) { return Media(expression.Reference); }
internal string Format(ExpressionContext context) { if (context.IsQueryOption && _operator != ExpressionOperator.None && _operator != ExpressionOperator.AND && _operator != ExpressionOperator.EQ) { throw new InvalidOperationException("Invalid custom query option"); } if (_operator == ExpressionOperator.None && _conversionType == null) { return this.Reference != null ? FormatReference(context) : this.Function != null ? FormatFunction(context) : FormatValue(context); } else if (_conversionType != null) { var expr = _left; if (expr.Reference == null && expr.Function == null && expr._conversionType == null) { object result; if (expr.Value != null && expr.Value.GetType().IsEnumType()) { expr = new ODataExpression(expr.Value); } else if (Utils.TryConvert(expr.Value, _conversionType, out result)) { expr = new ODataExpression(result); } } return FormatExpression(expr, context); } else if (_operator == ExpressionOperator.NOT || _operator == ExpressionOperator.NEG) { var left = FormatExpression(_left, context); var op = FormatOperator(context); if (NeedsGrouping(_left)) return string.Format("{0}({1})", op, left); else return string.Format("{0} {1}", op, left); } else { var left = FormatExpression(_left, context); var right = FormatExpression(_right, context); var op = FormatOperator(context); if (context.IsQueryOption) { return string.Format("{0}{1}{2}", left, op, right); } else { if (NeedsGrouping(_left)) return string.Format("({0}) {1} {2}", left, op, right); else if (NeedsGrouping(_right)) return string.Format("{0} {1} ({2})", left, op, right); else return string.Format("{0} {1} {2}", left, op, right); } } }
protected internal ODataExpression(ODataExpression left, ODataExpression right, ExpressionOperator expressionOperator) { _left = left; _right = right; _operator = expressionOperator; }
public FluentCommand Media(ODataExpression expression) { return(Media(expression.Reference)); }
public void Filter(ODataExpression expression) { _filterExpression = expression; }
public void For(ODataExpression expression) { For(expression.ToString()); }
public void As(ODataExpression expression) { As(expression.ToString()); }
private static ODataExpression ParseBinaryExpression(ODataExpression leftExpression, ODataExpression rightExpression, Expression operandExpression) { switch (operandExpression.NodeType) { case ExpressionType.Equal: return leftExpression == rightExpression; case ExpressionType.NotEqual: return leftExpression != rightExpression; case ExpressionType.LessThan: return leftExpression < rightExpression; case ExpressionType.LessThanOrEqual: return leftExpression <= rightExpression; case ExpressionType.GreaterThan: return leftExpression > rightExpression; case ExpressionType.GreaterThanOrEqual: return leftExpression >= rightExpression; case ExpressionType.And: case ExpressionType.AndAlso: return leftExpression && rightExpression; case ExpressionType.Or: case ExpressionType.OrElse: return leftExpression || rightExpression; case ExpressionType.Add: case ExpressionType.AddChecked: return leftExpression + rightExpression; case ExpressionType.Subtract: case ExpressionType.SubtractChecked: return leftExpression - rightExpression; case ExpressionType.Multiply: case ExpressionType.MultiplyChecked: return leftExpression * rightExpression; case ExpressionType.Divide: return leftExpression / rightExpression; case ExpressionType.Modulo: return leftExpression % rightExpression; } throw Utils.NotSupportedExpression(operandExpression); }
public void Filter(ODataExpression expression) { _namedKeyValues = TryInterpretFilterExpressionAsKey(expression); if (_namedKeyValues == null) { _filter = expression.Format(_schema, this.Table); } else { _topCount = -1; } }
protected internal ODataExpression(ODataExpression caller, string reference) { _functionCaller = caller; this.Reference = reference; }
private IDictionary<string, object> TryInterpretFilterExpressionAsKey(ODataExpression expression) { bool ok = false; IDictionary<string, object> namedKeyValues = new Dictionary<string, object>(); if (!ReferenceEquals(expression, null)) { ok = expression.ExtractEqualityComparisons(namedKeyValues); } return ok && this.Table.GetKeyNames().Count == namedKeyValues.Count() && this.Table.GetKeyNames().All(namedKeyValues.ContainsKey) ? namedKeyValues : null; }
protected internal ODataExpression(ODataExpression expr, Type conversionType) { _left = expr; _conversionType = conversionType; }
public Task LinkEntryAsync(ODataExpression expression, IDictionary <string, object> linkedEntryKey, CancellationToken cancellationToken) { return(_client.LinkEntryAsync(_command, _command.KeyValues, expression.AsString(_session), linkedEntryKey.ToDictionary(), cancellationToken)); }
public FluentCommand Link(ODataExpression expression) { if (IsBatchResponse) return this; _details.LinkExpression = expression; return this; }
public Task LinkEntryAsync(ODataExpression expression, ODataEntry linkedEntryKey) { return(_client.LinkEntryAsync(_command, _command.KeyValues, expression.AsString(_session), linkedEntryKey.ToDictionary(), CancellationToken.None)); }
private IDictionary<string, object> TryInterpretFilterExpressionAsKey(ODataExpression expression) { bool ok = false; IDictionary<string, object> namedKeyValues = new Dictionary<string, object>(); if (!ReferenceEquals(expression, null)) { ok = expression.ExtractLookupColumns(namedKeyValues); } if (!ok) return null; var keyNames = _details.Session.Metadata.GetDeclaredKeyPropertyNames(this.EntityCollection.Name).ToList(); return keyNames.Count == namedKeyValues.Count() && keyNames.All(namedKeyValues.ContainsKey) ? namedKeyValues : null; }
public void For(ODataExpression expression) { _collectionExpression = expression; }