Beispiel #1
0
        private static Func <object, object> GetConverter(bool forceDateTimesToUtc, PocoColumn pc, Type srcType, Type dstType)
        {
            Func <object, object> converter = null;

            // Get converter from the mapper
            if (Database.Mapper != null)
            {
                if (pc != null)
                {
                    converter = Database.Mapper.GetFromDbConverter(pc.PropertyInfo, srcType);
                }
                else
                {
                    var m2 = Database.Mapper as IMapper2;
                    if (m2 != null)
                    {
                        converter = m2.GetFromDbConverter(dstType, srcType);
                    }
                }
            }

            // Standard DateTime->Utc mapper
            if (forceDateTimesToUtc && converter == null && srcType == typeof(DateTime) && (dstType == typeof(DateTime) || dstType == typeof(DateTime?)))
            {
                converter = delegate(object src) { return(new DateTime(((DateTime)src).Ticks, DateTimeKind.Utc)); };
            }
            if ((srcType == typeof(Guid?) || srcType == typeof(Guid)) && dstType == typeof(string))
            {
                converter = delegate(object src)
                {
                    if (src == null)
                    {
                        return(null);
                    }
                    return(src.ToString());
                };
            }

            // Forced type conversion including integral types -> enum
            if (converter == null)
            {
                if (dstType.IsEnum && IsIntegralType(srcType))
                {
                    if (srcType != typeof(int))
                    {
                        converter = delegate(object src) { return(Convert.ChangeType(src, typeof(int), null)); };
                    }
                }
                else if (!dstType.IsAssignableFrom(srcType))
                {
                    converter = delegate(object src) { return(Convert.ChangeType(src, dstType, null)); };
                }
            }
            return(converter);
        }
Beispiel #2
0
        public PocoData(Type t)
        {
            type      = t;
            TableInfo = new TableInfo();

            // Get the table name
            var a = t.GetCustomAttributes(typeof(TableAttribute), true);

            TableInfo.TableName = a.Length == 0 ? t.Name : (a[0] as TableAttribute).Name;


            // Call column mapper
            if (Database.Mapper != null)
            {
                Database.Mapper.GetTableInfo(t, TableInfo);
            }

            // Work out bound properties
            bool ExplicitColumns = t.GetCustomAttributes(typeof(ExplicitColumnsAttribute), true).Length > 0;

            Columns       = new Dictionary <string, PocoColumn>(StringComparer.OrdinalIgnoreCase);
            IgnoreColumns = new Dictionary <string, PocoColumn>(StringComparer.OrdinalIgnoreCase);
            foreach (var pi in t.GetProperties())
            {
                a = pi.GetCustomAttributes(typeof(IDAttribute), true);
                if (a.Length > 0)
                {
                    IDAttribute idAttri = a[0] as IDAttribute;
                    TableInfo.PrimaryKey    = idAttri.Name ?? pi.Name;
                    TableInfo.SequenceName  = idAttri.Name ?? pi.Name;
                    TableInfo.AutoIncrement = idAttri.AutoIncrement;
                }


                // Work out if properties is to be included
                var ColAttrs = pi.GetCustomAttributes(typeof(ColumnAttribute), true);

                if (ExplicitColumns)
                {
                    if (ColAttrs.Length == 0)
                    {
                        continue;
                    }
                }
                else
                {
                }

                var pc = new PocoColumn();
                pc.PropertyInfo = pi;

                // Work out the DB column name
                if (ColAttrs.Length > 0)
                {
                    var colattr = (ColumnAttribute)ColAttrs[0];
                    pc.ColumnName = colattr.Name;
                    if ((colattr as ResultColumnAttribute) != null)
                    {
                        pc.ResultColumn = true;
                    }
                }
                if (pc.ColumnName == null)
                {
                    pc.ColumnName = pi.Name;
                    if (Database.Mapper != null && !Database.Mapper.MapPropertyToColumn(pi, ref pc.ColumnName, ref pc.ResultColumn))
                    {
                        continue;
                    }
                }
                if (pi.GetCustomAttributes(typeof(IgnoreAttribute), true).Length != 0)
                {
                    IgnoreColumns[pc.ColumnName] = pc;
                }
                // Store it
                Columns.Add(pc.ColumnName, pc);
            }
            TableInfo.PrimaryKey   = TableInfo.PrimaryKey ?? "ID";
            TableInfo.SequenceName = TableInfo.SequenceName ?? "ID";
            // Build column list for automatic select
            QueryColumns = (from c in Columns where !c.Value.ResultColumn select c.Key).ToArray();
        }