private void ProcessForeignKeys()
        {
            metaProvider.GetForeignKeys(this.Tables);
            metaProvider.GetIndexes(this.Indexes, this.Tables);

            foreach (MetaInfoTable table in this.Tables)
            {
                foreach (MetaInfoForeignKey fk in table.ForeignKeys)
                {
                    md.SetCurrentToRoot();
                    fk.Node = md.CreateElement("Relation");
                    XmlNode commentNode = md.CreateComment("Relation imported from FK {0}", fk.FullPersistentName);
                    md.AddAttribute("name", fk.Name);
                    md.AddAttribute("entity", fk.Child.Name);
                    // name2 will be generated by GL
                    md.AddAttribute("entity2", fk.Parent.Name);
                    md.AddAttribute("cardinality", "M:1");
                    md.AddAttribute("persistentName", fk.PersistentName);

                    // Try to associate with index
                    MetaInfoColumns fkCols = new MetaInfoColumns();
                    foreach (MetaInfoColumnsMatch cm in fk.ColumnsMatches)
                    {
                        fkCols.Add(cm.Child);
                    }
                    MetaInfoIndex ix = this.Indexes.FindByColumns(fkCols);
                    if (ix != null)
                    {
                        md.AddAttribute("indexName", ix.PersistentName);
                        ix.Generate = false;
                    }

                    foreach (MetaInfoColumnsMatch cm in fk.ColumnsMatches)
                    {
                        md.CreateElement("AttributeMatch");
                        md.AddAttribute("attribute", cm.Child.Name);
                        md.AddAttribute("attribute2", cm.Parent.Name);
                        fk.Node.AppendChild(md.CurrentNode);
                    }

                    // Place node after child entity declaration
                    if (fk.Child != null)
                    {
                        md.Document.DocumentElement.InsertAfter(fk.Node, fk.Child.Node);
                    }
                    else
                    {
                        md.Document.DocumentElement.AppendChild(commentNode);
                        md.Document.DocumentElement.AppendChild(fk.Node);
                    }
                }
            }
        }
Example #2
0
 public MetaInfoIndex FindByColumns(MetaInfoColumns cols)
 {
     foreach (MetaInfoIndex ix in this)
     {
         if (ix.Columns.Count == cols.Count)
         {
             bool colsMatched = true;
             for (int i = 0; i < cols.Count; i++)
             {
                 if (ix.Columns[i].Table.FullPersistentName != cols[i].Table.FullPersistentName ||
                     ix.Columns[i].Name != cols[i].Name)
                 {
                     colsMatched = false;
                     break;
                 }
             }
             if (colsMatched)
             {
                 return(ix);
             }
         }
     }
     return(null);
 }