public DbConstraintPkViewModel(DbTable table, DbPrimaryKey dbPrimaryKey) { this.dbPrimaryKey = dbPrimaryKey; this.DisplayName = "Pk " + table.DisplayName; this.Name = dbPrimaryKey.Name; this.Id = dbPrimaryKey.Id; }
private void LoadConstraints(DbSchema schema, XmlDocument doc) { XmlNode nodeModel = doc.SelectSingleNode("/Model/RootObject/Children/Model"); XmlNodeList nodes = doc.SelectNodes("/Model/RootObject/Children/Model/Tables/Table"); XmlNodeList nodeReferences = nodeModel.SelectNodes("References/Reference"); foreach (XmlNode nodeReference in nodeReferences) { if (nodeReference.SelectSingleNode("Cardinality").InnerText.Equals("0..*")) { DbForeignKey fk = new DbForeignKey(); fk.Id = nodeReference.Attributes["Id"].Value; fk.Name = nodeReference.SelectSingleNode("Code").InnerText; fk.DisplayName = nodeReference.SelectSingleNode("Name").InnerText; DbTable parentTable = (from t in schema.Tables where t.Id.Equals (nodeReference.SelectSingleNode ("ParentTable/Table").Attributes["Ref"].Value) select t).FirstOrDefault(); DbTable childTable = (from t in schema.Tables where t.Id.Equals(nodeReference.SelectSingleNode("ChildTable/Table").Attributes["Ref"].Value) select t).FirstOrDefault(); fk.ParentTableId = parentTable.Id; fk.ChildTableId = childTable.Id; childTable.ForeignKeys.Add(fk); fk.UpdateOnCascade = nodeReference.SelectSingleNode("UpdateConstraint").InnerXml.Equals("1"); fk.DeleteOnCascade = nodeReference.SelectSingleNode("DeleteConstraint").InnerXml.Equals("1"); XmlNodeList nodeJoins = nodeReference.SelectNodes("Joins/ReferenceJoin"); foreach (XmlNode nodeJoin in nodeJoins) { DbForeignKeyJoin join = new DbForeignKeyJoin(); join.Id = nodeJoin.Attributes["Id"].Value; String parentRef = nodeJoin.SelectSingleNode("Object1/Column").Attributes["Ref"].Value; String childRef = nodeJoin.SelectSingleNode("Object2/Column").Attributes["Ref"].Value; DbColumn parentColumn = (from c in parentTable.Columns where c.Id.Equals(parentRef) select c).FirstOrDefault(); DbColumn childColumn = (from c in childTable.Columns where c.Id.Equals(childRef) select c).FirstOrDefault(); if (parentColumn == null) throw new Exception ("Colonne parent null"); if (childColumn == null) throw new Exception("Colonne fille null"); join.ParentColumnId = parentColumn.Id; join.ChildColumnId = childColumn.Id; fk.Joins.Add(join); } } } foreach (XmlNode nodeTable in nodes) { String id = nodeTable.Attributes["Id"].Value; DbTable table = (from t in schema.Tables where t.Id.Equals(id) select t).FirstOrDefault(); XmlNode nodePk = nodeTable.SelectSingleNode("Keys/Key"); if (nodePk != null) { DbPrimaryKey pk = new DbPrimaryKey(); pk.Id = nodePk.Attributes["Id"].Value; pk.Name = nodePk.SelectSingleNode("Code").InnerText; pk.DisplayName = nodePk.SelectSingleNode("Name").InnerText; XmlNodeList pkColumnNodeList = nodePk.SelectNodes("Key.Columns/Column"); foreach (XmlNode pkColumnNode in pkColumnNodeList) { pk.ColumnIds.Add(pkColumnNode.Attributes["Ref"].Value); } table.PrimaryKey = pk; } if (table.PrimaryKey == null) { throw new Exception("Table " + table.Name + " sans clé primaire"); } XmlNodeList nodeIndexes = nodeTable.SelectNodes("Indexes/Index"); foreach (XmlNode nodeIndex in nodeIndexes) { if (nodeIndex.SelectSingleNode("Code") != null) { String name = nodeIndex.SelectSingleNode("Code").InnerText; if ((table.PrimaryKey != null && !table.PrimaryKey.Name.Equals(name)) || table.PrimaryKey == null) { if (nodeIndex.SelectSingleNode("Unique") != null && nodeIndex.SelectSingleNode("Unique").InnerText.Equals("1")) { DbUniqueKey uk = new DbUniqueKey(); uk.Id = nodeIndex.Attributes["Id"].Value; uk.Name = nodeIndex.SelectSingleNode("Code").InnerText; uk.DisplayName = nodeIndex.SelectSingleNode("Name").InnerText; XmlNodeList pkColumnNodeList = nodeIndex.SelectNodes("IndexColumns/IndexColumn"); foreach (XmlNode pkColumnNode in pkColumnNodeList) { XmlNode nodCol = pkColumnNode.SelectSingleNode("IndexColumn.Expression"); if (nodCol != null) { String expression = nodCol.InnerText; expression = expression.Replace(" ", ""); expression = expression.Replace("(", ""); expression = expression.Replace(")", ""); DbColumn column = (from c in table.Columns where c.Name.Equals(expression) select c).FirstOrDefault(); uk.ColumnIds.Add(column.Id); } } if (uk.ColumnIds.Count > 0) { table.UniqueKeys.Add(uk); } } } } } } }