5 use Drush\Log\LogLevel;
7 class Sqloracle extends SqlBase {
9 // The way you pass a sql file when issueing a query.
10 public $query_file = '@';
12 public function command() {
13 // use rlwrap if available for readline support
14 if ($handle = popen('rlwrap -v', 'r')) {
15 $command = 'rlwrap sqlplus';
24 public function creds() {
25 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']);
28 public function createdb_sql($dbname) {
29 return drush_log("Unable to generate CREATE DATABASE sql for $dbname", LogLevel::ERROR);
32 // @todo $suffix = '.sql';
33 public function query_format($query) {
34 // remove trailing semicolon from query if we have it
35 $query = preg_replace('/\;$/', '', $query);
37 // some sqlplus settings
38 $settings[] = "set TRIM ON";
39 $settings[] = "set FEEDBACK OFF";
40 $settings[] = "set UNDERLINE OFF";
41 $settings[] = "set PAGES 0";
42 $settings[] = "set PAGESIZE 50000";
44 // are we doing a describe ?
45 if (!preg_match('/^ *desc/i', $query)) {
46 $settings[] = "set LINESIZE 32767";
49 // are we doing a show tables ?
50 if (preg_match('/^ *show tables/i', $query)) {
51 $settings[] = "set HEADING OFF";
52 $query = "select object_name from user_objects where object_type='TABLE' order by object_name asc";
55 // create settings string
56 $sqlp_settings = implode("\n", $settings) . "\n";
58 // important for sqlplus to exit correctly
59 return "${sqlp_settings}${query};\nexit;\n";
62 public function listTables() {
63 $return = $this->query("SELECT TABLE_NAME FROM USER_TABLES WHERE TABLE_NAME NOT IN ('BLOBS','LONG_IDENTIFIERS')");
64 $tables = drush_shell_exec_output();
65 if (!empty($tables)) {
66 // Shift off the header of the column of data returned.
72 // @todo $file is no longer provided. We are supposed to return bash that can be piped to gzip.
73 // Probably Oracle needs to override dump() entirely - http://stackoverflow.com/questions/2236615/oracle-can-imp-exp-go-to-stdin-stdout.
74 public function dumpCmd($table_selection) {
75 $create_db = drush_get_option('create-db');
76 $exec = 'exp ' . $this->creds();
77 // Change variable '$file' by reference in order to get drush_log() to report.
79 $file = $this->db_spec['username'] . '.dmp';
81 $exec .= ' file=' . $file;
83 if (!empty($tables)) {
84 $exec .= ' tables="(' . implode(',', $tables) . ')"';
86 $exec .= ' owner=' . $this->db_spec['username'];
87 if ($option = drush_get_option('extra', $this->query_extra)) {
90 return array($exec, $file);