Odbc.php 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  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 Pdo;
  11. if (\PHP_VERSION_ID < 80400 && \extension_loaded('pdo_odbc')) {
  12. class Odbc extends \PDO
  13. {
  14. public const ATTR_USE_CURSOR_LIBRARY = \PDO::ODBC_ATTR_USE_CURSOR_LIBRARY;
  15. public const ATTR_ASSUME_UTF8 = \PDO::ODBC_ATTR_ASSUME_UTF8;
  16. public const SQL_USE_IF_NEEDED = \PDO::ODBC_SQL_USE_IF_NEEDED;
  17. public const SQL_USE_DRIVER = \PDO::ODBC_SQL_USE_DRIVER;
  18. public const SQL_USE_ODBC = \PDO::ODBC_SQL_USE_ODBC;
  19. public function __construct(string $dsn, ?string $username = null, ?string $password = null, ?array $options = null)
  20. {
  21. parent::__construct($dsn, $username, $password, $options);
  22. if ('odbc' !== $driver = $this->getAttribute(\PDO::ATTR_DRIVER_NAME)) {
  23. throw new \PDOException(\sprintf('Pdo\Odbc::__construct() cannot be used for connecting to the "%s" driver', $driver));
  24. }
  25. }
  26. public static function connect(string $dsn, ?string $username = null, ?string $password = null, ?array $options = null): self
  27. {
  28. try {
  29. return new self($dsn, $username, $password, $options);
  30. } catch (\PDOException $e) {
  31. throw preg_match('/^Pdo\\\\Odbc::__construct\(\) cannot be used for connecting to the "([a-z]+)" driver/', $e->getMessage(), $matches) ? new \PDOException(\sprintf('Pdo\Odbc::connect() cannot be used for connecting to the "%s" driver', $matches[1])) : $e;
  32. }
  33. }
  34. }
  35. }