Example #1
0
        public static void Map(object[] parents, object[] childs)
        {
            if (IsEmpty(parents) || IsEmpty(childs))
            {
                return;
            }

            DbRecordInfo         childRecordInfo   = DbAttributesManager.GetRecordInfo(childs[0].GetType());
            Type                 parentType        = parents[0].GetType();
            DbIdentityRecordInfo primaryRecordInfo = DbAttributesManager.GetRecordInfo(parentType) as DbIdentityRecordInfo;

            if (primaryRecordInfo == null)
            {
                throw new NdbNotIdentityException("Only DbIdentityRecord objects can have childs");
            }

            if (!primaryRecordInfo.Childs.ContainsKey(childRecordInfo.RecordType))
            {
                throw new NdbRelationException("{0} doesn't contains Childs of {1} type",
                                               primaryRecordInfo.RecordType, childRecordInfo.RecordType);
            }

            MemberInfo childReferenceField = primaryRecordInfo.Childs[childRecordInfo.RecordType];
            Type       childType           = DbFieldInfo.GetType(childReferenceField).GetElementType();

            var childsList = new List <object>(childs);

            foreach (object parent in parents)
            {
                ArrayList list    = new ArrayList();
                object    pkvalue = primaryRecordInfo.PrimaryKey.GetValue(parent);
                for (int i = childsList.Count - 1; i >= 0; i--)
                {
                    object childRecord     = childsList[i];
                    object foreignKeyValue = childRecordInfo.ForeignKeys[parentType].GetValue(childRecord);

                    if (pkvalue.Equals(foreignKeyValue))
                    {
                        Type type = parent.GetType();
                        if (childRecordInfo.Parents.ContainsKey(type))
                        {
                            DbFieldInfo.SetValue(childRecordInfo.Parents[type], childRecord, parent);
                        }

                        list.Add(childRecord);
                        childsList.RemoveAt(i);
                    }
                }

                DbFieldInfo.SetValue(childReferenceField, parent, list.ToArray(childType));
            }
        }
Example #2
0
        private void Load(Type type, MemberInfo[] fields)
        {
            for (int i = 0; i < fields.Length; i++)
            {
                MemberInfo field      = fields[i];
                Type       memberType = DbFieldInfo.GetType(field);

                object[] parentRecordsAttributes = field.GetCustomAttributes(typeof(DbParentRecordAttribute), true);
                if (parentRecordsAttributes != null && parentRecordsAttributes.Length > 0)
                {
                    parents.Add(memberType, field);
                }

                object[] childRecordsAttributes = field.GetCustomAttributes(typeof(DbChildRecordsAttribute), true);
                if (childRecordsAttributes != null && childRecordsAttributes.Length > 0)
                {
                    if (memberType.BaseType != typeof(Array))
                    {
                        throw new NdbException("DbChildRecordsAttribute can belong to Array fields ONLY");
                    }

                    childs.Add(memberType.GetElementType(), field);
                }

                object[] attributes = field.GetCustomAttributes(typeof(DbFieldAttribute), true);
                if (attributes != null && attributes.Length > 0)
                {
                    var    foreignTypes = new List <Type>(attributes.Length);
                    bool   isPrimary    = false;
                    object defaultValue = null;
                    string Name         = null;
                    uint   Size         = 0;
                    Type   DbType       = null;
                    foreach (DbFieldAttribute attribute in attributes)
                    {
                        if (attribute.DefaultValue != null)
                        {
                            defaultValue = attribute.DefaultValue;
                        }

                        if (attribute.IsDiffersFromDatabaseType)
                        {
                            DbType = attribute.DbType;
                        }

                        if (attribute.Size > 0)
                        {
                            Size = attribute.Size;
                        }

                        if (string.IsNullOrEmpty(Name))
                        {
                            Name = attribute.ColumnName;
                        }

                        var primaryKeyFieldAttribute = attribute as DbPrimaryKeyFieldAttribute;
                        if (primaryKeyFieldAttribute != null)
                        {
                            IsDbGeneratedPrimaryKey = primaryKeyFieldAttribute.IsDbGenerated;
                            isPrimary = true;
                        }
                        else
                        {
                            var dbForeignKeyFieldAttribute = attribute as DbForeignKeyFieldAttribute;
                            if (dbForeignKeyFieldAttribute != null)
                            {
                                foreignTypes.Add(dbForeignKeyFieldAttribute.ForeignKeyType);
                            }
                            //                                    foreignKeys.Add(dbForeignKeyFieldAttribute.Type, dbFieldInfo);
                        }
                    }

                    DbFieldInfo dbFieldInfo = new DbFieldInfo(field, Name, Size, DbType, defaultValue);

                    if (isPrimary)
                    {
                        primaryKey = dbFieldInfo;
                    }
                    else
                    {
                        dbFields.Add(dbFieldInfo);
                    }

                    foreach (Type foreignType in foreignTypes)
                    {
                        foreignKeys.Add(foreignType, dbFieldInfo);
                    }
                }
            }

            RecordInfo = (primaryKey != null)
                             ? new DbIdentityRecordInfo(primaryKey, IsDbGeneratedPrimaryKey)
                             : new DbRecordInfo();

            RecordInfo.RecordType  = type;
            RecordInfo.ForeignKeys = foreignKeys;
            RecordInfo.TableName   = LoadTableName(type);
            RecordInfo.Fields      = dbFields.ToArray();
            RecordInfo.Childs      = childs;
            RecordInfo.Parents     = parents;
        }