Example #1
0
        private string RewriteEnumFilters(string resourcePath)
        {
            var pattern = @"(?<prefix>.*)cast\((?<enumName>.*),.*\)%20eq%20(?<enumValue>.[^%&)]*)(?<suffix>.*)";
            var m       = System.Text.RegularExpressions.Regex.Match(resourcePath, pattern);

            if (m.Success)
            {
                var enumName  = m.Groups["enumName"].Value;
                var enumValue = m.Groups["enumValue"].Value;
                var pinfo     = this.QueryableType.GetTypeInfo().GetDeclaredProperty(enumName);
                if (pinfo == null)
                {
                    return(resourcePath);
                }
                var enumType = TypeFns.GetNonNullableType(pinfo.PropertyType);
                if (!enumType.GetTypeInfo().IsEnum)
                {
                    return(resourcePath);
                }
                var enumString = "'" + Enum.GetName(enumType, Int32.Parse(enumValue)) + "'";
                var result     = m.Groups["prefix"].Value
                                 + enumName + "%20eq%20" + enumString
                                 + m.Groups["suffix"].Value;
                // in case there is another cast in the string.
                return(RewriteResourcePath(result));
            }
            else
            {
                return(resourcePath);
            }
        }
        private static Object ConvertValue(Object val, Type toType)
        {
            Object result;

            if (val == null)
            {
                return(val);
            }
            if (toType == val.GetType())
            {
                return(val);
            }
            var nnToType = TypeFns.GetNonNullableType(toType);

            if (typeof(IConvertible).IsAssignableFrom(nnToType))
            {
                result = Convert.ChangeType(val, nnToType, System.Threading.Thread.CurrentThread.CurrentCulture);
            }
            else if (val is JObject)
            {
                var serializer = new JsonSerializer();
                result = serializer.Deserialize(new JTokenReader((JObject)val), toType);
            }
            else
            {
                // Guids fail above - try this
                TypeConverter typeConverter = TypeDescriptor.GetConverter(toType);
                result = typeConverter.ConvertFrom(val);
            }
            return(result);
        }
        private DataProperty CreateDataProperty(StructuralType structuralType, PropertyInfo pInfo)
        {
            var propType = pInfo.PropertyType;
            var dp       = new DataProperty(pInfo.Name);

            // TODO: handle isScalar
            if (typeof(IComplexObject).IsAssignableFrom(propType))
            {
                dp.ComplexType = MetadataStore.GetComplexType(propType);
                dp.IsNullable  = false;
                // complex Objects do not have defaultValues currently
            }
            else
            {
                var nnType = TypeFns.GetNonNullableType(propType);
                dp.ClrType = propType;

                dp.DataType     = DataType.FromClrType(nnType);
                dp.IsNullable   = TypeFns.IsNullableType(propType);
                dp.DefaultValue = dp.IsNullable ? null : dp.DataType.DefaultValue;
                var isEnumType = nnType.GetTypeInfo().IsEnum;
                if (isEnumType)
                {
                    dp.EnumTypeName = nnType.FullName;
                }
            }

            structuralType.AddDataProperty(dp);
            return(dp);
        }
Example #4
0
        public override void Validate(Type entityType)
        {
            if (Expr1Source == null)
            {
                throw new Exception("Unable to validate 1st expression: " + Expr1Source);
            }

            if (Expr1Source is string expr1SourceString)
            {
                if (expr1SourceString.EndsWith("Id") && entityType.GetProperty(expr1SourceString) == null)
                {
                    var match = _navigationPropertyNameRegex.Match(expr1SourceString);

                    if (match.Success)
                    {
                        expr1SourceString = $"{match.Groups[1].Value}.Id";
                        Expr1Source       = expr1SourceString;
                    }
                }
            }

            _block1 = BaseBlock.CreateLHSBlock(Expr1Source, entityType);

            if (_op == Operator.In && !(Expr2Source is IList))
            {
                throw new Exception("The 'in' operator requires that its right hand argument be an array");
            }

            // Special purpose Enum handling

            var enumType = GetEnumType(_block1);

            if (enumType != null)
            {
                if (Expr2Source != null)
                {
                    var et        = TypeFns.GetNonNullableType(enumType);
                    var expr2Enum = Enum.Parse(et, (string)Expr2Source);
                    _block2 = BaseBlock.CreateRHSBlock(expr2Enum, entityType, null);
                }
                else
                {
                    _block2 = BaseBlock.CreateRHSBlock(null, entityType, null);
                }
            }
            else
            {
                try
                {
                    _block2 = BaseBlock.CreateRHSBlock(Expr2Source, entityType, _block1.DataType);
                }
                catch (Exception)
                {
                    _block2 = BaseBlock.CreateRHSBlock(Expr2Source, entityType, null);
                }
            }
        }
Example #5
0
        private string NormalizeDataTypeName(Type type)
        {
            type = TypeFns.GetNonNullableType(type);
            var result = type.ToString().Replace("System.", "");

            if (result == "Byte[]")
            {
                return("Binary");
            }
            else
            {
                return(result);
            }
        }
        private static Object ConvertValue(Object val, Type toType)
        {
            Object result;

            if (val == null)
            {
                return(val);
            }
            if (toType == val.GetType())
            {
                return(val);
            }
            var nnToType = TypeFns.GetNonNullableType(toType);

            if (nnToType.IsEnum && val is string)
            {
                result = Enum.Parse(nnToType, val as string, true);
            }
            else if (typeof(IConvertible).IsAssignableFrom(nnToType))
            {
                result = Convert.ChangeType(val, nnToType, System.Threading.Thread.CurrentThread.CurrentCulture);
            }
            else if (val is JObject)
            {
                var serializer = new JsonSerializer();
                result = serializer.Deserialize(new JTokenReader((JObject)val), toType);
            }
            else
            {
                // Guids fail above - try this
                TypeConverter typeConverter = TypeDescriptor.GetConverter(toType);
                if (typeConverter.CanConvertFrom(val.GetType()))
                {
                    result = typeConverter.ConvertFrom(val);
                }
                else if (val is DateTime && toType == typeof(DateTimeOffset))
                {
                    // handle case where JSON deserializes to DateTime, but toType is DateTimeOffset.  DateTimeOffsetConverter doesn't work!
                    result = new DateTimeOffset((DateTime)val);
                }
                else
                {
                    result = val;
                }
            }
            return(result);
        }
Example #7
0
 private Expression GetPropertyExpression(Expression expr, string propertyName, Type returnType)
 {
     if (TypeFns.IsNullableType(expr.Type))
     {
         var nullBaseExpression = Expression.Constant(null, expr.Type);
         var test = Expression.Equal(expr, nullBaseExpression);
         expr = Expression.Convert(expr, TypeFns.GetNonNullableType(expr.Type));
         Expression propExpr = Expression.PropertyOrField(expr, propertyName);
         propExpr = Expression.Convert(propExpr, TypeFns.GetNullableType(returnType));
         var nullExpr = Expression.Constant(null, TypeFns.GetNullableType(returnType));
         return(Expression.Condition(test, nullExpr, propExpr));
     }
     else
     {
         return(Expression.PropertyOrField(expr, propertyName));
     }
 }
        private EntityInfo RestoreOriginal(EntityInfo entityInfo)
        {
            // fk's can get cleared depending on the order in which deletions occur -
            // EF needs the original values of these fk's under certain circumstances - ( not sure entirely what these are).
            // so we restore the original fk values right before we attach the entity
            // shouldn't be any side effects because we delete it immediately after.
            // ??? Do concurrency values also need to be restored in some cases
            // This method restores more than it actually needs to because we don't
            // have metadata easily avail here, but usually a deleted entity will
            // not have much in the way of OriginalValues.
            if (entityInfo.OriginalValuesMap == null || entityInfo.OriginalValuesMap.Keys.Count == 0)
            {
                return(entityInfo);
            }
            var entity     = entityInfo.Entity;
            var entityType = entity.GetType();
            //var efEntityType = GetEntityType(ObjectContext.MetadataWorkspace, entityType);
            //var keyPropertyNames = efEntityType.KeyMembers.Select(km => km.Name).ToList();
            var keyPropertyNames = GetKeyProperties(entityType).Select(kp => kp.Name).ToList();
            var ovl = entityInfo.OriginalValuesMap.ToList();

            for (var i = 0; i < ovl.Count; i++)
            {
                var kvp      = ovl[i];
                var propName = kvp.Key;
                // keys should be ignored
                if (keyPropertyNames.Contains(propName))
                {
                    continue;
                }
                var pi = entityType.GetProperty(propName);
                // unmapped properties should be ignored.
                if (pi == null)
                {
                    continue;
                }
                var nnPropType = TypeFns.GetNonNullableType(pi.PropertyType);
                // presumption here is that only a predefined type could be a fk or concurrency property
                if (TypeFns.IsPredefinedType(nnPropType))
                {
                    SetPropertyValue(entity, propName, kvp.Value);
                }
            }

            return(entityInfo);
        }
Example #9
0
        public void AddValidators(Type clrType)
        {
            if (!(this.IsNullable ?? false))
            {
                Validators.Add(MetaValidator.Required);
            }
            if (this.MaxLength != null)
            {
                Validators.Add(new MaxLengthMetaValidator(this.MaxLength.Value));
            }
            var validator = MetaValidator.FindValidator(TypeFns.GetNonNullableType(clrType));

            if (validator != null)
            {
                Validators.Add(validator);
            }
        }
Example #10
0
        public override void Validate(Type entityType)
        {
            if (Expr1Source == null)
            {
                throw new Exception("Unable to validate 1st expression: " + this.Expr1Source);
            }

            this._block1 = BaseBlock.CreateLHSBlock(Expr1Source, entityType);

            if (_op == Operator.In && !(Expr2Source is IList))
            {
                throw new Exception("The 'in' operator requires that its right hand argument be an array");
            }

            // Special purpose Enum handling

            var enumType = GetEnumType(this._block1);

            if (enumType != null)
            {
                if (Expr2Source != null)
                {
                    var et        = TypeFns.GetNonNullableType(enumType);
                    var expr2Enum = Enum.Parse(et, (string)Expr2Source);
                    this._block2 = BaseBlock.CreateRHSBlock(expr2Enum, entityType, null);
                }
                else
                {
                    this._block2 = BaseBlock.CreateRHSBlock(null, entityType, null);
                }
            }
            else
            {
                try
                {
                    this._block2 = BaseBlock.CreateRHSBlock(Expr2Source, entityType, this._block1.DataType);
                }
                catch (Exception)
                {
                    this._block2 = BaseBlock.CreateRHSBlock(Expr2Source, entityType, null);
                }
            }
        }
Example #11
0
        public static DataType FromType(Type type)
        {
            var nnType = TypeFns.GetNonNullableType(type);

            return(_typeMap[nnType]);
        }