mirror of
https://github.com/dg/dibi.git
synced 2025-09-03 11:02:35 +02:00
Compare commits
28 Commits
Author | SHA1 | Date | |
---|---|---|---|
|
73e16eb1a3 | ||
|
0b394a993d | ||
|
df3edee70b | ||
|
245da39a9f | ||
|
3df64fc3b3 | ||
|
95c3f72a17 | ||
|
d71caf0c75 | ||
|
3066fea2aa | ||
|
b00e556289 | ||
|
877dffd460 | ||
|
7049949b14 | ||
|
771e846a62 | ||
|
4e056c52dd | ||
|
40ad77cf5f | ||
|
d09b462eef | ||
|
4c796a0e0f | ||
|
304af5185d | ||
|
e046935137 | ||
|
1a77048225 | ||
|
ca74488636 | ||
|
07f994a0b5 | ||
|
5fa5acb724 | ||
|
98563d8165 | ||
|
c464960239 | ||
|
a9e90d0b22 | ||
|
decb30de1e | ||
|
f4e71e8855 | ||
|
39f59e0f08 |
31
.github/workflows/coding-style.yml
vendored
Normal file
31
.github/workflows/coding-style.yml
vendored
Normal file
@@ -0,0 +1,31 @@
|
|||||||
|
name: Coding Style
|
||||||
|
|
||||||
|
on: [push, pull_request]
|
||||||
|
|
||||||
|
jobs:
|
||||||
|
nette_cc:
|
||||||
|
name: Nette Code Checker
|
||||||
|
runs-on: ubuntu-latest
|
||||||
|
steps:
|
||||||
|
- uses: actions/checkout@v2
|
||||||
|
- uses: shivammathur/setup-php@v2
|
||||||
|
with:
|
||||||
|
php-version: 8.0
|
||||||
|
coverage: none
|
||||||
|
|
||||||
|
- run: composer create-project nette/code-checker temp/code-checker ^3 --no-progress
|
||||||
|
- run: php temp/code-checker/code-checker --strict-types --no-progress --ignore "tests/*/fixtures"
|
||||||
|
|
||||||
|
|
||||||
|
nette_cs:
|
||||||
|
name: Nette Coding Standard
|
||||||
|
runs-on: ubuntu-latest
|
||||||
|
steps:
|
||||||
|
- uses: actions/checkout@v2
|
||||||
|
- uses: shivammathur/setup-php@v2
|
||||||
|
with:
|
||||||
|
php-version: 8.0
|
||||||
|
coverage: none
|
||||||
|
|
||||||
|
- run: composer create-project nette/coding-standard temp/coding-standard ^3 --no-progress --ignore-platform-reqs
|
||||||
|
- run: php temp/coding-standard/ecs check
|
21
.github/workflows/static-analysis.yml
vendored
Normal file
21
.github/workflows/static-analysis.yml
vendored
Normal file
@@ -0,0 +1,21 @@
|
|||||||
|
name: Static Analysis (only informative)
|
||||||
|
|
||||||
|
on:
|
||||||
|
push:
|
||||||
|
branches:
|
||||||
|
- master
|
||||||
|
|
||||||
|
jobs:
|
||||||
|
phpstan:
|
||||||
|
name: PHPStan
|
||||||
|
runs-on: ubuntu-latest
|
||||||
|
steps:
|
||||||
|
- uses: actions/checkout@v2
|
||||||
|
- uses: shivammathur/setup-php@v2
|
||||||
|
with:
|
||||||
|
php-version: 8.0
|
||||||
|
coverage: none
|
||||||
|
|
||||||
|
- run: composer install --no-progress --prefer-dist
|
||||||
|
- run: composer phpstan -- --no-progress
|
||||||
|
continue-on-error: true # is only informative
|
121
.github/workflows/tests.yml
vendored
Normal file
121
.github/workflows/tests.yml
vendored
Normal file
@@ -0,0 +1,121 @@
|
|||||||
|
name: Tests
|
||||||
|
|
||||||
|
on: [push, pull_request]
|
||||||
|
|
||||||
|
env:
|
||||||
|
php-extensions: mbstring, intl, mysqli, pgsql, sqlsrv-5.9.0preview1, pdo_sqlsrv-5.9.0preview1
|
||||||
|
php-tools: "composer:v2, pecl"
|
||||||
|
|
||||||
|
jobs:
|
||||||
|
tests:
|
||||||
|
runs-on: ubuntu-latest
|
||||||
|
strategy:
|
||||||
|
matrix:
|
||||||
|
php: ['7.2', '7.3', '7.4', '8.0']
|
||||||
|
|
||||||
|
fail-fast: false
|
||||||
|
|
||||||
|
name: PHP ${{ matrix.php }} tests
|
||||||
|
|
||||||
|
services:
|
||||||
|
mysql57:
|
||||||
|
image: mysql:5.7
|
||||||
|
env:
|
||||||
|
MYSQL_DATABASE: dibi_test
|
||||||
|
MYSQL_ROOT_PASSWORD: root
|
||||||
|
ports:
|
||||||
|
- 3306:3306
|
||||||
|
options: >-
|
||||||
|
--health-cmd "mysqladmin ping -ppass"
|
||||||
|
--health-interval 10s
|
||||||
|
--health-start-period 10s
|
||||||
|
--health-timeout 5s
|
||||||
|
--health-retries 10
|
||||||
|
|
||||||
|
mysql80:
|
||||||
|
image: mysql:8.0
|
||||||
|
ports:
|
||||||
|
- 3307:3306
|
||||||
|
options: >-
|
||||||
|
--health-cmd="mysqladmin ping -ppass"
|
||||||
|
--health-interval=10s
|
||||||
|
--health-timeout=5s
|
||||||
|
--health-retries=5
|
||||||
|
-e MYSQL_ROOT_PASSWORD=root
|
||||||
|
-e MYSQL_DATABASE=dibi_test
|
||||||
|
--entrypoint sh mysql:8 -c "exec docker-entrypoint.sh mysqld --default-authentication-plugin=mysql_native_password"
|
||||||
|
|
||||||
|
postgres96:
|
||||||
|
image: postgres:9.6
|
||||||
|
env:
|
||||||
|
POSTGRES_USER: postgres
|
||||||
|
POSTGRES_PASSWORD: postgres
|
||||||
|
POSTGRES_DB: dibi_test
|
||||||
|
ports:
|
||||||
|
- 5432:5432
|
||||||
|
options: >-
|
||||||
|
--health-cmd pg_isready
|
||||||
|
--health-interval 10s
|
||||||
|
--health-timeout 5s
|
||||||
|
--health-retries 5
|
||||||
|
|
||||||
|
postgres13:
|
||||||
|
image: postgres:13
|
||||||
|
env:
|
||||||
|
POSTGRES_USER: postgres
|
||||||
|
POSTGRES_PASSWORD: postgres
|
||||||
|
POSTGRES_DB: dibi_test
|
||||||
|
ports:
|
||||||
|
- 5433:5432
|
||||||
|
options: >-
|
||||||
|
--health-cmd pg_isready
|
||||||
|
--health-interval 10s
|
||||||
|
--health-timeout 5s
|
||||||
|
--health-retries 5
|
||||||
|
|
||||||
|
mssql:
|
||||||
|
image: mcr.microsoft.com/mssql/server:latest
|
||||||
|
env:
|
||||||
|
ACCEPT_EULA: Y
|
||||||
|
SA_PASSWORD: YourStrong!Passw0rd
|
||||||
|
MSSQL_PID: Developer
|
||||||
|
ports:
|
||||||
|
- 1433:1433
|
||||||
|
options: >-
|
||||||
|
--name=mssql
|
||||||
|
--health-cmd "/opt/mssql-tools/bin/sqlcmd -S localhost -U SA -P 'YourStrong!Passw0rd' -Q 'SELECT 1'"
|
||||||
|
--health-interval 10s
|
||||||
|
--health-timeout 5s
|
||||||
|
--health-retries 5
|
||||||
|
|
||||||
|
steps:
|
||||||
|
- uses: actions/checkout@v2
|
||||||
|
- uses: shivammathur/setup-php@v2
|
||||||
|
with:
|
||||||
|
php-version: ${{ matrix.php }}
|
||||||
|
extensions: ${{ env.php-extensions }}
|
||||||
|
tools: ${{ env.php-tools }}
|
||||||
|
coverage: none
|
||||||
|
|
||||||
|
- name: Create databases.ini
|
||||||
|
run: cp ./tests/databases.github.ini ./tests/databases.ini
|
||||||
|
|
||||||
|
- name: Create MS SQL Database
|
||||||
|
run: docker exec -i mssql /opt/mssql-tools/bin/sqlcmd -S localhost -U SA -P 'YourStrong!Passw0rd' -Q 'CREATE DATABASE dibi_test'
|
||||||
|
|
||||||
|
- 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@v2
|
||||||
|
with:
|
||||||
|
name: output
|
||||||
|
path: tests/**/output
|
||||||
|
|
||||||
|
|
||||||
|
- name: Save Code Coverage
|
||||||
|
if: ${{ matrix.php == '8.0' }}
|
||||||
|
env:
|
||||||
|
COVERALLS_REPO_TOKEN: ${{ secrets.GITHUB_TOKEN }}
|
||||||
|
run: |
|
||||||
|
wget https://github.com/php-coveralls/php-coveralls/releases/download/v2.4.3/php-coveralls.phar
|
||||||
|
php php-coveralls.phar --verbose --config tests/.coveralls.yml
|
77
.travis.yml
77
.travis.yml
@@ -1,77 +0,0 @@
|
|||||||
language: php
|
|
||||||
php:
|
|
||||||
- 7.1
|
|
||||||
- 7.2
|
|
||||||
- 7.3
|
|
||||||
- 7.4
|
|
||||||
- 8.0snapshot
|
|
||||||
|
|
||||||
services:
|
|
||||||
- mysql
|
|
||||||
- postgresql
|
|
||||||
|
|
||||||
before_install:
|
|
||||||
# turn off XDebug
|
|
||||||
- phpenv config-rm xdebug.ini || return 0
|
|
||||||
|
|
||||||
# Create databases.ini
|
|
||||||
- cp ./tests/databases.travis.ini ./tests/databases.ini
|
|
||||||
|
|
||||||
# Create Postgre database
|
|
||||||
- psql -c 'CREATE DATABASE dibi_test' -U postgres
|
|
||||||
|
|
||||||
install:
|
|
||||||
- travis_retry composer install --no-progress --prefer-dist
|
|
||||||
|
|
||||||
script:
|
|
||||||
- vendor/bin/tester tests -s
|
|
||||||
|
|
||||||
after_failure:
|
|
||||||
# Print *.actual content
|
|
||||||
- for i in $(find tests -name \*.actual); do echo "--- $i"; cat $i; echo; echo; done
|
|
||||||
|
|
||||||
jobs:
|
|
||||||
include:
|
|
||||||
- name: Nette Code Checker
|
|
||||||
install:
|
|
||||||
- travis_retry composer create-project nette/code-checker temp/code-checker ^3 --no-progress
|
|
||||||
script:
|
|
||||||
- php temp/code-checker/code-checker --strict-types
|
|
||||||
|
|
||||||
|
|
||||||
- name: Nette Coding Standard
|
|
||||||
php: 7.4
|
|
||||||
install:
|
|
||||||
- travis_retry composer create-project nette/coding-standard temp/coding-standard ^3 --no-progress
|
|
||||||
script:
|
|
||||||
- php temp/coding-standard/ecs check src tests
|
|
||||||
|
|
||||||
|
|
||||||
- stage: Static Analysis (informative)
|
|
||||||
php: 7.4
|
|
||||||
script:
|
|
||||||
- composer run-script phpstan
|
|
||||||
|
|
||||||
|
|
||||||
- stage: Code Coverage
|
|
||||||
php: 7.4
|
|
||||||
script:
|
|
||||||
- vendor/bin/tester -p phpdbg tests -s --coverage ./coverage.xml --coverage-src ./src
|
|
||||||
after_script:
|
|
||||||
- wget https://github.com/satooshi/php-coveralls/releases/download/v1.0.1/coveralls.phar
|
|
||||||
- php coveralls.phar --verbose --config tests/.coveralls.yml
|
|
||||||
|
|
||||||
|
|
||||||
allow_failures:
|
|
||||||
- stage: Static Analysis (informative)
|
|
||||||
- stage: Code Coverage
|
|
||||||
|
|
||||||
|
|
||||||
dist: xenial
|
|
||||||
|
|
||||||
cache:
|
|
||||||
directories:
|
|
||||||
- $HOME/.composer/cache
|
|
||||||
|
|
||||||
notifications:
|
|
||||||
email: false
|
|
10
appveyor.yml
10
appveyor.yml
@@ -15,17 +15,17 @@ init:
|
|||||||
- SET ANSICON=121x90 (121x90)
|
- SET ANSICON=121x90 (121x90)
|
||||||
|
|
||||||
install:
|
install:
|
||||||
# Install PHP 7.1
|
# Install PHP 7.2
|
||||||
- IF EXIST c:\php7 (SET PHP=0) ELSE (SET PHP=1)
|
- IF EXIST c:\php7 (SET PHP=0) ELSE (SET PHP=1)
|
||||||
- IF %PHP%==1 mkdir c:\php7
|
- IF %PHP%==1 mkdir c:\php7
|
||||||
- IF %PHP%==1 cd c:\php7
|
- IF %PHP%==1 cd c:\php7
|
||||||
- IF %PHP%==1 curl https://windows.php.net/downloads/releases/archives/php-7.1.5-Win32-VC14-x64.zip --output php.zip
|
- IF %PHP%==1 curl https://windows.php.net/downloads/releases/archives/php-7.2.18-Win32-VC15-x64.zip --output php.zip
|
||||||
- IF %PHP%==1 7z x php.zip >nul
|
- IF %PHP%==1 7z x php.zip >nul
|
||||||
- IF %PHP%==1 echo extension_dir=ext >> php.ini
|
- IF %PHP%==1 echo extension_dir=ext >> php.ini
|
||||||
- IF %PHP%==1 echo extension=php_openssl.dll >> php.ini
|
- IF %PHP%==1 echo extension=php_openssl.dll >> php.ini
|
||||||
- IF %PHP%==1 appveyor DownloadFile https://files.nette.org/misc/php-sqlsrv.zip
|
- 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 7z x php-sqlsrv.zip >nul
|
- IF %PHP%==1 7z x sqlsrv.zip >nul
|
||||||
- IF %PHP%==1 copy SQLSRV\php_sqlsrv_71_ts.dll ext\php_sqlsrv_71_ts.dll
|
- IF %PHP%==1 copy Windows-7.2\x64\php_sqlsrv_72_ts.dll ext\php_sqlsrv_ts.dll
|
||||||
- IF %PHP%==1 del /Q *.zip
|
- IF %PHP%==1 del /Q *.zip
|
||||||
|
|
||||||
# Install Microsoft Access Database Engine x64
|
# Install Microsoft Access Database Engine x64
|
||||||
|
@@ -11,7 +11,7 @@
|
|||||||
}
|
}
|
||||||
],
|
],
|
||||||
"require": {
|
"require": {
|
||||||
"php": ">=7.1"
|
"php": ">=7.2"
|
||||||
},
|
},
|
||||||
"require-dev": {
|
"require-dev": {
|
||||||
"tracy/tracy": "~2.2",
|
"tracy/tracy": "~2.2",
|
||||||
@@ -31,7 +31,7 @@
|
|||||||
},
|
},
|
||||||
"extra": {
|
"extra": {
|
||||||
"branch-alias": {
|
"branch-alias": {
|
||||||
"dev-master": "4.1-dev"
|
"dev-master": "4.2-dev"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
12
readme.md
12
readme.md
@@ -2,7 +2,7 @@
|
|||||||
=========================================================
|
=========================================================
|
||||||
|
|
||||||
[](https://packagist.org/packages/dibi/dibi)
|
[](https://packagist.org/packages/dibi/dibi)
|
||||||
[](https://travis-ci.org/dg/dibi)
|
[](https://github.com/dg/dibi/actions)
|
||||||
[](https://ci.appveyor.com/project/dg/dibi/branch/master)
|
[](https://ci.appveyor.com/project/dg/dibi/branch/master)
|
||||||
[](https://github.com/dg/dibi/releases)
|
[](https://github.com/dg/dibi/releases)
|
||||||
[](https://github.com/dg/dibi/blob/master/license.md)
|
[](https://github.com/dg/dibi/blob/master/license.md)
|
||||||
@@ -15,12 +15,14 @@ Database access functions in PHP are not standardised. This library
|
|||||||
hides the differences between them, and above all, it gives you a very handy interface.
|
hides the differences between them, and above all, it gives you a very handy interface.
|
||||||
|
|
||||||
|
|
||||||
Support Project
|
Support Me
|
||||||
---------------
|
----------
|
||||||
|
|
||||||
Do you like Dibi? Are you looking forward to the new features?
|
Do you like Dibi? Are you looking forward to the new features?
|
||||||
|
|
||||||
[](https://nette.org/make-donation?to=dibi)
|
[](https://github.com/sponsors/dg)
|
||||||
|
|
||||||
|
Thank you!
|
||||||
|
|
||||||
|
|
||||||
Installation
|
Installation
|
||||||
@@ -32,7 +34,7 @@ Install Dibi via Composer:
|
|||||||
composer require dibi/dibi
|
composer require dibi/dibi
|
||||||
```
|
```
|
||||||
|
|
||||||
The Dibi 4.1 requires PHP version 7.1 and supports PHP up to 8.0.
|
The Dibi 4.2 requires PHP version 7.2 and supports PHP up to 8.0.
|
||||||
|
|
||||||
|
|
||||||
Usage
|
Usage
|
||||||
|
@@ -28,6 +28,9 @@ class Connection implements IConnection
|
|||||||
/** @var array Current connection configuration */
|
/** @var array Current connection configuration */
|
||||||
private $config;
|
private $config;
|
||||||
|
|
||||||
|
/** @var string[] resultset formats */
|
||||||
|
private $formats;
|
||||||
|
|
||||||
/** @var Driver|null */
|
/** @var Driver|null */
|
||||||
private $driver;
|
private $driver;
|
||||||
|
|
||||||
@@ -37,17 +40,26 @@ class Connection implements IConnection
|
|||||||
/** @var HashMap Substitutes for identifiers */
|
/** @var HashMap Substitutes for identifiers */
|
||||||
private $substitutes;
|
private $substitutes;
|
||||||
|
|
||||||
|
private $transactionDepth = 0;
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Connection options: (see driver-specific options too)
|
* Connection options: (see driver-specific options too)
|
||||||
* - lazy (bool) => if true, connection will be established only when required
|
* - lazy (bool) => if true, connection will be established only when required
|
||||||
* - result (array) => result set options
|
* - result (array) => result set options
|
||||||
* - formatDateTime => date-time format (if empty, DateTime objects will be returned)
|
* - normalize => normalizes result fields (default: true)
|
||||||
* - formatJson => json format (
|
* - formatDateTime => date-time format
|
||||||
* "string" for leaving value as is,
|
* empty for decoding as Dibi\DateTime (default)
|
||||||
* "object" for decoding json as \stdClass,
|
* "..." formatted according to given format, see https://www.php.net/manual/en/datetime.format.php
|
||||||
* "array" for decoding json as an array - default
|
* "native" for leaving value as is
|
||||||
* )
|
* - formatTimeInterval => time-interval format
|
||||||
|
* empty for decoding as DateInterval (default)
|
||||||
|
* "..." formatted according to given format, see https://www.php.net/manual/en/dateinterval.format.php
|
||||||
|
* "native" for leaving value as is
|
||||||
|
* - formatJson => json format
|
||||||
|
* "array" for decoding json as an array (default)
|
||||||
|
* "object" for decoding json as \stdClass
|
||||||
|
* "native" for leaving value as is
|
||||||
* - profiler (array)
|
* - profiler (array)
|
||||||
* - run (bool) => enable profiler?
|
* - run (bool) => enable profiler?
|
||||||
* - file => file to log
|
* - file => file to log
|
||||||
@@ -65,9 +77,15 @@ class Connection implements IConnection
|
|||||||
Helpers::alias($config, 'result|formatDateTime', 'resultDateTime');
|
Helpers::alias($config, 'result|formatDateTime', 'resultDateTime');
|
||||||
$config['driver'] = $config['driver'] ?? 'mysqli';
|
$config['driver'] = $config['driver'] ?? 'mysqli';
|
||||||
$config['name'] = $name;
|
$config['name'] = $name;
|
||||||
$config['result']['formatJson'] = $config['result']['formatJson'] ?? 'array';
|
|
||||||
$this->config = $config;
|
$this->config = $config;
|
||||||
|
|
||||||
|
$this->formats = [
|
||||||
|
Type::DATE => $this->config['result']['formatDate'],
|
||||||
|
Type::DATETIME => $this->config['result']['formatDateTime'],
|
||||||
|
Type::JSON => $this->config['result']['formatJson'] ?? 'array',
|
||||||
|
Type::TIME_INTERVAL => $this->config['result']['formatTimeInterval'] ?? null,
|
||||||
|
];
|
||||||
|
|
||||||
// profiler
|
// profiler
|
||||||
if (isset($config['profiler']['file']) && (!isset($config['profiler']['run']) || $config['profiler']['run'])) {
|
if (isset($config['profiler']['file']) && (!isset($config['profiler']['run']) || $config['profiler']['run'])) {
|
||||||
$filter = $config['profiler']['filter'] ?? Event::QUERY;
|
$filter = $config['profiler']['filter'] ?? Event::QUERY;
|
||||||
@@ -200,7 +218,7 @@ class Connection implements IConnection
|
|||||||
*/
|
*/
|
||||||
final public function query(...$args): Result
|
final public function query(...$args): Result
|
||||||
{
|
{
|
||||||
return $this->nativeQuery($this->translateArgs($args));
|
return $this->nativeQuery($this->translate(...$args));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@@ -211,7 +229,10 @@ class Connection implements IConnection
|
|||||||
*/
|
*/
|
||||||
final public function translate(...$args): string
|
final public function translate(...$args): string
|
||||||
{
|
{
|
||||||
return $this->translateArgs($args);
|
if (!$this->driver) {
|
||||||
|
$this->connect();
|
||||||
|
}
|
||||||
|
return (clone $this->translator)->translate($args);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@@ -222,7 +243,7 @@ class Connection implements IConnection
|
|||||||
final public function test(...$args): bool
|
final public function test(...$args): bool
|
||||||
{
|
{
|
||||||
try {
|
try {
|
||||||
Helpers::dump($this->translateArgs($args));
|
Helpers::dump($this->translate(...$args));
|
||||||
return true;
|
return true;
|
||||||
|
|
||||||
} catch (Exception $e) {
|
} catch (Exception $e) {
|
||||||
@@ -243,19 +264,7 @@ class Connection implements IConnection
|
|||||||
*/
|
*/
|
||||||
final public function dataSource(...$args): DataSource
|
final public function dataSource(...$args): DataSource
|
||||||
{
|
{
|
||||||
return new DataSource($this->translateArgs($args), $this);
|
return new DataSource($this->translate(...$args), $this);
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Generates SQL query.
|
|
||||||
*/
|
|
||||||
protected function translateArgs(array $args): string
|
|
||||||
{
|
|
||||||
if (!$this->driver) {
|
|
||||||
$this->connect();
|
|
||||||
}
|
|
||||||
return (clone $this->translator)->translate($args);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@@ -328,6 +337,10 @@ class Connection implements IConnection
|
|||||||
*/
|
*/
|
||||||
public function begin(string $savepoint = null): void
|
public function begin(string $savepoint = null): void
|
||||||
{
|
{
|
||||||
|
if ($this->transactionDepth !== 0) {
|
||||||
|
throw new \LogicException(__METHOD__ . '() call is forbidden inside a transaction() callback');
|
||||||
|
}
|
||||||
|
|
||||||
if (!$this->driver) {
|
if (!$this->driver) {
|
||||||
$this->connect();
|
$this->connect();
|
||||||
}
|
}
|
||||||
@@ -352,6 +365,10 @@ class Connection implements IConnection
|
|||||||
*/
|
*/
|
||||||
public function commit(string $savepoint = null): void
|
public function commit(string $savepoint = null): void
|
||||||
{
|
{
|
||||||
|
if ($this->transactionDepth !== 0) {
|
||||||
|
throw new \LogicException(__METHOD__ . '() call is forbidden inside a transaction() callback');
|
||||||
|
}
|
||||||
|
|
||||||
if (!$this->driver) {
|
if (!$this->driver) {
|
||||||
$this->connect();
|
$this->connect();
|
||||||
}
|
}
|
||||||
@@ -376,6 +393,10 @@ class Connection implements IConnection
|
|||||||
*/
|
*/
|
||||||
public function rollback(string $savepoint = null): void
|
public function rollback(string $savepoint = null): void
|
||||||
{
|
{
|
||||||
|
if ($this->transactionDepth !== 0) {
|
||||||
|
throw new \LogicException(__METHOD__ . '() call is forbidden inside a transaction() callback');
|
||||||
|
}
|
||||||
|
|
||||||
if (!$this->driver) {
|
if (!$this->driver) {
|
||||||
$this->connect();
|
$this->connect();
|
||||||
}
|
}
|
||||||
@@ -395,15 +416,42 @@ class Connection implements IConnection
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return mixed
|
||||||
|
*/
|
||||||
|
public function transaction(callable $callback)
|
||||||
|
{
|
||||||
|
if ($this->transactionDepth === 0) {
|
||||||
|
$this->begin();
|
||||||
|
}
|
||||||
|
|
||||||
|
$this->transactionDepth++;
|
||||||
|
try {
|
||||||
|
$res = $callback($this);
|
||||||
|
} catch (\Throwable $e) {
|
||||||
|
$this->transactionDepth--;
|
||||||
|
if ($this->transactionDepth === 0) {
|
||||||
|
$this->rollback();
|
||||||
|
}
|
||||||
|
throw $e;
|
||||||
|
}
|
||||||
|
|
||||||
|
$this->transactionDepth--;
|
||||||
|
if ($this->transactionDepth === 0) {
|
||||||
|
$this->commit();
|
||||||
|
}
|
||||||
|
|
||||||
|
return $res;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Result set factory.
|
* Result set factory.
|
||||||
*/
|
*/
|
||||||
public function createResultSet(ResultDriver $resultDriver): Result
|
public function createResultSet(ResultDriver $resultDriver): Result
|
||||||
{
|
{
|
||||||
$res = new Result($resultDriver);
|
return (new Result($resultDriver, $this->config['result']['normalize'] ?? true))
|
||||||
return $res->setFormat(Type::DATE, $this->config['result']['formatDate'])
|
->setFormats($this->formats);
|
||||||
->setFormat(Type::DATETIME, $this->config['result']['formatDateTime'])
|
|
||||||
->setFormat(Type::JSON, $this->config['result']['formatJson']);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@@ -72,7 +72,7 @@ class MySqliDriver implements Dibi\Driver
|
|||||||
$host = ini_get('mysqli.default_host');
|
$host = ini_get('mysqli.default_host');
|
||||||
if ($host) {
|
if ($host) {
|
||||||
$config['host'] = $host;
|
$config['host'] = $host;
|
||||||
$config['port'] = ini_get('mysqli.default_port');
|
$config['port'] = (int) ini_get('mysqli.default_port');
|
||||||
} else {
|
} else {
|
||||||
$config['host'] = null;
|
$config['host'] = null;
|
||||||
$config['port'] = null;
|
$config['port'] = null;
|
||||||
|
@@ -188,7 +188,7 @@ class PostgreDriver implements Dibi\Driver
|
|||||||
*/
|
*/
|
||||||
public function begin(string $savepoint = null): void
|
public function begin(string $savepoint = null): void
|
||||||
{
|
{
|
||||||
$this->query($savepoint ? "SAVEPOINT $savepoint" : 'START TRANSACTION');
|
$this->query($savepoint ? "SAVEPOINT {$this->escapeIdentifier($savepoint)}" : 'START TRANSACTION');
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@@ -198,7 +198,7 @@ class PostgreDriver implements Dibi\Driver
|
|||||||
*/
|
*/
|
||||||
public function commit(string $savepoint = null): void
|
public function commit(string $savepoint = null): void
|
||||||
{
|
{
|
||||||
$this->query($savepoint ? "RELEASE SAVEPOINT $savepoint" : 'COMMIT');
|
$this->query($savepoint ? "RELEASE SAVEPOINT {$this->escapeIdentifier($savepoint)}" : 'COMMIT');
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@@ -208,7 +208,7 @@ class PostgreDriver implements Dibi\Driver
|
|||||||
*/
|
*/
|
||||||
public function rollback(string $savepoint = null): void
|
public function rollback(string $savepoint = null): void
|
||||||
{
|
{
|
||||||
$this->query($savepoint ? "ROLLBACK TO SAVEPOINT $savepoint" : 'ROLLBACK');
|
$this->query($savepoint ? "ROLLBACK TO SAVEPOINT {$this->escapeIdentifier($savepoint)}" : 'ROLLBACK');
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@@ -28,9 +28,6 @@ class PostgreReflector implements Dibi\Reflector
|
|||||||
|
|
||||||
public function __construct(Dibi\Driver $driver, string $version)
|
public function __construct(Dibi\Driver $driver, string $version)
|
||||||
{
|
{
|
||||||
if ($version < 7.4) {
|
|
||||||
throw new Dibi\DriverException('Reflection requires PostgreSQL 7.4 and newer.');
|
|
||||||
}
|
|
||||||
$this->driver = $driver;
|
$this->driver = $driver;
|
||||||
$this->version = $version;
|
$this->version = $version;
|
||||||
}
|
}
|
||||||
|
@@ -53,6 +53,9 @@ class SqlsrvDriver implements Dibi\Driver
|
|||||||
|
|
||||||
if (isset($config['resource'])) {
|
if (isset($config['resource'])) {
|
||||||
$this->connection = $config['resource'];
|
$this->connection = $config['resource'];
|
||||||
|
if (!is_resource($this->connection)) {
|
||||||
|
throw new \InvalidArgumentException("Configuration option 'resource' is not resource.");
|
||||||
|
}
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
$options = $config['options'];
|
$options = $config['options'];
|
||||||
@@ -63,13 +66,15 @@ class SqlsrvDriver implements Dibi\Driver
|
|||||||
$options['UID'] = (string) $options['UID'];
|
$options['UID'] = (string) $options['UID'];
|
||||||
$options['Database'] = (string) $options['Database'];
|
$options['Database'] = (string) $options['Database'];
|
||||||
|
|
||||||
|
sqlsrv_configure('WarningsReturnAsErrors', 0);
|
||||||
$this->connection = sqlsrv_connect($config['host'], $options);
|
$this->connection = sqlsrv_connect($config['host'], $options);
|
||||||
|
if (!is_resource($this->connection)) {
|
||||||
|
$info = sqlsrv_errors(SQLSRV_ERR_ERRORS);
|
||||||
|
throw new Dibi\DriverException($info[0]['message'], $info[0]['code']);
|
||||||
|
}
|
||||||
|
sqlsrv_configure('WarningsReturnAsErrors', 1);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!is_resource($this->connection)) {
|
|
||||||
$info = sqlsrv_errors();
|
|
||||||
throw new Dibi\DriverException($info[0]['message'], $info[0]['code']);
|
|
||||||
}
|
|
||||||
$this->version = sqlsrv_server_info($this->connection)['SQLServerVersion'];
|
$this->version = sqlsrv_server_info($this->connection)['SQLServerVersion'];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@@ -41,10 +41,12 @@ class Result implements IDataSource
|
|||||||
private $formats = [];
|
private $formats = [];
|
||||||
|
|
||||||
|
|
||||||
public function __construct(ResultDriver $driver)
|
public function __construct(ResultDriver $driver, bool $normalize = true)
|
||||||
{
|
{
|
||||||
$this->driver = $driver;
|
$this->driver = $driver;
|
||||||
$this->detectTypes();
|
if ($normalize) {
|
||||||
|
$this->detectTypes();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@@ -454,8 +456,12 @@ class Result implements IDataSource
|
|||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
$value = $row[$key];
|
$value = $row[$key];
|
||||||
|
$format = $this->formats[$type] ?? null;
|
||||||
|
|
||||||
if ($type === Type::TEXT) {
|
if ($type === null || $format === 'native') {
|
||||||
|
$row[$key] = $value;
|
||||||
|
|
||||||
|
} elseif ($type === Type::TEXT) {
|
||||||
$row[$key] = (string) $value;
|
$row[$key] = (string) $value;
|
||||||
|
|
||||||
} elseif ($type === Type::INTEGER) {
|
} elseif ($type === Type::INTEGER) {
|
||||||
@@ -485,17 +491,16 @@ class Result implements IDataSource
|
|||||||
} elseif ($type === Type::DATETIME || $type === Type::DATE || $type === Type::TIME) {
|
} elseif ($type === Type::DATETIME || $type === Type::DATE || $type === Type::TIME) {
|
||||||
if ($value && substr((string) $value, 0, 3) !== '000') { // '', null, false, '0000-00-00', ...
|
if ($value && substr((string) $value, 0, 3) !== '000') { // '', null, false, '0000-00-00', ...
|
||||||
$value = new DateTime($value);
|
$value = new DateTime($value);
|
||||||
$row[$key] = empty($this->formats[$type])
|
$row[$key] = $format ? $value->format($format) : $value;
|
||||||
? $value
|
|
||||||
: $value->format($this->formats[$type]);
|
|
||||||
} else {
|
} else {
|
||||||
$row[$key] = null;
|
$row[$key] = null;
|
||||||
}
|
}
|
||||||
|
|
||||||
} elseif ($type === Type::TIME_INTERVAL) {
|
} elseif ($type === Type::TIME_INTERVAL) {
|
||||||
preg_match('#^(-?)(\d+)\D(\d+)\D(\d+)\z#', $value, $m);
|
preg_match('#^(-?)(\d+)\D(\d+)\D(\d+)\z#', $value, $m);
|
||||||
$row[$key] = new \DateInterval("PT$m[2]H$m[3]M$m[4]S");
|
$value = new \DateInterval("PT$m[2]H$m[3]M$m[4]S");
|
||||||
$row[$key]->invert = (int) (bool) $m[1];
|
$value->invert = (int) (bool) $m[1];
|
||||||
|
$row[$key] = $format ? $value->format($format) : $value;
|
||||||
|
|
||||||
} elseif ($type === Type::BINARY) {
|
} elseif ($type === Type::BINARY) {
|
||||||
$row[$key] = is_string($value)
|
$row[$key] = is_string($value)
|
||||||
@@ -503,15 +508,12 @@ class Result implements IDataSource
|
|||||||
: $value;
|
: $value;
|
||||||
|
|
||||||
} elseif ($type === Type::JSON) {
|
} elseif ($type === Type::JSON) {
|
||||||
if ($this->formats[$type] === 'string') {
|
if ($format === 'string') { // back compatibility with 'native'
|
||||||
$row[$key] = $value;
|
$row[$key] = $value;
|
||||||
} else {
|
} else {
|
||||||
$row[$key] = json_decode($value, $this->formats[$type] === 'array');
|
$row[$key] = json_decode($value, $format === 'array');
|
||||||
}
|
}
|
||||||
|
|
||||||
} elseif ($type === null) {
|
|
||||||
$row[$key] = $value;
|
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
throw new \RuntimeException('Unexpected type ' . $type);
|
throw new \RuntimeException('Unexpected type ' . $type);
|
||||||
}
|
}
|
||||||
@@ -549,7 +551,7 @@ class Result implements IDataSource
|
|||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Sets date format.
|
* Sets type format.
|
||||||
*/
|
*/
|
||||||
final public function setFormat(string $type, ?string $format): self
|
final public function setFormat(string $type, ?string $format): self
|
||||||
{
|
{
|
||||||
@@ -558,6 +560,16 @@ class Result implements IDataSource
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Sets type formats.
|
||||||
|
*/
|
||||||
|
final public function setFormats(array $formats): self
|
||||||
|
{
|
||||||
|
$this->formats = $formats;
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns data format.
|
* Returns data format.
|
||||||
*/
|
*/
|
||||||
|
@@ -53,6 +53,12 @@ class Row implements \ArrayAccess, \IteratorAggregate, \Countable
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public function __isset(string $key): bool
|
||||||
|
{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
/********************* interfaces ArrayAccess, Countable & IteratorAggregate ****************d*g**/
|
/********************* interfaces ArrayAccess, Countable & IteratorAggregate ****************d*g**/
|
||||||
|
|
||||||
|
|
||||||
|
@@ -46,7 +46,7 @@ trait Strict
|
|||||||
public static function __callStatic(string $name, array $args)
|
public static function __callStatic(string $name, array $args)
|
||||||
{
|
{
|
||||||
$rc = new ReflectionClass(static::class);
|
$rc = new ReflectionClass(static::class);
|
||||||
$items = array_intersect($rc->getMethods(ReflectionMethod::IS_PUBLIC), $rc->getMethods(ReflectionMethod::IS_STATIC));
|
$items = array_filter($rc->getMethods(\ReflectionMethod::IS_STATIC), function ($m) { return $m->isPublic(); });
|
||||||
$items = array_map(function ($item) { return $item->getName(); }, $items);
|
$items = array_map(function ($item) { return $item->getName(); }, $items);
|
||||||
$hint = ($t = Helpers::getSuggestion($items, $name))
|
$hint = ($t = Helpers::getSuggestion($items, $name))
|
||||||
? ", did you mean $t()?"
|
? ", did you mean $t()?"
|
||||||
@@ -68,7 +68,7 @@ trait Strict
|
|||||||
return $ret;
|
return $ret;
|
||||||
}
|
}
|
||||||
$rc = new ReflectionClass($this);
|
$rc = new ReflectionClass($this);
|
||||||
$items = array_diff($rc->getProperties(ReflectionProperty::IS_PUBLIC), $rc->getProperties(ReflectionProperty::IS_STATIC));
|
$items = array_filter($rc->getProperties(ReflectionProperty::IS_PUBLIC), function ($p) { return !$p->isStatic(); });
|
||||||
$items = array_map(function ($item) { return $item->getName(); }, $items);
|
$items = array_map(function ($item) { return $item->getName(); }, $items);
|
||||||
$hint = ($t = Helpers::getSuggestion($items, $name))
|
$hint = ($t = Helpers::getSuggestion($items, $name))
|
||||||
? ", did you mean $$t?"
|
? ", did you mean $$t?"
|
||||||
@@ -84,7 +84,7 @@ trait Strict
|
|||||||
public function __set(string $name, $value)
|
public function __set(string $name, $value)
|
||||||
{
|
{
|
||||||
$rc = new ReflectionClass($this);
|
$rc = new ReflectionClass($this);
|
||||||
$items = array_diff($rc->getProperties(ReflectionProperty::IS_PUBLIC), $rc->getProperties(ReflectionProperty::IS_STATIC));
|
$items = array_filter($rc->getProperties(ReflectionProperty::IS_PUBLIC), function ($p) { return !$p->isStatic(); });
|
||||||
$items = array_map(function ($item) { return $item->getName(); }, $items);
|
$items = array_map(function ($item) { return $item->getName(); }, $items);
|
||||||
$hint = ($t = Helpers::getSuggestion($items, $name))
|
$hint = ($t = Helpers::getSuggestion($items, $name))
|
||||||
? ", did you mean $$t?"
|
? ", did you mean $$t?"
|
||||||
|
@@ -25,6 +25,7 @@ declare(strict_types=1);
|
|||||||
* @method static void begin(string $savepoint = null)
|
* @method static void begin(string $savepoint = null)
|
||||||
* @method static void commit(string $savepoint = null)
|
* @method static void commit(string $savepoint = null)
|
||||||
* @method static void rollback(string $savepoint = null)
|
* @method static void rollback(string $savepoint = null)
|
||||||
|
* @method static mixed transaction(callable $callback)
|
||||||
* @method static Dibi\Reflection\Database getDatabaseInfo()
|
* @method static Dibi\Reflection\Database getDatabaseInfo()
|
||||||
* @method static Dibi\Fluent command()
|
* @method static Dibi\Fluent command()
|
||||||
* @method static Dibi\Fluent select(...$args)
|
* @method static Dibi\Fluent select(...$args)
|
||||||
@@ -44,7 +45,7 @@ class dibi
|
|||||||
|
|
||||||
/** version */
|
/** version */
|
||||||
public const
|
public const
|
||||||
VERSION = '4.1.3';
|
VERSION = '4.2.3';
|
||||||
|
|
||||||
/** sorting order */
|
/** sorting order */
|
||||||
public const
|
public const
|
||||||
|
@@ -1,4 +1,4 @@
|
|||||||
# for php-coveralls
|
# for php-coveralls
|
||||||
service_name: travis-ci
|
service_name: github-actions
|
||||||
coverage_clover: coverage.xml
|
coverage_clover: coverage.xml
|
||||||
json_path: coverage.json
|
json_path: coverage.json
|
||||||
|
89
tests/databases.github.ini
Normal file
89
tests/databases.github.ini
Normal file
@@ -0,0 +1,89 @@
|
|||||||
|
[sqlite] ; default
|
||||||
|
driver = sqlite
|
||||||
|
database = :memory:
|
||||||
|
system = sqlite
|
||||||
|
|
||||||
|
[sqlite pdo]
|
||||||
|
driver = pdo
|
||||||
|
dsn = "sqlite::memory:"
|
||||||
|
system = sqlite
|
||||||
|
|
||||||
|
[mysql 5.7]
|
||||||
|
driver = mysqli
|
||||||
|
host = "127.0.0.1"
|
||||||
|
database = dibi_test
|
||||||
|
username = root
|
||||||
|
password = root
|
||||||
|
port = 3306
|
||||||
|
system = mysql
|
||||||
|
|
||||||
|
[mysql 5.7pdo]
|
||||||
|
driver = pdo
|
||||||
|
dsn = "mysql:host=127.0.0.1;port=3306;dbname=dibi_test"
|
||||||
|
user = root
|
||||||
|
password = root
|
||||||
|
system = mysql
|
||||||
|
|
||||||
|
[mysql 8.0]
|
||||||
|
driver = mysqli
|
||||||
|
host = "127.0.0.1"
|
||||||
|
database = dibi_test
|
||||||
|
username = root
|
||||||
|
password = root
|
||||||
|
port = 3307
|
||||||
|
system = mysql
|
||||||
|
|
||||||
|
[mysql 8.0pdo]
|
||||||
|
driver = pdo
|
||||||
|
dsn = "mysql:host=127.0.0.1;port=3307;dbname=dibi_test"
|
||||||
|
user = root
|
||||||
|
password = root
|
||||||
|
system = mysql
|
||||||
|
|
||||||
|
[postgre 9.6]
|
||||||
|
driver = postgre
|
||||||
|
host = "127.0.0.1"
|
||||||
|
database = dibi_test
|
||||||
|
username = postgres
|
||||||
|
password = postgres
|
||||||
|
port = 5432
|
||||||
|
system = postgre
|
||||||
|
|
||||||
|
[postgre 9.6pdo]
|
||||||
|
driver = pdo
|
||||||
|
dsn = "pgsql:host=127.0.0.1;port=5432;dbname=dibi_test"
|
||||||
|
user = postgres
|
||||||
|
password = postgres
|
||||||
|
system = postgre
|
||||||
|
|
||||||
|
[postgre 13]
|
||||||
|
driver = postgre
|
||||||
|
host = "127.0.0.1"
|
||||||
|
database = dibi_test
|
||||||
|
username = postgres
|
||||||
|
password = postgres
|
||||||
|
port = 5433
|
||||||
|
system = postgre
|
||||||
|
|
||||||
|
[postgre 13pdo]
|
||||||
|
driver = pdo
|
||||||
|
dsn = "pgsql:host=127.0.0.1;port=5433;dbname=dibi_test"
|
||||||
|
user = postgres
|
||||||
|
password = postgres
|
||||||
|
system = postgre
|
||||||
|
|
||||||
|
[sqlsrv]
|
||||||
|
driver = sqlsrv
|
||||||
|
host = "localhost"
|
||||||
|
username = SA
|
||||||
|
password = "YourStrong!Passw0rd"
|
||||||
|
database = dibi_test
|
||||||
|
port = 1433
|
||||||
|
system = sqlsrv
|
||||||
|
|
||||||
|
;[sqlsrv pdo]
|
||||||
|
;driver = pdo
|
||||||
|
;dsn = "sqlsrv:Server=localhost,1433;Database=dibi_test"
|
||||||
|
;user = SA
|
||||||
|
;password = "YourStrong!Passw0rd"
|
||||||
|
;system = sqlsrv
|
@@ -1,38 +0,0 @@
|
|||||||
[sqlite] ; default
|
|
||||||
driver = sqlite
|
|
||||||
database = :memory:
|
|
||||||
system = sqlite
|
|
||||||
|
|
||||||
[sqlite pdo]
|
|
||||||
driver = pdo
|
|
||||||
dsn = "sqlite::memory:"
|
|
||||||
system = sqlite
|
|
||||||
|
|
||||||
[mysql improved]
|
|
||||||
driver = mysqli
|
|
||||||
host = 127.0.0.1
|
|
||||||
username = root
|
|
||||||
password =
|
|
||||||
charset = utf8
|
|
||||||
system = mysql
|
|
||||||
|
|
||||||
[mysql pdo]
|
|
||||||
driver = pdo
|
|
||||||
dsn = "mysql:host=127.0.0.1"
|
|
||||||
username = root
|
|
||||||
password =
|
|
||||||
system = mysql
|
|
||||||
|
|
||||||
[postgre]
|
|
||||||
driver = postgre
|
|
||||||
host = 127.0.0.1
|
|
||||||
username = postgres
|
|
||||||
password =
|
|
||||||
system = postgre
|
|
||||||
|
|
||||||
[postgre pdo]
|
|
||||||
driver = pdo
|
|
||||||
dsn = "pgsql:host=127.0.0.1;dbname=dibi_test"
|
|
||||||
username = postgres
|
|
||||||
password =
|
|
||||||
system = postgre
|
|
@@ -30,21 +30,102 @@ Assert::exception(function () use ($conn) {
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
|
|
||||||
$conn->begin();
|
test('begin() & rollback()', function () use ($conn) {
|
||||||
Assert::same(3, (int) $conn->query('SELECT COUNT(*) FROM [products]')->fetchSingle());
|
$conn->begin();
|
||||||
$conn->query('INSERT INTO [products]', [
|
Assert::same(3, (int) $conn->query('SELECT COUNT(*) FROM [products]')->fetchSingle());
|
||||||
'title' => 'Test product',
|
$conn->query('INSERT INTO [products]', [
|
||||||
]);
|
'title' => 'Test product',
|
||||||
Assert::same(4, (int) $conn->query('SELECT COUNT(*) FROM [products]')->fetchSingle());
|
]);
|
||||||
$conn->rollback();
|
Assert::same(4, (int) $conn->query('SELECT COUNT(*) FROM [products]')->fetchSingle());
|
||||||
Assert::same(3, (int) $conn->query('SELECT COUNT(*) FROM [products]')->fetchSingle());
|
$conn->rollback();
|
||||||
|
Assert::same(3, (int) $conn->query('SELECT COUNT(*) FROM [products]')->fetchSingle());
|
||||||
|
});
|
||||||
|
|
||||||
|
|
||||||
|
test('begin() & commit()', function () use ($conn) {
|
||||||
|
$conn->begin();
|
||||||
|
$conn->query('INSERT INTO [products]', [
|
||||||
|
'title' => 'Test product',
|
||||||
|
]);
|
||||||
|
$conn->commit();
|
||||||
|
Assert::same(4, (int) $conn->query('SELECT COUNT(*) FROM [products]')->fetchSingle());
|
||||||
|
});
|
||||||
|
|
||||||
|
|
||||||
$conn->begin();
|
test('transaction() fail', function () use ($conn) {
|
||||||
$conn->query('INSERT INTO [products]', [
|
Assert::exception(function () use ($conn) {
|
||||||
'title' => 'Test product',
|
$conn->transaction(function (Dibi\Connection $connection) {
|
||||||
]);
|
$connection->query('INSERT INTO [products]', [
|
||||||
$conn->commit();
|
'title' => 'Test product',
|
||||||
Assert::same(4, (int) $conn->query('SELECT COUNT(*) FROM [products]')->fetchSingle());
|
]);
|
||||||
|
throw new Exception('my exception');
|
||||||
|
});
|
||||||
|
}, \Throwable::class, 'my exception');
|
||||||
|
Assert::same(4, (int) $conn->query('SELECT COUNT(*) FROM [products]')->fetchSingle());
|
||||||
|
});
|
||||||
|
|
||||||
|
|
||||||
|
test('transaction() success', function () use ($conn) {
|
||||||
|
$conn->transaction(function (Dibi\Connection $connection) {
|
||||||
|
$connection->query('INSERT INTO [products]', [
|
||||||
|
'title' => 'Test product',
|
||||||
|
]);
|
||||||
|
});
|
||||||
|
Assert::same(5, (int) $conn->query('SELECT COUNT(*) FROM [products]')->fetchSingle());
|
||||||
|
});
|
||||||
|
|
||||||
|
|
||||||
|
test('nested transaction() call fail', function () use ($conn) {
|
||||||
|
Assert::exception(function () use ($conn) {
|
||||||
|
$conn->transaction(function (Dibi\Connection $connection) {
|
||||||
|
$connection->query('INSERT INTO [products]', [
|
||||||
|
'title' => 'Test product',
|
||||||
|
]);
|
||||||
|
|
||||||
|
$connection->transaction(function (Dibi\Connection $connection2) {
|
||||||
|
$connection2->query('INSERT INTO [products]', [
|
||||||
|
'title' => 'Test product',
|
||||||
|
]);
|
||||||
|
throw new Exception('my exception');
|
||||||
|
});
|
||||||
|
});
|
||||||
|
}, \Throwable::class, 'my exception');
|
||||||
|
Assert::same(5, (int) $conn->query('SELECT COUNT(*) FROM [products]')->fetchSingle());
|
||||||
|
});
|
||||||
|
|
||||||
|
|
||||||
|
test('nested transaction() call success', function () use ($conn) {
|
||||||
|
$conn->transaction(function (Dibi\Connection $connection) {
|
||||||
|
$connection->query('INSERT INTO [products]', [
|
||||||
|
'title' => 'Test product',
|
||||||
|
]);
|
||||||
|
|
||||||
|
$connection->transaction(function (Dibi\Connection $connection2) {
|
||||||
|
$connection2->query('INSERT INTO [products]', [
|
||||||
|
'title' => 'Test product',
|
||||||
|
]);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
Assert::same(7, (int) $conn->query('SELECT COUNT(*) FROM [products]')->fetchSingle());
|
||||||
|
});
|
||||||
|
|
||||||
|
|
||||||
|
test('begin(), commit() & rollback() calls are forbidden in transaction()', function () use ($conn) {
|
||||||
|
Assert::exception(function () use ($conn) {
|
||||||
|
$conn->transaction(function (Dibi\Connection $connection) {
|
||||||
|
$connection->begin();
|
||||||
|
});
|
||||||
|
}, \LogicException::class, Dibi\Connection::class . '::begin() call is forbidden inside a transaction() callback');
|
||||||
|
|
||||||
|
Assert::exception(function () use ($conn) {
|
||||||
|
$conn->transaction(function (Dibi\Connection $connection) {
|
||||||
|
$connection->commit();
|
||||||
|
});
|
||||||
|
}, \LogicException::class, Dibi\Connection::class . '::commit() call is forbidden inside a transaction() callback');
|
||||||
|
|
||||||
|
Assert::exception(function () use ($conn) {
|
||||||
|
$conn->transaction(function (Dibi\Connection $connection) {
|
||||||
|
$connection->rollback();
|
||||||
|
});
|
||||||
|
}, \LogicException::class, Dibi\Connection::class . '::rollback() call is forbidden inside a transaction() callback');
|
||||||
|
});
|
||||||
|
@@ -32,7 +32,7 @@ Assert::same(
|
|||||||
);
|
);
|
||||||
|
|
||||||
|
|
||||||
if (!in_array($config['driver'], ['sqlite', 'pdo', 'sqlsrv'], true)) {
|
if (!in_array($config['driver'], ['sqlite', 'sqlite3', 'pdo', 'sqlsrv'], true)) {
|
||||||
Assert::same(
|
Assert::same(
|
||||||
['products.product_id', 'orders.order_id', 'customers.name', 'xXx'],
|
['products.product_id', 'orders.order_id', 'customers.name', 'xXx'],
|
||||||
$info->getColumnNames(true)
|
$info->getColumnNames(true)
|
||||||
@@ -43,7 +43,7 @@ if (!in_array($config['driver'], ['sqlite', 'pdo', 'sqlsrv'], true)) {
|
|||||||
$columns = $info->getColumns();
|
$columns = $info->getColumns();
|
||||||
|
|
||||||
Assert::same('product_id', $columns[0]->getName());
|
Assert::same('product_id', $columns[0]->getName());
|
||||||
if (!in_array($config['driver'], ['sqlite', 'pdo', 'sqlsrv'], true)) {
|
if (!in_array($config['driver'], ['sqlite', 'sqlite3', 'pdo', 'sqlsrv'], true)) {
|
||||||
Assert::same('products', $columns[0]->getTableName());
|
Assert::same('products', $columns[0]->getTableName());
|
||||||
}
|
}
|
||||||
Assert::null($columns[0]->getVendorInfo('xxx'));
|
Assert::null($columns[0]->getVendorInfo('xxx'));
|
||||||
|
@@ -24,6 +24,17 @@ class MockResult extends Dibi\Result
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
test('', function () {
|
||||||
|
$result = new MockResult;
|
||||||
|
$result->setType('col', Type::TEXT);
|
||||||
|
$result->setFormat(Type::TEXT, 'native');
|
||||||
|
|
||||||
|
Assert::same(['col' => null], $result->test(['col' => null]));
|
||||||
|
Assert::same(['col' => true], $result->test(['col' => true]));
|
||||||
|
Assert::same(['col' => false], $result->test(['col' => false]));
|
||||||
|
});
|
||||||
|
|
||||||
|
|
||||||
test('', function () {
|
test('', function () {
|
||||||
$result = new MockResult;
|
$result = new MockResult;
|
||||||
$result->setType('col', Type::BOOL);
|
$result->setType('col', Type::BOOL);
|
||||||
|
@@ -35,6 +35,10 @@ Assert::error(function () use ($row) {
|
|||||||
Assert::false(isset($row->missing));
|
Assert::false(isset($row->missing));
|
||||||
Assert::false(isset($row['missing']));
|
Assert::false(isset($row['missing']));
|
||||||
|
|
||||||
|
// ??
|
||||||
|
Assert::same(123, $row->missing ?? 123);
|
||||||
|
Assert::same(123, $row['missing'] ?? 123);
|
||||||
|
|
||||||
|
|
||||||
// suggestions
|
// suggestions
|
||||||
Assert::error(function () use ($row) {
|
Assert::error(function () use ($row) {
|
||||||
|
@@ -1,7 +1,7 @@
|
|||||||
<?php
|
<?php
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @dataProvider ../databases.ini
|
* @dataProvider ../databases.ini !=sqlsrv
|
||||||
*/
|
*/
|
||||||
|
|
||||||
declare(strict_types=1);
|
declare(strict_types=1);
|
||||||
|
@@ -23,6 +23,10 @@ try {
|
|||||||
$config = reset($config);
|
$config = reset($config);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (isset($config['port'])) {
|
||||||
|
$config['port'] = (int) $config['port'];
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
// lock
|
// lock
|
||||||
define('TEMP_DIR', __DIR__ . '/../tmp');
|
define('TEMP_DIR', __DIR__ . '/../tmp');
|
||||||
|
@@ -36,7 +36,7 @@ Assert::same('SELECT', $e->getSql());
|
|||||||
|
|
||||||
$e = Assert::exception(function () use ($conn) {
|
$e = Assert::exception(function () use ($conn) {
|
||||||
$conn->query('INSERT INTO products (product_id, title) VALUES (1, "New")');
|
$conn->query('INSERT INTO products (product_id, title) VALUES (1, "New")');
|
||||||
}, Dibi\UniqueConstraintViolationException::class, "%a?%Duplicate entry '1' for key 'PRIMARY'", 1062);
|
}, Dibi\UniqueConstraintViolationException::class, "%a?%Duplicate entry '1' for key '%a?%PRIMARY'", 1062);
|
||||||
|
|
||||||
Assert::same("INSERT INTO products (product_id, title) VALUES (1, 'New')", $e->getSql());
|
Assert::same("INSERT INTO products (product_id, title) VALUES (1, 'New')", $e->getSql());
|
||||||
|
|
||||||
|
@@ -31,7 +31,7 @@ Assert::same("INSERT INTO products (product_id, title) VALUES (1, 'New')", $e->g
|
|||||||
|
|
||||||
$e = Assert::exception(function () use ($conn) {
|
$e = Assert::exception(function () use ($conn) {
|
||||||
$conn->query('INSERT INTO products (title) VALUES (NULL)');
|
$conn->query('INSERT INTO products (title) VALUES (NULL)');
|
||||||
}, Dibi\NotNullConstraintViolationException::class, '%a?%null value in column "title" violates not-null constraint%A?%', '23502');
|
}, Dibi\NotNullConstraintViolationException::class, '%a?%null value in column "title"%a%violates not-null constraint%A?%', '23502');
|
||||||
|
|
||||||
Assert::same('INSERT INTO products (title) VALUES (NULL)', $e->getSql());
|
Assert::same('INSERT INTO products (title) VALUES (NULL)', $e->getSql());
|
||||||
|
|
||||||
|
@@ -12,5 +12,5 @@ extension=php_pdo_pgsql.dll
|
|||||||
extension=php_pdo_sqlite.dll
|
extension=php_pdo_sqlite.dll
|
||||||
extension=php_pgsql.dll
|
extension=php_pgsql.dll
|
||||||
extension=php_sqlite3.dll
|
extension=php_sqlite3.dll
|
||||||
extension=php_sqlsrv_71_ts.dll
|
extension=php_sqlsrv_ts.dll
|
||||||
extension=php_odbc.dll
|
extension=php_odbc.dll
|
||||||
|
Reference in New Issue
Block a user