Ejemplo n.º 1
0
        /// <summary>
        /// Tries to extract the WellKnownTextSQL portion from an astoria spatial literal.
        /// </summary>
        /// <param name="spatialLiteral">The spatial literal.</param>
        /// <param name="prefix">The prefix.</param>
        /// <param name="wellKnownTextSql">The well known text SQL.</param>
        /// <returns>true if the extract was successful, false otherwise.</returns>
        private static bool TryExtractWellKnownTextSqlFromSpatialLiteral(string spatialLiteral, string prefix, out string wellKnownTextSql)
        {
            var worked = WebConvert.TryRemovePrefix(prefix, ref spatialLiteral);

            if (!worked)
            {
                wellKnownTextSql = null;
                return(false);
            }

            worked = WebConvert.TryRemoveQuotes(ref spatialLiteral);
            if (!worked)
            {
                wellKnownTextSql = null;
                return(false);
            }

            wellKnownTextSql = spatialLiteral;
            return(true);
        }
Ejemplo n.º 2
0
 private static bool QuotesAreValid(string text)
 {
     return(WebConvert.TryRemoveQuotes(ref text));
 }
Ejemplo n.º 3
0
        private Expression PromoteExpression(Expression expr, Type type, bool exact, bool isTypeCast = false)
        {
            Debug.Assert(expr != null, "expr != null");
            Debug.Assert(type != null, "type != null");
            if (expr.Type == type)
            {
                return(expr);
            }

            ConstantExpression ce = null;

            if (type == typeof(DateTime))
            {
                // Sometimes if the property type is DateTime? in provider, we pass the type as DateTimeOffset?
                // to ODataLib and it puts a Convert node to convert DateTimeOffset to DateTimeOffset?. Hence
                // to reach to the constant value, we need to handle the Convert node.
                if (expr.Type == typeof(DateTimeOffset) || expr.Type == typeof(DateTimeOffset?))
                {
                    if (expr.NodeType == ExpressionType.Convert)
                    {
                        ce = ((UnaryExpression)expr).Operand as ConstantExpression;
                    }
                }
            }

            if (ce == null)
            {
                ce = expr as ConstantExpression;
            }

            if (ce != null)
            {
                if (ce == ExpressionUtils.NullLiteral)
                {
                    if (WebUtil.TypeAllowsNull(type))
                    {
                        return(Expression.Constant(null, type));
                    }
                }
                else
                {
                    string text;
                    if (this.literals.TryGetValue(ce, out text))
                    {
                        Type   target = WebUtil.GetNonNullableType(type);
                        object value  = null;
                        if (ce.Type == typeof(string) && (target == typeof(Type) || target == typeof(ResourceType)))
                        {
                            // No qoutes required when the function is a cast
                            if (isTypeCast || WebConvert.TryRemoveQuotes(ref text))
                            {
                                ResourceType resourceType = this.tryResolveResourceType(text);
                                if (resourceType != null)
                                {
                                    if (target == typeof(Type))
                                    {
                                        if (resourceType.CanReflectOnInstanceType)
                                        {
                                            value = resourceType.InstanceType;
                                        }
                                    }
                                    else
                                    {
                                        if (resourceType.CanReflectOnInstanceType == false)
                                        {
                                            value = resourceType;
                                        }
                                    }
                                }
                            }
                        }
                        else if ((type == typeof(DateTime) || type == typeof(DateTime?)) &&
                                 (expr.Type == typeof(DateTimeOffset) || expr.Type == typeof(DateTimeOffset?)))
                        {
                            // Since the URI parser in ODataLib will always convert Constants as DateTimeOffset,
                            // and WCF DS Server supports DateTime clr type, we need to do the conversion whenever required.
                            value = WebUtil.ConvertDateTimeOffsetToDateTime((DateTimeOffset)ce.Value);
                        }
                        else
                        {
                            switch (Type.GetTypeCode(ce.Type))
                            {
                            case TypeCode.Int32:
                            case TypeCode.Int64:
                                value = ParseNumber(text, target);
                                break;

                            case TypeCode.Double:
                                if (target == typeof(decimal))
                                {
                                    value = ParseNumber(text, target);
                                }

                                break;
                            }
                        }

                        if (value != null)
                        {
                            return(Expression.Constant(value, type));
                        }
                    }
                }
            }

            if (IsCompatibleWith(expr.Type, type))
            {
                // (type != typeof(object) || expr.Type.IsValueType) part is added
                // to prevent cast to System.Object from non-value types (objects).
                if (type.IsValueType || exact && (type != typeof(object) || expr.Type.IsValueType))
                {
                    return(Expression.Convert(expr, type));
                }

                return(expr);
            }

            // Allow promotion from nullable to non-nullable by directly accessing underlying value.
            if (WebUtil.IsNullableType(expr.Type) && type.IsValueType)
            {
                Expression valueAccessExpression = Expression.Property(expr, "Value");
                valueAccessExpression = this.PromoteExpression(valueAccessExpression, type, exact, isTypeCast);
                return(valueAccessExpression);
            }

            return(null);
        }