5 class SqlSqlsrv extends SqlBase
8 // The way you pass a sql file when issueing a query.
9 public $queryFile = '-h -1 -i';
11 public function command()
16 public function creds($hide_password = true)
18 // Some drush commands (e.g. site-install) want to connect to the
19 // server, but not the database. Connect to the built-in database.
20 $dbSpec = $this->getDbSpec();
21 $database = empty($dbSpec['database']) ? 'master' : $dbSpec['database'];
22 // Host and port are optional but have defaults.
23 $host = empty($dbSpec['host']) ? '.\SQLEXPRESS' : $dbSpec['host'];
24 if ($dbSpec['username'] == '') {
25 return ' -S ' . $host . ' -d ' . $database;
27 return ' -S ' . $host . ' -d ' . $database . ' -U ' . $dbSpec['username'] . ' -P ' . $dbSpec['password'];
31 public function dbExists()
33 // TODO: untested, but the gist is here.
34 $dbSpec = $this->getDbSpec();
35 $database = $dbSpec['database'];
36 // Get a new class instance that has no 'database'.
37 $db_spec_no_db = $dbSpec;
38 unset($db_spec_no_db['database']);
39 $sql_no_db = new SqlSqlsrv($db_spec_no_db, $this->getOptions());
40 $query = "if db_id('$database') IS NOT NULL print 1";
41 drush_always_exec($sql_no_db->connect() . ' -Q %s', $query);
42 $output = drush_shell_exec_output();
43 return $output[0] == 1;
46 public function listTables()
48 $return = $this->alwaysQuery('SELECT TABLE_NAME FROM information_schema.tables');
49 $tables = drush_shell_exec_output();
50 if (!empty($tables)) {
51 // Shift off the header of the column of data returned.
57 // @todo $file is no longer provided. We are supposed to return bash that can be piped to gzip.
58 // Probably sqlsrv needs to override dump() entirely.
59 public function dumpCmd($table_selection)
61 $dbSpec = $this->getDbSpec();
63 $file = $dbSpec['database'] . '_' . date('Ymd_His') . '.bak';
65 $exec = "sqlcmd -U \"" . $dbSpec['username'] . "\" -P \"" . $dbSpec['password'] . "\" -S \"" . $dbSpec['host'] . "\" -Q \"BACKUP DATABASE [" . $dbSpec['database'] . "] TO DISK='" . $file . "'\"";
66 if ($option = $this->getOption('extra-dump', $this->queryExtra)) {
69 return [$exec, $file];