class Sqloracle

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

Namespace

Drush\Sql

Hierarchy

Expanded class hierarchy of Sqloracle

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::db_exists public function
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_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().
Sqloracle::$query_file public property Overrides SqlBase::$query_file
Sqloracle::command public function The unix command used to connect to the database. Overrides SqlBase::command
Sqloracle::createdb_sql public function Build a SQL string for dropping and creating a database. Overrides SqlBase::createdb_sql
Sqloracle::creds public function Build a fragment connection parameters. Overrides SqlBase::creds
Sqloracle::dumpCmd public function Overrides SqlBase::dumpCmd
Sqloracle::listTables public function Extract the name of all existing tables in the given database. Overrides SqlBase::listTables
Sqloracle::query_format public function Overrides SqlBase::query_format

File

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

View source
class Sqloracle extends SqlBase {

  // The way you pass a sql file when issueing a query.
  public $query_file = '@';

  public function command() {
    // use rlwrap if available for readline support
    if ($handle = popen('rlwrap -v', 'r')) {
      $command = 'rlwrap sqlplus';
      pclose($handle);
    }
    else {
      $command = 'sqlplus';
    }
    return $command;
  }

  public function creds() {
    return ' ' . $this->db_spec['username'] . '/' . $this->db_spec['password'] . ($this->db_spec['host'] == 'USETNS' ? '@' . $this->db_spec['database'] : '@//' . $this->db_spec['host'] . ':' . ($db_spec['port'] ? $db_spec['port'] : '1521') . '/' . $this->db_spec['database']);
  }

  public function createdb_sql($dbname) {
    return drush_log("Unable to generate CREATE DATABASE sql for $dbname", LogLevel::ERROR);
  }

  // @todo $suffix = '.sql';
  public function query_format($query) {
    // remove trailing semicolon from query if we have it
    $query = preg_replace('/\;$/', '', $query);

    // some sqlplus settings
    $settings[] = "set TRIM ON";
    $settings[] = "set FEEDBACK OFF";
    $settings[] = "set UNDERLINE OFF";
    $settings[] = "set PAGES 0";
    $settings[] = "set PAGESIZE 50000";

    // are we doing a describe ?
    if (!preg_match('/^ *desc/i', $query)) {
      $settings[] = "set LINESIZE 32767";
    }

    // are we doing a show tables ?
    if (preg_match('/^ *show tables/i', $query)) {
      $settings[] = "set HEADING OFF";
      $query = "select object_name from user_objects where object_type='TABLE' order by object_name asc";
    }

    // create settings string
    $sqlp_settings = implode("\n", $settings) . "\n";

    // important for sqlplus to exit correctly
    return "${sqlp_settings}${query};\nexit;\n";
  }

  public function listTables() {
    $return = $this->query("SELECT TABLE_NAME FROM USER_TABLES WHERE TABLE_NAME NOT IN ('BLOBS','LONG_IDENTIFIERS')");
    $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 Oracle needs to override dump() entirely - http://stackoverflow.com/questions/2236615/oracle-can-imp-exp-go-to-stdin-stdout.
  public function dumpCmd($table_selection) {
    $create_db = drush_get_option('create-db');
    $exec = 'exp ' . $this->creds();
    // Change variable '$file' by reference in order to get drush_log() to report.
    if (!$file) {
      $file = $this->db_spec['username'] . '.dmp';
    }
    $exec .= ' file=' . $file;

    if (!empty($tables)) {
      $exec .= ' tables="(' . implode(',', $tables) . ')"';
    }
    $exec .= ' owner=' . $this->db_spec['username'];
    return array($exec, $file);
  }
}