class Sqlsqlsrv

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

Namespace

Drush\Sql

Hierarchy

Expanded class hierarchy of Sqlsqlsrv

Members

Contains filters are case sensitive
Namesort descending Modifiers Type Description
SqlBase::$db_spec public property
SqlBase::$query_extra public property
SqlBase::connect public function A string for connecting to a database.
SqlBase::createdb public function Create a new database.
SqlBase::createdb_sql public function Build a SQL string for dropping and creating a database.
SqlBase::db_spec public function
SqlBase::delete public function
SqlBase::drop 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().
Sqlsqlsrv::$query_file public property Overrides SqlBase::$query_file
Sqlsqlsrv::command public function The unix command used to connect to the database. Overrides SqlBase::command
Sqlsqlsrv::creds public function Build a fragment connection parameters. Overrides SqlBase::creds
Sqlsqlsrv::db_exists public function Overrides SqlBase::db_exists
Sqlsqlsrv::dumpCmd public function Overrides SqlBase::dumpCmd
Sqlsqlsrv::listTables public function Extract the name of all existing tables in the given database. Overrides SqlBase::listTables

File

lib/Drush/Sql/Sqlsqlsrv.php, line 5

View source
class Sqlsqlsrv extends SqlBase {

  // The way you pass a sql file when issueing a query.
  public $query_file = '-h -1 -i';

  public function command() {
    return 'sqlcmd';
  }

  public function creds() {
    // Some drush commands (e.g. site-install) want to connect to the
    // server, but not the database.  Connect to the built-in database.
    $database = empty($this->db_spec['database']) ? 'master' : $this->db_spec['database'];
    // Host and port are optional but have defaults.
    $host = empty($this->db_spec['host']) ? '.\SQLEXPRESS' : $this->db_spec['host'];
    if ($this->db_spec['username'] == '') {
      return ' -S ' . $host . ' -d ' . $database;
    }
    else {
      return ' -S ' . $host . ' -d ' . $database . ' -U ' . $this->db_spec['username'] . ' -P ' . $this->db_spec['password'];
    }
  }

  public function db_exists() {
    // TODO: untested, but the gist is here.
    $database = $this->db_spec['database'];
    // Get a new class instance that has no 'database'.
    $db_spec_no_db = $this->db_spec;
    unset($db_spec_no_db['database']);
    $sql_no_db = drush_sql_get_class($db_spec_no_db);
    $query = "if db_id('$database') IS NOT NULL print 1";
    drush_shell_exec($sql_no_db->connect() . ' -Q %s', $query);
    $output = drush_shell_exec_output();
    return $output[0] == 1;
  }

  public function listTables() {
    $return = $this->query('SELECT TABLE_NAME FROM information_schema.tables');
    $tables = drush_shell_exec_output();
    if (!empty($tables)) {
      // Shift off the header of the column of data returned.
      array_shift($tables);
      return $tables;
    }
  }

  // @todo $file is no longer provided. We are supposed to return bash that can be piped to gzip.
  // Probably sqlsrv needs to override dump() entirely.
  public function dumpCmd($table_selection) {
    if (!$file) {
      $file = $this->db_spec['database'] . '_' . date('Ymd_His') . '.bak';
    }
    $exec = "sqlcmd -U \"" . $this->db_spec['username'] . "\" -P \"" . $this->db_spec['password'] . "\" -S \"" . $this->db_spec['host'] . "\" -Q \"BACKUP DATABASE [" . $this->db_spec['database'] . "] TO DISK='" . $file . "'\"";
    return array($exec, $file);
  }


}