1
0
mirror of https://github.com/dg/dibi.git synced 2025-09-01 10:02:53 +02:00

Compare commits

..

3 Commits
v3.2.2 ... v3.1

Author SHA1 Message Date
David Grudl
1809885d30 travis: uses NCS 2 2018-09-17 13:29:16 +02:00
David Grudl
e1256eca87 cs 2018-09-17 13:29:16 +02:00
David Grudl
f26bb27504 loader: added missing classes 2018-09-17 13:23:09 +02:00
69 changed files with 390 additions and 455 deletions

View File

@@ -33,12 +33,12 @@ jobs:
php: 7.1
install:
# Install Nette Code Checker
- travis_retry composer create-project nette/code-checker temp/code-checker ~2 --no-progress
- travis_retry composer create-project nette/code-checker temp/code-checker ^3 --no-progress
# Install Nette Coding Standard
- travis_retry composer create-project nette/coding-standard temp/coding-standard --no-progress
- travis_retry composer create-project nette/coding-standard temp/coding-standard ^2 --no-progress
script:
- php temp/code-checker/src/code-checker.php --short-arrays
- php temp/coding-standard/ecs check src tests examples --config temp/coding-standard/coding-standard-php56.neon
- php temp/code-checker/code-checker
- php temp/coding-standard/ecs check src tests examples --config temp/coding-standard/coding-standard-php56.yml
- stage: Code Coverage

View File

@@ -56,4 +56,4 @@ test_script:
on_failure:
# Print *.actual content
- for /r %%x in (*.actual) do ( type "%%x" )
- type tests\dibi\output\*.actual

View File

@@ -26,7 +26,7 @@
},
"extra": {
"branch-alias": {
"dev-master": "3.2-dev"
"dev-master": "3.1-dev"
}
}
}

View File

@@ -1,15 +1,13 @@
<!DOCTYPE html><link rel="stylesheet" href="data/style.css">
<h1>Connecting to Databases | Dibi</h1>
<h1>Connecting to Databases | dibi</h1>
<?php
if (@!include __DIR__ . '/../vendor/autoload.php') {
die('Install packages using `composer install`');
}
require __DIR__ . '/../src/loader.php';
// connects to SQlite using Dibi class
// connects to SQlite using dibi class
echo '<p>Connecting to Sqlite: ';
try {
dibi::connect([

View File

@@ -1,22 +1,20 @@
<!DOCTYPE html><link rel="stylesheet" href="data/style.css">
<h1>Database Reflection | Dibi</h1>
<h1>Database Reflection | dibi</h1>
<?php
if (@!include __DIR__ . '/../vendor/autoload.php') {
die('Install packages using `composer install`');
}
require __DIR__ . '/../src/loader.php';
$dibi = new Dibi\Connection([
dibi::connect([
'driver' => 'sqlite3',
'database' => 'data/sample.s3db',
]);
// retrieve database reflection
$database = $dibi->getDatabaseInfo();
$database = dibi::getDatabaseInfo();
echo "<h2>Database '{$database->getName()}'</h2>\n";
echo "<ul>\n";

View File

@@ -1,21 +1,19 @@
<!DOCTYPE html><link rel="stylesheet" href="data/style.css">
<h1>Dumping SQL and Result Set | Dibi</h1>
<h1>Dumping SQL and Result Set | dibi</h1>
<?php
if (@!include __DIR__ . '/../vendor/autoload.php') {
die('Install packages using `composer install`');
}
require __DIR__ . '/../src/loader.php';
$dibi = new Dibi\Connection([
dibi::connect([
'driver' => 'sqlite3',
'database' => 'data/sample.s3db',
]);
$res = $dibi->query('
$res = dibi::query('
SELECT * FROM products
INNER JOIN orders USING (product_id)
INNER JOIN customers USING (customer_id)

View File

@@ -9,11 +9,11 @@ Tracy\Debugger::enable();
?>
<!DOCTYPE html><link rel="stylesheet" href="data/style.css">
<h1>Fetching Examples | Dibi</h1>
<h1>Fetching Examples | dibi</h1>
<?php
$dibi = new Dibi\Connection([
dibi::connect([
'driver' => 'sqlite3',
'database' => 'data/sample.s3db',
]);
@@ -33,46 +33,46 @@ product_id | title
// fetch a single row
echo "<h2>fetch()</h2>\n";
$row = $dibi->fetch('SELECT title FROM products');
$row = dibi::fetch('SELECT title FROM products');
Tracy\Dumper::dump($row); // Chair
// fetch a single value
echo "<h2>fetchSingle()</h2>\n";
$value = $dibi->fetchSingle('SELECT title FROM products');
$value = dibi::fetchSingle('SELECT title FROM products');
Tracy\Dumper::dump($value); // Chair
// fetch complete result set
echo "<h2>fetchAll()</h2>\n";
$all = $dibi->fetchAll('SELECT * FROM products');
$all = dibi::fetchAll('SELECT * FROM products');
Tracy\Dumper::dump($all);
// fetch complete result set like association array
echo "<h2>fetchAssoc('title')</h2>\n";
$res = $dibi->query('SELECT * FROM products');
$res = dibi::query('SELECT * FROM products');
$assoc = $res->fetchAssoc('title'); // key
Tracy\Dumper::dump($assoc);
// fetch complete result set like pairs key => value
echo "<h2>fetchPairs('product_id', 'title')</h2>\n";
$res = $dibi->query('SELECT * FROM products');
$res = dibi::query('SELECT * FROM products');
$pairs = $res->fetchPairs('product_id', 'title');
Tracy\Dumper::dump($pairs);
// fetch row by row
echo "<h2>using foreach</h2>\n";
$res = $dibi->query('SELECT * FROM products');
$res = dibi::query('SELECT * FROM products');
foreach ($res as $n => $row) {
Tracy\Dumper::dump($row);
}
// more complex association array
$res = $dibi->query('
$res = dibi::query('
SELECT *
FROM products
INNER JOIN orders USING (product_id)
@@ -84,11 +84,11 @@ $assoc = $res->fetchAssoc('name|title'); // key
Tracy\Dumper::dump($assoc);
echo "<h2>fetchAssoc('name[]title')</h2>\n";
$res = $dibi->query('SELECT * FROM products INNER JOIN orders USING (product_id) INNER JOIN customers USING (customer_id)');
$res = dibi::query('SELECT * FROM products INNER JOIN orders USING (product_id) INNER JOIN customers USING (customer_id)');
$assoc = $res->fetchAssoc('name[]title'); // key
Tracy\Dumper::dump($assoc);
echo "<h2>fetchAssoc('name->title')</h2>\n";
$res = $dibi->query('SELECT * FROM products INNER JOIN orders USING (product_id) INNER JOIN customers USING (customer_id)');
$res = dibi::query('SELECT * FROM products INNER JOIN orders USING (product_id) INNER JOIN customers USING (customer_id)');
$assoc = $res->fetchAssoc('name->title'); // key
Tracy\Dumper::dump($assoc);

View File

@@ -1,20 +1,18 @@
<!DOCTYPE html><link rel="stylesheet" href="data/style.css">
<h1>Importing SQL Dump from File | Dibi</h1>
<h1>Importing SQL Dump from File | dibi</h1>
<?php
if (@!include __DIR__ . '/../vendor/autoload.php') {
die('Install packages using `composer install`');
}
require __DIR__ . '/../src/loader.php';
$dibi = new Dibi\Connection([
dibi::connect([
'driver' => 'sqlite3',
'database' => 'data/sample.s3db',
]);
$count = $dibi->loadFile('compress.zlib://data/sample.dump.sql.gz');
$count = dibi::loadFile('compress.zlib://data/sample.dump.sql.gz');
echo 'Number of SQL commands:', $count;

View File

@@ -1,15 +1,13 @@
<!DOCTYPE html><link rel="stylesheet" href="data/style.css">
<h1>Query Language & Conditions | Dibi</h1>
<h1>Query Language & Conditions | dibi</h1>
<?php
if (@!include __DIR__ . '/../vendor/autoload.php') {
die('Install packages using `composer install`');
}
require __DIR__ . '/../src/loader.php';
$dibi = new Dibi\Connection([
dibi::connect([
'driver' => 'sqlite3',
'database' => 'data/sample.s3db',
]);
@@ -25,7 +23,7 @@ $bar = 2;
$name = $cond1 ? 'K%' : null;
// if & end
$dibi->test('
dibi::test('
SELECT *
FROM customers
%if', isset($name), 'WHERE name LIKE ?', $name, '%end'
@@ -34,7 +32,7 @@ $dibi->test('
// if & else & (optional) end
$dibi->test('
dibi::test('
SELECT *
FROM people
WHERE id > 0
@@ -45,7 +43,7 @@ $dibi->test('
// nested condition
$dibi->test('
dibi::test('
SELECT *
FROM customers
WHERE
@@ -57,7 +55,7 @@ $dibi->test('
// IF()
$dibi->test('UPDATE products SET', [
'price' => $dibi->expression('IF(price_fixed, price, ?)', 123),
dibi::test('UPDATE products SET', [
'price' => ['IF(price_fixed, price, ?)', 123],
]);
// -> SELECT * FROM customers WHERE LIMIT 10

View File

@@ -1,17 +1,15 @@
<!DOCTYPE html><link rel="stylesheet" href="data/style.css">
<h1>Query Language Basic Examples | Dibi</h1>
<h1>Query Language Basic Examples | dibi</h1>
<?php
if (@!include __DIR__ . '/../vendor/autoload.php') {
die('Install packages using `composer install`');
}
require __DIR__ . '/../src/loader.php';
date_default_timezone_set('Europe/Prague');
$dibi = new Dibi\Connection([
dibi::connect([
'driver' => 'sqlite3',
'database' => 'data/sample.s3db',
]);
@@ -21,7 +19,7 @@ $dibi = new Dibi\Connection([
$ipMask = '192.168.%';
$timestamp = mktime(0, 0, 0, 10, 13, 1997);
$dibi->test('
dibi::test('
SELECT COUNT(*) as [count]
FROM [comments]
WHERE [ip] LIKE ?', $ipMask, '
@@ -31,7 +29,7 @@ $dibi->test('
// dibi detects INSERT or REPLACE command
$dibi->test('
dibi::test('
REPLACE INTO products', [
'title' => 'Super product',
'price' => 318,
@@ -47,12 +45,12 @@ $array = [
'brand' => null,
'created' => new DateTime,
];
$dibi->test('INSERT INTO products', $array, $array, $array);
dibi::test('INSERT INTO products', $array, $array, $array);
// -> INSERT INTO products ([title], [price], [brand], [created]) VALUES ('Super Product', ...) , (...) , (...)
// dibi detects UPDATE command
$dibi->test('
dibi::test('
UPDATE colors SET', [
'color' => 'blue',
'order' => 12,
@@ -63,7 +61,7 @@ $dibi->test('
// modifier applied to array
$array = [1, 2, 3];
$dibi->test('
dibi::test('
SELECT *
FROM people
WHERE id IN (?)', $array
@@ -76,7 +74,7 @@ $order = [
'field1' => 'asc',
'field2' => 'desc',
];
$dibi->test('
dibi::test('
SELECT *
FROM people
ORDER BY %by', $order, '
@@ -85,5 +83,5 @@ $dibi->test('
// indentifiers and strings syntax mix
$dibi->test('UPDATE [table] SET `item` = "5 1/4"" diskette"');
dibi::test('UPDATE [table] SET `item` = "5 1/4"" diskette"');
// -> UPDATE [table] SET [item] = '5 1/4" diskette'

View File

@@ -13,18 +13,18 @@ date_default_timezone_set('Europe/Prague');
?>
<!DOCTYPE html><link rel="stylesheet" href="data/style.css">
<h1>Result Set Data Types | Dibi</h1>
<h1>Result Set Data Types | dibi</h1>
<?php
$dibi = new Dibi\Connection([
dibi::connect([
'driver' => 'sqlite3',
'database' => 'data/sample.s3db',
]);
// using manual hints
$res = $dibi->query('SELECT * FROM [customers]');
$res = dibi::query('SELECT * FROM [customers]');
$res->setType('customer_id', Type::INTEGER)
->setType('added', Type::DATETIME)
@@ -40,7 +40,7 @@ Tracy\Dumper::dump($res->fetch());
// using auto-detection (works well with MySQL or other strictly typed databases)
$res = $dibi->query('SELECT * FROM [customers]');
$res = dibi::query('SELECT * FROM [customers]');
Tracy\Dumper::dump($res->fetch());
// outputs:

View File

@@ -9,7 +9,7 @@ if (@!include __DIR__ . '/../vendor/autoload.php') {
Tracy\Debugger::enable();
$dibi = new Dibi\Connection([
$connection = dibi::connect([
'driver' => 'sqlite3',
'database' => 'data/sample.s3db',
'profiler' => [
@@ -20,11 +20,11 @@ $dibi = new Dibi\Connection([
// add panel to debug bar
$panel = new Dibi\Bridges\Tracy\Panel;
$panel->register($dibi);
$panel->register($connection);
// throws error because SQL is bad
$dibi->query('SELECT FROM customers WHERE customer_id < ?', 38);
dibi::query('SELECT FROM customers WHERE customer_id < ?', 38);
?><!DOCTYPE html><link rel="stylesheet" href="data/style.css">

View File

@@ -9,7 +9,7 @@ if (@!include __DIR__ . '/../vendor/autoload.php') {
Tracy\Debugger::enable();
$dibi = new Dibi\Connection([
$connection = dibi::connect([
'driver' => 'sqlite3',
'database' => 'data/sample.s3db',
'profiler' => [
@@ -20,14 +20,14 @@ $dibi = new Dibi\Connection([
// add panel to debug bar
$panel = new Dibi\Bridges\Tracy\Panel;
$panel->register($dibi);
$panel->register($connection);
// query will be logged
$dibi->query('SELECT 123');
dibi::query('SELECT 123');
// result set will be dumped
Tracy\Debugger::barDump($dibi->fetchAll('SELECT * FROM customers WHERE customer_id < ?', 38), '[customers]');
Tracy\Debugger::barDump(dibi::fetchAll('SELECT * FROM customers WHERE customer_id < ?', 38), '[customers]');
?>

View File

@@ -1,18 +1,16 @@
<!DOCTYPE html><link rel="stylesheet" href="data/style.css">
<h1>Using DateTime | Dibi</h1>
<h1>Using DateTime | dibi</h1>
<?php
if (@!include __DIR__ . '/../vendor/autoload.php') {
die('Install packages using `composer install`');
}
require __DIR__ . '/../src/loader.php';
date_default_timezone_set('Europe/Prague');
// CHANGE TO REAL PARAMETERS!
$dibi = new Dibi\Connection([
dibi::connect([
'driver' => 'sqlite3',
'database' => 'data/sample.s3db',
'formatDate' => "'Y-m-d'",
@@ -21,7 +19,7 @@ $dibi = new Dibi\Connection([
// generate and dump SQL
$dibi->test('
dibi::test('
INSERT INTO [mytable]', [
'id' => 123,
'date' => new DateTime('12.3.2007'),

View File

@@ -9,11 +9,11 @@ Tracy\Debugger::enable();
?>
<!DOCTYPE html><link rel="stylesheet" href="data/style.css">
<h1>Using Extension Methods | Dibi</h1>
<h1>Using Extension Methods | dibi</h1>
<?php
$dibi = new Dibi\Connection([
dibi::connect([
'driver' => 'sqlite3',
'database' => 'data/sample.s3db',
]);
@@ -28,6 +28,6 @@ Dibi\Result::extensionMethod('fetchShuffle', function (Dibi\Result $obj) {
// fetch complete result set shuffled
$res = $dibi->query('SELECT * FROM [customers]');
$res = dibi::query('SELECT * FROM [customers]');
$all = $res->fetchShuffle();
Tracy\Dumper::dump($all);

View File

@@ -1,17 +1,15 @@
<!DOCTYPE html><link rel="stylesheet" href="data/style.css">
<h1>Using Fluent Syntax | Dibi</h1>
<h1>Using Fluent Syntax | dibi</h1>
<?php
if (@!include __DIR__ . '/../vendor/autoload.php') {
die('Install packages using `composer install`');
}
require __DIR__ . '/../src/loader.php';
date_default_timezone_set('Europe/Prague');
$dibi = new Dibi\Connection([
dibi::connect([
'driver' => 'sqlite3',
'database' => 'data/sample.s3db',
]);
@@ -25,7 +23,7 @@ $record = [
];
// SELECT ...
$dibi->select('product_id')->as('id')
dibi::select('product_id')->as('id')
->select('title')
->from('products')
->innerJoin('orders')->using('(product_id)')
@@ -37,35 +35,35 @@ $dibi->select('product_id')->as('id')
// SELECT ...
echo $dibi->select('title')->as('id')
echo dibi::select('title')->as('id')
->from('products')
->fetchSingle();
// -> Chair (as result of query: SELECT [title] AS [id] FROM [products])
// INSERT ...
$dibi->insert('products', $record)
dibi::insert('products', $record)
->setFlag('IGNORE')
->test();
// -> INSERT IGNORE INTO [products] ([title], [price], [active]) VALUES ('Super product', 318, 1)
// UPDATE ...
$dibi->update('products', $record)
dibi::update('products', $record)
->where('product_id = ?', $id)
->test();
// -> UPDATE [products] SET [title]='Super product', [price]=318, [active]=1 WHERE product_id = 10
// DELETE ...
$dibi->delete('products')
dibi::delete('products')
->where('product_id = ?', $id)
->test();
// -> DELETE FROM [products] WHERE product_id = 10
// custom commands
$dibi->command()
dibi::command()
->update('products')
->where('product_id = ?', $id)
->set($record)
@@ -73,7 +71,7 @@ $dibi->command()
// -> UPDATE [products] SET [title]='Super product', [price]=318, [active]=1 WHERE product_id = 10
$dibi->command()
dibi::command()
->truncate('products')
->test();
// -> TRUNCATE [products]

View File

@@ -1,30 +1,28 @@
<!DOCTYPE html><link rel="stylesheet" href="data/style.css">
<h1>Using Limit & Offset | Dibi</h1>
<h1>Using Limit & Offset | dibi</h1>
<?php
if (@!include __DIR__ . '/../vendor/autoload.php') {
die('Install packages using `composer install`');
}
require __DIR__ . '/../src/loader.php';
$dibi = new Dibi\Connection([
dibi::connect([
'driver' => 'sqlite3',
'database' => 'data/sample.s3db',
]);
// no limit
$dibi->test('SELECT * FROM [products]');
dibi::test('SELECT * FROM [products]');
// -> SELECT * FROM [products]
// with limit = 2
$dibi->test('SELECT * FROM [products] %lmt', 2);
dibi::test('SELECT * FROM [products] %lmt', 2);
// -> SELECT * FROM [products] LIMIT 2
// with limit = 2, offset = 1
$dibi->test('SELECT * FROM [products] %lmt %ofs', 2, 1);
dibi::test('SELECT * FROM [products] %lmt %ofs', 2, 1);
// -> SELECT * FROM [products] LIMIT 2 OFFSET 1

View File

@@ -1,17 +1,15 @@
<!DOCTYPE html><link rel="stylesheet" href="data/style.css">
<h1>Using Logger | Dibi</h1>
<h1>Using Logger | dibi</h1>
<?php
if (@!include __DIR__ . '/../vendor/autoload.php') {
die('Install packages using `composer install`');
}
require __DIR__ . '/../src/loader.php';
date_default_timezone_set('Europe/Prague');
$dibi = new Dibi\Connection([
dibi::connect([
'driver' => 'sqlite3',
'database' => 'data/sample.s3db',
// enable query logging to this file
@@ -23,11 +21,11 @@ $dibi = new Dibi\Connection([
try {
$res = $dibi->query('SELECT * FROM [customers] WHERE [customer_id] = ?', 1);
$res = dibi::query('SELECT * FROM [customers] WHERE [customer_id] = ?', 1);
$res = $dibi->query('SELECT * FROM [customers] WHERE [customer_id] < ?', 5);
$res = dibi::query('SELECT * FROM [customers] WHERE [customer_id] < ?', 5);
$res = $dibi->query('SELECT FROM [customers] WHERE [customer_id] < ?', 38);
$res = dibi::query('SELECT FROM [customers] WHERE [customer_id] < ?', 38);
} catch (Dibi\Exception $e) {
echo '<p>', get_class($e), ': ', $e->getMessage(), '</p>';
}

View File

@@ -2,16 +2,14 @@
<!DOCTYPE html><link rel="stylesheet" href="data/style.css">
<h1>Using Profiler | Dibi</h1>
<h1>Using Profiler | dibi</h1>
<?php
if (@!include __DIR__ . '/../vendor/autoload.php') {
die('Install packages using `composer install`');
}
require __DIR__ . '/../src/loader.php';
$dibi = new Dibi\Connection([
dibi::connect([
'driver' => 'sqlite3',
'database' => 'data/sample.s3db',
'profiler' => [
@@ -22,7 +20,7 @@ $dibi = new Dibi\Connection([
// execute some queries...
for ($i = 0; $i < 20; $i++) {
$res = $dibi->query('SELECT * FROM [customers] WHERE [customer_id] < ?', $i);
$res = dibi::query('SELECT * FROM [customers] WHERE [customer_id] < ?', $i);
}
// display output

View File

@@ -1,31 +1,29 @@
<!DOCTYPE html><link rel="stylesheet" href="data/style.css">
<h1>Using Substitutions | Dibi</h1>
<h1>Using Substitutions | dibi</h1>
<?php
if (@!include __DIR__ . '/../vendor/autoload.php') {
die('Install packages using `composer install`');
}
require __DIR__ . '/../src/loader.php';
$dibi = new Dibi\Connection([
dibi::connect([
'driver' => 'sqlite3',
'database' => 'data/sample.s3db',
]);
// create new substitution :blog: ==> wp_
$dibi->getSubstitutes()->blog = 'wp_';
dibi::getSubstitutes()->blog = 'wp_';
$dibi->test('SELECT * FROM [:blog:items]');
dibi::test('SELECT * FROM [:blog:items]');
// -> SELECT * FROM [wp_items]
// create new substitution :: (empty) ==> my_
$dibi->getSubstitutes()->{''} = 'my_';
dibi::getSubstitutes()->{''} = 'my_';
$dibi->test("UPDATE ::table SET [text]='Hello World'");
dibi::test("UPDATE ::table SET [text]='Hello World'");
// -> UPDATE my_table SET [text]='Hello World'
@@ -42,13 +40,13 @@ function substFallBack($expr)
// define callback
$dibi->getSubstitutes()->setCallback('substFallBack');
dibi::getSubstitutes()->setCallback('substFallBack');
// define substitutes as constants
define('SUBST_ACCOUNT', 'eshop_');
define('SUBST_ACTIVE', 7);
$dibi->test("
dibi::test("
UPDATE :account:user
SET name='John Doe', status=:active:
WHERE id=", 7

View File

@@ -1,36 +1,34 @@
<!DOCTYPE html><link rel="stylesheet" href="data/style.css">
<h1>Using Transactions | Dibi</h1>
<h1>Using Transactions | dibi</h1>
<?php
if (@!include __DIR__ . '/../vendor/autoload.php') {
die('Install packages using `composer install`');
}
require __DIR__ . '/../src/loader.php';
$dibi = new Dibi\Connection([
dibi::connect([
'driver' => 'sqlite3',
'database' => 'data/sample.s3db',
]);
echo "<h2>Before</h2>\n";
$dibi->query('SELECT * FROM [products]')->dump();
dibi::query('SELECT * FROM [products]')->dump();
// -> 3 rows
$dibi->begin();
$dibi->query('INSERT INTO [products]', [
dibi::begin();
dibi::query('INSERT INTO [products]', [
'title' => 'Test product',
]);
echo "<h2>After INSERT</h2>\n";
$dibi->query('SELECT * FROM [products]')->dump();
dibi::query('SELECT * FROM [products]')->dump();
$dibi->rollback(); // or $dibi->commit();
dibi::rollback(); // or dibi::commit();
echo "<h2>After rollback</h2>\n";
$dibi->query('SELECT * FROM [products]')->dump();
dibi::query('SELECT * FROM [products]')->dump();
// -> 3 rows again

View File

@@ -7,28 +7,22 @@
[![Latest Stable Version](https://poser.pugx.org/dibi/dibi/v/stable)](https://github.com/dg/dibi/releases)
[![License](https://img.shields.io/badge/license-New%20BSD-blue.svg)](https://github.com/dg/dibi/blob/master/license.md)
Introduction
------------
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.
The best way to install Dibi is to use a [Composer](https://getcomposer.org/download):
Installation
------------
php composer.phar require dibi/dibi
The recommended way to install Dibi is via Composer (alternatively you can [download package](https://github.com/dg/dibi/releases)):
Or you can download the latest package from https://dibiphp.com. In this
package is also `Dibi.minified`, shrinked single-file version of whole Dibi,
useful when you don't want to modify the library, but just use it.
```bash
composer require dibi/dibi
```
The Dibi 3.x requires PHP version 5.4.4 and supports PHP up to 7.2.
Dibi requires PHP 5.4.4 or later. It has been tested with PHP 7 too.
Usage
-----
Examples
--------
Refer to the `examples` directory for examples. Dibi documentation is
available on the [homepage](https://dibiphp.com).
@@ -36,34 +30,34 @@ available on the [homepage](https://dibiphp.com).
Connect to database:
```php
$dibi = new Dibi\Connection([
'driver' => 'mysqli',
// connect to database (static way)
dibi::connect([
'driver' => 'mysql',
'host' => 'localhost',
'username' => 'root',
'password' => '***',
]);
// or static way; in all other examples use dibi:: instead of $dibi->
dibi::connect($options);
// or object way; in all other examples use $connection-> instead of dibi::
$connection = new DibiConnection($options);
```
SELECT, INSERT, UPDATE
```php
$dibi->query('SELECT * FROM users WHERE id = ?', $id);
dibi::query('SELECT * FROM users WHERE id = ?', $id);
$arr = [
'name' => 'John',
'is_admin' => true,
];
$dibi->query('INSERT INTO users', $arr);
dibi::query('INSERT INTO users', $arr);
// INSERT INTO users (`name`, `is_admin`) VALUES ('John', 1)
$dibi->query('UPDATE users SET', $arr, 'WHERE `id`=?', $x);
dibi::query('UPDATE users SET', $arr, 'WHERE `id`=?', $x);
// UPDATE users SET `name`='John', `is_admin`=1 WHERE `id` = 123
$dibi->query('UPDATE users SET', [
dibi::query('UPDATE users SET', [
'title' => array('SHA1(?)', 'tajneheslo'),
]);
// UPDATE users SET 'title' = SHA1('tajneheslo')
@@ -72,7 +66,7 @@ $dibi->query('UPDATE users SET', [
Getting results
```php
$result = $dibi->query('SELECT * FROM users');
$result = dibi::query('SELECT * FROM users');
$value = $result->fetchSingle(); // single value
$all = $result->fetchAll(); // all rows
@@ -88,7 +82,7 @@ foreach ($result as $n => $row) {
Modifiers for arrays:
```php
$dibi->query('SELECT * FROM users WHERE %and', [
dibi::query('SELECT * FROM users WHERE %and', [
array('number > ?', 10),
array('number < ?', 100),
]);
@@ -111,7 +105,7 @@ $dibi->query('SELECT * FROM users WHERE %and', [
Modifiers for LIKE
```php
$dibi->query("SELECT * FROM table WHERE name LIKE %like~", $query);
dibi::query("SELECT * FROM table WHERE name LIKE %like~", $query);
```
<table>
@@ -123,7 +117,7 @@ $dibi->query("SELECT * FROM table WHERE name LIKE %like~", $query);
DateTime:
```php
$dibi->query('UPDATE users SET', [
dibi::query('UPDATE users SET', [
'time' => new DateTime,
]);
// UPDATE users SET ('2008-01-01 01:08:10')

View File

@@ -1,7 +1,7 @@
<?php
/**
* This file is part of the Dibi, smart database abstraction layer (https://dibiphp.com)
* This file is part of the "dibi" - smart database abstraction layer.
* Copyright (c) 2005 David Grudl (https://davidgrudl.com)
*/

View File

@@ -1,7 +1,7 @@
<?php
/**
* This file is part of the Dibi, smart database abstraction layer (https://dibiphp.com)
* This file is part of the "dibi" - smart database abstraction layer.
* Copyright (c) 2005 David Grudl (https://davidgrudl.com)
*/
@@ -23,7 +23,7 @@ class Panel implements Tracy\IBarPanel
/** @var int maximum SQL length */
public static $maxLength = 1000;
/** @var bool|string explain queries? */
/** @var bool explain queries? */
public $explain;
/** @var int */
@@ -110,10 +110,10 @@ class Panel implements Tracy\IBarPanel
$connection = $event->connection;
$explain = null; // EXPLAIN is called here to work SELECT FOUND_ROWS()
if ($this->explain && $event->type === Event::SELECT) {
$backup = [$connection->onEvent, \dibi::$numOfQueries, \dibi::$totalTime];
$connection->onEvent = null;
$cmd = is_string($this->explain) ? $this->explain : ($connection->getConfig('driver') === 'oracle' ? 'EXPLAIN PLAN FOR' : 'EXPLAIN');
try {
$backup = [$connection->onEvent, \dibi::$numOfQueries, \dibi::$totalTime];
$connection->onEvent = null;
$cmd = is_string($this->explain) ? $this->explain : ($connection->getConfig('driver') === 'oracle' ? 'EXPLAIN PLAN FOR' : 'EXPLAIN');
$explain = @Helpers::dump($connection->nativeQuery("$cmd $event->sql"), true);
} catch (Dibi\Exception $e) {
}

View File

@@ -1,7 +1,7 @@
<?php
/**
* This file is part of the Dibi, smart database abstraction layer (https://dibiphp.com)
* This file is part of the "dibi" - smart database abstraction layer.
* Copyright (c) 2005 David Grudl (https://davidgrudl.com)
*/
@@ -11,7 +11,7 @@ use Traversable;
/**
* Dibi connection.
* dibi connection.
*
* @property-read int $affectedRows
* @property-read int $insertId
@@ -29,7 +29,7 @@ class Connection
/** @var Driver */
private $driver;
/** @var Translator|null */
/** @var Translator */
private $translator;
/** @var bool Is connected? */
@@ -131,9 +131,8 @@ class Connection
*/
public function __destruct()
{
if ($this->connected && $this->driver->getResource()) {
$this->disconnect();
}
// disconnects and rolls back transaction - do not rely on auto-disconnect and rollback!
$this->connected && $this->driver->getResource() && $this->disconnect();
}
@@ -147,14 +146,10 @@ class Connection
try {
$this->driver->connect($this->config);
$this->connected = true;
if ($event) {
$this->onEvent($event->done());
}
$event && $this->onEvent($event->done());
} catch (Exception $e) {
if ($event) {
$this->onEvent($event->done($e));
}
$event && $this->onEvent($event->done($e));
throw $e;
}
}
@@ -216,9 +211,7 @@ class Connection
*/
final public function getDriver()
{
if (!$this->connected) {
$this->connect();
}
$this->connected || $this->connect();
return $this->driver;
}
@@ -292,9 +285,7 @@ class Connection
*/
protected function translateArgs($args)
{
if (!$this->connected) {
$this->connect();
}
$this->connected || $this->connect();
if (!$this->translator) {
$this->translator = new Translator($this);
}
@@ -311,9 +302,7 @@ class Connection
*/
final public function nativeQuery($sql)
{
if (!$this->connected) {
$this->connect();
}
$this->connected || $this->connect();
\dibi::$sql = $sql;
$event = $this->onEvent ? new Event($this, Event::QUERY, $sql) : null;
@@ -321,9 +310,7 @@ class Connection
$res = $this->driver->query($sql);
} catch (Exception $e) {
if ($event) {
$this->onEvent($event->done($e));
}
$event && $this->onEvent($event->done($e));
throw $e;
}
@@ -333,9 +320,7 @@ class Connection
$res = $this->driver->getAffectedRows();
}
if ($event) {
$this->onEvent($event->done($res));
}
$event && $this->onEvent($event->done($res));
return $res;
}
@@ -347,9 +332,7 @@ class Connection
*/
public function getAffectedRows()
{
if (!$this->connected) {
$this->connect();
}
$this->connected || $this->connect();
$rows = $this->driver->getAffectedRows();
if (!is_int($rows) || $rows < 0) {
throw new Exception('Cannot retrieve number of affected rows.');
@@ -376,9 +359,7 @@ class Connection
*/
public function getInsertId($sequence = null)
{
if (!$this->connected) {
$this->connect();
}
$this->connected || $this->connect();
$id = $this->driver->getInsertId($sequence);
if ($id < 1) {
throw new Exception('Cannot retrieve last generated ID.');
@@ -404,20 +385,14 @@ class Connection
*/
public function begin($savepoint = null)
{
if (!$this->connected) {
$this->connect();
}
$this->connected || $this->connect();
$event = $this->onEvent ? new Event($this, Event::BEGIN, $savepoint) : null;
try {
$this->driver->begin($savepoint);
if ($event) {
$this->onEvent($event->done());
}
$event && $this->onEvent($event->done());
} catch (Exception $e) {
if ($event) {
$this->onEvent($event->done($e));
}
$event && $this->onEvent($event->done($e));
throw $e;
}
}
@@ -430,20 +405,14 @@ class Connection
*/
public function commit($savepoint = null)
{
if (!$this->connected) {
$this->connect();
}
$this->connected || $this->connect();
$event = $this->onEvent ? new Event($this, Event::COMMIT, $savepoint) : null;
try {
$this->driver->commit($savepoint);
if ($event) {
$this->onEvent($event->done());
}
$event && $this->onEvent($event->done());
} catch (Exception $e) {
if ($event) {
$this->onEvent($event->done($e));
}
$event && $this->onEvent($event->done($e));
throw $e;
}
}
@@ -456,20 +425,14 @@ class Connection
*/
public function rollback($savepoint = null)
{
if (!$this->connected) {
$this->connect();
}
$this->connected || $this->connect();
$event = $this->onEvent ? new Event($this, Event::ROLLBACK, $savepoint) : null;
try {
$this->driver->rollback($savepoint);
if ($event) {
$this->onEvent($event->done());
}
$event && $this->onEvent($event->done());
} catch (Exception $e) {
if ($event) {
$this->onEvent($event->done($e));
}
$event && $this->onEvent($event->done($e));
throw $e;
}
}
@@ -596,7 +559,7 @@ class Connection
/**
* Executes SQL query and fetch results - shortcut for query() & fetchAll().
* @param array|mixed one or more arguments
* @return Row[]|array[]
* @return Row[]
* @throws Exception
*/
public function fetchAll($args)
@@ -662,9 +625,7 @@ class Connection
*/
public function getDatabaseInfo()
{
if (!$this->connected) {
$this->connect();
}
$this->connected || $this->connect();
return new Reflection\Database($this->driver->getReflector(), isset($this->config['database']) ? $this->config['database'] : null);
}

View File

@@ -1,7 +1,7 @@
<?php
/**
* This file is part of the Dibi, smart database abstraction layer (https://dibiphp.com)
* This file is part of the "dibi" - smart database abstraction layer.
* Copyright (c) 2005 David Grudl (https://davidgrudl.com)
*/
@@ -9,7 +9,7 @@ namespace Dibi;
/**
* Default implementation of IDataSource.
* Default implementation of IDataSource for dibi.
*/
class DataSource implements IDataSource
{
@@ -21,13 +21,13 @@ class DataSource implements IDataSource
/** @var string */
private $sql;
/** @var Result|null */
/** @var Result */
private $result;
/** @var int|null */
/** @var int */
private $count;
/** @var int|null */
/** @var int */
private $totalCount;
/** @var array */
@@ -131,6 +131,7 @@ class DataSource implements IDataSource
/**
* Returns the dibi connection.
* @return Connection
*/
final public function getConnection()

View File

@@ -1,7 +1,7 @@
<?php
/**
* This file is part of the Dibi, smart database abstraction layer (https://dibiphp.com)
* This file is part of the "dibi" - smart database abstraction layer.
* Copyright (c) 2005 David Grudl (https://davidgrudl.com)
*/
@@ -22,7 +22,7 @@ class DateTime extends \DateTime
{
if (is_numeric($time)) {
parent::__construct('@' . $time);
$this->setTimeZone($timezone ? $timezone : new \DateTimeZone(date_default_timezone_get()));
$this->setTimeZone($timezone ?: new \DateTimeZone(date_default_timezone_get()));
} elseif ($timezone === null) {
parent::__construct($time);
} else {

View File

@@ -1,7 +1,7 @@
<?php
/**
* This file is part of the Dibi, smart database abstraction layer (https://dibiphp.com)
* This file is part of the "dibi" - smart database abstraction layer.
* Copyright (c) 2005 David Grudl (https://davidgrudl.com)
*/
@@ -11,7 +11,7 @@ use Dibi;
/**
* The driver for Firebird/InterBase database.
* The dibi driver for Firebird/InterBase database.
*
* Driver options:
* - database => the path to database file (server:/path/database.fdb)
@@ -20,6 +20,7 @@ use Dibi;
* - charset => character encoding to set
* - buffers (int) => buffers is the number of database buffers to allocate for the server-side cache. If 0 or omitted, server chooses its own default.
* - resource (resource) => existing connection resource
* - lazy, profiler, result, substitutes, ... => see Dibi\Connection options
*/
class FirebirdDriver implements Dibi\Driver, Dibi\ResultDriver, Dibi\Reflector
{
@@ -312,7 +313,7 @@ class FirebirdDriver implements Dibi\Driver, Dibi\ResultDriver, Dibi\Reflector
if (!$value instanceof \DateTime && !$value instanceof \DateTimeInterface) {
$value = new Dibi\DateTime($value);
}
return "'" . substr($value->format('Y-m-d H:i:s.u'), 0, -2) . "'";
return $value->format("'Y-m-d H:i:s.u'");
}
@@ -374,9 +375,7 @@ class FirebirdDriver implements Dibi\Driver, Dibi\ResultDriver, Dibi\Reflector
*/
public function __destruct()
{
if ($this->autoFree && $this->getResultResource()) {
$this->free();
}
$this->autoFree && $this->getResultResource() && $this->free();
}

View File

@@ -1,7 +1,7 @@
<?php
/**
* This file is part of the Dibi, smart database abstraction layer (https://dibiphp.com)
* This file is part of the "dibi" - smart database abstraction layer.
* Copyright (c) 2005 David Grudl (https://davidgrudl.com)
*/
@@ -11,7 +11,7 @@ use Dibi;
/**
* The driver for MS SQL database.
* The dibi driver for MS SQL database.
*
* Driver options:
* - host => the MS SQL server host name. It can also include a port number (hostname:port)
@@ -329,9 +329,7 @@ class MsSqlDriver implements Dibi\Driver, Dibi\ResultDriver
*/
public function __destruct()
{
if ($this->autoFree && $this->getResultResource()) {
$this->free();
}
$this->autoFree && $this->getResultResource() && $this->free();
}

View File

@@ -1,7 +1,7 @@
<?php
/**
* This file is part of the Dibi, smart database abstraction layer (https://dibiphp.com)
* This file is part of the "dibi" - smart database abstraction layer.
* Copyright (c) 2005 David Grudl (https://davidgrudl.com)
*/
@@ -11,7 +11,7 @@ use Dibi;
/**
* The reflector for MS SQL databases.
* The dibi reflector for MS SQL databases.
* @internal
*/
class MsSqlReflector implements Dibi\Reflector
@@ -96,7 +96,10 @@ class MsSqlReflector implements Dibi\Reflector
");
$columns = [];
while ($row = $res->fetch(true)) {
static $size_cols = [
$size = false;
$type = strtoupper($row['DATA_TYPE']);
$size_cols = [
'DATETIME' => 'DATETIME_PRECISION',
'DECIMAL' => 'NUMERIC_PRECISION',
'CHAR' => 'CHARACTER_MAXIMUM_LENGTH',
@@ -105,13 +108,17 @@ class MsSqlReflector implements Dibi\Reflector
'VARCHAR' => 'CHARACTER_OCTET_LENGTH',
];
$type = strtoupper($row['DATA_TYPE']);
if (isset($size_cols[$type])) {
if ($size_cols[$type]) {
$size = $row[$size_cols[$type]];
}
}
$columns[] = [
'name' => $row['COLUMN_NAME'],
'table' => $table,
'nativetype' => $type,
'size' => isset($size_cols[$type], $row[$size_cols[$type]]) ? $row[$size_cols[$type]] : null,
'size' => $size,
'unsigned' => null,
'nullable' => $row['IS_NULLABLE'] === 'YES',
'default' => $row['COLUMN_DEFAULT'],

View File

@@ -1,7 +1,7 @@
<?php
/**
* This file is part of the Dibi, smart database abstraction layer (https://dibiphp.com)
* This file is part of the "dibi" - smart database abstraction layer.
* Copyright (c) 2005 David Grudl (https://davidgrudl.com)
*/
@@ -11,7 +11,7 @@ use Dibi;
/**
* The driver for MySQL database.
* The dibi driver for MySQL database.
*
* Driver options:
* - host => the MySQL server host name

View File

@@ -1,7 +1,7 @@
<?php
/**
* This file is part of the Dibi, smart database abstraction layer (https://dibiphp.com)
* This file is part of the "dibi" - smart database abstraction layer.
* Copyright (c) 2005 David Grudl (https://davidgrudl.com)
*/
@@ -11,7 +11,7 @@ use Dibi;
/**
* The reflector for MySQL databases.
* The dibi reflector for MySQL databases.
* @internal
*/
class MySqlReflector implements Dibi\Reflector

View File

@@ -1,7 +1,7 @@
<?php
/**
* This file is part of the Dibi, smart database abstraction layer (https://dibiphp.com)
* This file is part of the "dibi" - smart database abstraction layer.
* Copyright (c) 2005 David Grudl (https://davidgrudl.com)
*/
@@ -11,7 +11,7 @@ use Dibi;
/**
* The driver for MySQL database.
* The dibi driver for MySQL database via improved extension.
*
* Driver options:
* - host => the MySQL server host name
@@ -27,6 +27,7 @@ use Dibi;
* - unbuffered (bool) => sends query without fetching and buffering the result rows automatically?
* - sqlmode => see http://dev.mysql.com/doc/refman/5.0/en/server-sql-mode.html
* - resource (mysqli) => existing connection resource
* - lazy, profiler, result, substitutes, ... => see Dibi\Connection options
*/
class MySqliDriver implements Dibi\Driver, Dibi\ResultDriver
{
@@ -255,7 +256,7 @@ class MySqliDriver implements Dibi\Driver, Dibi\ResultDriver
/**
* Returns the connection resource.
* @return \mysqli|null
* @return \mysqli
*/
public function getResource()
{
@@ -416,9 +417,7 @@ class MySqliDriver implements Dibi\Driver, Dibi\ResultDriver
*/
public function __destruct()
{
if ($this->autoFree && $this->getResultResource()) {
@$this->free();
}
$this->autoFree && $this->getResultResource() && @$this->free();
}

View File

@@ -1,7 +1,7 @@
<?php
/**
* This file is part of the Dibi, smart database abstraction layer (https://dibiphp.com)
* This file is part of the "dibi" - smart database abstraction layer.
* Copyright (c) 2005 David Grudl (https://davidgrudl.com)
*/
@@ -11,7 +11,7 @@ use Dibi;
/**
* The driver interacting with databases via ODBC connections.
* The dibi driver interacting with databases via ODBC connections.
*
* Driver options:
* - dsn => driver specific DSN
@@ -19,6 +19,7 @@ use Dibi;
* - password (or pass)
* - persistent (bool) => try to find a persistent link?
* - resource (resource) => existing connection resource
* - lazy, profiler, result, substitutes, ... => see Dibi\Connection options
*/
class OdbcDriver implements Dibi\Driver, Dibi\ResultDriver, Dibi\Reflector
{
@@ -352,9 +353,7 @@ class OdbcDriver implements Dibi\Driver, Dibi\ResultDriver, Dibi\Reflector
*/
public function __destruct()
{
if ($this->autoFree && $this->getResultResource()) {
$this->free();
}
$this->autoFree && $this->getResultResource() && $this->free();
}

View File

@@ -1,7 +1,7 @@
<?php
/**
* This file is part of the Dibi, smart database abstraction layer (https://dibiphp.com)
* This file is part of the "dibi" - smart database abstraction layer.
* Copyright (c) 2005 David Grudl (https://davidgrudl.com)
*/
@@ -11,7 +11,7 @@ use Dibi;
/**
* The driver for Oracle database.
* The dibi driver for Oracle database.
*
* Driver options:
* - database => the name of the local Oracle instance or the name of the entry in tnsnames.ora
@@ -22,6 +22,7 @@ use Dibi;
* - nativeDate => use native date format (defaults to false)
* - resource (resource) => existing connection resource
* - persistent => Creates persistent connections with oci_pconnect instead of oci_new_connect
* - lazy, profiler, result, substitutes, ... => see Dibi\Connection options
*/
class OracleDriver implements Dibi\Driver, Dibi\ResultDriver, Dibi\Reflector
{
@@ -391,9 +392,7 @@ class OracleDriver implements Dibi\Driver, Dibi\ResultDriver, Dibi\Reflector
*/
public function __destruct()
{
if ($this->autoFree && $this->getResultResource()) {
$this->free();
}
$this->autoFree && $this->getResultResource() && $this->free();
}

View File

@@ -1,7 +1,7 @@
<?php
/**
* This file is part of the Dibi, smart database abstraction layer (https://dibiphp.com)
* This file is part of the "dibi" - smart database abstraction layer.
* Copyright (c) 2005 David Grudl (https://davidgrudl.com)
*/
@@ -12,7 +12,7 @@ use PDO;
/**
* The driver for PDO.
* The dibi driver for PDO.
*
* Driver options:
* - dsn => driver specific DSN
@@ -21,12 +21,13 @@ use PDO;
* - options (array) => driver specific options {@see PDO::__construct}
* - resource (PDO) => existing connection
* - version
* - lazy, profiler, result, substitutes, ... => see Dibi\Connection options
*/
class PdoDriver implements Dibi\Driver, Dibi\ResultDriver
{
use Dibi\Strict;
/** @var PDO|null Connection resource */
/** @var PDO Connection resource */
private $connection;
/** @var \PDOStatement|null Resultset resource */
@@ -103,14 +104,23 @@ class PdoDriver implements Dibi\Driver, Dibi\ResultDriver
*/
public function query($sql)
{
$res = $this->connection->query($sql);
if ($res) {
$this->affectedRows = $res->rowCount();
return $res->columnCount() ? $this->createResultDriver($res) : null;
}
// must detect if SQL returns result set or num of affected rows
$cmd = strtoupper(substr(ltrim($sql), 0, 6));
static $list = ['UPDATE' => 1, 'DELETE' => 1, 'INSERT' => 1, 'REPLAC' => 1];
$this->affectedRows = false;
if (isset($list[$cmd])) {
$this->affectedRows = $this->connection->exec($sql);
if ($this->affectedRows !== false) {
return null;
}
} else {
$res = $this->connection->query($sql);
if ($res) {
return $this->createResultDriver($res);
}
}
list($sqlState, $code, $message) = $this->connection->errorInfo();
$message = "SQLSTATE[$sqlState]: $message";
switch ($this->driverName) {
@@ -199,7 +209,7 @@ class PdoDriver implements Dibi\Driver, Dibi\ResultDriver
/**
* Returns the connection resource.
* @return PDO|null
* @return PDO
*/
public function getResource()
{

View File

@@ -1,7 +1,7 @@
<?php
/**
* This file is part of the Dibi, smart database abstraction layer (https://dibiphp.com)
* This file is part of the "dibi" - smart database abstraction layer.
* Copyright (c) 2005 David Grudl (https://davidgrudl.com)
*/
@@ -11,7 +11,7 @@ use Dibi;
/**
* The driver for PostgreSQL database.
* The dibi driver for PostgreSQL database.
*
* Driver options:
* - host, hostaddr, port, dbname, user, password, connect_timeout, options, sslmode, service => see PostgreSQL API
@@ -20,6 +20,7 @@ use Dibi;
* - charset => character encoding to set (default is utf8)
* - persistent (bool) => try to find a persistent link?
* - resource (resource) => existing connection resource
* - lazy, profiler, result, substitutes, ... => see Dibi\Connection options
*/
class PostgreDriver implements Dibi\Driver, Dibi\ResultDriver, Dibi\Reflector
{
@@ -428,9 +429,7 @@ class PostgreDriver implements Dibi\Driver, Dibi\ResultDriver, Dibi\Reflector
*/
public function __destruct()
{
if ($this->autoFree && $this->getResultResource()) {
$this->free();
}
$this->autoFree && $this->getResultResource() && $this->free();
}
@@ -548,7 +547,7 @@ class PostgreDriver implements Dibi\Driver, Dibi\ResultDriver, Dibi\Reflector
$res = $this->query($query);
$tables = pg_fetch_all($res->resultSet);
return $tables ? $tables : [];
return $tables ?: [];
}

View File

@@ -1,7 +1,7 @@
<?php
/**
* This file is part of the Dibi, smart database abstraction layer (https://dibiphp.com)
* This file is part of the "dibi" - smart database abstraction layer.
* Copyright (c) 2005 David Grudl (https://davidgrudl.com)
*/
@@ -12,7 +12,7 @@ use SQLite3;
/**
* The driver for SQLite3 database.
* The dibi driver for SQLite3 database.
*
* Driver options:
* - database (or file) => the filename of the SQLite3 database
@@ -21,6 +21,7 @@ use SQLite3;
* - dbcharset => database character encoding (will be converted to 'charset')
* - charset => character encoding to set (default is UTF-8)
* - resource (SQLite3) => existing connection resource
* - lazy, profiler, result, substitutes, ... => see Dibi\Connection options
*/
class Sqlite3Driver implements Dibi\Driver, Dibi\ResultDriver
{
@@ -35,10 +36,9 @@ class Sqlite3Driver implements Dibi\Driver, Dibi\ResultDriver
/** @var bool */
private $autoFree = true;
/** @var string Date format */
/** @var string Date and datetime format */
private $fmtDate;
/** @var string Datetime format */
private $fmtDateTime;
/** @var string character encoding */
@@ -214,7 +214,7 @@ class Sqlite3Driver implements Dibi\Driver, Dibi\ResultDriver
/**
* Returns the connection resource.
* @return SQLite3|null
* @return SQLite3
*/
public function getResource()
{
@@ -375,9 +375,7 @@ class Sqlite3Driver implements Dibi\Driver, Dibi\ResultDriver
*/
public function __destruct()
{
if ($this->autoFree && $this->getResultResource()) {
@$this->free();
}
$this->autoFree && $this->resultSet && @$this->free();
}
@@ -401,12 +399,15 @@ class Sqlite3Driver implements Dibi\Driver, Dibi\ResultDriver
{
$row = $this->resultSet->fetchArray($assoc ? SQLITE3_ASSOC : SQLITE3_NUM);
$charset = $this->charset === null ? null : $this->charset . '//TRANSLIT';
if ($row && $charset) {
if ($row && ($assoc || $charset)) {
$tmp = [];
foreach ($row as $k => $v) {
if (is_string($v)) {
$row[$k] = iconv($this->dbcharset, $charset, $v);
if ($charset !== null && is_string($v)) {
$v = iconv($this->dbcharset, $charset, $v);
}
$tmp[str_replace(['[', ']'], '', $k)] = $v;
}
return $tmp;
}
return $row;
}

View File

@@ -1,7 +1,7 @@
<?php
/**
* This file is part of the Dibi, smart database abstraction layer (https://dibiphp.com)
* This file is part of the "dibi" - smart database abstraction layer.
* Copyright (c) 2005 David Grudl (https://davidgrudl.com)
*/
@@ -11,7 +11,7 @@ use Dibi;
/**
* The reflector for SQLite database.
* The dibi reflector for SQLite database.
* @internal
*/
class SqliteReflector implements Dibi\Reflector

View File

@@ -1,7 +1,7 @@
<?php
/**
* This file is part of the Dibi, smart database abstraction layer (https://dibiphp.com)
* This file is part of the "dibi" - smart database abstraction layer.
* Copyright (c) 2005 David Grudl (https://davidgrudl.com)
*/
@@ -12,7 +12,7 @@ use Dibi\Helpers;
/**
* The driver for Microsoft SQL Server and SQL Azure databases.
* The dibi driver for Microsoft SQL Server and SQL Azure databases.
*
* Driver options:
* - host => the MS SQL server host name. It can also include a port number (hostname:port)
@@ -22,6 +22,7 @@ use Dibi\Helpers;
* - options (array) => connection options {@link https://msdn.microsoft.com/en-us/library/cc296161(SQL.90).aspx}
* - charset => character encoding to set (default is UTF-8)
* - resource (resource) => existing connection resource
* - lazy, profiler, result, substitutes, ... => see Dibi\Connection options
*/
class SqlsrvDriver implements Dibi\Driver, Dibi\ResultDriver
{
@@ -360,9 +361,7 @@ class SqlsrvDriver implements Dibi\Driver, Dibi\ResultDriver
*/
public function __destruct()
{
if ($this->autoFree && $this->getResultResource()) {
$this->free();
}
$this->autoFree && $this->getResultResource() && $this->free();
}

View File

@@ -1,7 +1,7 @@
<?php
/**
* This file is part of the Dibi, smart database abstraction layer (https://dibiphp.com)
* This file is part of the "dibi" - smart database abstraction layer.
* Copyright (c) 2005 David Grudl (https://davidgrudl.com)
*/
@@ -11,7 +11,7 @@ use Dibi;
/**
* The reflector for Microsoft SQL Server and SQL Azure databases.
* The dibi reflector for Microsoft SQL Server and SQL Azure databases.
* @internal
*/
class SqlsrvReflector implements Dibi\Reflector

View File

@@ -1,7 +1,7 @@
<?php
/**
* This file is part of the Dibi, smart database abstraction layer (https://dibiphp.com)
* This file is part of the "dibi" - smart database abstraction layer.
* Copyright (c) 2005 David Grudl (https://davidgrudl.com)
*/
@@ -43,10 +43,10 @@ class Event
/** @var float */
public $time;
/** @var int|null */
/** @var int */
public $count;
/** @var array|null */
/** @var array */
public $source;
@@ -74,15 +74,12 @@ class Event
}
}
\dibi::$elapsedTime = null;
\dibi::$elapsedTime = false;
\dibi::$numOfQueries++;
\dibi::$sql = $sql;
}
/**
* @param Result|DriverException|null
*/
public function done($result = null)
{
$this->result = $result;

View File

@@ -1,7 +1,7 @@
<?php
/**
* This file is part of the Dibi, smart database abstraction layer (https://dibiphp.com)
* This file is part of the "dibi" - smart database abstraction layer.
* Copyright (c) 2005 David Grudl (https://davidgrudl.com)
*/

View File

@@ -1,7 +1,7 @@
<?php
/**
* This file is part of the Dibi, smart database abstraction layer (https://dibiphp.com)
* This file is part of the "dibi" - smart database abstraction layer.
* Copyright (c) 2005 David Grudl (https://davidgrudl.com)
*/
@@ -9,11 +9,11 @@ namespace Dibi;
/**
* SQL builder via fluent interfaces.
* dibi SQL builder via fluent interfaces. EXPERIMENTAL!
*
* @method Fluent select(...$field)
* @method Fluent distinct()
* @method Fluent from($table, ...$args)
* @method Fluent from($table)
* @method Fluent where(...$cond)
* @method Fluent groupBy(...$field)
* @method Fluent having(...$cond)
@@ -86,7 +86,7 @@ class Fluent implements IDataSource
/** @var array */
private $setups = [];
/** @var string|null */
/** @var string */
private $command;
/** @var array */
@@ -95,7 +95,7 @@ class Fluent implements IDataSource
/** @var array */
private $flags = [];
/** @var array|null */
/** @var array */
private $cursor;
/** @var HashMap normalized clauses */
@@ -272,6 +272,7 @@ class Fluent implements IDataSource
/**
* Returns the dibi connection.
* @return Connection
*/
final public function getConnection()

View File

@@ -1,7 +1,7 @@
<?php
/**
* This file is part of the Dibi, smart database abstraction layer (https://dibiphp.com)
* This file is part of the "dibi" - smart database abstraction layer.
* Copyright (c) 2005 David Grudl (https://davidgrudl.com)
*/
@@ -14,7 +14,6 @@ namespace Dibi;
*/
abstract class HashMapBase
{
/** @var callable */
private $callback;
@@ -39,13 +38,14 @@ abstract class HashMapBase
/**
* Lazy cached storage.
*
* @internal
*/
final class HashMap extends HashMapBase
{
public function __set($nm, $val)
{
if ($nm === '') {
if ($nm == '') {
$nm = "\xFF";
}
$this->$nm = $val;
@@ -54,7 +54,7 @@ final class HashMap extends HashMapBase
public function __get($nm)
{
if ($nm === '') {
if ($nm == '') {
$nm = "\xFF";
return isset($this->$nm) ? $this->$nm : $this->$nm = call_user_func($this->getCallback(), '');
} else {

View File

@@ -1,7 +1,7 @@
<?php
/**
* This file is part of the Dibi, smart database abstraction layer (https://dibiphp.com)
* This file is part of the "dibi" - smart database abstraction layer.
* Copyright (c) 2005 David Grudl (https://davidgrudl.com)
*/
@@ -12,7 +12,7 @@ class Helpers
{
use Strict;
/** @var HashMap */
/** @var array */
private static $types;

View File

@@ -1,7 +1,7 @@
<?php
/**
* This file is part of the Dibi, smart database abstraction layer (https://dibiphp.com)
* This file is part of the "dibi" - smart database abstraction layer.
* Copyright (c) 2005 David Grudl (https://davidgrudl.com)
*/

View File

@@ -1,7 +1,7 @@
<?php
/**
* This file is part of the Dibi, smart database abstraction layer (https://dibiphp.com)
* This file is part of the "dibi" - smart database abstraction layer.
* Copyright (c) 2005 David Grudl (https://davidgrudl.com)
*/
@@ -11,7 +11,7 @@ use Dibi;
/**
* Dibi file logger.
* dibi file logger.
*/
class FileLogger
{

View File

@@ -1,7 +1,7 @@
<?php
/**
* This file is part of the Dibi, smart database abstraction layer (https://dibiphp.com)
* This file is part of the "dibi" - smart database abstraction layer.
* Copyright (c) 2005 David Grudl (https://davidgrudl.com)
*/
@@ -11,7 +11,7 @@ use Dibi;
/**
* FirePHP logger.
* dibi FirePHP logger.
*/
class FirePhpLogger
{
@@ -29,7 +29,7 @@ class FirePhpLogger
/** @var int */
public $filter;
/** @var float Elapsed time for all queries */
/** @var int Elapsed time for all queries */
public $totalTime = 0;
/** @var int Number of all queries */

View File

@@ -1,7 +1,7 @@
<?php
/**
* This file is part of the Dibi, smart database abstraction layer (https://dibiphp.com)
* This file is part of the "dibi" - smart database abstraction layer.
* Copyright (c) 2005 David Grudl (https://davidgrudl.com)
*/

View File

@@ -1,7 +1,7 @@
<?php
/**
* This file is part of the Dibi, smart database abstraction layer (https://dibiphp.com)
* This file is part of the "dibi" - smart database abstraction layer.
* Copyright (c) 2005 David Grudl (https://davidgrudl.com)
*/
@@ -24,10 +24,10 @@ class Database
/** @var Dibi\Reflector */
private $reflector;
/** @var string|null */
/** @var string */
private $name;
/** @var Table[]|null */
/** @var array */
private $tables;
@@ -39,7 +39,7 @@ class Database
/**
* @return string|null
* @return string
*/
public function getName()
{

View File

@@ -1,7 +1,7 @@
<?php
/**
* This file is part of the Dibi, smart database abstraction layer (https://dibiphp.com)
* This file is part of the "dibi" - smart database abstraction layer.
* Copyright (c) 2005 David Grudl (https://davidgrudl.com)
*/

View File

@@ -1,7 +1,7 @@
<?php
/**
* This file is part of the Dibi, smart database abstraction layer (https://dibiphp.com)
* This file is part of the "dibi" - smart database abstraction layer.
* Copyright (c) 2005 David Grudl (https://davidgrudl.com)
*/

View File

@@ -1,7 +1,7 @@
<?php
/**
* This file is part of the Dibi, smart database abstraction layer (https://dibiphp.com)
* This file is part of the "dibi" - smart database abstraction layer.
* Copyright (c) 2005 David Grudl (https://davidgrudl.com)
*/
@@ -23,10 +23,10 @@ class Result
/** @var Dibi\ResultDriver */
private $driver;
/** @var Column[]|null */
/** @var array */
private $columns;
/** @var string[]|null */
/** @var array */
private $names;

View File

@@ -1,7 +1,7 @@
<?php
/**
* This file is part of the Dibi, smart database abstraction layer (https://dibiphp.com)
* This file is part of the "dibi" - smart database abstraction layer.
* Copyright (c) 2005 David Grudl (https://davidgrudl.com)
*/
@@ -34,16 +34,16 @@ class Table
/** @var bool */
private $view;
/** @var Column[]|null */
/** @var array */
private $columns;
/** @var ForeignKey[]|null */
/** @var array */
private $foreignKeys;
/** @var Index[]|null */
/** @var array */
private $indexes;
/** @var Index|null */
/** @var Index */
private $primaryKey;

View File

@@ -1,7 +1,7 @@
<?php
/**
* This file is part of the Dibi, smart database abstraction layer (https://dibiphp.com)
* This file is part of the "dibi" - smart database abstraction layer.
* Copyright (c) 2005 David Grudl (https://davidgrudl.com)
*/
@@ -9,7 +9,20 @@ namespace Dibi;
/**
* Query result.
* dibi result set.
*
* <code>
* $result = dibi::query('SELECT * FROM [table]');
*
* $row = $result->fetch();
* $value = $result->fetchSingle();
* $table = $result->fetchAll();
* $pairs = $result->fetchPairs();
* $assoc = $result->fetchAssoc('col1');
* $assoc = $result->fetchAssoc('col1[]col2->col3');
*
* unset($result);
* </code>
*
* @property-read int $rowCount
*/
@@ -29,10 +42,10 @@ class Result implements IDataSource
/** @var bool Already fetched? Used for allowance for first seek(0) */
private $fetched = false;
/** @var string|null returned object class */
/** @var string returned object class */
private $rowClass = 'Dibi\Row';
/** @var callable|null returned object factory */
/** @var callable returned object factory*/
private $rowFactory;
/** @var array format */
@@ -134,16 +147,6 @@ class Result implements IDataSource
}
/**
* Returns the number of columns in a result set.
* @return int
*/
final public function getColumnCount()
{
return count($this->types);
}
/********************* fetching rows ****************d*g**/
@@ -222,7 +225,7 @@ class Result implements IDataSource
* Fetches all records from table.
* @param int offset
* @param int limit
* @return Row[]|array[]
* @return Row[]
*/
final public function fetchAll($offset = null, $limit = null)
{

View File

@@ -1,7 +1,7 @@
<?php
/**
* This file is part of the Dibi, smart database abstraction layer (https://dibiphp.com)
* This file is part of the "dibi" - smart database abstraction layer.
* Copyright (c) 2005 David Grudl (https://davidgrudl.com)
*/
@@ -10,6 +10,15 @@ namespace Dibi;
/**
* External result set iterator.
*
* This can be returned by Result::getIterator() method or using foreach
* <code>
* $result = dibi::query('SELECT * FROM table');
* foreach ($result as $row) {
* print_r($row);
* }
* unset($result);
* </code>
*/
class ResultIterator implements \Iterator, \Countable
{
@@ -22,7 +31,7 @@ class ResultIterator implements \Iterator, \Countable
private $row;
/** @var int */
private $pointer = 0;
private $pointer;
/**

View File

@@ -1,7 +1,7 @@
<?php
/**
* This file is part of the Dibi, smart database abstraction layer (https://dibiphp.com)
* This file is part of the "dibi" - smart database abstraction layer.
* Copyright (c) 2005 David Grudl (https://davidgrudl.com)
*/
@@ -31,7 +31,7 @@ class Row implements \ArrayAccess, \IteratorAggregate, \Countable
* Converts value to DateTime object.
* @param string key
* @param string format
* @return DateTime|string|null
* @return \DateTime
*/
public function asDateTime($key, $format = null)
{

View File

@@ -1,7 +1,7 @@
<?php
/**
* This file is part of the Dibi, smart database abstraction layer (https://dibiphp.com)
* This file is part of the "dibi" - smart database abstraction layer.
* Copyright (c) 2005 David Grudl (https://davidgrudl.com)
*/

View File

@@ -1,7 +1,7 @@
<?php
/**
* This file is part of the Dibi, smart database abstraction layer (https://dibiphp.com)
* This file is part of the "dibi" - smart database abstraction layer.
* Copyright (c) 2005 David Grudl (https://davidgrudl.com)
*/
@@ -9,7 +9,7 @@ namespace Dibi;
/**
* SQL translator.
* dibi SQL translator.
*/
final class Translator
{
@@ -39,10 +39,10 @@ final class Translator
/** @var int */
private $ifLevelStart = 0;
/** @var int|null */
/** @var int */
private $limit;
/** @var int|null */
/** @var int */
private $offset;
/** @var HashMap */
@@ -70,7 +70,6 @@ final class Translator
$args = array_values($args[0]);
}
$this->args = $args;
$this->errors = [];
$commandIns = null;
$lastArr = null;
@@ -316,11 +315,7 @@ final class Translator
if ($modifier) {
if ($value !== null && !is_scalar($value)) { // array is already processed
if ($value instanceof Literal && ($modifier === 'sql' || $modifier === 'SQL')) {
return (string) $value;
} elseif ($value instanceof Expression && $modifier === 'ex') {
return call_user_func_array([$this->connection, 'translate'], $value->getValues());
$modifier = 'SQL';
} elseif (($value instanceof \DateTime || $value instanceof \DateTimeInterface) && ($modifier === 'd' || $modifier === 't' || $modifier === 'dt')) {
// continue
} else {

View File

@@ -1,7 +1,7 @@
<?php
/**
* This file is part of the Dibi, smart database abstraction layer (https://dibiphp.com)
* This file is part of the "dibi" - smart database abstraction layer.
* Copyright (c) 2005 David Grudl (https://davidgrudl.com)
*/

View File

@@ -1,7 +1,7 @@
<?php
/**
* This file is part of the Dibi, smart database abstraction layer (https://dibiphp.com)
* This file is part of the "dibi" - smart database abstraction layer.
* Copyright (c) 2005 David Grudl (https://davidgrudl.com)
*/
@@ -9,7 +9,8 @@ use Dibi\Type;
/**
* Static container class for Dibi connections.
* This class is static container class for creating DB objects and
* store connections info.
*/
class dibi
{
@@ -21,8 +22,8 @@ class dibi
/** version */
const
VERSION = '3.2.2',
REVISION = 'released on 2018-05-02';
VERSION = '3.1.1',
REVISION = 'released on 2018-03-09';
/** sorting order */
const
@@ -48,13 +49,13 @@ class dibi
FIELD_DATETIME = Type::DATETIME,
FIELD_TIME = Type::TIME;
/** @var string|null Last SQL command @see dibi::query() */
/** @var string Last SQL command @see dibi::query() */
public static $sql;
/** @var float|null Elapsed time for last query */
/** @var int Elapsed time for last query */
public static $elapsedTime;
/** @var float Elapsed time for all queries */
/** @var int Elapsed time for all queries */
public static $totalTime;
/** @var int Number or queries */
@@ -63,7 +64,7 @@ class dibi
/** @var string Default dibi driver */
public static $defaultDriver = 'mysqli';
/** @var Dibi\Connection[] Connection registry storage for Dibi\Connection objects */
/** @var Dibi\Connection[] Connection registry storage for DibiConnection objects */
private static $registry = [];
/** @var Dibi\Connection Current connection */
@@ -84,7 +85,7 @@ class dibi
/**
* Creates a new Connection object and connects it to specified database.
* @param array|string connection parameters
* @param mixed connection parameters
* @param string connection name
* @return Dibi\Connection
* @throws Dibi\Exception
@@ -440,22 +441,10 @@ class dibi
* Prints out a syntax highlighted version of the SQL command or Result.
* @param string|Result
* @param bool return output instead of printing it?
* @return string|null
* @return string
*/
public static function dump($sql = null, $return = false)
{
return Dibi\Helpers::dump($sql, $return);
}
/**
* Strips microseconds part.
* @param \DateTime|\DateTimeInterface
* @return \DateTime|\DateTimeInterface
*/
public static function stripMicroseconds($dt)
{
$class = get_class($dt);
return new $class($dt->format('Y-m-d H:i:s'), $dt->getTimezone());
}
}

View File

@@ -1,7 +1,7 @@
<?php
/**
* This file is part of the Dibi, smart database abstraction layer (https://dibiphp.com)
* This file is part of the "dibi" - smart database abstraction layer.
* Copyright (c) 2005 David Grudl (https://davidgrudl.com)
*/
@@ -9,7 +9,7 @@ namespace Dibi;
/**
* Dibi common exception.
* dibi common exception.
*/
class Exception extends \Exception
{
@@ -18,8 +18,9 @@ class Exception extends \Exception
/**
* Construct a dibi exception.
* @param string Message describing the exception
* @param string|int
* @param mixed
* @param string SQL command
* @param \Exception
*/
@@ -103,7 +104,7 @@ class ProcedureException extends Exception
* @param int Some code
* @param string SQL command
*/
public function __construct($message = '', $code = 0, $severity = '', $sql = null)
public function __construct($message = null, $code = 0, $severity = null, $sql = null)
{
parent::__construct($message, (int) $code, $sql);
$this->severity = $severity;
@@ -116,7 +117,7 @@ class ProcedureException extends Exception
*/
public function getSeverity()
{
return $this->severity;
$this->severity;
}
}

View File

@@ -1,7 +1,7 @@
<?php
/**
* This file is part of the Dibi, smart database abstraction layer (https://dibiphp.com)
* This file is part of the "dibi" - smart database abstraction layer.
* Copyright (c) 2005 David Grudl (https://davidgrudl.com)
*/
@@ -19,10 +19,11 @@ interface IDataSource extends \Countable, \IteratorAggregate
/**
* Driver interface.
* dibi driver interface.
*/
interface Driver
{
/**
* Connects to a database.
* @param array
@@ -151,10 +152,11 @@ interface Driver
/**
* Result set driver interface.
* dibi result set driver interface.
*/
interface ResultDriver
{
/**
* Returns the number of rows in a result set.
* @return int
@@ -206,10 +208,11 @@ interface ResultDriver
/**
* Reflection driver.
* dibi driver reflection.
*/
interface Reflector
{
/**
* Returns list of tables.
* @return array of {name [, (bool) view ]}

View File

@@ -1,7 +1,7 @@
<?php
/**
* This file is part of the Dibi, smart database abstraction layer (https://dibiphp.com)
* This file is part of the "dibi" - smart database abstraction layer.
* Copyright (c) 2005 David Grudl (https://davidgrudl.com)
*/
@@ -17,27 +17,30 @@ spl_autoload_register(function ($class) {
'Dibi\Bridges\Nette\DibiExtension22' => 'Bridges/Nette/DibiExtension22.php',
'Dibi\Bridges\Tracy\Panel' => 'Bridges/Tracy/Panel.php',
'Dibi\Connection' => 'Connection.php',
'Dibi\ConstraintViolationException' => 'exceptions.php',
'Dibi\DataSource' => 'DataSource.php',
'Dibi\DateTime' => 'DateTime.php',
'Dibi\Driver' => 'interfaces.php',
'Dibi\DriverException' => 'exceptions.php',
'Dibi\Drivers\FirebirdDriver' => 'Drivers/FirebirdDriver.php',
'Dibi\Drivers\SqlsrvDriver' => 'Drivers/SqlsrvDriver.php',
'Dibi\Drivers\SqlsrvReflector' => 'Drivers/SqlsrvReflector.php',
'Dibi\Drivers\MsSqlDriver' => 'Drivers/MsSqlDriver.php',
'Dibi\Drivers\MsSqlReflector' => 'Drivers/MsSqlReflector.php',
'Dibi\Drivers\MySqlDriver' => 'Drivers/MySqlDriver.php',
'Dibi\Drivers\MySqliDriver' => 'Drivers/MySqliDriver.php',
'Dibi\Drivers\MySqlReflector' => 'Drivers/MySqlReflector.php',
'Dibi\Drivers\MySqliDriver' => 'Drivers/MySqliDriver.php',
'Dibi\Drivers\OdbcDriver' => 'Drivers/OdbcDriver.php',
'Dibi\Drivers\OracleDriver' => 'Drivers/OracleDriver.php',
'Dibi\Drivers\PdoDriver' => 'Drivers/PdoDriver.php',
'Dibi\Drivers\PostgreDriver' => 'Drivers/PostgreDriver.php',
'Dibi\Drivers\Sqlite3Driver' => 'Drivers/Sqlite3Driver.php',
'Dibi\Drivers\SqliteReflector' => 'Drivers/SqliteReflector.php',
'Dibi\Drivers\SqlsrvDriver' => 'Drivers/SqlsrvDriver.php',
'Dibi\Drivers\SqlsrvReflector' => 'Drivers/SqlsrvReflector.php',
'Dibi\Event' => 'Event.php',
'Dibi\Exception' => 'exceptions.php',
'Dibi\Expression' => 'Expression.php',
'Dibi\Fluent' => 'Fluent.php',
'Dibi\ForeignKeyConstraintViolationException' => 'exceptions.php',
'Dibi\HashMap' => 'HashMap.php',
'Dibi\HashMapBase' => 'HashMap.php',
'Dibi\Helpers' => 'Helpers.php',
@@ -46,6 +49,7 @@ spl_autoload_register(function ($class) {
'Dibi\Loggers\FileLogger' => 'Loggers/FileLogger.php',
'Dibi\Loggers\FirePhpLogger' => 'Loggers/FirePhpLogger.php',
'Dibi\NotImplementedException' => 'exceptions.php',
'Dibi\NotNullConstraintViolationException' => 'exceptions.php',
'Dibi\NotSupportedException' => 'exceptions.php',
'Dibi\PcreException' => 'exceptions.php',
'Dibi\ProcedureException' => 'exceptions.php',
@@ -63,6 +67,7 @@ spl_autoload_register(function ($class) {
'Dibi\Strict' => 'Strict.php',
'Dibi\Translator' => 'Translator.php',
'Dibi\Type' => 'Type.php',
'Dibi\UniqueConstraintViolationException' => 'exceptions.php',
], $old2new = [
'DibiColumnInfo' => 'Dibi\Reflection\Column',
'DibiConnection' => 'Dibi\Connection',
@@ -114,9 +119,26 @@ spl_autoload_register(function ($class) {
if (isset($map[$class])) {
require __DIR__ . '/Dibi/' . $map[$class];
} elseif (isset($old2new[$class])) {
$trace = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS);
$location = isset($trace[1]['file']) ? 'used in ' . $trace[1]['file'] . ':' . $trace[1]['line'] : '';
trigger_error("Class $class $location has been renamed to {$old2new[$class]}.", E_USER_DEPRECATED);
class_alias($old2new[$class], $class);
}
});
// preload for compatiblity
array_map('class_exists', [
'DibiConnection',
'DibiDateTime',
'DibiDriverException',
'DibiEvent',
'DibiException',
'DibiFluent',
'DibiLiteral',
'DibiNotImplementedException',
'DibiNotSupportedException',
'DibiPcreException',
'DibiProcedureException',
'DibiResult',
'DibiRow',
'IDataSource',
'IDibiDriver',
]);

View File

@@ -11,17 +11,12 @@ require __DIR__ . '/bootstrap.php';
$conn = new Dibi\Connection($config);
$conn->loadFile(__DIR__ . "/data/$config[system].sql");
$res = $conn->query('
$info = $conn->query('
SELECT products.product_id, orders.order_id, customers.name, products.product_id + 1 AS [xXx]
FROM ([products]
INNER JOIN [orders] ON [products.product_id] = [orders.product_id])
INNER JOIN [customers] ON [orders.customer_id] = [customers.customer_id]
');
$info = $res->getInfo();
Assert::same(4, $res->getColumnCount());
')->getInfo();
Assert::same(

View File

@@ -12,7 +12,7 @@ require __DIR__ . '/bootstrap.php';
$conn = new Dibi\Connection($config + ['formatDateTime' => "'Y-m-d H:i:s.u'", 'formatDate' => "'Y-m-d'"]);
// Dibi detects INSERT or REPLACE command & booleans
// dibi detects INSERT or REPLACE command & booleans
Assert::same(
reformat("REPLACE INTO [products] ([title], [price]) VALUES ('Drticka', 318)"),
$conn->translate('REPLACE INTO [products]', [
@@ -46,7 +46,7 @@ Assert::same(
);
// Dibi detects UPDATE command
// dibi detects UPDATE command
Assert::same(
reformat("UPDATE [colors] SET [color]='blue', [order]=12 WHERE [id]=123"),
$conn->translate('UPDATE [colors] SET', [
@@ -505,18 +505,6 @@ Assert::same(
);
Assert::same(
reformat('SELECT * FROM [table] WHERE (([left] = 1) OR ([top] = 2)) AND (number < 100)'),
$conn->translate('SELECT * FROM `table` WHERE %and', [
new Dibi\Expression('%or', [
'left' => 1,
'top' => 2,
]),
new Dibi\Expression('number < %i', 100),
])
);
$e = Assert::exception(function () use ($conn) {
$array6 = [
'id' => [1, 2, 3, 4],

View File

@@ -40,6 +40,9 @@ if ($config['driver'] === 'mysql' && PHP_VERSION_ID >= 70000) {
}
$conn = new Dibi\Connection($config);
function test(Closure $function)
{
$function();

View File

@@ -1,11 +0,0 @@
<?php
use Tester\Assert;
require __DIR__ . '/bootstrap.php';
$dt = new DateTime('2018-04-18 13:40:09.123456');
$res = dibi::stripMicroseconds($dt);
Assert::same('2018-04-18 13:40:09.123456', $dt->format('Y-m-d H:i:s.u'));
Assert::same('2018-04-18 13:40:09.000000', $res->format('Y-m-d H:i:s.u'));