Exemple #1
0
        private void GetTables()
        {
            var command = _connection.CreateCommand();

            command.CommandText =
                "SELECT schema_name(t.schema_id) AS [schema], t.name FROM sys.tables AS t " +
                $"WHERE t.name <> '{HistoryRepository.DefaultTableName}'";
            using (var reader = command.ExecuteReader())
            {
                while (reader.Read())
                {
                    var table = new TableModel
                    {
                        Database   = _databaseModel,
                        SchemaName = reader.GetValueOrDefault <string>("schema"),
                        Name       = reader.GetValueOrDefault <string>("name")
                    };

                    if (_tableSelectionSet.Allows(table.SchemaName, table.Name))
                    {
                        _databaseModel.Tables.Add(table);
                        _tables[TableKey(table)] = table;
                    }
                }
            }
        }
        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);
        }
        private void GetTables()
        {
            var command = _connection.CreateCommand();

            command.CommandText =
                "SELECT schema_name(t.schema_id) AS [schema], t.name FROM sys.tables AS t " +
                $"WHERE t.name <> '{HistoryRepository.DefaultTableName}'" +
                TemporalTableWhereClause;
            using (var reader = command.ExecuteReader())
            {
                while (reader.Read())
                {
                    var table = new TableModel
                    {
                        Database   = _databaseModel,
                        SchemaName = reader.GetValueOrDefault <string>("schema"),
                        Name       = reader.GetValueOrDefault <string>("name")
                    };

                    Logger.LogTrace(SqlServerDesignStrings.FoundTable(table.SchemaName, table.Name));

                    if (_tableSelectionSet.Allows(table.SchemaName, table.Name))
                    {
                        _databaseModel.Tables.Add(table);
                        _tables[TableKey(table)] = table;
                    }
                    else
                    {
                        Logger.LogTrace(
                            SqlServerDesignStrings.TableNotInSelectionSet(table.SchemaName, table.Name));
                    }
                }
            }
        }
Exemple #4
0
        void GetTables()
        {
            using (var command = new NpgsqlCommand(GetTablesQuery, _connection))
                using (var reader = command.ExecuteReader())
                {
                    while (reader.Read())
                    {
                        var table = new TableModel
                        {
                            SchemaName = reader.GetString(0),
                            Name       = reader.GetString(1)
                        };

                        if (_tableSelectionSet.Allows(table.SchemaName, table.Name))
                        {
                            _databaseModel.Tables.Add(table);
                            _tables[TableKey(table)] = table;
                        }
                    }
                }
        }
Exemple #5
0
        private void GetTables()
        {
            var command = _connection.CreateCommand();

            command.CommandText = @"SELECT TABLE_NAME FROM INFORMATION_SCHEMA.TABLES
                                    WHERE TABLE_TYPE = 'TABLE' AND (SUBSTRING(TABLE_NAME, 1,2) <> '__')";
            using (var reader = command.ExecuteReader())
            {
                while (reader.Read())
                {
                    var table = new TableModel
                    {
                        SchemaName = null,
                        Name       = reader.GetString(0)
                    };

                    if (_tableSelectionSet.Allows(table.Name))
                    {
                        _databaseModel.Tables.Add(table);
                        _tables[TableKey(table)] = table;
                    }
                }
            }
        }
Exemple #6
0
        private void GetSqliteMaster()
        {
            var command = _connection.CreateCommand();

            command.CommandText = "SELECT type, name, tbl_name FROM sqlite_master ORDER BY type DESC";

            using (var reader = command.ExecuteReader())
            {
                while (reader.Read())
                {
                    var type      = reader.GetValueOrDefault <string>("type");
                    var name      = reader.GetValueOrDefault <string>("name");
                    var tableName = reader.GetValueOrDefault <string>("tbl_name");

                    if (type == "table" &&
                        name != "sqlite_sequence" &&
                        _tableSelectionSet.Allows(name))
                    {
                        var table = new TableModel
                        {
                            Database = _databaseModel,
                            Name     = name
                        };

                        _databaseModel.Tables.Add(table);
                        _tables.Add(name, table);
                    }
                    else if (type == "index" &&
                             _tables.ContainsKey(tableName))
                    {
                        var table = _tables[tableName];

                        table.Indexes.Add(new IndexModel
                        {
                            Name  = name,
                            Table = table
                        });
                    }
                }
            }
        }
Exemple #7
0
        private void GetTables()
        {
            using (var command = _connection.CreateCommand())
            {
                command.CommandText =
                    "SELECT name FROM sqlite_master" +
                    " WHERE type = 'table'" +
                    " AND name <> 'sqlite_sequence'" +
                    $" AND name <> '{HistoryRepository.DefaultTableName}'";

                using (var reader = command.ExecuteReader())
                {
                    while (reader.Read())
                    {
                        var name = reader.GetValueOrDefault <string>("name");

                        Logger.LogTrace(SqliteDesignStrings.FoundTable(name));

                        if (_tableSelectionSet.Allows(name))
                        {
                            var table = new TableModel
                            {
                                Database = _databaseModel,
                                Name     = name
                            };

                            _databaseModel.Tables.Add(table);
                            _tables.Add(name, table);
                        }
                        else
                        {
                            Logger.LogTrace(SqliteDesignStrings.TableNotInSelectionSet(name));
                        }
                    }
                }
            }
        }
        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);
            }
        }
        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);
        }