Pgsql.php 3.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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_pgsql')) {
  12. class Pgsql extends \PDO
  13. {
  14. public const ATTR_DISABLE_PREPARES = \PDO::PGSQL_ATTR_DISABLE_PREPARES;
  15. public function __construct(string $dsn, ?string $username = null, ?string $password = null, ?array $options = null)
  16. {
  17. parent::__construct($dsn, $username, $password, $options);
  18. if ('pgsql' !== $driver = $this->getAttribute(\PDO::ATTR_DRIVER_NAME)) {
  19. throw new \PDOException(\sprintf('Pdo\Pgsql::__construct() cannot be used for connecting to the "%s" driver', $driver));
  20. }
  21. }
  22. public static function connect(string $dsn, ?string $username = null, ?string $password = null, ?array $options = null): self
  23. {
  24. try {
  25. return new self($dsn, $username, $password, $options);
  26. } catch (\PDOException $e) {
  27. throw preg_match('/^Pdo\\\\Pgsql::__construct\(\) cannot be used for connecting to the "([a-z]+)" driver/', $e->getMessage(), $matches) ? new \PDOException(\sprintf('Pdo\Pgsql::connect() cannot be used for connecting to the "%s" driver', $matches[1])) : $e;
  28. }
  29. }
  30. public function copyFromArray(string $tableName, array $rows, string $separator = "\t", string $nullAs = '\\\\N', ?string $fields = null): bool
  31. {
  32. return $this->pgsqlCopyFromArray($tableName, $rows, $separator, $nullAs, $fields);
  33. }
  34. public function copyFromFile(string $tableName, string $filename, string $separator = "\t", string $nullAs = '\\\\N', ?string $fields = null): bool
  35. {
  36. return $this->pgsqlCopyFromFile($tableName, $filename, $separator, $nullAs, $fields);
  37. }
  38. /**
  39. * @return array|false
  40. */
  41. public function copyToArray(string $tableName, string $separator = "\t", string $nullAs = '\\\\N', ?string $fields = null)
  42. {
  43. return $this->pgsqlCopyToArray($tableName, $separator, $nullAs, $fields);
  44. }
  45. public function copyToFile(string $tableName, string $filename, string $separator = "\t", string $nullAs = '\\\\N', ?string $fields = null): bool
  46. {
  47. return $this->pgsqlCopyToFile($tableName, $filename, $separator, $nullAs, $fields);
  48. }
  49. /**
  50. * @return array|false
  51. */
  52. public function getNotify(int $fetchMode = \PDO::FETCH_DEFAULT, int $timeoutMilliseconds = 0)
  53. {
  54. return $this->pgsqlGetNotify($fetchMode, $timeoutMilliseconds);
  55. }
  56. public function getPid(): int
  57. {
  58. return $this->pgsqlGetPid();
  59. }
  60. /**
  61. * @return string|false
  62. */
  63. public function lobCreate()
  64. {
  65. return $this->pgsqlLOBCreate();
  66. }
  67. /**
  68. * @return resource|false
  69. */
  70. public function lobOpen(string $oid, string $mode = 'rb')
  71. {
  72. return $this->pgsqlLOBOpen($oid, $mode);
  73. }
  74. public function lobUnlink(string $oid): bool
  75. {
  76. return $this->pgsqlLOBUnlink($oid);
  77. }
  78. }
  79. }