Beispiel #1
0
        public PocoData(Type t)
        {
            type = t;

            // Get the mapper for this type
            var mapper = Mappers.GetMapper(t);

            // Get the table info
            TableInfo = mapper.GetTableInfo(t);

            // Work out bound properties
            Columns = new Dictionary <string, PocoColumn>(StringComparer.OrdinalIgnoreCase);
            foreach (var pi in t.GetProperties())
            {
                ColumnInfo ci = mapper.GetColumnInfo(pi);
                if (ci == null)
                {
                    continue;
                }

                var pc = new PocoColumn();
                pc.PropertyInfo = pi;
                pc.ColumnName   = ci.ColumnName;
                pc.ResultColumn = ci.ResultColumn;
                pc.ForceToUtc   = ci.ForceToUtc;

                // Store it
                Columns.Add(pc.ColumnName, pc);
            }

            // Build column list for automatic select
            QueryColumns = (from c in Columns where !c.Value.ResultColumn select c.Key).ToArray();
        }
Beispiel #2
0
        private static Func <object, object> GetConverter(IMapper mapper, PocoColumn pc, Type srcType, Type dstType)
        {
            Func <object, object> converter = null;

            // Get converter from the mapper
            if (pc != null)
            {
                converter = mapper.GetFromDbConverter(pc.PropertyInfo, srcType);
                if (converter != null)
                {
                    return(converter);
                }
            }

            // Standard DateTime->Utc mapper
            if (pc != null && pc.ForceToUtc && srcType == typeof(DateTime) && (dstType == typeof(DateTime) || dstType == typeof(DateTime?)))
            {
                return(delegate(object src) { return new DateTime(((DateTime)src).Ticks, DateTimeKind.Utc); });
            }

            // Forced type conversion including integral types -> enum
            if (dstType.IsEnum && IsIntegralType(srcType))
            {
                if (srcType != typeof(int))
                {
                    return(delegate(object src) { return Convert.ChangeType(src, typeof(int), null); });
                }
            }
            else if (!dstType.IsAssignableFrom(srcType))
            {
                if (dstType.IsEnum && srcType == typeof(string))
                {
                    return(delegate(object src) { return EnumMapper.EnumFromString(dstType, (string)src); });
                }
                else
                {
                    return(delegate(object src) { return Convert.ChangeType(src, dstType, null); });
                }
            }

            return(null);
        }
Beispiel #3
0
        private static Func <object, object> GetConverter(IMapper mapper, PocoColumn pc, Type srcType, Type dstType)
        {
            Func <object, object> converter = null;

            // Get converter from the mapper
            if (pc != null)
            {
                converter = mapper.GetFromDbConverter(pc.PropertyInfo, srcType);
                if (converter != null)
                {
                    return(converter);
                }
            }

            // Standard DateTime->Utc mapper
            if (pc != null && pc.ForceToUtc && srcType == typeof(DateTime) && (dstType == typeof(DateTime) || dstType == typeof(DateTime?)))
            {
                return(delegate(object src) { return new DateTime(((DateTime)src).Ticks, DateTimeKind.Utc); });
            }

            if (srcType == typeof(TimeSpan) && (dstType == typeof(DateTime) || dstType == typeof(DateTime?)))
            {
                return(delegate(object src) { return new DateTime(((TimeSpan)src).Ticks); });
            }

            if (srcType == typeof(UInt64) && (dstType == typeof(bool) || dstType == typeof(bool?)))
            {
                //if (null != Nullable.GetUnderlyingType(dstType))
                //{
                //    var convert = new NullableConverter(dstType);
                //    return delegate (object src) { return convert.ConvertFrom(src + ""); };
                //}
                return(delegate(object src) { return src.ToBool_(); });
            }

            // Forced type conversion including integral types -> enum
            if ((dstType.IsEnum || (null != Nullable.GetUnderlyingType(dstType) && Nullable.GetUnderlyingType(dstType).IsEnum)) && IsIntegralType(srcType))
            {
                //if (srcType != typeof(int))
                //{
                //    return delegate (object src) { return Convert.ChangeType(src, typeof(int), null); };
                //}
                if (null != Nullable.GetUnderlyingType(dstType))
                {
                    var convert = new NullableConverter(dstType);
                    return(delegate(object src) { return convert.ConvertFrom(src + ""); });
                }
                else
                {
                    return(delegate(object src) { return Convert.ChangeType(src, typeof(int), null); });
                }
            }
            else if (!dstType.IsAssignableFrom(srcType))
            {
                if (dstType.IsEnum && srcType == typeof(string))
                {
                    return(delegate(object src) { return EnumMapper.EnumFromString(dstType, src + ""); });
                }
                else if (null != Nullable.GetUnderlyingType(dstType))
                {
                    var convert = new NullableConverter(dstType);
                    return(delegate(object src) { return convert.ConvertFrom(src + ""); });
                }
                else
                {
                    return(delegate(object src) { return Convert.ChangeType(src, dstType, null); });
                }
            }

            return(null);
        }