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.3
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.3
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
8 changes: 4 additions & 4 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 @@ -105,14 +105,14 @@ jobs:
- run: composer install --no-progress --prefer-dist
- run: vendor/bin/tester -p phpdbg tests -s -C --coverage ./coverage.xml --coverage-src ./src
- if: failure()
uses: actions/upload-artifact@v3
uses: actions/upload-artifact@v4
with:
name: output
name: output-${{ matrix.php }}
path: tests/**/output


- name: Save Code Coverage
if: ${{ matrix.php == '8.0' }}
if: ${{ matrix.php == '8.2' }}
env:
COVERALLS_REPO_TOKEN: ${{ secrets.GITHUB_TOKEN }}
run: |
Expand Down
13 changes: 8 additions & 5 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,29 +11,32 @@
}
],
"require": {
"php": "8.0 - 8.4"
"php": "8.2 - 8.4"
},
"require-dev": {
"tracy/tracy": "^2.9",
"nette/tester": "^2.5",
"nette/di": "^3.1",
"phpstan/phpstan": "^1.0",
"phpstan/phpstan-nette": "^2.0",
"jetbrains/phpstorm-attributes": "^1.0"
},
"replace": {
"dg/dibi": "*"
},
"autoload": {
"classmap": ["src/"]
"classmap": ["src/"],
"psr-4": {
"Dibi\\": "src"
}
},
"minimum-stability": "dev",
"minimum-stability": "stable",
"scripts": {
"phpstan": "phpstan analyse",
"tester": "tester tests -s"
},
"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
13 changes: 5 additions & 8 deletions src/Dibi/Bridges/Nette/DibiExtension3.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,21 +13,18 @@
use Nette;
use Nette\Schema\Expect;
use Tracy;
use function is_array;


/**
* Dibi extension for Nette Framework 3. Creates 'connection' & 'panel' services.
*/
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
16 changes: 8 additions & 8 deletions src/Dibi/Bridges/Tracy/Panel.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
use Dibi\Event;
use Dibi\Helpers;
use Tracy;
use function count, is_string, strlen;


/**
Expand All @@ -21,23 +22,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
43 changes: 33 additions & 10 deletions src/Dibi/Connection.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@

use JetBrains\PhpStorm\Language;
use Traversable;
use function array_key_exists, is_array, sprintf;
use const PHP_SAPI;


/**
Expand All @@ -19,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 @@ -120,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 @@ -196,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 @@ -284,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 @@ -448,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 @@ -657,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 @@ -666,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
8 changes: 5 additions & 3 deletions src/Dibi/DataSource.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,16 @@

namespace Dibi;

use function func_get_args, is_array, strpbrk;


/**
* Default implementation of IDataSource.
*/
class DataSource implements IDataSource
{
private Connection $connection;
private string $sql;
private readonly Connection $connection;
private readonly string $sql;
private ?Result $result = null;
private ?int $count = null;
private ?int $totalCount = null;
Expand All @@ -33,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
Loading