Sqlsqlite.php

  1. 8.0.x lib/Drush/Sql/Sqlsqlite.php
  2. 7.x lib/Drush/Sql/Sqlsqlite.php
  3. master lib/Drush/Sql/Sqlsqlite.php

Namespace

Drush\Sql

Classes

Namesort descending Description
Sqlsqlite

File

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