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 61624bb commit 4d26b4b
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 39 deletions.
17 changes: 9 additions & 8 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,

Check failure on line 42 in src/Database/Connection.php

View workflow job for this annotation

GitHub Actions / PHPStan

Property Nette\Database\Connection::$user is never read, only written.
#[\SensitiveParameter]

Check failure on line 43 in src/Database/Connection.php

View workflow job for this annotation

GitHub Actions / PHPStan

Property Nette\Database\Connection::$password is never read, only written.
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 @@ -61,15 +64,12 @@ public function connect(): void
}

try {
$this->pdo = new PDO($this->dsn, $this->user, $this->password, $this->options);
$this->pdo = $this->driver->connect();

Check failure on line 67 in src/Database/Connection.php

View workflow job for this annotation

GitHub Actions / PHPStan

Call to an undefined method Nette\Database\Drivers\Driver::connect().
} catch (PDOException $e) {
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);
$this->engine = $this->driver->createDatabaseEngine($this);

Check failure on line 72 in src/Database/Connection.php

View workflow job for this annotation

GitHub Actions / PHPStan

Method Nette\Database\Drivers\Driver::createDatabaseEngine() invoked with 1 parameter, 0 required.
Arrays::invoke($this->onConnect, $this);
}

Expand Down Expand Up @@ -241,6 +241,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 4d26b4b

Please sign in to comment.