Skip to content

Commit 07d30be

Browse files
committed
PDO replaced by Connection
1 parent 4a90581 commit 07d30be

21 files changed

Lines changed: 205 additions & 89 deletions

src/Database/Connection.php

Lines changed: 24 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212
use JetBrains\PhpStorm\Language;
1313
use Nette\Utils\Arrays;
1414
use Nette\Utils\DateTime;
15-
use PDO;
1615
use PDOException;
1716

1817

@@ -27,9 +26,9 @@ class Connection
2726
/** @var array<callable(self, Result|DriverException): void> Occurs after query is executed */
2827
public array $onQuery = [];
2928
private Drivers\Driver $driver;
30-
private Drivers\Engine $engine;
29+
private ?Drivers\Connection $connection = null;
30+
private ?Drivers\Engine $engine;
3131
private ?SqlPreprocessor $preprocessor;
32-
private ?PDO $pdo = null;
3332

3433
/** @var callable(array, Result): array */
3534
private $rowNormalizer = [Helpers::class, 'normalizeRow'];
@@ -39,10 +38,10 @@ class Connection
3938

4039
public function __construct(
4140
private readonly string $dsn,
42-
private readonly ?string $user = null,
41+
?string $user = null,
4342
#[\SensitiveParameter]
44-
private readonly ?string $password = null,
45-
private array $options = [],
43+
?string $password = null,
44+
array $options = [],
4645
) {
4746
if (($options['newDateTime'] ?? null) === false) {
4847
$this->rowNormalizer = fn($row, $resultSet) => Helpers::normalizeRow($row, $resultSet, DateTime::class);
@@ -59,13 +58,13 @@ public function __construct(
5958

6059
public function connect(): void
6160
{
62-
if ($this->pdo) {
61+
if ($this->connection) {
6362
return;
6463
}
6564

6665
try {
67-
$this->pdo = $this->driver->connect();
68-
$this->engine = $this->driver->createDatabaseEngine($this);
66+
$this->connection = $this->driver->connect();
67+
$this->engine = $this->driver->createDatabaseEngine($this->connection);
6968
} catch (PDOException $e) {
7069
throw ConnectionException::from($e);
7170
}
@@ -83,7 +82,7 @@ public function reconnect(): void
8382

8483
public function disconnect(): void
8584
{
86-
$this->pdo = null;
85+
$this->connection = null;
8786
}
8887

8988

@@ -93,19 +92,24 @@ public function getDsn(): string
9392
}
9493

9594

96-
public function getPdo(): PDO
95+
public function getPdo(): \PDO
9796
{
98-
$this->connect();
99-
return $this->pdo;
97+
return $this->getConnectionDriver()->getNativeConnection();
10098
}
10199

102100

103-
/** @deprecated use getDriver() */
104-
public function getSupplementalDriver(): Drivers\Engine
101+
public function getConnectionDriver(): Drivers\Connection
105102
{
106-
trigger_error(__METHOD__ . '() is deprecated, use getDriver()', E_USER_DEPRECATED);
107103
$this->connect();
108-
return $this->engine;
104+
return $this->connection;
105+
}
106+
107+
108+
/** @deprecated use getConnectionDriver() */
109+
public function getSupplementalDriver(): Drivers\Connection
110+
{
111+
trigger_error(__METHOD__ . '() is deprecated, use getConnectionDriver()', E_USER_DEPRECATED);
112+
return $this->getConnectionDriver();
109113
}
110114

111115

@@ -132,21 +136,16 @@ public function setRowNormalizer(?callable $normalizer): static
132136
public function getInsertId(?string $sequence = null): string
133137
{
134138
try {
135-
$res = $this->getPdo()->lastInsertId($sequence);
136-
return $res === false ? '0' : $res;
139+
return $this->getConnectionDriver()->getInsertId($sequence);
137140
} catch (PDOException $e) {
138141
throw $this->engine->convertException($e);
139142
}
140143
}
141144

142145

143-
public function quote(string $string, int $type = PDO::PARAM_STR): string
146+
public function quote(string $string): string
144147
{
145-
try {
146-
return $this->getPdo()->quote($string, $type);
147-
} catch (PDOException $e) {
148-
throw DriverException::from($e);
149-
}
148+
return $this->getConnectionDriver()->quote($string);
150149
}
151150

152151

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<?php
2+
3+
/**
4+
* This file is part of the Nette Framework (https://nette.org)
5+
* Copyright (c) 2004 David Grudl (https://davidgrudl.com)
6+
*/
7+
8+
declare(strict_types=1);
9+
10+
namespace Nette\Database\Drivers;
11+
12+
13+
/**
14+
* Database connection driver.
15+
*/
16+
interface Connection
17+
{
18+
function query(string $sql, array $params = []);
19+
20+
function getNativeConnection(): mixed;
21+
22+
function beginTransaction(): void;
23+
24+
function commit(): void;
25+
26+
function rollBack(): void;
27+
28+
function getInsertId(?string $sequence = null): int|string;
29+
30+
function quote(string $string): string;
31+
}

src/Database/Drivers/Driver.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
*/
1616
interface Driver
1717
{
18-
function connect();
18+
function connect(): Connection;
1919

20-
function createDatabaseEngine($connection): Engine;
20+
function createDatabaseEngine(Connection $connection): Engine;
2121
}

src/Database/Drivers/Engines/MSSQLEngine.php

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
namespace Nette\Database\Drivers\Engines;
1111

1212
use Nette;
13+
use Nette\Database\Drivers\Connection;
1314
use Nette\Database\Drivers\Engine;
1415

1516

@@ -19,7 +20,7 @@
1920
class MSSQLEngine implements Engine
2021
{
2122
public function __construct(
22-
private readonly Nette\Database\Connection $connection,
23+
private readonly Connection $connection,
2324
) {
2425
}
2526

@@ -114,7 +115,7 @@ public function getColumns(string $table): array
114115
WHERE
115116
TABLE_SCHEMA = ?
116117
AND TABLE_NAME = ?
117-
X, $table_schema, $table_name);
118+
X, [$table_schema, $table_name]);
118119

119120
while ($row = $rows->fetch()) {
120121
$columns[] = [
@@ -128,7 +129,7 @@ public function getColumns(string $table): array
128129
'default' => $row['COLUMN_DEFAULT'],
129130
'autoIncrement' => $row['DOMAIN_NAME'] === 'COUNTER',
130131
'primary' => $row['COLUMN_NAME'] === 'ID',
131-
'vendor' => (array) $row,
132+
'vendor' => $row,
132133
];
133134
}
134135

@@ -157,7 +158,7 @@ public function getIndexes(string $table): array
157158
t.name = ?
158159
ORDER BY
159160
t.name, ind.name, ind.index_id, ic.index_column_id
160-
X, $table_name);
161+
X, [$table_name]);
161162

162163
while ($row = $rows->fetch()) {
163164
$id = $row['name_index'];
@@ -198,7 +199,7 @@ public function getForeignKeys(string $table): array
198199
ON col2.column_id = referenced_column_id AND col2.object_id = tab2.object_id
199200
WHERE
200201
tab1.name = ?
201-
X, $table_name);
202+
X, [$table_name]);
202203

203204
$id = 0;
204205
while ($row = $rows->fetch()) {

src/Database/Drivers/Engines/MySQLEngine.php

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
namespace Nette\Database\Drivers\Engines;
1111

1212
use Nette;
13+
use Nette\Database\Drivers\Connection;
1314
use Nette\Database\Drivers\Engine;
1415

1516

@@ -22,7 +23,7 @@ class MySQLEngine implements Engine
2223

2324

2425
public function __construct(
25-
private readonly Nette\Database\Connection $connection,
26+
private readonly Connection $connection,
2627
) {
2728
}
2829

@@ -99,6 +100,7 @@ public function getTables(): array
99100
$tables = [];
100101
$rows = $this->connection->query('SHOW FULL TABLES');
101102
while ($row = $rows->fetch()) {
103+
$row = array_values($row);
102104
$tables[] = [
103105
'name' => $row[0],
104106
'view' => ($row[1] ?? null) === 'VIEW',
@@ -114,7 +116,7 @@ public function getColumns(string $table): array
114116
$columns = [];
115117
$rows = $this->connection->query('SHOW FULL COLUMNS FROM ' . $this->delimite($table));
116118
while ($row = $rows->fetch()) {
117-
$row = array_change_key_case((array) $row, CASE_LOWER);
119+
$row = array_change_key_case($row);
118120
$typeInfo = Nette\Database\Helpers::parseColumnType($row['type']);
119121
$columns[] = [
120122
'name' => $row['field'],
@@ -159,7 +161,7 @@ public function getForeignKeys(string $table): array
159161
WHERE TABLE_SCHEMA = DATABASE()
160162
AND REFERENCED_TABLE_NAME IS NOT NULL
161163
AND TABLE_NAME = ?
162-
X, $table);
164+
X, [$table]);
163165

164166
$id = 0;
165167
while ($row = $rows->fetch()) {

src/Database/Drivers/Engines/OracleEngine.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
namespace Nette\Database\Drivers\Engines;
1111

1212
use Nette;
13+
use Nette\Database\Drivers\Connection;
1314
use Nette\Database\Drivers\Engine;
1415

1516

@@ -22,7 +23,7 @@ class OracleEngine implements Engine
2223

2324

2425
public function __construct(
25-
private readonly Nette\Database\Connection $connection,
26+
private readonly Connection $connection,
2627
) {
2728
}
2829

@@ -98,6 +99,7 @@ public function getTables(): array
9899
$tables = [];
99100
$rows = $this->connection->query('SELECT * FROM cat');
100101
while ($row = $rows->fetch()) {
102+
$row = array_values($row);
101103
if ($row[1] === 'TABLE' || $row[1] === 'VIEW') {
102104
$tables[] = [
103105
'name' => $row[0],

src/Database/Drivers/Engines/PostgreSQLEngine.php

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
namespace Nette\Database\Drivers\Engines;
1111

1212
use Nette;
13+
use Nette\Database\Drivers\Connection;
1314
use Nette\Database\Drivers\Engine;
1415

1516

@@ -19,7 +20,7 @@
1920
class PostgreSQLEngine implements Engine
2021
{
2122
public function __construct(
22-
private readonly Nette\Database\Connection $connection,
23+
private readonly Connection $connection,
2324
) {
2425
}
2526

@@ -117,7 +118,7 @@ public function getTables(): array
117118
X);
118119

119120
while ($row = $rows->fetch()) {
120-
$tables[] = (array) $row;
121+
$tables[] = $row;
121122
}
122123

123124
return $tables;
@@ -162,10 +163,10 @@ public function getColumns(string $table): array
162163
AND NOT a.attisdropped
163164
ORDER BY
164165
a.attnum
165-
X, $this->delimiteFQN($table));
166+
X, [$this->delimiteFQN($table)]);
166167

167168
while ($row = $rows->fetch()) {
168-
$column = (array) $row;
169+
$column = $row;
169170
$column['vendor'] = $column;
170171
unset($column['sequence']);
171172

@@ -193,7 +194,7 @@ public function getIndexes(string $table): array
193194
WHERE
194195
c1.relkind IN ('r', 'p')
195196
AND c1.oid = ?::regclass
196-
X, $this->delimiteFQN($table));
197+
X, [$this->delimiteFQN($table)]);
197198

198199
while ($row = $rows->fetch()) {
199200
$id = $row['name'];
@@ -228,10 +229,10 @@ public function getForeignKeys(string $table): array
228229
co.contype = 'f'
229230
AND cl.oid = ?::regclass
230231
AND nf.nspname = ANY (pg_catalog.current_schemas(FALSE))
231-
X, $this->delimiteFQN($table));
232+
X, [$this->delimiteFQN($table)]);
232233

233234
while ($row = $rows->fetch()) {
234-
$keys[] = (array) $row;
235+
$keys[] = $row;
235236
}
236237
return $keys;
237238
}

src/Database/Drivers/Engines/SQLServerEngine.php

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
namespace Nette\Database\Drivers\Engines;
1111

1212
use Nette;
13+
use Nette\Database\Drivers\Connection;
1314
use Nette\Database\Drivers\Engine;
1415

1516

@@ -19,7 +20,7 @@
1920
class SQLServerEngine implements Engine
2021
{
2122
public function __construct(
22-
private readonly Nette\Database\Connection $connection,
23+
private readonly Connection $connection,
2324
) {
2425
}
2526

@@ -134,12 +135,12 @@ public function getColumns(string $table): array
134135
WHERE
135136
o.type IN ('U', 'V')
136137
AND o.name = ?
137-
X, $table);
138+
X, [$table]);
138139

139140
while ($row = $rows->fetch()) {
140-
$row = (array) $row;
141141
$row['vendor'] = $row;
142-
$row['scale'] = $row['scale'] ?: null;
142+
$row['size'] = $row['size'] ? (int) $row['size'] : null;
143+
$row['scale'] = $row['scale'] ? (int) $row['scale'] : null;
143144
$row['nullable'] = (bool) $row['nullable'];
144145
$row['autoIncrement'] = (bool) $row['autoIncrement'];
145146
$row['primary'] = (bool) $row['primary'];
@@ -173,7 +174,7 @@ public function getIndexes(string $table): array
173174
ORDER BY
174175
i.index_id,
175176
ic.index_column_id
176-
X, $table);
177+
X, [$table]);
177178

178179
while ($row = $rows->fetch()) {
179180
$id = $row['name'];
@@ -206,10 +207,10 @@ public function getForeignKeys(string $table): array
206207
JOIN sys.columns cf ON fkc.referenced_object_id = cf.object_id AND fkc.referenced_column_id = cf.column_id
207208
WHERE
208209
tl.name = ?
209-
X, $table);
210+
X, [$table]);
210211

211212
while ($row = $rows->fetch()) {
212-
$keys[$row['name']] = (array) $row;
213+
$keys[$row['name']] = $row;
213214
}
214215

215216
return array_values($keys);

0 commit comments

Comments
 (0)