Further Drupal 8.6.4 changes. Some core files were not committed before a commit...
[yaffs-website] / vendor / drush / drush / src / Sql / SqlSqlsrv.php
1 <?php
2
3 namespace Drush\Sql;
4
5 class SqlSqlsrv extends SqlBase
6 {
7
8   // The way you pass a sql file when issueing a query.
9     public $queryFile = '-h -1 -i';
10
11     public function command()
12     {
13         return 'sqlcmd';
14     }
15
16     public function creds($hide_password = true)
17     {
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;
26         } else {
27             return ' -S ' . $host . ' -d ' . $database . ' -U ' . $dbSpec['username'] . ' -P ' . $dbSpec['password'];
28         }
29     }
30
31     public function dbExists()
32     {
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;
44     }
45
46     public function listTables()
47     {
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.
52             array_shift($tables);
53             return $tables;
54         }
55     }
56
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)
60     {
61         $dbSpec = $this->getDbSpec();
62         if (!$file) {
63             $file = $dbSpec['database'] . '_' . date('Ymd_His') . '.bak';
64         }
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)) {
67             $exec .= " $option";
68         }
69         return [$exec, $file];
70     }
71 }