예제 #1
0
        private IList <string> GetAllTables(DbConnection connection)
        {
            var tables = new List <string>();

            string commandText = DbAdapter.BuildTableCommandText(this);

            var values = new List <string>();

            values.AddRange(TablesToIgnore ?? Enumerable.Empty <string>());
            values.AddRange(SchemasToExclude ?? Enumerable.Empty <string>());
            values.AddRange(SchemasToInclude ?? Enumerable.Empty <string>());

            using (var cmd = connection.CreateCommand(commandText, values.ToArray()))
            {
                using (var reader = cmd.ExecuteReader())
                {
                    while (reader.Read())
                    {
                        if (!reader.IsDBNull(0))
                        {
                            tables.Add("\"" + reader.GetString(0) + "\".\"" + reader.GetString(1) + "\"");
                        }
                        else
                        {
                            tables.Add("\"" + reader.GetString(1) + "\"");
                        }
                    }
                }
            }

            return(tables.ToList());
        }
예제 #2
0
        private async Task <IList <string> > GetAllTables(DbConnection connection)
        {
            var tables = new List <string>();

            string commandText = DbAdapter.BuildTableCommandText(this);

            using (var cmd = connection.CreateCommand())
            {
                cmd.CommandText = commandText;
                using (var reader = await cmd.ExecuteReaderAsync())
                {
                    while (await reader.ReadAsync())
                    {
                        if (!await reader.IsDBNullAsync(0))
                        {
                            tables.Add($"{DbAdapter.QuoteCharacter}{reader.GetString(0)}{DbAdapter.QuoteCharacter}.{DbAdapter.QuoteCharacter}{reader.GetString(1)}{DbAdapter.QuoteCharacter}");
                        }
                        else
                        {
                            tables.Add($"{DbAdapter.QuoteCharacter}{reader.GetString(1)}{DbAdapter.QuoteCharacter}");
                        }
                    }
                }
            }

            return(tables.ToList());
        }
예제 #3
0
        private async Task <HashSet <Relationship> > GetRelationships(DbConnection connection)
        {
            var rels        = new HashSet <Relationship>();
            var commandText = DbAdapter.BuildRelationshipCommandText(this);

            using (var cmd = connection.CreateCommand())
            {
                cmd.CommandText = commandText;
                using (DbDataReader reader = await cmd.ExecuteReaderAsync())
                {
                    while (await reader.ReadAsync())
                    {
                        rels.Add(new Relationship(
                                     reader.IsDBNull(0) ? null : reader.GetString(0),
                                     reader.GetString(1),
                                     reader.IsDBNull(2) ? null : reader.GetString(2),
                                     reader.GetString(3),
                                     reader.GetString(4),
                                     reader.FieldCount >= 6 && !reader.IsDBNull(5) ? reader.GetString(5) : null,
                                     reader.FieldCount == 7 && !reader.IsDBNull(6) ? reader.GetString(6) : null
                                     ));
                    }
                }
            }

            return(rels);
        }
예제 #4
0
        private IList <string> GetAllTables(DbConnection connection)
        {
            var tables = new List <string>();

            string commandText = DbAdapter.BuildTableCommandText(this);

            using (var cmd = connection.CreateCommand())
            {
                cmd.CommandText = commandText;
                using (var reader = cmd.ExecuteReader())
                {
                    while (reader.Read())
                    {
                        if (!reader.IsDBNull(0))
                        {
                            tables.Add("\"" + reader.GetString(0) + "\".\"" + reader.GetString(1) + "\"");
                        }
                        else
                        {
                            tables.Add("\"" + reader.GetString(1) + "\"");
                        }
                    }
                }
            }

            return(tables.ToList());
        }
예제 #5
0
        private IList <Relationship> GetRelationships(DbConnection connection)
        {
            var rels        = new List <Relationship>();
            var commandText = DbAdapter.BuildRelationshipCommandText(this);

            var values = new List <string>();

            values.AddRange(TablesToIgnore ?? Enumerable.Empty <string>());
            values.AddRange(SchemasToExclude ?? Enumerable.Empty <string>());
            values.AddRange(SchemasToInclude ?? Enumerable.Empty <string>());

            using (var cmd = connection.CreateCommand(commandText, values.ToArray()))
            {
                using (var reader = cmd.ExecuteReader())
                {
                    while (reader.Read())
                    {
                        var rel = new Relationship
                        {
                            PrimaryKeyTable = "\"" + reader.GetString(0) + "\".\"" + reader.GetString(1) + "\"",
                            ForeignKeyTable = "\"" + reader.GetString(2) + "\".\"" + reader.GetString(3) + "\""
                        };
                        rels.Add(rel);
                    }
                }
            }

            return(rels);
        }
예제 #6
0
        private async Task BuildDeleteTables(DbConnection connection)
        {
            var allTables = await GetAllTables(connection);

            if (CheckTemporalTables && DoesDbSupportsTemporalTables(connection))
            {
                _temporalTables = await GetAllTemporalTables(connection);
            }

            var allRelationships = await GetRelationships(connection);

            _graphBuilder = new GraphBuilder(allTables, allRelationships, DbAdapter == Respawn.DbAdapter.Jet);

            if (WithReseed)
            {
                if (DbAdapter == Respawn.DbAdapter.Jet)
                {
                    allTables = await GetTableSeeds(connection, allTables);
                }
                else
                {
                    ReseedSql = DbAdapter.BuildReseedSql(_graphBuilder.ToDelete);
                }
            }
            else
            {
                ReseedSql = null;
            }
            DeleteSql = DbAdapter.BuildDeleteCommandText(_graphBuilder);
        }
예제 #7
0
        private void BuildDeleteTables(DbConnection connection)
        {
            var allTables = GetAllTables(connection);

            var allRelationships = GetRelationships(connection);

            _tablesToDelete = BuildTableList(allTables, allRelationships);

            _deleteSql = DbAdapter.BuildDeleteCommandText(_tablesToDelete);
        }
예제 #8
0
 private bool DoesDbSupportsTemporalTables(DbConnection connection)
 {
     if (DbAdapter.GetType() == Respawn.DbAdapter.SqlServer.GetType())
     {
         const int SqlServer2016MajorBuildVersion = 13;
         string    serverVersion        = connection.ServerVersion;
         string[]  serverVersionDetails = serverVersion.Split(new string[] { "." }, StringSplitOptions.None);
         int       versionNumber        = int.Parse(serverVersionDetails[0]);
         return(versionNumber >= SqlServer2016MajorBuildVersion);
     }
     return(false);
 }
예제 #9
0
        private async Task BuildDeleteTables(DbConnection connection)
        {
            var allTables = await GetAllTables(connection);

            if (CheckTemporalTables && DoesDbSupportsTemporalTables(connection))
            {
                _temporalTables = await GetAllTemporalTables(connection);
            }

            var allRelationships = await GetRelationships(connection);

            _graphBuilder = new GraphBuilder(allTables, allRelationships);

            DeleteSql = DbAdapter.BuildDeleteCommandText(_graphBuilder);
            ReseedSql = WithReseed ? DbAdapter.BuildReseedSql(_graphBuilder.ToDelete) : null;
        }
예제 #10
0
        private async Task BuildDeleteTables(DbConnection connection)
        {
            var allTables = await GetAllTables(connection);

            var allRelationships = await GetRelationships(connection);

            _graphBuilder = new GraphBuilder(allTables, allRelationships);

            DeleteSql = DbAdapter.BuildDeleteCommandText(_graphBuilder);
            if (WithReseed)
            {
                ReseedSql = DbAdapter.BuildReseedSql(_graphBuilder.ToDelete);
            }
            else
            {
                ReseedSql = null;
            }
        }
예제 #11
0
        private async Task <IList <TemporalTable> > GetAllTemporalTables(DbConnection connection)
        {
            var tables = new List <TemporalTable>();

            var commandText = DbAdapter.BuildTemporalTableCommandText(this);

            using var cmd = connection.CreateCommand();

            cmd.CommandText = commandText;

            using var reader = await cmd.ExecuteReaderAsync();

            while (await reader.ReadAsync())
            {
                tables.Add(new TemporalTable(await reader.IsDBNullAsync(0) ? null : reader.GetString(0), reader.GetString(1), reader.GetString(2), reader.GetString(3)));
            }

            return(tables);
        }
예제 #12
0
        private async Task <HashSet <Table> > GetAllTables(DbConnection connection)
        {
            var tables = new HashSet <Table>();

            string commandText = DbAdapter.BuildTableCommandText(this);

            using (var cmd = connection.CreateCommand())
            {
                cmd.CommandText = commandText;
                using (var reader = await cmd.ExecuteReaderAsync())
                {
                    while (await reader.ReadAsync())
                    {
                        tables.Add(new Table(reader.IsDBNull(0) ? null : reader.GetString(0), reader.GetString(1)));
                    }
                }
            }

            return(tables);
        }
예제 #13
0
        private async Task <HashSet <Relationship> > GetRelationships(DbConnection connection)
        {
            var rels        = new HashSet <Relationship>();
            var commandText = DbAdapter.BuildRelationshipCommandText(this);

            using var cmd = connection.CreateCommand();

            cmd.CommandText = commandText;

            using var reader = await cmd.ExecuteReaderAsync();

            while (await reader.ReadAsync())
            {
                rels.Add(new Relationship(
                             new Table(await reader.IsDBNullAsync(0) ? null : reader.GetString(0), reader.GetString(1)),
                             new Table(await reader.IsDBNullAsync(2) ? null : reader.GetString(2), reader.GetString(3)),
                             reader.GetString(4)));
            }

            return(rels);
        }
예제 #14
0
        public virtual async Task Reset(DbConnection connection)
        {
            if (string.IsNullOrWhiteSpace(DeleteSql))
            {
                DatabaseName = connection.Database;
                await BuildDeleteTables(connection);
            }

            if (_temporalTables.Any())
            {
                var turnOffVersioningCommandText = DbAdapter.BuildTurnOffSystemVersioningCommandText(_temporalTables);
                await ExecuteAlterSystemVersioningAsync(connection, turnOffVersioningCommandText);
            }

            await ExecuteDeleteSqlAsync(connection);

            if (_temporalTables.Any())
            {
                var turnOnVersioningCommandText = DbAdapter.BuildTurnOnSystemVersioningCommandText(_temporalTables);
                await ExecuteAlterSystemVersioningAsync(connection, turnOnVersioningCommandText);
            }
        }
예제 #15
0
 private async Task <HashSet <Table> > GetTableSeeds(DbConnection connection, HashSet <Table> tables)
 {
     using (var cmd = connection.CreateCommand())
     {
         foreach (Table table in tables)
         {
             cmd.CommandText = DbAdapter.BuildFullColumnQueryCommandText(table);
             using (var reader = await cmd.ExecuteReaderAsync())
             {
                 for (int i = 0; i < reader.FieldCount; i++)
                 {
                     if (reader.GetDataTypeName(i) == DbAdapter.SeedColumnTypeName())
                     {
                         table.SeedColumn = reader.GetName(i);
                         break;
                     }
                 }
             }
         }
     }
     return(tables);
 }
예제 #16
0
        private IList <Relationship> GetRelationships(DbConnection connection)
        {
            var rels        = new List <Relationship>();
            var commandText = DbAdapter.BuildRelationshipCommandText(this);

            using (var cmd = connection.CreateCommand())
            {
                cmd.CommandText = commandText;
                using (var reader = cmd.ExecuteReader())
                {
                    while (reader.Read())
                    {
                        var rel = new Relationship
                        {
                            PrimaryKeyTable = "\"" + reader.GetString(0) + "\".\"" + reader.GetString(1) + "\"",
                            ForeignKeyTable = "\"" + reader.GetString(2) + "\".\"" + reader.GetString(3) + "\""
                        };
                        rels.Add(rel);
                    }
                }
            }

            return(rels);
        }
예제 #17
0
        private async Task <IList <Relationship> > GetRelationships(DbConnection connection)
        {
            var rels        = new List <Relationship>();
            var commandText = DbAdapter.BuildRelationshipCommandText(this);

            using (var cmd = connection.CreateCommand())
            {
                cmd.CommandText = commandText;
                using (var reader = await cmd.ExecuteReaderAsync())
                {
                    while (await reader.ReadAsync())
                    {
                        var rel = new Relationship
                        {
                            PrimaryKeyTable = $"{DbAdapter.QuoteCharacter}{reader.GetString(0)}{DbAdapter.QuoteCharacter}.{DbAdapter.QuoteCharacter}{reader.GetString(1)}{DbAdapter.QuoteCharacter}",
                            ForeignKeyTable = $"{DbAdapter.QuoteCharacter}{reader.GetString(2)}{DbAdapter.QuoteCharacter}.{DbAdapter.QuoteCharacter}{reader.GetString(3)}{DbAdapter.QuoteCharacter}"
                        };
                        rels.Add(rel);
                    }
                }
            }

            return(rels);
        }