class Sqlmysql

  1. 8.0.x lib/Drush/Sql/Sqlmysql.php Sqlmysql
  2. 7.x lib/Drush/Sql/Sqlmysql.php Sqlmysql
  3. master lib/Drush/Sql/Sqlmysql.php Sqlmysql

Namespace

Drush\Sql

Hierarchy

Expanded class hierarchy of Sqlmysql

Members

Contains filters are case sensitive
Namesort descending Modifiers Type Description
SqlBase::$db_spec public property
SqlBase::$query_extra public property
SqlBase::$query_file public property
SqlBase::connect public function A string for connecting to a database.
SqlBase::createdb public function Create a new database.
SqlBase::db_spec public function
SqlBase::delete public function
SqlBase::drop public function
SqlBase::drop_or_create public function Drop all tables (if DB exists) or CREATE target database.
SqlBase::dump public function
SqlBase::dumpFile public function
SqlBase::get_expanded_table_selection public function 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.
SqlBase::params_to_options public function
SqlBase::query public function Execute a SQL query.
SqlBase::query_format public function
SqlBase::query_prefix public function
SqlBase::scheme public function The active database driver.
SqlBase::su public function Adjust DB connection with superuser credentials if provided.
SqlBase::__construct public function Typically, SqlBase objects are contructed via drush_sql_get_class().
Sqlmysql::command public function The unix command used to connect to the database. Overrides SqlBase::command
Sqlmysql::createdb_sql public function Build a SQL string for dropping and creating a database. Overrides SqlBase::createdb_sql
Sqlmysql::creds public function Build a fragment connection parameters. Overrides SqlBase::creds
Sqlmysql::db_exists public function Overrides SqlBase::db_exists
Sqlmysql::dumpCmd public function Overrides SqlBase::dumpCmd
Sqlmysql::listTables public function Extract the name of all existing tables in the given database. Overrides SqlBase::listTables
Sqlmysql::silent public function Overrides SqlBase::silent

File

lib/Drush/Sql/Sqlmysql.php, line 5

View source
class Sqlmysql extends SqlBase {

  public function command() {
    return 'mysql';
  }

  public function creds($hide_password = TRUE) {
    if ($hide_password) {
      // EMPTY password is not the same as NO password, and is valid.
      $contents = <<<EOT
#This file was written by Drush's Sqlmysql.inc.
[client]
user="{$this->db_spec['username']}"
password="{$this->db_spec['password']}"
EOT;

      $file = drush_save_data_to_temp_file($contents);
      $parameters['defaults-extra-file'] = $file;
    }
    else {
      // User is required. Drupal calls it 'username'. MySQL calls it 'user'.
      $parameters['user'] = $this->db_spec['username'];
      // EMPTY password is not the same as NO password, and is valid.
      if (isset($this->db_spec['password'])) {
        $parameters['password'] = $this->db_spec['password'];
      }
    }

    // Some drush commands (e.g. site-install) want to connect to the
    // server, but not the database.  Connect to the built-in database.
    $parameters['database'] = empty($this->db_spec['database']) ? 'information_schema' : $this->db_spec['database'];

    // Default to unix socket if configured.
    if (!empty($this->db_spec['unix_socket'])) {
      $parameters['socket'] = $this->db_spec['unix_socket'];
    }
    // EMPTY host is not the same as NO host, and is valid (see unix_socket).
    elseif (isset($this->db_spec['host'])) {
      $parameters['host'] = $this->db_spec['host'];
    }

    if (!empty($this->db_spec['port'])) {
      $parameters['port'] = $this->db_spec['port'];
    }

    return $this->params_to_options($parameters);
  }

  public function silent() {
    return '--silent';
  }

  public function createdb_sql($dbname, $quoted = FALSE) {
    if ($quoted) {
      $dbname = '`' . $dbname . '`';
    }
    $sql[] = sprintf('DROP DATABASE IF EXISTS %s;', $dbname);
    $sql[] = sprintf('CREATE DATABASE %s /*!40100 DEFAULT CHARACTER SET utf8 */;', $dbname);
    $db_superuser = drush_get_option('db-su');
    if (isset($db_superuser)) {
      $sql[] = sprintf('GRANT ALL PRIVILEGES ON %s.* TO \'%s\'@\'%s\'', $dbname, $this->db_spec['username'], $this->db_spec['host']);
      $sql[] = sprintf("IDENTIFIED BY '%s';", $this->db_spec['password']);
      $sql[] = 'FLUSH PRIVILEGES;';
    }
    return implode(' ', $sql);
  }

  public function db_exists() {
    $current = drush_get_context('DRUSH_SIMULATE');
    drush_set_context('DRUSH_SIMULATE', FALSE);
    // Suppress output. We only care about return value.
    $return = $this->query("SELECT 1;", NULL, drush_bit_bucket());
    drush_set_context('DRUSH_SIMULATE', $current);
    return $return;
  }

  public function listTables() {
    $current = drush_get_context('DRUSH_SIMULATE');
    drush_set_context('DRUSH_SIMULATE', FALSE);
    $return = $this->query('SHOW TABLES;');
    $tables = drush_shell_exec_output();
    drush_set_context('DRUSH_SIMULATE', $current);
    return $tables;
  }

  public function dumpCmd($table_selection) {
    $parens = FALSE;
    $skip_tables = $table_selection['skip'];
    $structure_tables = $table_selection['structure'];
    $tables = $table_selection['tables'];

    $ignores = array();
    $skip_tables = array_merge($structure_tables, $skip_tables);
    $data_only = drush_get_option('data-only');
    // The ordered-dump option is only supported by MySQL for now.
    // @todo add documention once a hook for drush_get_option_help() is available.
    // @see drush_get_option_help() in drush.inc
    $ordered_dump = drush_get_option('ordered-dump');

    $exec = 'mysqldump ';
    // mysqldump wants 'databasename' instead of 'database=databasename' for no good reason.
    $only_db_name = str_replace('--database=', ' ', $this->creds());
    $exec .= $only_db_name;

    // We had --skip-add-locks here for a while to help people with insufficient permissions,
    // but removed it because it slows down the import a lot.  See http://drupal.org/node/1283978
    $extra = ' --no-autocommit --single-transaction --opt -Q';
    if (isset($data_only)) {
      $extra .= ' --no-create-info';
    }
    if (isset($ordered_dump)) {
      $extra .= ' --skip-extended-insert --order-by-primary';
    }
    $exec .= $extra;

    if (!empty($tables)) {
      $exec .= ' ' . implode(' ', $tables);
    }
    else {
      // Append the ignore-table options.
      foreach ($skip_tables as $table) {
        $ignores[] = '--ignore-table=' . $this->db_spec['database'] . '.' . $table;
        $parens = TRUE;
      }
      $exec .= ' ' . implode(' ', $ignores);

      // Run mysqldump again and append output if we need some structure only tables.
      if (!empty($structure_tables)) {
        $exec .= " && mysqldump " . $only_db_name . " --no-data $extra " . implode(' ', $structure_tables);
        $parens = TRUE;
      }
    }
    return $parens ? "($exec)" : $exec;
  }
}