399c219f98003cdbf23daa0abd9957458d9877e9
[yaffs-website] / vendor / drush / drush / lib / Drush / Sql / Sqloracle.php
1 <?php
2
3 namespace Drush\Sql;
4
5 use Drush\Log\LogLevel;
6
7 class Sqloracle extends SqlBase {
8
9   // The way you pass a sql file when issueing a query.
10   public $query_file = '@';
11
12   public function command() {
13     // use rlwrap if available for readline support
14     if ($handle = popen('rlwrap -v', 'r')) {
15       $command = 'rlwrap sqlplus';
16       pclose($handle);
17     }
18     else {
19       $command = 'sqlplus';
20     }
21     return $command;
22   }
23
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']);
26   }
27
28   public function createdb_sql($dbname) {
29     return drush_log("Unable to generate CREATE DATABASE sql for $dbname", LogLevel::ERROR);
30   }
31
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);
36
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";
43
44     // are we doing a describe ?
45     if (!preg_match('/^ *desc/i', $query)) {
46       $settings[] = "set LINESIZE 32767";
47     }
48
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";
53     }
54
55     // create settings string
56     $sqlp_settings = implode("\n", $settings) . "\n";
57
58     // important for sqlplus to exit correctly
59     return "${sqlp_settings}${query};\nexit;\n";
60   }
61
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.
67       array_shift($tables);
68       return $tables;
69     }
70   }
71
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.
78     if (!$file) {
79       $file = $this->db_spec['username'] . '.dmp';
80     }
81     $exec .= ' file=' . $file;
82
83     if (!empty($tables)) {
84       $exec .= ' tables="(' . implode(',', $tables) . ')"';
85     }
86     $exec .= ' owner=' . $this->db_spec['username'];
87     if ($option = drush_get_option('extra', $this->query_extra)) {
88       $exec .= " $option";
89     }
90     return array($exec, $file);
91   }
92 }