function _drush_sql_get_db_table_list

6.x sql.drush.inc _drush_sql_get_db_table_list($db_spec, $site_record = NULL)

Extract the name of all existing tables in the given database.

Parameters

$db_spec: For D5/D6, a $db_url. For D7, a target in the default DB connection.

$site_record: Necessary for remote database.

Return value

array An array of table names which exist in the current database.

2 calls to _drush_sql_get_db_table_list()
drush_sql_get_expanded_table_selection in commands/sql/sql.drush.inc
Get a list of all table names and expand input that may contain wildcards (`*`) if necessary so that the array returned only contains valid table names i.e. actual tables that exist, without a wildcard.
_drush_sql_drop in commands/sql/sql.drush.inc

File

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

Code

function _drush_sql_get_db_table_list($db_spec, $site_record = NULL) {
  $target_site = '@self';
  if (isset($site_record)) {
    $target_site = $site_record;
  }

  // Prepare the query to obtain the list of tables depending on the
  // database type.
  $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';
      break;
  }

  // @todo Add error handling.
  // Send db-spec because site-install has no $databases written to settings.php when
  // this is called by sql-drop. Also needed during archive-restore.
  $options = array(
    'db-spec' => $db_spec,
    'result-file' => FALSE,
  );
  $backend_options = array(
    'integrate' => FALSE,
    'method' => 'POST',
    'override-simulated' => TRUE,
  );
  $result = drush_invoke_process($target_site, 'sql-query', array($query), $options, $backend_options);
  $tables_raw = preg_split('/\r\n|\r|\n/', rtrim($result['output']));

  $tables = array();
  if (!empty($tables_raw)) {
    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.
      foreach ($tables_raw as $line) {
        preg_match_all('/[^\s]+/', $line, $matches);
        if (!empty($matches[0])) {
          foreach ($matches[0] as $match) {
            $tables[] = $match;
          }
        }
      }
    }
    elseif ($scheme === 'sqlsrv') {
      // Shift off the header of the column of data returned.
      array_pop($tables_raw);
      array_pop($tables_raw);
      $tables = $tables_raw;
    }
    else {
      // Shift off the header of the column of data returned.
      array_shift($tables_raw);
      $tables = $tables_raw;
    }
  }

  return $tables;
}