| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | namespace Db3v4l\Core\DatabaseManager; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | use Db3v4l\API\Interfaces\DatabaseManager; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  | use Db3v4l\API\Interfaces\SqlExecutor\Executor; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | use Db3v4l\Core\SqlAction\Command; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  |  * This DBManager uses PDB as 'database'. As such, it does not support oracle <= 11 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  |  * Also, Oracle XE has a fairly stringent limit on PDBS that can be concurrently created (3 total in 18c...) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  |  */ | 
                            
                    |  |  |  | 
                                                                                        
                                                                                            
                                                                                            
                                                                                            
                                                                                            
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  | class OraclePDB extends BaseManager implements DatabaseManager | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |      * Returns the sql 'action' used to list all available databases | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |      * @return Command | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |      * @todo (for each database) retrieve the charset/collation | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |     public function getListDatabasesSqlAction() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |         return new Command( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |             "SELECT pdb_name AS Database FROM dba_pdbs ORDER BY pdb_name;", | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |             function ($output, $executor) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |                 /** @var Executor $executor */ | 
                            
                    |  |  |  | 
                                                                                        
                                                                                            
                                                                                            
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |                 return $executor->resultSetToArray($output); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |         ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |      * Returns the sql 'action' used to create a new db and accompanying user. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |      * NB: the 'new database' is created as pdb and the new user is created as local to the pdb | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |      * NB: needs SYS connection... | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |      * @param string $dbName | 
                            
                    |  |  |  | 
                                                                                        
                                                                                            
                                                                                            
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |      * @param string $userName | 
                            
                    |  |  |  | 
                                                                                        
                                                                                            
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |      * @param string $password | 
                            
                    |  |  |  | 
                                                                                        
                                                                                            
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |      * @param string $charset charset/collation name | 
                            
                    |  |  |  | 
                                                                                        
                                                                                            
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |      * @return Command | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |      * @throws \Exception | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |      * @todo prevent sql injection! | 
                            
                    |  |  |  | 
                                                                                        
                                                                                            
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |     public function getCreateDatabaseSqlAction($dbName, $userName, $password, $charset = null) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |         //$collation = $this->getCollationName($charset); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |         /// @todo throw if $charset is not the same as the db one | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |         if ($userName == '') { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |             throw new \Exception("Can not create a PDB database on Oracle without an accompanying user"); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |         $statements = [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |             "CREATE PLUGGABLE DATABASE \"$dbName\" ADMIN USER $userName IDENTIFIED BY \"$password\" CREATE_FILE_DEST='/opt/oracle/oradata';", | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |             "ALTER PLUGGABLE DATABASE \"$dbName\" OPEN READ WRITE;" | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |         ]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |         return new Command($statements); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |      * Returns the sql 'action' used to drop a db and associated user account | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |      * @param string $dbName | 
                            
                    |  |  |  | 
                                                                                        
                                                                                            
                                                                                            
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |      * @param string $userName | 
                            
                    |  |  |  | 
                                                                                        
                                                                                            
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |      * @return Command | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |      * @param bool $ifExists | 
                            
                    |  |  |  | 
                                                                                        
                                                                                            
                                                                                            
                                                                                            
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |      * @todo prevent sql injection! | 
                            
                    |  |  |  | 
                                                                                        
                                                                                            
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |     public function getDropDatabaseSqlAction($dbName, $userName, $ifExists = false) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |         if ($userName != '') { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |             /// @todo check if $userName is not local to $dbName | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |             ///       select pdb_name from cdb_users, dba_pdbs where dba_pdbs.pdb_id = cdb_users.con_id and username = xxx | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |         /// @todo check support for IF EXISTS | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |         $statements = [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |             "ALTER PLUGGABLE DATABASE \"$dbName\" CLOSE;", | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |             "DROP PLUGGABLE DATABASE \"$dbName\" INCLUDING DATAFILES;", | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |         ]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |         return new Command($statements); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  |      * Returns the sql 'action' used to list all existing db users | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  |      * @return Command | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 88 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 89 |  |  |     public function getListUsersSqlAction() | 
            
                                                                        
                            
            
                                    
            
            
                | 90 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 91 |  |  |         return new Command( | 
            
                                                                        
                            
            
                                    
            
            
                | 92 |  |  |             // NB: we filter out 'system' users, as there are many... | 
            
                                                                        
                            
            
                                    
            
            
                | 93 |  |  |             "SELECT username FROM cdb_users WHERE oracle_maintained != 'Y' ORDER BY username;", | 
            
                                                                        
                            
            
                                    
            
            
                | 94 |  |  |             function ($output, $executor) { | 
            
                                                                        
                            
            
                                    
            
            
                | 95 |  |  |                 /** @var Executor $executor */ | 
                            
                    |  |  |  | 
                                                                                        
                                                                                            
                                                                                            
                                                                                     | 
            
                                                                        
                            
            
                                    
            
            
                | 96 |  |  |                 return $executor->resultSetToArray($output); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 |  |  |         ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 |  |  |     /** | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 |  |  |      * @param string $userName | 
                            
                    |  |  |  | 
                                                                                        
                                                                                            
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 |  |  |      * @param bool $ifExists | 
                            
                    |  |  |  | 
                                                                                        
                                                                                            
                                                                                            
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 |  |  |      * @return Command | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 |  |  |      * @todo prevent sql injection! | 
                            
                    |  |  |  | 
                                                                                        
                                                                                            
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 |  |  |     public function getDropUserSqlAction($userName, $ifExists = false) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 |  |  |         /// @todo check which pdb the user is in, and switch to it | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 |  |  |         ///       select pdb_name from cdb_users, dba_pdbs where dba_pdbs.pdb_id = cdb_users.con_id and username = xxx | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 |  |  |         ///       alter session set container = xxx; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 112 |  |  |         return new Command([ | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 113 |  |  |             "DROP USER $userName CASCADE;", | 
            
                                                                                                            
                            
            
                                    
            
            
                | 114 |  |  |         ]); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 115 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 116 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 117 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 118 |  |  |      * Returns the sql 'action' used to list all available ('valid') collations | 
            
                                                                                                            
                            
            
                                    
            
            
                | 119 |  |  |      * @return Command | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 120 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 121 |  |  |     public function getListCollationsSqlAction() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 122 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 123 |  |  |         return new Command( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 124 |  |  |             "SELECT value AS Collation FROM v\$nls_valid_values WHERE parameter = 'CHARACTERSET' ORDER BY value;", | 
            
                                                                                                            
                            
            
                                    
            
            
                | 125 |  |  |             function ($output, $executor) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 126 |  |  |                 /** @var Executor $executor */ | 
                            
                    |  |  |  | 
                                                                                        
                                                                                            
                                                                                            
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 127 |  |  |                 return $executor->resultSetToArray($output); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 128 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 129 |  |  |         ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 130 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 131 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 132 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 133 |  |  |      * Returns the sql 'action' used to retrieve the db instance version info | 
            
                                                                                                            
                            
            
                                    
            
            
                | 134 |  |  |      * @return Command | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 135 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 136 |  |  |     public function getRetrieveVersionInfoSqlAction() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 137 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 138 |  |  |         return new Command( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 139 |  |  |             "SELECT version_full || ' (' || banner || ')' AS version FROM  v\$version, v\$instance;", | 
            
                                                                                                            
                            
            
                                    
            
            
                | 140 |  |  |             function ($output, $executor) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 141 |  |  |                 /** @var Executor $executor */ | 
                            
                    |  |  |  | 
                                                                                        
                                                                                            
                                                                                            
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 142 |  |  |                 return $executor->resultSetToArray($output)[0]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 143 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 144 |  |  |         ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 145 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 146 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 147 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 148 |  |  |      * Transform collation name into a supported one | 
            
                                                                                                            
                            
            
                                    
            
            
                | 149 |  |  |      * @param null|string $charset so far only 'utf8' is supported... | 
                            
                    |  |  |  | 
                                                                                        
                                                                                            
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 150 |  |  |      * @return null|string | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 151 |  |  |      * @todo what shall we accept as valid input, ie. 'generic' charset names ? maybe do 2 passes: known-db-charset => generic => specific for each db ? | 
                            
                    |  |  |  | 
                                                                                        
                                                                                            
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 152 |  |  |      *       see: https://www.iana.org/assignments/character-sets/character-sets.xhtml for IANA names | 
            
                                                                                                            
                            
            
                                    
            
            
                | 153 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 154 |  |  |     /*protected function getCollationName($charset) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 155 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 156 |  |  |         if ($charset == null) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 157 |  |  |             return null; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 158 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 159 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 160 |  |  |         $charset = trim(strtolower($charset)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 161 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 162 |  |  |         // accept official iana charset name, but most dbs prefer 'utf8'... | 
            
                                                                                                            
                            
            
                                    
            
            
                | 163 |  |  |         if ($charset == 'utf-8') { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 164 |  |  |             $charset = 'utf8'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 165 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 166 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 167 |  |  |         if ($charset == 'utf8') { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 168 |  |  |             $charset = 'AL32UTF8'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 169 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 170 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 171 |  |  |         return $charset; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 172 |  |  |     }*/ | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 173 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 174 |  |  |  |