mirror of
https://github.com/dg/dibi.git
synced 2025-08-06 14:16:39 +02:00
- improved examples
This commit is contained in:
@@ -12,23 +12,19 @@ dibi::connect(array(
|
|||||||
|
|
||||||
|
|
||||||
// no limit
|
// no limit
|
||||||
$res = dibi::query('SELECT * FROM [products]');
|
dibi::test('SELECT * FROM [products]');
|
||||||
foreach ($res as $n => $row) {
|
// -> SELECT * FROM [products]
|
||||||
print_r($row);
|
|
||||||
}
|
|
||||||
|
|
||||||
echo '<hr>';
|
echo '<hr>';
|
||||||
|
|
||||||
// with limit = 2
|
// with limit = 2
|
||||||
$res = dibi::query('SELECT * FROM [products] %lmt', 2);
|
dibi::test('SELECT * FROM [products] %lmt', 2);
|
||||||
foreach ($res as $n => $row) {
|
// -> SELECT * FROM [products] LIMIT 2
|
||||||
print_r($row);
|
|
||||||
}
|
|
||||||
|
|
||||||
echo '<hr>';
|
echo '<hr>';
|
||||||
|
|
||||||
// with limit = 2, offset = 1
|
// with limit = 2, offset = 1
|
||||||
$res = dibi::query('SELECT * FROM [products] %lmt %ofs', 2, 1);
|
dibi::test('SELECT * FROM [products] %lmt %ofs', 2, 1);
|
||||||
foreach ($res as $n => $row) {
|
// -> SELECT * FROM [products] LIMIT 2 OFFSET 1
|
||||||
print_r($row);
|
|
||||||
}
|
|
||||||
|
@@ -26,3 +26,4 @@ INSERT INTO [mytable]", array(
|
|||||||
'date' => dibi::date('12.3.2007'),
|
'date' => dibi::date('12.3.2007'),
|
||||||
'stamp' => dibi::dateTime('23.1.2007 10:23'),
|
'stamp' => dibi::dateTime('23.1.2007 10:23'),
|
||||||
));
|
));
|
||||||
|
// -> INSERT INTO [mytable] ([id], [date], [stamp]) VALUES (123, '2007-03-12', '2007-01-23 10-23-00')
|
||||||
|
@@ -22,9 +22,11 @@ echo '<h2>dibi::dump()</h2>';
|
|||||||
|
|
||||||
// dump last query (dibi::$sql)
|
// dump last query (dibi::$sql)
|
||||||
dibi::dump();
|
dibi::dump();
|
||||||
|
// -> SELECT * FROM [products] INNER JOIN [orders] USING ([product_id]) INNER JOIN [customers] USING ([customer_id])
|
||||||
|
|
||||||
|
|
||||||
// dump result table
|
// dump result table
|
||||||
echo '<h2>DibiResult::dump()</h2>';
|
echo '<h2>DibiResult::dump()</h2>';
|
||||||
|
|
||||||
$res->dump();
|
$res->dump();
|
||||||
|
// -> [table]
|
||||||
|
@@ -12,7 +12,7 @@ dibi::connect(array(
|
|||||||
|
|
||||||
$id = 10;
|
$id = 10;
|
||||||
$record = array(
|
$record = array(
|
||||||
'title' => 'Drtička na trávu',
|
'title' => 'Super product',
|
||||||
'price' => 318,
|
'price' => 318,
|
||||||
'active' => TRUE,
|
'active' => TRUE,
|
||||||
);
|
);
|
||||||
@@ -25,6 +25,10 @@ dibi::select('product_id')->as('id')
|
|||||||
->innerJoin('customers USING (customer_id)')
|
->innerJoin('customers USING (customer_id)')
|
||||||
->orderBy('title')
|
->orderBy('title')
|
||||||
->test();
|
->test();
|
||||||
|
// -> SELECT [product_id] AS [id] , [title] FROM [products] INNER JOIN [orders]
|
||||||
|
// USING (product_id) INNER JOIN customers USING (customer_id) ORDER BY [title]
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
echo "\n";
|
echo "\n";
|
||||||
|
|
||||||
@@ -32,6 +36,9 @@ echo "\n";
|
|||||||
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])
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
echo "\n";
|
echo "\n";
|
||||||
|
|
||||||
@@ -39,6 +46,9 @@ echo "\n";
|
|||||||
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)
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
echo "\n";
|
echo "\n";
|
||||||
|
|
||||||
@@ -46,6 +56,9 @@ echo "\n";
|
|||||||
dibi::update('products', $record)
|
dibi::update('products', $record)
|
||||||
->where('product_id = %d', $id)
|
->where('product_id = %d', $id)
|
||||||
->test();
|
->test();
|
||||||
|
// -> UPDATE [products] SET [title]='Super product', [price]=318, [active]=1 WHERE product_id = 10
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
echo "\n";
|
echo "\n";
|
||||||
|
|
||||||
@@ -53,6 +66,9 @@ echo "\n";
|
|||||||
dibi::delete('products')
|
dibi::delete('products')
|
||||||
->where('product_id = %d', $id)
|
->where('product_id = %d', $id)
|
||||||
->test();
|
->test();
|
||||||
|
// -> DELETE FROM [products] WHERE product_id = 10
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
echo "\n";
|
echo "\n";
|
||||||
|
|
||||||
@@ -62,9 +78,13 @@ dibi::command()
|
|||||||
->where('product_id = %d', $id)
|
->where('product_id = %d', $id)
|
||||||
->set($record)
|
->set($record)
|
||||||
->test();
|
->test();
|
||||||
|
// -> UPDATE [products] SET [title]='Super product', [price]=318, [active]=1 WHERE product_id = 10
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
echo "\n";
|
echo "\n";
|
||||||
|
|
||||||
dibi::command()
|
dibi::command()
|
||||||
->truncate('products')
|
->truncate('products')
|
||||||
->test();
|
->test();
|
||||||
|
// -> TRUNCATE [products]
|
||||||
|
@@ -11,6 +11,6 @@ dibi::connect(array(
|
|||||||
));
|
));
|
||||||
|
|
||||||
|
|
||||||
$count = dibi::loadFile('compress.zlib://dump.sql.gz');
|
$count = dibi::loadFile('compress.zlib://sample.dump.sql.gz');
|
||||||
|
|
||||||
echo 'Number of SQL commands:', $count;
|
echo 'Number of SQL commands:', $count;
|
@@ -13,9 +13,15 @@ dibi::connect(array(
|
|||||||
|
|
||||||
$res = dibi::query('SELECT * FROM [customers]');
|
$res = dibi::query('SELECT * FROM [customers]');
|
||||||
|
|
||||||
// auto-convert this column to integer
|
// auto-converts this column to integer
|
||||||
$res->setType('customer_id', Dibi::FIELD_INTEGER);
|
$res->setType('customer_id', Dibi::FIELD_INTEGER);
|
||||||
$res->setType('added', Dibi::FIELD_DATETIME, 'H:i j.n.Y');
|
$res->setType('added', Dibi::FIELD_DATETIME, 'H:i j.n.Y');
|
||||||
|
|
||||||
$row = $res->fetch();
|
$row = $res->fetch();
|
||||||
var_dump($row);
|
var_dump($row);
|
||||||
|
// outputs:
|
||||||
|
// object(DibiRow)#3 (3) {
|
||||||
|
// customer_id => int(1)
|
||||||
|
// name => string(11) "Dave Lister"
|
||||||
|
// added => string(15) "17:20 11.3.2007"
|
||||||
|
// }
|
||||||
|
@@ -1,11 +1,20 @@
|
|||||||
<h1>Nette::Debug && dibi example</h1>
|
<h1>Nette::Debug & dibi example</h1>
|
||||||
|
|
||||||
|
|
||||||
|
<p>Dibi can display and log exceptions via Nette::Debug. You first need to install Nette Framework. You can download it from here:</p>
|
||||||
|
|
||||||
|
<ul>
|
||||||
|
<li>Nette Framework: http://nettephp.com
|
||||||
|
</ul>
|
||||||
|
|
||||||
<?php
|
<?php
|
||||||
|
|
||||||
require_once 'Nette/Debug.php';
|
require_once 'Nette/Debug.php';
|
||||||
require_once '../dibi/dibi.php';
|
require_once '../dibi/dibi.php';
|
||||||
|
|
||||||
|
|
||||||
|
// enable Nette::Debug
|
||||||
Debug::enable();
|
Debug::enable();
|
||||||
Debug::enableProfiler();
|
|
||||||
|
|
||||||
|
|
||||||
dibi::connect(array(
|
dibi::connect(array(
|
||||||
|
@@ -24,3 +24,12 @@ for ($i=0; $i<20; $i++) {
|
|||||||
<p>Elapsed time for last query: <strong><?php echo sprintf('%0.3f', dibi::$elapsedTime * 1000); ?> ms</strong></p>
|
<p>Elapsed time for last query: <strong><?php echo sprintf('%0.3f', dibi::$elapsedTime * 1000); ?> ms</strong></p>
|
||||||
|
|
||||||
<p>Total elapsed time: <strong><?php echo sprintf('%0.3f', dibi::$totalTime * 1000); ?> ms</strong></p>
|
<p>Total elapsed time: <strong><?php echo sprintf('%0.3f', dibi::$totalTime * 1000); ?> ms</strong></p>
|
||||||
|
|
||||||
|
<br>
|
||||||
|
|
||||||
|
<p>Dibi can log to your Firebug Console. You first need to install the Firefox, Firebug and FirePHP extensions. You can install them from here:</p>
|
||||||
|
|
||||||
|
<ul>
|
||||||
|
<li>Firebug: https://addons.mozilla.org/en-US/firefox/addon/1843
|
||||||
|
<li>FirePHP: http://www.firephp.org/
|
||||||
|
</ul>
|
BIN
examples/sample.dump.sql.gz
Normal file
BIN
examples/sample.dump.sql.gz
Normal file
Binary file not shown.
@@ -19,11 +19,13 @@ dibi::connect(array(
|
|||||||
|
|
||||||
// dibi detects INSERT or REPLACE command
|
// dibi detects INSERT or REPLACE command
|
||||||
dibi::test('
|
dibi::test('
|
||||||
REPLACE INTO [products]', array(
|
REPLACE INTO [products]', array(
|
||||||
'title' => 'Drti<EFBFBD>ka na tr<74>vu',
|
'title' => 'Super product',
|
||||||
'price' => 318,
|
'price' => 318,
|
||||||
'active' => TRUE,
|
'active' => TRUE,
|
||||||
));
|
));
|
||||||
|
// -> REPLACE INTO [products] ([title], [price], [active]) VALUES ('Super product', 318, 1)
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
// multiple INSERT command
|
// multiple INSERT command
|
||||||
@@ -34,15 +36,19 @@ $array = array(
|
|||||||
'created' => dibi::datetime(),
|
'created' => dibi::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 detects UPDATE command
|
||||||
dibi::test("
|
dibi::test("
|
||||||
UPDATE [colors] SET", array(
|
UPDATE [colors] SET", array(
|
||||||
'color' => 'blue',
|
'color' => 'blue',
|
||||||
'order' => 12,
|
'order' => 12,
|
||||||
), "
|
), "
|
||||||
WHERE [id]=%i", 123);
|
WHERE [id]=%i", 123);
|
||||||
|
// -> UPDATE [colors] SET [color]='blue', [order]=12 WHERE [id]=123
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
// SELECT
|
// SELECT
|
||||||
@@ -50,20 +56,24 @@ $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 %s', $ipMask, '
|
WHERE [ip] LIKE %s', $ipMask, '
|
||||||
AND [date] > ', dibi::date($timestamp)
|
AND [date] > ', dibi::date($timestamp)
|
||||||
);
|
);
|
||||||
|
// -> SELECT COUNT(*) as [count] FROM [comments] WHERE [ip] LIKE '192.168.%' AND [date] > 876693600
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
// IN array
|
// IN array
|
||||||
$array = array(1, 2, 3);
|
$array = array(1, 2, 3);
|
||||||
dibi::test("
|
dibi::test("
|
||||||
SELECT *
|
SELECT *
|
||||||
FROM [people]
|
FROM [people]
|
||||||
WHERE [id] IN (", $array, ")
|
WHERE [id] IN (", $array, ")
|
||||||
");
|
");
|
||||||
|
// -> SELECT * FROM [people] WHERE [id] IN ( 1, 2, 3 )
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
// ORDER BY array
|
// ORDER BY array
|
||||||
@@ -76,3 +86,4 @@ SELECT *
|
|||||||
FROM [people]
|
FROM [people]
|
||||||
ORDER BY %by", $order, "
|
ORDER BY %by", $order, "
|
||||||
");
|
");
|
||||||
|
// -> SELECT * FROM [people] ORDER BY [field1] ASC, [field2] DESC
|
||||||
|
@@ -14,8 +14,8 @@ dibi::connect(array(
|
|||||||
));
|
));
|
||||||
|
|
||||||
|
|
||||||
$cond1 = rand(0,2) < 1;
|
$cond1 = TRUE;
|
||||||
$cond2 = rand(0,2) < 1;
|
$cond2 = FALSE;
|
||||||
$foo = -1;
|
$foo = -1;
|
||||||
$bar = 2;
|
$bar = 2;
|
||||||
|
|
||||||
@@ -28,6 +28,9 @@ SELECT *
|
|||||||
FROM [customers]
|
FROM [customers]
|
||||||
%if', isset($name), 'WHERE [name] LIKE %s', $name, '%end'
|
%if', isset($name), 'WHERE [name] LIKE %s', $name, '%end'
|
||||||
);
|
);
|
||||||
|
// -> SELECT * FROM [customers] WHERE [name] LIKE 'K%'
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
// if & else & (optional) end
|
// if & else & (optional) end
|
||||||
@@ -38,6 +41,8 @@ WHERE [id] > 0
|
|||||||
%if", ($foo > 0), "AND [foo]=%i", $foo, "
|
%if", ($foo > 0), "AND [foo]=%i", $foo, "
|
||||||
%else %if", ($bar > 0), "AND [bar]=%i", $bar, "
|
%else %if", ($bar > 0), "AND [bar]=%i", $bar, "
|
||||||
");
|
");
|
||||||
|
// -> SELECT * FROM [people] WHERE [id] > 0 AND [bar]=2
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
// nested condition
|
// nested condition
|
||||||
@@ -49,3 +54,4 @@ WHERE
|
|||||||
%if', $cond2, 'AND [admin]=1 %end
|
%if', $cond2, 'AND [admin]=1 %end
|
||||||
%else 1 LIMIT 10 %end'
|
%else 1 LIMIT 10 %end'
|
||||||
);
|
);
|
||||||
|
// -> SELECT * FROM [customers] WHERE LIMIT 10
|
@@ -10,12 +10,14 @@ dibi::connect(array(
|
|||||||
));
|
));
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
// create new substitution :blog: ==> wp_
|
// create new substitution :blog: ==> wp_
|
||||||
dibi::addSubst('blog', 'wp_');
|
dibi::addSubst('blog', 'wp_');
|
||||||
|
|
||||||
|
|
||||||
// generate and dump SQL
|
|
||||||
dibi::test("UPDATE [:blog:items] SET [text]='Hello World'");
|
dibi::test("UPDATE [:blog:items] SET [text]='Hello World'");
|
||||||
|
// -> UPDATE [wp_items] SET [text]='Hello World'
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
@@ -23,22 +25,20 @@ dibi::test("UPDATE [:blog:items] SET [text]='Hello World'");
|
|||||||
// create new substitution :: (empty) ==> my_
|
// create new substitution :: (empty) ==> my_
|
||||||
dibi::addSubst('', 'my_');
|
dibi::addSubst('', 'my_');
|
||||||
|
|
||||||
|
|
||||||
// generate and dump SQL
|
|
||||||
dibi::test("UPDATE [database.::table] SET [text]='Hello World'");
|
dibi::test("UPDATE [database.::table] SET [text]='Hello World'");
|
||||||
|
// -> UPDATE [database].[my_table] SET [text]='Hello World'
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
// create substitution fallback
|
||||||
function substFallBack($expr)
|
function substFallBack($expr)
|
||||||
{
|
{
|
||||||
return 'the_' . $expr;
|
return 'the_' . $expr;
|
||||||
}
|
}
|
||||||
|
|
||||||
// create substitution fallback
|
|
||||||
dibi::setSubstFallBack('substFallBack');
|
dibi::setSubstFallBack('substFallBack');
|
||||||
|
|
||||||
|
|
||||||
// generate and dump SQL
|
|
||||||
dibi::test("UPDATE [:account:user] SET [name]='John Doe'");
|
dibi::test("UPDATE [:account:user] SET [name]='John Doe'");
|
||||||
|
// -> UPDATE [the_accountuser] SET [name]='John Doe'
|
||||||
|
@@ -13,7 +13,7 @@ dibi::connect(array(
|
|||||||
|
|
||||||
echo "<h2>Before:</h2>\n";
|
echo "<h2>Before:</h2>\n";
|
||||||
dibi::query('SELECT * FROM [products]')->dump();
|
dibi::query('SELECT * FROM [products]')->dump();
|
||||||
|
// -> 3 rows
|
||||||
|
|
||||||
|
|
||||||
dibi::begin();
|
dibi::begin();
|
||||||
@@ -26,3 +26,4 @@ dibi::rollback(); // or dibi::commit();
|
|||||||
|
|
||||||
echo "<h2>After:</h2>\n";
|
echo "<h2>After:</h2>\n";
|
||||||
dibi::query('SELECT * FROM [products]')->dump();
|
dibi::query('SELECT * FROM [products]')->dump();
|
||||||
|
// -> 3 rows
|
||||||
|
Reference in New Issue
Block a user