Skip to content

Commit 1297897

Browse files
committed
database connection moved to PdoDriver::connect()
1 parent 98d167f commit 1297897

File tree

10 files changed

+76
-54
lines changed

10 files changed

+76
-54
lines changed

src/Database/Connection.php

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,8 @@ class Connection
2525

2626
/** @var array<callable(self, ResultSet|DriverException): void> Occurs after query is executed */
2727
public array $onQuery = [];
28-
private Driver $driver;
28+
private ?Driver $driver = null;
2929
private SqlPreprocessor $preprocessor;
30-
private ?PDO $pdo = null;
3130

3231
/** @var callable(array, ResultSet): array */
3332
private $rowNormalizer;
@@ -53,23 +52,21 @@ public function __construct(
5352

5453
public function connect(): void
5554
{
56-
if ($this->pdo) {
55+
if ($this->driver) {
5756
return;
5857
}
5958

60-
try {
61-
$this->pdo = new PDO($this->dsn, $this->user, $this->password, $this->options);
62-
$this->pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
63-
} catch (PDOException $e) {
64-
throw ConnectionException::from($e);
65-
}
66-
59+
$dsn = explode(':', $this->dsn)[0];
6760
$class = empty($this->options['driverClass'])
68-
? 'Nette\Database\Drivers\\' . ucfirst(str_replace('sql', 'Sql', $this->pdo->getAttribute(PDO::ATTR_DRIVER_NAME))) . 'Driver'
61+
? 'Nette\Database\Drivers\\' . ucfirst(str_replace('sql', 'Sql', $dsn)) . 'Driver'
6962
: $this->options['driverClass'];
63+
if (!class_exists($class)) {
64+
throw new ConnectionException("Invalid data source '$dsn'.");
65+
}
66+
7067
$this->driver = new $class;
68+
$this->driver->connect($this->dsn, $this->user, $this->password, $this->options);
7169
$this->preprocessor = new SqlPreprocessor($this);
72-
$this->driver->initialize($this, $this->options);
7370
Arrays::invoke($this->onConnect, $this);
7471
}
7572

@@ -83,7 +80,7 @@ public function reconnect(): void
8380

8481
public function disconnect(): void
8582
{
86-
$this->pdo = null;
83+
$this->driver = null;
8784
}
8885

8986

@@ -93,10 +90,11 @@ public function getDsn(): string
9390
}
9491

9592

93+
/** deprecated use getDriver()->getPdo() */
9694
public function getPdo(): PDO
9795
{
9896
$this->connect();
99-
return $this->pdo;
97+
return $this->driver->getPdo();
10098
}
10199

102100

src/Database/Driver.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,9 @@ interface Driver
2525

2626
/**
2727
* Initializes connection.
28+
* @throws ConnectionException
2829
*/
29-
function initialize(Connection $connection, array $options): void;
30+
function connect(string $dsn, ?string $user = null, ?string $password = null, ?array $options = null): void;
3031

3132
/**
3233
* Converts PDOException to DriverException or its descendant.

src/Database/Drivers/MsSqlDriver.php

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -17,15 +17,6 @@
1717
*/
1818
class MsSqlDriver extends PdoDriver
1919
{
20-
private Nette\Database\Connection $connection;
21-
22-
23-
public function initialize(Nette\Database\Connection $connection, array $options): void
24-
{
25-
$this->connection = $connection;
26-
}
27-
28-
2920
public function convertException(\PDOException $e): Nette\Database\DriverException
3021
{
3122
return Nette\Database\DriverException::from($e);

src/Database/Drivers/MySqlDriver.php

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,6 @@ class MySqlDriver extends PdoDriver
3232
public const ERROR_DATA_TRUNCATED = self::ErrorDataTruncated;
3333

3434

35-
private Nette\Database\Connection $connection;
3635
private bool $supportBooleans;
3736

3837

@@ -42,17 +41,23 @@ class MySqlDriver extends PdoDriver
4241
* - sqlmode => see http://dev.mysql.com/doc/refman/5.0/en/server-sql-mode.html
4342
* - supportBooleans => converts INT(1) to boolean
4443
*/
45-
public function initialize(Nette\Database\Connection $connection, array $options): void
44+
public function connect(
45+
string $dsn,
46+
?string $user = null,
47+
#[\SensitiveParameter]
48+
?string $password = null,
49+
?array $options = null,
50+
): void
4651
{
47-
$this->connection = $connection;
52+
parent::connect($dsn, $user, $password, $options);
4853
$charset = $options['charset']
49-
?? (version_compare($connection->getPdo()->getAttribute(\PDO::ATTR_SERVER_VERSION), '5.5.3', '>=') ? 'utf8mb4' : 'utf8');
54+
?? (version_compare($this->pdo->getAttribute(\PDO::ATTR_SERVER_VERSION), '5.5.3', '>=') ? 'utf8mb4' : 'utf8');
5055
if ($charset) {
51-
$connection->query('SET NAMES ?', $charset);
56+
$this->pdo->query('SET NAMES ' . $this->pdo->quote($charset));
5257
}
5358

5459
if (isset($options['sqlmode'])) {
55-
$connection->query('SET sql_mode=?', $options['sqlmode']);
60+
$this->pdo->query('SET sql_mode=' . $this->pdo->quote($options['sqlmode']));
5661
}
5762

5863
$this->supportBooleans = (bool) ($options['supportBooleans'] ?? false);

src/Database/Drivers/OciDriver.php

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,18 @@
1717
*/
1818
class OciDriver extends PdoDriver
1919
{
20-
private Nette\Database\Connection $connection;
2120
private string $fmtDateTime;
2221

2322

24-
public function initialize(Nette\Database\Connection $connection, array $options): void
23+
public function connect(
24+
string $dsn,
25+
?string $user = null,
26+
#[\SensitiveParameter]
27+
?string $password = null,
28+
?array $options = null,
29+
): void
2530
{
26-
$this->connection = $connection;
31+
parent::connect($dsn, $user, $password, $options);
2732
$this->fmtDateTime = $options['formatDateTime'] ?? 'U';
2833
}
2934

src/Database/Drivers/OdbcDriver.php

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,6 @@
1717
*/
1818
class OdbcDriver extends PdoDriver
1919
{
20-
public function initialize(Nette\Database\Connection $connection, array $options): void
21-
{
22-
}
23-
24-
2520
public function convertException(\PDOException $e): Nette\Database\DriverException
2621
{
2722
return Nette\Database\DriverException::from($e);

src/Database/Drivers/PdoDriver.php

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,37 @@
1010
namespace Nette\Database\Drivers;
1111

1212
use Nette;
13+
use PDO;
14+
use PDOException;
1315

1416

1517
/**
1618
* PDO-based driver.
1719
*/
1820
abstract class PdoDriver implements Nette\Database\Driver
1921
{
22+
protected ?PDO $pdo = null;
23+
24+
25+
public function connect(
26+
string $dsn,
27+
?string $user = null,
28+
#[\SensitiveParameter]
29+
?string $password = null,
30+
?array $options = null,
31+
): void
32+
{
33+
try {
34+
$this->pdo = new PDO($dsn, $user, $password, $options);
35+
$this->pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
36+
} catch (PDOException $e) {
37+
throw Nette\Database\ConnectionException::from($e);
38+
}
39+
}
40+
41+
42+
public function getPdo(): ?PDO
43+
{
44+
return $this->pdo;
45+
}
2046
}

src/Database/Drivers/PgSqlDriver.php

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -17,15 +17,6 @@
1717
*/
1818
class PgSqlDriver extends PdoDriver
1919
{
20-
private Nette\Database\Connection $connection;
21-
22-
23-
public function initialize(Nette\Database\Connection $connection, array $options): void
24-
{
25-
$this->connection = $connection;
26-
}
27-
28-
2920
public function convertException(\PDOException $e): Nette\Database\DriverException
3021
{
3122
$code = $e->errorInfo[0] ?? null;

src/Database/Drivers/SqliteDriver.php

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,18 @@
1717
*/
1818
class SqliteDriver extends PdoDriver
1919
{
20-
private Nette\Database\Connection $connection;
2120
private string $fmtDateTime;
2221

2322

24-
public function initialize(Nette\Database\Connection $connection, array $options): void
23+
public function connect(
24+
string $dsn,
25+
?string $user = null,
26+
#[\SensitiveParameter]
27+
?string $password = null,
28+
?array $options = null,
29+
): void
2530
{
26-
$this->connection = $connection;
31+
parent::connect($dsn, $user, $password, $options);
2732
$this->fmtDateTime = $options['formatDateTime'] ?? 'U';
2833
}
2934

src/Database/Drivers/SqlsrvDriver.php

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,19 @@
1717
*/
1818
class SqlsrvDriver extends PdoDriver
1919
{
20-
private Nette\Database\Connection $connection;
2120
private string $version;
2221

2322

24-
public function initialize(Nette\Database\Connection $connection, array $options): void
23+
public function connect(
24+
string $dsn,
25+
?string $user = null,
26+
#[\SensitiveParameter]
27+
?string $password = null,
28+
?array $options = null,
29+
): void
2530
{
26-
$this->connection = $connection;
27-
$this->version = $connection->getPdo()->getAttribute(\PDO::ATTR_SERVER_VERSION);
31+
parent::connect($dsn, $user, $password, $options);
32+
$this->version = $this->pdo->getAttribute(\PDO::ATTR_SERVER_VERSION);
2833
}
2934

3035

0 commit comments

Comments
 (0)