Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
},
"extra": {
"branch-alias": {
"dev-master": "5.1-dev"
"dev-master": "6.0-dev"
}
}
}
2 changes: 1 addition & 1 deletion readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ Install Dibi via Composer:
composer require dibi/dibi
```

The Dibi 5.1 requires PHP version 8.2 and supports PHP up to 8.5.
The Dibi 6.0 requires PHP version 8.2 and supports PHP up to 8.5.


Usage
Expand Down
41 changes: 31 additions & 10 deletions src/Dibi/Connection.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,28 @@
* @property-read int $affectedRows
* @property-read int $insertId
*/
class Connection implements IConnection
class Connection
{
private const Drivers = [
'firebird' => Drivers\Ibase\Connection::class,
'mysqli' => Drivers\MySQLi\Connection::class,
'odbc' => Drivers\ODBC\Connection::class,
'oracle' => Drivers\OCI8\Connection::class,
'pdo' => Drivers\PDO\Connection::class,
'postgre' => Drivers\PgSQL\Connection::class,
'sqlite3' => Drivers\SQLite3\Connection::class,
'sqlite' => Drivers\SQLite3\Connection::class,
'sqlsrv' => Drivers\SQLSrv\Connection::class,
];

/** function (Event $event); Occurs after query is executed */
public ?array $onEvent = [];
private array $config;

/** @var string[] resultset formats */
private array $formats;
private ?Driver $driver = null;
private ?Drivers\Connection $driver = null;
private Drivers\Engine $engine;
private ?Translator $translator = null;

/** @var array<string, callable(object): Expression | null> */
Expand Down Expand Up @@ -122,17 +135,16 @@ public function __destruct()
*/
final public function connect(): void
{
if ($this->config['driver'] instanceof Driver) {
if ($this->config['driver'] instanceof Drivers\Connection) {
$this->driver = $this->config['driver'];
$this->translator = new Translator($this);
return;

} elseif (is_subclass_of($this->config['driver'], Driver::class)) {
} elseif (is_subclass_of($this->config['driver'], Drivers\Connection::class)) {
$class = $this->config['driver'];

} else {
$class = preg_replace(['#\W#', '#sql#'], ['_', 'Sql'], ucfirst(strtolower($this->config['driver'])));
$class = "Dibi\\Drivers\\{$class}Driver";
$class = self::Drivers[strtolower($this->config['driver'])] ?? throw new Exception("Unknown driver '{$this->config['driver']}'.");
if (!class_exists($class)) {
throw new Exception("Unable to create instance of Dibi driver '$class'.");
}
Expand Down Expand Up @@ -198,7 +210,7 @@ final public function getConfig(?string $key = null, $default = null): mixed
/**
* Returns the driver and connects to a database in lazy mode.
*/
final public function getDriver(): Driver
final public function getDriver(): Drivers\Connection
{
if (!$this->driver) {
$this->connect();
Expand Down Expand Up @@ -286,7 +298,7 @@ final public function nativeQuery(#[Language('SQL')] string $sql): Result
throw $e;
}

$res = $this->createResultSet($res ?: new Drivers\NoDataResult(max(0, $this->driver->getAffectedRows())));
$res = $this->createResultSet($res ?: new Drivers\Dummy\Result(max(0, $this->driver->getAffectedRows())));
if ($event) {
$this->onEvent($event->done($res));
}
Expand Down Expand Up @@ -450,7 +462,7 @@ public function transaction(callable $callback): mixed
/**
* Result set factory.
*/
public function createResultSet(ResultDriver $resultDriver): Result
public function createResultSet(Drivers\Result $resultDriver): Result
{
return (new Result($resultDriver, $this->config['result']['normalize'] ?? true))
->setFormats($this->formats);
Expand Down Expand Up @@ -659,6 +671,15 @@ public function loadFile(string $file, ?callable $onProgress = null): int
}


public function getDatabaseEngine(): Drivers\Engine
{
if (!$this->driver) { // TODO
$this->connect();
}
return $this->engine ??= $this->driver->getReflector();
}


/**
* Gets a information about the current database.
*/
Expand All @@ -668,7 +689,7 @@ public function getDatabaseInfo(): Reflection\Database
$this->connect();
}

return new Reflection\Database($this->driver->getReflector(), $this->config['database'] ?? null);
return new Reflection\Database($this->getDatabaseEngine(), $this->config['database'] ?? null);
}


Expand Down
2 changes: 1 addition & 1 deletion src/Dibi/DataSource.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ class DataSource implements IDataSource
public function __construct(string $sql, Connection $connection)
{
$this->sql = strpbrk($sql, " \t\r\n") === false
? $connection->getDriver()->escapeIdentifier($sql) // table name
? $connection->getDatabaseEngine()->escapeIdentifier($sql) // table name
: '(' . $sql . ') t'; // SQL command
$this->connection = $connection;
}
Expand Down
77 changes: 77 additions & 0 deletions src/Dibi/Drivers/Connection.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
<?php

/**
* This file is part of the Dibi, smart database abstraction layer (https://dibi.nette.org)
* Copyright (c) 2005 David Grudl (https://davidgrudl.com)
*/

declare(strict_types=1);

namespace Dibi\Drivers;

use Dibi\DriverException;
use Dibi\Exception;


/**
* Database connection driver.
*/
interface Connection
{
/**
* Disconnects from a database.
* @throws Exception
*/
function disconnect(): void;

/**
* Internal: Executes the SQL query.
* @throws DriverException
*/
function query(string $sql): ?Result;

/**
* Gets the number of affected rows by the last INSERT, UPDATE or DELETE query.
*/
function getAffectedRows(): ?int;

/**
* Retrieves the ID generated for an AUTO_INCREMENT column by the previous INSERT query.
*/
function getInsertId(?string $sequence): ?int;

/**
* Begins a transaction (if supported).
* @throws DriverException
*/
function begin(?string $savepoint = null): void;

/**
* Commits statements in a transaction.
* @throws DriverException
*/
function commit(?string $savepoint = null): void;

/**
* Rollback changes in a transaction.
* @throws DriverException
*/
function rollback(?string $savepoint = null): void;

/**
* Returns the connection resource.
*/
function getResource(): mixed;

/**
* Returns the connection reflector.
*/
function getReflector(): Engine;

/**
* Encodes data for use in a SQL statement.
*/
function escapeText(string $value): string;

function escapeBinary(string $value): string;
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,22 +7,23 @@

declare(strict_types=1);

namespace Dibi\Drivers;
namespace Dibi\Drivers\Dummy;

use Dibi;
use Dibi\Drivers;


/**
* The dummy driver for testing purposes.
*/
class DummyDriver implements Dibi\Driver, Dibi\ResultDriver, Dibi\Reflector
class Connection implements Drivers\Connection, Drivers\Result, Drivers\Engine
{
public function disconnect(): void
{
}


public function query(string $sql): ?Dibi\ResultDriver
public function query(string $sql): ?Result
{
return null;
}
Expand Down Expand Up @@ -64,7 +65,7 @@ public function getResource(): mixed
/**
* Returns the connection reflector.
*/
public function getReflector(): Dibi\Reflector
public function getReflector(): Drivers\Engine
{
return $this;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,15 @@

declare(strict_types=1);

namespace Dibi\Drivers;
namespace Dibi\Drivers\Dummy;

use Dibi;
use Dibi\Drivers;


/**
* The driver for no result set.
*/
class NoDataResult implements Dibi\ResultDriver
class Result implements Drivers\Result
{
public function __construct(
private readonly int $rows,
Expand Down
60 changes: 60 additions & 0 deletions src/Dibi/Drivers/Engine.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
<?php

/**
* This file is part of the Dibi, smart database abstraction layer (https://dibi.nette.org)
* Copyright (c) 2005 David Grudl (https://davidgrudl.com)
*/

declare(strict_types=1);

namespace Dibi\Drivers;


/**
* Engine-specific behaviors.
*/
interface Engine
{
function escapeIdentifier(string $value): string;

function escapeBool(bool $value): string;

function escapeDate(\DateTimeInterface $value): string;

function escapeDateTime(\DateTimeInterface $value): string;

function escapeDateInterval(\DateInterval $value): string;

/**
* Encodes string for use in a LIKE statement.
*/
function escapeLike(string $value, int $pos): string;

/**
* Injects LIMIT/OFFSET to the SQL query.
*/
function applyLimit(string &$sql, ?int $limit, ?int $offset): void;

/**
* Returns list of tables.
* @return array of {name [, (bool) view ]}
*/
function getTables(): array;

/**
* Returns metadata for all columns in a table.
* @return array of {name, nativetype [, table, fullname, (int) size, (bool) nullable, (mixed) default, (bool) autoincrement, (array) vendor ]}
*/
function getColumns(string $table): array;

/**
* Returns metadata for all indexes in a table.
* @return array of {name, (array of names) columns [, (bool) unique, (bool) primary ]}
*/
function getIndexes(string $table): array;

/**
* Returns metadata for all foreign keys in a table.
*/
function getForeignKeys(string $table): array;
}
Loading
Loading