Pgsql.php
184 lines
| 1 | <?php |
| 2 | /** |
| 3 | * Piwik - free/libre analytics platform |
| 4 | * |
| 5 | * @link https://matomo.org |
| 6 | * @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later |
| 7 | * |
| 8 | */ |
| 9 | namespace Piwik\Db\Adapter\Pdo; |
| 10 | |
| 11 | use Exception; |
| 12 | use PDO; |
| 13 | use PDOException; |
| 14 | use Piwik\Config; |
| 15 | use Piwik\Db\AdapterInterface; |
| 16 | use Piwik\Piwik; |
| 17 | use Zend_Db_Adapter_Pdo_Pgsql; |
| 18 | |
| 19 | /** |
| 20 | */ |
| 21 | class Pgsql extends Zend_Db_Adapter_Pdo_Pgsql implements AdapterInterface |
| 22 | { |
| 23 | /** |
| 24 | * Reset the configuration variables in this adapter. |
| 25 | */ |
| 26 | public function resetConfig() |
| 27 | { |
| 28 | $this->_config = array(); |
| 29 | } |
| 30 | |
| 31 | /** |
| 32 | * Return default port. |
| 33 | * |
| 34 | * @return int |
| 35 | */ |
| 36 | public static function getDefaultPort() |
| 37 | { |
| 38 | return 5432; |
| 39 | } |
| 40 | |
| 41 | /** |
| 42 | * Check PostgreSQL version |
| 43 | * |
| 44 | * @throws Exception |
| 45 | */ |
| 46 | public function checkServerVersion() |
| 47 | { |
| 48 | $databaseVersion = $this->getServerVersion(); |
| 49 | $requiredVersion = Config::getInstance()->General['minimum_pgsql_version']; |
| 50 | |
| 51 | if (version_compare($databaseVersion, $requiredVersion) === -1) { |
| 52 | throw new Exception(Piwik::translate('General_ExceptionDatabaseVersion', array('PostgreSQL', $databaseVersion, $requiredVersion))); |
| 53 | } |
| 54 | } |
| 55 | |
| 56 | /** |
| 57 | * Check client version compatibility against database server |
| 58 | */ |
| 59 | public function checkClientVersion() |
| 60 | { |
| 61 | } |
| 62 | |
| 63 | /** |
| 64 | * Returns true if this adapter's required extensions are enabled |
| 65 | * |
| 66 | * @return bool |
| 67 | */ |
| 68 | public static function isEnabled() |
| 69 | { |
| 70 | return extension_loaded('PDO') && extension_loaded('pdo_pgsql'); |
| 71 | } |
| 72 | |
| 73 | /** |
| 74 | * Returns true if this adapter supports blobs as fields |
| 75 | * |
| 76 | * @return bool |
| 77 | */ |
| 78 | public function hasBlobDataType() |
| 79 | { |
| 80 | // large objects must be loaded from a file using a non-SQL API |
| 81 | // and then referenced by the object ID (oid); |
| 82 | // the alternative, bytea fields, incur a space and time |
| 83 | // penalty for encoding/decoding |
| 84 | return false; |
| 85 | } |
| 86 | |
| 87 | /** |
| 88 | * Returns true if this adapter supports bulk loading |
| 89 | * |
| 90 | * @return bool |
| 91 | */ |
| 92 | public function hasBulkLoader() |
| 93 | { |
| 94 | /** |
| 95 | * COPY ? |
| 96 | * |
| 97 | * @link http://www.postgresql.org/docs/current/interactive/sql-copy.html |
| 98 | */ |
| 99 | return false; |
| 100 | } |
| 101 | |
| 102 | /** |
| 103 | * Test error number |
| 104 | * |
| 105 | * @param Exception $e |
| 106 | * @param string $errno |
| 107 | * @return bool |
| 108 | */ |
| 109 | public function isErrNo($e, $errno) |
| 110 | { |
| 111 | // map MySQL driver-specific error codes to PostgreSQL SQLSTATE |
| 112 | $map = array( |
| 113 | // MySQL: Unknown database '%s' |
| 114 | // PostgreSQL: database "%s" does not exist |
| 115 | '1049' => '08006', |
| 116 | |
| 117 | // MySQL: Table '%s' already exists |
| 118 | // PostgreSQL: relation "%s" already exists |
| 119 | '1050' => '42P07', |
| 120 | |
| 121 | // MySQL: Unknown column '%s' in '%s' |
| 122 | // PostgreSQL: column "%s" does not exist |
| 123 | '1054' => '42703', |
| 124 | |
| 125 | // MySQL: Duplicate column name '%s' |
| 126 | // PostgreSQL: column "%s" of relation "%s" already exists |
| 127 | '1060' => '42701', |
| 128 | |
| 129 | // MySQL: Duplicate key name '%s' |
| 130 | // PostgreSQL: relation "%s" already exists |
| 131 | '1061' => '42P07', |
| 132 | |
| 133 | // MySQL: Duplicate entry '%s' for key '%s' |
| 134 | // PostgreSQL: duplicate key violates unique constraint |
| 135 | '1062' => '23505', |
| 136 | |
| 137 | // MySQL: Can't DROP '%s'; check that column/key exists |
| 138 | // PostgreSQL: index "%s" does not exist |
| 139 | '1091' => '42704', |
| 140 | |
| 141 | // MySQL: Table '%s.%s' doesn't exist |
| 142 | // PostgreSQL: relation "%s" does not exist |
| 143 | '1146' => '42P01', |
| 144 | ); |
| 145 | |
| 146 | if (preg_match('/([0-9]{2}[0-9P][0-9]{2})/', $e->getMessage(), $match)) { |
| 147 | return $match[1] == $map[$errno]; |
| 148 | } |
| 149 | |
| 150 | return false; |
| 151 | } |
| 152 | |
| 153 | /** |
| 154 | * Is the connection character set equal to utf8? |
| 155 | * |
| 156 | * @return bool |
| 157 | */ |
| 158 | public function isConnectionUTF8() |
| 159 | { |
| 160 | $charset = $this->fetchOne('SHOW client_encoding'); |
| 161 | return strtolower($charset) === 'utf8'; |
| 162 | } |
| 163 | |
| 164 | /** |
| 165 | * Retrieve client version in PHP style |
| 166 | * |
| 167 | * @return string |
| 168 | */ |
| 169 | public function getClientVersion() |
| 170 | { |
| 171 | $this->_connect(); |
| 172 | try { |
| 173 | $version = $this->_connection->getAttribute(PDO::ATTR_CLIENT_VERSION); |
| 174 | $matches = null; |
| 175 | if (preg_match('/((?:[0-9]{1,2}\.){1,3}[0-9]{1,2})/', $version, $matches)) { |
| 176 | return $matches[1]; |
| 177 | } |
| 178 | } catch (PDOException $e) { |
| 179 | // In case of the driver doesn't support getting attributes |
| 180 | } |
| 181 | return null; |
| 182 | } |
| 183 | } |
| 184 |