public void CreateForeignKey( Dictionary <string, UserTable> userTables, Dictionary <string, PrimaryKeyColumn> primaryKeyColumns, Dictionary <string, UniqueConstraintColumn> uniqueConstraintColumns, IDataRecord reader) { var schemaName = Convert.ToString(reader[SchemaNameOrdinal]); var tableName = Convert.ToString(reader[TableNameOrdinal]); var columnName = Convert.ToString(reader[ColumnNameOrdinal]); var objectName = Convert.ToString(reader[ObjectNameOrdinal]); var referencedSchemaName = Convert.ToString(reader[ReferencedSchemaNameOrdinal]); var referencedTableName = Convert.ToString(reader[ReferencedTableNameOrdinal]); var referencedColumnName = Convert.ToString(reader[ReferencedColumnNameOrdinal]); var referencedObjectName = Convert.ToString(reader[ReferencedObjectNameOrdinal]); var userTableNamespaceBuilder = new StringBuilder(schemaName.Length + tableName.Length + 1); userTableNamespaceBuilder.Append(schemaName). Append(Constants.Dot). Append(tableName); var userTableNamespace = userTableNamespaceBuilder.ToString(); if (!userTables.ContainsKey(userTableNamespace)) { return; } var userTable = userTables[userTableNamespace]; if (userTable == null) { return; } var referencedUserTableNamespaceBuilder = new StringBuilder(referencedSchemaName.Length + referencedTableName.Length + 1); referencedUserTableNamespaceBuilder.Append(referencedSchemaName). Append(Constants.Dot). Append(referencedTableName); var referencedUserTableNamespace = referencedUserTableNamespaceBuilder.ToString(); if (!userTables.ContainsKey(referencedUserTableNamespace)) { return; } var referencedUserTable = userTables[referencedUserTableNamespace]; if (referencedUserTable == null) { return; } var referencedUserTableColumnNamespaceBuilder = new StringBuilder(referencedUserTableNamespace.Length + referencedObjectName.Length + referencedColumnName.Length + 2); referencedUserTableColumnNamespaceBuilder.Append(referencedUserTableNamespace). Append(Constants.Dot). Append(referencedObjectName). Append(Constants.Dot). Append(referencedColumnName); var referencedUserTableColumnNamespace = referencedUserTableColumnNamespaceBuilder.ToString(); PrimaryKeyColumn referencedPrimaryKeyColumn; primaryKeyColumns.TryGetValue(referencedUserTableColumnNamespace, out referencedPrimaryKeyColumn); UniqueConstraintColumn referencedUniqueConstraintColumn = null; if (referencedPrimaryKeyColumn == null) { uniqueConstraintColumns.TryGetValue(referencedUserTableColumnNamespace, out referencedUniqueConstraintColumn); } if (referencedPrimaryKeyColumn == null && referencedUniqueConstraintColumn == null) { throw new Exception( string.Format( "The foreign key {0}.{1} column has no associated primary key or unique constraint column.", objectName, columnName)); } var foreignKeyNamespaceBuilder = new StringBuilder(userTableNamespace.Length + objectName + 1); foreignKeyNamespaceBuilder.Append(userTableNamespace). Append(Constants.Dot). Append(objectName); var foreignKeyNamespace = foreignKeyNamespaceBuilder.ToString(); var foreignKey = userTable.ForeignKeys[foreignKeyNamespace]; if (foreignKey == null) { foreignKey = new ForeignKey { UserTable = userTable, ObjectName = objectName, IsDisabled = Convert.ToBoolean(reader[IsDisabledOrdinal]), IsNotForReplication = Convert.ToBoolean(reader[IsNotForReplicationOrdinal]), IsNotTrusted = Convert.ToBoolean(reader[IsNotTrustedOrdinal]), IsSystemNamed = Convert.ToBoolean(reader[IsSystemNamedOrdinal]), DeleteAction = Convert.ToInt32(reader[DeleteActionOrdinal]), DeleteActionDescription = Convert.ToString(reader[DeleteActionDescriptionOrdinal]), UpdateAction = Convert.ToInt32(reader[UpdateActionOrdinal]), UpdateActionDescription = Convert.ToString(reader[UpdateActionDescriptionOrdinal]) }; userTable.ForeignKeys.Add(foreignKey); } var foreignKeyColumn = new ForeignKeyColumn { ReferencedUserTable = referencedUserTable, ForeignKey = foreignKey, ObjectName = columnName, KeyOrdinal = Convert.ToInt32(reader[KeyOrdinalOrdinal]), ReferencedColumn = referencedPrimaryKeyColumn as IIndexColumn ?? referencedUniqueConstraintColumn }; foreignKey.ForeignKeyColumns.Add(foreignKeyColumn); }
public void CreateUniqueConstraint( Dictionary <string, UserTable> userTables, Dictionary <string, UniqueConstraintColumn> uniqueConstraintColumns, IDataRecord reader) { var schemaName = Convert.ToString(reader[SchemaNameOrdinal]); var tableName = Convert.ToString(reader[TableNameOrdinal]); var objectName = Convert.ToString(reader[ObjectNameOrdinal]); var userTableNamespaceBuilder = new StringBuilder(schemaName.Length + tableName.Length + 1); userTableNamespaceBuilder.Append(schemaName). Append(Constants.Dot). Append(tableName); var userTableNamespace = userTableNamespaceBuilder.ToString(); if (!userTables.ContainsKey(userTableNamespace)) { return; } var userTable = userTables[userTableNamespace]; if (userTable == null) { return; } var uniqueConstraintNamespaceBuilder = new StringBuilder(userTableNamespace.Length + objectName.Length + 1); uniqueConstraintNamespaceBuilder. Append(userTableNamespace). Append(Constants.Dot). Append(objectName); var uniqueConstraintNamespace = uniqueConstraintNamespaceBuilder.ToString(); var uniqueConstraint = userTable.UniqueConstraints[uniqueConstraintNamespace]; if (uniqueConstraint == null) { uniqueConstraint = new UniqueConstraint { UserTable = userTable, ObjectName = objectName, FileGroup = Convert.ToString(reader[FileGroupOrdinal]), IgnoreDupKey = Convert.ToBoolean(reader[IgnoreDupKeyOrdinal]), IsClustered = Convert.ToBoolean(reader[IsClusteredOrdinal]), FillFactor = Convert.ToInt32(reader[FillFactorOrdinal]), IsPadded = Convert.ToBoolean(reader[IsPaddedOrdinal]), IsDisabled = Convert.ToBoolean(reader[IsDisabledOrdinal]), AllowRowLocks = Convert.ToBoolean(reader[AllowRowLocksOrdinal]), AllowPageLocks = Convert.ToBoolean(reader[AllowPageLocksOrdinal]), IndexType = Convert.ToString(reader[IndexTypeOrdinal]) // TODO: Remove this if possible... check other index code logic for usage (Mysql has BTREE, FULLTEXT, etc..., SQL Server doesn't) }; userTable.UniqueConstraints.Add(uniqueConstraint); } var uniqueConstraintColumn = new UniqueConstraintColumn { UniqueConstraint = uniqueConstraint, ObjectName = Convert.ToString(reader[ColumnNameOrdinal]), IsDescendingKey = Convert.ToBoolean(reader[IsDescendingKeyOrdinal]), KeyOrdinal = Convert.ToInt32(reader[KeyOrdinalOrdinal]), PartitionOrdinal = Convert.ToInt32(reader[PartitionOrdinalOrdinal]) }; uniqueConstraint.UniqueConstraintColumns.Add(uniqueConstraintColumn); uniqueConstraintColumns.Add(uniqueConstraintColumn.Namespace, uniqueConstraintColumn); }