Esempio n. 1
0
        public PocoData(Type t, IMapper mapper, Cache <string, Type> aliasToTypeCache) : this()
        {
            aliasToType = aliasToTypeCache;
            type        = t;
            Mapper      = mapper;
            TableInfo   = TableInfo.FromPoco(t);

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

            var alias = CreateAlias(type.Name, type);

            TableInfo.AutoAlias = alias;
            var index = 0;

            // Work out bound properties
            Columns = new Dictionary <string, PocoColumn>(StringComparer.OrdinalIgnoreCase);
            foreach (var mi in ReflectionUtils.GetFieldsAndPropertiesForClasses(t))
            {
                ColumnInfo ci = ColumnInfo.FromMemberInfo(mi);
                if (ci.IgnoreColumn)
                {
                    continue;
                }

                var pc = new PocoColumn();
                pc.TableInfo         = TableInfo;
                pc.MemberInfo        = mi;
                pc.ColumnName        = ci.ColumnName;
                pc.ResultColumn      = ci.ResultColumn;
                pc.ForceToUtc        = ci.ForceToUtc;
                pc.ComputedColumn    = ci.ComputedColumn;
                pc.ColumnType        = ci.ColumnType;
                pc.ColumnAlias       = ci.ColumnAlias;
                pc.VersionColumn     = ci.VersionColumn;
                pc.VersionColumnType = ci.VersionColumnType;

                if (Mapper != null && !Mapper.MapMemberToColumn(mi, ref pc.ColumnName, ref pc.ResultColumn))
                {
                    continue;
                }

                pc.AutoAlias = alias + "_" + index++;

                // Store it
                if (!Columns.ContainsKey(pc.ColumnName))
                {
                    Columns.Add(pc.ColumnName, pc);
                }
            }

            // Build column list for automatic select
            QueryColumns = Columns.Where(c => !c.Value.ResultColumn).ToArray();
        }
Esempio n. 2
0
 private static void PushMemberOntoStack(ILGenerator il, PocoColumn pc)
 {
     if (pc.MemberInfo.IsField())
     {
         il.Emit(OpCodes.Stfld, (FieldInfo)pc.MemberInfo);
     }
     else
     {
         il.Emit(OpCodes.Callvirt, ((PropertyInfo)pc.MemberInfo).GetSetMethodOnDeclaringType());
     }
 }
Esempio n. 3
0
        public static Func <object, object> GetConverter(IMapper mapper, PocoColumn pc, Type srcType, Type dstType)
        {
            Func <object, object> converter = null;

            // Get converter from the mapper
            if (mapper != null)
            {
                converter = pc != null?mapper.GetFromDbConverter(pc.MemberInfo, srcType) : mapper.GetFromDbConverter(dstType, srcType);

                if (converter != null)
                {
                    return(converter);
                }
            }

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

            // Forced type conversion including integral types -> enum
            var underlyingType = _underlyingTypes.Get(dstType, () => Nullable.GetUnderlyingType(dstType));

            if (dstType.IsEnum || (underlyingType != null && underlyingType.IsEnum))
            {
                if (srcType == typeof(string))
                {
                    converter = src => EnumMapper.EnumFromString((underlyingType ?? dstType), (string)src);
                    return(converter);
                }

                if (IsIntegralType(srcType))
                {
                    converter = src => Enum.ToObject((underlyingType ?? dstType), src);
                    return(converter);
                }
            }
            else if (!dstType.IsAssignableFrom(srcType))
            {
                converter = src => Convert.ChangeType(src, (underlyingType ?? dstType), null);
            }
            return(converter);
        }
Esempio n. 4
0
        public static bool TryGetColumnByName(Dictionary <string, PocoColumn> columns, string name, out PocoColumn pc)
        {
            // Try to get the column by name directly (works when the poco property name matches the DB column name).
            var found = (columns.TryGetValue(name, out pc) || columns.TryGetValue(name.Replace("_", ""), out pc));

            if (!found)
            {
                // Try to get the column by the poco member name (the poco property name is different from the DB column name).
                pc    = columns.Values.Where(c => c.MemberInfo.Name == name).FirstOrDefault();
                found = (pc != null);
            }
            return(found);
        }