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