Exemplo n.º 1
0
        public void Process(Dictionary <string, string> fields, DbFieldInfo fi)
        {
            SqlType = accessor.GetSqlType(fi);

            string columnName = (accessor.IsPostgre) ? fi.Name.ToLower() : fi.Name;

            if (!fields.ContainsKey(columnName))
            {
                IsNew = true;
            }
            else
            {
                CurrentSqlType = fields[columnName];

                if (CurrentSqlType != SqlType)
                {
                    string type1 = GetSqlType(CurrentSqlType);
                    string type2 = GetSqlType(SqlType);
                    if (type1 == type2 ||
                        (IsStringType(type1) && IsStringType(type1)) ||
                        (IsDateTimeType(type1) && IsDateTimeType(type1))
                        )
                    {
                        return;
                    }

                    IsDifferent = true;
                }
            }
        }
Exemplo n.º 2
0
        /// <summary>
        /// Determines whether the specified <see cref="T:System.Object"/> is equal to the current <see cref="T:System.Object"/>.
        /// </summary>
        /// <param name="obj">The <see cref="T:System.Object"/> to compare with the current <see cref="T:System.Object"/>.</param>
        /// <returns>
        /// true if the specified <see cref="T:System.Object"/> is equal to the current <see cref="T:System.Object"/>; otherwise, false.
        /// </returns>
        /// <exception cref="T:System.NullReferenceException">
        /// The <paramref name="obj"/> parameter is null.
        /// </exception>
        public override bool Equals(object obj)
        {
            if (obj == null)
            {
                return(false);
            }

            DbFieldInfo info = obj as DbFieldInfo;

            return(info != null && FieldType == info.FieldType && Name == info.Name && Size == info.Size);
        }
Exemplo n.º 3
0
        public DbLinqAttributesLoader(Type type, TableAttribute tableAttribute)
        {
            bool IsDbGeneratedPrimaryKey = false;
            DbFieldInfo primaryKey = null;
            bool primaryKeyFound = false;
            MemberInfo[] fields = type.GetMembers(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic);

            foreach (MemberInfo field in fields)
            {
                object[] attributes = field.GetCustomAttributes(typeof (ColumnAttribute), true);
                if (attributes.Length > 0)
                {
                    ColumnAttribute attribute = (ColumnAttribute) attributes[0];

                    DbFieldInfo dbFieldInfo = new DbFieldInfo(field, attribute.Name, 0, null, null);

                    if (attribute.IsPrimaryKey)
                    {
                        if (primaryKeyFound) //found second primary key mean it's composite
                        {
                            if (primaryKey != null)
                            {
                                dbFields.Add(primaryKey);
                                primaryKey = null;
                            }
                            dbFields.Add(dbFieldInfo);
                        }
                        else
                        {
                            IsDbGeneratedPrimaryKey = attribute.IsDbGenerated;
                            primaryKey = dbFieldInfo;
                        }
                        primaryKeyFound = true;
                    }
                    else
                    {
                        dbFields.Add(dbFieldInfo);
                    }
                }
            }

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

            RecordInfo.RecordType = type;
            RecordInfo.ForeignKeys = new Dictionary<Type, DbFieldInfo>(0);
            RecordInfo.TableName = string.IsNullOrEmpty(tableAttribute.Name) ? type.Name :  tableAttribute.Name;
            RecordInfo.Fields = dbFields.ToArray();
            RecordInfo.Childs = new Dictionary<Type, MemberInfo>(0);
            RecordInfo.Parents = new Dictionary<Type, MemberInfo>(0);
        }
Exemplo n.º 4
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));
            }
        }
Exemplo n.º 5
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;
        }