mirror of
https://github.com/dg/dibi.git
synced 2025-08-03 20:57:36 +02:00
examples: dibi:: replaced with $dibi->
This commit is contained in:
@@ -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";
|
||||||
|
@@ -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)
|
||||||
|
@@ -13,7 +13,7 @@ Tracy\Debugger::enable();
|
|||||||
|
|
||||||
<?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);
|
||||||
|
@@ -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;
|
||||||
|
@@ -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
|
||||||
|
@@ -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'
|
||||||
|
@@ -17,14 +17,14 @@ date_default_timezone_set('Europe/Prague');
|
|||||||
|
|
||||||
<?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:
|
||||||
|
@@ -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">
|
||||||
|
|
||||||
|
@@ -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]');
|
||||||
|
|
||||||
|
|
||||||
?>
|
?>
|
||||||
|
@@ -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'),
|
||||||
|
@@ -13,7 +13,7 @@ Tracy\Debugger::enable();
|
|||||||
|
|
||||||
<?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);
|
||||||
|
@@ -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]
|
||||||
|
@@ -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
|
||||||
|
@@ -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>';
|
||||||
}
|
}
|
||||||
|
@@ -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
|
||||||
|
@@ -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
|
||||||
|
@@ -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
|
||||||
|
Reference in New Issue
Block a user