Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PDO: Support for PDO::ERRMODE_EXCEPTION (PHP8) #394

Open
wants to merge 10 commits into
base: master
Choose a base branch
from
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 .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: ['7.2', '7.3', '7.4', '8.0', '8.1']
php: ['8.0', '8.1']

fail-fast: false

Expand Down
20 changes: 10 additions & 10 deletions appveyor.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
build: off
cache:
- c:\php7 -> appveyor.yml
- c:\php -> appveyor.yml
- '%LOCALAPPDATA%\Composer\files -> appveyor.yml'

clone_folder: c:\projects\dibi
Expand All @@ -11,21 +11,21 @@ services:
- mysql

init:
- SET PATH=c:\php7;%PATH%
- SET PATH=c:\php;%PATH%
- SET ANSICON=121x90 (121x90)

install:
# Install PHP 7.2
- IF EXIST c:\php7 (SET PHP=0) ELSE (SET PHP=1)
- IF %PHP%==1 mkdir c:\php7
- IF %PHP%==1 cd c:\php7
- IF %PHP%==1 curl https://windows.php.net/downloads/releases/archives/php-7.2.18-Win32-VC15-x64.zip --output php.zip
# Install PHP 8.0
- IF EXIST c:\php (SET PHP=0) ELSE (SET PHP=1)
- IF %PHP%==1 mkdir c:\php
- IF %PHP%==1 cd c:\php
- IF %PHP%==1 curl https://windows.php.net/downloads/releases/archives/php-8.0.1-Win32-vs16-x64.zip --output php.zip
- IF %PHP%==1 7z x php.zip >nul
- IF %PHP%==1 echo extension_dir=ext >> php.ini
- IF %PHP%==1 echo extension=php_openssl.dll >> php.ini
- IF %PHP%==1 curl https://github.com/microsoft/msphpsql/releases/download/v5.8.0/Windows-7.2.zip -L --output sqlsrv.zip
- IF %PHP%==1 curl https://github.com/microsoft/msphpsql/releases/download/v5.9.0/Windows-8.0.zip -L --output sqlsrv.zip
- IF %PHP%==1 7z x sqlsrv.zip >nul
- IF %PHP%==1 copy Windows-7.2\x64\php_sqlsrv_72_ts.dll ext\php_sqlsrv_ts.dll
- IF %PHP%==1 copy Windows-8.0\x64\php_sqlsrv_80_ts.dll ext\php_sqlsrv_ts.dll
- IF %PHP%==1 del /Q *.zip

# Install Microsoft Access Database Engine x64
Expand All @@ -41,7 +41,7 @@ install:
- copy tests\databases.appveyor.ini tests\databases.ini

test_script:
- vendor\bin\tester tests -s -p c:\php7\php -c tests\php-win.ini
- vendor\bin\tester tests -s -p c:\php\php -c tests\php-win.ini

on_failure:
# Print *.actual content
Expand Down
8 changes: 4 additions & 4 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,11 @@
}
],
"require": {
"php": ">=7.2"
"php": ">=8.0 <8.2"
},
"require-dev": {
"tracy/tracy": "~2.2",
"nette/tester": "~2.0",
"tracy/tracy": "^2.8",
"nette/tester": "^2.4",
"nette/di": "^3.0",
"phpstan/phpstan": "^0.12"
},
Expand All @@ -31,7 +31,7 @@
},
"extra": {
"branch-alias": {
"dev-master": "4.2-dev"
"dev-master": "5.0-dev"
}
}
}
4 changes: 2 additions & 2 deletions examples/query-language-and-conditions.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
$dibi->test('
SELECT *
FROM customers
%if', isset($name), 'WHERE name LIKE ?', $name, '%end'
%if', isset($name), 'WHERE name LIKE ?', $name, '%end',
);
// -> SELECT * FROM customers WHERE name LIKE 'K%'

Expand All @@ -54,7 +54,7 @@
WHERE
%if', isset($name), 'name LIKE ?', $name, '
%if', $cond2, 'AND admin=1 %end
%else 1 LIMIT 10 %end'
%else 1 LIMIT 10 %end',
);
// -> SELECT * FROM customers WHERE LIMIT 10

Expand Down
4 changes: 2 additions & 2 deletions examples/query-language-basic-examples.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
SELECT COUNT(*) as [count]
FROM [comments]
WHERE [ip] LIKE ?', $ipMask, '
AND [date] > ', new Dibi\DateTime($timestamp)
AND [date] > ', new Dibi\DateTime($timestamp),
);
// -> SELECT COUNT(*) as [count] FROM [comments] WHERE [ip] LIKE '192.168.%' AND [date] > 876693600

Expand Down Expand Up @@ -69,7 +69,7 @@
$dibi->test('
SELECT *
FROM people
WHERE id IN (?)', $array
WHERE id IN (?)', $array,
);
// -> SELECT * FROM people WHERE id IN ( 1, 2, 3 )

Expand Down
2 changes: 1 addition & 1 deletion examples/using-datetime.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,6 @@
'id' => 123,
'date' => new DateTime('12.3.2007'),
'stamp' => new DateTime('23.1.2007 10:23'),
]
],
);
// -> INSERT INTO [mytable] ([id], [date], [stamp]) VALUES (123, '2007-03-12', '2007-01-23 10-23-00')
2 changes: 1 addition & 1 deletion examples/using-substitutions.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,6 @@ function substFallBack($expr)
$dibi->test("
UPDATE :account:user
SET name='John Doe', status=:active:
WHERE id=", 7
WHERE id=", 7,
);
// -> UPDATE eshop_user SET name='John Doe', status=7 WHERE id= 7
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 4.2 requires PHP version 7.2 and supports PHP up to 8.1.
The Dibi 5.0 requires PHP version 8.0 and supports PHP up to 8.1.


Usage
Expand Down
8 changes: 3 additions & 5 deletions src/Dibi/Bridges/Nette/DibiExtension22.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,9 @@
*/
class DibiExtension22 extends Nette\DI\CompilerExtension
{
/** @var bool|null */
private $debugMode;
private ?bool $debugMode;

/** @var bool|null */
private $cliMode;
private ?bool $cliMode;


public function __construct(?bool $debugMode = null, ?bool $cliMode = null)
Expand Down Expand Up @@ -66,7 +64,7 @@ public function loadConfiguration()
if (class_exists(Tracy\Debugger::class)) {
$connection->addSetup(
[new Nette\DI\Statement('Tracy\Debugger::getBlueScreen'), 'addPanel'],
[[Dibi\Bridges\Tracy\Panel::class, 'renderException']]
[[Dibi\Bridges\Tracy\Panel::class, 'renderException']],
);
}

Expand Down
17 changes: 7 additions & 10 deletions src/Dibi/Bridges/Tracy/Panel.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,20 +22,17 @@ class Panel implements Tracy\IBarPanel
{
use Dibi\Strict;

/** @var int maximum SQL length */
public static $maxLength = 1000;
/** maximum SQL length */
public static int $maxLength = 1000;

/** @var bool|string explain queries? */
public $explain;
public bool|string $explain;

/** @var int */
public $filter;
public int $filter;

/** @var array */
private $events = [];
private array $events = [];


public function __construct($explain = true, ?int $filter = null)
public function __construct(bool $explain = true, ?int $filter = null)
{
$this->filter = $filter ?: Event::QUERY;
$this->explain = $explain;
Expand Down Expand Up @@ -174,7 +171,7 @@ public function getPanel(): ?string
private function getConnectionName(Dibi\Connection $connection): string
{
$driver = $connection->getConfig('driver');
return (is_object($driver) ? get_class($driver) : $driver)
return (is_object($driver) ? $driver::class : $driver)
. ($connection->getConfig('name') ? '/' . $connection->getConfig('name') : '')
. ($connection->getConfig('host') ? "\u{202f}@\u{202f}" . $connection->getConfig('host') : '');
}
Expand Down
66 changes: 25 additions & 41 deletions src/Dibi/Connection.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,25 +22,22 @@ class Connection implements IConnection
{
use Strict;

/** @var array of function (Event $event); Occurs after query is executed */
public $onEvent = [];
/** function (Event $event); Occurs after query is executed */
public array $onEvent = [];

/** @var array Current connection configuration */
private $config;
/** Current connection configuration */
private array $config;

/** @var string[] resultset formats */
private $formats;
private array $formats;

/** @var Driver|null */
private $driver;
private ?Driver $driver = null;

/** @var Translator|null */
private $translator;
private ?Translator $translator = null;

/** @var HashMap Substitutes for identifiers */
private $substitutes;
private HashMap $substitutes;

private $transactionDepth = 0;
private int $transactionDepth = 0;


/**
Expand Down Expand Up @@ -75,7 +72,7 @@ public function __construct(array $config, ?string $name = null)
Helpers::alias($config, 'host', 'hostname');
Helpers::alias($config, 'result|formatDate', 'resultDate');
Helpers::alias($config, 'result|formatDateTime', 'resultDateTime');
$config['driver'] = $config['driver'] ?? 'mysqli';
$config['driver'] ??= 'mysqli';
$config['name'] = $name;
$this->config = $config;

Expand All @@ -93,7 +90,7 @@ public function __construct(array $config, ?string $name = null)
$this->onEvent[] = [new Loggers\FileLogger($config['profiler']['file'], $filter, $errorsOnly), 'logEvent'];
}

$this->substitutes = new HashMap(function (string $expr) { return ":$expr:"; });
$this->substitutes = new HashMap(fn(string $expr) => ":$expr:");
if (!empty($config['substitutes'])) {
foreach ($config['substitutes'] as $key => $value) {
$this->substitutes->$key = $value;
Expand Down Expand Up @@ -190,9 +187,8 @@ final public function isConnected(): bool
/**
* Returns configuration variable. If no $key is passed, returns the entire array.
* @see self::__construct
* @return mixed
*/
final public function getConfig(?string $key = null, $default = null)
final public function getConfig(?string $key = null, $default = null): mixed
{
return $key === null
? $this->config
Expand All @@ -215,21 +211,19 @@ final public function getDriver(): Driver

/**
* Generates (translates) and executes SQL query.
* @param mixed ...$args
* @throws Exception
*/
final public function query(...$args): Result
final public function query(mixed ...$args): Result
{
return $this->nativeQuery($this->translate(...$args));
}


/**
* Generates SQL query.
* @param mixed ...$args
* @throws Exception
*/
final public function translate(...$args): string
final public function translate(mixed ...$args): string
{
if (!$this->driver) {
$this->connect();
Expand All @@ -241,9 +235,8 @@ final public function translate(...$args): string

/**
* Generates and prints SQL query.
* @param mixed ...$args
*/
final public function test(...$args): bool
final public function test(mixed ...$args): bool
{
try {
Helpers::dump($this->translate(...$args));
Expand All @@ -253,7 +246,7 @@ final public function test(...$args): bool
if ($e->getSql()) {
Helpers::dump($e->getSql());
} else {
echo get_class($e) . ': ' . $e->getMessage() . (PHP_SAPI === 'cli' ? "\n" : '<br>');
echo $e::class . ': ' . $e->getMessage() . (PHP_SAPI === 'cli' ? "\n" : '<br>');
}

return false;
Expand All @@ -263,10 +256,9 @@ final public function test(...$args): bool

/**
* Generates (translates) and returns SQL query as DataSource.
* @param mixed ...$args
* @throws Exception
*/
final public function dataSource(...$args): DataSource
final public function dataSource(mixed ...$args): DataSource
{
return new DataSource($this->translate(...$args), $this);
}
Expand Down Expand Up @@ -429,10 +421,7 @@ public function rollback(?string $savepoint = null): void
}


/**
* @return mixed
*/
public function transaction(callable $callback)
public function transaction(callable $callback): mixed
{
if ($this->transactionDepth === 0) {
$this->begin();
Expand Down Expand Up @@ -527,9 +516,9 @@ public function getSubstitutes(): HashMap
*/
public function substitute(string $value): string
{
return strpos($value, ':') === false
? $value
: preg_replace_callback('#:([^:\s]*):#', function (array $m) { return $this->substitutes->{$m[1]}; }, $value);
return str_contains($value, ':')
? preg_replace_callback('#:([^:\s]*):#', fn(array $m) => $this->substitutes->{$m[1]}, $value)
: $value;
}


Expand All @@ -538,45 +527,40 @@ public function substitute(string $value): string

/**
* Executes SQL query and fetch result - shortcut for query() & fetch().
* @param mixed ...$args
* @throws Exception
*/
public function fetch(...$args): ?Row
public function fetch(mixed ...$args): ?Row
{
return $this->query($args)->fetch();
}


/**
* Executes SQL query and fetch results - shortcut for query() & fetchAll().
* @param mixed ...$args
* @return Row[]|array[]
* @throws Exception
*/
public function fetchAll(...$args): array
public function fetchAll(mixed ...$args): array
{
return $this->query($args)->fetchAll();
}


/**
* Executes SQL query and fetch first column - shortcut for query() & fetchSingle().
* @param mixed ...$args
* @return mixed
* @throws Exception
*/
public function fetchSingle(...$args)
public function fetchSingle(mixed ...$args): mixed
{
return $this->query($args)->fetchSingle();
}


/**
* Executes SQL query and fetch pairs - shortcut for query() & fetchPairs().
* @param mixed ...$args
* @throws Exception
*/
public function fetchPairs(...$args): array
public function fetchPairs(mixed ...$args): array
{
return $this->query($args)->fetchPairs();
}
Expand Down
Loading