Skip to content

Commit

Permalink
renamed Nette\Database\Driver -> Nette\Database\Drivers\Engine (BC br…
Browse files Browse the repository at this point in the history
…eak)
  • Loading branch information
dg committed Aug 19, 2024
1 parent f15da15 commit 4cf644b
Show file tree
Hide file tree
Showing 45 changed files with 339 additions and 315 deletions.
22 changes: 11 additions & 11 deletions src/Database/Connection.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ class Connection

/** @var array<callable(self, ResultSet|DriverException): void> Occurs after query is executed */
public array $onQuery = [];
private Driver $driver;
private Drivers\Engine $engine;
private SqlPreprocessor $preprocessor;
private ?PDO $pdo = null;

Expand Down Expand Up @@ -68,9 +68,9 @@ public function connect(): void
$class = empty($this->options['driverClass'])
? 'Nette\Database\Drivers\\' . ucfirst(str_replace('sql', 'Sql', $this->pdo->getAttribute(PDO::ATTR_DRIVER_NAME))) . 'Driver'
: $this->options['driverClass'];
$this->driver = new $class;
$this->engine = new $class;
$this->preprocessor = new SqlPreprocessor($this);
$this->driver->initialize($this, $this->options);
$this->engine->initialize($this, $this->options);
Arrays::invoke($this->onConnect, $this);
}

Expand Down Expand Up @@ -101,25 +101,25 @@ public function getPdo(): PDO
}


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


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


public function getReflection(): Reflection
{
return new Reflection($this->getDriver());
return new Reflection($this->getDatabaseEngine());
}


Expand All @@ -136,7 +136,7 @@ public function getInsertId(?string $sequence = null): string
$res = $this->getPdo()->lastInsertId($sequence);
return $res === false ? '0' : $res;
} catch (PDOException $e) {
throw $this->driver->convertException($e);
throw $this->engine->convertException($e);
}
}

Expand Down
12 changes: 7 additions & 5 deletions src/Database/Driver.php → src/Database/Drivers/Engine.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,15 @@

declare(strict_types=1);

namespace Nette\Database;
namespace Nette\Database\Drivers;

use Nette\Database;


/**
* Supplemental PDO database driver.
* Engine-specific behaviors, features and SQL dialects.
*/
interface Driver
interface Engine
{
public const
SupportSequence = 'sequence',
Expand All @@ -26,12 +28,12 @@ interface Driver
/**
* Initializes connection.
*/
function initialize(Connection $connection, array $options): void;
function initialize(Database\Connection $connection, array $options): void;

/**
* Converts PDOException to DriverException or its descendant.
*/
function convertException(\PDOException $e): DriverException;
function convertException(\PDOException $e): Database\DriverException;

/**
* Delimites identifier for use in a SQL statement.
Expand Down
2 changes: 1 addition & 1 deletion src/Database/Drivers/MsSqlDriver.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
/**
* Supplemental MS SQL database driver.
*/
class MsSqlDriver implements Nette\Database\Driver
class MsSqlDriver implements Engine
{
private Nette\Database\Connection $connection;

Expand Down
2 changes: 1 addition & 1 deletion src/Database/Drivers/MySqlDriver.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
/**
* Supplemental MySQL database driver.
*/
class MySqlDriver implements Nette\Database\Driver
class MySqlDriver implements Engine
{
private Nette\Database\Connection $connection;
private bool $convertBoolean;
Expand Down
2 changes: 1 addition & 1 deletion src/Database/Drivers/OciDriver.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
/**
* Supplemental Oracle database driver.
*/
class OciDriver implements Nette\Database\Driver
class OciDriver implements Engine
{
private Nette\Database\Connection $connection;
private string $fmtDateTime;
Expand Down
2 changes: 1 addition & 1 deletion src/Database/Drivers/OdbcDriver.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
/**
* Supplemental ODBC database driver.
*/
class OdbcDriver implements Nette\Database\Driver
class OdbcDriver implements Engine
{
public function initialize(Nette\Database\Connection $connection, array $options): void
{
Expand Down
2 changes: 1 addition & 1 deletion src/Database/Drivers/PgSqlDriver.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
/**
* Supplemental PostgreSQL database driver.
*/
class PgSqlDriver implements Nette\Database\Driver
class PgSqlDriver implements Engine
{
private Nette\Database\Connection $connection;

Expand Down
2 changes: 1 addition & 1 deletion src/Database/Drivers/SqliteDriver.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
/**
* Supplemental SQLite3 database driver.
*/
class SqliteDriver implements Nette\Database\Driver
class SqliteDriver implements Engine
{
private Nette\Database\Connection $connection;
private string $fmtDateTime;
Expand Down
2 changes: 1 addition & 1 deletion src/Database/Drivers/SqlsrvDriver.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
/**
* Supplemental SQL Server 2005 and later database driver.
*/
class SqlsrvDriver implements Nette\Database\Driver
class SqlsrvDriver implements Engine
{
private Nette\Database\Connection $connection;

Expand Down
6 changes: 6 additions & 0 deletions src/Database/Explorer.php
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,12 @@ public function getConnection(): Connection
}


public function getDatabaseEngine(): Drivers\Engine
{
return $this->connection->getDatabaseEngine();
}


public function getStructure(): IStructure
{
return $this->structure;
Expand Down
8 changes: 4 additions & 4 deletions src/Database/Reflection.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ final class Reflection


public function __construct(
private readonly Driver $driver,
private readonly Drivers\Engine $engine,
) {
unset($this->tables);
}
Expand Down Expand Up @@ -67,16 +67,16 @@ private function getFullName(string $name): string


/** @internal */
public function getDriver(): Driver
public function getDatabaseEngine(): Drivers\Engine
{
return $this->driver;
return $this->engine;
}


private function initTables(): void
{
$res = [];
foreach ($this->driver->getTables() as $row) {
foreach ($this->engine->getTables() as $row) {
$res[$row['fullName'] ?? $row['name']] = new Table($this, $row['name'], $row['view'], $row['fullName'] ?? null);
}
$this->tables = $res;
Expand Down
6 changes: 3 additions & 3 deletions src/Database/Reflection/Table.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public function getColumn(string $name): Column
private function initColumns(): void
{
$res = [];
foreach ($this->reflection->getDriver()->getColumns($this->name) as $row) {
foreach ($this->reflection->getDatabaseEngine()->getColumns($this->name) as $row) {
$row['table'] = $this;
$res[$row['name']] = new Column(...$row);
}
Expand All @@ -65,7 +65,7 @@ private function initIndexes(): void
$row['primary'],
is_string($row['name']) ? $row['name'] : null,
),
$this->reflection->getDriver()->getIndexes($this->name),
$this->reflection->getDatabaseEngine()->getIndexes($this->name),
);
}

Expand All @@ -83,7 +83,7 @@ private function initPrimaryKey(): void
private function initForeignKeys(): void
{
$tmp = [];
foreach ($this->reflection->getDriver()->getForeignKeys($this->name) as $row) {
foreach ($this->reflection->getDatabaseEngine()->getForeignKeys($this->name) as $row) {
$id = $row['name'];
$foreignTable = $this->reflection->getTable($row['table']);
$tmp[$id][0] = $foreignTable;
Expand Down
4 changes: 2 additions & 2 deletions src/Database/ResultSet.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ public function __construct(
$this->pdoStatement->execute();
}
} catch (\PDOException $e) {
$e = $connection->getDriver()->convertException($e);
$e = $connection->getDatabaseEngine()->convertException($e);
$e->queryString = $queryString;
$e->params = $params;
throw $e;
Expand Down Expand Up @@ -108,7 +108,7 @@ public function getRowCount(): ?int

public function getColumnTypes(): array
{
$this->types ??= $this->connection->getDriver()->getColumnTypes($this->pdoStatement);
$this->types ??= $this->connection->getDatabaseEngine()->getColumnTypes($this->pdoStatement);
return $this->types;
}

Expand Down
12 changes: 6 additions & 6 deletions src/Database/SqlPreprocessor.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ class SqlPreprocessor
];

private readonly Connection $connection;
private readonly Driver $driver;
private readonly Drivers\Engine $engine;
private array $params;
private array $remaining;
private int $counter;
Expand All @@ -62,7 +62,7 @@ class SqlPreprocessor
public function __construct(Connection $connection)
{
$this->connection = $connection;
$this->driver = $connection->getDriver();
$this->engine = $connection->getDatabaseEngine();
}


Expand Down Expand Up @@ -177,10 +177,10 @@ private function formatValue(mixed $value, ?string $mode = null): string
return $res;

} elseif ($value instanceof \DateTimeInterface) {
return $this->driver->formatDateTime($value);
return $this->engine->formatDateTime($value);

} elseif ($value instanceof \DateInterval) {
return $this->driver->formatDateInterval($value);
return $this->engine->formatDateInterval($value);

} elseif ($value instanceof \BackedEnum && is_scalar($value->value)) {
$this->remaining[] = $value->value;
Expand Down Expand Up @@ -231,7 +231,7 @@ private function formatValue(mixed $value, ?string $mode = null): string
$vx[] = implode(', ', $vx2);
}

$select = $this->driver->isSupported(Driver::SupportMultiInsertAsSelect);
$select = $this->engine->isSupported(Drivers\Engine::SupportMultiInsertAsSelect);
return '(' . implode(', ', $kx) . ($select ? ') SELECT ' : ') VALUES (')
. implode($select ? ' UNION ALL SELECT ' : '), (', $vx) . ($select ? '' : ')');
}
Expand Down Expand Up @@ -320,6 +320,6 @@ private function formatValue(mixed $value, ?string $mode = null): string

private function delimite(string $name): string
{
return implode('.', array_map($this->driver->delimite(...), explode('.', $name)));
return implode('.', array_map($this->engine->delimite(...), explode('.', $name)));
}
}
10 changes: 5 additions & 5 deletions src/Database/Structure.php
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ public function getPrimaryKeySequence(string $table): ?string
$this->needStructure();
$table = $this->resolveFQTableName($table);

if (!$this->connection->getDriver()->isSupported(Driver::SupportSequence)) {
if (!$this->connection->getDatabaseEngine()->isSupported(Drivers\Engine::SupportSequence)) {
return null;
}

Expand Down Expand Up @@ -177,10 +177,10 @@ protected function needStructure(): void

protected function loadStructure(): array
{
$driver = $this->connection->getDriver();
$engine = $this->connection->getDatabaseEngine();

$structure = [];
$structure['tables'] = $driver->getTables();
$structure['tables'] = $engine->getTables();

foreach ($structure['tables'] as $tablePair) {
if (isset($tablePair['fullName'])) {
Expand All @@ -190,7 +190,7 @@ protected function loadStructure(): array
$table = $tablePair['name'];
}

$structure['columns'][strtolower($table)] = $columns = $driver->getColumns($table);
$structure['columns'][strtolower($table)] = $columns = $engine->getColumns($table);

if (!$tablePair['view']) {
$structure['primary'][strtolower($table)] = $this->analyzePrimaryKey($columns);
Expand Down Expand Up @@ -233,7 +233,7 @@ protected function analyzeForeignKeys(array &$structure, string $table): void
{
$lowerTable = strtolower($table);

$foreignKeys = $this->connection->getDriver()->getForeignKeys($table);
$foreignKeys = $this->connection->getDatabaseEngine()->getForeignKeys($table);

$fksColumnsCounts = [];
foreach ($foreignKeys as $foreignKey) {
Expand Down
2 changes: 1 addition & 1 deletion src/Database/Table/Selection.php
Original file line number Diff line number Diff line change
Expand Up @@ -790,7 +790,7 @@ public function insert(iterable $data): ActiveRow|array|int|bool

// First check sequence
if (!empty($primarySequenceName) && $primaryAutoincrementKey) {
$primaryKey[$primaryAutoincrementKey] = $this->explorer->getInsertId($this->explorer->getConnection()->getDriver()->delimite($primarySequenceName));
$primaryKey[$primaryAutoincrementKey] = $this->explorer->getInsertId($this->explorer->getConnection()->getDatabaseEngine()->delimite($primarySequenceName));

// Autoincrement primary without sequence
} elseif ($primaryAutoincrementKey) {
Expand Down
Loading

0 comments on commit 4cf644b

Please sign in to comment.