/// <summary>
        ///     This API supports the Entity Framework Core infrastructure and is not intended to be used
        ///     directly from your code. This API may change or be removed in future releases.
        /// </summary>
        public virtual DatabaseModel Create(string connectionString, TableSelectionSet tableSelectionSet)
        {
            Check.NotEmpty(connectionString, nameof(connectionString));
            Check.NotNull(tableSelectionSet, nameof(tableSelectionSet));

            ResetState();

            using (_connection = new SqliteConnection(connectionString))
            {
                _connection.Open();
                _tableSelectionSet = tableSelectionSet;

                string databaseName = null;
                try
                {
                    databaseName = Path.GetFileNameWithoutExtension(_connection.DataSource);
                }
                catch (ArgumentException)
                {
                    // graceful fallback
                }

                _databaseModel.DatabaseName = !string.IsNullOrEmpty(databaseName)
                    ? databaseName
                    : _connection.DataSource;

                GetTables();
                GetColumns();
                GetIndexes();
                GetForeignKeys();
                return _databaseModel;
            }
        }
        public DatabaseModel Create(DbConnection connection, TableSelectionSet tableSelectionSet)
        {
            ResetState();

            _connection = (MySqlConnection)connection;

            var connectionStartedOpen = _connection.State == ConnectionState.Open;

            if (!connectionStartedOpen)
            {
                _connection.Open();
            }

            try
            {
                _tableSelectionSet = tableSelectionSet;

                _databaseModel.DatabaseName  = _connection.Database;
                _databaseModel.DefaultSchema = null;

                GetTables();
                GetColumns();
                GetPrimaryKeys();
                GetIndexes();
                GetConstraints();
                return(_databaseModel);
            }
            finally
            {
                if (!connectionStartedOpen)
                {
                    _connection.Close();
                }
            }
        }
Beispiel #3
0
        public static bool Allows(this TableSelectionSet _tableSelectionSet, /* [NotNull] */ string schemaName, /* [NotNull] */ string tableName)
        {
            if (_tableSelectionSet == null ||
                (_tableSelectionSet.Schemas.Count == 0 &&
                 _tableSelectionSet.Tables.Count == 0))
            {
                return(true);
            }

            var result = false;

            foreach (var schemaSelection in _tableSelectionSet.Schemas)
            {
                if (EqualsWithQuotes(schemaSelection.Text, schemaName))
                {
                    schemaSelection.IsMatched = true;
                    result = true;
                }
            }

            foreach (var tableSelection in _tableSelectionSet.Tables)
            {
                var components = tableSelection.Text.Split('.');
                if (components.Length == 1
                    ? EqualsWithQuotes(components[0], tableName)
                    : EqualsWithQuotes(components[0], schemaName) && EqualsWithQuotes(components[1], tableName))
                {
                    tableSelection.IsMatched = true;
                    result = true;
                }
            }

            return(result);
        }
        public override IModel Create(string connectionString, TableSelectionSet tableSelectionSet)
        {
            var model = base.Create(connectionString, tableSelectionSet);

            model.Scaffolding().UseProviderMethodName = nameof(MySQLDbContextOptionsExtensions.UseMySQL);
            return(model);
        }
        public virtual Task <ReverseEngineerFiles> ReverseEngineerAsync(
            [NotNull] string provider,
            [NotNull] string connectionString,
            [CanBeNull] string outputDir,
            [CanBeNull] string dbContextClassName,
            [CanBeNull] List <string> schemas,
            [CanBeNull] List <string> tables,
            bool useDataAnnotations,
            CancellationToken cancellationToken = default(CancellationToken))
        {
            Check.NotEmpty(provider, nameof(provider));
            Check.NotEmpty(connectionString, nameof(connectionString));

            var services = _servicesBuilder.Build(provider);

            var loggerFactory = services.GetRequiredService <ILoggerFactory>();

            loggerFactory.AddProvider(_loggerProvider);

            var generator         = services.GetRequiredService <ReverseEngineeringGenerator>();
            var tableSelectionSet = new TableSelectionSet(tables, schemas);
            var configuration     = new ReverseEngineeringConfiguration
            {
                ConnectionString     = connectionString,
                ContextClassName     = dbContextClassName,
                ProjectPath          = _projectDir,
                ProjectRootNamespace = _rootNamespace,
                OutputPath           = outputDir,
                TableSelectionSet    = tableSelectionSet,
                UseFluentApiOnly     = !useDataAnnotations
            };

            return(generator.GenerateAsync(configuration, cancellationToken));
        }
        public DatabaseModel CreateModel(string createSql, TableSelectionSet selection = null, ILogger logger = null)
        {
            TestStore.ExecuteNonQuery("DROP SCHEMA public CASCADE; CREATE SCHEMA public; " + createSql);

            return(new NpgsqlDatabaseModelFactory(new TestLoggerFactory(logger))
                   .Create(TestStore.ConnectionString, selection ?? TableSelectionSet.All));
        }
Beispiel #7
0
        public void Allows_updates_IsMatched_for_matching_table_selections_which_specify_schema()
        {
            var tableNames = new List <string>
            {
                "schema0.table0", "[schema0].[table0]", "[schema0].table0", "schema0.[table0]",
                "schema0.table1", "[schema0].[table1]", "[schema0].table1", "schema0.[table1]",
                "schema1.table0", "[schema1].[table0]", "[schema1].table0", "schema1.[table0]"
            };
            var tableSelectionSet = new TableSelectionSet(tableNames);

            Assert.Equal(0, tableSelectionSet.Schemas.Count);
            Assert.Equal(12, tableSelectionSet.Tables.Count);

            foreach (var table in tableSelectionSet.Tables)
            {
                Assert.False(table.IsMatched);
            }

            Assert.True(tableSelectionSet.Allows("schema0", "table0"));
            foreach (var table in tableSelectionSet.Tables.Take(4))
            {
                Assert.True(table.IsMatched);
            }
            foreach (var table in tableSelectionSet.Tables.Skip(4))
            {
                Assert.False(table.IsMatched);
            }
        }
        /// <summary>
        ///     This API supports the Entity Framework Core infrastructure and is not intended to be used
        ///     directly from your code. This API may change or be removed in future releases.
        /// </summary>
        public virtual DatabaseModel Create(string connectionString, TableSelectionSet tableSelectionSet)
        {
            Check.NotEmpty(connectionString, nameof(connectionString));
            Check.NotNull(tableSelectionSet, nameof(tableSelectionSet));

            ResetState();

            using (_connection = new SqlConnection(connectionString))
            {
                _connection.Open();
                _tableSelectionSet = tableSelectionSet;

                _databaseModel.DatabaseName = _connection.Database;

                Version.TryParse(_connection.ServerVersion, out _serverVersion);
                if (SupportsSequences)
                {
                    GetSequences();
                }

                GetDefaultSchema();
                GetTypeAliases();
                GetTables();
                GetColumns();
                GetIndexes();
                GetForeignKeys();
                return _databaseModel;
            }
        }
Beispiel #9
0
        public DatabaseModel CreateModel(string createSql, TableSelectionSet selection = null, ILogger logger = null)
        {
            TestStore.ExecuteNonQuery(createSql);

            return(new SqlServerDatabaseModelFactory(new TestLoggerFactory(logger))
                   .Create(TestStore.ConnectionString, selection ?? TableSelectionSet.All));
        }
        public void Inserting_an_AnySchema_selection_removes_previous_selections_with_that_table()
        {
            var includeA1 = new TableSelection()
            {
                Schema = "A", Table = "First"
            };
            var includeA2 = new TableSelection()
            {
                Schema = "A", Table = "Second"
            };
            var includeB1 = new TableSelection()
            {
                Schema = "B", Table = "First"
            };
            var includeB2 = new TableSelection()
            {
                Schema = "B", Table = "Second"
            };

            var excludeA1 = new TableSelection()
            {
                Schema = "A", Table = "First", Exclude = true
            };
            var excludeA2 = new TableSelection()
            {
                Schema = "A", Table = "Second", Exclude = true
            };
            var excludeB1 = new TableSelection()
            {
                Schema = "B", Table = "First", Exclude = true
            };
            var excludeB2 = new TableSelection()
            {
                Schema = "B", Table = "Second", Exclude = true
            };

            var includeTableFirstAnySchema = new TableSelection()
            {
                Schema = TableSelection.Any, Table = "First"
            };
            var excludeTableSecondAnySchema = new TableSelection()
            {
                Schema = TableSelection.Any, Table = "Second", Exclude = true
            };

            var tableSelectionSet = new TableSelectionSet();

            tableSelectionSet.AddSelections(new [] { includeA1, includeA2, includeB1, includeB2 });
            tableSelectionSet.AddSelections(new [] { excludeA1, excludeA2, excludeB1, excludeB2 });
            tableSelectionSet.AddSelections(new [] { includeTableFirstAnySchema });
            tableSelectionSet.AddSelections(new [] { excludeTableSecondAnySchema });

            Assert.Equal(new List <TableSelection> {
                includeA2, includeB2, includeTableFirstAnySchema
            }, tableSelectionSet.InclusiveSelections);
            Assert.Equal(new List <TableSelection> {
                excludeA1, excludeB1, excludeTableSecondAnySchema
            }, tableSelectionSet.ExclusiveSelections);
        }
        public void If_an_AnyTable_selection_exists_adding_a_new_specific_selection_which_matches_does_not_change_selections()
        {
            var includeA1 = new TableSelection()
            {
                Schema = "A", Table = "First"
            };
            var includeA2 = new TableSelection()
            {
                Schema = "A", Table = "Second"
            };
            var includeB1 = new TableSelection()
            {
                Schema = "B", Table = "First"
            };
            var includeB2 = new TableSelection()
            {
                Schema = "B", Table = "Second"
            };

            var excludeA1 = new TableSelection()
            {
                Schema = "A", Table = "First", Exclude = true
            };
            var excludeA2 = new TableSelection()
            {
                Schema = "A", Table = "Second", Exclude = true
            };
            var excludeB1 = new TableSelection()
            {
                Schema = "B", Table = "First", Exclude = true
            };
            var excludeB2 = new TableSelection()
            {
                Schema = "B", Table = "Second", Exclude = true
            };

            var includeSchemaAAnyTable = new TableSelection()
            {
                Schema = "A", Table = TableSelection.Any
            };
            var excludeSchemaBAnyTable = new TableSelection()
            {
                Schema = "B", Table = TableSelection.Any, Exclude = true
            };

            var tableSelectionSet = new TableSelectionSet();

            tableSelectionSet.AddSelections(new[] { includeSchemaAAnyTable });
            tableSelectionSet.AddSelections(new[] { excludeSchemaBAnyTable });
            tableSelectionSet.AddSelections(new[] { includeA1, includeA2, includeB1, includeB2 });
            tableSelectionSet.AddSelections(new[] { excludeA1, excludeA2, excludeB1, excludeB2 });

            Assert.Equal(new List <TableSelection> {
                includeSchemaAAnyTable, includeB1, includeB2
            }, tableSelectionSet.InclusiveSelections);
            Assert.Equal(new List <TableSelection> {
                excludeSchemaBAnyTable, excludeA1, excludeA2
            }, tableSelectionSet.ExclusiveSelections);
        }
Beispiel #12
0
 private void ResetState()
 {
     _connection        = null;
     _tableSelectionSet = null;
     _databaseModel     = new DatabaseModel();
     _tables            = new Dictionary <string, TableModel>(StringComparer.OrdinalIgnoreCase);
     _tableColumns      = new Dictionary <string, ColumnModel>(StringComparer.OrdinalIgnoreCase);
 }
        public DatabaseModel CreateModel(string createSql, TableSelectionSet selection = null)
        {
            _testStore.ExecuteNonQuery(createSql);

            var reader = new SqlServerDatabaseModelFactory();

            return(reader.Create(_testStore.Connection.ConnectionString, selection ?? TableSelectionSet.All));
        }
 private void ResetState()
 {
     _connection = null;
     _tableSelectionSet = null;
     _databaseModel = new DatabaseModel();
     _tables = new Dictionary<string, TableModel>(StringComparer.OrdinalIgnoreCase);
     _tableColumns = new Dictionary<string, ColumnModel>(StringComparer.OrdinalIgnoreCase);
 }
Beispiel #15
0
        public DatabaseModel CreateModel(string createSql, TableSelectionSet selection = null, ILogger logger = null)
        {
            TestStore.ExecuteNonQuery(createSql);

            return(new SqlServerDatabaseModelFactory(
                       new InterceptingLogger <LoggerCategory.Scaffolding>(
                           new TestLoggerFactory(logger),
                           new LoggingOptions()))
                   .Create(TestStore.ConnectionString, selection ?? TableSelectionSet.All));
        }
        public void ExcludeAll_disallows_all_selections()
        {
            var tableSelectionSet = new TableSelectionSet();

            tableSelectionSet.AddSelections(TableSelection.ExclusiveAll);

            Assert.False(tableSelectionSet.Allows("schema1", "table1"));
            Assert.False(tableSelectionSet.Allows("schema2", "table2"));
            Assert.False(tableSelectionSet.Allows("schema3", "table3"));
            Assert.False(tableSelectionSet.Allows("schemaN", "tableN"));
        }
        public DatabaseModel CreateModel(List <string> createSql, TableSelectionSet selection = null)
        {
            foreach (var sql in createSql)
            {
                _testStore.ExecuteNonQuery(sql);
            }

            var reader = new SqlCeDatabaseModelFactory(new LoggerFactory());

            return(reader.Create(_testStore.Connection.ConnectionString, selection ?? TableSelectionSet.All));
        }
        public DatabaseModel CreateModel(string sql, TableSelectionSet selection, ILogger logger = null, bool executeScript = false)
        {
            if (executeScript)
            {
                MySQLTestStore.ExecuteScript(sql);
            }
            else
            {
                MySQLTestStore.Execute(sql);
            }

            return(new MySQLDatabaseModelFactory(new MyTestLoggerFactory(logger).CreateLogger <MySQLDatabaseModelFactory>()).
                   Create(MySQLTestStore.rootConnectionString + ";database=" + dbName + ";", selection ?? TableSelectionSet.All));
        }
        public void Inserting_an_AnySchemaAnyTable_selection_removes_all_previous_selections()
        {
            var includeA1 = new TableSelection()
            {
                Schema = "A", Table = "First"
            };
            var includeA2 = new TableSelection()
            {
                Schema = "A", Table = "Second"
            };
            var includeB1 = new TableSelection()
            {
                Schema = "B", Table = "First"
            };
            var includeB2 = new TableSelection()
            {
                Schema = "B", Table = "Second"
            };

            var excludeA1 = new TableSelection()
            {
                Schema = "A", Table = "First", Exclude = true
            };
            var excludeA2 = new TableSelection()
            {
                Schema = "A", Table = "Second", Exclude = true
            };
            var excludeB1 = new TableSelection()
            {
                Schema = "B", Table = "First", Exclude = true
            };
            var excludeB2 = new TableSelection()
            {
                Schema = "B", Table = "Second", Exclude = true
            };

            var tableSelectionSet = new TableSelectionSet();

            tableSelectionSet.AddSelections(new [] { includeA1, includeA2, includeB1, includeB2 });
            tableSelectionSet.AddSelections(new [] { excludeA1, excludeA2, excludeB1, excludeB2 });
            tableSelectionSet.AddSelections(new [] { TableSelection.InclusiveAll });
            tableSelectionSet.AddSelections(new [] { TableSelection.ExclusiveAll });

            Assert.Equal(new List <TableSelection> {
                TableSelection.InclusiveAll
            }, tableSelectionSet.InclusiveSelections);
            Assert.Equal(new List <TableSelection> {
                TableSelection.ExclusiveAll
            }, tableSelectionSet.ExclusiveSelections);
        }
        public void It_filters_tables()
        {
            var sql = @"CREATE TABLE [K2] ( Id int);
CREATE TABLE [Kilimanjaro] ( Id int);";

            var selectionSet = new TableSelectionSet(new List <string> {
                "K2"
            });

            var dbModel = CreateModel(sql, selectionSet);
            var table   = Assert.Single(dbModel.Tables);

            Assert.Equal("K2", table.Name);
        }
        public void ExcludeAnySchema_disallows_all_selections_with_that_table()
        {
            var tableSelectionSet = new TableSelectionSet();

            tableSelectionSet.AddSelections(new TableSelection()
            {
                Schema = TableSelection.Any, Table = "SpecificTable", Exclude = true
            });

            Assert.False(tableSelectionSet.Allows("schema1", "SpecificTable"));
            Assert.True(tableSelectionSet.Allows("schema2", "OtherTable"));
            Assert.False(tableSelectionSet.Allows("schema3", "SpecificTable"));
            Assert.True(tableSelectionSet.Allows("schemaN", "OtherTable"));
        }
        public void ExcludeAnyTable_disallows_all_selections_with_that_schema()
        {
            var tableSelectionSet = new TableSelectionSet();

            tableSelectionSet.AddSelections(new TableSelection()
            {
                Schema = "SpecificSchema", Table = TableSelection.Any, Exclude = true
            });

            Assert.False(tableSelectionSet.Allows("SpecificSchema", "table1"));
            Assert.True(tableSelectionSet.Allows("OtherSchema", "table2"));
            Assert.False(tableSelectionSet.Allows("SpecificSchema", "table3"));
            Assert.True(tableSelectionSet.Allows("YetAnotherSchema", "table4"));
        }
        public virtual IModel GenerateMetadataModel(
            [NotNull] string connectionString, [CanBeNull] TableSelectionSet tableSelectionSet)
        {
            Check.NotEmpty(connectionString, nameof(connectionString));

            _tableSelectionSet = tableSelectionSet ?? TableSelectionSet.InclusiveAll;

            var relationalModel = ConstructRelationalModel(connectionString);

            var nameMapper = GetNameMapper(relationalModel);

            var codeGenModel = ConstructCodeGenModel(relationalModel, nameMapper);

            return(codeGenModel);
        }
        public void It_filters_views()
        {
            _fixture.ExecuteNonQuery(@"CREATE TABLE [dbo].[Hills] (Id int PRIMARY KEY, Name nvarchar(100), Height int);");
            _fixture.ExecuteNonQuery(@"CREATE VIEW [dbo].[ShortHills] WITH SCHEMABINDING AS SELECT Name FROM [dbo].[Hills] WHERE Height < 100;");
            var sql = "CREATE UNIQUE CLUSTERED INDEX IX_ShortHills_Names ON ShortHills (Name);";

            var selectionSet = new TableSelectionSet(new List <string> {
                "Hills", "ShortHills"
            });
            var logger = new SqlServerDatabaseModelFixture.TestLogger();

            var dbModel = CreateModel(sql, selectionSet, logger);

            Assert.Single(dbModel.Tables);
            Assert.DoesNotContain(logger.Items, i => i.logLevel == LogLevel.Warning);
        }
Beispiel #25
0
        public void It_filters_tables()
        {
            var sql = @"CREATE TABLE public.k2 (id int, a varchar, UNIQUE (a));" +
                      @"CREATE TABLE public.kilimanjaro (id int, b varchar, UNIQUE (b), FOREIGN KEY (b) REFERENCES k2 (a));";

            var selectionSet = new TableSelectionSet(new List <string> {
                "k2"
            });

            var dbModel = CreateModel(sql, selectionSet);
            var table   = Assert.Single(dbModel.Tables);

            Assert.Equal("k2", table.Name);
            Assert.Equal(2, table.Columns.Count);
            Assert.Equal(1, table.Indexes.Count);
            Assert.Empty(table.ForeignKeys);
        }
        public void IncludeAll_allows_all_selections()
        {
            var tableSelectionSet = new TableSelectionSet();

            Assert.True(tableSelectionSet.Allows("schema1", "table1"));
            Assert.True(tableSelectionSet.Allows("schema2", "table2"));
            Assert.True(tableSelectionSet.Allows("schema3", "table3"));
            Assert.True(tableSelectionSet.Allows("schemaN", "tableN"));

            // this has the same effect as above
            tableSelectionSet.AddSelections(TableSelection.InclusiveAll);

            Assert.True(tableSelectionSet.Allows("schema1", "table1"));
            Assert.True(tableSelectionSet.Allows("schema2", "table2"));
            Assert.True(tableSelectionSet.Allows("schema3", "table3"));
            Assert.True(tableSelectionSet.Allows("schemaN", "tableN"));
        }
Beispiel #27
0
        public DatabaseModel Create(string connectionString, TableSelectionSet tableSelectionSet)
        {
            if (String.IsNullOrEmpty(connectionString))
            {
                new ArgumentException("Argument is empty", "connectionString");
            }

            if (tableSelectionSet == null)
            {
                new ArgumentNullException("tableSelectionSet");
            }

            using (var connection = new MySqlConnection(connectionString))
            {
                return(Create(connection, tableSelectionSet));
            }
        }
        public void It_correctly_assigns_excludes_and_includes()
        {
            var includeA = new TableSelection() { Schema = "A", Table = "A" };
            var includeB = new TableSelection() { Schema = "B", Table = "B" };
            var includeC = new TableSelection() { Schema = "C", Table = "C" };

            var excludeZ = new TableSelection() { Schema = "Z", Table = "Z", Exclude = true };
            var excludeY = new TableSelection() { Schema = "Y", Table = "Y", Exclude = true };
            var excludeX = new TableSelection() { Schema = "X", Table = "X", Exclude = true };

            var tableSelectionSet = new TableSelectionSet();
            tableSelectionSet.AddSelections(new [] { excludeZ, includeB, excludeX });
            tableSelectionSet.AddSelections(new [] { includeA, excludeY, includeC });

            Assert.Equal(new List<TableSelection> { includeB, includeA, includeC }, tableSelectionSet.InclusiveSelections);
            Assert.Equal(new List<TableSelection> { excludeZ, excludeX, excludeY }, tableSelectionSet.ExclusiveSelections);
        }
        /// <summary>
        ///     This API supports the Entity Framework Core infrastructure and is not intended to be used
        ///     directly from your code. This API may change or be removed in future releases.
        /// </summary>
        public override IModel Create(string connectionString, TableSelectionSet tableSelectionSet)
        {
            if (tableSelectionSet != null
                && tableSelectionSet.Schemas.Any())
            {
                Logger.LogWarning(SqliteDesignStrings.UsingSchemaSelectionsWarning);

                // we've logged a general warning above that sqlite ignores all
                // schema selections so mark all of them as matched so that we don't
                // also log warnings about not matching each individual selection
                tableSelectionSet.Schemas.ToList().ForEach(s => s.IsMatched = true);
            }

            var model = base.Create(connectionString, tableSelectionSet);
            model.Scaffolding().UseProviderMethodName = nameof(SqliteDbContextOptionsBuilderExtensions.UseSqlite);
            return model;
        }
        public void It_filters_tables()
        {
            var sql = @"CREATE TABLE [dbo].[K2] ( Id int, A varchar, UNIQUE (A ) );
CREATE TABLE [dbo].[Kilimanjaro] ( Id int, B varchar, UNIQUE (B), FOREIGN KEY (B) REFERENCES K2 (A) );";

            var selectionSet = new TableSelectionSet(new List <string> {
                "K2"
            });

            var dbModel = CreateModel(sql, selectionSet);
            var table   = Assert.Single(dbModel.Tables);

            Assert.Equal("K2", table.Name);
            Assert.Equal(2, table.Columns.Count);
            Assert.Equal(1, table.Indexes.Count);
            Assert.Empty(table.ForeignKeys);
        }
Beispiel #31
0
        public DatabaseModel Create(DbConnection connection, IEnumerable <string> tables, IEnumerable <string> schemas)
        {
            ThrowIf.Argument.IsNull(connection, nameof(connection));
            ThrowIf.Argument.IsNull(tables, nameof(tables));
            ThrowIf.Argument.IsNull(schemas, nameof(schemas));

            ResetState();

            _connection = (MySqlConnection)connection;

            var connectionStartedOpen = _connection.State == ConnectionState.Open;

            if (!connectionStartedOpen)
            {
                _connection.Open();
            }

            try
            {
                _tableSelectionSet = new TableSelectionSet(tables, schemas);
                if (schemas.Count() == 0)
                {
                    _schemaList = $"'{_connection.Database}'";
                }
                else
                {
                    _schemaList = $"'{schemas.Join("', '")}'";
                }

                _databaseModel.DatabaseName = _connection.Database;
                GetTables();
                GetColumns();
                GetIndexes();
                GetForeignKeys();
                return(_databaseModel);
            }
            finally
            {
                if (!connectionStartedOpen)
                {
                    _connection.Close();
                }
            }
        }
        public void Re_inserting_a_specific_selection_does_not_change_selections()
        {
            var includeA = new TableSelection()
            {
                Schema = "A", Table = "A"
            };
            var includeB = new TableSelection()
            {
                Schema = "B", Table = "B"
            };
            var includeC = new TableSelection()
            {
                Schema = "C", Table = "C"
            };

            var excludeZ = new TableSelection()
            {
                Schema = "Z", Table = "Z", Exclude = true
            };
            var excludeY = new TableSelection()
            {
                Schema = "Y", Table = "Y", Exclude = true
            };
            var excludeX = new TableSelection()
            {
                Schema = "X", Table = "X", Exclude = true
            };

            var tableSelectionSet = new TableSelectionSet();

            tableSelectionSet.AddSelections(new[] { includeA, includeB, includeC });
            tableSelectionSet.AddSelections(new[] { excludeX, excludeY, excludeZ });

            // re-insert same selections
            tableSelectionSet.AddSelections(new[] { includeA });
            tableSelectionSet.AddSelections(new[] { excludeZ });

            Assert.Equal(new List <TableSelection> {
                includeA, includeB, includeC
            }, tableSelectionSet.InclusiveSelections);
            Assert.Equal(new List <TableSelection> {
                excludeX, excludeY, excludeZ
            }, tableSelectionSet.ExclusiveSelections);
        }
        public static bool Allows(this TableSelectionSet _tableSelectionSet, string schemaName, string tableName)
        {
            if (_tableSelectionSet == null ||
                (_tableSelectionSet.Schemas.Count == 0 &&
                 _tableSelectionSet.Tables.Count == 0))
            {
                return(true);
            }

            var result = false;

            if (_tableSelectionSet.Schemas.Count == 0)
            {
                result = true;
            }

            foreach (var schemaSelection in _tableSelectionSet.Schemas)
            {
                if (schemaSelection.Text.Equals(schemaName, StringComparison.OrdinalIgnoreCase))
                {
                    schemaSelection.IsMatched = true;
                    result = true;
                }
            }

            if (_tableSelectionSet.Tables.Count > 0 && result)
            {
                result = false;
                foreach (var tableSelection in _tableSelectionSet.Tables)
                {
                    var components = tableSelection.Text.Split('.');
                    if (components.Length == 1
              ? components[0].Equals(tableName, StringComparison.OrdinalIgnoreCase)
              : components[0].Equals(schemaName, StringComparison.OrdinalIgnoreCase) && components[1].Equals(tableName, StringComparison.OrdinalIgnoreCase))
                    {
                        tableSelection.IsMatched = true;
                        result = true;
                    }
                }
            }

            return(result);
        }
        public void Exclude_specific_tables_disallows_only_those_specific_selections()
        {
            var exclude1 = new TableSelection()
            {
                Schema = "schema1", Table = "table1", Exclude = true
            };
            var exclude2 = new TableSelection()
            {
                Schema = "schema2", Table = "table2", Exclude = true
            };

            var tableSelectionSet = new TableSelectionSet();

            tableSelectionSet.AddSelections(new[] { exclude1, exclude2 });

            Assert.False(tableSelectionSet.Allows("schema1", "table1"));
            Assert.False(tableSelectionSet.Allows("schema2", "table2"));
            Assert.True(tableSelectionSet.Allows("schema1", "table2"));
            Assert.True(tableSelectionSet.Allows("schema2", "table1"));
        }
        public void It_correctly_assigns_excludes_and_includes()
        {
            var includeA = new TableSelection()
            {
                Schema = "A", Table = "A"
            };
            var includeB = new TableSelection()
            {
                Schema = "B", Table = "B"
            };
            var includeC = new TableSelection()
            {
                Schema = "C", Table = "C"
            };

            var excludeZ = new TableSelection()
            {
                Schema = "Z", Table = "Z", Exclude = true
            };
            var excludeY = new TableSelection()
            {
                Schema = "Y", Table = "Y", Exclude = true
            };
            var excludeX = new TableSelection()
            {
                Schema = "X", Table = "X", Exclude = true
            };

            var tableSelectionSet = new TableSelectionSet();

            tableSelectionSet.AddSelections(new [] { excludeZ, includeB, excludeX });
            tableSelectionSet.AddSelections(new [] { includeA, excludeY, includeC });

            Assert.Equal(new List <TableSelection> {
                includeB, includeA, includeC
            }, tableSelectionSet.InclusiveSelections);
            Assert.Equal(new List <TableSelection> {
                excludeZ, excludeX, excludeY
            }, tableSelectionSet.ExclusiveSelections);
        }
        public void Inserting_an_AnyTable_selection_removes_previous_selections_with_that_schema()
        {
            var includeA1 = new TableSelection() { Schema = "A", Table = "First" };
            var includeA2 = new TableSelection() { Schema = "A", Table = "Second" };
            var includeB1 = new TableSelection() { Schema = "B", Table = "First" };
            var includeB2 = new TableSelection() { Schema = "B", Table = "Second" };

            var excludeA1 = new TableSelection() { Schema = "A", Table = "First", Exclude = true };
            var excludeA2 = new TableSelection() { Schema = "A", Table = "Second", Exclude = true };
            var excludeB1 = new TableSelection() { Schema = "B", Table = "First", Exclude = true };
            var excludeB2 = new TableSelection() { Schema = "B", Table = "Second", Exclude = true };

            var includeSchemaAAnyTable = new TableSelection() { Schema = "A", Table = TableSelection.Any };
            var excludeSchemaBAnyTable = new TableSelection() { Schema = "B", Table = TableSelection.Any, Exclude = true };

            var tableSelectionSet = new TableSelectionSet();
            tableSelectionSet.AddSelections(new [] { includeA1, includeA2, includeB1, includeB2 });
            tableSelectionSet.AddSelections(new [] { excludeA1, excludeA2, excludeB1, excludeB2 });
            tableSelectionSet.AddSelections(new [] { includeSchemaAAnyTable });
            tableSelectionSet.AddSelections(new [] { excludeSchemaBAnyTable });

            Assert.Equal(new List<TableSelection> { includeB1, includeB2, includeSchemaAAnyTable }, tableSelectionSet.InclusiveSelections);
            Assert.Equal(new List<TableSelection> { excludeA1, excludeA2, excludeSchemaBAnyTable }, tableSelectionSet.ExclusiveSelections);
        }
        public void ExcludeAnyTable_disallows_all_selections_with_that_schema()
        {
            var tableSelectionSet = new TableSelectionSet();
            tableSelectionSet.AddSelections(new TableSelection() { Schema = "SpecificSchema", Table = TableSelection.Any, Exclude = true });

            Assert.False(tableSelectionSet.Allows("SpecificSchema", "table1"));
            Assert.True(tableSelectionSet.Allows("OtherSchema", "table2"));
            Assert.False(tableSelectionSet.Allows("SpecificSchema", "table3"));
            Assert.True(tableSelectionSet.Allows("YetAnotherSchema", "table4"));
        }
 /// <summary>
 ///     This API supports the Entity Framework Core infrastructure and is not intended to be used
 ///     directly from your code. This API may change or be removed in future releases.
 /// </summary>
 public override IModel Create(string connectionString, TableSelectionSet tableSelectionSet)
 {
     var model = base.Create(connectionString, tableSelectionSet);
     model.Scaffolding().UseProviderMethodName = nameof(SqlServerDbContextOptionsExtensions.UseSqlServer);
     return model;
 }
        public void Re_inserting_a_specific_selection_does_not_change_selections()
        {
            var includeA = new TableSelection() { Schema = "A", Table = "A" };
            var includeB = new TableSelection() { Schema = "B", Table = "B" };
            var includeC = new TableSelection() { Schema = "C", Table = "C" };

            var excludeZ = new TableSelection() { Schema = "Z", Table = "Z", Exclude = true };
            var excludeY = new TableSelection() { Schema = "Y", Table = "Y", Exclude = true };
            var excludeX = new TableSelection() { Schema = "X", Table = "X", Exclude = true };

            var tableSelectionSet = new TableSelectionSet();
            tableSelectionSet.AddSelections(new[] { includeA, includeB, includeC });
            tableSelectionSet.AddSelections(new[] { excludeX, excludeY, excludeZ });

            // re-insert same selections
            tableSelectionSet.AddSelections(new[] { includeA });
            tableSelectionSet.AddSelections(new[] { excludeZ });

            Assert.Equal(new List<TableSelection> { includeA, includeB, includeC }, tableSelectionSet.InclusiveSelections);
            Assert.Equal(new List<TableSelection> { excludeX, excludeY, excludeZ }, tableSelectionSet.ExclusiveSelections);
        }
        public void If_an_AnyTable_selection_exists_adding_a_new_specific_selection_which_matches_does_not_change_selections()
        {
            var includeA1 = new TableSelection() { Schema = "A", Table = "First" };
            var includeA2 = new TableSelection() { Schema = "A", Table = "Second" };
            var includeB1 = new TableSelection() { Schema = "B", Table = "First" };
            var includeB2 = new TableSelection() { Schema = "B", Table = "Second" };

            var excludeA1 = new TableSelection() { Schema = "A", Table = "First", Exclude = true };
            var excludeA2 = new TableSelection() { Schema = "A", Table = "Second", Exclude = true };
            var excludeB1 = new TableSelection() { Schema = "B", Table = "First", Exclude = true };
            var excludeB2 = new TableSelection() { Schema = "B", Table = "Second", Exclude = true };

            var includeSchemaAAnyTable = new TableSelection() { Schema = "A", Table = TableSelection.Any };
            var excludeSchemaBAnyTable = new TableSelection() { Schema = "B", Table = TableSelection.Any, Exclude = true };

            var tableSelectionSet = new TableSelectionSet();
            tableSelectionSet.AddSelections(new[] { includeSchemaAAnyTable });
            tableSelectionSet.AddSelections(new[] { excludeSchemaBAnyTable });
            tableSelectionSet.AddSelections(new[] { includeA1, includeA2, includeB1, includeB2 });
            tableSelectionSet.AddSelections(new[] { excludeA1, excludeA2, excludeB1, excludeB2 });

            Assert.Equal(new List<TableSelection> { includeSchemaAAnyTable, includeB1, includeB2 }, tableSelectionSet.InclusiveSelections);
            Assert.Equal(new List<TableSelection> { excludeSchemaBAnyTable, excludeA1, excludeA2 }, tableSelectionSet.ExclusiveSelections);
        }
        public void If_an_AnySchemaAnyTable_selection_exists_adding_any_new_selection_does_not_change_selections()
        {
            var includeA1 = new TableSelection() { Schema = "A", Table = "First" };
            var includeA2 = new TableSelection() { Schema = "A", Table = "Second" };
            var includeB1 = new TableSelection() { Schema = "B", Table = "First" };
            var includeB2 = new TableSelection() { Schema = "B", Table = "Second" };

            var excludeA1 = new TableSelection() { Schema = "A", Table = "First", Exclude = true };
            var excludeA2 = new TableSelection() { Schema = "A", Table = "Second", Exclude = true };
            var excludeB1 = new TableSelection() { Schema = "B", Table = "First", Exclude = true };
            var excludeB2 = new TableSelection() { Schema = "B", Table = "Second", Exclude = true };

            var includeSchemaAAnyTable = new TableSelection() { Schema = "A", Table = TableSelection.Any };
            var excludeSchemaBAnyTable = new TableSelection() { Schema = "B", Table = TableSelection.Any, Exclude = true };
            var includeTableFirstAnySchema = new TableSelection() { Schema = TableSelection.Any, Table = "First" };
            var excludeTableSecondAnySchema = new TableSelection() { Schema = TableSelection.Any, Table = "Second", Exclude = true };

            var tableSelectionSet = new TableSelectionSet();
            tableSelectionSet.AddSelections(new[] { TableSelection.InclusiveAll });
            tableSelectionSet.AddSelections(new[] { TableSelection.ExclusiveAll });
            tableSelectionSet.AddSelections(new[] { includeSchemaAAnyTable, excludeSchemaBAnyTable, includeTableFirstAnySchema, excludeTableSecondAnySchema });

            Assert.Equal(new List<TableSelection> { TableSelection.InclusiveAll }, tableSelectionSet.InclusiveSelections);
            Assert.Equal(new List<TableSelection> { TableSelection.ExclusiveAll }, tableSelectionSet.ExclusiveSelections);

            tableSelectionSet.AddSelections(new[] { includeA1, includeA2, includeB1, includeB2 });
            tableSelectionSet.AddSelections(new[] { excludeA1, excludeA2, excludeB1, excludeB2 });

            Assert.Equal(new List<TableSelection> { TableSelection.InclusiveAll }, tableSelectionSet.InclusiveSelections);
            Assert.Equal(new List<TableSelection> { TableSelection.ExclusiveAll }, tableSelectionSet.ExclusiveSelections);
        }
        public void IncludeAll_allows_all_selections()
        {
            var tableSelectionSet = new TableSelectionSet();

            Assert.True(tableSelectionSet.Allows("schema1", "table1"));
            Assert.True(tableSelectionSet.Allows("schema2", "table2"));
            Assert.True(tableSelectionSet.Allows("schema3", "table3"));
            Assert.True(tableSelectionSet.Allows("schemaN", "tableN"));

            // this has the same effect as above
            tableSelectionSet.AddSelections(TableSelection.InclusiveAll);

            Assert.True(tableSelectionSet.Allows("schema1", "table1"));
            Assert.True(tableSelectionSet.Allows("schema2", "table2"));
            Assert.True(tableSelectionSet.Allows("schema3", "table3"));
            Assert.True(tableSelectionSet.Allows("schemaN", "tableN"));
        }
        public void ExcludeAll_disallows_all_selections()
        {
            var tableSelectionSet = new TableSelectionSet();
            tableSelectionSet.AddSelections(TableSelection.ExclusiveAll);

            Assert.False(tableSelectionSet.Allows("schema1", "table1"));
            Assert.False(tableSelectionSet.Allows("schema2", "table2"));
            Assert.False(tableSelectionSet.Allows("schema3", "table3"));
            Assert.False(tableSelectionSet.Allows("schemaN", "tableN"));
        }
        public void ExcludeAnySchema_disallows_all_selections_with_that_table()
        {
            var tableSelectionSet = new TableSelectionSet();
            tableSelectionSet.AddSelections(new TableSelection() { Schema = TableSelection.Any, Table = "SpecificTable", Exclude = true });

            Assert.False(tableSelectionSet.Allows("schema1", "SpecificTable"));
            Assert.True(tableSelectionSet.Allows("schema2", "OtherTable"));
            Assert.False(tableSelectionSet.Allows("schema3", "SpecificTable"));
            Assert.True(tableSelectionSet.Allows("schemaN", "OtherTable"));
        }
        public void Exclude_specific_tables_disallows_only_those_specific_selections()
        {
            var exclude1 = new TableSelection() { Schema = "schema1", Table = "table1", Exclude = true };
            var exclude2 = new TableSelection() { Schema = "schema2", Table = "table2", Exclude = true };

            var tableSelectionSet = new TableSelectionSet();
            tableSelectionSet.AddSelections(new[] { exclude1, exclude2 });

            Assert.False(tableSelectionSet.Allows("schema1", "table1"));
            Assert.False(tableSelectionSet.Allows("schema2", "table2"));
            Assert.True(tableSelectionSet.Allows("schema1", "table2"));
            Assert.True(tableSelectionSet.Allows("schema2", "table1"));
        }
        public void Include_and_Exclude_together_allow_those_selections_included_which_are_not_excluded()
        {
            var include1 = new TableSelection() { Schema = "schema1", Table = TableSelection.Any };
            var include2 = new TableSelection() { Schema = "schema2", Table = "table2" };
            var exclude1 = new TableSelection() { Schema = "schema1", Table = "table1", Exclude = true };
            var exclude2 = new TableSelection() { Schema = "schema3", Table = TableSelection.Any, Exclude = true };

            var tableSelectionSet = new TableSelectionSet();
            tableSelectionSet.AddSelections(new[] { include1, include2, exclude1, exclude2 });

            Assert.False(tableSelectionSet.Allows("schema1", "table1"));
            Assert.True(tableSelectionSet.Allows("schema1", "table2"));
            Assert.True(tableSelectionSet.Allows("schema1", "table3"));
            Assert.False(tableSelectionSet.Allows("schema2", "table1"));
            Assert.True(tableSelectionSet.Allows("schema2", "table2"));
            Assert.False(tableSelectionSet.Allows("schema2", "table3"));
            Assert.False(tableSelectionSet.Allows("schema3", "table1"));
            Assert.False(tableSelectionSet.Allows("schema3", "table2"));
            Assert.False(tableSelectionSet.Allows("schema3", "table3"));
            Assert.False(tableSelectionSet.Allows("schemaN", "table1"));
            Assert.False(tableSelectionSet.Allows("schemaN", "table2"));
            Assert.False(tableSelectionSet.Allows("schemaN", "table3"));
        }
        public void Inserting_an_AnySchemaAnyTable_selection_removes_all_previous_selections()
        {
            var includeA1 = new TableSelection() { Schema = "A", Table = "First" };
            var includeA2 = new TableSelection() { Schema = "A", Table = "Second" };
            var includeB1 = new TableSelection() { Schema = "B", Table = "First" };
            var includeB2 = new TableSelection() { Schema = "B", Table = "Second" };

            var excludeA1 = new TableSelection() { Schema = "A", Table = "First", Exclude = true };
            var excludeA2 = new TableSelection() { Schema = "A", Table = "Second", Exclude = true };
            var excludeB1 = new TableSelection() { Schema = "B", Table = "First", Exclude = true };
            var excludeB2 = new TableSelection() { Schema = "B", Table = "Second", Exclude = true };

            var tableSelectionSet = new TableSelectionSet();
            tableSelectionSet.AddSelections(new [] { includeA1, includeA2, includeB1, includeB2 });
            tableSelectionSet.AddSelections(new [] { excludeA1, excludeA2, excludeB1, excludeB2 });
            tableSelectionSet.AddSelections(new [] { TableSelection.InclusiveAll });
            tableSelectionSet.AddSelections(new [] { TableSelection.ExclusiveAll });

            Assert.Equal(new List<TableSelection> { TableSelection.InclusiveAll }, tableSelectionSet.InclusiveSelections);
            Assert.Equal(new List<TableSelection> { TableSelection.ExclusiveAll }, tableSelectionSet.ExclusiveSelections);
        }