Ejemplo n.º 1
0
        private bool IsFunction(string objectName, ExpressionContext context)
        {
            FunctionMapping mapping;
            var             adapterVersion = context.Session == null ? AdapterVersion.Default : context.Session.Adapter.AdapterVersion;

            return(FunctionMapping.TryGetFunctionMapping(objectName, 0, adapterVersion, out mapping));
        }
        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
            {
                throw new NotSupportedException(string.Format("The function {0} is not supported or called with wrong number of arguments", this.Function.FunctionName));
            }
        }
        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))
            {
                var mappedFunction     = mapping.FunctionMapper(this.Function.FunctionName, _functionCaller.Format(context), this.Function.Arguments).Function;
                var formattedArguments = string.Join(",", (IEnumerable <object>)mappedFunction.Arguments.Select(x => FormatExpression(x, context)));

                return(string.Format("{0}({1})",
                                     mappedFunction.FunctionName, formattedArguments));
            }
            else if (string.Equals(this.Function.FunctionName, ODataLiteral.Any, StringComparison.OrdinalIgnoreCase) ||
                     string.Equals(this.Function.FunctionName, ODataLiteral.All, StringComparison.OrdinalIgnoreCase))
            {
                var formattedArguments = string.Format("x{0}:x{0}/{1}",
                                                       ArgumentCounter >= 0 ? (1 + (ArgumentCounter++) % 9).ToString() : string.Empty,
                                                       FormatExpression(this.Function.Arguments.First(), new ExpressionContext(context.Session,
                                                                                                                               new EntityCollection(_functionCaller.Reference, context.EntityCollection))));

                return(string.Format("{0}/{1}({2})",
                                     _functionCaller.Format(context), this.Function.FunctionName.ToLower(), formattedArguments));
            }
            else
            {
                throw new NotSupportedException(string.Format("The function {0} is not supported or called with wrong number of arguments", this.Function.FunctionName));
            }
        }
Ejemplo n.º 4
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));
        }
        private string FormatAsFunction(string objectName, ExpressionContext context)
        {
            var adapterVersion = context.Session?.Adapter.AdapterVersion ?? AdapterVersion.Default;

            if (FunctionMapping.TryGetFunctionMapping(objectName, 0, adapterVersion, out var mapping))
            {
                var mappedFunction = mapping.FunctionMapper(objectName, _functionCaller, null).Function;
                return($"{mappedFunction.FunctionName}({FormatCallerReference()})");
            }
            else
            {
                return(null);
            }
        }
Ejemplo n.º 6
0
        private string FormatAsFunction(string objectName, ExpressionContext context)
        {
            FunctionMapping mapping;
            var             adapterVersion = context.Session == null ? AdapterVersion.Default : context.Session.Adapter.AdapterVersion;

            if (FunctionMapping.TryGetFunctionMapping(objectName, 0, adapterVersion, out mapping))
            {
                var mappedFunction = mapping.FunctionMapper(objectName, _functionCaller, null).Function;
                return(string.Format("{0}({1})", mappedFunction.FunctionName, FormatCallerReference()));
            }
            else
            {
                return(null);
            }
        }
        private bool IsFunction(string objectName, ExpressionContext context)
        {
            var adapterVersion = context.Session?.Adapter.AdapterVersion ?? AdapterVersion.Default;

            return(FunctionMapping.TryGetFunctionMapping(objectName, 0, adapterVersion, out _));
        }