5 use Drush\Log\LogLevel;
7 class Sqlsqlite extends SqlBase {
8 public function command() {
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'];
19 public function createdb_sql($dbname, $quoted = false) {
24 * Create a new database.
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.
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);
37 // Make sure sqlite can create file
38 $path = dirname($file);
39 drush_log("SQLITE: creating '$path' for creating '$file'", LogLevel::DEBUG);
41 if (!file_exists($path)) {
42 drush_log("SQLITE: Cannot create $path", LogLevel::ERROR);
50 public function db_exists() {
51 return file_exists($this->db_spec['database']);
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
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
63 foreach ($tables_raw as $line) {
64 preg_match_all('/[^\s]+/', $line, $matches);
65 if (!empty($matches[0])) {
66 foreach ($matches[0] as $match) {
74 public function drop($tables) {
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; ";
81 return $this->query($sql);
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.
94 if ($option = drush_get_option('extra', $this->query_extra)) {