Dblib.php 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  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_dblib')) {
  12. class Dblib extends \PDO
  13. {
  14. public const ATTR_CONNECTION_TIMEOUT = \PDO::DBLIB_ATTR_CONNECTION_TIMEOUT;
  15. public const ATTR_QUERY_TIMEOUT = \PDO::DBLIB_ATTR_QUERY_TIMEOUT;
  16. public const ATTR_STRINGIFY_UNIQUEIDENTIFIER = \PDO::DBLIB_ATTR_STRINGIFY_UNIQUEIDENTIFIER;
  17. public const ATTR_VERSION = \PDO::DBLIB_ATTR_VERSION;
  18. public const ATTR_TDS_VERSION = \PHP_VERSION_ID >= 70300 ? \PDO::DBLIB_ATTR_TDS_VERSION : 1004;
  19. public const ATTR_SKIP_EMPTY_ROWSETS = \PHP_VERSION_ID >= 70300 ? \PDO::DBLIB_ATTR_SKIP_EMPTY_ROWSETS : 1005;
  20. public const ATTR_DATETIME_CONVERT = \PHP_VERSION_ID >= 70300 ? \PDO::DBLIB_ATTR_DATETIME_CONVERT : 1006;
  21. public function __construct(string $dsn, ?string $username = null, ?string $password = null, ?array $options = null)
  22. {
  23. parent::__construct($dsn, $username, $password, $options);
  24. if ('dblib' !== $driver = $this->getAttribute(\PDO::ATTR_DRIVER_NAME)) {
  25. throw new \PDOException(\sprintf('Pdo\Dblib::__construct() cannot be used for connecting to the "%s" driver', $driver));
  26. }
  27. }
  28. public static function connect(string $dsn, ?string $username = null, ?string $password = null, ?array $options = null): self
  29. {
  30. try {
  31. return new self($dsn, $username, $password, $options);
  32. } catch (\PDOException $e) {
  33. throw preg_match('/^Pdo\\\\Dblib::__construct\(\) cannot be used for connecting to the "([a-z]+)" driver/', $e->getMessage(), $matches) ? new \PDOException(\sprintf('Pdo\Dblib::connect() cannot be used for connecting to the "%s" driver', $matches[1])) : $e;
  34. }
  35. }
  36. }
  37. }