1
0
mirror of https://github.com/dg/dibi.git synced 2025-02-23 10:26:21 +01:00
php-dibi/tests/dibi/Translator.conditions.phpt
Tomáš Kuthan 7f22279333 SqlsrvDriver: Correct escaping of special characters (N prefix) (#332)
In case nvarchar type is used and e.g. chinese have to be saved, there have to be N in front of the value, this escaping works fine for varchar columns as well.
2019-08-30 18:54:06 +02:00

100 lines
1.4 KiB
PHP

<?php
/**
* @dataProvider ../databases.ini
*/
declare(strict_types=1);
use Tester\Assert;
require __DIR__ . '/bootstrap.php';
$conn = new Dibi\Connection($config);
// if & end
Assert::same(
reformat('
SELECT *
FROM [customers]
/* WHERE ... LIKE ... */'),
$conn->translate('
SELECT *
FROM [customers]
%if', isset($name), 'WHERE [name] LIKE %s', 'xxx', '%end'
));
// if & else & end (last end is optional)
Assert::same(
reformat('
SELECT *
FROM [customers] /* ... */'),
$conn->translate('
SELECT *
FROM %if', true, '[customers] %else [products]'
));
// if & else & (optional) end
Assert::match(
reformat('
SELECT *
FROM [people]
WHERE [id] > 0
/* AND ...=...
*/ AND [bar]=1
'),
$conn->translate('
SELECT *
FROM [people]
WHERE [id] > 0
%if', false, 'AND [foo]=%i', 1, '
%else %if', true, 'AND [bar]=%i', 1, '
'));
// nested condition
Assert::match(
reformat([
'sqlsrv' => "
SELECT *
FROM [customers]
WHERE
[name] LIKE N'xxx'
/* AND ...=1 */
/* 1 LIMIT 10 */",
"
SELECT *
FROM [customers]
WHERE
[name] LIKE 'xxx'
/* AND ...=1 */
/* 1 LIMIT 10 */",
]),
$conn->translate('
SELECT *
FROM [customers]
WHERE
%if', true, '[name] LIKE %s', 'xxx', '
%if', false, 'AND [admin]=1 %end
%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'
));