diff --git a/src/Database/Connection.php b/src/Database/Connection.php index 8c9a6fa41..657d69271 100644 --- a/src/Database/Connection.php +++ b/src/Database/Connection.php @@ -28,7 +28,7 @@ class Connection public array $onQuery = []; private Drivers\Driver $driver; private Drivers\Engine $engine; - private SqlPreprocessor $preprocessor; + private ?SqlPreprocessor $preprocessor; private ?PDO $pdo = null; /** @var callable(array, ResultSet): array */ @@ -39,7 +39,6 @@ class Connection public function __construct( private readonly string $dsn, - #[\SensitiveParameter] private readonly ?string $user = null, #[\SensitiveParameter] private readonly ?string $password = null, @@ -48,7 +47,11 @@ public function __construct( if (($options['newDateTime'] ?? null) === false) { $this->rowNormalizer = fn($row, $resultSet) => Helpers::normalizeRow($row, $resultSet, DateTime::class); } - if (empty($options['lazy'])) { + $lazy = $options['lazy'] ?? false; + unset($options['newDateTime'], $options['lazy']); + + $this->driver = (new Factory)->createDriverFromDsn($dsn, $user, $password, $options); + if (!$lazy) { $this->connect(); } } @@ -66,9 +69,7 @@ public function connect(): void throw ConnectionException::from($e); } - $this->driver = (new Factory)->createDriverFromDsn($this->dsn, $this->user, $this->password, $this->options); $this->engine = $this->driver->createDatabaseEngine(); - $this->preprocessor = new SqlPreprocessor($this); $this->engine->initialize($this, $this->options); Arrays::invoke($this->onConnect, $this); } @@ -241,6 +242,7 @@ public function queryArgs(string $sql, array $params): ResultSet public function preprocess(string $sql, ...$params): array { $this->connect(); + $this->preprocessor ??= new SqlPreprocessor($this); return $params ? $this->preprocessor->process(func_get_args()) : [$sql, []]; diff --git a/tests/Database/Connection.exceptions.phpt b/tests/Database/Connection.exceptions.phpt deleted file mode 100644 index 8efbc2871..000000000 --- a/tests/Database/Connection.exceptions.phpt +++ /dev/null @@ -1,22 +0,0 @@ - new Nette\Database\Connection('unknown'), - Nette\Database\ConnectionException::class, - '%a%valid data source %a%', - 0, -); - -Assert::same(null, $e->getDriverCode()); -Assert::same(null, $e->getSqlState()); diff --git a/tests/Database/Connection.lazy.phpt b/tests/Database/Connection.lazy.phpt index 2c877d389..18260f182 100644 --- a/tests/Database/Connection.lazy.phpt +++ b/tests/Database/Connection.lazy.phpt @@ -16,30 +16,28 @@ require __DIR__ . '/../bootstrap.php'; test('non lazy', function () { Assert::exception( - fn() => new Nette\Database\Connection('dsn', 'user', 'password'), - Nette\Database\DriverException::class, - '%a%valid data source %a%', + fn() => new Nette\Database\Connection('xxx', 'user', 'password'), + LogicException::class, + "Unknown PDO driver 'xxx'.", ); }); -test('lazy', function () { - $connection = new Nette\Database\Connection('dsn', 'user', 'password', ['lazy' => true]); +test('lazy & explorer', function () { + $connection = new Nette\Database\Connection('mysql:', 'user', 'password', ['lazy' => true]); $explorer = new Nette\Database\Explorer($connection, new Structure($connection, new DevNullStorage)); Assert::exception( fn() => $explorer->query('SELECT ?', 10), Nette\Database\DriverException::class, - '%a%valid data source %a%', ); }); -test('', function () { - $connection = new Nette\Database\Connection('dsn', 'user', 'password', ['lazy' => true]); +test('lazy', function () { + $connection = new Nette\Database\Connection('mysql:', 'user', 'password', ['lazy' => true]); Assert::exception( fn() => $connection->quote('x'), Nette\Database\DriverException::class, - '%a%valid data source %a%', ); });