Sqlite.php 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  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_sqlite')) {
  12. class Sqlite extends \PDO
  13. {
  14. public const ATTR_EXTENDED_RESULT_CODES = \PHP_VERSION_ID >= 70400 ? \PDO::SQLITE_ATTR_EXTENDED_RESULT_CODES : 1002;
  15. public const ATTR_OPEN_FLAGS = \PHP_VERSION_ID >= 70300 ? \PDO::SQLITE_ATTR_OPEN_FLAGS : 1000;
  16. public const ATTR_READONLY_STATEMENT = \PHP_VERSION_ID >= 70400 ? \PDO::SQLITE_ATTR_READONLY_STATEMENT : 1001;
  17. public const DETERMINISTIC = \PDO::SQLITE_DETERMINISTIC;
  18. public const OPEN_READONLY = \PHP_VERSION_ID >= 70300 ? \PDO::SQLITE_OPEN_READONLY : 1;
  19. public const OPEN_READWRITE = \PHP_VERSION_ID >= 70300 ? \PDO::SQLITE_OPEN_READWRITE : 2;
  20. public const OPEN_CREATE = \PHP_VERSION_ID >= 70300 ? \PDO::SQLITE_OPEN_CREATE : 4;
  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 ('sqlite' !== $driver = $this->getAttribute(\PDO::ATTR_DRIVER_NAME)) {
  25. throw new \PDOException(\sprintf('Pdo\Sqlite::__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\\\\Sqlite::__construct\(\) cannot be used for connecting to the "([a-z]+)" driver/', $e->getMessage(), $matches) ? new \PDOException(\sprintf('Pdo\Sqlite::connect() cannot be used for connecting to the "%s" driver', $matches[1])) : $e;
  34. }
  35. }
  36. public function createAggregate(string $name, callable $step, callable $finalize, int $numArgs = -1): bool
  37. {
  38. return $this->sqliteCreateAggregate($name, $step, $finalize, $numArgs);
  39. }
  40. public function createCollation(string $name, callable $callback): bool
  41. {
  42. return $this->sqliteCreateCollation($name, $callback);
  43. }
  44. public function createFunction(string $function_name, callable $callback, int $num_args = -1, int $flags = 0): bool
  45. {
  46. return $this->sqliteCreateFunction($function_name, $callback, $num_args, $flags);
  47. }
  48. }
  49. }