Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
53 commits
Select commit Hold shift + click to select a range
52a346c
ResultSet: added fetchList() as alias for fetchFields() & shortcuts
dg Aug 28, 2024
7c4be75
github actions updated
dg Sep 2, 2024
caaaa42
cs
dg Sep 4, 2024
3a1030c
opened 4.0-dev
dg Mar 1, 2021
e67b168
readme: added jumbo
dg May 16, 2024
35cf35a
removed old Driver::SUPPORT & MySqlDriver::Error constants (BC break)
dg May 10, 2024
49c05db
deprecated methods trigger notices
dg Jan 19, 2022
0e7f616
DatabaseExtension: removed obsolete option 'reflection'
dg Aug 29, 2024
4d4ae57
Driver::getColumns() nativetype -> nativeType, autoincrement -> autoI…
dg Sep 4, 2024
c2fe331
Reflection: added 'scale' field
dg Sep 4, 2024
c26d1e1
returns date-time as immutable Nette\Database\DateTime by default (BC…
dg Aug 27, 2024
5059ce6
MySQL: convertBoolean is true by default (BC break)
dg Aug 28, 2024
923bc75
removed aliases for ISupplementalDriver, IConventions (BC break)
dg Aug 10, 2024
1727e55
removed IRow & IRowContainer (BC break)
dg Aug 10, 2024
5c5b607
removed formatLike() (BC break)
dg Sep 4, 2024
6cffe42
ConnectionPanel: refactoring, initialize($addBarPanel) is true by def…
dg Aug 27, 2024
40ab5c6
connections are always lazy (BC break)
dg Aug 29, 2024
8c86228
renamed Nette\Database\ResultSet -> Result
dg Aug 28, 2024
96c2e8a
renamed Nette\Database\Driver -> Nette\Database\Drivers\Engine (BC br…
dg Aug 28, 2024
f3e3cd9
renamed drivers to *Engine
dg Sep 3, 2024
362e62c
added new drivers
dg Aug 16, 2024
b435456
Connection: the driver is created in the constructor
dg Sep 3, 2024
e511b44
connections & engines are created by Driver
dg Sep 4, 2024
9718880
PDO replaced by Connection
dg Sep 4, 2024
b775226
added Connection::getServerVersion()
dg Aug 15, 2024
b11c0bf
added Connection::execute()
dg Sep 4, 2024
be8936f
Connection::getPdo() deprecated
dg Aug 15, 2024
0cf2662
added TypeConverter
dg Aug 17, 2024
4415f62
uses TypeConverter to convert values to PHP
dg Sep 4, 2024
bb7f7fb
added option convertDateTime
dg Sep 3, 2024
0eb80cf
added option convertDecimal & converts to int/float [Closes #257]
dg Sep 3, 2024
68f45f8
SqlsrvDriver: converts BIT to boolean (BC break)
dg Aug 17, 2024
9075da3
removed custom rowNormalizer (BC break)
dg Sep 4, 2024
4bb25d2
PDOStatement replaced by Result
dg Aug 29, 2024
1e40d23
Result::getColumnMeta is cached (ref #212)
dg Aug 15, 2024
9bc920b
try/catch checking and converting of PDOException moved to drivers
dg Sep 4, 2024
955b992
DriverException::getCode() returns driver error code instead of SQLSt…
dg Aug 27, 2024
235def4
Connection: calling query() moved here from Result
dg Sep 3, 2024
2478ffb
added Connection::getLastQuery(), Result::getQuery(), DriverException…
dg Sep 3, 2024
3cadf6e
Connection::getInsertId() returns int|string or exception (BC break)
dg Aug 14, 2024
11f5d20
drivers: getForeignKeys() works with multi-column foreign keys
dg Sep 4, 2024
f981ccc
Explorer, Selection, Structure: change of constructor dependencies (B…
dg Aug 13, 2024
9b50bd2
Connection::getDsn() deprecated (BC break)
dg Aug 17, 2024
45c712f
auxiliary commit
dg Aug 29, 2024
b9d5ce0
merging Connection & Explorer classes into one (BC break)
dg Sep 6, 2024
33708a6
removed IStructure (BC break)
dg Sep 2, 2024
24167bc
Engine::applyLimit() returns string (BC break)
dg Sep 3, 2024
1ef0bd4
Engine::delimite() -> delimit() (BC break)
dg Sep 4, 2024
b7aeb45
Selection: uses yield for iteration
dg Sep 5, 2024
2e2aa3b
Result: uses yield for iteration
dg Sep 5, 2024
4b58125
added Explorer::createFromDsn() & createFromParameters()
dg Sep 3, 2024
a1798c0
wip php 8.4 test
dg Aug 14, 2024
1769e30
Remove importants from database panel css to allow overwrite it in cu…
website21cz Oct 15, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
connections & engines are created by Driver
  • Loading branch information
dg committed Sep 4, 2024
commit e511b4456cceaa6aaf94e5245dc87c8aa85c8d5b
29 changes: 16 additions & 13 deletions src/Database/Connection.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,11 +49,10 @@ class Connection

public function __construct(
private readonly string $dsn,
?string $username = null,
#[\SensitiveParameter]
private readonly ?string $user = null,
#[\SensitiveParameter]
private readonly ?string $password = null,
private readonly 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 @@ -63,7 +62,15 @@ public function __construct(
$class = empty($options['driverClass'])
? (self::Drivers['pdo-' . $driver] ?? throw new \LogicException("Unknown PDO driver '$driver'."))
: $options['driverClass'];
$this->driver = new $class;
$args = compact('dsn', 'username', 'password', 'options');
unset($options['lazy'], $options['newDateTime'], $options['driverClass']);
foreach ($options as $key => $value) {
if (!is_int($key) && $value !== null) {
$args[$key] = $value;
unset($args['options'][$key]);
}
}
$this->driver = new $class(...$args);
}


Expand All @@ -74,13 +81,11 @@ public function connect(): void
}

try {
$this->pdo = new PDO($this->dsn, $this->user, $this->password, $this->options);
$this->pdo = $this->driver->connect();
} catch (PDOException $e) {
throw ConnectionException::from($e);
}

$this->engine = $this->driver->createEngine();
$this->engine->initialize($this, $this->options);
Arrays::invoke($this->onConnect, $this);
}

Expand Down Expand Up @@ -115,15 +120,13 @@ public function getPdo(): PDO
public function getSupplementalDriver(): Drivers\Engine
{
trigger_error(__METHOD__ . '() is deprecated, use getDriver()', E_USER_DEPRECATED);
$this->connect();
return $this->engine;
return $this->getDatabaseEngine();
}


public function getDatabaseEngine(): Drivers\Engine
{
$this->connect();
return $this->engine;
return $this->engine ??= $this->driver->createEngine($this);
}


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

Expand Down
5 changes: 4 additions & 1 deletion src/Database/Drivers/Driver.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@
*/
interface Driver
{
/** Establishes a connection to the database. */
function connect();

/** Creates a engine instance for the specific database platform. */
function createEngine(): Engine;
function createEngine($connection): Engine;
}
5 changes: 0 additions & 5 deletions src/Database/Drivers/Engine.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,6 @@ interface Engine
*/
function isSupported(string $feature): bool;

/**
* Initializes connection.
*/
function initialize(Database\Connection $connection, array $options): void;

/**
* Converts PDOException to DriverException or its descendant.
*/
Expand Down
9 changes: 3 additions & 6 deletions src/Database/Drivers/Engines/MSSQLEngine.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,9 @@
*/
class MSSQLEngine implements Engine
{
private Nette\Database\Connection $connection;


public function initialize(Nette\Database\Connection $connection, array $options): void
{
$this->connection = $connection;
public function __construct(
private readonly Nette\Database\Connection $connection,
) {
}


Expand Down
25 changes: 4 additions & 21 deletions src/Database/Drivers/Engines/MySQLEngine.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,29 +18,12 @@
*/
class MySQLEngine implements Engine
{
private Nette\Database\Connection $connection;
private bool $convertBoolean;
public bool $convertBoolean = true;


/**
* Driver options:
* - charset => character encoding to set (default is utf8mb4)
* - sqlmode => see http://dev.mysql.com/doc/refman/5.0/en/server-sql-mode.html
* - convertBoolean => converts INT(1) to boolean
*/
public function initialize(Nette\Database\Connection $connection, array $options): void
{
$this->connection = $connection;
$charset = $options['charset'] ?? 'utf8mb4';
if ($charset) {
$connection->query('SET NAMES ?', $charset);
}

if (isset($options['sqlmode'])) {
$connection->query('SET sql_mode=?', $options['sqlmode']);
}

$this->convertBoolean = (bool) ($options['convertBoolean'] ?? true);
public function __construct(
private readonly Nette\Database\Connection $connection,
) {
}


Expand Down
5 changes: 0 additions & 5 deletions src/Database/Drivers/Engines/ODBCEngine.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,6 @@
*/
class ODBCEngine implements Engine
{
public function initialize(Nette\Database\Connection $connection, array $options): void
{
}


public function isSupported(string $feature): bool
{
return $feature === self::SupportSubselect;
Expand Down
12 changes: 5 additions & 7 deletions src/Database/Drivers/Engines/OracleEngine.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,12 @@
*/
class OracleEngine implements Engine
{
private Nette\Database\Connection $connection;
private string $fmtDateTime;
public string $formatDateTime = 'U';


public function initialize(Nette\Database\Connection $connection, array $options): void
{
$this->connection = $connection;
$this->fmtDateTime = $options['formatDateTime'] ?? 'U';
public function __construct(
private readonly Nette\Database\Connection $connection,
) {
}


Expand Down Expand Up @@ -65,7 +63,7 @@ public function delimite(string $name): string

public function formatDateTime(\DateTimeInterface $value): string
{
return $value->format($this->fmtDateTime);
return $value->format($this->formatDateTime);
}


Expand Down
9 changes: 3 additions & 6 deletions src/Database/Drivers/Engines/PostgreSQLEngine.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,9 @@
*/
class PostgreSQLEngine implements Engine
{
private Nette\Database\Connection $connection;


public function initialize(Nette\Database\Connection $connection, array $options): void
{
$this->connection = $connection;
public function __construct(
private readonly Nette\Database\Connection $connection,
) {
}


Expand Down
9 changes: 3 additions & 6 deletions src/Database/Drivers/Engines/SQLServerEngine.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,9 @@
*/
class SQLServerEngine implements Engine
{
private Nette\Database\Connection $connection;


public function initialize(Nette\Database\Connection $connection, array $options): void
{
$this->connection = $connection;
public function __construct(
private readonly Nette\Database\Connection $connection,
) {
}


Expand Down
14 changes: 6 additions & 8 deletions src/Database/Drivers/Engines/SQLiteEngine.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,12 @@
*/
class SQLiteEngine implements Engine
{
private Nette\Database\Connection $connection;
private string $fmtDateTime;
public string $formatDateTime = 'U';


public function initialize(Nette\Database\Connection $connection, array $options): void
{
$this->connection = $connection;
$this->fmtDateTime = $options['formatDateTime'] ?? 'U';
public function __construct(
private readonly Nette\Database\Connection $connection,
) {
}


Expand Down Expand Up @@ -78,7 +76,7 @@ public function delimite(string $name): string

public function formatDateTime(\DateTimeInterface $value): string
{
return $value->format($this->fmtDateTime);
return $value->format($this->formatDateTime);
}


Expand Down Expand Up @@ -234,7 +232,7 @@ public function getColumnTypes(\PDOStatement $statement): array
for ($col = 0; $col < $count; $col++) {
$meta = $statement->getColumnMeta($col);
if (isset($meta['sqlite:decl_type'])) {
$types[$meta['name']] = $this->fmtDateTime === 'U' && in_array($meta['sqlite:decl_type'], ['DATE', 'DATETIME'], strict: true)
$types[$meta['name']] = $this->formatDateTime === 'U' && in_array($meta['sqlite:decl_type'], ['DATE', 'DATETIME'], strict: true)
? Nette\Database\IStructure::FIELD_UNIX_TIMESTAMP
: Nette\Database\Helpers::detectType($meta['sqlite:decl_type']);
} elseif (isset($meta['native_type'])) {
Expand Down
14 changes: 14 additions & 0 deletions src/Database/Drivers/PDO/Driver.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,18 @@
*/
abstract class Driver implements Drivers\Driver
{
public function __construct(
protected readonly string $dsn,
protected readonly ?string $username = null,
#[\SensitiveParameter]
protected readonly ?string $password = null,
protected readonly array $options = [],
) {
}


public function connect()
{
return new \PDO($this->dsn, $this->username, $this->password, $this->options);
}
}
4 changes: 2 additions & 2 deletions src/Database/Drivers/PDO/MSSQL/Driver.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@
*/
class Driver extends Drivers\PDO\Driver
{
public function createEngine(): MSSQLEngine
public function createEngine($connection): MSSQLEngine
{
return new MSSQLEngine;
return new MSSQLEngine($connection);
}
}
35 changes: 33 additions & 2 deletions src/Database/Drivers/PDO/MySQL/Driver.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,39 @@
*/
class Driver extends Drivers\PDO\Driver
{
public function createEngine(): MySQLEngine
private const DefaultCharset = 'utf8mb4';


public function __construct(
protected readonly string $dsn,
protected readonly ?string $username = null,
#[\SensitiveParameter]
protected readonly ?string $password = null,
protected readonly array $options = [],
protected readonly ?string $charset = self::DefaultCharset,
protected readonly ?string $sqlmode = null,
protected readonly ?bool $convertBoolean = null,
) {
}


public function connect()
{
$connection = parent::connect();
if ($this->charset) {
$connection->query('SET NAMES ' . $connection->quote($this->charset));
}
if ($this->sqlmode) {
$connection->query('SET sql_mode=' . $connection->quote($this->sqlmode));
}
return $connection;
}


public function createEngine($connection): MySQLEngine
{
return new MySQLEngine;
$engine = new MySQLEngine($connection);
$engine->convertBoolean = $this->convertBoolean ?? $engine->convertBoolean;
return $engine;
}
}
19 changes: 17 additions & 2 deletions src/Database/Drivers/PDO/OCI/Driver.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,23 @@
*/
class Driver extends Drivers\PDO\Driver
{
public function createEngine(): OracleEngine
public function __construct(
protected readonly string $dsn,
protected readonly ?string $username = null,
#[\SensitiveParameter]
protected readonly ?string $password = null,
protected readonly array $options = [],
protected readonly ?string $formatDateTime = null,
) {
}


public function createEngine($connection): OracleEngine
{
return new OracleEngine;
$engine = new OracleEngine($connection);
if ($this->formatDateTime) {
$engine->formatDateTime = $this->formatDateTime;
}
return $engine;
}
}
2 changes: 1 addition & 1 deletion src/Database/Drivers/PDO/ODBC/Driver.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
*/
class Driver extends Drivers\PDO\Driver
{
public function createEngine(): ODBCEngine
public function createEngine($connection): ODBCEngine
{
return new ODBCEngine;
}
Expand Down
4 changes: 2 additions & 2 deletions src/Database/Drivers/PDO/PgSQL/Driver.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@
*/
class Driver extends Drivers\PDO\Driver
{
public function createEngine(): PostgreSQLEngine
public function createEngine($connection): PostgreSQLEngine
{
return new PostgreSQLEngine;
return new PostgreSQLEngine($connection);
}
}
4 changes: 2 additions & 2 deletions src/Database/Drivers/PDO/SQLSrv/Driver.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@
*/
class Driver extends Drivers\PDO\Driver
{
public function createEngine(): SQLServerEngine
public function createEngine($connection): SQLServerEngine
{
return new SQLServerEngine;
return new SQLServerEngine($connection);
}
}
Loading