/// <summary> /// Returns the custom property sql table for an SqlTable /// </summary> /// <returns></returns> private static SqlTable GetCustomPropertySqlTableForEntityType(EntityType entityType) { // mbr - 08-12-2005 - changed... // string name = GetExtendedNativeNameForEntityType(entityType).Name; NativeName name = entityType.NativeNameExtended; SqlTable entityTypeTable = new SqlTable(null, entityType); SqlTable table = new SqlTable(name.Name); // add the standard columns... SqlColumn[] columns = new SqlColumn[6]; columns[0] = new SqlColumn(NameColumnName, NameColumnName, DbType.String, MaxNativeNameLength, EntityFieldFlags.Key); columns[1] = new SqlColumn(IntegerColumnName, IntegerColumnName, DbType.Int64, -1, EntityFieldFlags.Nullable); columns[2] = new SqlColumn(DecimalColumnName, DecimalColumnName, DbType.Decimal, -1, EntityFieldFlags.Nullable); columns[3] = new SqlColumn(DateTimeColumnName, DateTimeColumnName, DbType.DateTime, -1, EntityFieldFlags.Nullable); columns[4] = new SqlColumn(StringColumnName, StringColumnName, DbType.String, MaxStringPropertyLength, EntityFieldFlags.Nullable); columns[5] = new SqlColumn(BinaryColumnName, BinaryColumnName, DbType.Object, -1, EntityFieldFlags.Nullable | EntityFieldFlags.Large); // walk... foreach (SqlColumn column in entityTypeTable.GetKeyColumns()) { SqlColumn keyColumn = (SqlColumn)column.Clone(); if (keyColumn.IsAutoIncrement) { keyColumn.Flags ^= EntityFieldFlags.AutoIncrement; } // add... table.Columns.Add(keyColumn); } // add... table.Columns.AddRange(columns); return(table); }
/// <summary> /// Adds links to parents in the schema. /// </summary> /// <param name="schema"></param> private void AddParentLinks(SqlSchema schema) { if (schema == null) { throw new ArgumentNullException("schema"); } // get all constraints... DataTable table = this.ExecuteDataTable(new SqlStatement(@"select ctuparent.table_name as parent_table_name, unique_constraint_name as parent_constraint_name, ctuchild.table_name as child_table_name, rc.constraint_name as child_constraint_name from information_schema.referential_constraints rc inner join information_schema.constraint_table_usage ctuparent on unique_constraint_name = ctuparent.constraint_name inner join information_schema.constraint_table_usage ctuchild on rc.constraint_name = ctuchild.constraint_name order by ctuparent.table_name, rc.constraint_name" , this.Dialect)); // walk each row... foreach (DataRow constraint in table.Rows) { // parent/child... SqlTable parent = schema.Tables[(string)constraint["parent_table_name"]]; if (parent != null) { // child... SqlTable child = schema.Tables[(string)constraint["child_table_name"]]; if (child != null) { // get the names... string parentConstraint = (string)constraint["parent_constraint_name"]; string childConstraint = (string)constraint["child_constraint_name"]; // find the columns... SqlColumn[] parentColumns = this.GetConstraintColumns(parent, parentConstraint); SqlColumn[] childColumns = this.GetConstraintColumns(child, childConstraint); // check... if (parentColumns == null) { throw new InvalidOperationException("parentColumns is null."); } if (childColumns == null) { throw new InvalidOperationException("childColumns is null."); } if (parentColumns.Length > 0 && parentColumns.Length == childColumns.Length) { // get the parent key columns... ArrayList parentKeyColumns = new ArrayList(parent.GetKeyColumns()); if (parentKeyColumns.Count > 0) { // loop and remove... foreach (SqlColumn parentColumn in parentColumns) { parentKeyColumns.Remove(parentColumn); } // eliminated? if (parentKeyColumns.Count == 0) { // this is a legitimate match... create a link for that... SqlChildToParentLink link = new SqlChildToParentLink(childConstraint, parent); // add columns... link.Columns.AddRange(childColumns); link.LinkFields.AddRange(parent.GetKeyColumns()); // add... child.LinksToParents.Add(link); // mbr - 04-10-2007 - set the owner... link.SetSchema(child.Schema); } } } } } } }