Pgsql.php
117 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 | |
| 10 | namespace Piwik\Tracker\Db\Pdo; |
| 11 | |
| 12 | use Exception; |
| 13 | use PDO; |
| 14 | |
| 15 | /** |
| 16 | * PDO PostgreSQL wrapper |
| 17 | * |
| 18 | */ |
| 19 | class Pgsql extends Mysql |
| 20 | { |
| 21 | /** |
| 22 | * Builds the DB object |
| 23 | * |
| 24 | * @param array $dbInfo |
| 25 | * @param string $driverName |
| 26 | */ |
| 27 | public function __construct($dbInfo, $driverName = 'pgsql') |
| 28 | { |
| 29 | parent::__construct($dbInfo, $driverName); |
| 30 | } |
| 31 | |
| 32 | /** |
| 33 | * Connects to the DB |
| 34 | * |
| 35 | * @throws Exception if there was an error connecting the DB |
| 36 | */ |
| 37 | public function connect() |
| 38 | { |
| 39 | if (self::$profiling) { |
| 40 | $timer = $this->initProfiler(); |
| 41 | } |
| 42 | |
| 43 | $this->connection = new PDO($this->dsn, $this->username, $this->password, $config = array()); |
| 44 | $this->connection->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); |
| 45 | // we may want to setAttribute(PDO::ATTR_TIMEOUT ) to a few seconds (default is 60) in case the DB is locked |
| 46 | // the matomo.php would stay waiting for the database... bad! |
| 47 | // we delete the password from this object "just in case" it could be printed |
| 48 | $this->password = ''; |
| 49 | |
| 50 | if (!empty($this->charset)) { |
| 51 | $sql = "SET NAMES '" . $this->charset . "'"; |
| 52 | $this->connection->exec($sql); |
| 53 | } |
| 54 | |
| 55 | if (self::$profiling && isset($timer)) { |
| 56 | $this->recordQueryProfile('connect', $timer); |
| 57 | } |
| 58 | } |
| 59 | |
| 60 | /** |
| 61 | * Test error number |
| 62 | * |
| 63 | * @param Exception $e |
| 64 | * @param string $errno |
| 65 | * @return bool |
| 66 | */ |
| 67 | public function isErrNo($e, $errno) |
| 68 | { |
| 69 | // map MySQL driver-specific error codes to PostgreSQL SQLSTATE |
| 70 | $map = array( |
| 71 | // MySQL: Unknown database '%s' |
| 72 | // PostgreSQL: database "%s" does not exist |
| 73 | '1049' => '08006', |
| 74 | |
| 75 | // MySQL: Table '%s' already exists |
| 76 | // PostgreSQL: relation "%s" already exists |
| 77 | '1050' => '42P07', |
| 78 | |
| 79 | // MySQL: Unknown column '%s' in '%s' |
| 80 | // PostgreSQL: column "%s" does not exist |
| 81 | '1054' => '42703', |
| 82 | |
| 83 | // MySQL: Duplicate column name '%s' |
| 84 | // PostgreSQL: column "%s" of relation "%s" already exists |
| 85 | '1060' => '42701', |
| 86 | |
| 87 | // MySQL: Duplicate entry '%s' for key '%s' |
| 88 | // PostgreSQL: duplicate key violates unique constraint |
| 89 | '1062' => '23505', |
| 90 | |
| 91 | // MySQL: Can't DROP '%s'; check that column/key exists |
| 92 | // PostgreSQL: index "%s" does not exist |
| 93 | '1091' => '42704', |
| 94 | |
| 95 | // MySQL: Table '%s.%s' doesn't exist |
| 96 | // PostgreSQL: relation "%s" does not exist |
| 97 | '1146' => '42P01', |
| 98 | ); |
| 99 | |
| 100 | if (preg_match('/([0-9]{2}[0-9P][0-9]{2})/', $e->getMessage(), $match)) { |
| 101 | return $match[1] == $map[$errno]; |
| 102 | } |
| 103 | return false; |
| 104 | } |
| 105 | |
| 106 | /** |
| 107 | * Return number of affected rows in last query |
| 108 | * |
| 109 | * @param mixed $queryResult Result from query() |
| 110 | * @return int |
| 111 | */ |
| 112 | public function rowCount($queryResult) |
| 113 | { |
| 114 | return $queryResult->rowCount(); |
| 115 | } |
| 116 | } |
| 117 |