1
0
mirror of https://github.com/dg/dibi.git synced 2025-02-24 02:43:09 +01:00
php-dibi/tests/dibi/Translator.conditions.phpt

100 lines
1.4 KiB
Plaintext
Raw Normal View History

2015-01-12 05:33:41 +01:00
<?php
/**
* @dataProvider ../databases.ini
*/
2017-06-09 22:20:47 +02:00
declare(strict_types=1);
2015-01-12 05:33:41 +01:00
use Tester\Assert;
require __DIR__ . '/bootstrap.php';
$conn = new Dibi\Connection($config);
2015-01-12 05:33:41 +01:00
// if & end
Assert::same(
2015-06-19 03:11:36 +02:00
reformat('
2015-01-12 05:33:41 +01:00
SELECT *
FROM [customers]
2015-06-19 03:11:36 +02:00
/* WHERE ... LIKE ... */'),
2015-01-12 05:33:41 +01:00
$conn->translate('
SELECT *
FROM [customers]
%if', isset($name), 'WHERE [name] LIKE %s', 'xxx', '%end'
));
// if & else & end (last end is optional)
Assert::same(
2015-06-19 03:11:36 +02:00
reformat('
2015-01-12 05:33:41 +01:00
SELECT *
2015-06-19 03:11:36 +02:00
FROM [customers] /* ... */'),
2015-01-12 05:33:41 +01:00
$conn->translate('
SELECT *
FROM %if', true, '[customers] %else [products]'
2015-01-12 05:33:41 +01:00
));
// if & else & (optional) end
Assert::match(
2015-06-19 03:11:36 +02:00
reformat('
2015-01-12 05:33:41 +01:00
SELECT *
FROM [people]
WHERE [id] > 0
/* AND ...=...
*/ AND [bar]=1
2015-06-19 03:11:36 +02:00
'),
2015-01-12 05:33:41 +01:00
2015-06-19 03:11:36 +02:00
$conn->translate('
2015-01-12 05:33:41 +01:00
SELECT *
FROM [people]
WHERE [id] > 0
%if', false, 'AND [foo]=%i', 1, '
%else %if', true, 'AND [bar]=%i', 1, '
2015-06-19 03:11:36 +02:00
'));
2015-01-12 05:33:41 +01:00
// nested condition
Assert::match(
reformat([
'sqlsrv' => "
SELECT *
FROM [customers]
WHERE
[name] LIKE N'xxx'
/* AND ...=1 */
/* 1 LIMIT 10 */",
"
2015-01-12 05:33:41 +01:00
SELECT *
FROM [customers]
WHERE
[name] LIKE 'xxx'
/* AND ...=1 */
/* 1 LIMIT 10 */",
]),
2015-01-12 05:33:41 +01:00
$conn->translate('
SELECT *
FROM [customers]
WHERE
%if', true, '[name] LIKE %s', 'xxx', '
%if', false, 'AND [admin]=1 %end
2015-01-12 05:33:41 +01:00
%else 1 LIMIT 10 %end'
));
// limit & offset
Assert::same(
'SELECT * FROM foo /* (limit 3) (offset 5) */',
$conn->translate(
'SELECT * FROM foo',
'%if', false,
'%lmt', 3,
'%ofs', 5,
'%end'
));