Sqlmysql.php

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

Namespace

Drush\Sql

Classes

Namesort descending Description
Sqlmysql

File

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