Skip to content

Commit

Permalink
PDO replaced by Connection
Browse files Browse the repository at this point in the history
  • Loading branch information
dg committed Aug 19, 2024
1 parent d6fc4ff commit 81a8b89
Show file tree
Hide file tree
Showing 21 changed files with 208 additions and 88 deletions.
48 changes: 24 additions & 24 deletions src/Database/Connection.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,9 @@ class Connection
/** @var array<callable(self, ResultSet|DriverException): void> Occurs after query is executed */
public array $onQuery = [];
private Drivers\Driver $driver;
private Drivers\Engine $engine;
private ?Drivers\Connection $connection = null;
private ?Drivers\Engine $engine;
private ?SqlPreprocessor $preprocessor;
private ?PDO $pdo = null;

/** @var callable(array, ResultSet): array */
private $rowNormalizer = [Helpers::class, 'normalizeRow'];
Expand All @@ -39,10 +39,10 @@ class Connection

public function __construct(
private readonly string $dsn,
private readonly ?string $user = null,
?string $user = null,
#[\SensitiveParameter]
private readonly ?string $password = null,
private array $options = [],
?string $password = null,
array $options = [],
) {
if (($options['newDateTime'] ?? null) === false) {
$this->rowNormalizer = fn($row, $resultSet) => Helpers::normalizeRow($row, $resultSet, DateTime::class);
Expand All @@ -59,13 +59,13 @@ public function __construct(

public function connect(): void
{
if ($this->pdo) {
if ($this->connection) {
return;
}

try {
$this->pdo = $this->driver->connect();
$this->engine = $this->driver->createDatabaseEngine($this);
$this->connection = $this->driver->connect();
$this->engine = $this->driver->createDatabaseEngine($this->connection);
} catch (PDOException $e) {
throw ConnectionException::from($e);
}
Expand All @@ -83,7 +83,7 @@ public function reconnect(): void

public function disconnect(): void
{
$this->pdo = null;
$this->connection = null;
}


Expand All @@ -93,19 +93,24 @@ public function getDsn(): string
}


public function getPdo(): PDO
public function getPdo(): \PDO
{
$this->connect();
return $this->pdo;
return $this->getConnectionDriver()->getNativeConnection();
}


/** @deprecated use getDriver() */
public function getSupplementalDriver(): Drivers\Engine
public function getConnectionDriver(): Drivers\Connection
{
trigger_error(__METHOD__ . '() is deprecated, use getDriver()', E_USER_DEPRECATED);
$this->connect();
return $this->engine;
return $this->connection;
}


/** @deprecated use getConnectionDriver() */
public function getSupplementalDriver(): Drivers\Connection
{
trigger_error(__METHOD__ . '() is deprecated, use getConnectionDriver()', E_USER_DEPRECATED);
return $this->getConnectionDriver();
}


Expand All @@ -132,21 +137,16 @@ public function setRowNormalizer(?callable $normalizer): static
public function getInsertId(?string $sequence = null): string
{
try {
$res = $this->getPdo()->lastInsertId($sequence);
return $res === false ? '0' : $res;
return $this->getConnectionDriver()->getInsertId($sequence);
} catch (PDOException $e) {
throw $this->engine->convertException($e);
}
}


public function quote(string $string, int $type = PDO::PARAM_STR): string
public function quote(string $string): string
{
try {
return $this->getPdo()->quote($string, $type);
} catch (PDOException $e) {
throw DriverException::from($e);
}
return $this->getConnectionDriver()->quote($string);
}


Expand Down
31 changes: 31 additions & 0 deletions src/Database/Drivers/Connection.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

/**
* This file is part of the Nette Framework (https://nette.org)
* Copyright (c) 2004 David Grudl (https://davidgrudl.com)
*/

declare(strict_types=1);

namespace Nette\Database\Drivers;


/**
* Database connection driver.
*/
interface Connection
{
function query(string $sql, array $params = []);

function getNativeConnection(): mixed;

function beginTransaction(): void;

function commit(): void;

function rollBack(): void;

function getInsertId(?string $sequence = null): int|string;

function quote(string $string): string;
}
4 changes: 2 additions & 2 deletions src/Database/Drivers/Driver.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
*/
interface Driver
{
function connect();
function connect(): Connection;

function createDatabaseEngine($connection): Engine;
function createDatabaseEngine(Connection $connection): Engine;
}
11 changes: 6 additions & 5 deletions src/Database/Drivers/Engines/MSSQLEngine.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
namespace Nette\Database\Drivers\Engines;

use Nette;
use Nette\Database\Drivers\Connection;
use Nette\Database\Drivers\Engine;


Expand All @@ -19,7 +20,7 @@
class MSSQLEngine implements Engine
{
public function __construct(
private readonly Nette\Database\Connection $connection,
private readonly Connection $connection,
) {
}

Expand Down Expand Up @@ -114,7 +115,7 @@ public function getColumns(string $table): array
WHERE
TABLE_SCHEMA = ?
AND TABLE_NAME = ?
X, $table_schema, $table_name);
X, [$table_schema, $table_name]);

while ($row = $rows->fetch()) {
$columns[] = [
Expand All @@ -128,7 +129,7 @@ public function getColumns(string $table): array
'default' => $row['COLUMN_DEFAULT'],
'autoIncrement' => $row['DOMAIN_NAME'] === 'COUNTER',
'primary' => $row['COLUMN_NAME'] === 'ID',
'vendor' => (array) $row,
'vendor' => $row,
];
}

Expand Down Expand Up @@ -157,7 +158,7 @@ public function getIndexes(string $table): array
t.name = ?
ORDER BY
t.name, ind.name, ind.index_id, ic.index_column_id
X, $table_name);
X, [$table_name]);

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

$id = 0;
while ($row = $rows->fetch()) {
Expand Down
9 changes: 6 additions & 3 deletions src/Database/Drivers/Engines/MySQLEngine.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@
namespace Nette\Database\Drivers\Engines;

use Nette;
use Nette\Database\Drivers\Connection;
use Nette\Database\Drivers\Engine;
use Nette\Database\TypeConverter;


/**
Expand All @@ -22,7 +24,7 @@ class MySQLEngine implements Engine


public function __construct(
private readonly Nette\Database\Connection $connection,
private readonly Connection $connection,
) {
}

Expand Down Expand Up @@ -99,6 +101,7 @@ public function getTables(): array
$tables = [];
$rows = $this->connection->query('SHOW FULL TABLES');
while ($row = $rows->fetch()) {
$row = array_values($row);
$tables[] = [
'name' => $row[0],
'view' => ($row[1] ?? null) === 'VIEW',
Expand All @@ -114,7 +117,7 @@ public function getColumns(string $table): array
$columns = [];
$rows = $this->connection->query('SHOW FULL COLUMNS FROM ' . $this->delimite($table));
while ($row = $rows->fetch()) {
$row = array_change_key_case((array) $row, CASE_LOWER);
$row = array_change_key_case($row);
$typeInfo = Nette\Database\Helpers::parseColumnType($row['type']);
$columns[] = [
'name' => $row['field'],
Expand Down Expand Up @@ -159,7 +162,7 @@ public function getForeignKeys(string $table): array
WHERE TABLE_SCHEMA = DATABASE()
AND REFERENCED_TABLE_NAME IS NOT NULL
AND TABLE_NAME = ?
X, $table);
X, [$table]);

$id = 0;
while ($row = $rows->fetch()) {
Expand Down
4 changes: 3 additions & 1 deletion src/Database/Drivers/Engines/OracleEngine.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
namespace Nette\Database\Drivers\Engines;

use Nette;
use Nette\Database\Drivers\Connection;
use Nette\Database\Drivers\Engine;


Expand All @@ -22,7 +23,7 @@ class OracleEngine implements Engine


public function __construct(
private readonly Nette\Database\Connection $connection,
private readonly Connection $connection,
) {
}

Expand Down Expand Up @@ -98,6 +99,7 @@ public function getTables(): array
$tables = [];
$rows = $this->connection->query('SELECT * FROM cat');
while ($row = $rows->fetch()) {
$row = array_values($row);
if ($row[1] === 'TABLE' || $row[1] === 'VIEW') {
$tables[] = [
'name' => $row[0],
Expand Down
15 changes: 8 additions & 7 deletions src/Database/Drivers/Engines/PostgreSQLEngine.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
namespace Nette\Database\Drivers\Engines;

use Nette;
use Nette\Database\Drivers\Connection;
use Nette\Database\Drivers\Engine;


Expand All @@ -19,7 +20,7 @@
class PostgreSQLEngine implements Engine
{
public function __construct(
private readonly Nette\Database\Connection $connection,
private readonly Connection $connection,
) {
}

Expand Down Expand Up @@ -117,7 +118,7 @@ public function getTables(): array
X);

while ($row = $rows->fetch()) {
$tables[] = (array) $row;
$tables[] = $row;
}

return $tables;
Expand Down Expand Up @@ -162,10 +163,10 @@ public function getColumns(string $table): array
AND NOT a.attisdropped
ORDER BY
a.attnum
X, $this->delimiteFQN($table));
X, [$this->delimiteFQN($table)]);

while ($row = $rows->fetch()) {
$column = (array) $row;
$column = $row;
$column['vendor'] = $column;
unset($column['sequence']);

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

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

while ($row = $rows->fetch()) {
$keys[] = (array) $row;
$keys[] = $row;
}
return $keys;
}
Expand Down
16 changes: 9 additions & 7 deletions src/Database/Drivers/Engines/SQLServerEngine.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@
namespace Nette\Database\Drivers\Engines;

use Nette;
use Nette\Database\Drivers\Connection;
use Nette\Database\Drivers\Engine;
use Nette\Database\TypeConverter;


/**
Expand All @@ -19,7 +21,7 @@
class SQLServerEngine implements Engine
{
public function __construct(
private readonly Nette\Database\Connection $connection,
private readonly Connection $connection,
) {
}

Expand Down Expand Up @@ -134,12 +136,12 @@ public function getColumns(string $table): array
WHERE
o.type IN ('U', 'V')
AND o.name = ?
X, $table);
X, [$table]);

while ($row = $rows->fetch()) {
$row = (array) $row;
$row['vendor'] = $row;
$row['scale'] = $row['scale'] ?: null;
$row['size'] = $row['size'] ? (int) $row['size'] : null;
$row['scale'] = $row['scale'] ? (int) $row['scale'] : null;
$row['nullable'] = (bool) $row['nullable'];
$row['autoIncrement'] = (bool) $row['autoIncrement'];
$row['primary'] = (bool) $row['primary'];
Expand Down Expand Up @@ -173,7 +175,7 @@ public function getIndexes(string $table): array
ORDER BY
i.index_id,
ic.index_column_id
X, $table);
X, [$table]);

while ($row = $rows->fetch()) {
$id = $row['name'];
Expand Down Expand Up @@ -206,10 +208,10 @@ public function getForeignKeys(string $table): array
JOIN sys.columns cf ON fkc.referenced_object_id = cf.object_id AND fkc.referenced_column_id = cf.column_id
WHERE
tl.name = ?
X, $table);
X, [$table]);

while ($row = $rows->fetch()) {
$keys[$row['name']] = (array) $row;
$keys[$row['name']] = $row;
}

return array_values($keys);
Expand Down
Loading

0 comments on commit 81a8b89

Please sign in to comment.