Ejemplo n.º 1
0
        private static IMetaItem CreateTable(Type type, IEnumerable <IColumn> dbColumns, IEnumerable <IRelation> dbRelations)
        {
            MetaTable  table      = new MetaTable();
            EntityInfo entityInfo = CacheManager.GetEntityInfo(type);

            table.Name = entityInfo.TableInfo.TableName;

            foreach (IColumn dbColumn in dbColumns)
            {
                MetaColumn metaColumn = new MetaColumn();
                metaColumn.ColumnType = dbColumn.ColumnType;
                metaColumn.Name       = dbColumn.ColumnName;
                metaColumn.Null       = dbColumn.Nullable;
                metaColumn.Size       = dbColumn.Size;
                table.Columns.Add(metaColumn);
            }

            foreach (IRelation relation in dbRelations)
            {
                EntityInfo relatedEntityInfo = CacheManager.GetEntityInfo(relation.RelatedObjectType);

                MetaForeignKey foreignKey = new MetaForeignKey();
                foreignKey.Name    = relation.RelationShipName;
                foreignKey.ToTable = relatedEntityInfo.TableInfo.TableName;
                foreach (RelationColumnMapping mapping in relation.TableColumnMappings)
                {
                    string fromCol = entityInfo.FindColumnByAttribute(mapping.FromField).ColumnName;
                    string toCol   = relatedEntityInfo.FindColumnByAttribute(mapping.ToField).ColumnName;
                    foreignKey.ColumnMappings.Add(new MetaForeignKeyColumnMapping(fromCol, toCol));
                }
                foreignKey.DeleteRule = relation.DeleteRule;
                foreignKey.UpdateRule = relation.UpdateRule;
                table.ForeignKeys.Add(foreignKey);
            }

            MetaPrimaryKey primaryKey = new MetaPrimaryKey();

            primaryKey.Name = "pk_" + table.Name;
            foreach (IColumn dbColumn in dbColumns)
            {
                if (dbColumn.Key)
                {
                    primaryKey.ColumnNames.Add(dbColumn.ColumnName);
                }
            }
            if (primaryKey.ColumnNames.Count > 0)
            {
                table.PrimaryKey = primaryKey;
            }
            return(table);
        }
Ejemplo n.º 2
0
        protected override void ExtractForeignKeyData(ITransaction tx, MetaTable table)
        {
            try
            {
                var cmd = tx.CreateCommand();
                cmd.CommandText = $"SELECT f.name AS ForeignKey," +
                                  $" OBJECT_NAME(f.parent_object_id) AS TableName," +
                                  $" COL_NAME(fc.parent_object_id, fc.parent_column_id) AS ColumnName," +
                                  $" OBJECT_NAME (f.referenced_object_id) AS ReferenceTableName," +
                                  $" COL_NAME(fc.referenced_object_id," +
                                  $" fc.referenced_column_id) AS ReferenceColumnName," +
                                  $" f.update_referential_action as updateAction," +
                                  $" f.delete_referential_action as deleteAction " +
                                  $"FROM sys.foreign_keys AS f INNER JOIN sys.foreign_key_columns AS fc " +
                                  $" ON f.OBJECT_ID = fc.constraint_object_id " +
                                  $"WHERE OBJECT_NAME(f.parent_object_id) = '{table.Name}'; ";

                using (var reader = cmd.ExecuteReader())
                {
                    while (reader.Read())
                    {
                        var foreignKeyName = reader["ForeignKey"].ToString();
                        var foreignKey     = table.ForeignKeys.SingleOrDefault(fk => fk.Name == foreignKeyName);

                        if (foreignKey == null)
                        {
                            foreignKey            = new MetaForeignKey();
                            foreignKey.Name       = foreignKeyName;
                            foreignKey.ToTable    = reader["ReferenceTableName"].ToString();
                            foreignKey.UpdateRule = (ReferentialRuleType)
                                                    int.Parse(reader["updateAction"].ToString());
                            foreignKey.DeleteRule = (ReferentialRuleType)
                                                    int.Parse(reader["deleteAction"].ToString());
                        }

                        foreignKey.ColumnMappings.Add(new MetaForeignKeyColumnMapping(
                                                          reader["columnName"].ToString(),
                                                          reader["referenceColumnName"].ToString())
                                                      );
                    }
                }
            }
            catch (Exception e)
            {
                Logger.GetLogger(Config.LoggerName).Fatal(
                    string.Format("Exception occured while trying to read primary key in table {0}", table.Name), e);
                throw new DBPatchingException(e.Message, e);
            }
        }
Ejemplo n.º 3
0
        protected override void ExtractForeignKeyData(ITransaction tx, MetaTable table)
        {
            var foreignKeyMap = new Dictionary <string, MetaForeignKey>();

            var fromTableColMap = new Dictionary <string, Dictionary <int, string> >();
            var toTableColMap   = new Dictionary <string, Dictionary <int, string> >();

            try
            {
                var       dbConnection = (DbConnection)tx.Connection;
                DataTable fkTable      = dbConnection.GetSchema("Foreign_Keys", new[] { null, null, table.Name, null });
                if (fkTable == null)
                {
                    Logger.GetLogger(Config.LoggerName).Fatal(
                        string.Format("Unable to read the list of foregin keys in table {0}", table.Name));
                    return;
                }
                foreach (DataRow fkRow in fkTable.Rows)
                {
                    string fkName = fkRow["FK_NAME"].ToString();
                    if (!foreignKeyMap.ContainsKey(fkName))
                    {
                        var foreignKey = new MetaForeignKey();
                        table.ForeignKeys.Add(foreignKey);
                        foreignKeyMap.Add(fkName, foreignKey);
                        foreignKey.Name       = fkName;
                        foreignKey.ToTable    = fkRow["FK_TABLE_NAME"].ToString();
                        foreignKey.UpdateRule = (ReferentialRuleType)fkRow["UPDATE_RULE"];
                        foreignKey.DeleteRule = (ReferentialRuleType)fkRow["DELETE_RULE"];

                        fromTableColMap.Add(fkName, new Dictionary <int, string>());
                        toTableColMap.Add(fkName, new Dictionary <int, string>());
                    }

                    fromTableColMap[fkName].Add(fromTableColMap[fkName].Count, fkRow["PK_COLUMN_NAME"].ToString());
                    toTableColMap[fkName].Add(toTableColMap[fkName].Count, fkRow["FK_COLUMN_NAME"].ToString());
                }

                foreach (string key in foreignKeyMap.Keys)
                {
                    MetaForeignKey foreignKey = foreignKeyMap[key];

                    var fromList = new List <int>(fromTableColMap[key].Keys);
                    fromList.Sort();

                    var toList = new List <int>(toTableColMap[key].Keys);
                    toList.Sort();

                    IEnumerator <int> toListEnumerator = toList.GetEnumerator();
                    toListEnumerator.Reset();

                    foreach (int ordinal in fromList)
                    {
                        string fromCol = fromTableColMap[key][ordinal];
                        string toCol   = null;
                        if (toListEnumerator.MoveNext())
                        {
                            toCol = toTableColMap[key][toListEnumerator.Current];
                        }
                        foreignKey.ColumnMappings.Add(new MetaForeignKeyColumnMapping(fromCol, toCol));
                    }
                }
            }
            catch (Exception e)
            {
                Logger.GetLogger(Config.LoggerName).Fatal(
                    string.Format("Exception occured while trying to read foreign key information in table {0}",
                                  table.Name), e);
                throw new DBPatchingException(e.Message, e);
            }
        }