public void Allows_updates_IsMatched_for_matching_selections()
        {
            var tableNames = new List<string> { "table0", "table1", "table2" };
            var tableSelectionSet = new TableSelectionSet(tableNames);

            Assert.Equal(3, tableSelectionSet.Tables.Count);

            // Allows() has not run yet - so all table selections should have IsMatched false
            foreach (var table in tableSelectionSet.Tables)
            {
                Assert.False(table.IsMatched);
            }

            // When Allows() runs the matching table selection is marked true
            Assert.True(tableSelectionSet.Allows("table0"));
            var table0Selection = tableSelectionSet.Tables.First();
            var table1Selection = tableSelectionSet.Tables.Skip(1).First();
            var table2Selection = tableSelectionSet.Tables.Last();
            Assert.True(table0Selection.IsMatched);
            Assert.False(table1Selection.IsMatched);
            Assert.False(table2Selection.IsMatched);

            // When Allows() runs again the 2nd table selection is also marked true
            Assert.True(tableSelectionSet.Allows("table1"));
            Assert.True(table0Selection.IsMatched);
            Assert.True(table1Selection.IsMatched);
            Assert.False(table2Selection.IsMatched);

            // When Allows() runs a third time against a non-selected table no further table selection is marked true
            Assert.False(tableSelectionSet.Allows("tableOther"));
            Assert.True(table0Selection.IsMatched);
            Assert.True(table1Selection.IsMatched);
            Assert.False(table2Selection.IsMatched);
        }
        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);
        }
Exemplo n.º 3
0
        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;

                if (SupportsSequences)
                {
                    GetSequences();
                }

                GetDefaultSchema();
                GetTypeAliases();
                GetTables();
                GetColumns();
                GetIndexes();
                GetForeignKeys();
                return(_databaseModel);
            }
        }
        public override IModel Create(string connectionString, TableSelectionSet tableSelectionSet)
        {
            var model = base.Create(connectionString, tableSelectionSet);

            model.Scaffolding().UseProviderMethodName = nameof(SqlServerDbContextOptionsExtensions.UseSqlServer);
            return(model);
        }
Exemplo n.º 5
0
        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;

                GetSqliteMaster();
                GetColumns();
                GetIndexes();
                GetForeignKeys();
                return(_databaseModel);
            }
        }
Exemplo n.º 6
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);
 }
Exemplo n.º 7
0
        public virtual void CheckSelectionsMatched([NotNull] TableSelectionSet tableSelectionSet)
        {
            foreach (var schemaSelection in tableSelectionSet.Schemas.Where(s => !s.IsMatched))
            {
                Logger.LogWarning(RelationalDesignStrings.MissingSchema(schemaSelection.Text));
            }

            foreach (var tableSelection in tableSelectionSet.Tables.Where(t => !t.IsMatched))
            {
                Logger.LogWarning(RelationalDesignStrings.MissingTable(tableSelection.Text));
            }
        }
        public virtual IModel Create(string connectionString, TableSelectionSet tableSelectionSet)
        {
            Check.NotEmpty(connectionString, nameof(connectionString));

            var databaseModel = _databaseModelFactory.Create(connectionString, tableSelectionSet ?? TableSelectionSet.All);
            if (tableSelectionSet != null)
            {
                CheckSelectionsMatched(tableSelectionSet);
            }

            return CreateFromDatabaseModel(databaseModel);
        }
Exemplo n.º 9
0
        public virtual IModel Create(string connectionString, TableSelectionSet tableSelectionSet)
        {
            Check.NotEmpty(connectionString, nameof(connectionString));

            var databaseModel = _databaseModelFactory.Create(connectionString, tableSelectionSet ?? TableSelectionSet.All);

            if (tableSelectionSet != null)
            {
                CheckSelectionsMatched(tableSelectionSet);
            }

            return(CreateFromDatabaseModel(databaseModel));
        }
Exemplo n.º 10
0
        public override IModel Create(string connectionString, TableSelectionSet tableSelectionSet)
        {
            if ((tableSelectionSet != null) &&
                tableSelectionSet.Schemas.Any())
            {
                Logger.LogWarning("You have specified some schema selections. The SQL Server Compact provider does not support these and they will be ignored. Note: it does support table selections.");

                tableSelectionSet.Schemas.ToList().ForEach(s => s.IsMatched = true);
            }

            var model = base.Create(connectionString, tableSelectionSet);

            model.Scaffolding().UseProviderMethodName = nameof(SqlCeDbContextOptionsExtensions.UseSqlCe);
            return(model);
        }
Exemplo n.º 11
0
        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);
        }
Exemplo n.º 12
0
        /// <summary>
        ///     Tests whether the table is allowed by the <see cref="TableSelectionSet" /> and
        ///     updates the <see cref="TableSelectionSet" />'s <see cref="TableSelectionSet.Selection" />(s)
        ///     to mark that they have been matched.
        /// </summary>
        /// <param name="tableSet"> the <see cref="TableSelectionSet" /> to test </param>
        /// <param name="tableName"> name of the database table to check </param>
        /// <returns> whether or not the table is allowed </returns>
        public static bool Allows(this TableSelectionSet tableSet, string tableName)
        {
            if (tableSet == null ||
                tableSet.Tables.Count == 0)
            {
                return(true);
            }

            //TODO: look into performance for large selection sets and numbers of tables
            var result = false;
            var matchingTableSelections = tableSet.Tables.Where(
                t => t.Text.Equals(tableName, StringComparison.OrdinalIgnoreCase));

            if (matchingTableSelections.Any())
            {
                matchingTableSelections.ToList().ForEach(selection => selection.IsMatched = true);
                result = true;
            }

            return(result);
        }
        /// <summary>
        ///     Tests whether the schema/table is allowed by the <see cref="TableSelectionSet" />
        ///     and updates the <see cref="TableSelectionSet" />'s <see cref="TableSelectionSet.Selection" />(s)
        ///     to mark that they have been matched.
        /// </summary>
        /// <param name="tableSelectionSet"> the <see cref="TableSelectionSet" /> to test </param>
        /// <param name="schemaName"> name of the database schema to check </param>
        /// <param name="tableName"> name of the database table to check </param>
        /// <returns> whether or not the schema/table is allowed </returns>
        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;

            //TODO: look into performance for large selection sets and numbers of tables
            foreach (var pattern in schemaPatterns)
            {
                var patternToMatch           = pattern.Replace("{schema}", schemaName);
                var matchingSchemaSelections = tableSelectionSet.Schemas.Where(
                    s => s.Text.Equals(patternToMatch, StringComparison.OrdinalIgnoreCase));
                if (matchingSchemaSelections.Any())
                {
                    matchingSchemaSelections.ToList().ForEach(selection => selection.IsMatched = true);
                    result = true;
                }
            }

            foreach (var pattern in tablePatterns)
            {
                var patternToMatch          = pattern.Replace("{schema}", schemaName).Replace("{table}", tableName);
                var matchingTableSelections = tableSelectionSet.Tables.Where(
                    t => t.Text.Equals(patternToMatch, StringComparison.OrdinalIgnoreCase));
                if (matchingTableSelections.Any())
                {
                    matchingTableSelections.ToList().ForEach(selection => selection.IsMatched = true);
                    result = true;
                }
            }

            return(result);
        }
Exemplo n.º 14
0
        public DatabaseModel Create(string connectionString, TableSelectionSet tableSelectionSet)
        {
            Check.NotEmpty(connectionString, nameof(connectionString));
            Check.NotNull(tableSelectionSet, nameof(tableSelectionSet));

            ResetState();

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

                _databaseModel.DatabaseName      = _connection.Database;
                _databaseModel.DefaultSchemaName = "public";

                GetTables();
                GetColumns();
                GetIndexes();
                GetConstraints();
                GetSequences();
                return(_databaseModel);
            }
        }
Exemplo n.º 15
0
        public virtual Task<ReverseEngineerFiles> ReverseEngineerAsync(
            [NotNull] string provider,
            [NotNull] string connectionString,
            [CanBeNull] string outputDir,
            [CanBeNull] string dbContextClassName,
            [NotNull] IEnumerable<string> schemas,
            [NotNull] IEnumerable<string> tables,
            bool useDataAnnotations,
            bool overwriteFiles,
            CancellationToken cancellationToken = default(CancellationToken))
        {
            Check.NotEmpty(provider, nameof(provider));
            Check.NotEmpty(connectionString, nameof(connectionString));
            Check.NotNull(schemas, nameof(schemas));
            Check.NotNull(tables, nameof(tables));

            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,
                OverwriteFiles = overwriteFiles
            };

            return generator.GenerateAsync(configuration, cancellationToken);
        }
        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 DatabaseModel CreateModel(string createSql, TableSelectionSet selection = null)
        {
            _testStore.ExecuteNonQuery(createSql);

            return _factory.Create(_testStore.ConnectionString, selection ?? TableSelectionSet.All);
        }
        public void Allows_updates_IsMatched_only_for_matching_selections()
        {
            var tableNames = new List<string> { "table0", "[table0]", "table1", "[table1]" };
            var schemaNames = new List<string> { "schemaA", "[schemaA]", "schemaB", "[schemaB]" };
            var tableSelectionSet = new TableSelectionSet(tableNames, schemaNames);

            Assert.Equal(4, tableSelectionSet.Schemas.Count);
            Assert.Equal(4, tableSelectionSet.Tables.Count);

            // Allows() has not run yet - so all selections should have IsMatched false
            foreach (var schema in tableSelectionSet.Schemas)
            {
                Assert.False(schema.IsMatched);
            }
            foreach (var table in tableSelectionSet.Tables)
            {
                Assert.False(table.IsMatched);
            }

            var schemaASelection = tableSelectionSet.Schemas.First();
            var schemaASelectionWithSquareBrackets = tableSelectionSet.Schemas.Skip(1).First();
            var schemaBSelection = tableSelectionSet.Schemas.Skip(2).First();
            var schemaBSelectionWithSquareBrackets = tableSelectionSet.Schemas.Skip(3).First();
            var table0Selection = tableSelectionSet.Tables.First();
            var table0SelectionWithSquareBrackets = tableSelectionSet.Tables.Skip(1).First();
            var table1Selection = tableSelectionSet.Tables.Skip(2).First();
            var table1SelectionWithSquareBrackets = tableSelectionSet.Tables.Skip(3).First();

            // When Allows() runs against a table which matches just on a schema then
            // those schema selections' IsMatched are marked true (regardless of square brackets)
            // but the table selections' IsMatched remain false
            Assert.True(tableSelectionSet.Allows("schemaA", "tableOther"));
            Assert.True(schemaASelection.IsMatched);
            Assert.True(schemaASelectionWithSquareBrackets.IsMatched);
            Assert.False(schemaBSelection.IsMatched);
            Assert.False(schemaBSelectionWithSquareBrackets.IsMatched);
            Assert.False(table0Selection.IsMatched);
            Assert.False(table0SelectionWithSquareBrackets.IsMatched);
            Assert.False(table1Selection.IsMatched);
            Assert.False(table1SelectionWithSquareBrackets.IsMatched);

            // When Allows() runs against a table which matches just on a table then
            // those table selections' IsMatched are marked true (regardless of square brackets)
            // the schema selections' IsMatched remain as they were before
            Assert.True(tableSelectionSet.Allows("schemaOther", "table0"));
            Assert.True(schemaASelection.IsMatched);
            Assert.True(schemaASelectionWithSquareBrackets.IsMatched);
            Assert.False(schemaBSelection.IsMatched);
            Assert.False(schemaBSelectionWithSquareBrackets.IsMatched);
            Assert.True(table0Selection.IsMatched);
            Assert.True(table0SelectionWithSquareBrackets.IsMatched);
            Assert.False(table1Selection.IsMatched);
            Assert.False(table1SelectionWithSquareBrackets.IsMatched);

            // When Allows() runs against a non-selected schema/table no further schema or table selection is marked true
            Assert.False(tableSelectionSet.Allows("schemaOther", "tableOther"));
            Assert.True(schemaASelection.IsMatched);
            Assert.True(schemaASelectionWithSquareBrackets.IsMatched);
            Assert.False(schemaBSelection.IsMatched);
            Assert.False(schemaBSelectionWithSquareBrackets.IsMatched);
            Assert.True(table0Selection.IsMatched);
            Assert.True(table0SelectionWithSquareBrackets.IsMatched);
            Assert.False(table1Selection.IsMatched);
            Assert.False(table1SelectionWithSquareBrackets.IsMatched);

            // When Allows() runs against a table which matches on both schema and table then
            // both the matching schema selections' and the matching table selections' IsMatched
            // are marked true (regardless of square brackets)
            Assert.True(tableSelectionSet.Allows("schemaB", "table1"));
            Assert.True(schemaASelection.IsMatched);
            Assert.True(schemaASelectionWithSquareBrackets.IsMatched);
            Assert.True(schemaBSelection.IsMatched);
            Assert.True(schemaBSelectionWithSquareBrackets.IsMatched);
            Assert.True(table0Selection.IsMatched);
            Assert.True(table0SelectionWithSquareBrackets.IsMatched);
            Assert.True(table1Selection.IsMatched);
            Assert.True(table1SelectionWithSquareBrackets.IsMatched);
        }
 public virtual DatabaseModel Create(string connectionString, TableSelectionSet tableSelectionSet)
 {
     throw new NotImplementedException();
 }
        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);
            }
        }