bc1e025fdcb4b029e85b2afa6b33da31e82c7473
[yaffs-website] / vendor / drush / drush / src / Sql / SqlSqlite.php
1 <?php
2
3 namespace Drush\Sql;
4
5 use Drush\Log\LogLevel;
6
7 class SqlSqlite extends SqlBase
8 {
9     public function command()
10     {
11         return 'sqlite3';
12     }
13
14     public function creds($hide_password = true)
15     {
16         // SQLite doesn't do user management, instead relying on the filesystem
17         // for that. So the only info we really need is the path to the database
18         // file, and not as a "--key=value" parameter.
19         return ' '  .  $this->getDbSpec()['database'];
20     }
21
22     public function createdbSql($dbname, $quoted = false)
23     {
24         return '';
25     }
26
27     /**
28      * Create a new database.
29      *
30      * @param boolean $quoted
31      *   Quote the database name. Mysql uses backticks to quote which can cause problems
32      *   in a Windows shell. Set TRUE if the CREATE is not running on the bash command line.
33      */
34     public function createdb($quoted = false)
35     {
36         $file = $this->getDbSpec()['database'];
37         if (file_exists($file)) {
38             drush_log("SQLITE: Deleting existing database '$file'", LogLevel::DEBUG);
39             drush_delete_dir($file, true);
40         }
41
42         // Make sure sqlite can create file
43         $path = dirname($file);
44         drush_log("SQLITE: creating '$path' for creating '$file'", LogLevel::DEBUG);
45         drush_mkdir($path);
46         if (!file_exists($path)) {
47             drush_log("SQLITE: Cannot create $path", LogLevel::ERROR);
48             return false;
49         } else {
50             return true;
51         }
52     }
53
54     public function dbExists()
55     {
56         return file_exists($this->getDbSpec()['database']);
57     }
58
59     public function listTables()
60     {
61         $return = $this->alwaysQuery('.tables');
62         $tables_raw = drush_shell_exec_output();
63         // SQLite's '.tables' command always outputs the table names in a column
64         // format, like this:
65         // table_alpha    table_charlie    table_echo
66         // table_bravo    table_delta      table_foxtrot
67         // â€¦and there doesn't seem to be a way to fix that. So we need to do some
68         // clean-up.
69         $tables = [];
70         foreach ($tables_raw as $line) {
71             preg_match_all('/[^\s]+/', $line, $matches);
72             if (!empty($matches[0])) {
73                 foreach ($matches[0] as $match) {
74                     $tables[] = $match;
75                 }
76             }
77         }
78         return $tables;
79     }
80
81     public function drop($tables)
82     {
83         $return = true;
84         $sql = '';
85         if ($tables) {
86             // SQLite only wants one table per DROP TABLE command (so we have to do
87             // "DROP TABLE foo; DROP TABLE bar;" instead of "DROP TABLE foo, bar;").
88             foreach ($tables as $table) {
89                 $sql .= "DROP TABLE $table; ";
90             }
91             $return = $this->query($sql);
92         }
93         return $return;
94     }
95
96     public function dumpCmd($table_selection)
97     {
98         // Dumping is usually not necessary in SQLite, since all database data
99         // is stored in a single file which can be copied just
100         // like any other file. But it still has a use in migration purposes and
101         // building human-readable diffs and such, so let's do it anyway.
102         $exec = $this->connect();
103         // SQLite's dump command doesn't support many of the features of its
104         // Postgres or MySQL equivalents. We may be able to fake some in the
105         // future, but for now, let's just support simple dumps.
106         $exec .= ' ".dump"';
107         if ($option = $this->getOption('extra-dump')) {
108             $exec .= " $option";
109         }
110         return $exec;
111     }
112 }