Example #1
0
        /// <summary>
        /// Creates a new OidColumn and adds it to the OidColumns list.
        /// </summary>
        /// <returns>The created OidColumn object.</returns>
        public OidColumn NewOidColumn()
        {
            OidColumn column = new OidColumn(this);

            this.oidColumns.Add(column);
            return(column);
        }
Example #2
0
 internal ClassOid(XmlNode oidNode, Class parent)
     : base(oidNode, parent)
 {
     if (oidNode.Attributes["ColumnName"] == null)  // new mapping
     {
         XmlNodeList nl = oidNode.SelectNodes(parent.Parent.selectOidColumns);
         foreach (XmlNode columnNode in nl)
         {
             this.oidColumns.Add(new OidColumn(columnNode, this));
         }
     }
     else
     {
         OidColumn oidColumn = new OidColumn(this);
         this.oidColumns.Add(oidColumn);
         oidColumn.Name = oidNode.Attributes["ColumnName"].Value;
         if (null != oidNode.Attributes["FieldName"])
         {
             oidColumn.FieldName = oidNode.Attributes["FieldName"].Value;
         }
         if (null != oidNode.Attributes["ChildRelation"])
         {
             oidColumn.RelationName = oidNode.Attributes["ChildRelation"].Value;
         }
         if (null != oidNode.Attributes["ParentRelation"])
         {
             OidColumn secondOid = new OidColumn(this);
             secondOid.RelationName = oidNode.Attributes["ParentRelation"].Value;
             secondOid.Name         = secondOid.RelationName;
             this.oidColumns.Add(secondOid);
         }
     }
 }
Example #3
0
 private bool OidsAreDifferent(ClassOid oid1, ClassOid oid2)
 {
     if (oid1.OidColumns.Count != oid2.OidColumns.Count)
     {
         return(true);
     }
     for (int i = 0; i < oid1.OidColumns.Count; i++)
     {
         OidColumn oidc1 = (OidColumn)oid1.OidColumns[i];
         OidColumn oidc2 = (OidColumn)oid2.OidColumns[i];
         if (OidColumnsAreDifferent(oidc1, oidc2))
         {
             return(true);
         }
     }
     return(false);
 }
Example #4
0
        /// <summary>
        /// Checks, if the OidColumns collection matches the columnAttributes for a given class.
        /// If the OidColums collection doesn't match, it will be completely rebuilt.
        /// </summary>
        /// <param name="columnAttributes">An array of OidColumnAttribute objects or null, if no attribute was set. If columnAttributes is null, the mappings are left as they are except there is no OidColumn defined. If no OidColumn is defined, a standard OidColumn will be generated. The standard OidColumn is an autoincremented integer column with the name "ID".</param>
        public void RemapOidColumns(OidColumnAttribute[] columnAttributes)
        {
            if (columnAttributes == null)  // No attrs defined
            {
                return;
            }

            // If there are no differences between attributes and mappings,
            // return
            bool remap = false;

            if (columnAttributes.Length == this.oidColumns.Count)
            {
                for (int i = 0; i < columnAttributes.Length; i++)
                {
                    OidColumn oidColumn = (OidColumn)this.oidColumns[i];
                    columnAttributes[i].RemapColumn(oidColumn);
                }
            }
            else
            {
                remap = true;
            }

            if (!remap)
            {
                return;
            }

            // We assume, that we can use the same name, if a remapping of a single column is necessary.
            if (columnAttributes.Length == 1 && oidColumns.Count == 1 && string.IsNullOrEmpty(columnAttributes[0].Name))
            {
                columnAttributes[0].Name = ((OidColumn)oidColumns[0]).Name;
            }

            this.oidColumns.Clear();
            for (int i = 0; i < columnAttributes.Length; i++)
            {
                this.oidColumns.Add(columnAttributes[i].CreateOidColumn(this));
            }
        }
Example #5
0
 private bool OidColumnsAreDifferent(OidColumn c1, OidColumn c2)
 {
     if (c1.FieldName != c2.FieldName)
     {
         return(true);
     }
     if (c1.RelationName != c2.RelationName)
     {
         return(true);
     }
     if (c1.AutoIncremented != c2.AutoIncremented)
     {
         return(true);
     }
     if (c1.AutoIncrementStart != c2.AutoIncrementStart)
     {
         return(true);
     }
     if (c1.AutoIncrementStep != c2.AutoIncrementStep)
     {
         return(true);
     }
     return(ColumnsAreDifferent(c1, c2));
 }
Example #6
0
        /// <summary>
        /// Adds a class mapping.
        /// </summary>
        /// <param name="fullName">Fully qualified name of the class.</param>
        /// <param name="AssemblyName">Name of the assembly, the class resides in.</param>
        /// <param name="columnAttributes">If not null, the mapping values will be taken from the column attributes.</param>
        /// <returns>A new Class object.</returns>
        public Class AddStandardClass(string fullName, string AssemblyName, OidColumnAttribute[] columnAttributes)
        {
            this.Changed = true;
            Class c = new Class(this);

            c.FullName     = fullName;
            c.AssemblyName = AssemblyName;

            string tableName = fullName.Substring(fullName.LastIndexOf('.') + 1);
            int    p         = tableName.LastIndexOf('`');

            if (p > -1)
            {
                tableName = tableName.Substring(0, p);
            }

            if (this.standardDbOwner != string.Empty)
            {
                tableName = standardDbOwner + "." + tableName;
            }

            bool tableNameIsFree = false;
            int  i = 0;

            while (!tableNameIsFree)
            {
                tableNameIsFree = true;
                foreach (Class c2 in classes.Values)
                {
                    if (c2.TableName == tableName)
                    {
                        tableNameIsFree = false;
                        break;
                    }
                }
                if (!tableNameIsFree)
                {
                    i++;
                    tableName = fullName.Substring(fullName.LastIndexOf('.') + 1) + i.ToString();
                }
            }

            c.TableName = tableName;

            if (connections.Count == 0)
            {
                AddStandardConnection();
            }
            c.ConnectionId = this.connections[0].ID;
            ClassOid oid = c.NewOid();

            if (columnAttributes == null)
            {
                OidColumn column = oid.NewOidColumn();
                column.Name = "ID";
            }
            else
            {
                oid.RemapOidColumns(columnAttributes);
                int count = oid.OidColumns.Count;
                i = 1;
                new OidColumnIterator(oid).Iterate(delegate(OidColumn oidColumn, bool isLastElement)
                {
                    if (string.IsNullOrEmpty(oidColumn.Name))
                    {
                        if (count == 1)
                        {
                            oidColumn.Name = "ID";
                        }
                        else
                        {
                            oidColumn.Name = "ID" + i;
                        }
                    }
                    i++;
                });
            }

            AddClass(c);
            return(c);
        }
Example #7
0
        void IFieldInitializer.InitFields()
        {
            bool isEnhancing = ((IEnhancerSupport)Parent.Parent).IsEnhancing;

            if (!isEnhancing)
            {
                GetOrdinal();
            }

            Class relatedClass = this.RelatedClass;

            Type t = Parent.SystemType;

            if (t == null)
            {
                throw new InternalException(1155, "Relation.InitFields");
            }

            FieldInfo fi = null;

            while (fi == null && t != typeof(object))
            {
                fi = t.GetField(fieldName, BindingFlags.NonPublic | BindingFlags.Instance);
                if (fi == null)
                {
                    t = t.BaseType;
                }
            }
            if (fi == null)
            {
                throw new NDOException(20, "Can't find field " + Parent.SystemType.Name + "." + FieldName);
            }

            FieldType = fi.FieldType;

            System.Attribute     a  = System.Attribute.GetCustomAttribute(fi, typeof(NDORelationAttribute), false);
            NDORelationAttribute ra = (NDORelationAttribute)a;

            this.composition = ra != null && (ra.Info & RelationInfo.Composite) != 0;

            if (fi.FieldType == typeof(System.Collections.IList) || fi.FieldType.GetInterface("IList") != null || fi.FieldType.FullName.StartsWith("System.Collections.Generic.IList`1"))
            {
                this.multiplicity = RelationMultiplicity.List;
            }
            else if (fi.FieldType.GetCustomAttributes(typeof(NDOPersistentAttribute), false).Length > 0)
            {
                this.multiplicity = RelationMultiplicity.Element;
            }
            else
            {
                throw new NDOException(111, "Invalid field type for relation " + t.FullName + "." + FieldName + ": Type = " + fi.FieldType.Name);
            }


            // This could be easier, if we hadn't the choice whether to use
            // polymorphy or not.
            bool cond1 = this.Multiplicity == RelationMultiplicity.Element &&
                         this.ForeignKeyTypeColumnName != null;
            bool cond2 = this.Multiplicity == RelationMultiplicity.List &&
                         this.MappingTable != null && this.MappingTable.ChildForeignKeyTypeColumnName != null;

            hasSubclasses = (relatedClass.HasSubclasses) &&
                            (cond1 || cond2);


            if (this.multiplicity == RelationMultiplicity.List)
            {
                if (ra == null)
                {
                    throw new NDOException(97, $"Can't determine relation type for relation {Parent.FullName}.{fi.Name}");
                }

                if (ra.RelationType == null && fi.FieldType.IsGenericType)
                {
                    this.referencedType = fi.FieldType.GetGenericArguments()[0];
                }
                else
                {
                    this.referencedType = ra.RelationType;
                }
                if (referencedType == null)
                {
                    throw new NDOException(101, "Can't determine referenced type in relation " + this.Parent.FullName + "." + this.fieldName + ". Provide a type parameter for the [NDORelation] attribute.");
                }
            }
            else
            {
                this.referencedType = FieldType;
            }

            if (HasSubclasses && Multiplicity == RelationMultiplicity.List && MappingTable == null)
            {
                //throw new NDOException(21, "Polymorphic 1:n-relation w/o mapping table is not supported");
                Debug.WriteLine("NDO Warning: Polymorphic 1:n-relation " + Parent.FullName + "." + this.FieldName + " w/o mapping table");
            }

            this.definingClass = this.Parent;
            Type bt = this.Parent.SystemType.BaseType;

            while (typeof(IPersistenceCapable).IsAssignableFrom(bt))
            {
                Class pcl = this.Parent.Parent.FindClass(bt);
                if (pcl.FindRelation(this.fieldName) != null)
                {
                    this.definingClass = pcl;
                }
                else
                {
                    break;
                }
                bt = bt.BaseType;
            }

            // Do not set fkColumn.Size to a value != 0 in during enhancing,
            // because that value would be hard coded into the mapping file.
            // Use (!isEnhancing) to make sure, that the code wasn't called  from the enhancer.
            if (this.MappingTable != null)
            {
                // r.ForeignKeyColumns points to the own table.
                if (Parent.Oid.OidColumns.Count != this.foreignKeyColumns.Count)
                {
                    throw new NDOException(115, "Column count between relation and Oid doesn't match. Type " + Parent.FullName + " has an oid column count of " + Parent.Oid.OidColumns.Count + ". The Relation " + Parent.FullName + "." + this.fieldName + " has a foreign key column count of " + this.foreignKeyColumns.Count + '.');
                }
                int i = 0;
                new ForeignKeyIterator(this).Iterate(delegate(ForeignKeyColumn fkColumn, bool isLastIndex)
                {
                    OidColumn oidColumn = (OidColumn)Parent.Oid.OidColumns[i];
                    if (!isEnhancing && fkColumn.Size == 0)
                    {
                        fkColumn.Size = oidColumn.Size;
                    }
                    fkColumn.SystemType = oidColumn.SystemType;
                    i++;
                }
                                                     );

                // r.MappingTable.ChildForeignKeyColumns points to the table of the related class.
                if (relatedClass.Oid.OidColumns.Count != this.mappingTable.ChildForeignKeyColumns.Count())
                {
                    throw new NDOException(115, "Column count between relation and Oid doesn't match. Type " + relatedClass.FullName + " has an oid column count of " + relatedClass.Oid.OidColumns.Count + ". The Relation " + this.Parent.FullName + "." + this.fieldName + " has a foreign key column count of " + this.mappingTable.ChildForeignKeyColumns.Count() + '.');
                }
                i = 0;
                new ForeignKeyIterator(this.mappingTable).Iterate(delegate(ForeignKeyColumn fkColumn, bool isLastIndex)
                {
                    OidColumn oidColumn = (OidColumn)relatedClass.Oid.OidColumns[i];
                    if (!isEnhancing && fkColumn.Size == 0)
                    {
                        fkColumn.Size = oidColumn.Size;
                    }
                    fkColumn.SystemType = oidColumn.SystemType;
                    i++;
                }
                                                                  );
            }
            else if (this.multiplicity == RelationMultiplicity.Element)  // The foreign key points to the tabel of the related class.
            {
                if (relatedClass.Oid.OidColumns.Count != this.foreignKeyColumns.Count)
                {
                    throw new NDOException(115, "Column count between relation and Oid doesn't match. Type " + relatedClass.FullName + " has an oid column count of " + relatedClass.Oid.OidColumns.Count + ". The Relation " + Parent.FullName + "." + this.fieldName + " has a foreign key column count of " + this.foreignKeyColumns.Count + '.');
                }
                int i = 0;
                new ForeignKeyIterator(this).Iterate(delegate(ForeignKeyColumn fkColumn, bool isLastIndex)
                {
                    OidColumn oidColumn = (OidColumn)relatedClass.Oid.OidColumns[i];
                    if (!isEnhancing && fkColumn.Size == 0)
                    {
                        fkColumn.Size = oidColumn.Size;
                    }
                    fkColumn.SystemType = oidColumn.SystemType;
                    i++;
                }
                                                     );
            }
            else  // List relation. The foreign key points to the own table.
            {
                if (Parent.Oid.OidColumns.Count != this.foreignKeyColumns.Count)
                {
                    throw new NDOException(115, "Column count between relation and Oid doesn't match. Type " + Parent.FullName + " has an oid column count of " + Parent.Oid.OidColumns.Count + ". The Relation " + Parent.FullName + "." + this.fieldName + " has a foreign key column count of " + this.foreignKeyColumns.Count + '.');
                }
                int i = 0;
                new ForeignKeyIterator(this).Iterate(delegate(ForeignKeyColumn fkColumn, bool isLastIndex)
                {
                    OidColumn oidColumn = (OidColumn)Parent.Oid.OidColumns[i];
                    if (!isEnhancing && fkColumn.Size == 0)
                    {
                        fkColumn.Size = oidColumn.Size;
                    }
                    fkColumn.SystemType = oidColumn.SystemType;
                    i++;
                }
                                                     );
            }
        }