Exemplo n.º 1
0
        private string FormatFunction(ExpressionContext context)
        {
            FunctionMapping mapping;
            var             adapterVersion = context.Session == null ? AdapterVersion.Default : context.Session.Adapter.AdapterVersion;

            if (FunctionMapping.TryGetFunctionMapping(this.Function.FunctionName, this.Function.Arguments.Count(), adapterVersion, out mapping))
            {
                return(FormatMappedFunction(context, mapping));
            }
            else if (string.Equals(this.Function.FunctionName, ODataLiteral.Any, StringComparison.OrdinalIgnoreCase) ||
                     string.Equals(this.Function.FunctionName, ODataLiteral.All, StringComparison.OrdinalIgnoreCase))
            {
                return(FormatAnyAllFunction(context));
            }
            else if (string.Equals(this.Function.FunctionName, ODataLiteral.IsOf, StringComparison.OrdinalIgnoreCase) ||
                     string.Equals(this.Function.FunctionName, ODataLiteral.Cast, StringComparison.OrdinalIgnoreCase))
            {
                return(FormatIsOfCastFunction(context));
            }
            else if (string.Equals(this.Function.FunctionName, "get_Item", StringComparison.Ordinal) &&
                     this.Function.Arguments.Count == 1)
            {
                return(FormatArrayIndexFunction(context));
            }
            else if (string.Equals(this.Function.FunctionName, "ToString", StringComparison.Ordinal) &&
                     this.Function.Arguments.Count == 0)
            {
                return(_functionCaller.Reference);
            }
            else if (_functionCaller.IsNull && this.Function.Arguments.Count == 1)
            {
                var val = this.Function.Arguments.First();
                if (val.Value != null)
                {
                    var formattedVal = ODataExpression.FromValue(
                        string.Equals(this.Function.FunctionName, "ToBoolean", StringComparison.Ordinal) ? Convert.ToBoolean(val.Value) :
                        string.Equals(this.Function.FunctionName, "ToByte", StringComparison.Ordinal) ? Convert.ToByte(val.Value) :
                        string.Equals(this.Function.FunctionName, "ToChar", StringComparison.Ordinal) ? Convert.ToChar(val.Value) :
                        string.Equals(this.Function.FunctionName, "ToDateTime", StringComparison.Ordinal) ? Convert.ToDateTime(val.Value) :
                        string.Equals(this.Function.FunctionName, "ToDecimal", StringComparison.Ordinal) ? Convert.ToDecimal(val.Value) :
                        string.Equals(this.Function.FunctionName, "ToDouble", StringComparison.Ordinal) ? Convert.ToDouble(val.Value) :
                        string.Equals(this.Function.FunctionName, "ToInt16", StringComparison.Ordinal) ? Convert.ToInt16(val.Value) :
                        string.Equals(this.Function.FunctionName, "ToInt32", StringComparison.Ordinal) ? Convert.ToInt32(val.Value) :
                        string.Equals(this.Function.FunctionName, "ToInt64", StringComparison.Ordinal) ? Convert.ToInt64(val.Value) :
                        string.Equals(this.Function.FunctionName, "ToSByte", StringComparison.Ordinal) ? Convert.ToSByte(val.Value) :
                        string.Equals(this.Function.FunctionName, "ToSingle", StringComparison.Ordinal) ? Convert.ToSingle(val.Value) :
                        string.Equals(this.Function.FunctionName, "ToString", StringComparison.Ordinal) ? Convert.ToString(val.Value) :
                        string.Equals(this.Function.FunctionName, "ToUInt16", StringComparison.Ordinal) ? Convert.ToUInt16(val.Value) :
                        string.Equals(this.Function.FunctionName, "ToUInt32", StringComparison.Ordinal) ? Convert.ToUInt32(val.Value) :
                        string.Equals(this.Function.FunctionName, "ToUInt64", StringComparison.Ordinal) ? (object)Convert.ToUInt64(val.Value)
                        : null);
                    if (formattedVal.Value != null)
                    {
                        return(FormatExpression(formattedVal, context));
                    }
                }
            }

            throw new NotSupportedException(string.Format("The function {0} is not supported or called with wrong number of arguments", this.Function.FunctionName));
        }
Exemplo n.º 2
0
        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);
        }
Exemplo n.º 3
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.Search != null)
            {
                extraClauses.Add(string.Format("{0}={1}", ODataLiteral.Search, Uri.EscapeDataString(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);
        }