| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  | /* | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  |  * This file is part of the Pomm package. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  |  * (c) 2014 - 2015 Grégoire HUBERT <[email protected]> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  |  * For the full copyright and license information, please view the LICENSE | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  |  * file that was distributed with this source code. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  | namespace PommProject\Foundation\Inspector; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  | use PommProject\Foundation\Exception\FoundationException; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  | use PommProject\Foundation\ConvertedResultIterator; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  | use PommProject\Foundation\Client\Client; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  | use PommProject\Foundation\Where; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |  * SchemaInspector | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |  * Schema inspector client. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |  * @package     Pomm | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |  * @copyright   2015 Grégoire HUBERT | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |  * @author      Grégoire HUBERT | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |  * @license     X11 {@link http://opensource.org/licenses/mit-license.php} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |  * @see Client | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 28 |  |  |  */ | 
            
                                                                        
                            
            
                                    
            
            
                | 29 |  |  | class SchemaInspector extends Client | 
            
                                                                        
                            
            
                                    
            
            
                | 30 |  |  | { | 
            
                                                                        
                            
            
                                    
            
            
                | 31 |  |  |     use InspectorTrait; | 
            
                                                                        
                            
            
                                    
            
            
                | 32 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 33 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 34 |  |  |      * getClientType | 
            
                                                                        
                            
            
                                    
            
            
                | 35 |  |  |      * | 
            
                                                                        
                            
            
                                    
            
            
                | 36 |  |  |      * @see ClientInterface | 
            
                                                                        
                            
            
                                    
            
            
                | 37 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |     public function getClientType() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |         return 'inspector'; | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 41 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 42 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 43 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 44 |  |  |      * getClientIdentifier | 
            
                                                                        
                            
            
                                    
            
            
                | 45 |  |  |      * | 
            
                                                                        
                            
            
                                    
            
            
                | 46 |  |  |      * @see ClientInterface | 
            
                                                                        
                            
            
                                    
            
            
                | 47 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |     public function getClientIdentifier() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |         return 'schema'; | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 51 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 52 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 53 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 54 |  |  |      * getSchemas | 
            
                                                                        
                            
            
                                    
            
            
                | 55 |  |  |      * | 
            
                                                                        
                            
            
                                    
            
            
                | 56 |  |  |      * Return a list of available schemas in the current database. An | 
            
                                                                        
                            
            
                                    
            
            
                | 57 |  |  |      * additional criteria can be given to filter the results. | 
            
                                                                        
                            
            
                                    
            
            
                | 58 |  |  |      * Criterias can be a combination of: | 
            
                                                                        
                            
            
                                    
            
            
                | 59 |  |  |      * * n schema | 
            
                                                                        
                            
            
                                    
            
            
                | 60 |  |  |      * * d description | 
            
                                                                        
                            
            
                                    
            
            
                | 61 |  |  |      * * o owner | 
            
                                                                        
                            
            
                                    
            
            
                | 62 |  |  |      * | 
            
                                                                        
                            
            
                                    
            
            
                | 63 |  |  |      * If no criteria is provided, all schemas will be returned including | 
            
                                                                        
                            
            
                                    
            
            
                | 64 |  |  |      * system schemas (temporary schema, toast schemas etc.) | 
            
                                                                        
                            
            
                                    
            
            
                | 65 |  |  |      * | 
            
                                                                        
                            
            
                                    
            
            
                | 66 |  |  |      * @param   Where $where | 
            
                                                                        
                            
            
                                    
            
            
                | 67 |  |  |      * @return  ConvertedResultIterator | 
            
                                                                        
                            
            
                                    
            
            
                | 68 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |     public function getSchemas(Where $where = null) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |         $sql = <<<SQL | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  | select | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |     n.nspname     as "name", | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |     n.oid         as "oid", | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |     d.description as "comment", | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |     count(c)      as "relations", | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |     o.rolname     as "owner" | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  | from pg_catalog.pg_namespace n | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |     left join pg_catalog.pg_description d on n.oid = d.objoid | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |     left join pg_catalog.pg_class c on | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |         c.relnamespace = n.oid and c.relkind in ('r', 'v') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |     join pg_catalog.pg_roles o on n.nspowner = o.oid | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  | where {condition} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  | group by 1, 2, 3, 5 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  | order by 1 asc; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  | SQL; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  |         $condition = Where::create() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  |             ->andWhere($where) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  |             ; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  |         return $this->executeSql($sql, $condition); | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 92 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 93 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 94 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 95 |  |  |      * getUserSchemas | 
            
                                                                        
                            
            
                                    
            
            
                | 96 |  |  |      * | 
            
                                                                        
                            
            
                                    
            
            
                | 97 |  |  |      * Return a list of user schema (not pg_* nor information_schema). | 
            
                                                                        
                            
            
                                    
            
            
                | 98 |  |  |      * | 
            
                                                                        
                            
            
                                    
            
            
                | 99 |  |  |      * @param   Where $where | 
            
                                                                        
                            
            
                                    
            
            
                | 100 |  |  |      * @return  ConvertedResultIterator | 
            
                                                                        
                            
            
                                    
            
            
                | 101 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 |  |  |     public function getUserSchemas(Where $where = null) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 |  |  |         $condition = Where::create() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 |  |  |             ->andWhere($where) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 |  |  |             ->andwhere("n.nspname !~ $*", ['^pg_']) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 |  |  |             ->andWhere("n.nspname <> $*", ['information_schema']) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 |  |  |             ; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 |  |  |         return $this->getSchemas($condition); | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 111 |  |  |     } | 
            
                                                        
            
                                    
            
            
                | 112 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 113 |  |  |  |