1
0
mirror of https://github.com/dg/dibi.git synced 2025-09-04 11:45:27 +02:00

Compare commits

..

9 Commits

Author SHA1 Message Date
David Grudl
5a7543a5a6 Released version 3.2.2 2018-05-10 21:55:12 +02:00
David Grudl
2848c965f9 Result: added getColumnCount() 2018-05-10 21:55:12 +02:00
David Grudl
3660f26e03 improved phpDoc, capitalized Dibi 2018-05-10 21:54:30 +02:00
David Grudl
f474abfeda type improvements 2018-05-02 10:49:01 +02:00
David Grudl
809386edf8 added dibi::stripMicroseconds 2018-04-19 13:04:47 +02:00
David Grudl
1909c98e6d Sqlite3Driver: for SQLite 3 is not needed to strip [] from column names 2018-04-19 13:04:47 +02:00
David Grudl
8985c71276 refactoring 2018-04-19 13:04:39 +02:00
David Grudl
0d5fd9d65b type fixes 2018-04-17 13:09:27 +02:00
David Grudl
4049ea4717 examples: dibi:: replaced with $dibi-> 2018-04-17 13:09:27 +02:00
64 changed files with 350 additions and 299 deletions

View File

@@ -1,6 +1,6 @@
<!DOCTYPE html><link rel="stylesheet" href="data/style.css"> <!DOCTYPE html><link rel="stylesheet" href="data/style.css">
<h1>Connecting to Databases | dibi</h1> <h1>Connecting to Databases | Dibi</h1>
<?php <?php
@@ -9,7 +9,7 @@ if (@!include __DIR__ . '/../vendor/autoload.php') {
} }
// connects to SQlite using dibi class // connects to SQlite using Dibi class
echo '<p>Connecting to Sqlite: '; echo '<p>Connecting to Sqlite: ';
try { try {
dibi::connect([ dibi::connect([

View File

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

View File

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

View File

@@ -1,6 +1,6 @@
<!DOCTYPE html><link rel="stylesheet" href="data/style.css"> <!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 <?php
@@ -9,12 +9,12 @@ if (@!include __DIR__ . '/../vendor/autoload.php') {
} }
dibi::connect([ $dibi = new Dibi\Connection([
'driver' => 'sqlite3', 'driver' => 'sqlite3',
'database' => 'data/sample.s3db', '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; echo 'Number of SQL commands:', $count;

View File

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

View File

@@ -1,6 +1,6 @@
<!DOCTYPE html><link rel="stylesheet" href="data/style.css"> <!DOCTYPE html><link rel="stylesheet" href="data/style.css">
<h1>Query Language Basic Examples | dibi</h1> <h1>Query Language Basic Examples | Dibi</h1>
<?php <?php
@@ -11,7 +11,7 @@ if (@!include __DIR__ . '/../vendor/autoload.php') {
date_default_timezone_set('Europe/Prague'); date_default_timezone_set('Europe/Prague');
dibi::connect([ $dibi = new Dibi\Connection([
'driver' => 'sqlite3', 'driver' => 'sqlite3',
'database' => 'data/sample.s3db', 'database' => 'data/sample.s3db',
]); ]);
@@ -21,7 +21,7 @@ dibi::connect([
$ipMask = '192.168.%'; $ipMask = '192.168.%';
$timestamp = mktime(0, 0, 0, 10, 13, 1997); $timestamp = mktime(0, 0, 0, 10, 13, 1997);
dibi::test(' $dibi->test('
SELECT COUNT(*) as [count] SELECT COUNT(*) as [count]
FROM [comments] FROM [comments]
WHERE [ip] LIKE ?', $ipMask, ' WHERE [ip] LIKE ?', $ipMask, '
@@ -31,7 +31,7 @@ dibi::test('
// dibi detects INSERT or REPLACE command // dibi detects INSERT or REPLACE command
dibi::test(' $dibi->test('
REPLACE INTO products', [ REPLACE INTO products', [
'title' => 'Super product', 'title' => 'Super product',
'price' => 318, 'price' => 318,
@@ -47,12 +47,12 @@ $array = [
'brand' => null, 'brand' => null,
'created' => new DateTime, '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', ...) , (...) , (...) // -> INSERT INTO products ([title], [price], [brand], [created]) VALUES ('Super Product', ...) , (...) , (...)
// dibi detects UPDATE command // dibi detects UPDATE command
dibi::test(' $dibi->test('
UPDATE colors SET', [ UPDATE colors SET', [
'color' => 'blue', 'color' => 'blue',
'order' => 12, 'order' => 12,
@@ -63,7 +63,7 @@ dibi::test('
// modifier applied to array // modifier applied to array
$array = [1, 2, 3]; $array = [1, 2, 3];
dibi::test(' $dibi->test('
SELECT * SELECT *
FROM people FROM people
WHERE id IN (?)', $array WHERE id IN (?)', $array
@@ -76,7 +76,7 @@ $order = [
'field1' => 'asc', 'field1' => 'asc',
'field2' => 'desc', 'field2' => 'desc',
]; ];
dibi::test(' $dibi->test('
SELECT * SELECT *
FROM people FROM people
ORDER BY %by', $order, ' ORDER BY %by', $order, '
@@ -85,5 +85,5 @@ dibi::test('
// indentifiers and strings syntax mix // 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' // -> 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"> <!DOCTYPE html><link rel="stylesheet" href="data/style.css">
<h1>Result Set Data Types | dibi</h1> <h1>Result Set Data Types | Dibi</h1>
<?php <?php
dibi::connect([ $dibi = new Dibi\Connection([
'driver' => 'sqlite3', 'driver' => 'sqlite3',
'database' => 'data/sample.s3db', 'database' => 'data/sample.s3db',
]); ]);
// using manual hints // using manual hints
$res = dibi::query('SELECT * FROM [customers]'); $res = $dibi->query('SELECT * FROM [customers]');
$res->setType('customer_id', Type::INTEGER) $res->setType('customer_id', Type::INTEGER)
->setType('added', Type::DATETIME) ->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) // 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()); Tracy\Dumper::dump($res->fetch());
// outputs: // outputs:

View File

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

View File

@@ -9,7 +9,7 @@ if (@!include __DIR__ . '/../vendor/autoload.php') {
Tracy\Debugger::enable(); Tracy\Debugger::enable();
$connection = dibi::connect([ $dibi = new Dibi\Connection([
'driver' => 'sqlite3', 'driver' => 'sqlite3',
'database' => 'data/sample.s3db', 'database' => 'data/sample.s3db',
'profiler' => [ 'profiler' => [
@@ -20,14 +20,14 @@ $connection = dibi::connect([
// add panel to debug bar // add panel to debug bar
$panel = new Dibi\Bridges\Tracy\Panel; $panel = new Dibi\Bridges\Tracy\Panel;
$panel->register($connection); $panel->register($dibi);
// query will be logged // query will be logged
dibi::query('SELECT 123'); $dibi->query('SELECT 123');
// result set will be dumped // 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,6 +1,6 @@
<!DOCTYPE html><link rel="stylesheet" href="data/style.css"> <!DOCTYPE html><link rel="stylesheet" href="data/style.css">
<h1>Using DateTime | dibi</h1> <h1>Using DateTime | Dibi</h1>
<?php <?php
@@ -12,7 +12,7 @@ date_default_timezone_set('Europe/Prague');
// CHANGE TO REAL PARAMETERS! // CHANGE TO REAL PARAMETERS!
dibi::connect([ $dibi = new Dibi\Connection([
'driver' => 'sqlite3', 'driver' => 'sqlite3',
'database' => 'data/sample.s3db', 'database' => 'data/sample.s3db',
'formatDate' => "'Y-m-d'", 'formatDate' => "'Y-m-d'",
@@ -21,7 +21,7 @@ dibi::connect([
// generate and dump SQL // generate and dump SQL
dibi::test(' $dibi->test('
INSERT INTO [mytable]', [ INSERT INTO [mytable]', [
'id' => 123, 'id' => 123,
'date' => new DateTime('12.3.2007'), 'date' => new DateTime('12.3.2007'),

View File

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

View File

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

View File

@@ -1,6 +1,6 @@
<!DOCTYPE html><link rel="stylesheet" href="data/style.css"> <!DOCTYPE html><link rel="stylesheet" href="data/style.css">
<h1>Using Limit & Offset | dibi</h1> <h1>Using Limit & Offset | Dibi</h1>
<?php <?php
@@ -9,22 +9,22 @@ if (@!include __DIR__ . '/../vendor/autoload.php') {
} }
dibi::connect([ $dibi = new Dibi\Connection([
'driver' => 'sqlite3', 'driver' => 'sqlite3',
'database' => 'data/sample.s3db', 'database' => 'data/sample.s3db',
]); ]);
// no limit // no limit
dibi::test('SELECT * FROM [products]'); $dibi->test('SELECT * FROM [products]');
// -> SELECT * FROM [products] // -> SELECT * FROM [products]
// with limit = 2 // with limit = 2
dibi::test('SELECT * FROM [products] %lmt', 2); $dibi->test('SELECT * FROM [products] %lmt', 2);
// -> SELECT * FROM [products] LIMIT 2 // -> SELECT * FROM [products] LIMIT 2
// with limit = 2, offset = 1 // 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 // -> SELECT * FROM [products] LIMIT 2 OFFSET 1

View File

@@ -1,6 +1,6 @@
<!DOCTYPE html><link rel="stylesheet" href="data/style.css"> <!DOCTYPE html><link rel="stylesheet" href="data/style.css">
<h1>Using Logger | dibi</h1> <h1>Using Logger | Dibi</h1>
<?php <?php
@@ -11,7 +11,7 @@ if (@!include __DIR__ . '/../vendor/autoload.php') {
date_default_timezone_set('Europe/Prague'); date_default_timezone_set('Europe/Prague');
dibi::connect([ $dibi = new Dibi\Connection([
'driver' => 'sqlite3', 'driver' => 'sqlite3',
'database' => 'data/sample.s3db', 'database' => 'data/sample.s3db',
// enable query logging to this file // enable query logging to this file
@@ -23,11 +23,11 @@ dibi::connect([
try { 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) { } catch (Dibi\Exception $e) {
echo '<p>', get_class($e), ': ', $e->getMessage(), '</p>'; echo '<p>', get_class($e), ': ', $e->getMessage(), '</p>';
} }

View File

@@ -2,7 +2,7 @@
<!DOCTYPE html><link rel="stylesheet" href="data/style.css"> <!DOCTYPE html><link rel="stylesheet" href="data/style.css">
<h1>Using Profiler | dibi</h1> <h1>Using Profiler | Dibi</h1>
<?php <?php
@@ -11,7 +11,7 @@ if (@!include __DIR__ . '/../vendor/autoload.php') {
} }
dibi::connect([ $dibi = new Dibi\Connection([
'driver' => 'sqlite3', 'driver' => 'sqlite3',
'database' => 'data/sample.s3db', 'database' => 'data/sample.s3db',
'profiler' => [ 'profiler' => [
@@ -22,7 +22,7 @@ dibi::connect([
// execute some queries... // execute some queries...
for ($i = 0; $i < 20; $i++) { 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 // display output

View File

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

View File

@@ -1,6 +1,6 @@
<!DOCTYPE html><link rel="stylesheet" href="data/style.css"> <!DOCTYPE html><link rel="stylesheet" href="data/style.css">
<h1>Using Transactions | dibi</h1> <h1>Using Transactions | Dibi</h1>
<?php <?php
@@ -9,28 +9,28 @@ if (@!include __DIR__ . '/../vendor/autoload.php') {
} }
dibi::connect([ $dibi = new Dibi\Connection([
'driver' => 'sqlite3', 'driver' => 'sqlite3',
'database' => 'data/sample.s3db', 'database' => 'data/sample.s3db',
]); ]);
echo "<h2>Before</h2>\n"; echo "<h2>Before</h2>\n";
dibi::query('SELECT * FROM [products]')->dump(); $dibi->query('SELECT * FROM [products]')->dump();
// -> 3 rows // -> 3 rows
dibi::begin(); $dibi->begin();
dibi::query('INSERT INTO [products]', [ $dibi->query('INSERT INTO [products]', [
'title' => 'Test product', 'title' => 'Test product',
]); ]);
echo "<h2>After INSERT</h2>\n"; 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"; echo "<h2>After rollback</h2>\n";
dibi::query('SELECT * FROM [products]')->dump(); $dibi->query('SELECT * FROM [products]')->dump();
// -> 3 rows again // -> 3 rows again

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@@ -1,7 +1,7 @@
<?php <?php
/** /**
* This file is part of the "dibi" - smart database abstraction layer. * This file is part of the Dibi, smart database abstraction layer (https://dibiphp.com)
* Copyright (c) 2005 David Grudl (https://davidgrudl.com) * Copyright (c) 2005 David Grudl (https://davidgrudl.com)
*/ */
@@ -11,7 +11,7 @@ use Dibi;
/** /**
* The dibi driver for Firebird/InterBase database. * The driver for Firebird/InterBase database.
* *
* Driver options: * Driver options:
* - database => the path to database file (server:/path/database.fdb) * - database => the path to database file (server:/path/database.fdb)
@@ -20,7 +20,6 @@ use Dibi;
* - charset => character encoding to set * - 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. * - 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 * - resource (resource) => existing connection resource
* - lazy, profiler, result, substitutes, ... => see Dibi\Connection options
*/ */
class FirebirdDriver implements Dibi\Driver, Dibi\ResultDriver, Dibi\Reflector class FirebirdDriver implements Dibi\Driver, Dibi\ResultDriver, Dibi\Reflector
{ {
@@ -375,7 +374,9 @@ class FirebirdDriver implements Dibi\Driver, Dibi\ResultDriver, Dibi\Reflector
*/ */
public function __destruct() public function __destruct()
{ {
$this->autoFree && $this->getResultResource() && $this->free(); if ($this->autoFree && $this->getResultResource()) {
$this->free();
}
} }

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@@ -1,7 +1,7 @@
<?php <?php
/** /**
* This file is part of the "dibi" - smart database abstraction layer. * This file is part of the Dibi, smart database abstraction layer (https://dibiphp.com)
* Copyright (c) 2005 David Grudl (https://davidgrudl.com) * Copyright (c) 2005 David Grudl (https://davidgrudl.com)
*/ */
@@ -12,7 +12,7 @@ use PDO;
/** /**
* The dibi driver for PDO. * The driver for PDO.
* *
* Driver options: * Driver options:
* - dsn => driver specific DSN * - dsn => driver specific DSN
@@ -21,13 +21,12 @@ use PDO;
* - options (array) => driver specific options {@see PDO::__construct} * - options (array) => driver specific options {@see PDO::__construct}
* - resource (PDO) => existing connection * - resource (PDO) => existing connection
* - version * - version
* - lazy, profiler, result, substitutes, ... => see Dibi\Connection options
*/ */
class PdoDriver implements Dibi\Driver, Dibi\ResultDriver class PdoDriver implements Dibi\Driver, Dibi\ResultDriver
{ {
use Dibi\Strict; use Dibi\Strict;
/** @var PDO Connection resource */ /** @var PDO|null Connection resource */
private $connection; private $connection;
/** @var \PDOStatement|null Resultset resource */ /** @var \PDOStatement|null Resultset resource */
@@ -200,7 +199,7 @@ class PdoDriver implements Dibi\Driver, Dibi\ResultDriver
/** /**
* Returns the connection resource. * Returns the connection resource.
* @return PDO * @return PDO|null
*/ */
public function getResource() public function getResource()
{ {

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@@ -1,7 +1,7 @@
<?php <?php
/** /**
* This file is part of the "dibi" - smart database abstraction layer. * This file is part of the Dibi, smart database abstraction layer (https://dibiphp.com)
* Copyright (c) 2005 David Grudl (https://davidgrudl.com) * Copyright (c) 2005 David Grudl (https://davidgrudl.com)
*/ */
@@ -9,20 +9,7 @@ namespace Dibi;
/** /**
* dibi result set. * Query result.
*
* <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 * @property-read int $rowCount
*/ */
@@ -42,10 +29,10 @@ class Result implements IDataSource
/** @var bool Already fetched? Used for allowance for first seek(0) */ /** @var bool Already fetched? Used for allowance for first seek(0) */
private $fetched = false; private $fetched = false;
/** @var string returned object class */ /** @var string|null returned object class */
private $rowClass = 'Dibi\Row'; private $rowClass = 'Dibi\Row';
/** @var callable returned object factory*/ /** @var callable|null returned object factory */
private $rowFactory; private $rowFactory;
/** @var array format */ /** @var array format */
@@ -147,6 +134,16 @@ 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**/ /********************* fetching rows ****************d*g**/
@@ -225,7 +222,7 @@ class Result implements IDataSource
* Fetches all records from table. * Fetches all records from table.
* @param int offset * @param int offset
* @param int limit * @param int limit
* @return Row[] * @return Row[]|array[]
*/ */
final public function fetchAll($offset = null, $limit = null) final public function fetchAll($offset = null, $limit = null)
{ {

View File

@@ -1,7 +1,7 @@
<?php <?php
/** /**
* This file is part of the "dibi" - smart database abstraction layer. * This file is part of the Dibi, smart database abstraction layer (https://dibiphp.com)
* Copyright (c) 2005 David Grudl (https://davidgrudl.com) * Copyright (c) 2005 David Grudl (https://davidgrudl.com)
*/ */
@@ -10,15 +10,6 @@ namespace Dibi;
/** /**
* External result set iterator. * 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 class ResultIterator implements \Iterator, \Countable
{ {
@@ -31,7 +22,7 @@ class ResultIterator implements \Iterator, \Countable
private $row; private $row;
/** @var int */ /** @var int */
private $pointer; private $pointer = 0;
/** /**

View File

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

View File

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

View File

@@ -1,7 +1,7 @@
<?php <?php
/** /**
* This file is part of the "dibi" - smart database abstraction layer. * This file is part of the Dibi, smart database abstraction layer (https://dibiphp.com)
* Copyright (c) 2005 David Grudl (https://davidgrudl.com) * Copyright (c) 2005 David Grudl (https://davidgrudl.com)
*/ */
@@ -9,7 +9,7 @@ namespace Dibi;
/** /**
* dibi SQL translator. * SQL translator.
*/ */
final class Translator final class Translator
{ {
@@ -39,10 +39,10 @@ final class Translator
/** @var int */ /** @var int */
private $ifLevelStart = 0; private $ifLevelStart = 0;
/** @var int */ /** @var int|null */
private $limit; private $limit;
/** @var int */ /** @var int|null */
private $offset; private $offset;
/** @var HashMap */ /** @var HashMap */
@@ -70,6 +70,7 @@ final class Translator
$args = array_values($args[0]); $args = array_values($args[0]);
} }
$this->args = $args; $this->args = $args;
$this->errors = [];
$commandIns = null; $commandIns = null;
$lastArr = null; $lastArr = null;

View File

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

View File

@@ -1,7 +1,7 @@
<?php <?php
/** /**
* This file is part of the "dibi" - smart database abstraction layer. * This file is part of the Dibi, smart database abstraction layer (https://dibiphp.com)
* Copyright (c) 2005 David Grudl (https://davidgrudl.com) * Copyright (c) 2005 David Grudl (https://davidgrudl.com)
*/ */
@@ -9,8 +9,7 @@ use Dibi\Type;
/** /**
* This class is static container class for creating DB objects and * Static container class for Dibi connections.
* store connections info.
*/ */
class dibi class dibi
{ {
@@ -22,8 +21,8 @@ class dibi
/** version */ /** version */
const const
VERSION = '3.2.1', VERSION = '3.2.2',
REVISION = 'released on 2018-04-14'; REVISION = 'released on 2018-05-02';
/** sorting order */ /** sorting order */
const const
@@ -49,13 +48,13 @@ class dibi
FIELD_DATETIME = Type::DATETIME, FIELD_DATETIME = Type::DATETIME,
FIELD_TIME = Type::TIME; FIELD_TIME = Type::TIME;
/** @var string Last SQL command @see dibi::query() */ /** @var string|null Last SQL command @see dibi::query() */
public static $sql; public static $sql;
/** @var int Elapsed time for last query */ /** @var float|null Elapsed time for last query */
public static $elapsedTime; public static $elapsedTime;
/** @var int Elapsed time for all queries */ /** @var float Elapsed time for all queries */
public static $totalTime; public static $totalTime;
/** @var int Number or queries */ /** @var int Number or queries */
@@ -85,7 +84,7 @@ class dibi
/** /**
* Creates a new Connection object and connects it to specified database. * Creates a new Connection object and connects it to specified database.
* @param mixed connection parameters * @param array|string connection parameters
* @param string connection name * @param string connection name
* @return Dibi\Connection * @return Dibi\Connection
* @throws Dibi\Exception * @throws Dibi\Exception
@@ -441,10 +440,22 @@ class dibi
* Prints out a syntax highlighted version of the SQL command or Result. * Prints out a syntax highlighted version of the SQL command or Result.
* @param string|Result * @param string|Result
* @param bool return output instead of printing it? * @param bool return output instead of printing it?
* @return string * @return string|null
*/ */
public static function dump($sql = null, $return = false) public static function dump($sql = null, $return = false)
{ {
return Dibi\Helpers::dump($sql, $return); 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 <?php
/** /**
* This file is part of the "dibi" - smart database abstraction layer. * This file is part of the Dibi, smart database abstraction layer (https://dibiphp.com)
* Copyright (c) 2005 David Grudl (https://davidgrudl.com) * Copyright (c) 2005 David Grudl (https://davidgrudl.com)
*/ */
@@ -9,7 +9,7 @@ namespace Dibi;
/** /**
* dibi common exception. * Dibi common exception.
*/ */
class Exception extends \Exception class Exception extends \Exception
{ {
@@ -18,9 +18,8 @@ class Exception extends \Exception
/** /**
* Construct a dibi exception.
* @param string Message describing the exception * @param string Message describing the exception
* @param mixed * @param string|int
* @param string SQL command * @param string SQL command
* @param \Exception * @param \Exception
*/ */
@@ -104,7 +103,7 @@ class ProcedureException extends Exception
* @param int Some code * @param int Some code
* @param string SQL command * @param string SQL command
*/ */
public function __construct($message = null, $code = 0, $severity = null, $sql = null) public function __construct($message = '', $code = 0, $severity = '', $sql = null)
{ {
parent::__construct($message, (int) $code, $sql); parent::__construct($message, (int) $code, $sql);
$this->severity = $severity; $this->severity = $severity;
@@ -117,7 +116,7 @@ class ProcedureException extends Exception
*/ */
public function getSeverity() public function getSeverity()
{ {
$this->severity; return $this->severity;
} }
} }

View File

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

View File

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

View File

@@ -11,12 +11,17 @@ require __DIR__ . '/bootstrap.php';
$conn = new Dibi\Connection($config); $conn = new Dibi\Connection($config);
$conn->loadFile(__DIR__ . "/data/$config[system].sql"); $conn->loadFile(__DIR__ . "/data/$config[system].sql");
$info = $conn->query(' $res = $conn->query('
SELECT products.product_id, orders.order_id, customers.name, products.product_id + 1 AS [xXx] SELECT products.product_id, orders.order_id, customers.name, products.product_id + 1 AS [xXx]
FROM ([products] FROM ([products]
INNER JOIN [orders] ON [products.product_id] = [orders.product_id]) INNER JOIN [orders] ON [products.product_id] = [orders.product_id])
INNER JOIN [customers] ON [orders.customer_id] = [customers.customer_id] INNER JOIN [customers] ON [orders.customer_id] = [customers.customer_id]
')->getInfo(); ');
$info = $res->getInfo();
Assert::same(4, $res->getColumnCount());
Assert::same( 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'"]); $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( Assert::same(
reformat("REPLACE INTO [products] ([title], [price]) VALUES ('Drticka', 318)"), reformat("REPLACE INTO [products] ([title], [price]) VALUES ('Drticka', 318)"),
$conn->translate('REPLACE INTO [products]', [ $conn->translate('REPLACE INTO [products]', [
@@ -46,7 +46,7 @@ Assert::same(
); );
// dibi detects UPDATE command // Dibi detects UPDATE command
Assert::same( Assert::same(
reformat("UPDATE [colors] SET [color]='blue', [order]=12 WHERE [id]=123"), reformat("UPDATE [colors] SET [color]='blue', [order]=12 WHERE [id]=123"),
$conn->translate('UPDATE [colors] SET', [ $conn->translate('UPDATE [colors] SET', [

View File

@@ -0,0 +1,11 @@
<?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'));