コード例 #1
0
        public bool IsAllFieldValid(Type type)
        {
            DbRecordInfo info = DbAttributesManager.GetRecordInfo(type);
            Dictionary <string, string> fields = accessor.LoadFields(info.TableName);

            if (fields.Count == 0)
            {
                throw new NdbException("Unable to load fields for the following table: " + info.TableName);
            }

            DbFieldCheckResult checker = new DbFieldCheckResult(accessor);

            foreach (DbFieldInfo fi in info.Fields)
            {
                checker.Process(fields, fi);

                if (checker.IsNew)
                {
                    LastError = string.Format("{0} in {1} ({2}) isn't present in db"
                                              , fi.Name, type, fi.FieldType);
                    return(false);
                }

                if (checker.IsDifferent)
                {
                    LastError = string.Format("{0} in {1} is {2} but column is {3}"
                                              , fi.Name, type, fi.FieldType, checker.CurrentSqlType);
                    return(false);
                }
            }

            return(true);
        }
コード例 #2
0
        private static DbRecords loadRecords(Type type, IDataReader reader)
        {
            DbRecordInfo info    = DbAttributesManager.GetRecordInfo(type);
            Array        records = DbGateway.LoadRecords(type, reader, info);

            return(new DbRecords(info, records));
        }
コード例 #3
0
        public void Build(Type type)
        {
            DbRecordInfo info = DbAttributesManager.GetRecordInfo(type);

            TableName = info.TableName;

            Dictionary <string, string> fields = accessor.LoadFields(TableName);

            FieldsToCreate = new Dictionary <string, string>();
            FieldsToUpdate = new Dictionary <string, string>();

            DbFieldCheckResult checker = new DbFieldCheckResult(accessor);

            foreach (DbFieldInfo fi in info.Fields)
            {
                checker.Process(fields, fi);

                if (checker.IsNew)
                {
                    FieldsToCreate.Add(fi.Name, checker.SqlType);
                }
                else
                if (checker.IsDifferent)
                {
                    FieldsToUpdate.Add(fi.Name, checker.SqlType);
                }
            }
        }
コード例 #4
0
ファイル: DbRecordsMapper.cs プロジェクト: mayvazyan/ndb
        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));
            }
        }