5 class Sqlsqlsrv extends SqlBase {
7 // The way you pass a sql file when issueing a query.
8 public $query_file = '-h -1 -i';
10 public function command() {
14 public function creds() {
15 // Some drush commands (e.g. site-install) want to connect to the
16 // server, but not the database. Connect to the built-in database.
17 $database = empty($this->db_spec['database']) ? 'master' : $this->db_spec['database'];
18 // Host and port are optional but have defaults.
19 $host = empty($this->db_spec['host']) ? '.\SQLEXPRESS' : $this->db_spec['host'];
20 if ($this->db_spec['username'] == '') {
21 return ' -S ' . $host . ' -d ' . $database;
24 return ' -S ' . $host . ' -d ' . $database . ' -U ' . $this->db_spec['username'] . ' -P ' . $this->db_spec['password'];
28 public function db_exists() {
29 // TODO: untested, but the gist is here.
30 $database = $this->db_spec['database'];
31 // Get a new class instance that has no 'database'.
32 $db_spec_no_db = $this->db_spec;
33 unset($db_spec_no_db['database']);
34 $sql_no_db = drush_sql_get_class($db_spec_no_db);
35 $query = "if db_id('$database') IS NOT NULL print 1";
36 drush_shell_exec($sql_no_db->connect() . ' -Q %s', $query);
37 $output = drush_shell_exec_output();
38 return $output[0] == 1;
41 public function listTables() {
42 $return = $this->query('SELECT TABLE_NAME FROM information_schema.tables');
43 $tables = drush_shell_exec_output();
44 if (!empty($tables)) {
45 // Shift off the header of the column of data returned.
51 // @todo $file is no longer provided. We are supposed to return bash that can be piped to gzip.
52 // Probably sqlsrv needs to override dump() entirely.
53 public function dumpCmd($table_selection) {
55 $file = $this->db_spec['database'] . '_' . date('Ymd_His') . '.bak';
57 $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 . "'\"";
58 if ($option = drush_get_option('extra', $this->query_extra)) {
61 return array($exec, $file);