getSQLState(); switch ($sqlState) { case '40001': case '40P01': return new Exception\DeadlockException($message, $exception); case '0A000': // Foreign key constraint violations during a TRUNCATE operation // are considered "feature not supported" in PostgreSQL. if (strpos($exception->getMessage(), 'truncate') !== false) { return new Exception\ForeignKeyConstraintViolationException($message, $exception); } break; case '23502': return new Exception\NotNullConstraintViolationException($message, $exception); case '23503': return new Exception\ForeignKeyConstraintViolationException($message, $exception); case '23505': return new Exception\UniqueConstraintViolationException($message, $exception); case '42601': return new Exception\SyntaxErrorException($message, $exception); case '42702': return new Exception\NonUniqueFieldNameException($message, $exception); case '42703': return new Exception\InvalidFieldNameException($message, $exception); case '42P01': return new Exception\TableNotFoundException($message, $exception); case '42P07': return new Exception\TableExistsException($message, $exception); case '08006': return new Exception\ConnectionException($message, $exception); case '7': // Prior to fixing https://bugs.php.net/bug.php?id=64705 (PHP 7.3.22 and PHP 7.4.10), // in some cases (mainly connection errors) the PDO exception wouldn't provide a SQLSTATE via its code. // The exception code would be always set to 7 here. // We have to match against the SQLSTATE in the error message in these cases. if (strpos($exception->getMessage(), 'SQLSTATE[08006]') !== false) { return new Exception\ConnectionException($message, $exception); } break; } return new Exception\DriverException($message, $exception); } /** * {@inheritdoc} */ public function createDatabasePlatformForVersion($version) { if (! preg_match('/^(?P\d+)(?:\.(?P\d+)(?:\.(?P\d+))?)?/', $version, $versionParts)) { throw DBALException::invalidPlatformVersionSpecified( $version, '..' ); } $majorVersion = $versionParts['major']; $minorVersion = $versionParts['minor'] ?? 0; $patchVersion = $versionParts['patch'] ?? 0; $version = $majorVersion . '.' . $minorVersion . '.' . $patchVersion; switch (true) { case version_compare($version, '10.0', '>='): return new PostgreSQL100Platform(); case version_compare($version, '9.4', '>='): return new PostgreSQL94Platform(); case version_compare($version, '9.2', '>='): return new PostgreSQL92Platform(); case version_compare($version, '9.1', '>='): return new PostgreSQL91Platform(); default: return new PostgreSqlPlatform(); } } /** * {@inheritdoc} */ public function getDatabase(Connection $conn) { $params = $conn->getParams(); if (isset($params['dbname'])) { return $params['dbname']; } $database = $conn->query('SELECT CURRENT_DATABASE()')->fetchColumn(); assert($database !== false); return $database; } /** * {@inheritdoc} */ public function getDatabasePlatform() { return new PostgreSqlPlatform(); } /** * {@inheritdoc} */ public function getSchemaManager(Connection $conn) { return new PostgreSqlSchemaManager($conn); } }