Skip to content

Commit 4900753

Browse files
committed
database connection moved to PDODriver::connect()
1 parent 202f8d2 commit 4900753

10 files changed

Lines changed: 53 additions & 63 deletions

File tree

src/Database/Connection.php

Lines changed: 13 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -32,12 +32,10 @@ class Connection
3232

3333
private array $options;
3434

35-
private Driver $driver;
35+
private ?Driver $driver = null;
3636

3737
private SqlPreprocessor $preprocessor;
3838

39-
private ?PDO $pdo = null;
40-
4139
/** @var callable(array, ResultSet): array */
4240
private $rowNormalizer = [Helpers::class, 'normalizeRow'];
4341

@@ -59,23 +57,20 @@ public function __construct(string $dsn, string $user = null, string $password =
5957

6058
public function connect(): void
6159
{
62-
if ($this->pdo) {
60+
if ($this->driver) {
6361
return;
6462
}
65-
66-
try {
67-
$this->pdo = new PDO($this->params[0], $this->params[1], $this->params[2], $this->options);
68-
$this->pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
69-
} catch (PDOException $e) {
70-
throw ConnectionException::from($e);
71-
}
72-
63+
$dsn = explode(':', $this->params[0])[0];
7364
$class = empty($this->options['driverClass'])
74-
? 'Nette\Database\Drivers\\' . ucfirst(str_replace('sql', 'Sql', $this->pdo->getAttribute(PDO::ATTR_DRIVER_NAME))) . 'Driver'
65+
? 'Nette\Database\Drivers\\' . ucfirst(str_replace('sql', 'Sql', $dsn)) . 'Driver'
7566
: $this->options['driverClass'];
76-
$this->driver = new $class;
67+
if (!class_exists($class)) {
68+
throw new ConnectionException("Invalid data source '$dsn'.");
69+
}
70+
71+
$this->driver = new $class($this);
72+
$this->driver->connect($this->params[0], $this->params[1], $this->params[2], $this->options);
7773
$this->preprocessor = new SqlPreprocessor($this);
78-
$this->driver->initialize($this, $this->options);
7974
Arrays::invoke($this->onConnect, $this);
8075
}
8176

@@ -89,7 +84,7 @@ public function reconnect(): void
8984

9085
public function disconnect(): void
9186
{
92-
$this->pdo = null;
87+
$this->driver = null;
9388
}
9489

9590

@@ -99,10 +94,11 @@ public function getDsn(): string
9994
}
10095

10196

97+
/** deprecated use getDriver()->getPDO() */
10298
public function getPdo(): PDO
10399
{
104100
$this->connect();
105-
return $this->pdo;
101+
return $this->driver->getPDO();
106102
}
107103

108104

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: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -22,24 +22,22 @@ class MySqlDriver extends PDODriver
2222
ERROR_DUPLICATE_ENTRY = 1062,
2323
ERROR_DATA_TRUNCATED = 1265;
2424

25-
private Nette\Database\Connection $connection;
26-
2725

2826
/**
2927
* Driver options:
3028
* - charset => character encoding to set (default is utf8 or utf8mb4 since MySQL 5.5.3)
3129
* - sqlmode => see http://dev.mysql.com/doc/refman/5.0/en/server-sql-mode.html
3230
*/
33-
public function initialize(Nette\Database\Connection $connection, array $options): void
31+
public function connect(string $dsn, string $user = null, string $password = null, array $options = null): void
3432
{
35-
$this->connection = $connection;
33+
parent::connect($dsn, $user, $password, $options);
3634
$charset = $options['charset']
37-
?? (version_compare($connection->getPdo()->getAttribute(\PDO::ATTR_SERVER_VERSION), '5.5.3', '>=') ? 'utf8mb4' : 'utf8');
35+
?? (version_compare($this->pdo->getAttribute(\PDO::ATTR_SERVER_VERSION), '5.5.3', '>=') ? 'utf8mb4' : 'utf8');
3836
if ($charset) {
39-
$connection->query('SET NAMES ?', $charset);
37+
$this->pdo->query('SET NAMES ' . $this->pdo->quote($charset));
4038
}
4139
if (isset($options['sqlmode'])) {
42-
$connection->query('SET sql_mode=?', $options['sqlmode']);
40+
$this->pdo->query('SET sql_mode=' . $this->pdo->quote($options['sqlmode']));
4341
}
4442
}
4543

src/Database/Drivers/OciDriver.php

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,15 +17,13 @@
1717
*/
1818
class OciDriver extends PDODriver
1919
{
20-
private Nette\Database\Connection $connection;
21-
2220
/** Datetime format */
2321
private string $fmtDateTime;
2422

2523

26-
public function initialize(Nette\Database\Connection $connection, array $options): void
24+
public function connect(string $dsn, string $user = null, string $password = null, array $options = null): void
2725
{
28-
$this->connection = $connection;
26+
parent::connect($dsn, $user, $password, $options);
2927
$this->fmtDateTime = $options['formatDateTime'] ?? 'U';
3028
}
3129

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 & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@
1010
namespace Nette\Database\Drivers;
1111

1212
use Nette;
13+
use Nette\Database\Connection;
14+
use PDO;
15+
use PDOException;
1316

1417

1518
/**
@@ -19,9 +22,30 @@ abstract class PDODriver implements Nette\Database\Driver
1922
{
2023
use Nette\SmartObject;
2124

25+
protected ?Connection $connection;
2226

23-
public function getColumnTypes(\PDOStatement $statement): array
27+
protected ?PDO $pdo = null;
28+
29+
30+
public function __construct(Connection $connection = null)
31+
{
32+
$this->connection = $connection;
33+
}
34+
35+
36+
public function connect(string $dsn, string $user = null, string $password = null, array $options = null): void
37+
{
38+
try {
39+
$this->pdo = new PDO($dsn, $user, $password, $options);
40+
$this->pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
41+
} catch (PDOException $e) {
42+
throw Nette\Database\ConnectionException::from($e);
43+
}
44+
}
45+
46+
47+
public function getPdo(): ?PDO
2448
{
25-
return Nette\Database\Helpers::detectTypes($statement);
49+
return $this->pdo;
2650
}
2751
}

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: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,15 +17,13 @@
1717
*/
1818
class SqliteDriver extends PDODriver
1919
{
20-
private Nette\Database\Connection $connection;
21-
2220
/** Datetime format */
2321
private string $fmtDateTime;
2422

2523

26-
public function initialize(Nette\Database\Connection $connection, array $options): void
24+
public function connect(string $dsn, string $user = null, string $password = null, array $options = null): void
2725
{
28-
$this->connection = $connection;
26+
parent::connect($dsn, $user, $password, $options);
2927
$this->fmtDateTime = $options['formatDateTime'] ?? 'U';
3028
}
3129

src/Database/Drivers/SqlsrvDriver.php

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,15 +17,13 @@
1717
*/
1818
class SqlsrvDriver extends PDODriver
1919
{
20-
private Nette\Database\Connection $connection;
21-
2220
private string $version;
2321

2422

25-
public function initialize(Nette\Database\Connection $connection, array $options): void
23+
public function connect(string $dsn, string $user = null, string $password = null, array $options = null): void
2624
{
27-
$this->connection = $connection;
28-
$this->version = $connection->getPdo()->getAttribute(\PDO::ATTR_SERVER_VERSION);
25+
parent::connect($dsn, $user, $password, $options);
26+
$this->version = $this->pdo->getAttribute(\PDO::ATTR_SERVER_VERSION);
2927
}
3028

3129

0 commit comments

Comments
 (0)