コード例 #1
0
        public void ParseOracle12Identity()
        {
            //arrange
            var table = new DatabaseSchema(null, SqlType.Oracle)
                        .AddTable("Demo")
                        .AddColumn("ID", DbType.Int32).AddPrimaryKey()
                        .AddColumn("NAME").Table;

            var dataTable = new DataTable("Name")
            {
                Locale = CultureInfo.InvariantCulture
            };

            dataTable.Columns.Add("TableName");
            dataTable.Columns.Add("ColumnName");
            dataTable.Columns.Add("IDENTITY_OPTIONS");
            dataTable.Columns.Add("GENERATION_TYPE");
            dataTable.Rows.Add(new object[] { "Demo", "ID",
                                              "START WITH: 1, INCREMENT BY: 1, MAX_VALUE: 9999999999999999999999999999, MIN_VALUE: 1, CYCLE_FLAG: N, CACHE_SIZE: 20, ORDER_FLAG: N",
                                              "BY DEFAULT" });

            //act
            SchemaConstraintConverter.AddIdentity(dataTable, table);
            var id = table.FindColumn("ID");

            //assert
            Assert.IsTrue(table.HasAutoNumberColumn);
            Assert.IsTrue(id.IsAutoNumber);
            Assert.AreEqual(1, id.IdentityDefinition.IdentitySeed);
            Assert.AreEqual(1, id.IdentityDefinition.IdentityIncrement);
            Assert.IsTrue(id.IdentityDefinition.IdentityByDefault);
        }
コード例 #2
0
ファイル: TableLoader.cs プロジェクト: zls3201/dbschemareader
        private void AddOthers(string schemaOwner, string tableName, DatabaseTable table, DataSet ds)
        {
            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);
            }
        }
コード例 #3
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");
            }

            DatabaseTable table;

            using (DataSet ds = _sr.Table(tableName))
            {
                if (ds == null)
                {
                    return(null);
                }
                if (ds.Tables.Count == 0)
                {
                    return(null);
                }

                table = DatabaseSchema.FindTableByName(tableName);
                if (table == null)
                {
                    table = new DatabaseTable();
                    DatabaseSchema.Tables.Add(table);
                }
                table.Name        = tableName;
                table.SchemaOwner = _sr.Owner;
                //columns must be done first as it is updated by the others
                table.Columns.Clear();
                var columnConverter = new ColumnConverter(ds.Tables[_sr.ColumnsCollectionName]);
                table.Columns.AddRange(columnConverter.Columns());
                if (ds.Tables.Contains(_sr.PrimaryKeysCollectionName))
                {
                    var converter     = new SchemaConstraintConverter(ds.Tables[_sr.PrimaryKeysCollectionName], ConstraintType.PrimaryKey);
                    var pkConstraints = converter.Constraints();
                    PrimaryKeyLogic.AddPrimaryKey(table, pkConstraints);
                }
                if (ds.Tables.Contains(_sr.ForeignKeysCollectionName))
                {
                    var converter = new SchemaConstraintConverter(ds.Tables[_sr.ForeignKeysCollectionName], ConstraintType.ForeignKey);
                    table.AddConstraints(converter.Constraints());
                }
                if (ds.Tables.Contains(_sr.ForeignKeyColumnsCollectionName))
                {
                    var fkConverter = new ForeignKeyColumnConverter(ds.Tables[_sr.ForeignKeyColumnsCollectionName]);
                    fkConverter.AddForeignKeyColumns(table.ForeignKeys);
                }

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

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

                if (ds.Tables.Contains(_sr.IdentityColumnsCollectionName))
                {
                    SchemaConstraintConverter.AddIdentity(ds.Tables[_sr.IdentityColumnsCollectionName], table);
                }
            }

            if (DatabaseSchema.DataTypes.Count > 0)
            {
                DatabaseSchemaFixer.UpdateDataTypes(DatabaseSchema);
            }
            _sr.PostProcessing(DatabaseSchema);

            return(table);
        }
コード例 #4
0
        /// <summary>
        /// Gets all tables (plus constraints, indexes and triggers).
        /// </summary>
        public IList <DatabaseTable> AllTables()
        {
            DataTable tabs = _sr.Tables();
            //get full datatables for all tables, to minimize database calls

            //we either use the converters directly (DataTable to our db model)
            //or loaders, which wrap the schema loader calls and converters
            //loaders hide the switch between calling for all tables, or a specific table
            var columnLoader     = new ColumnLoader(_sr);
            var constraintLoader = new SchemaConstraintLoader(_sr);
            var indexLoader      = new IndexLoader(_sr);

            DataTable ids = _sr.IdentityColumns(null);

            DataTable triggers         = _sr.Triggers(null);
            var       triggerConverter = new TriggerConverter(triggers);

            var tables = SchemaConverter.Tables(tabs);

            tables.Sort(delegate(DatabaseTable t1, DatabaseTable t2)
            {
                //doesn't account for mixed schemas
                return(string.Compare(t1.Name, t2.Name, StringComparison.OrdinalIgnoreCase));
            });


            foreach (DatabaseTable table in tables)
            {
                var tableName       = table.Name;
                var databaseColumns = columnLoader.Load(tableName);
                table.Columns.AddRange(databaseColumns);

                var pkConstraints = constraintLoader.Load(tableName, ConstraintType.PrimaryKey);
                PrimaryKeyLogic.AddPrimaryKey(table, pkConstraints);

                var fks = constraintLoader.Load(tableName, ConstraintType.ForeignKey);
                table.AddConstraints(fks);

                table.AddConstraints(constraintLoader.Load(tableName, ConstraintType.UniqueKey));
                table.AddConstraints(constraintLoader.Load(tableName, ConstraintType.Check));

                indexLoader.AddIndexes(table);

                SchemaConstraintConverter.AddIdentity(ids, table);
                table.Triggers.Clear();
                table.Triggers.AddRange(triggerConverter.Triggers(tableName));
                _sr.PostProcessing(table);
            }
            DatabaseSchema.Tables.Clear();
            DatabaseSchema.Tables.AddRange(tables);
            UpdateReferences();

            if (DatabaseSchema.DataTypes.Count > 0)
            {
                DatabaseSchemaFixer.UpdateDataTypes(DatabaseSchema);
            }

            _sr.PostProcessing(DatabaseSchema);

            return(tables);
        }
コード例 #5
0
        public IList <DatabaseTable> Load(CancellationToken ct)
        {
            if (ct.IsCancellationRequested)
            {
                return(EmptyList());
            }
            RaiseReadingProgress(SchemaObjectType.Tables);

            //get full datatables for all tables, to minimize database calls
            var tabs = _schemaReader.Tables();

            //we either use the converters directly (DataTable to our db model)
            //or loaders, which wrap the schema loader calls and converters
            //loaders hide the switch between calling for all tables, or a specific table
            if (ct.IsCancellationRequested)
            {
                return(EmptyList());
            }
            RaiseReadingProgress(SchemaObjectType.Columns);
            var columnLoader = new ColumnLoader(_schemaReader);

            if (ct.IsCancellationRequested)
            {
                return(EmptyList());
            }
            RaiseReadingProgress(SchemaObjectType.Constraints);
            var constraintLoader = new SchemaConstraintLoader(_schemaReader);
            var indexLoader      = new IndexLoader(_schemaReader);

            var ids       = _schemaReader.IdentityColumns(null);
            var computeds = _schemaReader.ComputedColumns(null);

            if (ct.IsCancellationRequested)
            {
                return(EmptyList());
            }
            RaiseReadingProgress(SchemaObjectType.Descriptions);
            var tableDescriptions  = new TableDescriptionConverter(_schemaReader.TableDescription(null));
            var columnDescriptions = new ColumnDescriptionConverter(_schemaReader.ColumnDescription(null));

            DataTable triggers         = _schemaReader.Triggers(null);
            var       triggerConverter = new TriggerConverter(triggers);

            if (ct.IsCancellationRequested)
            {
                return(EmptyList());
            }
            ReaderEventArgs.RaiseEvent(ReaderProgress, this, ProgressType.Processing, SchemaObjectType.Tables);
            var tables      = SchemaConverter.Tables(tabs);
            var tableFilter = _exclusions.TableFilter;

            if (tableFilter != null)
            {
                tables.RemoveAll(t => tableFilter.Exclude(t.Name));
            }
            tables.Sort(delegate(DatabaseTable t1, DatabaseTable t2)
            {
                //doesn't account for mixed schemas
                return(string.Compare(t1.Name, t2.Name, StringComparison.OrdinalIgnoreCase));
            });

            int tablesCount = tables.Count;

            for (var i = 0; i < tablesCount; i++)
            {
                var table      = tables[i];
                var tableName  = table.Name;
                var schemaName = table.SchemaOwner;

                if (ct.IsCancellationRequested)
                {
                    return(tables);
                }
                ReaderEventArgs.RaiseEvent(ReaderProgress, this, ProgressType.Processing, SchemaObjectType.Tables, tableName, i, tablesCount);
                table.Description = tableDescriptions.FindDescription(table.SchemaOwner, tableName);

                var databaseColumns = columnLoader.Load(tableName, schemaName);
                table.Columns.AddRange(databaseColumns);

                columnDescriptions.AddDescriptions(table);

                var pkConstraints = constraintLoader.Load(tableName, schemaName, ConstraintType.PrimaryKey);
                PrimaryKeyLogic.AddPrimaryKey(table, pkConstraints);

                var fks = constraintLoader.Load(tableName, schemaName, ConstraintType.ForeignKey);
                table.AddConstraints(fks);

                table.AddConstraints(constraintLoader.Load(tableName, schemaName, ConstraintType.UniqueKey));
                table.AddConstraints(constraintLoader.Load(tableName, schemaName, ConstraintType.Check));
                table.AddConstraints(constraintLoader.Load(tableName, schemaName, ConstraintType.Default));

                indexLoader.AddIndexes(table);

                SchemaConstraintConverter.AddIdentity(ids, table);
                SchemaConstraintConverter.AddComputed(computeds, table);

                table.Triggers.Clear();
                table.Triggers.AddRange(triggerConverter.Triggers(tableName));
                _schemaReader.PostProcessing(table);
            }
            return(tables);
        }