Exemplo n.º 1
0
        private void DropAllTables(Schema schema, bool extractCatalogAfterDropping)
        {
            var batch       = SqlDml.Batch();
            var foreignKeys = schema.Tables.SelectMany(el => el.TableConstraints.OfType <ForeignKey>());

            foreach (var foreignKey in foreignKeys)
            {
                batch.Add(SqlDdl.Alter(foreignKey.Table, SqlDdl.DropConstraint(foreignKey)));
            }
            foreach (var view in schema.Views)
            {
                batch.Add(SqlDdl.Drop(view));
                schema.Tables.Remove(schema.Tables[view.Name]);
            }
            foreach (var table in schema.Tables)
            {
                batch.Add(SqlDdl.Drop(table));
            }
            if (batch.Count <= 0)
            {
                return;
            }
            var sqlCommand = SqlConnection.CreateCommand();

            sqlCommand.CommandText = SqlDriver.Compile(batch).GetCommandText();
            sqlCommand.ExecuteNonQuery();
            if (extractCatalogAfterDropping)
            {
                Catalog = SqlDriver.ExtractCatalog(SqlConnection);
            }
        }
Exemplo n.º 2
0
        private static void DropForeignKeys(StorageDriver driver, IEnumerable <Table> tables, ISqlExecutor executor)
        {
            var statements = tables
                             .SelectMany(t => t.TableConstraints.OfType <ForeignKey>())
                             .Select(fk => driver.Compile(SqlDdl.Alter(fk.Table, SqlDdl.DropConstraint(fk))).GetCommandText())
                             .ToList();

            executor.ExecuteMany(statements);
        }
        public void Test159()
        {
            var t = Catalog.Schemas["Sakila"].Tables["customer"];

            Xtensive.Sql.Model.UniqueConstraint uc = t.CreateUniqueConstraint("newUniqueConstraint", t.TableColumns["email"]);
            SqlAlterTable stmt = SqlDdl.Alter(t, SqlDdl.DropConstraint(uc));

            Console.Write(Compile(stmt));
        }
Exemplo n.º 4
0
        private static Task DropForeignKeysAsync(
            StorageDriver driver, IEnumerable <Table> tables, ISqlExecutor executor, CancellationToken token)
        {
            var statements = tables
                             .SelectMany(t => t.TableConstraints.OfType <ForeignKey>())
                             .Select(fk => driver.Compile(SqlDdl.Alter(fk.Table, SqlDdl.DropConstraint(fk))).GetCommandText())
                             .ToList();

            return(executor.ExecuteManyAsync(statements, token));
        }
Exemplo n.º 5
0
        private void DropSchema(Catalog catalog)
        {
            var schema    = catalog.DefaultSchema;
            var dropBatch = SqlDml.Batch();

            foreach (var constraint in schema.Tables.Where(t => t.TableConstraints.Count != 0).SelectMany(t => t.TableConstraints.OfType <ForeignKey>()))
            {
                if (dropBatch.Count > 31)
                {
                    ExecuteBatch(dropBatch);
                    dropBatch = SqlDml.Batch();
                }
                dropBatch.Add(SqlDdl.Alter(constraint.Table, SqlDdl.DropConstraint(constraint)));
            }

            foreach (var view in schema.Views)
            {
                if (dropBatch.Count > 31)
                {
                    ExecuteBatch(dropBatch);
                    dropBatch = SqlDml.Batch();
                }
                dropBatch.Add(SqlDdl.Drop(view));
                schema.Tables.Remove(schema.Tables[view.Name]);
            }

            foreach (var schemaTable in schema.Tables)
            {
                if (dropBatch.Count > 31)
                {
                    ExecuteBatch(dropBatch);
                    dropBatch = SqlDml.Batch();
                }
                dropBatch.Add(SqlDdl.Drop(schemaTable));
            }

            if (dropBatch.Count != 0)
            {
                ExecuteBatch(dropBatch);
            }
        }
        private void ClearSchema(SqlDriver driver)
        {
            using (var connection = driver.CreateConnection()) {
                connection.Open();
                try {
                    var schema      = driver.ExtractSchema(connection, SpecialSchemaAlias);
                    var foreignKeys = schema.Tables
                                      .Select(t => new {
                        Table       = t,
                        ForeignKeys = t.TableConstraints.OfType <ForeignKey>()
                    });
                    foreach (var dropConstraintText in from foreignKeyInfo in foreignKeys
                             from foreignKey in foreignKeyInfo.ForeignKeys
                             select driver.Compile(SqlDdl.Alter(foreignKeyInfo.Table, SqlDdl.DropConstraint(foreignKey))).GetCommandText())
                    {
                        using (var command = connection.CreateCommand(dropConstraintText))
                            command.ExecuteNonQuery();
                    }

                    foreach (var table in schema.Tables)
                    {
                        var dropTableText = driver.Compile(SqlDdl.Drop(table, true)).GetCommandText();
                        using (var command = connection.CreateCommand(dropTableText))
                            command.ExecuteNonQuery();
                    }
                }
                finally {
                    connection.Close();
                }
            }
        }