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

examples: better headings and comments

This commit is contained in:
David Grudl
2010-08-03 11:48:51 +02:00
parent 65dba03652
commit 8f9435b518
16 changed files with 154 additions and 103 deletions

View File

@@ -1,6 +1,6 @@
<!DOCTYPE html><link rel="stylesheet" href="data/style.css">
<h1>dibi apply limit/offset example</h1>
<h1>Using Limit & Offset | dibi</h1>
<?php

View File

@@ -1,6 +1,6 @@
<!DOCTYPE html><link rel="stylesheet" href="data/style.css">
<h1>dibi::connect() example</h1>
<h1>Connecting to Databases | dibi</h1>
<?php
@@ -8,7 +8,7 @@ require_once 'Nette/Debug.php';
require_once '../dibi/dibi.php';
// connects to SQlite
// connects to SQlite using dibi class
echo '<p>Connecting to Sqlite: ';
try {
dibi::connect(array(
@@ -25,6 +25,23 @@ echo "</p>\n";
// connects to SQlite using DibiConnection object
echo '<p>Connecting to Sqlite: ';
try {
$connection = new DibiConnection(array(
'driver' => 'sqlite',
'database' => 'data/sample.sdb',
));
echo 'OK';
} catch (DibiException $e) {
echo get_class($e), ': ', $e->getMessage(), "\n";
}
echo "</p>\n";
// connects to MySQL using DSN
echo '<p>Connecting to MySQL: ';
try {
@@ -40,7 +57,7 @@ echo "</p>\n";
// connects to MySQLi using array
echo '<p>Connecting to MySQL: ';
echo '<p>Connecting to MySQLi: ';
try {
dibi::connect(array(
'driver' => 'mysqli',

View File

@@ -1,6 +1,6 @@
<!DOCTYPE html><link rel="stylesheet" href="data/style.css">
<h1>dibi dump example</h1>
<h1>Dumping SQL and Result Set | dibi</h1>
<?php
@@ -16,9 +16,9 @@ dibi::connect(array(
$res = dibi::query('
SELECT * FROM [products]
INNER JOIN [orders] USING ([product_id])
INNER JOIN [customers] USING ([customer_id])
SELECT * FROM [products]
INNER JOIN [orders] USING ([product_id])
INNER JOIN [customers] USING ([customer_id])
');
@@ -26,11 +26,9 @@ 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

@@ -1,6 +1,6 @@
<!DOCTYPE html><link rel="stylesheet" href="data/style.css">
<h1>dibi extension method example</h1>
<h1>Using Extension Methods | dibi</h1>
<?php

View File

@@ -1,6 +1,6 @@
<!DOCTYPE html><link rel="stylesheet" href="data/style.css">
<h1>dibi fetch example</h1>
<h1>Fetching Examples | dibi</h1>
<?php
@@ -27,43 +27,51 @@ product_id | title
// fetch a single row
$row = dibi::fetch('SELECT title FROM [products]');
echo "<h2>fetch()</h2>\n";
$row = dibi::fetch('SELECT title FROM products');
Debug::dump($row); // Chair
// fetch a single value
echo "<h2>fetchSingle()</h2>\n";
$value = dibi::fetchSingle('SELECT [title] FROM [products]');
Debug::dump($value); // Chair
// fetch complete result set
echo "<h2>fetchAll()</h2>\n";
$all = dibi::fetchAll('SELECT * FROM [products]');
Debug::dump($all);
// fetch complete result set like association array
echo "<h2>fetchAssoc('title')</h2>\n";
$res = dibi::query('SELECT * FROM [products]');
$assoc = $res->fetchAssoc('title'); // key
Debug::dump($assoc);
// fetch complete result set like pairs key => value
echo "<h2>fetchPairs('product_id', 'title')</h2>\n";
$pairs = $res->fetchPairs('product_id', 'title');
Debug::dump($pairs);
// fetch row by row
echo "<h2>using foreach</h2>\n";
foreach ($res as $n => $row) {
Debug::dump($row);
}
// fetch row by row with defined offset
echo "<h2>getIterator(2)</h2>\n";
foreach ($res->getIterator(2) as $n => $row) {
Debug::dump($row);
}
// fetch row by row with defined offset and limit
echo "<h2>getIterator(2, 1)</h2>\n";
foreach ($res->getIterator(2, 1) as $n => $row) {
Debug::dump($row);
}
@@ -71,17 +79,20 @@ foreach ($res->getIterator(2, 1) as $n => $row) {
// more complex association array
$res = dibi::query('
SELECT *
FROM [products]
INNER JOIN [orders] USING ([product_id])
INNER JOIN [customers] USING ([customer_id])
SELECT *
FROM [products]
INNER JOIN [orders] USING ([product_id])
INNER JOIN [customers] USING ([customer_id])
');
echo "<h2>fetchAssoc('customers.name|products.title')</h2>\n";
$assoc = $res->fetchAssoc('customers.name|products.title'); // key
Debug::dump($assoc);
echo "<h2>fetchAssoc('customers.name[]products.title')</h2>\n";
$assoc = $res->fetchAssoc('customers.name[]products.title'); // key
Debug::dump($assoc);
echo "<h2>fetchAssoc('customers.name->products.title')</h2>\n";
$assoc = $res->fetchAssoc('customers.name->products.title'); // key
Debug::dump($assoc);

View File

@@ -1,6 +1,6 @@
<!DOCTYPE html><link rel="stylesheet" href="data/style.css">
<h1>dibi fluent example</h1>
<h1>Using Fluent Syntax | dibi</h1>
<?php
@@ -36,8 +36,6 @@ dibi::select('product_id')->as('id')
echo "\n";
// SELECT ...
echo dibi::select('title')->as('id')
->from('products')
@@ -46,8 +44,6 @@ echo dibi::select('title')->as('id')
echo "\n";
// INSERT ...
dibi::insert('products', $record)
->setFlag('IGNORE')
@@ -56,8 +52,6 @@ dibi::insert('products', $record)
echo "\n";
// UPDATE ...
dibi::update('products', $record)
->where('product_id = %d', $id)
@@ -66,8 +60,6 @@ dibi::update('products', $record)
echo "\n";
// DELETE ...
dibi::delete('products')
->where('product_id = %d', $id)
@@ -76,8 +68,6 @@ dibi::delete('products')
echo "\n";
// custom commands
dibi::command()
->update('products')
@@ -88,8 +78,6 @@ dibi::command()
echo "\n";
dibi::command()
->truncate('products')
->test();

View File

@@ -1,6 +1,6 @@
<!DOCTYPE html><link rel="stylesheet" href="data/style.css">
<h1>dibi import SQL dump example</h1>
<h1>Importing SQL Dump from File | dibi</h1>
<?php

View File

@@ -1,6 +1,6 @@
<!DOCTYPE html><link rel="stylesheet" href="data/style.css">
<h1>dibi logger example</h1>
<h1>Using Logger | dibi</h1>
<?php
@@ -17,7 +17,7 @@ dibi::connect(array(
));
// enable log to this file
// enable query logging to this file
dibi::getProfiler()->setFile('data/log.sql');
@@ -34,6 +34,7 @@ try {
}
// outputs a log file
echo "<h2>File data/log.sql:</h2>";
echo '<pre>', file_get_contents('data/log.sql'), '</pre>';

View File

@@ -1,6 +1,6 @@
<!DOCTYPE html><link rel="stylesheet" href="data/style.css">
<h1>dibi metatypes example</h1>
<h1>Result Set Data Types | dibi</h1>
<?php
@@ -16,14 +16,28 @@ dibi::connect(array(
));
// using manual hints
$res = dibi::query('SELECT * FROM [customers]');
// auto-converts this column to integer
$res->setType('customer_id', Dibi::INTEGER);
$res->setType('added', Dibi::DATETIME, 'H:i j.n.Y');
$res->setType('customer_id', Dibi::INTEGER)
->setType('added', Dibi::DATETIME, 'H:i j.n.Y');
$row = $res->fetch();
Debug::dump($row);
Debug::dump( $res->fetch() );
// outputs:
// object(DibiRow)#3 (3) {
// customer_id => int(1)
// name => string(11) "Dave Lister"
// added => object(DateTime53) {}
// }
// using auto-detection (works well with MySQL or other strictly typed databases)
$res = dibi::query('SELECT * FROM [customers]');
$res->detectTypes();
Debug::dump( $res->fetch() );
// outputs:
// object(DibiRow)#3 (3) {
// customer_id => int(1)

View File

@@ -1,6 +1,6 @@
<!DOCTYPE html><link rel="stylesheet" href="data/style.css">
<h1>Nette\Debug & dibi example</h1>
<h1>Nette\Debug & SQL Exceptions | dibi</h1>
<p>Dibi can display and log exceptions via Nette\Debug, part of Nette Framework.</p>
@@ -27,6 +27,5 @@ dibi::connect(array(
));
// throws error
// throws error because SQL is bad
dibi::query('SELECT FROM [customers] WHERE [customer_id] < %i', 38);

View File

@@ -1,6 +1,6 @@
<!DOCTYPE html><link rel="stylesheet" href="data/style.css">
<h1>Nette\Debug & dibi example 2</h1>
<h1>Nette\Debug & Variables | dibi</h1>
<p>Dibi can dump variables via Nette\Debug, part of Nette Framework.</p>
@@ -27,6 +27,4 @@ dibi::connect(array(
));
// throws error
Debug::barDump( dibi::fetchAll('SELECT * FROM [customers] WHERE [customer_id] < %i', 38), '[customers]' );

View File

@@ -1,7 +1,8 @@
<?php ob_start(1) // needed by FirePHP ?>
<!DOCTYPE html><link rel="stylesheet" href="data/style.css">
<h1>Dibi profiler example</h1>
<h1>Using Profiler | dibi</h1>
<?php
@@ -16,10 +17,12 @@ dibi::connect(array(
));
// execute some queries...
for ($i=0; $i<20; $i++) {
$res = dibi::query('SELECT * FROM [customers] WHERE [customer_id] < %i', $i);
}
// display output
?>
<p>Last query: <strong><?php echo dibi::$sql; ?></strong></p>

View File

@@ -1,6 +1,6 @@
<!DOCTYPE html><link rel="stylesheet" href="data/style.css">
<h1>dibi SQL builder example</h1>
<h1>Query Language Basic Examples | dibi</h1>
<?php
@@ -16,11 +16,25 @@ dibi::connect(array(
));
// SELECT
$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 '192.168.%' AND [date] > 876693600
// dibi detects INSERT or REPLACE command
dibi::test('
REPLACE INTO [products]', array(
'title' => 'Super product',
'price' => 318,
'title' => 'Super product',
'price' => 318,
'active' => TRUE,
));
// -> REPLACE INTO [products] ([title], [price], [active]) VALUES ('Super product', 318, 1)
@@ -29,9 +43,9 @@ dibi::test('
// multiple INSERT command
$array = array(
'title' => 'Super Product',
'price' => 12,
'brand' => NULL,
'title' => 'Super Product',
'price' => 12,
'brand' => NULL,
'created' => new DateTime,
);
dibi::test("INSERT INTO [products]", $array, $array, $array);
@@ -50,21 +64,7 @@ dibi::test("
// SELECT
$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 '192.168.%' AND [date] > 876693600
// IN array
// modifier applied to array
$array = array(1, 2, 3);
dibi::test("
SELECT *
@@ -75,14 +75,22 @@ dibi::test("
// ORDER BY array
// modifier %by for ORDER BY
$order = array(
'field1' => 'asc',
'field2' => 'desc',
);
dibi::test("
SELECT *
FROM [people]
ORDER BY %by", $order, "
SELECT *
FROM [people]
ORDER BY %by", $order, "
");
// -> SELECT * FROM [people] ORDER BY [field1] ASC, [field2] DESC
// indentifiers and strings syntax mix
dibi::test('UPDATE [table] SET `item` = "5 1/4"" diskette"');
// -> UPDATE [table] SET [item] = '5 1/4" diskette'

View File

@@ -1,6 +1,6 @@
<!DOCTYPE html><link rel="stylesheet" href="data/style.css">
<h1>dibi conditional SQL example</h1>
<h1>Query Language & Conditions | dibi</h1>
<?php
@@ -14,19 +14,20 @@ dibi::connect(array(
));
// some variables
$cond1 = TRUE;
$cond2 = FALSE;
$foo = -1;
$bar = 2;
// conditional variable
$name = $cond1 ? 'K%' : NULL;
// if & end
dibi::test('
SELECT *
FROM [customers]
%if', isset($name), 'WHERE [name] LIKE %s', $name, '%end'
SELECT *
FROM [customers]
%if', isset($name), 'WHERE [name] LIKE %s', $name, '%end'
);
// -> SELECT * FROM [customers] WHERE [name] LIKE 'K%'
@@ -35,11 +36,11 @@ FROM [customers]
// if & else & (optional) end
dibi::test("
SELECT *
FROM [people]
WHERE [id] > 0
%if", ($foo > 0), "AND [foo]=%i", $foo, "
%else %if", ($bar > 0), "AND [bar]=%i", $bar, "
SELECT *
FROM [people]
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
@@ -47,11 +48,11 @@ WHERE [id] > 0
// nested condition
dibi::test('
SELECT *
FROM [customers]
WHERE
%if', isset($name), '[name] LIKE %s', $name, '
%if', $cond2, 'AND [admin]=1 %end
%else 1 LIMIT 10 %end'
SELECT *
FROM [customers]
WHERE
%if', isset($name), '[name] LIKE %s', $name, '
%if', $cond2, 'AND [admin]=1 %end
%else 1 LIMIT 10 %end'
);
// -> 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">
<h1>dibi prefix & substitute example</h1>
<h1>Using Substitutions | dibi</h1>
<?php
@@ -19,24 +19,34 @@ dibi::connect(array(
// create new substitution :blog: ==> wp_
dibi::addSubst('blog', 'wp_');
dibi::test("UPDATE :blog:items SET [text]='Hello World'");
// -> UPDATE wp_items SET [text]='Hello World'
dibi::test("SELECT * FROM [:blog:items]");
// -> SELECT * FROM [wp_items]
// create substitution fallback
// create substitutions using fallback callback
function substFallBack($expr)
{
if (defined($expr)) {
return constant($expr);
$const = 'SUBST_' . strtoupper($expr);
if (defined($const)) {
return constant($const);
} else {
return 'the_' . $expr;
throw new Exception("Undefined substitution :$expr:");
}
}
// define callback
dibi::setSubstFallBack('substFallBack');
dibi::test("UPDATE [:account:user] SET [name]='John Doe', [active]=:true:");
// -> UPDATE [the_accountuser] SET [name]='John Doe', [active]=1
// define substitutes as constants
define('SUBST_ACCOUNT', 'eshop_');
define('SUBST_ACTIVE', 7);
dibi::test("
UPDATE [:account:user]
SET [name]='John Doe', [status]=:active:
WHERE id=", 7
);
// -> UPDATE [the_accountuser] SET [name]='John Doe', [status]=7 WHERE id=7

View File

@@ -1,6 +1,6 @@
<!DOCTYPE html><link rel="stylesheet" href="data/style.css">
<h1>dibi transaction example</h1>
<h1>Using Transactions | dibi</h1>
<?php
@@ -14,7 +14,7 @@ dibi::connect(array(
));
echo "<h2>Before:</h2>\n";
echo "<h2>Before</h2>\n";
dibi::query('SELECT * FROM [products]')->dump();
// -> 3 rows
@@ -23,10 +23,13 @@ dibi::begin();
dibi::query('INSERT INTO [products]', array(
'title' => 'Test product',
));
echo "<h2>After INSERT</h2>\n";
dibi::query('SELECT * FROM [products]')->dump();
dibi::rollback(); // or dibi::commit();
echo "<h2>After:</h2>\n";
echo "<h2>After rollback</h2>\n";
dibi::query('SELECT * FROM [products]')->dump();
// -> 3 rows
// -> 3 rows again