5 use Drush\Log\LogLevel;
7 class SqlSqlite extends SqlBase
9 public function command()
14 public function creds($hide_password = true)
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'];
22 public function createdbSql($dbname, $quoted = false)
28 * Create a new database.
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.
34 public function createdb($quoted = false)
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);
42 // Make sure sqlite can create file
43 $path = dirname($file);
44 drush_log("SQLITE: creating '$path' for creating '$file'", LogLevel::DEBUG);
46 if (!file_exists($path)) {
47 drush_log("SQLITE: Cannot create $path", LogLevel::ERROR);
54 public function dbExists()
56 return file_exists($this->getDbSpec()['database']);
59 public function listTables()
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
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
70 foreach ($tables_raw as $line) {
71 preg_match_all('/[^\s]+/', $line, $matches);
72 if (!empty($matches[0])) {
73 foreach ($matches[0] as $match) {
81 public function drop($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; ";
91 $return = $this->query($sql);
96 public function dumpCmd($table_selection)
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.
107 if ($option = $this->getOption('extra-dump')) {