| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  * Adds support for PostgreSQL databases | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  |  * PHP Version 5 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  |  * @category  Core | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  |  * @package   Database | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  |  * @author    Hans-Joachim Piepereit <[email protected]> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  |  * @copyright 2013 cSphere Team | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  |  * @license   http://opensource.org/licenses/bsd-license Simplified BSD License | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |  * @link      http://www.csphere.eu | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  |  **/ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  | namespace csphere\core\database; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |  * Adds support for PostgreSQL databases | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |  * @category  Core | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |  * @package   Database | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |  * @author    Hans-Joachim Piepereit <[email protected]> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |  * @copyright 2013 cSphere Team | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |  * @license   http://opensource.org/licenses/bsd-license Simplified BSD License | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |  * @link      http://www.csphere.eu | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |  **/ | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 28 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 29 |  |  | class Driver_PDO_PGSQL extends Base_PDO | 
            
                                                                        
                            
            
                                    
            
            
                | 30 |  |  | { | 
            
                                                                        
                            
            
                                    
            
            
                | 31 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 32 |  |  |      * Creates the database handler object | 
            
                                                                        
                            
            
                                    
            
            
                | 33 |  |  |      * | 
            
                                                                        
                            
            
                                    
            
            
                | 34 |  |  |      * @param array $config Configuration details as an array | 
            
                                                                        
                            
            
                                    
            
            
                | 35 |  |  |      * | 
            
                                                                        
                            
            
                                    
            
            
                | 36 |  |  |      * @throws \Exception | 
            
                                                                        
                            
            
                                    
            
            
                | 37 |  |  |      * | 
            
                                                                        
                            
            
                                    
            
            
                | 38 |  |  |      * @return \csphere\core\database\Driver_PDO_PGSQL | 
            
                                                                        
                            
            
                                    
            
            
                | 39 |  |  |      **/ | 
                            
                    |  |  |  | 
                                                                                        
                                                                                            
                                                                                     | 
            
                                                                        
                            
            
                                    
            
            
                | 40 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |     public function __construct(array $config) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |         parent::__construct($config); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |         if (!extension_loaded('pdo_pgsql')) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |             throw new \Exception('Extension "pdo_pgsql" not found'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |         $dsn = empty($config['host']) ? '' : | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |                'host=' . $config['host'] . ';'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |         $dsn .= 'dbname=' . $config['schema']; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |         $options = []; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |         // Use try catch to hide connection details | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |         try { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |             $this->con = new \PDO( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |                 'pgsql:' . $dsn, $config['username'], $config['password'], $options | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |             ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |             $this->con->setAttribute(\PDO::ATTR_ERRMODE, \PDO::ERRMODE_EXCEPTION); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |             $this->con->exec('SET client_encoding TO UNICODE'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |         catch(\PDOException $pdo_error) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |             $this->error('Connect', [], $pdo_error->getMessage(), false); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |         } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 72 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 73 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 74 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 75 |  |  |      * Returns the ID of the last insert query | 
            
                                                                        
                            
            
                                    
            
            
                | 76 |  |  |      * | 
            
                                                                        
                            
            
                                    
            
            
                | 77 |  |  |      * @return integer | 
            
                                                                        
                            
            
                                    
            
            
                | 78 |  |  |      **/ | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                        
                            
            
                                    
            
            
                | 79 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |     protected function insertId() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |         // PDO method lastinsertid is not working for PGSQL | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |         $sth = $this->con->query('SELECT LASTVAL()'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |         $last = $sth->fetch(\PDO::FETCH_ASSOC); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  |         $result = isset($last['lastval']) ? $last['lastval'] : 0; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  |         return $result; | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 90 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 91 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 92 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 93 |  |  |      * Builds the query string part for limit and offset | 
            
                                                                        
                            
            
                                    
            
            
                | 94 |  |  |      * | 
            
                                                                        
                            
            
                                    
            
            
                | 95 |  |  |      * @param integer $first Number of the first dataset to show | 
            
                                                                        
                            
            
                                    
            
            
                | 96 |  |  |      * @param integer $max   Number of datasets to show from first on | 
            
                                                                        
                            
            
                                    
            
            
                | 97 |  |  |      * | 
            
                                                                        
                            
            
                                    
            
            
                | 98 |  |  |      * @return string | 
            
                                                                        
                            
            
                                    
            
            
                | 99 |  |  |      **/ | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                        
                            
            
                                    
            
            
                | 100 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 |  |  |     protected function limits($first, $max) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 |  |  |         $string = 'LIMIT ' . (int)$max . ' OFFSET ' . (int)$first; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 |  |  |         return $string; | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 106 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 107 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 108 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 109 |  |  |      * Replaces driver specific query placeholders | 
            
                                                                        
                            
            
                                    
            
            
                | 110 |  |  |      * | 
            
                                                                        
                            
            
                                    
            
            
                | 111 |  |  |      * @param string $replace The string to use for replaces | 
            
                                                                        
                            
            
                                    
            
            
                | 112 |  |  |      * | 
            
                                                                        
                            
            
                                    
            
            
                | 113 |  |  |      * @return string | 
            
                                                                        
                            
            
                                    
            
            
                | 114 |  |  |      **/ | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                        
                            
            
                                    
            
            
                | 115 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 116 |  |  |     protected function replace($replace) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 117 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 118 |  |  |         $change_pgsql = ['{engine}' => '', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 119 |  |  |                          '{integer}' => 'integer', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 120 |  |  |                          '{optimize}' => 'VACUUM', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 121 |  |  |                          '{serial}' => 'serial', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 122 |  |  |                          '{text}' => 'text', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 123 |  |  |                          '{longtext}' => 'text', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 124 |  |  |                          '{varchar}' => 'varchar']; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 125 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 126 |  |  |         foreach ($change_pgsql AS $key => $pgsql) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 127 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 128 |  |  |             $replace = str_replace($key, $pgsql, $replace); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 129 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 130 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 131 |  |  |         return $replace; | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 132 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 133 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 134 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 135 |  |  |      * Returns a formatted array with statistics | 
            
                                                                        
                            
            
                                    
            
            
                | 136 |  |  |      * | 
            
                                                                        
                            
            
                                    
            
            
                | 137 |  |  |      * @return array | 
            
                                                                        
                            
            
                                    
            
            
                | 138 |  |  |      **/ | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                        
                            
            
                                    
            
            
                | 139 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 140 |  |  |     public function info() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 141 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 142 |  |  |         // Get general PDO information | 
            
                                                                                                            
                            
            
                                    
            
            
                | 143 |  |  |         $info = parent::info(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 144 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 145 |  |  |         // Get encoding of database | 
            
                                                                                                            
                            
            
                                    
            
            
                | 146 |  |  |         $query    = 'SHOW SERVER_ENCODING'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 147 |  |  |         $encoding = $this->query($query, [], 0, 0); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 148 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 149 |  |  |         $info['encoding'] = $encoding[0]['server_encoding']; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 150 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 151 |  |  |         // Get size of database | 
            
                                                                                                            
                            
            
                                    
            
            
                | 152 |  |  |         $query = 'SELECT pg_database_size(\'' . $this->config['schema'] | 
            
                                                                                                            
                            
            
                                    
            
            
                | 153 |  |  |                . '\') AS size'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 154 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 155 |  |  |         $size = $this->query($query, [], 0, 0); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 156 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 157 |  |  |         $info['size'] = (int)$size[0]['size']; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 158 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 159 |  |  |         // Get amount of tables | 
            
                                                                                                            
                            
            
                                    
            
            
                | 160 |  |  |         $query = 'SELECT COUNT(*) AS tables FROM information_schema.tables ' | 
            
                                                                                                            
                            
            
                                    
            
            
                | 161 |  |  |                . 'WHERE table_name LIKE \'' . $this->config['schema'] . '%\''; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 162 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 163 |  |  |         $tables = $this->query($query, [], 0, 0); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 164 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 165 |  |  |         $info['tables'] = $tables[0]['tables']; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 166 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 167 |  |  |         return $info; | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 168 |  |  |     } | 
            
                                                        
            
                                    
            
            
                | 169 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 170 |  |  |  | 
            
                        
Adding a
@returnannotation to a constructor is not recommended, since a constructor does not have a meaningful return value.Please refer to the PHP core documentation on constructors.