mirror of
https://github.com/dg/dibi.git
synced 2025-08-06 06:07:39 +02:00
examples: better headings and comments
This commit is contained in:
@@ -1,6 +1,6 @@
|
|||||||
<!DOCTYPE html><link rel="stylesheet" href="data/style.css">
|
<!DOCTYPE html><link rel="stylesheet" href="data/style.css">
|
||||||
|
|
||||||
<h1>dibi apply limit/offset example</h1>
|
<h1>Using Limit & Offset | dibi</h1>
|
||||||
|
|
||||||
<?php
|
<?php
|
||||||
|
|
||||||
|
@@ -1,6 +1,6 @@
|
|||||||
<!DOCTYPE html><link rel="stylesheet" href="data/style.css">
|
<!DOCTYPE html><link rel="stylesheet" href="data/style.css">
|
||||||
|
|
||||||
<h1>dibi::connect() example</h1>
|
<h1>Connecting to Databases | dibi</h1>
|
||||||
|
|
||||||
<?php
|
<?php
|
||||||
|
|
||||||
@@ -8,7 +8,7 @@ require_once 'Nette/Debug.php';
|
|||||||
require_once '../dibi/dibi.php';
|
require_once '../dibi/dibi.php';
|
||||||
|
|
||||||
|
|
||||||
// connects to SQlite
|
// connects to SQlite using dibi class
|
||||||
echo '<p>Connecting to Sqlite: ';
|
echo '<p>Connecting to Sqlite: ';
|
||||||
try {
|
try {
|
||||||
dibi::connect(array(
|
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
|
// connects to MySQL using DSN
|
||||||
echo '<p>Connecting to MySQL: ';
|
echo '<p>Connecting to MySQL: ';
|
||||||
try {
|
try {
|
||||||
@@ -40,7 +57,7 @@ echo "</p>\n";
|
|||||||
|
|
||||||
|
|
||||||
// connects to MySQLi using array
|
// connects to MySQLi using array
|
||||||
echo '<p>Connecting to MySQL: ';
|
echo '<p>Connecting to MySQLi: ';
|
||||||
try {
|
try {
|
||||||
dibi::connect(array(
|
dibi::connect(array(
|
||||||
'driver' => 'mysqli',
|
'driver' => 'mysqli',
|
||||||
|
@@ -1,6 +1,6 @@
|
|||||||
<!DOCTYPE html><link rel="stylesheet" href="data/style.css">
|
<!DOCTYPE html><link rel="stylesheet" href="data/style.css">
|
||||||
|
|
||||||
<h1>dibi dump example</h1>
|
<h1>Dumping SQL and Result Set | dibi</h1>
|
||||||
|
|
||||||
<?php
|
<?php
|
||||||
|
|
||||||
@@ -16,9 +16,9 @@ dibi::connect(array(
|
|||||||
|
|
||||||
|
|
||||||
$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])
|
||||||
');
|
');
|
||||||
|
|
||||||
|
|
||||||
@@ -26,11 +26,9 @@ 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]
|
|
||||||
|
@@ -1,6 +1,6 @@
|
|||||||
<!DOCTYPE html><link rel="stylesheet" href="data/style.css">
|
<!DOCTYPE html><link rel="stylesheet" href="data/style.css">
|
||||||
|
|
||||||
<h1>dibi extension method example</h1>
|
<h1>Using Extension Methods | dibi</h1>
|
||||||
|
|
||||||
<?php
|
<?php
|
||||||
|
|
||||||
|
@@ -1,6 +1,6 @@
|
|||||||
<!DOCTYPE html><link rel="stylesheet" href="data/style.css">
|
<!DOCTYPE html><link rel="stylesheet" href="data/style.css">
|
||||||
|
|
||||||
<h1>dibi fetch example</h1>
|
<h1>Fetching Examples | dibi</h1>
|
||||||
|
|
||||||
<?php
|
<?php
|
||||||
|
|
||||||
@@ -27,43 +27,51 @@ product_id | title
|
|||||||
|
|
||||||
|
|
||||||
// fetch a single row
|
// 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
|
Debug::dump($row); // Chair
|
||||||
|
|
||||||
|
|
||||||
// fetch a single value
|
// fetch a single value
|
||||||
|
echo "<h2>fetchSingle()</h2>\n";
|
||||||
$value = dibi::fetchSingle('SELECT [title] FROM [products]');
|
$value = dibi::fetchSingle('SELECT [title] FROM [products]');
|
||||||
Debug::dump($value); // Chair
|
Debug::dump($value); // Chair
|
||||||
|
|
||||||
|
|
||||||
// fetch complete result set
|
// fetch complete result set
|
||||||
|
echo "<h2>fetchAll()</h2>\n";
|
||||||
$all = dibi::fetchAll('SELECT * FROM [products]');
|
$all = dibi::fetchAll('SELECT * FROM [products]');
|
||||||
Debug::dump($all);
|
Debug::dump($all);
|
||||||
|
|
||||||
|
|
||||||
// fetch complete result set like association array
|
// fetch complete result set like association array
|
||||||
|
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
|
||||||
Debug::dump($assoc);
|
Debug::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";
|
||||||
$pairs = $res->fetchPairs('product_id', 'title');
|
$pairs = $res->fetchPairs('product_id', 'title');
|
||||||
Debug::dump($pairs);
|
Debug::dump($pairs);
|
||||||
|
|
||||||
|
|
||||||
// fetch row by row
|
// fetch row by row
|
||||||
|
echo "<h2>using foreach</h2>\n";
|
||||||
foreach ($res as $n => $row) {
|
foreach ($res as $n => $row) {
|
||||||
Debug::dump($row);
|
Debug::dump($row);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
// fetch row by row with defined offset
|
// fetch row by row with defined offset
|
||||||
|
echo "<h2>getIterator(2)</h2>\n";
|
||||||
foreach ($res->getIterator(2) as $n => $row) {
|
foreach ($res->getIterator(2) as $n => $row) {
|
||||||
Debug::dump($row);
|
Debug::dump($row);
|
||||||
}
|
}
|
||||||
|
|
||||||
// fetch row by row with defined offset and limit
|
// fetch row by row with defined offset and limit
|
||||||
|
echo "<h2>getIterator(2, 1)</h2>\n";
|
||||||
foreach ($res->getIterator(2, 1) as $n => $row) {
|
foreach ($res->getIterator(2, 1) as $n => $row) {
|
||||||
Debug::dump($row);
|
Debug::dump($row);
|
||||||
}
|
}
|
||||||
@@ -71,17 +79,20 @@ foreach ($res->getIterator(2, 1) as $n => $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])
|
||||||
INNER JOIN [customers] USING ([customer_id])
|
INNER JOIN [customers] USING ([customer_id])
|
||||||
');
|
');
|
||||||
|
|
||||||
|
echo "<h2>fetchAssoc('customers.name|products.title')</h2>\n";
|
||||||
$assoc = $res->fetchAssoc('customers.name|products.title'); // key
|
$assoc = $res->fetchAssoc('customers.name|products.title'); // key
|
||||||
Debug::dump($assoc);
|
Debug::dump($assoc);
|
||||||
|
|
||||||
|
echo "<h2>fetchAssoc('customers.name[]products.title')</h2>\n";
|
||||||
$assoc = $res->fetchAssoc('customers.name[]products.title'); // key
|
$assoc = $res->fetchAssoc('customers.name[]products.title'); // key
|
||||||
Debug::dump($assoc);
|
Debug::dump($assoc);
|
||||||
|
|
||||||
|
echo "<h2>fetchAssoc('customers.name->products.title')</h2>\n";
|
||||||
$assoc = $res->fetchAssoc('customers.name->products.title'); // key
|
$assoc = $res->fetchAssoc('customers.name->products.title'); // key
|
||||||
Debug::dump($assoc);
|
Debug::dump($assoc);
|
||||||
|
@@ -1,6 +1,6 @@
|
|||||||
<!DOCTYPE html><link rel="stylesheet" href="data/style.css">
|
<!DOCTYPE html><link rel="stylesheet" href="data/style.css">
|
||||||
|
|
||||||
<h1>dibi fluent example</h1>
|
<h1>Using Fluent Syntax | dibi</h1>
|
||||||
|
|
||||||
<?php
|
<?php
|
||||||
|
|
||||||
@@ -36,8 +36,6 @@ dibi::select('product_id')->as('id')
|
|||||||
|
|
||||||
|
|
||||||
|
|
||||||
echo "\n";
|
|
||||||
|
|
||||||
// SELECT ...
|
// SELECT ...
|
||||||
echo dibi::select('title')->as('id')
|
echo dibi::select('title')->as('id')
|
||||||
->from('products')
|
->from('products')
|
||||||
@@ -46,8 +44,6 @@ echo dibi::select('title')->as('id')
|
|||||||
|
|
||||||
|
|
||||||
|
|
||||||
echo "\n";
|
|
||||||
|
|
||||||
// INSERT ...
|
// INSERT ...
|
||||||
dibi::insert('products', $record)
|
dibi::insert('products', $record)
|
||||||
->setFlag('IGNORE')
|
->setFlag('IGNORE')
|
||||||
@@ -56,8 +52,6 @@ dibi::insert('products', $record)
|
|||||||
|
|
||||||
|
|
||||||
|
|
||||||
echo "\n";
|
|
||||||
|
|
||||||
// UPDATE ...
|
// UPDATE ...
|
||||||
dibi::update('products', $record)
|
dibi::update('products', $record)
|
||||||
->where('product_id = %d', $id)
|
->where('product_id = %d', $id)
|
||||||
@@ -66,8 +60,6 @@ dibi::update('products', $record)
|
|||||||
|
|
||||||
|
|
||||||
|
|
||||||
echo "\n";
|
|
||||||
|
|
||||||
// DELETE ...
|
// DELETE ...
|
||||||
dibi::delete('products')
|
dibi::delete('products')
|
||||||
->where('product_id = %d', $id)
|
->where('product_id = %d', $id)
|
||||||
@@ -76,8 +68,6 @@ dibi::delete('products')
|
|||||||
|
|
||||||
|
|
||||||
|
|
||||||
echo "\n";
|
|
||||||
|
|
||||||
// custom commands
|
// custom commands
|
||||||
dibi::command()
|
dibi::command()
|
||||||
->update('products')
|
->update('products')
|
||||||
@@ -88,8 +78,6 @@ dibi::command()
|
|||||||
|
|
||||||
|
|
||||||
|
|
||||||
echo "\n";
|
|
||||||
|
|
||||||
dibi::command()
|
dibi::command()
|
||||||
->truncate('products')
|
->truncate('products')
|
||||||
->test();
|
->test();
|
||||||
|
@@ -1,6 +1,6 @@
|
|||||||
<!DOCTYPE html><link rel="stylesheet" href="data/style.css">
|
<!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
|
<?php
|
||||||
|
|
||||||
|
@@ -1,6 +1,6 @@
|
|||||||
<!DOCTYPE html><link rel="stylesheet" href="data/style.css">
|
<!DOCTYPE html><link rel="stylesheet" href="data/style.css">
|
||||||
|
|
||||||
<h1>dibi logger example</h1>
|
<h1>Using Logger | dibi</h1>
|
||||||
|
|
||||||
<?php
|
<?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');
|
dibi::getProfiler()->setFile('data/log.sql');
|
||||||
|
|
||||||
|
|
||||||
@@ -34,6 +34,7 @@ try {
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
// outputs a log file
|
||||||
echo "<h2>File data/log.sql:</h2>";
|
echo "<h2>File data/log.sql:</h2>";
|
||||||
|
|
||||||
echo '<pre>', file_get_contents('data/log.sql'), '</pre>';
|
echo '<pre>', file_get_contents('data/log.sql'), '</pre>';
|
||||||
|
@@ -1,6 +1,6 @@
|
|||||||
<!DOCTYPE html><link rel="stylesheet" href="data/style.css">
|
<!DOCTYPE html><link rel="stylesheet" href="data/style.css">
|
||||||
|
|
||||||
<h1>dibi metatypes example</h1>
|
<h1>Result Set Data Types | dibi</h1>
|
||||||
|
|
||||||
<?php
|
<?php
|
||||||
|
|
||||||
@@ -16,14 +16,28 @@ dibi::connect(array(
|
|||||||
));
|
));
|
||||||
|
|
||||||
|
|
||||||
|
// using manual hints
|
||||||
$res = dibi::query('SELECT * FROM [customers]');
|
$res = dibi::query('SELECT * FROM [customers]');
|
||||||
|
|
||||||
// auto-converts this column to integer
|
$res->setType('customer_id', Dibi::INTEGER)
|
||||||
$res->setType('customer_id', Dibi::INTEGER);
|
->setType('added', Dibi::DATETIME, 'H:i j.n.Y');
|
||||||
$res->setType('added', Dibi::DATETIME, 'H:i j.n.Y');
|
|
||||||
|
|
||||||
$row = $res->fetch();
|
Debug::dump( $res->fetch() );
|
||||||
Debug::dump($row);
|
// 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:
|
// outputs:
|
||||||
// object(DibiRow)#3 (3) {
|
// object(DibiRow)#3 (3) {
|
||||||
// customer_id => int(1)
|
// customer_id => int(1)
|
||||||
|
@@ -1,6 +1,6 @@
|
|||||||
<!DOCTYPE html><link rel="stylesheet" href="data/style.css">
|
<!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>
|
<p>Dibi can display and log exceptions via Nette\Debug, part of Nette Framework.</p>
|
||||||
|
|
||||||
@@ -27,6 +27,5 @@ dibi::connect(array(
|
|||||||
));
|
));
|
||||||
|
|
||||||
|
|
||||||
|
// throws error because SQL is bad
|
||||||
// throws error
|
|
||||||
dibi::query('SELECT FROM [customers] WHERE [customer_id] < %i', 38);
|
dibi::query('SELECT FROM [customers] WHERE [customer_id] < %i', 38);
|
||||||
|
@@ -1,6 +1,6 @@
|
|||||||
<!DOCTYPE html><link rel="stylesheet" href="data/style.css">
|
<!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>
|
<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]' );
|
Debug::barDump( dibi::fetchAll('SELECT * FROM [customers] WHERE [customer_id] < %i', 38), '[customers]' );
|
||||||
|
@@ -1,7 +1,8 @@
|
|||||||
<?php ob_start(1) // needed by FirePHP ?>
|
<?php ob_start(1) // needed by FirePHP ?>
|
||||||
|
|
||||||
<!DOCTYPE html><link rel="stylesheet" href="data/style.css">
|
<!DOCTYPE html><link rel="stylesheet" href="data/style.css">
|
||||||
|
|
||||||
<h1>Dibi profiler example</h1>
|
<h1>Using Profiler | dibi</h1>
|
||||||
|
|
||||||
<?php
|
<?php
|
||||||
|
|
||||||
@@ -16,10 +17,12 @@ dibi::connect(array(
|
|||||||
));
|
));
|
||||||
|
|
||||||
|
|
||||||
|
// 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', $i);
|
$res = dibi::query('SELECT * FROM [customers] WHERE [customer_id] < %i', $i);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// display output
|
||||||
?>
|
?>
|
||||||
<p>Last query: <strong><?php echo dibi::$sql; ?></strong></p>
|
<p>Last query: <strong><?php echo dibi::$sql; ?></strong></p>
|
||||||
|
|
||||||
|
@@ -1,6 +1,6 @@
|
|||||||
<!DOCTYPE html><link rel="stylesheet" href="data/style.css">
|
<!DOCTYPE html><link rel="stylesheet" href="data/style.css">
|
||||||
|
|
||||||
<h1>dibi SQL builder example</h1>
|
<h1>Query Language Basic Examples | dibi</h1>
|
||||||
|
|
||||||
<?php
|
<?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 detects INSERT or REPLACE command
|
||||||
dibi::test('
|
dibi::test('
|
||||||
REPLACE INTO [products]', array(
|
REPLACE INTO [products]', array(
|
||||||
'title' => 'Super product',
|
'title' => 'Super product',
|
||||||
'price' => 318,
|
'price' => 318,
|
||||||
'active' => TRUE,
|
'active' => TRUE,
|
||||||
));
|
));
|
||||||
// -> REPLACE INTO [products] ([title], [price], [active]) VALUES ('Super product', 318, 1)
|
// -> REPLACE INTO [products] ([title], [price], [active]) VALUES ('Super product', 318, 1)
|
||||||
@@ -29,9 +43,9 @@ dibi::test('
|
|||||||
|
|
||||||
// multiple INSERT command
|
// multiple INSERT command
|
||||||
$array = array(
|
$array = array(
|
||||||
'title' => 'Super Product',
|
'title' => 'Super Product',
|
||||||
'price' => 12,
|
'price' => 12,
|
||||||
'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);
|
||||||
@@ -50,21 +64,7 @@ dibi::test("
|
|||||||
|
|
||||||
|
|
||||||
|
|
||||||
// SELECT
|
// modifier applied to array
|
||||||
$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
|
|
||||||
$array = array(1, 2, 3);
|
$array = array(1, 2, 3);
|
||||||
dibi::test("
|
dibi::test("
|
||||||
SELECT *
|
SELECT *
|
||||||
@@ -75,14 +75,22 @@ dibi::test("
|
|||||||
|
|
||||||
|
|
||||||
|
|
||||||
// ORDER BY array
|
// modifier %by for ORDER BY
|
||||||
$order = array(
|
$order = array(
|
||||||
'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, "
|
||||||
");
|
");
|
||||||
// -> SELECT * FROM [people] ORDER BY [field1] ASC, [field2] DESC
|
// -> 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'
|
||||||
|
|
||||||
|
|
||||||
|
@@ -1,6 +1,6 @@
|
|||||||
<!DOCTYPE html><link rel="stylesheet" href="data/style.css">
|
<!DOCTYPE html><link rel="stylesheet" href="data/style.css">
|
||||||
|
|
||||||
<h1>dibi conditional SQL example</h1>
|
<h1>Query Language & Conditions | dibi</h1>
|
||||||
|
|
||||||
<?php
|
<?php
|
||||||
|
|
||||||
@@ -14,19 +14,20 @@ dibi::connect(array(
|
|||||||
));
|
));
|
||||||
|
|
||||||
|
|
||||||
|
// some variables
|
||||||
$cond1 = TRUE;
|
$cond1 = TRUE;
|
||||||
$cond2 = FALSE;
|
$cond2 = FALSE;
|
||||||
$foo = -1;
|
$foo = -1;
|
||||||
$bar = 2;
|
$bar = 2;
|
||||||
|
|
||||||
|
// conditional variable
|
||||||
$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 %s', $name, '%end'
|
%if', isset($name), 'WHERE [name] LIKE %s', $name, '%end'
|
||||||
);
|
);
|
||||||
// -> SELECT * FROM [customers] WHERE [name] LIKE 'K%'
|
// -> SELECT * FROM [customers] WHERE [name] LIKE 'K%'
|
||||||
|
|
||||||
@@ -35,11 +36,11 @@ FROM [customers]
|
|||||||
|
|
||||||
// if & else & (optional) end
|
// if & else & (optional) end
|
||||||
dibi::test("
|
dibi::test("
|
||||||
SELECT *
|
SELECT *
|
||||||
FROM [people]
|
FROM [people]
|
||||||
WHERE [id] > 0
|
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
|
// -> SELECT * FROM [people] WHERE [id] > 0 AND [bar]=2
|
||||||
|
|
||||||
@@ -47,11 +48,11 @@ WHERE [id] > 0
|
|||||||
|
|
||||||
// nested condition
|
// nested condition
|
||||||
dibi::test('
|
dibi::test('
|
||||||
SELECT *
|
SELECT *
|
||||||
FROM [customers]
|
FROM [customers]
|
||||||
WHERE
|
WHERE
|
||||||
%if', isset($name), '[name] LIKE %s', $name, '
|
%if', isset($name), '[name] LIKE %s', $name, '
|
||||||
%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
|
// -> SELECT * FROM [customers] WHERE LIMIT 10
|
||||||
|
@@ -1,6 +1,6 @@
|
|||||||
<!DOCTYPE html><link rel="stylesheet" href="data/style.css">
|
<!DOCTYPE html><link rel="stylesheet" href="data/style.css">
|
||||||
|
|
||||||
<h1>dibi prefix & substitute example</h1>
|
<h1>Using Substitutions | dibi</h1>
|
||||||
|
|
||||||
<?php
|
<?php
|
||||||
|
|
||||||
@@ -19,24 +19,34 @@ dibi::connect(array(
|
|||||||
// create new substitution :blog: ==> wp_
|
// create new substitution :blog: ==> wp_
|
||||||
dibi::addSubst('blog', 'wp_');
|
dibi::addSubst('blog', 'wp_');
|
||||||
|
|
||||||
dibi::test("UPDATE :blog:items SET [text]='Hello World'");
|
dibi::test("SELECT * FROM [:blog:items]");
|
||||||
// -> UPDATE wp_items SET [text]='Hello World'
|
// -> SELECT * FROM [wp_items]
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
// create substitution fallback
|
// create substitutions using fallback callback
|
||||||
function substFallBack($expr)
|
function substFallBack($expr)
|
||||||
{
|
{
|
||||||
if (defined($expr)) {
|
$const = 'SUBST_' . strtoupper($expr);
|
||||||
return constant($expr);
|
if (defined($const)) {
|
||||||
|
return constant($const);
|
||||||
} else {
|
} else {
|
||||||
return 'the_' . $expr;
|
throw new Exception("Undefined substitution :$expr:");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// define callback
|
||||||
dibi::setSubstFallBack('substFallBack');
|
dibi::setSubstFallBack('substFallBack');
|
||||||
|
|
||||||
dibi::test("UPDATE [:account:user] SET [name]='John Doe', [active]=:true:");
|
// define substitutes as constants
|
||||||
// -> UPDATE [the_accountuser] SET [name]='John Doe', [active]=1
|
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
|
||||||
|
@@ -1,6 +1,6 @@
|
|||||||
<!DOCTYPE html><link rel="stylesheet" href="data/style.css">
|
<!DOCTYPE html><link rel="stylesheet" href="data/style.css">
|
||||||
|
|
||||||
<h1>dibi transaction example</h1>
|
<h1>Using Transactions | dibi</h1>
|
||||||
|
|
||||||
<?php
|
<?php
|
||||||
|
|
||||||
@@ -14,7 +14,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
|
// -> 3 rows
|
||||||
|
|
||||||
@@ -23,10 +23,13 @@ dibi::begin();
|
|||||||
dibi::query('INSERT INTO [products]', array(
|
dibi::query('INSERT INTO [products]', array(
|
||||||
'title' => 'Test product',
|
'title' => 'Test product',
|
||||||
));
|
));
|
||||||
|
|
||||||
|
echo "<h2>After INSERT</h2>\n";
|
||||||
|
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:</h2>\n";
|
|
||||||
dibi::query('SELECT * FROM [products]')->dump();
|
dibi::query('SELECT * FROM [products]')->dump();
|
||||||
// -> 3 rows
|
// -> 3 rows again
|
||||||
|
Reference in New Issue
Block a user