5 use Drush\Log\LogLevel;
7 class SqlOracle extends SqlBase
10 // The way you pass a sql file when issueing a query.
11 public $queryFile = '@';
13 public function command()
15 // use rlwrap if available for readline support
16 if ($handle = popen('rlwrap -v', 'r')) {
17 $command = 'rlwrap sqlplus';
25 public function creds($hide_password = true)
27 return ' ' . $this->dbSpec['username'] . '/' . $this->dbSpec['password'] . ($this->dbSpec['host'] == 'USETNS' ? '@' . $this->dbSpec['database'] : '@//' . $this->dbSpec['host'] . ':' . ($db_spec['port'] ? $db_spec['port'] : '1521') . '/' . $this->dbSpec['database']);
30 public function createdbSql($dbname)
32 return drush_log("Unable to generate CREATE DATABASE sql for $dbname", LogLevel::ERROR);
35 // @todo $suffix = '.sql';
36 public function queryFormat($query)
38 // remove trailing semicolon from query if we have it
39 $query = preg_replace('/\;$/', '', $query);
41 // some sqlplus settings
42 $settings[] = "set TRIM ON";
43 $settings[] = "set FEEDBACK OFF";
44 $settings[] = "set UNDERLINE OFF";
45 $settings[] = "set PAGES 0";
46 $settings[] = "set PAGESIZE 50000";
48 // are we doing a describe ?
49 if (!preg_match('/^ *desc/i', $query)) {
50 $settings[] = "set LINESIZE 32767";
53 // are we doing a show tables ?
54 if (preg_match('/^ *show tables/i', $query)) {
55 $settings[] = "set HEADING OFF";
56 $query = "select object_name from user_objects where object_type='TABLE' order by object_name asc";
59 // create settings string
60 $sqlp_settings = implode("\n", $settings) . "\n";
62 // important for sqlplus to exit correctly
63 return "${sqlp_settings}${query};\nexit;\n";
66 public function listTables()
68 $return = $this->alwaysQuery("SELECT TABLE_NAME FROM USER_TABLES WHERE TABLE_NAME NOT IN ('BLOBS','LONG_IDENTIFIERS')");
69 $tables = drush_shell_exec_output();
70 if (!empty($tables)) {
71 // Shift off the header of the column of data returned.
77 // @todo $file is no longer provided. We are supposed to return bash that can be piped to gzip.
78 // Probably Oracle needs to override dump() entirely - http://stackoverflow.com/questions/2236615/oracle-can-imp-exp-go-to-stdin-stdout.
79 public function dumpCmd($table_selection)
81 $create_db = $this->getOption('create-db');
82 $exec = 'exp ' . $this->creds();
83 // Change variable '$file' by reference in order to get drush_log() to report.
85 $file = $this->dbSpec['username'] . '.dmp';
87 $exec .= ' file=' . $file;
89 if (!empty($tables)) {
90 $exec .= ' tables="(' . implode(',', $tables) . ')"';
92 $exec .= ' owner=' . $this->dbSpec['username'];
93 if ($option = $this->getOption('extra-dump', $this->queryExtra)) {
96 return [$exec, $file];