예제 #1
0
        static DBInitializer()
        {
            DefaultCRUDRepository <TestContext, ReferenceModel> .
            DeclareProjection(m => new ReferenceType
            {
                AMonth  = Month.FromDateTime(m.AMonth),
                ANMonth = Month.FromDateTime(m.ANMonth.Value),
                AWeek   = Week.FromDateTime(m.AWeek),
                ANWeek  = Week.FromDateTime(m.ANWeek.Value)
            });

            DefaultCRUDRepository <TestContext, ReferenceModel> .
            DeclareProjection <IFilterReferenceTypeWithChildren>(m => new ReferenceTypeWithChildren
            {
                AMonth   = Month.FromDateTime(m.AMonth),
                ANMonth  = Month.FromDateTime(m.ANMonth.Value),
                AWeek    = Week.FromDateTime(m.AWeek),
                ANWeek   = Week.FromDateTime(m.ANWeek.Value),
                Children = m.Children.Select(l => new NestedReferenceType {
                })
            });

            DefaultCRUDRepository <TestContext, ReferenceModel> .
            DeclareProjection <ReferenceTypeWithChildren>(m => new ReferenceTypeWithChildren
            {
                AMonth   = Month.FromDateTime(m.AMonth),
                ANMonth  = Month.FromDateTime(m.ANMonth.Value),
                AWeek    = Week.FromDateTime(m.AWeek),
                ANWeek   = Week.FromDateTime(m.ANWeek.Value),
                Children = m.Children.Select(l => new NestedReferenceType {
                })
            });
        }
        private object convertValue(object value, out short dateTimeType, Type propertyType)
        {
            dateTimeType = 0;
            propertyType = Nullable.GetUnderlyingType(propertyType) ?? propertyType;
            if (value == null)
            {
                return(null);
            }
            if (value is Microsoft.OData.Edm.Date)
            {
                Microsoft.OData.Edm.Date dt = (Microsoft.OData.Edm.Date)value;
                value        = new DateTime(dt.Year, dt.Month, dt.Day, 0, 0, 0, DateTimeKind.Unspecified);
                dateTimeType = QueryFilterCondition.IsDate;
                if (propertyType == typeof(Month))
                {
                    value = Month.FromDateTime((DateTime)value);
                }
                else if (propertyType == typeof(Week))
                {
                    value = Week.FromDateTime((DateTime)value);
                }
            }
            else if (value is Microsoft.OData.Edm.TimeOfDay)
            {
                var tValue = (Microsoft.OData.Edm.TimeOfDay)value;
                value        = new TimeSpan(0, tValue.Hours, tValue.Minutes, tValue.Seconds, (int)tValue.Milliseconds);
                dateTimeType = QueryFilterCondition.IsTime;
            }
            else if (value is DateTimeOffset)
            {
                dateTimeType = QueryFilterCondition.IsDateTime;
                if (propertyType == typeof(DateTime))
                {
                    var cvalue = ((DateTimeOffset)value).UtcDateTime;
                    value = new DateTime(cvalue.Year, cvalue.Month, cvalue.Day,
                                         cvalue.Hour, cvalue.Minute, cvalue.Second, cvalue.Millisecond, DateTimeKind.Unspecified);
                }
            }
            else if (value is TimeSpan)
            {
                dateTimeType = QueryFilterCondition.IsDuration;
            }

            else if (value.GetType() != propertyType)
            {
                if (propertyType.GetTypeInfo().IsEnum)
                {
                    value = Enum.ToObject(propertyType, value);
                }
                else
                {
                    value = Convert.ChangeType(value, propertyType);
                }
            }
            return(value);
        }
예제 #3
0
        public void SingleTransformDefault()
        {
            MappingContext.Default
            .Add <ReferenceModel, ReferenceTypeWithChildren>(m => new ReferenceTypeWithChildren
            {
                AMonth   = Month.FromDateTime(m.AMonth),
                ANMonth  = Month.FromDateTime(m.ANMonth.Value),
                AWeek    = Week.FromDateTime(m.AWeek),
                ANWeek   = Week.FromDateTime(m.ANWeek.Value),
                Children = m.Children.Select(l => new NestedReferenceType {
                })
            });
            var res = allModels[0].Map().To <ReferenceTypeWithChildren>();

            Assert.NotNull(res);
            Assert.NotNull(res.Children);
            Assert.Equal(res.Children.Count(), 2);
            Assert.Equal(res.AWeek, Week.FromDateTime(new DateTime(2016, 10, 7)));
            Assert.Equal(res.AMonth, Month.FromDateTime(new DateTime(2016, 10, 7)));
            Assert.Equal(res.AString, "dummy1");
        }
예제 #4
0
        private object addDelay(string delayRef, ref IComparable value, object model, object fixedDelay = null, bool subtract = false)
        {
            if (delayRef != null && delayRef.StartsWith("!"))
            {
                if (TargetType == typeof(DateTime) || TargetType == typeof(DateTimeOffset) || TargetType == typeof(Month) || TargetType == typeof(Week))
                {
                    fixedDelay = TimeSpan.Parse(delayRef.Substring(1), CultureInfo.InvariantCulture);
                }
                else
                {
                    fixedDelay = Convert.ChangeType(delayRef.Substring(1), TargetType, CultureInfo.InvariantCulture);
                }
            }
            object toAdd = fixedDelay;

            if (fixedDelay == null)
            {
                if (string.IsNullOrWhiteSpace(delayRef))
                {
                    return(null);
                }
                PropertyAccessor delayProp = null;
                try
                {
                    delayProp = new PropertyAccessor(model, delayRef, false);
                }
                catch
                {
                    return(null);
                }
                if (delayProp == null)
                {
                    return(null);
                }
                toAdd = delayProp.Value;
            }
            if (toAdd == null)
            {
                return(null);
            }
            if (subtract)
            {
                if (TargetType == typeof(Int32))
                {
                    value = Convert.ToInt32(value) - Convert.ToInt32(toAdd);
                }
                else if (TargetType == typeof(Int16))
                {
                    value = Convert.ToInt16(value) - Convert.ToInt16(toAdd);
                }
                else if (TargetType == typeof(Int64))
                {
                    value = Convert.ToInt64(value) - Convert.ToInt64(toAdd);
                }
                else if (TargetType == typeof(UInt32))
                {
                    value = Convert.ToUInt32(value) - Convert.ToUInt32(toAdd);
                }
                else if (TargetType == typeof(UInt16))
                {
                    value = Convert.ToUInt16(value) - Convert.ToUInt16(toAdd);
                }
                else if (TargetType == typeof(UInt64))
                {
                    value = Convert.ToUInt64(value) - Convert.ToUInt64(toAdd);
                }
                else if (TargetType == typeof(byte))
                {
                    value = Convert.ToByte(value) - Convert.ToByte(toAdd);
                }
                else if (TargetType == typeof(sbyte))
                {
                    value = Convert.ToSByte(value) - Convert.ToSByte(toAdd);
                }
                else if (TargetType == typeof(decimal))
                {
                    value = Convert.ToDecimal(value) - Convert.ToDecimal(toAdd);
                }
                else if (TargetType == typeof(float))
                {
                    value = Convert.ToSingle(value) - Convert.ToSingle(toAdd);
                }
                else if (TargetType == typeof(double))
                {
                    value = Convert.ToDouble(value) - Convert.ToDouble(toAdd);
                }
                else if (TargetType == typeof(TimeSpan))
                {
                    value = (TimeSpan)value - (TimeSpan)toAdd;
                    toAdd = -Convert.ToInt64(((TimeSpan)toAdd).TotalMilliseconds);
                }
                else if (TargetType == typeof(DateTime))
                {
                    value = Convert.ToDateTime(value).Add(-(TimeSpan)toAdd);
                    toAdd = -Convert.ToInt64(((TimeSpan)toAdd).TotalMilliseconds);
                }
                else if (TargetType == typeof(DateTimeOffset))
                {
                    value = ((DateTimeOffset)value).Add(-(TimeSpan)toAdd);
                    toAdd = -Convert.ToInt64(((TimeSpan)toAdd).TotalMilliseconds);
                }
                else if (TargetType == typeof(Month))
                {
                    value = Month.FromDateTime(((Month)value).ToDateTime().Add(-(TimeSpan)toAdd));
                    toAdd = -Convert.ToInt64(((TimeSpan)toAdd).TotalMilliseconds);
                }
                else if (TargetType == typeof(Week))
                {
                    value = Week.FromDateTime(((Week)value).StartDate().Add(-(TimeSpan)toAdd));
                    toAdd = -Convert.ToInt64(((TimeSpan)toAdd).TotalMilliseconds);
                }
            }
            else
            {
                if (TargetType == typeof(Int32))
                {
                    value = Convert.ToInt32(value) + Convert.ToInt32(toAdd);
                }
                else if (TargetType == typeof(Int16))
                {
                    value = Convert.ToInt16(value) + Convert.ToInt16(toAdd);
                }
                else if (TargetType == typeof(Int64))
                {
                    value = Convert.ToInt64(value) + Convert.ToInt64(toAdd);
                }
                else if (TargetType == typeof(UInt32))
                {
                    value = Convert.ToUInt32(value) + Convert.ToUInt32(toAdd);
                }
                else if (TargetType == typeof(UInt16))
                {
                    value = Convert.ToUInt16(value) + Convert.ToUInt16(toAdd);
                }
                else if (TargetType == typeof(UInt64))
                {
                    value = Convert.ToUInt64(value) + Convert.ToUInt64(toAdd);
                }
                else if (TargetType == typeof(byte))
                {
                    value = Convert.ToByte(value) + Convert.ToByte(toAdd);
                }
                else if (TargetType == typeof(sbyte))
                {
                    value = Convert.ToSByte(value) + Convert.ToSByte(toAdd);
                }
                else if (TargetType == typeof(decimal))
                {
                    value = Convert.ToDecimal(value) + Convert.ToDecimal(toAdd);
                }
                else if (TargetType == typeof(float))
                {
                    value = Convert.ToSingle(value) + Convert.ToSingle(toAdd);
                }
                else if (TargetType == typeof(double))
                {
                    value = Convert.ToDouble(value) + Convert.ToDouble(toAdd);
                }
                else if (TargetType == typeof(TimeSpan))
                {
                    value = (TimeSpan)value + (TimeSpan)toAdd;
                    toAdd = Convert.ToInt64(((TimeSpan)toAdd).TotalMilliseconds);
                }
                else if (TargetType == typeof(DateTime))
                {
                    value = Convert.ToDateTime(value).Add((TimeSpan)toAdd);
                    toAdd = Convert.ToInt64(((TimeSpan)toAdd).TotalMilliseconds);
                }
                else if (TargetType == typeof(DateTimeOffset))
                {
                    value = ((DateTimeOffset)value).Add((TimeSpan)toAdd);
                    toAdd = Convert.ToInt64(((TimeSpan)toAdd).TotalMilliseconds);
                }
                else if (TargetType == typeof(Month))
                {
                    value = Month.FromDateTime(((Month)value).ToDateTime().Add((TimeSpan)toAdd));
                    toAdd = Convert.ToInt64(((TimeSpan)toAdd).TotalMilliseconds);
                }
                else if (TargetType == typeof(Week))
                {
                    value = Week.FromDateTime(((Week)value).StartDate().Add((TimeSpan)toAdd));
                    toAdd = Convert.ToInt64(((TimeSpan)toAdd).TotalMilliseconds);
                }
            }
            return(toAdd);
        }
        internal override Expression BuildExpression(ParameterExpression par, Type t)
        {
            QueryOptions operation;
            var          builder = getBuilder(out operation);

            if (builder == null)
            {
                throw new OperationNotAllowedException(Property, Operator);
            }
            var propertyAccess = BuildAccess(Property, par, t, operation, Operator);
            var value          = Value;
            var pType          = Nullable.GetUnderlyingType(propertyAccess.Type) ?? propertyAccess.Type;
            var oType          = propertyAccess.Type;

            if (Value != null && pType != Value.GetType())
            {
                var type = Value.GetType();



                if (pType == typeof(TimeSpan) && type == typeof(DateTime))
                {
                    var dt = (DateTime)Value;
                    value = dt.Subtract(dt.Date);
                }
                else if (pType == typeof(Month) && type == typeof(DateTime))
                {
                    value = Month.FromDateTime((DateTime)value);
                }
                else if (pType == typeof(Week) && type == typeof(DateTime))
                {
                    value = Week.FromDateTime((DateTime)value);
                }
                else if (type == typeof(DateTimeOffset) && pType == typeof(DateTime))
                {
                    var cvalue = ((DateTimeOffset)value).UtcDateTime;
                    value = new DateTime(cvalue.Year, cvalue.Month, cvalue.Day,
                                         cvalue.Hour, cvalue.Minute, cvalue.Second, cvalue.Millisecond, DateTimeKind.Unspecified);;
                }
                else if (type == typeof(DateTime) && pType == typeof(DateTimeOffset))
                {
                    var cvalue = ((DateTime)value).ToUniversalTime();
                    value = new DateTimeOffset(cvalue.Year, cvalue.Month, cvalue.Day,
                                               cvalue.Hour, cvalue.Minute, cvalue.Second, new TimeSpan(0));
                }
                else
                {
                    value = Convert.ChangeType(Value, propertyAccess.Type);
                }
            }



            if (Inv)
            {
                return(builder(Expression.Constant(value, oType), propertyAccess));
            }
            else
            {
                return(builder(propertyAccess, Expression.Constant(value, oType)));
            }
        }