1
0
mirror of https://github.com/dg/dibi.git synced 2025-07-31 19:30:30 +02:00

examples: dibi:: replaced with $dibi->

This commit is contained in:
David Grudl
2018-04-17 10:03:43 +02:00
parent 3ccd802814
commit a6c53c7462
18 changed files with 77 additions and 77 deletions

View File

@@ -79,7 +79,7 @@ try {
'driver' => 'odbc',
'username' => 'root',
'password' => '***',
'dsn' => 'Driver={Microsoft Access Driver (*.mdb)};Dbq=' . __DIR__ . '/data/sample.mdb',
'dsn' => 'Driver={Microsoft Access Driver (*.mdb, *.accdb)};Dbq=' . __DIR__ . '/data/sample.mdb',
]);
echo 'OK';
} catch (Dibi\Exception $e) {

View File

@@ -12,14 +12,14 @@ if (@!include __DIR__ . '/../vendor/autoload.php') {
}
dibi::connect([
$dibi = new Dibi\Connection([
'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

@@ -12,13 +12,13 @@ if (@!include __DIR__ . '/../vendor/autoload.php') {
}
dibi::connect([
$dibi = new Dibi\Connection([
'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

@@ -14,7 +14,7 @@ Tracy\Debugger::enable();
<?php
dibi::connect([
$dibi = new Dibi\Connection([
'driver' => 'sqlite3',
'database' => 'data/sample.s3db',
]);
@@ -34,46 +34,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)
@@ -85,11 +85,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

@@ -12,12 +12,12 @@ if (@!include __DIR__ . '/../vendor/autoload.php') {
}
dibi::connect([
$dibi = new Dibi\Connection([
'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

@@ -12,7 +12,7 @@ if (@!include __DIR__ . '/../vendor/autoload.php') {
}
dibi::connect([
$dibi = new Dibi\Connection([
'driver' => 'sqlite3',
'database' => 'data/sample.s3db',
]);
@@ -28,7 +28,7 @@ $bar = 2;
$name = $cond1 ? 'K%' : null;
// if & end
dibi::test('
$dibi->test('
SELECT *
FROM customers
%if', isset($name), 'WHERE name LIKE ?', $name, '%end'
@@ -37,7 +37,7 @@ dibi::test('
// if & else & (optional) end
dibi::test('
$dibi->test('
SELECT *
FROM people
WHERE id > 0
@@ -48,7 +48,7 @@ dibi::test('
// nested condition
dibi::test('
$dibi->test('
SELECT *
FROM customers
WHERE
@@ -60,7 +60,7 @@ dibi::test('
// IF()
dibi::test('UPDATE products SET', [
'price' => ['IF(price_fixed, price, ?)', 123],
$dibi->test('UPDATE products SET', [
'price' => $dibi->expression('IF(price_fixed, price, ?)', 123),
]);
// -> SELECT * FROM customers WHERE LIMIT 10

View File

@@ -14,7 +14,7 @@ if (@!include __DIR__ . '/../vendor/autoload.php') {
date_default_timezone_set('Europe/Prague');
dibi::connect([
$dibi = new Dibi\Connection([
'driver' => 'sqlite3',
'database' => 'data/sample.s3db',
]);
@@ -24,7 +24,7 @@ dibi::connect([
$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, '
@@ -34,7 +34,7 @@ dibi::test('
// dibi detects INSERT or REPLACE command
dibi::test('
$dibi->test('
REPLACE INTO products', [
'title' => 'Super product',
'price' => 318,
@@ -50,12 +50,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,
@@ -66,7 +66,7 @@ dibi::test('
// modifier applied to array
$array = [1, 2, 3];
dibi::test('
$dibi->test('
SELECT *
FROM people
WHERE id IN (?)', $array
@@ -79,7 +79,7 @@ $order = [
'field1' => 'asc',
'field2' => 'desc',
];
dibi::test('
$dibi->test('
SELECT *
FROM people
ORDER BY %by', $order, '
@@ -88,5 +88,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

@@ -18,14 +18,14 @@ date_default_timezone_set('Europe/Prague');
<?php
dibi::connect([
$dibi = new Dibi\Connection([
'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)
@@ -41,7 +41,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

@@ -10,7 +10,7 @@ if (@!include __DIR__ . '/../vendor/autoload.php') {
Tracy\Debugger::enable();
$connection = dibi::connect([
$dibi = new Dibi\Connection([
'driver' => 'sqlite3',
'database' => 'data/sample.s3db',
'profiler' => [
@@ -21,11 +21,11 @@ $connection = dibi::connect([
// add panel to debug bar
$panel = new Dibi\Bridges\Tracy\Panel;
$panel->register($connection);
$panel->register($dibi);
// 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

@@ -10,7 +10,7 @@ if (@!include __DIR__ . '/../vendor/autoload.php') {
Tracy\Debugger::enable();
$connection = dibi::connect([
$dibi = new Dibi\Connection([
'driver' => 'sqlite3',
'database' => 'data/sample.s3db',
'profiler' => [
@@ -21,14 +21,14 @@ $connection = dibi::connect([
// add panel to debug bar
$panel = new Dibi\Bridges\Tracy\Panel;
$panel->register($connection);
$panel->register($dibi);
// 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

@@ -15,7 +15,7 @@ date_default_timezone_set('Europe/Prague');
// CHANGE TO REAL PARAMETERS!
dibi::connect([
$dibi = new Dibi\Connection([
'driver' => 'sqlite3',
'database' => 'data/sample.s3db',
'formatDate' => "'Y-m-d'",
@@ -24,7 +24,7 @@ dibi::connect([
// generate and dump SQL
dibi::test('
$dibi->test('
INSERT INTO [mytable]', [
'id' => 123,
'date' => new DateTime('12.3.2007'),

View File

@@ -14,7 +14,7 @@ Tracy\Debugger::enable();
<?php
dibi::connect([
$dibi = new Dibi\Connection([
'driver' => 'sqlite3',
'database' => 'data/sample.s3db',
]);
@@ -29,6 +29,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

@@ -14,7 +14,7 @@ if (@!include __DIR__ . '/../vendor/autoload.php') {
date_default_timezone_set('Europe/Prague');
dibi::connect([
$dibi = new Dibi\Connection([
'driver' => 'sqlite3',
'database' => 'data/sample.s3db',
]);
@@ -28,7 +28,7 @@ $record = [
];
// SELECT ...
dibi::select('product_id')->as('id')
$dibi->select('product_id')->as('id')
->select('title')
->from('products')
->innerJoin('orders')->using('(product_id)')
@@ -40,35 +40,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)
@@ -76,7 +76,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

@@ -12,22 +12,22 @@ if (@!include __DIR__ . '/../vendor/autoload.php') {
}
dibi::connect([
$dibi = new Dibi\Connection([
'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

@@ -14,7 +14,7 @@ if (@!include __DIR__ . '/../vendor/autoload.php') {
date_default_timezone_set('Europe/Prague');
dibi::connect([
$dibi = new Dibi\Connection([
'driver' => 'sqlite3',
'database' => 'data/sample.s3db',
// enable query logging to this file
@@ -26,11 +26,11 @@ dibi::connect([
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

@@ -13,7 +13,7 @@ if (@!include __DIR__ . '/../vendor/autoload.php') {
}
dibi::connect([
$dibi = new Dibi\Connection([
'driver' => 'sqlite3',
'database' => 'data/sample.s3db',
'profiler' => [
@@ -24,7 +24,7 @@ dibi::connect([
// 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

@@ -12,23 +12,23 @@ if (@!include __DIR__ . '/../vendor/autoload.php') {
}
dibi::connect([
$dibi = new Dibi\Connection([
'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'
@@ -45,13 +45,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

@@ -12,28 +12,28 @@ if (@!include __DIR__ . '/../vendor/autoload.php') {
}
dibi::connect([
$dibi = new Dibi\Connection([
'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