1
0
mirror of https://github.com/dg/dibi.git synced 2025-08-05 21:58:10 +02:00

- improved examples

This commit is contained in:
David Grudl
2008-10-28 02:06:55 +00:00
parent ab892255d3
commit 7a6cdc8afa
14 changed files with 105 additions and 45 deletions

View File

@@ -1 +0,0 @@
- event, log, profiler

View File

@@ -12,23 +12,19 @@ dibi::connect(array(
// no limit
$res = dibi::query('SELECT * FROM [products]');
foreach ($res as $n => $row) {
print_r($row);
}
dibi::test('SELECT * FROM [products]');
// -> SELECT * FROM [products]
echo '<hr>';
// with limit = 2
$res = dibi::query('SELECT * FROM [products] %lmt', 2);
foreach ($res as $n => $row) {
print_r($row);
}
dibi::test('SELECT * FROM [products] %lmt', 2);
// -> SELECT * FROM [products] LIMIT 2
echo '<hr>';
// with limit = 2, offset = 1
$res = dibi::query('SELECT * FROM [products] %lmt %ofs', 2, 1);
foreach ($res as $n => $row) {
print_r($row);
}
dibi::test('SELECT * FROM [products] %lmt %ofs', 2, 1);
// -> SELECT * FROM [products] LIMIT 2 OFFSET 1

View File

@@ -26,3 +26,4 @@ INSERT INTO [mytable]", array(
'date' => dibi::date('12.3.2007'),
'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')

View File

@@ -22,9 +22,11 @@ echo '<h2>dibi::dump()</h2>';
// dump last query (dibi::$sql)
dibi::dump();
// -> SELECT * FROM [products] INNER JOIN [orders] USING ([product_id]) INNER JOIN [customers] USING ([customer_id])
// dump result table
echo '<h2>DibiResult::dump()</h2>';
$res->dump();
// -> [table]

View File

@@ -12,7 +12,7 @@ dibi::connect(array(
$id = 10;
$record = array(
'title' => 'Drtička na trávu',
'title' => 'Super product',
'price' => 318,
'active' => TRUE,
);
@@ -25,6 +25,10 @@ dibi::select('product_id')->as('id')
->innerJoin('customers USING (customer_id)')
->orderBy('title')
->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";
@@ -32,6 +36,9 @@ echo "\n";
echo dibi::select('title')->as('id')
->from('products')
->fetchSingle();
// -> Chair (as result of query: SELECT [title] AS [id] FROM [products])
echo "\n";
@@ -39,6 +46,9 @@ echo "\n";
dibi::insert('products', $record)
->setFlag('IGNORE')
->test();
// -> INSERT IGNORE INTO [products] ([title], [price], [active]) VALUES ('Super product', 318, 1)
echo "\n";
@@ -46,6 +56,9 @@ echo "\n";
dibi::update('products', $record)
->where('product_id = %d', $id)
->test();
// -> UPDATE [products] SET [title]='Super product', [price]=318, [active]=1 WHERE product_id = 10
echo "\n";
@@ -53,6 +66,9 @@ echo "\n";
dibi::delete('products')
->where('product_id = %d', $id)
->test();
// -> DELETE FROM [products] WHERE product_id = 10
echo "\n";
@@ -62,9 +78,13 @@ dibi::command()
->where('product_id = %d', $id)
->set($record)
->test();
// -> UPDATE [products] SET [title]='Super product', [price]=318, [active]=1 WHERE product_id = 10
echo "\n";
dibi::command()
->truncate('products')
->test();
// -> TRUNCATE [products]

View File

@@ -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;

View File

@@ -13,9 +13,15 @@ dibi::connect(array(
$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('added', Dibi::FIELD_DATETIME, 'H:i j.n.Y');
$row = $res->fetch();
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"
// }

View File

@@ -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
require_once 'Nette/Debug.php';
require_once '../dibi/dibi.php';
// enable Nette::Debug
Debug::enable();
Debug::enableProfiler();
dibi::connect(array(

View File

@@ -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>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

Binary file not shown.

View File

@@ -19,11 +19,13 @@ dibi::connect(array(
// dibi detects INSERT or REPLACE command
dibi::test('
REPLACE INTO [products]', array(
'title' => 'Drti<EFBFBD>ka na tr<74>vu',
'price' => 318,
'active' => TRUE,
REPLACE INTO [products]', array(
'title' => 'Super product',
'price' => 318,
'active' => TRUE,
));
// -> REPLACE INTO [products] ([title], [price], [active]) VALUES ('Super product', 318, 1)
// multiple INSERT command
@@ -34,15 +36,19 @@ $array = array(
'created' => dibi::datetime(),
);
dibi::test("INSERT INTO [products]", $array, $array, $array);
// -> INSERT INTO [products] ([title], [price], [brand], [created]) VALUES ('Super Product', ...) , (...) , (...)
// dibi detects UPDATE command
dibi::test("
UPDATE [colors] SET", array(
'color' => 'blue',
'order' => 12,
), "
WHERE [id]=%i", 123);
UPDATE [colors] SET", array(
'color' => 'blue',
'order' => 12,
), "
WHERE [id]=%i", 123);
// -> UPDATE [colors] SET [color]='blue', [order]=12 WHERE [id]=123
// SELECT
@@ -50,20 +56,24 @@ $ipMask = '192.168.%';
$timestamp = mktime(0, 0, 0, 10, 13, 1997);
dibi::test('
SELECT COUNT(*) as [count]
FROM [comments]
WHERE [ip] LIKE %s', $ipMask, '
AND [date] > ', dibi::date($timestamp)
SELECT COUNT(*) as [count]
FROM [comments]
WHERE [ip] LIKE %s', $ipMask, '
AND [date] > ', dibi::date($timestamp)
);
// -> SELECT COUNT(*) as [count] FROM [comments] WHERE [ip] LIKE '192.168.%' AND [date] > 876693600
// IN array
$array = array(1, 2, 3);
dibi::test("
SELECT *
FROM [people]
WHERE [id] IN (", $array, ")
SELECT *
FROM [people]
WHERE [id] IN (", $array, ")
");
// -> SELECT * FROM [people] WHERE [id] IN ( 1, 2, 3 )
// ORDER BY array
@@ -76,3 +86,4 @@ SELECT *
FROM [people]
ORDER BY %by", $order, "
");
// -> SELECT * FROM [people] ORDER BY [field1] ASC, [field2] DESC

View File

@@ -14,8 +14,8 @@ dibi::connect(array(
));
$cond1 = rand(0,2) < 1;
$cond2 = rand(0,2) < 1;
$cond1 = TRUE;
$cond2 = FALSE;
$foo = -1;
$bar = 2;
@@ -28,6 +28,9 @@ SELECT *
FROM [customers]
%if', isset($name), 'WHERE [name] LIKE %s', $name, '%end'
);
// -> SELECT * FROM [customers] WHERE [name] LIKE 'K%'
// if & else & (optional) end
@@ -38,6 +41,8 @@ WHERE [id] > 0
%if", ($foo > 0), "AND [foo]=%i", $foo, "
%else %if", ($bar > 0), "AND [bar]=%i", $bar, "
");
// -> SELECT * FROM [people] WHERE [id] > 0 AND [bar]=2
// nested condition
@@ -49,3 +54,4 @@ WHERE
%if', $cond2, 'AND [admin]=1 %end
%else 1 LIMIT 10 %end'
);
// -> SELECT * FROM [customers] WHERE LIMIT 10

View File

@@ -10,12 +10,14 @@ dibi::connect(array(
));
// create new substitution :blog: ==> wp_
dibi::addSubst('blog', 'wp_');
// generate and dump SQL
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_
dibi::addSubst('', 'my_');
// generate and dump SQL
dibi::test("UPDATE [database.::table] SET [text]='Hello World'");
// -> UPDATE [database].[my_table] SET [text]='Hello World'
// create substitution fallback
function substFallBack($expr)
{
return 'the_' . $expr;
}
// create substitution fallback
dibi::setSubstFallBack('substFallBack');
// generate and dump SQL
dibi::test("UPDATE [:account:user] SET [name]='John Doe'");
// -> UPDATE [the_accountuser] SET [name]='John Doe'

View File

@@ -13,7 +13,7 @@ dibi::connect(array(
echo "<h2>Before:</h2>\n";
dibi::query('SELECT * FROM [products]')->dump();
// -> 3 rows
dibi::begin();
@@ -26,3 +26,4 @@ dibi::rollback(); // or dibi::commit();
echo "<h2>After:</h2>\n";
dibi::query('SELECT * FROM [products]')->dump();
// -> 3 rows