class Sqlsqlite

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

Namespace

Drush\Sql

Hierarchy

Expanded class hierarchy of Sqlsqlite

Members

Contains filters are case sensitive
Namesort descending Modifiers Type Description
SqlBase::$db_spec public property
SqlBase::$query_extra public property
SqlBase::$query_file public property
SqlBase::connect public function A string for connecting to a database.
SqlBase::db_spec public function
SqlBase::delete public function
SqlBase::drop_or_create public function Drop all tables (if DB exists) or CREATE target database.
SqlBase::dump public function
SqlBase::dumpFile public function
SqlBase::get_expanded_table_selection public function Get a list of all table names and expand input that may contain wildcards (`*`) if necessary so that the array returned only contains valid table names i.e. actual tables that exist, without a wildcard.
SqlBase::params_to_options public function
SqlBase::query public function Execute a SQL query.
SqlBase::query_format public function
SqlBase::query_prefix public function
SqlBase::scheme public function The active database driver.
SqlBase::silent public function
SqlBase::su public function Adjust DB connection with superuser credentials if provided.
SqlBase::__construct public function Typically, SqlBase objects are contructed via drush_sql_get_class().
Sqlsqlite::command public function The unix command used to connect to the database. Overrides SqlBase::command
Sqlsqlite::createdb public function Create a new database. Overrides SqlBase::createdb
Sqlsqlite::createdb_sql public function Build a SQL string for dropping and creating a database. Overrides SqlBase::createdb_sql
Sqlsqlite::creds public function Build a fragment connection parameters. Overrides SqlBase::creds
Sqlsqlite::db_exists public function Overrides SqlBase::db_exists
Sqlsqlite::drop public function Overrides SqlBase::drop
Sqlsqlite::dumpCmd public function Overrides SqlBase::dumpCmd
Sqlsqlite::listTables public function Extract the name of all existing tables in the given database. Overrides SqlBase::listTables

File

lib/Drush/Sql/Sqlsqlite.php, line 7

View source
class Sqlsqlite extends SqlBase {
  public function command() {
    return 'sqlite3';
  }

  public function creds($hide_password = TRUE) {
    // SQLite doesn't do user management, instead relying on the filesystem
    // for that. So the only info we really need is the path to the database
    // file, and not as a "--key=value" parameter.
    return ' ' . $this->db_spec['database'];
  }

  public function createdb_sql($dbname, $quoted = false) {
    return '';
  }

  /**
   * Create a new database.
   *
   * @param boolean $quoted
   *   Quote the database name. Mysql uses backticks to quote which can cause problems
   *   in a Windows shell. Set TRUE if the CREATE is not running on the bash command line.
   */
  public function createdb($quoted = FALSE) {
    $file = $this->db_spec['database'];
    if (file_exists($file)) {
      drush_log("SQLITE: Deleting existing database '$file'", LogLevel::DEBUG);
      drush_delete_dir($file, TRUE);
    }

    // Make sure sqlite can create file
    $path = dirname($file);
    drush_log("SQLITE: creating '$path' for creating '$file'", LogLevel::DEBUG);
    drush_mkdir($path);
    if (!file_exists($path)) {
      drush_log("SQLITE: Cannot create $path", LogLevel::ERROR);
    }
  }

  public function db_exists() {
    return file_exists($this->db_spec['database']);
  }

  public function listTables() {
    $return = $this->query('.tables');
    $tables_raw = drush_shell_exec_output();
    // SQLite's '.tables' command always outputs the table names in a column
    // format, like this:
    // table_alpha    table_charlie    table_echo
    // table_bravo    table_delta      table_foxtrot
    // …and there doesn't seem to be a way to fix that. So we need to do some
    // clean-up.
    foreach ($tables_raw as $line) {
      preg_match_all('/[^\s]+/', $line, $matches);
      if (!empty($matches[0])) {
        foreach ($matches[0] as $match) {
          $tables[] = $match;
        }
      }
    }
    return $tables;
  }

  public function drop($tables) {
    $sql = '';
    // SQLite only wants one table per DROP TABLE command (so we have to do
    // "DROP TABLE foo; DROP TABLE bar;" instead of "DROP TABLE foo, bar;").
    foreach ($tables as $table) {
      $sql .= "DROP TABLE $table; ";
    }
    return $this->query($sql);
  }

  public function dumpCmd($table_selection) {
    // Dumping is usually not necessary in SQLite, since all database data
    // is stored in a single file which can be copied just
    // like any other file. But it still has a use in migration purposes and
    // building human-readable diffs and such, so let's do it anyway.
    $exec = $this->connect();
    // SQLite's dump command doesn't support many of the features of its
    // Postgres or MySQL equivalents. We may be able to fake some in the
    // future, but for now, let's just support simple dumps.
    $exec .= ' ".dump"';
    return $exec;
  }


}