Skip to content

Commit 9a04ff5

Browse files
committed
Connection: the driver is created in the constructor
1 parent be103e1 commit 9a04ff5

3 files changed

Lines changed: 14 additions & 36 deletions

File tree

src/Database/Connection.php

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ class Connection
2828
public array $onQuery = [];
2929
private Drivers\Driver $driver;
3030
private Drivers\Engine $engine;
31-
private SqlPreprocessor $preprocessor;
31+
private ?SqlPreprocessor $preprocessor;
3232
private ?PDO $pdo = null;
3333

3434
/** @var callable(array, Result): array */
@@ -39,7 +39,6 @@ class Connection
3939

4040
public function __construct(
4141
private readonly string $dsn,
42-
#[\SensitiveParameter]
4342
private readonly ?string $user = null,
4443
#[\SensitiveParameter]
4544
private readonly ?string $password = null,
@@ -48,7 +47,11 @@ public function __construct(
4847
if (($options['newDateTime'] ?? null) === false) {
4948
$this->rowNormalizer = fn($row, $resultSet) => Helpers::normalizeRow($row, $resultSet, DateTime::class);
5049
}
51-
if (empty($options['lazy'])) {
50+
$lazy = $options['lazy'] ?? false;
51+
unset($options['newDateTime'], $options['lazy']);
52+
53+
$this->driver = (new Factory)->createDriverFromDsn($dsn, $user, $password, $options);
54+
if (!$lazy) {
5255
$this->connect();
5356
}
5457
}
@@ -66,9 +69,7 @@ public function connect(): void
6669
throw ConnectionException::from($e);
6770
}
6871

69-
$this->driver = (new Factory)->createDriverFromDsn($this->dsn, $this->user, $this->password, $this->options);
7072
$this->engine = $this->driver->createDatabaseEngine();
71-
$this->preprocessor = new SqlPreprocessor($this);
7273
$this->engine->initialize($this, $this->options);
7374
Arrays::invoke($this->onConnect, $this);
7475
}
@@ -241,6 +242,7 @@ public function queryArgs(string $sql, array $params): Result
241242
public function preprocess(string $sql, ...$params): array
242243
{
243244
$this->connect();
245+
$this->preprocessor ??= new SqlPreprocessor($this);
244246
return $params
245247
? $this->preprocessor->process(func_get_args())
246248
: [$sql, []];

tests/Database/Connection.exceptions.phpt

Lines changed: 0 additions & 22 deletions
This file was deleted.

tests/Database/connection.option.lazy.phpt

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -16,30 +16,28 @@ require __DIR__ . '/../bootstrap.php';
1616

1717
test('non lazy', function () {
1818
Assert::exception(
19-
fn() => new Nette\Database\Connection('dsn', 'user', 'password'),
20-
Nette\Database\DriverException::class,
21-
'%a%valid data source %a%',
19+
fn() => new Nette\Database\Connection('xxx', 'user', 'password'),
20+
LogicException::class,
21+
"Unknown PDO driver 'xxx'.",
2222
);
2323
});
2424

2525

26-
test('lazy', function () {
27-
$connection = new Nette\Database\Connection('dsn', 'user', 'password', ['lazy' => true]);
26+
test('lazy & explorer', function () {
27+
$connection = new Nette\Database\Connection('mysql:', 'user', 'password', ['lazy' => true]);
2828
$explorer = new Nette\Database\Explorer($connection, new Structure($connection, new DevNullStorage));
2929
Assert::exception(
3030
fn() => $explorer->query('SELECT ?', 10),
3131
Nette\Database\DriverException::class,
32-
'%a%valid data source %a%',
3332
);
3433
});
3534

3635

37-
test('', function () {
38-
$connection = new Nette\Database\Connection('dsn', 'user', 'password', ['lazy' => true]);
36+
test('lazy', function () {
37+
$connection = new Nette\Database\Connection('mysql:', 'user', 'password', ['lazy' => true]);
3938
Assert::exception(
4039
fn() => $connection->quote('x'),
4140
Nette\Database\DriverException::class,
42-
'%a%valid data source %a%',
4341
);
4442
});
4543

0 commit comments

Comments
 (0)