public override IList<DatabaseConstraint> ForeignKeys(string tableName)
 {
     var dt = _schemaReader.ForeignKeys(tableName);
     var fkcols = _schemaReader.ForeignKeyColumns(tableName);
     var converter = new SchemaConstraintConverter(dt, ConstraintType.ForeignKey);
     var fks = converter.Constraints();
     var fkColumnConverter = new ForeignKeyColumnConverter(fkcols);
     fkColumnConverter.AddForeignKeyColumns(fks);
     return fks;
 }
Ejemplo n.º 2
0
        /// <summary>
        /// Gets the table. If <see cref="Owner"/> is specified, it is used.
        /// </summary>
        /// <param name="tableName">Name of the table. Oracle names can be case sensitive.</param>
        public DatabaseTable Table(string tableName)
        {
            if (string.IsNullOrEmpty(tableName)) throw new ArgumentNullException("tableName");

            var schemaOwner = _schemaReader.Owner;
            DatabaseTable table;
            using (DataSet ds = _schemaReader.Table(tableName))
            {
                if (ds == null) return null;
                if (ds.Tables.Count == 0) return null;

                table = DatabaseSchema.FindTableByName(tableName, schemaOwner);
                if (table == null)
                {
                    table = new DatabaseTable();
                    DatabaseSchema.Tables.Add(table);
                }
                table.Name = tableName;
                table.SchemaOwner = schemaOwner;
                //columns must be done first as it is updated by the others
                table.Columns.Clear();
                var columnConverter = new ColumnConverter(ds.Tables[_schemaReader.ColumnsCollectionName]);
                var databaseColumns = columnConverter.Columns(tableName, schemaOwner).ToList();
                if (!databaseColumns.Any())
                {
                    //need to define the schema
                    databaseColumns = columnConverter.Columns().ToList();
                    var first = databaseColumns.FirstOrDefault();
                    if (first != null)
                    {
                        //take the schema of the first we find
                        table.SchemaOwner = schemaOwner = first.SchemaOwner;
                    }
                    databaseColumns = columnConverter.Columns(tableName, schemaOwner).ToList();
                }
                table.Columns.AddRange(databaseColumns);
                if (ds.Tables.Contains(_schemaReader.PrimaryKeysCollectionName))
                {
                    var converter = new SchemaConstraintConverter(ds.Tables[_schemaReader.PrimaryKeysCollectionName], ConstraintType.PrimaryKey);
                    var pkConstraints = converter.Constraints();
                    PrimaryKeyLogic.AddPrimaryKey(table, pkConstraints);
                }
                if (ds.Tables.Contains(_schemaReader.ForeignKeysCollectionName))
                {
                    var converter = new SchemaConstraintConverter(ds.Tables[_schemaReader.ForeignKeysCollectionName], ConstraintType.ForeignKey);
                    table.AddConstraints(converter.Constraints());
                }
                if (ds.Tables.Contains(_schemaReader.ForeignKeyColumnsCollectionName))
                {
                    var fkConverter = new ForeignKeyColumnConverter(ds.Tables[_schemaReader.ForeignKeyColumnsCollectionName]);
                    fkConverter.AddForeignKeyColumns(table.ForeignKeys);
                }

                if (ds.Tables.Contains(_schemaReader.UniqueKeysCollectionName))
                {
                    var converter = new SchemaConstraintConverter(ds.Tables[_schemaReader.UniqueKeysCollectionName], ConstraintType.UniqueKey);
                    table.AddConstraints(converter.Constraints());
                }
                if (ds.Tables.Contains(_schemaReader.ComputedColumnsCollectionName))
                {
                    SchemaConstraintConverter.AddComputed(ds.Tables[_schemaReader.ComputedColumnsCollectionName], table);
                }

                var indexConverter = new IndexConverter(ds.Tables[_schemaReader.IndexColumnsCollectionName], null);
                table.Indexes.AddRange(indexConverter.Indexes(tableName, schemaOwner));

                if (ds.Tables.Contains(_schemaReader.IdentityColumnsCollectionName))
                    SchemaConstraintConverter.AddIdentity(ds.Tables[_schemaReader.IdentityColumnsCollectionName], table);

                _schemaReader.PostProcessing(table);

            }

            if (DatabaseSchema.DataTypes.Count > 0)
                DatabaseSchemaFixer.UpdateDataTypes(DatabaseSchema);
            _schemaReader.PostProcessing(DatabaseSchema);

            return table;
        }
 public override IList<DatabaseConstraint> DefaultConstraints(string tableName)
 {
     var dt = _schemaReader.DefaultConstraints(tableName);
     var converter = new SchemaConstraintConverter(dt, ConstraintType.Default);
     return converter.Constraints();
 }