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
4 changes: 2 additions & 2 deletions .github/workflows/coding-style.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ jobs:
- uses: actions/checkout@v4
- uses: shivammathur/setup-php@v2
with:
php-version: 8.0
php-version: 8.2
coverage: none

- run: composer create-project nette/code-checker temp/code-checker ^3 --no-progress
Expand All @@ -24,7 +24,7 @@ jobs:
- uses: actions/checkout@v4
- uses: shivammathur/setup-php@v2
with:
php-version: 8.0
php-version: 8.2
coverage: none

- run: composer create-project nette/coding-standard temp/coding-standard ^3 --no-progress
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/static-analysis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ jobs:
- uses: actions/checkout@v4
- uses: shivammathur/setup-php@v2
with:
php-version: 8.0
php-version: 8.2
coverage: none

- run: composer install --no-progress --prefer-dist
Expand Down
4 changes: 2 additions & 2 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ jobs:
runs-on: ubuntu-latest
strategy:
matrix:
php: ['8.0', '8.1', '8.2', '8.3', '8.4']
php: ['8.2', '8.3', '8.4']

fail-fast: false

Expand Down Expand Up @@ -112,7 +112,7 @@ jobs:


- name: Save Code Coverage
if: ${{ matrix.php == '8.0' }}
if: ${{ matrix.php == '8.2' }}
env:
COVERALLS_REPO_TOKEN: ${{ secrets.GITHUB_TOKEN }}
run: |
Expand Down
4 changes: 2 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
}
],
"require": {
"php": "8.0 - 8.4"
"php": "8.2 - 8.4"
},
"require-dev": {
"tracy/tracy": "^2.9",
Expand All @@ -33,7 +33,7 @@
},
"extra": {
"branch-alias": {
"dev-master": "5.0-dev"
"dev-master": "6.0-dev"
}
}
}
2 changes: 1 addition & 1 deletion examples/using-substitutions.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ function substFallBack($expr)


// define callback
$dibi->getSubstitutes()->setCallback('substFallBack');
$dibi->getSubstitutes()->setCallback(substFallBack(...));

// define substitutes as constants
define('SUBST_ACCOUNT', 'eshop_');
Expand Down
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.0 requires PHP version 8.0 and supports PHP up to 8.4.
The Dibi 5.0 requires PHP version 8.2 and supports PHP up to 8.4.


Usage
Expand Down
12 changes: 4 additions & 8 deletions src/Dibi/Bridges/Nette/DibiExtension22.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,10 @@
*/
class DibiExtension22 extends Nette\DI\CompilerExtension
{
private ?bool $debugMode;
private ?bool $cliMode;


public function __construct(?bool $debugMode = null, ?bool $cliMode = null)
{
$this->debugMode = $debugMode;
$this->cliMode = $cliMode;
public function __construct(
private ?bool $debugMode = null,
private ?bool $cliMode = null,
) {
}


Expand Down
12 changes: 4 additions & 8 deletions src/Dibi/Bridges/Nette/DibiExtension3.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,10 @@
*/
class DibiExtension3 extends Nette\DI\CompilerExtension
{
private ?bool $debugMode;
private ?bool $cliMode;


public function __construct(?bool $debugMode = null, ?bool $cliMode = null)
{
$this->debugMode = $debugMode;
$this->cliMode = $cliMode;
public function __construct(
private ?bool $debugMode = null,
private ?bool $cliMode = null,
) {
}


Expand Down
15 changes: 7 additions & 8 deletions src/Dibi/Bridges/Tracy/Panel.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,23 +21,22 @@
class Panel implements Tracy\IBarPanel
{
public static int $maxLength = 1000;
public bool|string $explain;
public int $filter;

private array $events = [];


public function __construct(bool $explain = true, ?int $filter = null)
{
$this->filter = $filter ?: Event::QUERY;
$this->explain = $explain;
public function __construct(
public bool|string $explain = true,
public int $filter = Event::QUERY,
) {
}


public function register(Dibi\Connection $connection): void
{
Tracy\Debugger::getBar()->addPanel($this);
Tracy\Debugger::getBlueScreen()->addPanel([self::class, 'renderException']);
$connection->onEvent[] = [$this, 'logEvent'];
Tracy\Debugger::getBlueScreen()->addPanel(self::renderException(...));
$connection->onEvent[] = $this->logEvent(...);
}


Expand Down
27 changes: 19 additions & 8 deletions src/Dibi/Connection.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,25 @@
*/
class Connection implements IConnection
{
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 ?Translator $translator = null;

/** @var array<string, callable(object): Expression | null> */
Expand Down Expand Up @@ -120,17 +132,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 @@ -196,7 +207,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 @@ -284,7 +295,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 @@ -448,7 +459,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
8 changes: 4 additions & 4 deletions src/Dibi/DataSource.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@
*/
class DataSource implements IDataSource
{
private Connection $connection;
private string $sql;
private readonly IConnection $connection;
private readonly string $sql;
private ?Result $result = null;
private ?int $count = null;
private ?int $totalCount = null;
Expand All @@ -30,7 +30,7 @@ class DataSource implements IDataSource
/**
* @param string $sql command or table or view name, as data source
*/
public function __construct(string $sql, Connection $connection)
public function __construct(string $sql, IConnection $connection)
{
$this->sql = strpbrk($sql, " \t\r\n") === false
? $connection->getDriver()->escapeIdentifier($sql) // table name
Expand Down Expand Up @@ -99,7 +99,7 @@ public function applyLimit(int $limit, ?int $offset = null): static
}


final public function getConnection(): Connection
final public function getConnection(): IConnection
{
return $this->connection;
}
Expand Down
97 changes: 97 additions & 0 deletions src/Dibi/Drivers/Connection.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
<?php

/**
* This file is part of the Dibi, smart database abstraction layer (https://dibiphp.com)
* 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;

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;
}
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
Loading