Skip to content

Commit

Permalink
Connection: the driver is created in the constructor
Browse files Browse the repository at this point in the history
  • Loading branch information
dg committed Aug 19, 2024
1 parent 3fd184e commit 8a20c98
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 36 deletions.
12 changes: 7 additions & 5 deletions src/Database/Connection.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 */
Expand All @@ -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,
Expand All @@ -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();
}
}
Expand All @@ -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);
}
Expand Down Expand Up @@ -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, []];
Expand Down
22 changes: 0 additions & 22 deletions tests/Database/Connection.exceptions.phpt

This file was deleted.

16 changes: 7 additions & 9 deletions tests/Database/Connection.lazy.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -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%',
);
});

Expand Down

0 comments on commit 8a20c98

Please sign in to comment.