diff --git a/examples/apply-limit.php b/examples/apply-limit.php
index 01a56142..0ad8d6de 100644
--- a/examples/apply-limit.php
+++ b/examples/apply-limit.php
@@ -1,6 +1,6 @@
-
dibi apply limit/offset example
+Using Limit & Offset | dibi
-dibi::connect() example
+Connecting to Databases | dibi
Connecting to Sqlite: ';
try {
dibi::connect(array(
@@ -25,6 +25,23 @@ echo "\n";
+// connects to SQlite using DibiConnection object
+echo '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 "
\n";
+
+
+
+
// connects to MySQL using DSN
echo 'Connecting to MySQL: ';
try {
@@ -40,7 +57,7 @@ echo "
\n";
// connects to MySQLi using array
-echo 'Connecting to MySQL: ';
+echo '
Connecting to MySQLi: ';
try {
dibi::connect(array(
'driver' => 'mysqli',
diff --git a/examples/dump.php b/examples/dump.php
index afafa259..f25f4557 100644
--- a/examples/dump.php
+++ b/examples/dump.php
@@ -1,6 +1,6 @@
-
dibi dump example
+Dumping SQL and Result Set | dibi
dibi::dump()';
// 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 'DibiResult::dump() ';
$res->dump();
-// -> [table]
diff --git a/examples/extension.method.php b/examples/extension.method.php
index fa0e2c81..9a88a0f3 100644
--- a/examples/extension.method.php
+++ b/examples/extension.method.php
@@ -1,6 +1,6 @@
-dibi extension method example
+Using Extension Methods | dibi
-dibi fetch example
+Fetching Examples | dibi
fetch()\n";
+$row = dibi::fetch('SELECT title FROM products');
Debug::dump($row); // Chair
// fetch a single value
+echo "fetchSingle() \n";
$value = dibi::fetchSingle('SELECT [title] FROM [products]');
Debug::dump($value); // Chair
// fetch complete result set
+echo "fetchAll() \n";
$all = dibi::fetchAll('SELECT * FROM [products]');
Debug::dump($all);
// fetch complete result set like association array
+echo "fetchAssoc('title') \n";
$res = dibi::query('SELECT * FROM [products]');
$assoc = $res->fetchAssoc('title'); // key
Debug::dump($assoc);
// fetch complete result set like pairs key => value
+echo "fetchPairs('product_id', 'title') \n";
$pairs = $res->fetchPairs('product_id', 'title');
Debug::dump($pairs);
// fetch row by row
+echo "using foreach \n";
foreach ($res as $n => $row) {
Debug::dump($row);
}
// fetch row by row with defined offset
+echo "getIterator(2) \n";
foreach ($res->getIterator(2) as $n => $row) {
Debug::dump($row);
}
// fetch row by row with defined offset and limit
+echo "getIterator(2, 1) \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 "fetchAssoc('customers.name|products.title') \n";
$assoc = $res->fetchAssoc('customers.name|products.title'); // key
Debug::dump($assoc);
+echo "fetchAssoc('customers.name[]products.title') \n";
$assoc = $res->fetchAssoc('customers.name[]products.title'); // key
Debug::dump($assoc);
+echo "fetchAssoc('customers.name->products.title') \n";
$assoc = $res->fetchAssoc('customers.name->products.title'); // key
Debug::dump($assoc);
diff --git a/examples/fluent.test.php b/examples/fluent.test.php
index 9b87bf3e..aa5bc2a6 100644
--- a/examples/fluent.test.php
+++ b/examples/fluent.test.php
@@ -1,6 +1,6 @@
-dibi fluent example
+Using Fluent Syntax | dibi
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();
diff --git a/examples/load-sql-dump.php b/examples/load-sql-dump.php
index 4c61d44c..72a8019b 100644
--- a/examples/load-sql-dump.php
+++ b/examples/load-sql-dump.php
@@ -1,6 +1,6 @@
-dibi import SQL dump example
+Importing SQL Dump from File | dibi
-dibi logger example
+Using Logger | dibi
setFile('data/log.sql');
@@ -34,6 +34,7 @@ try {
}
+// outputs a log file
echo "File data/log.sql: ";
echo '', file_get_contents('data/log.sql'), ' ';
diff --git a/examples/metatypes.php b/examples/metatypes.php
index be9c95cd..62688847 100644
--- a/examples/metatypes.php
+++ b/examples/metatypes.php
@@ -1,6 +1,6 @@
-dibi metatypes example
+Result Set Data Types | dibi
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)
diff --git a/examples/nette-debug.php b/examples/nette-debug.php
index 6984a54e..7ff794d3 100644
--- a/examples/nette-debug.php
+++ b/examples/nette-debug.php
@@ -1,6 +1,6 @@
-Nette\Debug & dibi example
+Nette\Debug & SQL Exceptions | dibi
Dibi can display and log exceptions via Nette\Debug, part of Nette Framework.
@@ -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);
diff --git a/examples/nette-debug2.php b/examples/nette-debug2.php
index 614798d4..dabe46c3 100644
--- a/examples/nette-debug2.php
+++ b/examples/nette-debug2.php
@@ -1,6 +1,6 @@
-Nette\Debug & dibi example 2
+Nette\Debug & Variables | dibi
Dibi can dump variables via Nette\Debug, part of Nette Framework.
@@ -27,6 +27,4 @@ dibi::connect(array(
));
-
-// throws error
Debug::barDump( dibi::fetchAll('SELECT * FROM [customers] WHERE [customer_id] < %i', 38), '[customers]' );
diff --git a/examples/profiler.php b/examples/profiler.php
index af597788..cff06129 100644
--- a/examples/profiler.php
+++ b/examples/profiler.php
@@ -1,7 +1,8 @@
+
-Dibi profiler example
+Using Profiler | dibi
Last query:
diff --git a/examples/sql-builder.php b/examples/sql-builder.php
index dc483c2c..a21a1584 100644
--- a/examples/sql-builder.php
+++ b/examples/sql-builder.php
@@ -1,6 +1,6 @@
-dibi SQL builder example
+Query Language Basic Examples | dibi
', 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'
+
+
diff --git a/examples/sql-condition.php b/examples/sql-condition.php
index 4276bb8d..5c0a3412 100644
--- a/examples/sql-condition.php
+++ b/examples/sql-condition.php
@@ -1,6 +1,6 @@
-dibi conditional SQL example
+Query Language & Conditions | dibi
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
\ No newline at end of file
+// -> SELECT * FROM [customers] WHERE LIMIT 10
diff --git a/examples/substitutions.php b/examples/substitutions.php
index 05dc1324..47f0e893 100644
--- a/examples/substitutions.php
+++ b/examples/substitutions.php
@@ -1,6 +1,6 @@
-dibi prefix & substitute example
+Using Substitutions | dibi
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
diff --git a/examples/transaction.php b/examples/transaction.php
index ac653351..c93898c2 100644
--- a/examples/transaction.php
+++ b/examples/transaction.php
@@ -1,6 +1,6 @@
-dibi transaction example
+Using Transactions | dibi
Before:\n";
+echo "Before \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 "After INSERT \n";
+dibi::query('SELECT * FROM [products]')->dump();
+
+
dibi::rollback(); // or dibi::commit();
-
-
-echo "After: \n";
+echo "After rollback \n";
dibi::query('SELECT * FROM [products]')->dump();
-// -> 3 rows
+// -> 3 rows again