|
| 1 | +<?php declare(strict_types = 1); |
| 2 | + |
| 3 | +namespace Nextras\Dbal\Drivers\PdoSqlite; |
| 4 | + |
| 5 | + |
| 6 | +use DateTimeZone; |
| 7 | +use Exception; |
| 8 | +use Nextras\Dbal\Connection; |
| 9 | +use Nextras\Dbal\Drivers\Exception\ConnectionException; |
| 10 | +use Nextras\Dbal\Drivers\Exception\DriverException; |
| 11 | +use Nextras\Dbal\Drivers\Exception\ForeignKeyConstraintViolationException; |
| 12 | +use Nextras\Dbal\Drivers\Exception\NotNullConstraintViolationException; |
| 13 | +use Nextras\Dbal\Drivers\Exception\QueryException; |
| 14 | +use Nextras\Dbal\Drivers\Exception\UniqueConstraintViolationException; |
| 15 | +use Nextras\Dbal\Drivers\Pdo\PdoDriver; |
| 16 | +use Nextras\Dbal\Exception\NotSupportedException; |
| 17 | +use Nextras\Dbal\IConnection; |
| 18 | +use Nextras\Dbal\ILogger; |
| 19 | +use Nextras\Dbal\Platforms\IPlatform; |
| 20 | +use Nextras\Dbal\Platforms\SqlitePlatform; |
| 21 | +use Nextras\Dbal\Result\IResultAdapter; |
| 22 | +use PDOStatement; |
| 23 | + |
| 24 | + |
| 25 | +/** |
| 26 | + * Driver for php_pdo_sqlite ext. |
| 27 | + * |
| 28 | + * Supported configuration options: |
| 29 | + * - filename - file path to database or `:memory:`; defaults to :memory: |
| 30 | + */ |
| 31 | +class PdoSqliteDriver extends PdoDriver |
| 32 | +{ |
| 33 | + /** @var PdoSqliteResultNormalizerFactory */ |
| 34 | + private $resultNormalizerFactory; |
| 35 | + |
| 36 | + |
| 37 | + public function connect(array $params, ILogger $logger): void |
| 38 | + { |
| 39 | + $file = $params['filename'] ?? ':memory:'; |
| 40 | + $dsn = "sqlite:$file"; |
| 41 | + $this->connectPdo($dsn, '', '', [], $logger); |
| 42 | + $this->resultNormalizerFactory = new PdoSqliteResultNormalizerFactory(); |
| 43 | + |
| 44 | + $this->connectionTz = new DateTimeZone('UTC'); |
| 45 | + $this->loggedQuery('PRAGMA foreign_keys = 1'); |
| 46 | + } |
| 47 | + |
| 48 | + |
| 49 | + public function createPlatform(IConnection $connection): IPlatform |
| 50 | + { |
| 51 | + return new SqlitePlatform($connection); |
| 52 | + } |
| 53 | + |
| 54 | + |
| 55 | + public function getLastInsertedId(?string $sequenceName = null) |
| 56 | + { |
| 57 | + return $this->query('SELECT last_insert_rowid()')->fetchField(); |
| 58 | + } |
| 59 | + |
| 60 | + |
| 61 | + public function setTransactionIsolationLevel(int $level): void |
| 62 | + { |
| 63 | + static $levels = [ |
| 64 | + Connection::TRANSACTION_READ_UNCOMMITTED => 'READ UNCOMMITTED', |
| 65 | + Connection::TRANSACTION_READ_COMMITTED => 'READ COMMITTED', |
| 66 | + Connection::TRANSACTION_REPEATABLE_READ => 'REPEATABLE READ', |
| 67 | + Connection::TRANSACTION_SERIALIZABLE => 'SERIALIZABLE', |
| 68 | + ]; |
| 69 | + if (!isset($levels[$level])) { |
| 70 | + throw new NotSupportedException("Unsupported transaction level $level"); |
| 71 | + } |
| 72 | + $this->loggedQuery("SET SESSION TRANSACTION ISOLATION LEVEL {$levels[$level]}"); |
| 73 | + } |
| 74 | + |
| 75 | + |
| 76 | + protected function createResultAdapter(PDOStatement $statement): IResultAdapter |
| 77 | + { |
| 78 | + return (new PdoSqliteResultAdapter($statement, $this->resultNormalizerFactory))->toBuffered(); |
| 79 | + } |
| 80 | + |
| 81 | + |
| 82 | + protected function convertIdentifierToSql(string $identifier): string |
| 83 | + { |
| 84 | + return '[' . strtr($identifier, '[]', ' ') . ']'; |
| 85 | + } |
| 86 | + |
| 87 | + |
| 88 | + protected function createException(string $error, int $errorNo, string $sqlState, ?string $query = null): Exception |
| 89 | + { |
| 90 | + if (stripos($error, 'FOREIGN KEY constraint failed') !== false) { |
| 91 | + return new ForeignKeyConstraintViolationException($error, $errorNo, '', null, $query); |
| 92 | + } elseif ( |
| 93 | + strpos($error, 'must be unique') !== false |
| 94 | + || strpos($error, 'is not unique') !== false |
| 95 | + || strpos($error, 'are not unique') !== false |
| 96 | + || strpos($error, 'UNIQUE constraint failed') !== false |
| 97 | + ) { |
| 98 | + return new UniqueConstraintViolationException($error, $errorNo, '', null, $query); |
| 99 | + } elseif ( |
| 100 | + strpos($error, 'may not be NULL') !== false |
| 101 | + || strpos($error, 'NOT NULL constraint failed') !== false |
| 102 | + ) { |
| 103 | + return new NotNullConstraintViolationException($error, $errorNo, '', null, $query); |
| 104 | + } elseif (stripos($error, 'unable to open database') !== false) { |
| 105 | + return new ConnectionException($error, $errorNo, ''); |
| 106 | + } elseif ($query !== null) { |
| 107 | + return new QueryException($error, $errorNo, '', null, $query); |
| 108 | + } else { |
| 109 | + return new DriverException($error, $errorNo, ''); |
| 110 | + } |
| 111 | + } |
| 112 | +} |
0 commit comments