Sqlsqlsrv.php

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

Namespace

Drush\Sql

Classes

Namesort ascending Description
Sqlsqlsrv

File

lib/Drush/Sql/Sqlsqlsrv.php
View source
  1. <?php
  2. namespace Drush\Sql;
  3. class Sqlsqlsrv extends SqlBase {
  4. // The way you pass a sql file when issueing a query.
  5. public $query_file = '-h -1 -i';
  6. public function command() {
  7. return 'sqlcmd';
  8. }
  9. public function creds() {
  10. // Some drush commands (e.g. site-install) want to connect to the
  11. // server, but not the database. Connect to the built-in database.
  12. $database = empty($this->db_spec['database']) ? 'master' : $this->db_spec['database'];
  13. // Host and port are optional but have defaults.
  14. $host = empty($this->db_spec['host']) ? '.\SQLEXPRESS' : $this->db_spec['host'];
  15. if ($this->db_spec['username'] == '') {
  16. return ' -S ' . $host . ' -d ' . $database;
  17. }
  18. else {
  19. return ' -S ' . $host . ' -d ' . $database . ' -U ' . $this->db_spec['username'] . ' -P ' . $this->db_spec['password'];
  20. }
  21. }
  22. public function db_exists() {
  23. // TODO: untested, but the gist is here.
  24. $database = $this->db_spec['database'];
  25. // Get a new class instance that has no 'database'.
  26. $db_spec_no_db = $this->db_spec;
  27. unset($db_spec_no_db['database']);
  28. $sql_no_db = drush_sql_get_class($db_spec_no_db);
  29. $query = "if db_id('$database') IS NOT NULL print 1";
  30. drush_shell_exec($sql_no_db->connect() . ' -Q %s', $query);
  31. $output = drush_shell_exec_output();
  32. return $output[0] == 1;
  33. }
  34. public function listTables() {
  35. $return = $this->query('SELECT TABLE_NAME FROM information_schema.tables');
  36. $tables = drush_shell_exec_output();
  37. if (!empty($tables)) {
  38. // Shift off the header of the column of data returned.
  39. array_shift($tables);
  40. return $tables;
  41. }
  42. }
  43. // @todo $file is no longer provided. We are supposed to return bash that can be piped to gzip.
  44. // Probably sqlsrv needs to override dump() entirely.
  45. public function dumpCmd($table_selection) {
  46. if (!$file) {
  47. $file = $this->db_spec['database'] . '_' . date('Ymd_His') . '.bak';
  48. }
  49. $exec = "sqlcmd -U \"" . $this->db_spec['username'] . "\" -P \"" . $this->db_spec['password'] . "\" -S \"" . $this->db_spec['host'] . "\" -Q \"BACKUP DATABASE [" . $this->db_spec['database'] . "] TO DISK='" . $file . "'\"";
  50. return array($exec, $file);
  51. }
  52. }