Exemple #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();
        }
Exemple #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());
     }
 }
Exemple #3
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();
        }
Exemple #4
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);
        }
Exemple #5
0
        public static Func <object, object> GetConverter(MapperCollection mapper, PocoColumn pc, Type srcType, Type dstType)
        {
            Func <object, object> converter = null;

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

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

            if (pc != null &&
                pc.SerializedColumn)
            {
                converter = src => DatabaseFactory.ColumnSerializer.Deserialize((string)src, dstType);
                return(converter);
            }

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

            //Todo:The following changes were done to NPoco

            #region "Changes"

            // DateTime->DateTimeOffset
            if (pc != null &&
                srcType == typeof(DateTime) &&
                (dstType == typeof(DateTimeOffset) || dstType == typeof(DateTimeOffset?)))
            {
                converter = src => new DateTimeOffset((DateTime)src);
                return(converter);
            }

            // DateTimeOffset --> DateTime
            if (pc != null &&
                srcType == typeof(DateTimeOffset) &&
                (dstType == typeof(DateTime) || dstType == typeof(DateTime?)))
            {
                converter = src => (DateTime)src;
                return(converter);
            }

            #endregion

            // Forced type conversion including integral types -> enum
            var underlyingType = UnderlyingTypes.Get(dstType, () => Nullable.GetUnderlyingType(dstType));
            if (dstType.GetTypeInfo().IsEnum ||
                (underlyingType != null && underlyingType.GetTypeInfo().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);
        }
Exemple #6
0
 public virtual object ProcessDefaultMappings(PocoColumn pocoColumn, object value)
 {
     return(value);
 }
Exemple #7
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);
        }
        private IEnumerable <PocoMemberPlan> GetPocoMembers(MapperCollection mapper, ColumnInfo[] columnInfos, List <MemberInfo> memberInfos, string prefix = null)
        {
            MemberInfo[] capturedMembers = memberInfos.ToArray();
            string       capturedPrefix  = prefix;

            foreach (ColumnInfo columnInfo in columnInfos)
            {
                if (columnInfo.IgnoreColumn)
                {
                    continue;
                }

                Type memberInfoType = columnInfo.MemberInfo.GetMemberInfoType();
                if (columnInfo.ReferenceType == ReferenceType.Many)
                {
                    memberInfoType = memberInfoType.GetGenericArguments().First();
                }

                PocoMemberPlan[]  childrenPlans      = new PocoMemberPlan[0];
                TableInfoPlan     childTableInfoPlan = null;
                List <MemberInfo> members            = new List <MemberInfo>(capturedMembers)
                {
                    columnInfo.MemberInfo
                };

                if (columnInfo.ComplexMapping || columnInfo.ReferenceType != ReferenceType.None)
                {
                    if (capturedMembers.GroupBy(x => x.GetMemberInfoType()).Any(x => x.Count() >= 2))
                    {
                        continue;
                    }

                    ColumnInfo[] childColumnInfos = this.GetColumnInfos(memberInfoType);

                    if (columnInfo.ReferenceType != ReferenceType.None)
                    {
                        childTableInfoPlan = this.GetTableInfo(memberInfoType, childColumnInfos, members);
                    }

                    string newPrefix = JoinStrings(capturedPrefix, columnInfo.ReferenceType != ReferenceType.None ? "" : (columnInfo.ComplexPrefix ?? columnInfo.MemberInfo.Name));

                    childrenPlans = this.GetPocoMembers(mapper, childColumnInfos, members, newPrefix).ToArray();
                }

                MemberInfo capturedMemberInfo = columnInfo.MemberInfo;
                ColumnInfo capturedColumnInfo = columnInfo;

                List <MemberAccessor> accessors = this.GetMemberAccessors(members);
                Type           memberType       = capturedMemberInfo.GetMemberInfoType();
                bool           isList           = IsList(capturedMemberInfo);
                Type           listType         = GetListType(memberType, isList);
                bool           isDynamic        = capturedMemberInfo.IsDynamic();
                FastCreate     fastCreate       = GetFastCreate(memberType, mapper, isList, isDynamic);
                string         columnName       = this.GetColumnName(capturedPrefix, capturedColumnInfo.ColumnName ?? capturedMemberInfo.Name);
                MemberInfoData memberInfoData   = new MemberInfoData(capturedMemberInfo);

                yield return(tableInfo =>
                {
                    PocoColumn pc = new PocoColumn
                    {
                        ReferenceType = capturedColumnInfo.ReferenceType,
                        TableInfo = tableInfo,
                        MemberInfoData = memberInfoData,
                        MemberInfoChain = members,
                        ColumnName = columnName,
                        ResultColumn = capturedColumnInfo.ResultColumn,
                        ForceToUtc = capturedColumnInfo.ForceToUtc,
                        ComputedColumn = capturedColumnInfo.ComputedColumn,
                        ComputedColumnType = capturedColumnInfo.ComputedColumnType,
                        ColumnType = capturedColumnInfo.ColumnType,
                        ColumnAlias = capturedColumnInfo.ColumnAlias,
                        VersionColumn = capturedColumnInfo.VersionColumn,
                        VersionColumnType = capturedColumnInfo.VersionColumnType,
                        SerializedColumn = capturedColumnInfo.SerializedColumn
                    };

                    pc.SetMemberAccessors(accessors);

                    TableInfo childrenTableInfo = childTableInfoPlan == null ? tableInfo : childTableInfoPlan();
                    List <PocoMember> children = childrenPlans.Select(plan => plan(childrenTableInfo)).ToList();

                    // Cascade ResultColumn down
                    foreach (PocoMember child in children.Where(child => child.PocoColumn != null && pc.ResultColumn))
                    {
                        child.PocoColumn.ResultColumn = true;
                    }

                    PocoMember pocoMember = new PocoMember()
                    {
                        MemberInfoData = memberInfoData,
                        MemberInfoChain = members,
                        IsList = isList,
                        IsDynamic = isDynamic,
                        PocoColumn = capturedColumnInfo.ComplexMapping ? null : pc,
                        ReferenceType = capturedColumnInfo.ReferenceType,
                        ReferenceMemberName = capturedColumnInfo.ReferenceMemberName,
                        PocoMemberChildren = children,
                    };

                    pocoMember.SetMemberAccessor(accessors[accessors.Count - 1], fastCreate, listType);

                    return pocoMember;
                });
            }
        }