function _drush_sql_drop

6.x sql.drush.inc _drush_sql_drop($db_spec = NULL)
4.x sql.drush.inc _drush_sql_drop($db_spec = NULL)
5.x sql.drush.inc _drush_sql_drop($db_spec = NULL)
2 calls to _drush_sql_drop()

File

commands/sql/sql.drush.inc, line 730
Drush sql commands

Code

function _drush_sql_drop($db_spec = NULL) {
  // TODO: integrate with _drush_sql_get_table_list?

  $suffix = '';
  $scheme = _drush_sql_get_scheme($db_spec);
  switch ($scheme) {
    case 'pgsql':
      $query = drush_sql_show_tables_pgsql();
      break;
    case 'sqlite':
      $query = '.tables';
      break;
    case 'sqlsrv':
      $query = 'SELECT TABLE_NAME FROM information_schema.tables';
      break;
    case 'oracle':
      $query = "SELECT TABLE_NAME FROM USER_TABLES WHERE TABLE_NAME NOT IN ('BLOBS','LONG_IDENTIFIERS')";
      $suffix = '.sql';
      break;
    default:
      $query = 'SHOW TABLES;';
  }

  $filename = drush_save_data_to_temp_file($query, $suffix);
  $exec = drush_sql_build_exec($db_spec, $filename);

  // Actually run this prep query no matter if in SIMULATE.
  $old = drush_get_context('DRUSH_SIMULATE');
  drush_set_context('DRUSH_SIMULATE', FALSE);
  drush_shell_exec($exec);
  drush_set_context('DRUSH_SIMULATE', $old);
  if ($tables = drush_shell_exec_output()) {
    if ($scheme === 'sqlite') {
      // SQLite's '.tables' command always outputs the table names in a column
      // format, like this:
      // table_alpha    table_charlie    table_echo
      // table_bravo    table_delta      table_foxtrot
      // …and there doesn't seem to be a way to fix that. So we need to do some
      // clean-up.
      // Since we're already doing iteration here, might as well build the SQL
      // too, since SQLite only wants one table per DROP TABLE command (so we have
      // to do "DROP TABLE foo; DROP TABLE bar;" instead of
      // "DROP TABLE foo, bar;").
      $sql = '';
      foreach ($tables as $line) {
        preg_match_all('/[^\s]+/', $line, $matches);
        if (!empty($matches[0])) {
          foreach ($matches[0] as $match) {
            $sql .= "DROP TABLE {$match};";
          }
        }
      }
      // We can't use drush_op('db_query', $sql) because it will only perform one
      // SQL command and we're technically performing several.
      $exec = _drush_sql_connect($db_spec);
      $exec .= " '{$sql}'";
      return drush_op_system($exec) == 0;
    }
    elseif ($scheme === 'sqlsrv') {
      // Shift off the header of the column of data returned.
      array_pop($tables);
      array_pop($tables);
      $sql = 'DROP TABLE ' . implode(', ', $tables);
      return _drush_sql_query($sql, $db_spec);
    }
    else {
      // Shift off the header of the column of data returned.
      array_shift($tables);
      $sql = 'DROP TABLE ' . implode(', ', $tables);
      return _drush_sql_query($sql, $db_spec);
    }
  }
  else {
    drush_log(dt('No tables to drop.'), 'ok');
  }
  return TRUE;
}