7 class Sqlmysql extends SqlBase {
9 public function command() {
13 public function creds($hide_password = TRUE) {
15 // EMPTY password is not the same as NO password, and is valid.
17 #This file was written by Drush's Sqlmysql.php.
19 user="{$this->db_spec['username']}"
20 password="{$this->db_spec['password']}"
23 $file = drush_save_data_to_temp_file($contents);
24 $parameters['defaults-extra-file'] = $file;
27 // User is required. Drupal calls it 'username'. MySQL calls it 'user'.
28 $parameters['user'] = $this->db_spec['username'];
29 // EMPTY password is not the same as NO password, and is valid.
30 if (isset($this->db_spec['password'])) {
31 $parameters['password'] = $this->db_spec['password'];
35 // Some drush commands (e.g. site-install) want to connect to the
36 // server, but not the database. Connect to the built-in database.
37 $parameters['database'] = empty($this->db_spec['database']) ? 'information_schema' : $this->db_spec['database'];
39 // Default to unix socket if configured.
40 if (!empty($this->db_spec['unix_socket'])) {
41 $parameters['socket'] = $this->db_spec['unix_socket'];
43 // EMPTY host is not the same as NO host, and is valid (see unix_socket).
44 elseif (isset($this->db_spec['host'])) {
45 $parameters['host'] = $this->db_spec['host'];
48 if (!empty($this->db_spec['port'])) {
49 $parameters['port'] = $this->db_spec['port'];
52 if (!empty($this->db_spec['pdo']['unix_socket'])) {
53 $parameters['socket'] = $this->db_spec['pdo']['unix_socket'];
56 if (!empty($this->db_spec['pdo'][PDO::MYSQL_ATTR_SSL_CA])) {
57 $parameters['ssl-ca'] = $this->db_spec['pdo'][PDO::MYSQL_ATTR_SSL_CA];
60 if (!empty($this->db_spec['pdo'][PDO::MYSQL_ATTR_SSL_CAPATH])) {
61 $parameters['ssl-capath'] = $this->db_spec['pdo'][PDO::MYSQL_ATTR_SSL_CAPATH];
64 if (!empty($this->db_spec['pdo'][PDO::MYSQL_ATTR_SSL_CERT])) {
65 $parameters['ssl-cert'] = $this->db_spec['pdo'][PDO::MYSQL_ATTR_SSL_CERT];
68 if (!empty($this->db_spec['pdo'][PDO::MYSQL_ATTR_SSL_CIPHER])) {
69 $parameters['ssl-cipher'] = $this->db_spec['pdo'][PDO::MYSQL_ATTR_SSL_CIPHER];
72 if (!empty($this->db_spec['pdo'][PDO::MYSQL_ATTR_SSL_KEY])) {
73 $parameters['ssl-key'] = $this->db_spec['pdo'][PDO::MYSQL_ATTR_SSL_KEY];
76 return $this->params_to_options($parameters);
79 public function silent() {
83 public function createdb_sql($dbname, $quoted = FALSE) {
85 $dbname = '`' . $dbname . '`';
87 $sql[] = sprintf('DROP DATABASE IF EXISTS %s;', $dbname);
88 $sql[] = sprintf('CREATE DATABASE %s /*!40100 DEFAULT CHARACTER SET utf8 */;', $dbname);
89 $db_superuser = drush_get_option('db-su');
90 if (isset($db_superuser)) {
91 // - For a localhost database, create a localhost user. This is important for security.
92 // localhost is special and only allows local Unix socket file connections.
93 // - If the database is on a remote server, create a wilcard user with %.
94 // We can't easily know what IP adderss or hostname would represent our server.
95 $domain = ($this->db_spec['host'] == 'localhost') ? 'localhost' : '%';
96 $sql[] = sprintf('GRANT ALL PRIVILEGES ON %s.* TO \'%s\'@\'%s\'', $dbname, $this->db_spec['username'], $domain);
97 $sql[] = sprintf("IDENTIFIED BY '%s';", $this->db_spec['password']);
98 $sql[] = 'FLUSH PRIVILEGES;';
100 return implode(' ', $sql);
103 public function db_exists() {
104 $current = drush_get_context('DRUSH_SIMULATE');
105 drush_set_context('DRUSH_SIMULATE', FALSE);
106 // Suppress output. We only care about return value.
107 $return = $this->query("SELECT 1;", NULL, drush_bit_bucket());
108 drush_set_context('DRUSH_SIMULATE', $current);
112 public function listTables() {
113 $current = drush_get_context('DRUSH_SIMULATE');
114 drush_set_context('DRUSH_SIMULATE', FALSE);
115 $return = $this->query('SHOW TABLES;');
116 $tables = drush_shell_exec_output();
117 drush_set_context('DRUSH_SIMULATE', $current);
121 public function dumpCmd($table_selection) {
123 $skip_tables = $table_selection['skip'];
124 $structure_tables = $table_selection['structure'];
125 $tables = $table_selection['tables'];
128 $skip_tables = array_merge($structure_tables, $skip_tables);
129 $data_only = drush_get_option('data-only');
130 // The ordered-dump option is only supported by MySQL for now.
131 // @todo add documention once a hook for drush_get_option_help() is available.
132 // @see drush_get_option_help() in drush.inc
133 $ordered_dump = drush_get_option('ordered-dump');
135 $exec = 'mysqldump ';
136 // mysqldump wants 'databasename' instead of 'database=databasename' for no good reason.
137 $only_db_name = str_replace('--database=', ' ', $this->creds());
138 $exec .= $only_db_name;
140 // We had --skip-add-locks here for a while to help people with insufficient permissions,
141 // but removed it because it slows down the import a lot. See http://drupal.org/node/1283978
142 $extra = ' --no-autocommit --single-transaction --opt -Q';
143 if (isset($data_only)) {
144 $extra .= ' --no-create-info';
146 if (isset($ordered_dump)) {
147 $extra .= ' --skip-extended-insert --order-by-primary';
149 if ($option = drush_get_option('extra', $this->query_extra)) {
150 $extra .= " $option";
154 if (!empty($tables)) {
155 $exec .= ' ' . implode(' ', $tables);
158 // Append the ignore-table options.
159 foreach ($skip_tables as $table) {
160 $ignores[] = '--ignore-table=' . $this->db_spec['database'] . '.' . $table;
163 $exec .= ' '. implode(' ', $ignores);
165 // Run mysqldump again and append output if we need some structure only tables.
166 if (!empty($structure_tables)) {
167 $exec .= " && mysqldump " . $only_db_name . " --no-data $extra " . implode(' ', $structure_tables);
171 return $parens ? "($exec)" : $exec;