2010-08-03 12:28:07 +02:00
|
|
|
<!DOCTYPE html><link rel="stylesheet" href="data/style.css">
|
|
|
|
|
2010-08-03 11:48:51 +02:00
|
|
|
<h1>Query Language & Conditions | dibi</h1>
|
2010-08-03 12:28:07 +02:00
|
|
|
|
2008-07-17 03:51:29 +00:00
|
|
|
<?php
|
|
|
|
|
2011-07-01 08:06:36 +02:00
|
|
|
require_once 'Nette/Debugger.php';
|
2008-07-17 03:51:29 +00:00
|
|
|
require_once '../dibi/dibi.php';
|
|
|
|
|
|
|
|
|
|
|
|
dibi::connect(array(
|
|
|
|
'driver' => 'sqlite',
|
2010-08-03 12:11:14 +02:00
|
|
|
'database' => 'data/sample.sdb',
|
2008-07-17 03:51:29 +00:00
|
|
|
));
|
|
|
|
|
|
|
|
|
2010-08-03 11:48:51 +02:00
|
|
|
// some variables
|
2008-10-28 02:06:55 +00:00
|
|
|
$cond1 = TRUE;
|
|
|
|
$cond2 = FALSE;
|
2008-07-17 03:51:29 +00:00
|
|
|
$foo = -1;
|
|
|
|
$bar = 2;
|
|
|
|
|
2010-08-03 11:48:51 +02:00
|
|
|
// conditional variable
|
2008-07-17 03:51:29 +00:00
|
|
|
$name = $cond1 ? 'K%' : NULL;
|
|
|
|
|
|
|
|
// if & end
|
|
|
|
dibi::test('
|
2010-08-03 11:48:51 +02:00
|
|
|
SELECT *
|
2010-08-03 17:12:34 +02:00
|
|
|
FROM customers
|
|
|
|
%if', isset($name), 'WHERE name LIKE %s', $name, '%end'
|
2008-07-17 03:51:29 +00:00
|
|
|
);
|
2010-08-03 17:12:34 +02:00
|
|
|
// -> SELECT * FROM customers WHERE name LIKE 'K%'
|
2008-10-28 02:06:55 +00:00
|
|
|
|
|
|
|
|
2008-07-17 03:51:29 +00:00
|
|
|
|
|
|
|
|
|
|
|
// if & else & (optional) end
|
|
|
|
dibi::test("
|
2010-08-03 11:48:51 +02:00
|
|
|
SELECT *
|
2010-08-03 17:12:34 +02:00
|
|
|
FROM people
|
|
|
|
WHERE id > 0
|
|
|
|
%if", ($foo > 0), "AND foo=%i", $foo, "
|
|
|
|
%else %if", ($bar > 0), "AND bar=%i", $bar, "
|
2008-07-17 03:51:29 +00:00
|
|
|
");
|
2010-08-03 17:12:34 +02:00
|
|
|
// -> SELECT * FROM people WHERE id > 0 AND bar=2
|
2008-10-28 02:06:55 +00:00
|
|
|
|
2008-07-17 03:51:29 +00:00
|
|
|
|
|
|
|
|
|
|
|
// nested condition
|
|
|
|
dibi::test('
|
2010-08-03 11:48:51 +02:00
|
|
|
SELECT *
|
2010-08-03 17:12:34 +02:00
|
|
|
FROM customers
|
2010-08-03 11:48:51 +02:00
|
|
|
WHERE
|
2010-08-03 17:12:34 +02:00
|
|
|
%if', isset($name), 'name LIKE %s', $name, '
|
|
|
|
%if', $cond2, 'AND admin=1 %end
|
2010-08-03 11:48:51 +02:00
|
|
|
%else 1 LIMIT 10 %end'
|
2008-07-17 03:51:29 +00:00
|
|
|
);
|
2010-08-03 17:12:34 +02:00
|
|
|
// -> SELECT * FROM customers WHERE LIMIT 10
|