mirror of
https://github.com/dg/dibi.git
synced 2025-08-06 06:07:39 +02:00
added new tests
This commit is contained in:
159
tests/dibi/DataSource.phpt
Normal file
159
tests/dibi/DataSource.phpt
Normal file
@@ -0,0 +1,159 @@
|
||||
<?php
|
||||
|
||||
use Tester\Assert;
|
||||
|
||||
require __DIR__ . '/bootstrap.php';
|
||||
|
||||
|
||||
$conn = new DibiConnection($config);
|
||||
$conn->loadFile(__DIR__ . "/data/$config[system].sql");
|
||||
|
||||
|
||||
$ds = $conn->dataSource('SELECT * FROM products');
|
||||
Assert::match(
|
||||
reformat("
|
||||
SELECT *
|
||||
FROM (SELECT * FROM products) t"),
|
||||
(string) $ds
|
||||
);
|
||||
|
||||
|
||||
Assert::same(3, $ds->count());
|
||||
Assert::same(3, $ds->getTotalCount());
|
||||
Assert::same(
|
||||
reformat('SELECT COUNT(*) FROM (SELECT * FROM products) t'),
|
||||
dibi::$sql
|
||||
);
|
||||
|
||||
|
||||
$ds->select('title');
|
||||
$ds->orderBy('title', dibi::DESC);
|
||||
$ds->where('title like "%a%"');
|
||||
Assert::match(
|
||||
reformat("
|
||||
SELECT [title]
|
||||
FROM (SELECT * FROM products) t
|
||||
WHERE (title like '%a%')
|
||||
ORDER BY [title] DESC
|
||||
"),
|
||||
(string) $ds
|
||||
);
|
||||
|
||||
|
||||
$ds->select('product_id');
|
||||
$ds->orderBy('product_id', dibi::ASC);
|
||||
$ds->where('product_id = %i', 1);
|
||||
Assert::match(
|
||||
reformat("
|
||||
SELECT [title], [product_id]
|
||||
FROM (SELECT * FROM products) t
|
||||
WHERE (title like '%a%') AND (product_id = 1)
|
||||
ORDER BY [title] DESC, [product_id] ASC
|
||||
"),
|
||||
(string) $ds
|
||||
);
|
||||
|
||||
|
||||
$ds->select(array('product_id'));
|
||||
$ds->orderBy(array('product_id' => dibi::ASC));
|
||||
$ds->where(array('product_id = 1'));
|
||||
Assert::match(
|
||||
reformat("
|
||||
SELECT [product_id]
|
||||
FROM (SELECT * FROM products) t
|
||||
WHERE (title like '%a%') AND (product_id = 1) AND (product_id = 1)
|
||||
ORDER BY [product_id] ASC
|
||||
"),
|
||||
(string) $ds
|
||||
);
|
||||
|
||||
|
||||
Assert::same(1, $ds->count());
|
||||
Assert::same(3, $ds->getTotalCount());
|
||||
Assert::match(reformat("SELECT COUNT(*) FROM (
|
||||
SELECT [product_id]
|
||||
FROM (SELECT * FROM products) t
|
||||
WHERE (title like '%a%') AND (product_id = 1) AND (product_id = 1)
|
||||
ORDER BY [product_id] ASC
|
||||
) t"), dibi::$sql);
|
||||
Assert::same(1, $ds->toDataSource()->count());
|
||||
|
||||
|
||||
Assert::equal(array(
|
||||
new DibiRow(array(
|
||||
'product_id' => 1,
|
||||
)),
|
||||
), iterator_to_array($ds));
|
||||
|
||||
Assert::match(
|
||||
reformat("
|
||||
SELECT [product_id]
|
||||
FROM (SELECT * FROM products) t
|
||||
WHERE (title like '%a%') AND (product_id = 1) AND (product_id = 1)
|
||||
ORDER BY [product_id] ASC
|
||||
"),
|
||||
dibi::$sql
|
||||
);
|
||||
|
||||
|
||||
$fluent = $ds->toFluent();
|
||||
Assert::same(1, $fluent->count());
|
||||
Assert::match(
|
||||
reformat("SELECT * FROM (
|
||||
SELECT [product_id]
|
||||
FROM (SELECT * FROM products) t
|
||||
WHERE (title like '%a%') AND (product_id = 1) AND (product_id = 1)
|
||||
ORDER BY [product_id] ASC
|
||||
) t"),
|
||||
(string) $fluent
|
||||
);
|
||||
|
||||
|
||||
$ds = $conn->select('title')->from('products')->toDataSource();
|
||||
Assert::match(
|
||||
reformat("
|
||||
SELECT *
|
||||
FROM (SELECT [title] FROM [products]) t"),
|
||||
(string) $ds
|
||||
);
|
||||
|
||||
Assert::equal(new DibiRow(array(
|
||||
'product_id' => 1,
|
||||
'title' => 'Chair',
|
||||
)), $conn->dataSource('SELECT * FROM products ORDER BY product_id')->fetch());
|
||||
|
||||
Assert::same(1, $conn->dataSource('SELECT * FROM products ORDER BY product_id')->fetchSingle());
|
||||
|
||||
Assert::same(
|
||||
array(1 => 'Chair', 'Table', 'Computer'),
|
||||
$conn->dataSource('SELECT * FROM products ORDER BY product_id')->fetchPairs()
|
||||
);
|
||||
|
||||
Assert::equal(array(
|
||||
1 => new DibiRow(array(
|
||||
'product_id' => 1,
|
||||
'title' => 'Chair',
|
||||
)),
|
||||
new DibiRow(array(
|
||||
'product_id' => 2,
|
||||
'title' => 'Table',
|
||||
)),
|
||||
new DibiRow(array(
|
||||
'product_id' => 3,
|
||||
'title' => 'Computer',
|
||||
)),
|
||||
), $conn->dataSource('SELECT * FROM products ORDER BY product_id')->fetchAssoc('product_id'));
|
||||
|
||||
|
||||
$ds = new DibiDataSource('products', $conn);
|
||||
|
||||
Assert::match(
|
||||
reformat("
|
||||
SELECT *
|
||||
FROM [products]"),
|
||||
(string) $ds
|
||||
);
|
||||
|
||||
Assert::same(3, $ds->count());
|
||||
Assert::same(3, $ds->getTotalCount());
|
||||
Assert::same(reformat('SELECT COUNT(*) FROM [products]'), dibi::$sql);
|
35
tests/dibi/DibiConnection.affectedRows.phpt
Normal file
35
tests/dibi/DibiConnection.affectedRows.phpt
Normal file
@@ -0,0 +1,35 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* @dataProvider ../databases.ini
|
||||
*/
|
||||
|
||||
use Tester\Assert;
|
||||
|
||||
require __DIR__ . '/bootstrap.php';
|
||||
|
||||
$conn = new DibiConnection($config);
|
||||
$conn->loadFile(__DIR__ . "/data/$config[system].sql");
|
||||
|
||||
|
||||
$conn->query('INSERT INTO products', array(
|
||||
'title' => 'Test product',
|
||||
));
|
||||
Assert::same(1, $conn->getAffectedRows());
|
||||
|
||||
|
||||
$conn->query('UPDATE products SET title="xxx" WHERE product_id > 100');
|
||||
Assert::same(0, $conn->getAffectedRows());
|
||||
|
||||
|
||||
$conn->query('UPDATE products SET title="xxx"');
|
||||
Assert::same(4, $conn->getAffectedRows());
|
||||
|
||||
|
||||
$conn->query('DELETE FROM orders');
|
||||
$conn->query('DELETE FROM products WHERE product_id > 100');
|
||||
Assert::same(0, $conn->getAffectedRows());
|
||||
|
||||
|
||||
$conn->query('DELETE FROM products WHERE product_id < 3');
|
||||
Assert::same(2, $conn->getAffectedRows());
|
37
tests/dibi/DibiConnection.connect.phpt
Normal file
37
tests/dibi/DibiConnection.connect.phpt
Normal file
@@ -0,0 +1,37 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* @dataProvider ../databases.ini
|
||||
*/
|
||||
|
||||
use Tester\Assert;
|
||||
|
||||
require __DIR__ . '/bootstrap.php';
|
||||
|
||||
|
||||
test(function() use ($config) {
|
||||
$conn = new DibiConnection($config);
|
||||
Assert::true($conn->isConnected());
|
||||
|
||||
$conn->disconnect();
|
||||
Assert::false($conn->isConnected());
|
||||
});
|
||||
|
||||
|
||||
test(function() use ($config) { // lazy
|
||||
$conn = new DibiConnection($config + array('lazy' => TRUE));
|
||||
Assert::false($conn->isConnected());
|
||||
|
||||
$conn->query('SELECT 1');
|
||||
Assert::true($conn->isConnected());
|
||||
});
|
||||
|
||||
|
||||
test(function() use ($config) { // query string
|
||||
$conn = new DibiConnection(http_build_query($config, NULL, '&'));
|
||||
Assert::true($conn->isConnected());
|
||||
|
||||
Assert::null($conn->getConfig('lazy'));
|
||||
Assert::same($config['driver'], $conn->getConfig('driver'));
|
||||
Assert::type('IDibiDriver', $conn->getDriver());
|
||||
});
|
329
tests/dibi/DibiConnection.fetch.phpt
Normal file
329
tests/dibi/DibiConnection.fetch.phpt
Normal file
@@ -0,0 +1,329 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* @dataProvider ../databases.ini
|
||||
*/
|
||||
|
||||
use Tester\Assert;
|
||||
|
||||
require __DIR__ . '/bootstrap.php';
|
||||
|
||||
$conn = new DibiConnection($config);
|
||||
$conn->loadFile(__DIR__ . "/data/$config[system].sql");
|
||||
|
||||
|
||||
function num($n)
|
||||
{
|
||||
global $config;
|
||||
if (substr(@$config['dsn'], 0, 5) === 'odbc:' || $config['driver'] === 'sqlite') {
|
||||
$n = is_float($n) ? "$n.0" : (string) $n;
|
||||
}
|
||||
return $n;
|
||||
}
|
||||
|
||||
|
||||
// fetch a single value
|
||||
$res = $conn->query('SELECT [title] FROM [products]');
|
||||
Assert::same('Chair', $res->fetchSingle());
|
||||
|
||||
|
||||
// fetch complete result set
|
||||
$res = $conn->query('SELECT * FROM [products] ORDER BY product_id');
|
||||
Assert::equal(array(
|
||||
new DibiRow(array('product_id' => num(1), 'title' => 'Chair')),
|
||||
new DibiRow(array('product_id' => num(2), 'title' => 'Table')),
|
||||
new DibiRow(array('product_id' => num(3), 'title' => 'Computer')),
|
||||
), $res->fetchAll());
|
||||
|
||||
|
||||
// fetch complete result set like pairs key => value
|
||||
$res = $conn->query('SELECT * FROM [products] ORDER BY product_id');
|
||||
Assert::same(
|
||||
array(1 => 'Chair', 'Table', 'Computer'),
|
||||
$res->fetchPairs('product_id', 'title')
|
||||
);
|
||||
|
||||
$res = $conn->query('SELECT * FROM [products] ORDER BY product_id');
|
||||
Assert::same(
|
||||
array(1 => 'Chair', 'Table', 'Computer'),
|
||||
$res->fetchPairs()
|
||||
);
|
||||
|
||||
|
||||
// fetch row by row
|
||||
$res = $conn->query('SELECT * FROM [products] ORDER BY product_id');
|
||||
Assert::equal(array(
|
||||
new DibiRow(array('product_id' => num(1), 'title' => 'Chair')),
|
||||
new DibiRow(array('product_id' => num(2), 'title' => 'Table')),
|
||||
new DibiRow(array('product_id' => num(3), 'title' => 'Computer')),
|
||||
), iterator_to_array($res));
|
||||
|
||||
|
||||
// fetch complete result set like association array
|
||||
$res = $conn->query('SELECT * FROM [products] ORDER BY product_id');
|
||||
Assert::equal(array(
|
||||
'Chair' => new DibiRow(array('product_id' => num(1), 'title' => 'Chair')),
|
||||
'Table' => new DibiRow(array('product_id' => num(2), 'title' => 'Table')),
|
||||
'Computer' => new DibiRow(array('product_id' => num(3), 'title' => 'Computer')),
|
||||
), $res->fetchAssoc('title'));
|
||||
|
||||
|
||||
|
||||
// more complex association array
|
||||
function query($conn) {
|
||||
|
||||
return $conn->query($conn->getConfig('system') === 'odbc' ? '
|
||||
SELECT products.title, customers.name, orders.amount
|
||||
FROM ([products]
|
||||
INNER JOIN [orders] ON [products.product_id] = [orders.product_id])
|
||||
INNER JOIN [customers] ON [orders.customer_id] = [customers.customer_id]
|
||||
ORDER BY orders.order_id
|
||||
' : '
|
||||
SELECT products.title AS title, customers.name AS name, orders.amount AS amount
|
||||
FROM [products]
|
||||
INNER JOIN [orders] USING ([product_id])
|
||||
INNER JOIN [customers] USING ([customer_id])
|
||||
ORDER BY orders.order_id
|
||||
');
|
||||
}
|
||||
|
||||
|
||||
Assert::equal(array(
|
||||
'Arnold Rimmer' => array(
|
||||
'Chair' => new DibiRow(array('title' => 'Chair', 'name' => 'Arnold Rimmer', 'amount' => num(7.0))),
|
||||
'Computer' => new DibiRow(array('title' => 'Computer', 'name' => 'Arnold Rimmer', 'amount' => num(2.0))),
|
||||
),
|
||||
'Dave Lister' => array(
|
||||
'Table' => new DibiRow(array('title' => 'Table', 'name' => 'Dave Lister', 'amount' => num(3.0))),
|
||||
),
|
||||
'Kristine Kochanski' => array(
|
||||
'Computer' => new DibiRow(array('title' => 'Computer', 'name' => 'Kristine Kochanski', 'amount' => num(5.0))),
|
||||
),
|
||||
), query($conn)->fetchAssoc('name,title'));
|
||||
|
||||
|
||||
Assert::equal(array(
|
||||
'Arnold Rimmer' => array(
|
||||
array(
|
||||
'Chair' => new DibiRow(array('title' => 'Chair', 'name' => 'Arnold Rimmer', 'amount' => num(7.0))),
|
||||
),
|
||||
array(
|
||||
'Computer' => new DibiRow(array('title' => 'Computer', 'name' => 'Arnold Rimmer', 'amount' => num(2.0))),
|
||||
),
|
||||
),
|
||||
'Dave Lister' => array(
|
||||
array(
|
||||
'Table' => new DibiRow(array('title' => 'Table', 'name' => 'Dave Lister', 'amount' => num(3.0))),
|
||||
),
|
||||
),
|
||||
'Kristine Kochanski' => array(
|
||||
array(
|
||||
'Computer' => new DibiRow(array('title' => 'Computer', 'name' => 'Kristine Kochanski', 'amount' => num(5.0))),
|
||||
),
|
||||
),
|
||||
), query($conn)->fetchAssoc('name,#,title'));
|
||||
|
||||
|
||||
Assert::equal(array(
|
||||
'Arnold Rimmer' => array(
|
||||
'title' => array(
|
||||
'Chair' => new DibiRow(array('title' => 'Chair', 'name' => 'Arnold Rimmer', 'amount' => num(7.0))),
|
||||
'Computer' => new DibiRow(array('title' => 'Computer', 'name' => 'Arnold Rimmer', 'amount' => num(2.0))),
|
||||
),
|
||||
'name' => 'Arnold Rimmer',
|
||||
'amount' => num(7.0),
|
||||
),
|
||||
'Dave Lister' => array(
|
||||
'title' => array(
|
||||
'Table' => new DibiRow(array('title' => 'Table', 'name' => 'Dave Lister', 'amount' => num(3.0))),
|
||||
),
|
||||
'name' => 'Dave Lister',
|
||||
'amount' => num(3.0),
|
||||
),
|
||||
'Kristine Kochanski' => array(
|
||||
'title' => array(
|
||||
'Computer' => new DibiRow(array('title' => 'Computer', 'name' => 'Kristine Kochanski', 'amount' => num(5.0))),
|
||||
),
|
||||
'name' => 'Kristine Kochanski',
|
||||
'amount' => num(5.0),
|
||||
),
|
||||
), query($conn)->fetchAssoc('name,=,title'));
|
||||
|
||||
|
||||
Assert::equal(array(
|
||||
'Arnold Rimmer' => new DibiRow(array(
|
||||
'title' => array(
|
||||
'Chair' => new DibiRow(array('title' => 'Chair', 'name' => 'Arnold Rimmer', 'amount' => num(7.0))),
|
||||
'Computer' => new DibiRow(array('title' => 'Computer', 'name' => 'Arnold Rimmer', 'amount' => num(2.0))),
|
||||
),
|
||||
'name' => 'Arnold Rimmer',
|
||||
'amount' => num(7.0),
|
||||
)),
|
||||
'Dave Lister' => new DibiRow(array(
|
||||
'title' => array(
|
||||
'Table' => new DibiRow(array('title' => 'Table', 'name' => 'Dave Lister', 'amount' => num(3.0))),
|
||||
),
|
||||
'name' => 'Dave Lister',
|
||||
'amount' => num(3.0),
|
||||
)),
|
||||
'Kristine Kochanski' => new DibiRow(array(
|
||||
'title' => array(
|
||||
'Computer' => new DibiRow(array('title' => 'Computer', 'name' => 'Kristine Kochanski', 'amount' => num(5.0))),
|
||||
),
|
||||
'name' => 'Kristine Kochanski',
|
||||
'amount' => num(5.0),
|
||||
)),
|
||||
), query($conn)->fetchAssoc('name,@,title'));
|
||||
|
||||
|
||||
Assert::equal(array(
|
||||
new DibiRow(array('title' => 'Chair', 'name' => 'Arnold Rimmer', 'amount' => num(7.0))),
|
||||
new DibiRow(array(
|
||||
'title' => 'Computer', 'name' => 'Arnold Rimmer', 'amount' => num(2.0))),
|
||||
new DibiRow(array(
|
||||
'title' => 'Table', 'name' => 'Dave Lister', 'amount' => num(3.0))),
|
||||
new DibiRow(array(
|
||||
'title' => 'Computer', 'name' => 'Kristine Kochanski', 'amount' => num(5.0))),
|
||||
), query($conn)->fetchAssoc('@,='));
|
||||
|
||||
|
||||
Assert::equal(array(
|
||||
'Arnold Rimmer' => array(
|
||||
'title' => array(
|
||||
'Chair' => new DibiRow(array('title' => 'Chair', 'name' => 'Arnold Rimmer', 'amount' => num(7.0))),
|
||||
'Computer' => new DibiRow(array('title' => 'Computer', 'name' => 'Arnold Rimmer', 'amount' => num(2.0))),
|
||||
),
|
||||
'name' => 'Arnold Rimmer',
|
||||
'amount' => num(7.0),
|
||||
),
|
||||
'Dave Lister' => array(
|
||||
'title' => array(
|
||||
'Table' => new DibiRow(array('title' => 'Table', 'name' => 'Dave Lister', 'amount' => num(3.0))),
|
||||
),
|
||||
'name' => 'Dave Lister',
|
||||
'amount' => num(3.0),
|
||||
),
|
||||
'Kristine Kochanski' => array(
|
||||
'title' => array(
|
||||
'Computer' => new DibiRow(array('title' => 'Computer', 'name' => 'Kristine Kochanski', 'amount' => num(5.0))),
|
||||
),
|
||||
'name' => 'Kristine Kochanski',
|
||||
'amount' => num(5.0),
|
||||
),
|
||||
), query($conn)->fetchAssoc('name,=,title,@'));
|
||||
|
||||
|
||||
// old syntax
|
||||
Assert::equal(array(
|
||||
'Arnold Rimmer' => array(
|
||||
'Chair' => new DibiRow(array('title' => 'Chair', 'name' => 'Arnold Rimmer', 'amount' => num(7.0))),
|
||||
'Computer' => new DibiRow(array('title' => 'Computer', 'name' => 'Arnold Rimmer', 'amount' => num(2.0))),
|
||||
),
|
||||
'Dave Lister' => array(
|
||||
'Table' => new DibiRow(array('title' => 'Table', 'name' => 'Dave Lister', 'amount' => num(3.0))),
|
||||
),
|
||||
'Kristine Kochanski' => array(
|
||||
'Computer' => new DibiRow(array('title' => 'Computer', 'name' => 'Kristine Kochanski', 'amount' => num(5.0))),
|
||||
),
|
||||
), query($conn)->fetchAssoc('name|title'));
|
||||
|
||||
|
||||
Assert::equal(array(
|
||||
'Arnold Rimmer' => array(
|
||||
array(
|
||||
'Chair' => new DibiRow(array('title' => 'Chair', 'name' => 'Arnold Rimmer', 'amount' => num(7.0))),
|
||||
),
|
||||
array(
|
||||
'Computer' => new DibiRow(array('title' => 'Computer', 'name' => 'Arnold Rimmer', 'amount' => num(2.0))),
|
||||
),
|
||||
),
|
||||
'Dave Lister' => array(
|
||||
array(
|
||||
'Table' => new DibiRow(array('title' => 'Table', 'name' => 'Dave Lister', 'amount' => num(3.0))),
|
||||
),
|
||||
),
|
||||
'Kristine Kochanski' => array(
|
||||
array(
|
||||
'Computer' => new DibiRow(array('title' => 'Computer', 'name' => 'Kristine Kochanski', 'amount' => num(5.0))),
|
||||
),
|
||||
),
|
||||
), query($conn)->fetchAssoc('name[]title'));
|
||||
|
||||
|
||||
Assert::equal(array(
|
||||
'Arnold Rimmer' => new DibiRow(array(
|
||||
'title' => array(
|
||||
'Chair' => new DibiRow(array('title' => 'Chair', 'name' => 'Arnold Rimmer', 'amount' => num(7.0))),
|
||||
'Computer' => new DibiRow(array('title' => 'Computer', 'name' => 'Arnold Rimmer', 'amount' => num(2.0))),
|
||||
),
|
||||
'name' => 'Arnold Rimmer',
|
||||
'amount' => num(7.0),
|
||||
)),
|
||||
'Dave Lister' => new DibiRow(array(
|
||||
'title' => array(
|
||||
'Table' => new DibiRow(array('title' => 'Table', 'name' => 'Dave Lister', 'amount' => num(3.0))),
|
||||
),
|
||||
'name' => 'Dave Lister',
|
||||
'amount' => num(3.0),
|
||||
)),
|
||||
'Kristine Kochanski' => new DibiRow(array(
|
||||
'title' => array(
|
||||
'Computer' => new DibiRow(array('title' => 'Computer', 'name' => 'Kristine Kochanski', 'amount' => num(5.0))),
|
||||
),
|
||||
'name' => 'Kristine Kochanski',
|
||||
'amount' => num(5.0),
|
||||
)),
|
||||
), query($conn)->fetchAssoc('name->title'));
|
||||
|
||||
|
||||
Assert::equal(array(
|
||||
'Arnold Rimmer' => new DibiRow(array(
|
||||
'title' => array('Chair' => 'Arnold Rimmer', 'Computer' => 'Arnold Rimmer'),
|
||||
'name' => 'Arnold Rimmer',
|
||||
'amount' => num(7.0),
|
||||
)),
|
||||
'Dave Lister' => new DibiRow(array(
|
||||
'title' => array('Table' => 'Dave Lister'),
|
||||
'name' => 'Dave Lister',
|
||||
'amount' => num(3.0),
|
||||
)),
|
||||
'Kristine Kochanski' => new DibiRow(array(
|
||||
'title' => array('Computer' => 'Kristine Kochanski'),
|
||||
'name' => 'Kristine Kochanski',
|
||||
'amount' => num(5.0),
|
||||
)),
|
||||
), query($conn)->fetchAssoc('name->title=name'));
|
||||
|
||||
|
||||
Assert::equal(array(
|
||||
new DibiRow(array('title' => 'Chair', 'name' => 'Arnold Rimmer', 'amount' => num(7.0))),
|
||||
new DibiRow(array('title' => 'Computer', 'name' => 'Arnold Rimmer', 'amount' => num(2.0))),
|
||||
new DibiRow(array('title' => 'Table', 'name' => 'Dave Lister', 'amount' => num(3.0))),
|
||||
new DibiRow(array('title' => 'Computer', 'name' => 'Kristine Kochanski', 'amount' => num(5.0))),
|
||||
), query($conn)->fetchAssoc('[]'));
|
||||
|
||||
|
||||
Assert::equal(array(
|
||||
'Arnold Rimmer' => new DibiRow(array(
|
||||
'title' => array(
|
||||
'Chair' => new DibiRow(array('title' => 'Chair', 'name' => 'Arnold Rimmer', 'amount' => num(7.0))),
|
||||
'Computer' => new DibiRow(array('title' => 'Computer', 'name' => 'Arnold Rimmer', 'amount' => num(2.0))),
|
||||
),
|
||||
'name' => 'Arnold Rimmer',
|
||||
'amount' => num(7.0),
|
||||
)),
|
||||
'Dave Lister' => new DibiRow(array(
|
||||
'title' => array(
|
||||
'Table' => new DibiRow(array('title' => 'Table', 'name' => 'Dave Lister', 'amount' => num(3.0))),
|
||||
),
|
||||
'name' => 'Dave Lister',
|
||||
'amount' => num(3.0),
|
||||
)),
|
||||
'Kristine Kochanski' => new DibiRow(array(
|
||||
'title' => array(
|
||||
'Computer' => new DibiRow(array('title' => 'Computer', 'name' => 'Kristine Kochanski', 'amount' => num(5.0))),
|
||||
),
|
||||
'name' => 'Kristine Kochanski',
|
||||
'amount' => num(5.0),
|
||||
)),
|
||||
), query($conn)->fetchAssoc('name->title->'));
|
26
tests/dibi/DibiConnection.substitutions.phpt
Normal file
26
tests/dibi/DibiConnection.substitutions.phpt
Normal file
@@ -0,0 +1,26 @@
|
||||
<?php
|
||||
|
||||
use Tester\Assert;
|
||||
|
||||
require __DIR__ . '/bootstrap.php';
|
||||
|
||||
|
||||
$conn = new DibiConnection($config);
|
||||
|
||||
// create new substitution :blog: ==> wp_
|
||||
$conn->getSubstitutes()->blog = 'wp_';
|
||||
|
||||
Assert::same(
|
||||
reformat('UPDATE wp_items SET [text]=\'Hello World\''),
|
||||
$conn->translate("UPDATE :blog:items SET [text]='Hello World'")
|
||||
);
|
||||
|
||||
Assert::same(
|
||||
reformat('UPDATE \'wp_\' SET [text]=\'Hello World\''),
|
||||
$conn->translate("UPDATE :blog: SET [text]='Hello World'")
|
||||
);
|
||||
|
||||
Assert::same(
|
||||
reformat('UPDATE \':blg:\' SET [text]=\'Hello World\''),
|
||||
$conn->translate("UPDATE :blg: SET [text]='Hello World'")
|
||||
);
|
48
tests/dibi/DibiConnection.transactions.phpt
Normal file
48
tests/dibi/DibiConnection.transactions.phpt
Normal file
@@ -0,0 +1,48 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* @dataProvider ../databases.ini
|
||||
*/
|
||||
|
||||
use Tester\Assert;
|
||||
|
||||
require __DIR__ . '/bootstrap.php';
|
||||
|
||||
|
||||
$conn = new DibiConnection($config);
|
||||
$conn->loadFile(__DIR__ . "/data/$config[system].sql");
|
||||
|
||||
|
||||
/*Assert::exception(function() use ($conn) {
|
||||
$conn->rollback();
|
||||
}, 'DibiException');
|
||||
|
||||
Assert::exception(function() use ($conn) {
|
||||
$conn->commit();
|
||||
}, 'DibiException');
|
||||
|
||||
$conn->begin();
|
||||
Assert::exception(function() use ($conn) {
|
||||
$conn->begin();
|
||||
}, 'DibiException');
|
||||
*/
|
||||
|
||||
|
||||
$conn->begin();
|
||||
Assert::same(3, (int) $conn->query('SELECT COUNT(*) FROM [products]')->fetchSingle());
|
||||
$conn->query('INSERT INTO [products]', array(
|
||||
'title' => 'Test product',
|
||||
));
|
||||
Assert::same(4, (int) $conn->query('SELECT COUNT(*) FROM [products]')->fetchSingle());
|
||||
$conn->rollback();
|
||||
Assert::same(3, (int) $conn->query('SELECT COUNT(*) FROM [products]')->fetchSingle());
|
||||
|
||||
|
||||
|
||||
|
||||
$conn->begin();
|
||||
$conn->query('INSERT INTO [products]', array(
|
||||
'title' => 'Test product',
|
||||
));
|
||||
$conn->commit();
|
||||
Assert::same(4, (int) $conn->query('SELECT COUNT(*) FROM [products]')->fetchSingle());
|
45
tests/dibi/DibiFluent.delete.phpt
Normal file
45
tests/dibi/DibiFluent.delete.phpt
Normal file
@@ -0,0 +1,45 @@
|
||||
<?php
|
||||
|
||||
use Tester\Assert;
|
||||
|
||||
require __DIR__ . '/bootstrap.php';
|
||||
|
||||
|
||||
$conn = new DibiConnection($config);
|
||||
|
||||
|
||||
$fluent = $conn->delete('table')->as('bAlias')
|
||||
->setFlag('IGNORE');
|
||||
|
||||
Assert::same(
|
||||
reformat('DELETE IGNORE FROM [table] AS [bAlias]'),
|
||||
(string) $fluent
|
||||
);
|
||||
|
||||
$fluent->removeClause('from')->from('anotherTable');
|
||||
|
||||
Assert::same(
|
||||
reformat('DELETE IGNORE FROM [anotherTable]'),
|
||||
(string) $fluent
|
||||
);
|
||||
|
||||
$fluent->using('thirdTable');
|
||||
|
||||
Assert::same(
|
||||
reformat('DELETE IGNORE FROM [anotherTable] USING [thirdTable]'),
|
||||
(string) $fluent
|
||||
);
|
||||
|
||||
$fluent->setFlag('IGNORE', FALSE);
|
||||
|
||||
Assert::same(
|
||||
reformat('DELETE FROM [anotherTable] USING [thirdTable]'),
|
||||
(string) $fluent
|
||||
);
|
||||
|
||||
$fluent->limit(10);
|
||||
|
||||
Assert::same(
|
||||
reformat('DELETE FROM [anotherTable] USING [thirdTable] LIMIT 10'),
|
||||
(string) $fluent
|
||||
);
|
59
tests/dibi/DibiFluent.fetch.phpt
Normal file
59
tests/dibi/DibiFluent.fetch.phpt
Normal file
@@ -0,0 +1,59 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* @dataProvider ../databases.ini
|
||||
*/
|
||||
|
||||
use Tester\Assert;
|
||||
|
||||
require __DIR__ . '/bootstrap.php';
|
||||
|
||||
$conn = new DibiConnection($config);
|
||||
$conn->loadFile(__DIR__ . "/data/$config[system].sql");
|
||||
|
||||
|
||||
function num($n)
|
||||
{
|
||||
global $config;
|
||||
if (substr(@$config['dsn'], 0, 5) === 'odbc:' || $config['driver'] === 'sqlite') {
|
||||
$n = is_float($n) ? "$n.0" : (string) $n;
|
||||
}
|
||||
return $n;
|
||||
}
|
||||
|
||||
|
||||
// fetch a single value
|
||||
$res = $conn->select('title')->from('products')->orderBy('product_id');
|
||||
Assert::equal('Chair', $res->fetchSingle());
|
||||
|
||||
|
||||
// fetch complete result set
|
||||
$res = $conn->select('*')->from('products')->orderBy('product_id');
|
||||
Assert::equal(array(
|
||||
new DibiRow(array('product_id' => num(1), 'title' => 'Chair')),
|
||||
new DibiRow(array('product_id' => num(2), 'title' => 'Table')),
|
||||
new DibiRow(array('product_id' => num(3), 'title' => 'Computer')),
|
||||
), $res->fetchAll());
|
||||
|
||||
|
||||
// more complex association array
|
||||
if ($config['system'] !== 'odbc') {
|
||||
$res = $conn->select(array('products.title' => 'title', 'customers.name' => 'name'))->select('orders.amount')->as('amount')
|
||||
->from('products')
|
||||
->innerJoin('orders')->using('(product_id)')
|
||||
->innerJoin('customers')->using('([customer_id])')
|
||||
->orderBy('order_id');
|
||||
|
||||
Assert::equal(array(
|
||||
'Arnold Rimmer' => array(
|
||||
'Chair' => new DibiRow(array('title' => 'Chair', 'name' => 'Arnold Rimmer', 'amount' => num(7.0))),
|
||||
'Computer' => new DibiRow(array('title' => 'Computer', 'name' => 'Arnold Rimmer', 'amount' => num(2.0))),
|
||||
),
|
||||
'Dave Lister' => array(
|
||||
'Table' => new DibiRow(array('title' => 'Table', 'name' => 'Dave Lister', 'amount' => num(3.0))),
|
||||
),
|
||||
'Kristine Kochanski' => array(
|
||||
'Computer' => new DibiRow(array('title' => 'Computer', 'name' => 'Kristine Kochanski', 'amount' => num(5.0))),
|
||||
),
|
||||
), $res->fetchAssoc('name,title'));
|
||||
}
|
58
tests/dibi/DibiFluent.insert.phpt
Normal file
58
tests/dibi/DibiFluent.insert.phpt
Normal file
@@ -0,0 +1,58 @@
|
||||
<?php
|
||||
|
||||
use Tester\Assert;
|
||||
|
||||
require __DIR__ . '/bootstrap.php';
|
||||
|
||||
|
||||
$conn = new DibiConnection($config);
|
||||
|
||||
|
||||
$arr = array(
|
||||
'title' => 'Super Product',
|
||||
'price' => 12,
|
||||
'brand' => NULL,
|
||||
);
|
||||
|
||||
$fluent = $conn->insert('table', $arr)
|
||||
->setFlag('IGNORE')->setFlag('DELAYED');
|
||||
|
||||
Assert::same(
|
||||
reformat('INSERT IGNORE DELAYED INTO [table] ([title], [price], [brand]) VALUES (\'Super Product\', 12, NULL)'),
|
||||
(string) $fluent
|
||||
);
|
||||
|
||||
$fluent->setFlag('IGNORE', FALSE);
|
||||
|
||||
Assert::same(
|
||||
reformat('INSERT DELAYED INTO [table] ([title], [price], [brand]) VALUES (\'Super Product\', 12, NULL)'),
|
||||
(string) $fluent
|
||||
);
|
||||
|
||||
$fluent->setFlag('HIGH_priority');
|
||||
|
||||
Assert::same(
|
||||
reformat('INSERT DELAYED HIGH_PRIORITY INTO [table] ([title], [price], [brand]) VALUES (\'Super Product\', 12, NULL)'),
|
||||
(string) $fluent
|
||||
);
|
||||
|
||||
$fluent->into('anotherTable');
|
||||
|
||||
Assert::same(
|
||||
reformat('INSERT DELAYED HIGH_PRIORITY INTO [anotherTable] VALUES (\'Super Product\', 12, NULL)'),
|
||||
(string) $fluent
|
||||
);
|
||||
|
||||
$fluent->values('%l', $arr);
|
||||
|
||||
Assert::same(
|
||||
reformat('INSERT DELAYED HIGH_PRIORITY INTO [anotherTable] VALUES (\'Super Product\', 12, NULL) , (\'Super Product\', 12, NULL)'),
|
||||
(string) $fluent
|
||||
);
|
||||
|
||||
$fluent->values($arr);
|
||||
|
||||
Assert::same(
|
||||
reformat('INSERT DELAYED HIGH_PRIORITY INTO [anotherTable] VALUES (\'Super Product\', 12, NULL) , (\'Super Product\', 12, NULL) , (\'Super Product\', 12, NULL)'),
|
||||
(string) $fluent
|
||||
);
|
139
tests/dibi/DibiFluent.select.phpt
Normal file
139
tests/dibi/DibiFluent.select.phpt
Normal file
@@ -0,0 +1,139 @@
|
||||
<?php
|
||||
|
||||
use Tester\Assert;
|
||||
|
||||
require __DIR__ . '/bootstrap.php';
|
||||
|
||||
|
||||
$conn = new DibiConnection($config);
|
||||
|
||||
|
||||
$max = 10;
|
||||
$min = 5;
|
||||
|
||||
$fluent = $conn->select('*')
|
||||
->select('a')
|
||||
->select('b')->as('bAlias')
|
||||
->select(array('c', 'd', 'e'))
|
||||
->select('%n', 'd');
|
||||
|
||||
Assert::same(
|
||||
reformat('SELECT * , [a] , [b] AS [bAlias] , [c], [d], [e] , [d]'),
|
||||
(string) $fluent
|
||||
);
|
||||
|
||||
$fluent->from('table')->as('tableAlias')
|
||||
->innerJoin('table1')->on('table.col = table1.col')
|
||||
->innerJoin('table2')->on('table.col = table2.col');
|
||||
|
||||
Assert::same(
|
||||
reformat('SELECT * , [a] , [b] AS [bAlias] , [c], [d], [e] , [d] FROM [table] AS [tableAlias] INNER JOIN [table1] ON table.col = table1.col INNER JOIN [table2] ON table.col = table2.col'),
|
||||
(string) $fluent
|
||||
);
|
||||
|
||||
$fluent->from('anotherTable');
|
||||
|
||||
Assert::same(
|
||||
reformat('SELECT * , [a] , [b] AS [bAlias] , [c], [d], [e] , [d] FROM [table] AS [tableAlias] INNER JOIN [table1] ON table.col = table1.col INNER JOIN [table2] ON table.col = table2.col , [anotherTable]'),
|
||||
(string) $fluent
|
||||
);
|
||||
|
||||
$fluent->removeClause('from')->from('anotherTable');
|
||||
|
||||
Assert::same(
|
||||
reformat('SELECT * , [a] , [b] AS [bAlias] , [c], [d], [e] , [d] FROM [anotherTable]'),
|
||||
(string) $fluent
|
||||
);
|
||||
|
||||
$fluent->as('anotherAlias')
|
||||
->clause('from')
|
||||
->innerJoin('table3')
|
||||
->on('table.col = table3.col');
|
||||
|
||||
Assert::same(
|
||||
reformat('SELECT * , [a] , [b] AS [bAlias] , [c], [d], [e] , [d] FROM [anotherTable] AS [anotherAlias] INNER JOIN [table3] ON table.col = table3.col'),
|
||||
(string) $fluent
|
||||
);
|
||||
|
||||
$fluent->where('col > %i', $max)
|
||||
->or('col < %i', $min)
|
||||
->where('active = 1')
|
||||
->where('col')->in(array(1,2,3))
|
||||
->orderBy('val')->asc()
|
||||
->orderBy('[val2] DESC')
|
||||
->orderBy(array('val3' => -1));
|
||||
|
||||
Assert::same(
|
||||
reformat('SELECT * , [a] , [b] AS [bAlias] , [c], [d], [e] , [d] FROM [anotherTable] AS [anotherAlias] INNER JOIN [table3] ON table.col = table3.col WHERE col > 10 OR col < 5 AND active = 1 AND [col] IN (1, 2, 3) ORDER BY [val] ASC , [val2] DESC , [val3] DESC'),
|
||||
(string) $fluent
|
||||
);
|
||||
|
||||
$fluent->orderBy(DibiFluent::REMOVE);
|
||||
|
||||
Assert::same(
|
||||
reformat('SELECT * , [a] , [b] AS [bAlias] , [c], [d], [e] , [d] FROM [anotherTable] AS [anotherAlias] INNER JOIN [table3] ON table.col = table3.col WHERE col > 10 OR col < 5 AND active = 1 AND [col] IN (1, 2, 3)'),
|
||||
(string) $fluent
|
||||
);
|
||||
|
||||
|
||||
try {
|
||||
$fluent = $conn->select('*')->from('table')->fetch();
|
||||
} catch (Exception $e) {
|
||||
}
|
||||
Assert::same(
|
||||
reformat(' SELECT * FROM [table] LIMIT 1'),
|
||||
dibi::$sql
|
||||
);
|
||||
|
||||
|
||||
$fluent = $conn->select('*')
|
||||
->select(
|
||||
$conn->select('count(*)')
|
||||
->from('precteni')->as('P')
|
||||
->where('P.id_clanku', '=', 'C.id_clanku')
|
||||
)
|
||||
->from('clanky')->as('C')
|
||||
->where('id_clanku=%i', 123)
|
||||
->limit(1)
|
||||
->offset(0);
|
||||
|
||||
Assert::same(
|
||||
reformat('SELECT * , (SELECT count(*) FROM [precteni] AS [P] WHERE P.id_clanku = C.id_clanku) FROM [clanky] AS [C] WHERE id_clanku=123 LIMIT 1 OFFSET 0'),
|
||||
(string) $fluent
|
||||
);
|
||||
|
||||
|
||||
$fluent = $conn->select('*')
|
||||
->select(array('x' => 'xAlias'))
|
||||
->from('products')
|
||||
->innerJoin('orders')->using('(product_id)')
|
||||
->innerJoin('customers')->using('([customer_id])')
|
||||
->innerJoin('items')->using('(%n)', array('customer_id', 'order_id'));
|
||||
|
||||
Assert::same(
|
||||
reformat('SELECT * , [x] AS [xAlias] FROM [products] INNER JOIN [orders] USING (product_id) INNER JOIN [customers] USING ([customer_id]) INNER JOIN [items] USING ([customer_id], [order_id])'),
|
||||
(string) $fluent
|
||||
);
|
||||
|
||||
|
||||
|
||||
$fluent = $conn->command()->select()
|
||||
->from('products')
|
||||
->select('*')
|
||||
->innerJoin('orders')->using('(product_id)');
|
||||
|
||||
Assert::same(
|
||||
reformat('SELECT * FROM [products] INNER JOIN [orders] USING (product_id)'),
|
||||
(string) $fluent
|
||||
);
|
||||
|
||||
|
||||
$fluent = $conn->select('*')
|
||||
->from(array('me' => 't'))
|
||||
->where('col > %i', $max)
|
||||
->where(array('x' => 'a', 'b', 'c'));
|
||||
|
||||
Assert::same(
|
||||
reformat('SELECT * FROM [me] AS [t] WHERE col > 10 AND ([x] = \'a\') AND (b) AND (c)'),
|
||||
(string) $fluent
|
||||
);
|
30
tests/dibi/DibiFluent.update.phpt
Normal file
30
tests/dibi/DibiFluent.update.phpt
Normal file
@@ -0,0 +1,30 @@
|
||||
<?php
|
||||
|
||||
use Tester\Assert;
|
||||
|
||||
require __DIR__ . '/bootstrap.php';
|
||||
|
||||
|
||||
$conn = new DibiConnection($config);
|
||||
|
||||
|
||||
$arr = array(
|
||||
'title' => 'Super Product',
|
||||
'price' => 12,
|
||||
'brand' => NULL,
|
||||
);
|
||||
|
||||
$fluent = $conn->update('table', $arr)
|
||||
->setFlag('IGNORE')->setFlag('DELAYED');
|
||||
|
||||
Assert::same(
|
||||
reformat('UPDATE IGNORE DELAYED [table] SET [title]=\'Super Product\', [price]=12, [brand]=NULL'),
|
||||
(string) $fluent
|
||||
);
|
||||
|
||||
$fluent->set(array('another' => 123));
|
||||
|
||||
Assert::same(
|
||||
reformat('UPDATE IGNORE DELAYED [table] SET [title]=\'Super Product\', [price]=12, [brand]=NULL , [another]=123'),
|
||||
(string) $fluent
|
||||
);
|
57
tests/dibi/DibiResult.meta.phpt
Normal file
57
tests/dibi/DibiResult.meta.phpt
Normal file
@@ -0,0 +1,57 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* @dataProvider ../databases.ini
|
||||
*/
|
||||
|
||||
use Tester\Assert;
|
||||
|
||||
require __DIR__ . '/bootstrap.php';
|
||||
|
||||
if ($config['system'] === 'odbc') {
|
||||
Tester\Environment::skip('Not supported.');
|
||||
}
|
||||
|
||||
$conn = new DibiConnection($config);
|
||||
$conn->loadFile(__DIR__ . "/data/$config[system].sql");
|
||||
|
||||
$info = $conn->query('
|
||||
SELECT products.product_id, orders.order_id, customers.name, products.product_id + 1 AS xxx
|
||||
FROM products
|
||||
INNER JOIN orders USING (product_id)
|
||||
INNER JOIN customers USING (customer_id)
|
||||
')->getInfo();
|
||||
|
||||
|
||||
Assert::same(
|
||||
array('product_id', 'order_id', 'name', 'xxx'),
|
||||
$info->getColumnNames()
|
||||
);
|
||||
|
||||
|
||||
if ($config['driver'] !== 'sqlite3' && $config['driver'] !== 'pdo') {
|
||||
Assert::same(
|
||||
array('products.product_id', 'orders.order_id', 'customers.name', 'xxx'),
|
||||
$info->getColumnNames(TRUE)
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
$columns = $info->getColumns();
|
||||
|
||||
Assert::same('product_id', $columns[0]->name);
|
||||
if ($config['driver'] !== 'sqlite3' && $config['driver'] !== 'pdo') {
|
||||
Assert::same('products', $columns[0]->tableName);
|
||||
}
|
||||
Assert::null($columns[0]->getVendorInfo('xxx'));
|
||||
if ($config['system'] !== 'sqlite') {
|
||||
Assert::same('i', $columns[0]->type);
|
||||
}
|
||||
Assert::null($columns[0]->nullable);
|
||||
|
||||
Assert::same('xxx', $columns[3]->name);
|
||||
Assert::null($columns[3]->tableName);
|
||||
if ($config['system'] !== 'sqlite') {
|
||||
Assert::same('i', $columns[0]->type);
|
||||
}
|
||||
Assert::null($columns[3]->nullable);
|
18
tests/dibi/DibiResult.types.phpt
Normal file
18
tests/dibi/DibiResult.types.phpt
Normal file
@@ -0,0 +1,18 @@
|
||||
<?php
|
||||
|
||||
use Tester\Assert;
|
||||
|
||||
require __DIR__ . '/bootstrap.php';
|
||||
|
||||
$conn = new DibiConnection($config);
|
||||
$conn->loadFile(__DIR__ . "/data/$config[system].sql");
|
||||
|
||||
$res = $conn->query('SELECT * FROM [customers]');
|
||||
|
||||
// auto-converts this column to integer
|
||||
$res->setType('customer_id', Dibi::DATETIME, 'H:i j.n.Y');
|
||||
|
||||
Assert::equal(new DibiRow(array(
|
||||
'customer_id' => new DibiDateTime('1970-01-01 01:00:01'),
|
||||
'name' => 'Dave Lister',
|
||||
)), $res->fetch());
|
76
tests/dibi/DibiTranslator.conditions.phpt
Normal file
76
tests/dibi/DibiTranslator.conditions.phpt
Normal file
@@ -0,0 +1,76 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* @dataProvider ../databases.ini
|
||||
*/
|
||||
|
||||
use Tester\Assert;
|
||||
|
||||
require __DIR__ . '/bootstrap.php';
|
||||
|
||||
$conn = new DibiConnection($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("
|
||||
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'
|
||||
));
|
47
tests/dibi/DibiTranslator.identifiers.phpt
Normal file
47
tests/dibi/DibiTranslator.identifiers.phpt
Normal file
@@ -0,0 +1,47 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* @dataProvider ../databases.ini
|
||||
*/
|
||||
|
||||
use Tester\Assert;
|
||||
|
||||
require __DIR__ . '/bootstrap.php';
|
||||
|
||||
$conn = new DibiConnection($config);
|
||||
|
||||
|
||||
Assert::same(
|
||||
reformat('SELECT * FROM where WHERE select < 2'),
|
||||
$conn->translate('SELECT * FROM where WHERE select < 2')
|
||||
);
|
||||
|
||||
|
||||
Assert::same(
|
||||
reformat('SELECT * FROM [where] WHERE where.select < 2'),
|
||||
$conn->translate('SELECT * FROM [where] WHERE where.select < 2')
|
||||
);
|
||||
|
||||
|
||||
Assert::same(
|
||||
reformat('SELECT * FROM [where] WHERE [where].[select] < 2'),
|
||||
$conn->translate('SELECT * FROM [where] WHERE [where.select] < 2')
|
||||
);
|
||||
|
||||
|
||||
Assert::same(
|
||||
reformat('SELECT * FROM [where] as [temp] WHERE [temp].[select] < 2'),
|
||||
$conn->translate('SELECT * FROM [where] as [temp] WHERE [temp.select] < 2')
|
||||
);
|
||||
|
||||
|
||||
Assert::same(
|
||||
reformat('SELECT * FROM [where] WHERE [quot\'n\' space] > 2'),
|
||||
$conn->translate("SELECT * FROM [where] WHERE [quot'n' space] > 2")
|
||||
);
|
||||
|
||||
|
||||
Assert::same(
|
||||
reformat('SELECT * FROM [where] WHERE [where].[quot\'n\' space] > 2'),
|
||||
$conn->translate("SELECT * FROM [where] WHERE [where.quot'n' space] > 2")
|
||||
);
|
510
tests/dibi/DibiTranslator.phpt
Normal file
510
tests/dibi/DibiTranslator.phpt
Normal file
@@ -0,0 +1,510 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* @dataProvider ../databases.ini
|
||||
*/
|
||||
|
||||
use Tester\Assert;
|
||||
|
||||
require __DIR__ . '/bootstrap.php';
|
||||
|
||||
$conn = new DibiConnection($config + array('formatDateTime' => "'Y-m-d H:i:s'", 'formatDate' => "'Y-m-d'"));
|
||||
|
||||
|
||||
// dibi detects INSERT or REPLACE command & booleans
|
||||
Assert::same(
|
||||
reformat("REPLACE INTO [products] ([title], [price]) VALUES ('Drticka', 318)"),
|
||||
$conn->translate('REPLACE INTO [products]', array(
|
||||
'title' => 'Drticka',
|
||||
'price' => 318,
|
||||
)));
|
||||
|
||||
|
||||
// multiple INSERT command
|
||||
$array = array(
|
||||
'title' => 'Super Product',
|
||||
'price' => 12,
|
||||
'brand' => NULL,
|
||||
);
|
||||
Assert::same(
|
||||
reformat('INSERT INTO [products] ([title], [price], [brand]) VALUES (\'Super Product\', 12, NULL) , (\'Super Product\', 12, NULL) , (\'Super Product\', 12, NULL)'),
|
||||
$conn->translate("INSERT INTO [products]", $array, $array, $array)
|
||||
);
|
||||
|
||||
|
||||
// multiple INSERT command II
|
||||
$array = array(
|
||||
array('pole' => 'hodnota1', 'bit' => 1),
|
||||
array('pole' => 'hodnota2', 'bit' => 1),
|
||||
array('pole' => 'hodnota3', 'bit' => 1)
|
||||
);
|
||||
Assert::same(
|
||||
reformat('INSERT INTO [products] ([pole], [bit]) VALUES (\'hodnota1\', 1) , (\'hodnota2\', 1) , (\'hodnota3\', 1)'),
|
||||
$conn->translate("INSERT INTO [products] %ex", $array)
|
||||
);
|
||||
|
||||
|
||||
// dibi detects UPDATE command
|
||||
Assert::same(
|
||||
reformat("UPDATE [colors] SET [color]='blue', [order]=12 WHERE [id]=123"),
|
||||
$conn->translate('UPDATE [colors] SET', array(
|
||||
'color' => 'blue',
|
||||
'order' => 12,
|
||||
), "WHERE [id]=%i", 123));
|
||||
|
||||
|
||||
// IN array
|
||||
$array = array(1, 2, 3);
|
||||
Assert::same(
|
||||
reformat('SELECT * FROM [people] WHERE [id] IN ( 1, 2, 3 )'),
|
||||
$conn->translate("SELECT * FROM [people] WHERE [id] IN (", $array, ")")
|
||||
);
|
||||
|
||||
|
||||
// long numbers
|
||||
Assert::same(
|
||||
reformat('SELECT -123456789123456789123456789'),
|
||||
$conn->translate("SELECT %i", '-123456789123456789123456789')
|
||||
);
|
||||
|
||||
// long float numbers
|
||||
Assert::same(
|
||||
reformat('SELECT -.12345678912345678912345678e10'),
|
||||
$conn->translate("SELECT %f", '-.12345678912345678912345678e10')
|
||||
);
|
||||
|
||||
// hex numbers
|
||||
Assert::same(
|
||||
reformat('SELECT 17'),
|
||||
$conn->translate("SELECT %i", '0x11')
|
||||
);
|
||||
|
||||
// invalid input
|
||||
$e = Assert::exception(function() use ($conn) {
|
||||
$conn->translate("SELECT %s", (object) array(123), ', %m', 123);
|
||||
}, 'DibiException', 'SQL translate error');
|
||||
Assert::same('SELECT **Unexpected type object** , **Unknown or invalid modifier %m**', $e->getSql());
|
||||
|
||||
Assert::same(
|
||||
reformat('SELECT * FROM [table] WHERE id=10 AND name=\'ahoj\''),
|
||||
$conn->translate('SELECT * FROM [table] WHERE id=%i AND name=%s', 10, 'ahoj')
|
||||
);
|
||||
|
||||
Assert::same(
|
||||
reformat('TEST ([cond] > 2) OR ([cond2] = \'3\') OR (cond3 < RAND())'),
|
||||
$conn->translate('TEST %or', array('[cond] > 2', '[cond2] = "3"', 'cond3 < RAND()'))
|
||||
);
|
||||
|
||||
Assert::same(
|
||||
reformat('TEST ([cond] > 2) AND ([cond2] = \'3\') AND (cond3 < RAND())'),
|
||||
$conn->translate('TEST %and', array('[cond] > 2', '[cond2] = "3"', 'cond3 < RAND()'))
|
||||
);
|
||||
|
||||
//
|
||||
$where = array();
|
||||
$where[] = '[age] > 20';
|
||||
$where[] = '[email] IS NOT NULL';
|
||||
Assert::same(
|
||||
reformat('SELECT * FROM [table] WHERE ([age] > 20) AND ([email] IS NOT NULL)'),
|
||||
$conn->translate('SELECT * FROM [table] WHERE %and', $where)
|
||||
);
|
||||
|
||||
|
||||
$where = array();
|
||||
$where['age'] = NULL;
|
||||
$where['email'] = 'ahoj';
|
||||
$where['id%l'] = array(10, 20, 30);
|
||||
Assert::same(
|
||||
reformat('SELECT * FROM [table] WHERE ([age] IS NULL) AND ([email] = \'ahoj\') AND ([id] IN (10, 20, 30))'),
|
||||
$conn->translate('SELECT * FROM [table] WHERE %and', $where)
|
||||
);
|
||||
|
||||
|
||||
$where = array();
|
||||
Assert::same(
|
||||
reformat('SELECT * FROM [table] WHERE 1=1'),
|
||||
$conn->translate('SELECT * FROM [table] WHERE %and', $where)
|
||||
);
|
||||
|
||||
|
||||
// ORDER BY array
|
||||
$order = array(
|
||||
'field1' => 'asc',
|
||||
'field2' => 'desc',
|
||||
'field3' => 1,
|
||||
'field4' => -1,
|
||||
'field5' => TRUE,
|
||||
'field6' => FALSE,
|
||||
);
|
||||
Assert::same(
|
||||
reformat("SELECT * FROM [people] ORDER BY [field1] ASC, [field2] DESC, [field3] ASC, [field4] DESC, [field5] ASC, [field6] DESC"),
|
||||
$conn->translate("SELECT * FROM [people] ORDER BY %by", $order)
|
||||
);
|
||||
|
||||
|
||||
// with limit = 2
|
||||
Assert::same(
|
||||
reformat(array(
|
||||
'odbc' => 'SELECT TOP 2 * FROM (SELECT * FROM [products] ) t',
|
||||
'SELECT * FROM [products] LIMIT 2',
|
||||
)),
|
||||
$conn->translate('SELECT * FROM [products] %lmt', 2)
|
||||
);
|
||||
|
||||
if ($config['system'] === 'odbc') {
|
||||
Assert::exception(function() use ($conn) {
|
||||
$conn->translate('SELECT * FROM [products] %lmt %ofs', 2, 1);
|
||||
}, 'DibiException');
|
||||
} else {
|
||||
// with limit = 2, offset = 1
|
||||
Assert::same(
|
||||
reformat('SELECT * FROM [products] LIMIT 2 OFFSET 1'),
|
||||
$conn->translate('SELECT * FROM [products] %lmt %ofs', 2, 1)
|
||||
);
|
||||
|
||||
// with offset = 50
|
||||
Assert::same(
|
||||
reformat(array(
|
||||
'mysql' => 'SELECT * FROM `products` LIMIT 18446744073709551615 OFFSET 50',
|
||||
'pgsql' => 'SELECT * FROM "products" OFFSET 50',
|
||||
'SELECT * FROM [products] LIMIT -1 OFFSET 50',
|
||||
)),
|
||||
$conn->translate('SELECT * FROM [products] %ofs', 50)
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
Assert::same(
|
||||
reformat(array(
|
||||
'odbc' => 'INSERT INTO test ([a2], [a4], [b1], [b2], [b3], [b4], [b5], [b6], [b7], [b8], [b9]) VALUES (#09/26/1212 00:00:00#, #12/31/1969 22:13:20#, #09/26/1212#, #09/26/1212 00:00:00#, #12/31/1969#, #12/31/1969 22:13:20#, #09/26/1212 00:00:00#, #09/26/1212#, #09/26/1212 00:00:00#, NULL, NULL)',
|
||||
"INSERT INTO test ([a2], [a4], [b1], [b2], [b3], [b4], [b5], [b6], [b7], [b8], [b9]) VALUES ('1212-09-26 00:00:00', '1969-12-31 22:13:20', '1212-09-26', '1212-09-26 00:00:00', '1969-12-31', '1969-12-31 22:13:20', '1212-09-26 00:00:00', '1212-09-26', '1212-09-26 00:00:00', NULL, NULL)",
|
||||
)),
|
||||
$conn->translate("INSERT INTO test", array(
|
||||
'a2' => new DibiDateTime('1212-09-26'),
|
||||
'a4' => new DibiDateTime(-10000),
|
||||
'b1%d' => '1212-09-26',
|
||||
'b2%t' => '1212-09-26',
|
||||
'b3%d' => -10000,
|
||||
'b4%t' => -10000,
|
||||
'b5' => new DateTime('1212-09-26'),
|
||||
'b6%d' => new DateTime('1212-09-26'),
|
||||
'b7%t' => new DateTime('1212-09-26'),
|
||||
'b8%d' => NULL,
|
||||
'b9%t' => NULL,
|
||||
)));
|
||||
|
||||
|
||||
|
||||
// like
|
||||
if ($config['driver'] !== 'sqlite') { // sqlite2
|
||||
Assert::same(
|
||||
reformat(array(
|
||||
'sqlite' => "SELECT * FROM products WHERE (title LIKE 'C%' ESCAPE '\\' AND title LIKE '%r' ESCAPE '\\') OR title LIKE '%a\n\\%\\_\\\\''\"%' ESCAPE '\\'",
|
||||
'odbc' => "SELECT * FROM products WHERE (title LIKE 'C%' AND title LIKE '%r') OR title LIKE '%a\n[%][_]\\''\"%'",
|
||||
'pgsql' => "SELECT * FROM products WHERE (title LIKE 'C%' AND title LIKE '%r') OR title LIKE '%a\n\\\\%\\\\_\\''\"%'",
|
||||
"SELECT * FROM products WHERE (title LIKE 'C%' AND title LIKE '%r') OR title LIKE '%a\\n\\%\\_\\\\\\\\\'\"%'",
|
||||
)),
|
||||
$conn->translate("SELECT * FROM products WHERE (title LIKE %like~ AND title LIKE %~like) OR title LIKE %~like~", 'C', 'r', "a\n%_\\'\"")
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
$e = Assert::exception(function() use ($conn) {
|
||||
$conn->translate("SELECT '");
|
||||
}, 'DibiException', 'SQL translate error');
|
||||
Assert::same('SELECT **Alone quote**', $e->getSql());
|
||||
|
||||
Assert::match(
|
||||
reformat(array(
|
||||
'mysql' => "SELECT DISTINCT HIGH_PRIORITY SQL_BUFFER_RESULT
|
||||
CONCAT(last_name, ', ', first_name) AS full_name
|
||||
GROUP BY `user`
|
||||
HAVING MAX(salary) > %i 123
|
||||
INTO OUTFILE '/tmp/result\'.txt'
|
||||
FIELDS TERMINATED BY ',' OPTIONALLY ENCLOSED BY '\\\"'
|
||||
LINES TERMINATED BY '\\\\n'
|
||||
",
|
||||
"SELECT DISTINCT HIGH_PRIORITY SQL_BUFFER_RESULT
|
||||
CONCAT(last_name, ', ', first_name) AS full_name
|
||||
GROUP BY [user]
|
||||
HAVING MAX(salary) > %i 123
|
||||
INTO OUTFILE '/tmp/result''.txt'
|
||||
FIELDS TERMINATED BY ',' OPTIONALLY ENCLOSED BY '\"'
|
||||
LINES TERMINATED BY '\\n'
|
||||
"
|
||||
)),
|
||||
$conn->translate('%sql', "SELECT DISTINCT HIGH_PRIORITY SQL_BUFFER_RESULT
|
||||
CONCAT(last_name, \", \", first_name) AS full_name
|
||||
GROUP BY [user]
|
||||
HAVING MAX(salary) > %i", 123, "
|
||||
INTO OUTFILE '/tmp/result''.txt'
|
||||
FIELDS TERMINATED BY ',' OPTIONALLY ENCLOSED BY '\"'
|
||||
LINES TERMINATED BY '\\n'
|
||||
")
|
||||
);
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
$array1 = array(1, 2, 3);
|
||||
$array2 = array('one', 'two', 'three');
|
||||
$array3 = array(
|
||||
'col1' => 'one',
|
||||
'col2' => 'two',
|
||||
'col3' => 'three',
|
||||
);
|
||||
$array4 = array(
|
||||
'a' => 12,
|
||||
'b' => NULL,
|
||||
'c' => new DibiDateTime('12.3.2007'),
|
||||
'd' => 'any string',
|
||||
);
|
||||
|
||||
$array5 = array('RAND()', '[col1] > [col2]');
|
||||
|
||||
|
||||
Assert::match(
|
||||
reformat(array(
|
||||
'mysql' => "SELECT *
|
||||
FROM `db`.`table`
|
||||
WHERE (`test`.`a` LIKE '1995-03-01'
|
||||
OR `b1` IN ( 1, 2, 3 )
|
||||
OR `b2` IN ('1', '2', '3' )
|
||||
OR `b3` IN ( )
|
||||
OR `b4` IN ( 'one', 'two', 'three' )
|
||||
OR `b5` IN (`col1` AS `one`, `col2` AS `two`, `col3` AS `three` )
|
||||
OR `b6` IN ('one', 'two', 'three')
|
||||
OR `b7` IN (NULL)
|
||||
OR `b8` IN (RAND() `col1` > `col2` )
|
||||
OR `b9` IN ( )
|
||||
AND `c` = 'embedded \' string'
|
||||
OR `d`=10
|
||||
OR `e`=NULL
|
||||
OR `true`= 1
|
||||
OR `false`= 0
|
||||
OR `str_null`=NULL
|
||||
OR `str_not_null`='hello'
|
||||
LIMIT 10",
|
||||
'pgsql' => 'SELECT *
|
||||
FROM "db"."table"
|
||||
WHERE ("test"."a" LIKE \'1995-03-01\'
|
||||
OR "b1" IN ( 1, 2, 3 )
|
||||
OR "b2" IN (\'1\', \'2\', \'3\' )
|
||||
OR "b3" IN ( )
|
||||
OR "b4" IN ( \'one\', \'two\', \'three\' )
|
||||
OR "b5" IN ("col1" AS "one", "col2" AS "two", "col3" AS "three" )
|
||||
OR "b6" IN (\'one\', \'two\', \'three\')
|
||||
OR "b7" IN (NULL)
|
||||
OR "b8" IN (RAND() "col1" > "col2" )
|
||||
OR "b9" IN ( )
|
||||
AND "c" = \'embedded \'\' string\'
|
||||
OR "d"=10
|
||||
OR "e"=NULL
|
||||
OR "true"= TRUE
|
||||
OR "false"= FALSE
|
||||
OR "str_null"=NULL
|
||||
OR "str_not_null"=\'hello\'
|
||||
LIMIT 10',
|
||||
'odbc' => "SELECT *
|
||||
FROM [db].[table]
|
||||
WHERE ([test].[a] LIKE #03/01/1995#
|
||||
OR [b1] IN ( 1, 2, 3 )
|
||||
OR [b2] IN ('1', '2', '3' )
|
||||
OR [b3] IN ( )
|
||||
OR [b4] IN ( 'one', 'two', 'three' )
|
||||
OR [b5] IN ([col1] AS [one], [col2] AS [two], [col3] AS [three] )
|
||||
OR [b6] IN ('one', 'two', 'three')
|
||||
OR [b7] IN (NULL)
|
||||
OR [b8] IN (RAND() [col1] > [col2] )
|
||||
OR [b9] IN ( )
|
||||
AND [c] = 'embedded '' string'
|
||||
OR [d]=10
|
||||
OR [e]=NULL
|
||||
OR [true]= 1
|
||||
OR [false]= 0
|
||||
OR [str_null]=NULL
|
||||
OR [str_not_null]='hello'
|
||||
LIMIT 10",
|
||||
"SELECT *
|
||||
FROM [db].[table]
|
||||
WHERE ([test].[a] LIKE '1995-03-01'
|
||||
OR [b1] IN ( 1, 2, 3 )
|
||||
OR [b2] IN ('1', '2', '3' )
|
||||
OR [b3] IN ( )
|
||||
OR [b4] IN ( 'one', 'two', 'three' )
|
||||
OR [b5] IN ([col1] AS [one], [col2] AS [two], [col3] AS [three] )
|
||||
OR [b6] IN ('one', 'two', 'three')
|
||||
OR [b7] IN (NULL)
|
||||
OR [b8] IN (RAND() [col1] > [col2] )
|
||||
OR [b9] IN ( )
|
||||
AND [c] = 'embedded '' string'
|
||||
OR [d]=10
|
||||
OR [e]=NULL
|
||||
OR [true]= 1
|
||||
OR [false]= 0
|
||||
OR [str_null]=NULL
|
||||
OR [str_not_null]='hello'
|
||||
LIMIT 10",
|
||||
)),
|
||||
|
||||
$conn->translate("SELECT *
|
||||
FROM [db.table]
|
||||
WHERE ([test.a] LIKE %d", '1995-03-01', "
|
||||
OR [b1] IN (", $array1, ")
|
||||
OR [b2] IN (%s", $array1, ")
|
||||
OR [b3] IN (%s", array(), ")
|
||||
OR [b4] IN (", $array2, ")
|
||||
OR [b5] IN (%n", $array3, ")
|
||||
OR [b6] IN %l", $array3, "
|
||||
OR [b7] IN %in", array(), "
|
||||
OR [b8] IN (%sql", $array5, ")
|
||||
OR [b9] IN (", array(), ")
|
||||
AND [c] = 'embedded '' string'
|
||||
OR [d]=%i", 10.3, "
|
||||
OR [e]=%i", NULL, "
|
||||
OR [true]=", TRUE, "
|
||||
OR [false]=", FALSE, "
|
||||
OR [str_null]=%sn", '', "
|
||||
OR [str_not_null]=%sn", 'hello', "
|
||||
LIMIT 10")
|
||||
);
|
||||
|
||||
|
||||
Assert::same(
|
||||
reformat('TEST [cond] > 2 [cond2] = \'3\' cond3 < RAND() 123'),
|
||||
$conn->translate('TEST %ex', array('[cond] > 2', '[cond2] = "3"', 'cond3 < RAND()'), 123)
|
||||
);
|
||||
|
||||
|
||||
Assert::same(
|
||||
reformat('TEST ([cond] > 2) OR ([cond2] > 3) OR ([cond3] = 10 + 1)'),
|
||||
$conn->translate('TEST %or', array('`cond` > 2', array('[cond2] > %i', '3'), 'cond3%sql' => array('10 + 1')))
|
||||
);
|
||||
|
||||
|
||||
Assert::same(
|
||||
reformat('TEST ([cond] = 2) OR ([cond3] = RAND())'),
|
||||
$conn->translate('TEST %or', array('cond' => 2, 'cond3%sql' => 'RAND()'))
|
||||
);
|
||||
|
||||
|
||||
Assert::same(
|
||||
reformat('TEST ([cond1] 3) OR ([cond2] RAND()) OR ([cond3] LIKE \'string\')'),
|
||||
$conn->translate('TEST %or', array('cond1%ex' => 3, 'cond2%ex' => 'RAND()', 'cond3%ex' => array('LIKE %s', 'string')))
|
||||
);
|
||||
|
||||
|
||||
Assert::same(
|
||||
reformat(array(
|
||||
'odbc' => 'SELECT TOP 10 * FROM (SELECT * FROM [test] WHERE [id] LIKE \'%d%t\' ) t',
|
||||
'SELECT * FROM [test] WHERE [id] LIKE \'%d%t\' LIMIT 10',
|
||||
)),
|
||||
$conn->translate("SELECT * FROM [test] WHERE %n LIKE '%d%t' %lmt", 'id', 10)
|
||||
);
|
||||
|
||||
|
||||
$where = array(
|
||||
'tablename.column' => 1,
|
||||
);
|
||||
Assert::same(
|
||||
reformat('SELECT * FROM [tablename] WHERE ([tablename].[column] = 1)'),
|
||||
$conn->translate('SELECT * FROM [tablename] WHERE %and', $where)
|
||||
);
|
||||
|
||||
|
||||
Assert::same(
|
||||
reformat('SELECT FROM ... '),
|
||||
$conn->translate('SELECT FROM ... %lmt', NULL)
|
||||
);
|
||||
|
||||
Assert::same(
|
||||
reformat('SELECT \'%i\''),
|
||||
$conn->translate("SELECT '%i'")
|
||||
);
|
||||
|
||||
Assert::same(
|
||||
reformat('SELECT \'%i\''),
|
||||
$conn->translate('SELECT "%i"')
|
||||
);
|
||||
|
||||
|
||||
Assert::same(
|
||||
reformat('INSERT INTO [products] ([product_id], [title]) VALUES (1, SHA1(\'Test product\')) , (1, SHA1(\'Test product\'))'),
|
||||
$conn->translate('INSERT INTO [products]', array(
|
||||
'product_id' => 1,
|
||||
'title' => array('SHA1(%s)', 'Test product'),
|
||||
), array(
|
||||
'product_id' => 1,
|
||||
'title' => array('SHA1(%s)', 'Test product'),
|
||||
))
|
||||
);
|
||||
|
||||
Assert::same(
|
||||
reformat('UPDATE [products] [product_id]=1, [title]=SHA1(\'Test product\')'),
|
||||
$conn->translate('UPDATE [products]', array(
|
||||
'product_id' => 1,
|
||||
'title' => array('SHA1(%s)', 'Test product'),
|
||||
))
|
||||
);
|
||||
|
||||
|
||||
$e = Assert::exception(function() use ($conn) {
|
||||
$array6 = array(
|
||||
'id' => array(1, 2, 3, 4),
|
||||
'text' => array('ahoj', 'jak', 'se', array('SUM(%i)', '5')),
|
||||
'num%i' => array('1', ''),
|
||||
);
|
||||
$conn->translate('INSERT INTO test %m', $array6);
|
||||
}, 'DibiException', 'SQL translate error');
|
||||
Assert::same('INSERT INTO test **Multi-insert array "num%i" is different.**', $e->getSql());
|
||||
|
||||
$array6 = array(
|
||||
'id' => array(1, 2, 3, 4),
|
||||
'text' => array('ahoj', 'jak', 'se', array('SUM(%i)', '5')),
|
||||
'num%i' => array('1', '', 10.3, 1),
|
||||
);
|
||||
|
||||
Assert::same(
|
||||
reformat('INSERT INTO test ([id], [text], [num]) VALUES (1, \'ahoj\', 1), (2, \'jak\', 0), (3, \'se\', 10), (4, SUM(5), 1)'),
|
||||
$conn->translate('INSERT INTO test %m', $array6)
|
||||
);
|
||||
|
||||
|
||||
$by = array (
|
||||
array('funkce(nazev_pole) ASC'),
|
||||
'jine_pole' => 'DESC'
|
||||
);
|
||||
|
||||
Assert::same(
|
||||
reformat('SELECT * FROM table ORDER BY funkce(nazev_pole) ASC, [jine_pole] DESC'),
|
||||
$conn->translate("SELECT * FROM table ORDER BY %by", $by)
|
||||
);
|
||||
|
||||
Assert::same(
|
||||
reformat('INSERT INTO [test].*'),
|
||||
$conn->translate('INSERT INTO [test.*]')
|
||||
);
|
||||
|
||||
Assert::same(
|
||||
reformat('INSERT INTO 0'),
|
||||
$conn->translate('INSERT INTO %f', 'ahoj')
|
||||
);
|
||||
|
||||
|
||||
setLocale(LC_ALL, 'czech');
|
||||
|
||||
Assert::same(
|
||||
reformat("UPDATE [colors] SET [color]='blue', [price]=-12.4, [spec]=-9E-005, [spec2]=1000, [spec3]=10000, [spec4]=10000 WHERE [price]=123.5"),
|
||||
|
||||
$conn->translate("UPDATE [colors] SET", array(
|
||||
'color' => 'blue',
|
||||
'price' => -12.4,
|
||||
'spec%f' => '-9E-005',
|
||||
'spec2%f' => 1000.00,
|
||||
'spec3%i' => 10000,
|
||||
'spec4' => 10000,
|
||||
), "WHERE [price]=%f", 123.5)
|
||||
);
|
@@ -29,6 +29,13 @@ define('TEMP_DIR', __DIR__ . '/../tmp');
|
||||
Tester\Environment::lock($config['system'], TEMP_DIR);
|
||||
|
||||
|
||||
// ODBC
|
||||
if ($config['system'] === 'odbc') {
|
||||
copy(__DIR__ . '/data/odbc.mdb', TEMP_DIR . '/odbc.mdb');
|
||||
$config['dsn'] = str_replace('data/odbc.mdb', TEMP_DIR . '/odbc.mdb', $config['dsn']);
|
||||
}
|
||||
|
||||
|
||||
try {
|
||||
new DibiConnection($config);
|
||||
} catch (DibiNotSupportedException $e) {
|
||||
|
52
tests/dibi/data/mysql.sql
Normal file
52
tests/dibi/data/mysql.sql
Normal file
@@ -0,0 +1,52 @@
|
||||
/*!40102 SET storage_engine = InnoDB */;
|
||||
|
||||
DROP DATABASE IF EXISTS dibi_test;
|
||||
CREATE DATABASE dibi_test;
|
||||
USE dibi_test;
|
||||
|
||||
DROP TABLE IF EXISTS `products`;
|
||||
CREATE TABLE `products` (
|
||||
`product_id` int(11) NOT NULL AUTO_INCREMENT,
|
||||
`title` varchar(100) DEFAULT NULL,
|
||||
PRIMARY KEY (`product_id`),
|
||||
KEY `title` (`title`)
|
||||
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
|
||||
|
||||
INSERT INTO `products` (`product_id`, `title`) VALUES
|
||||
(1, 'Chair'),
|
||||
(3, 'Computer'),
|
||||
(2, 'Table');
|
||||
|
||||
DROP TABLE IF EXISTS `customers`;
|
||||
CREATE TABLE `customers` (
|
||||
`customer_id` int(11) NOT NULL AUTO_INCREMENT,
|
||||
`name` varchar(100) DEFAULT NULL,
|
||||
PRIMARY KEY (`customer_id`)
|
||||
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
|
||||
|
||||
INSERT INTO `customers` (`customer_id`, `name`) VALUES
|
||||
(1, 'Dave Lister'),
|
||||
(2, 'Arnold Rimmer'),
|
||||
(3, 'The Cat'),
|
||||
(4, 'Holly'),
|
||||
(5, 'Kryten'),
|
||||
(6, 'Kristine Kochanski');
|
||||
|
||||
DROP TABLE IF EXISTS `orders`;
|
||||
CREATE TABLE `orders` (
|
||||
`order_id` int(11) NOT NULL AUTO_INCREMENT,
|
||||
`customer_id` int(11) NOT NULL,
|
||||
`product_id` int(11) NOT NULL,
|
||||
`amount` float NOT NULL,
|
||||
PRIMARY KEY (`order_id`),
|
||||
KEY `customer_id` (`customer_id`),
|
||||
KEY `product_id` (`product_id`),
|
||||
CONSTRAINT `orders_ibfk_4` FOREIGN KEY (`product_id`) REFERENCES `products` (`product_id`) ON UPDATE CASCADE,
|
||||
CONSTRAINT `orders_ibfk_3` FOREIGN KEY (`customer_id`) REFERENCES `customers` (`customer_id`) ON UPDATE CASCADE
|
||||
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
|
||||
|
||||
INSERT INTO `orders` (`order_id`, `customer_id`, `product_id`, `amount`) VALUES
|
||||
(1, 2, 1, 7),
|
||||
(2, 2, 3, 2),
|
||||
(3, 1, 2, 3),
|
||||
(4, 6, 3, 5);
|
BIN
tests/dibi/data/odbc.mdb
Normal file
BIN
tests/dibi/data/odbc.mdb
Normal file
Binary file not shown.
32
tests/dibi/data/odbc.sql
Normal file
32
tests/dibi/data/odbc.sql
Normal file
@@ -0,0 +1,32 @@
|
||||
CREATE TABLE products (
|
||||
product_id COUNTER,
|
||||
title TEXT(50)
|
||||
);
|
||||
|
||||
INSERT INTO products (product_id, title) VALUES (1, 'Chair');
|
||||
INSERT INTO products (product_id, title) VALUES (2, 'Table');
|
||||
INSERT INTO products (product_id, title) VALUES (3, 'Computer');
|
||||
|
||||
CREATE TABLE [customers] (
|
||||
[customer_id] COUNTER,
|
||||
[name] TEXT(50)
|
||||
);
|
||||
|
||||
INSERT INTO `customers` (`customer_id`, `name`) VALUES (1, 'Dave Lister');
|
||||
INSERT INTO `customers` (`customer_id`, `name`) VALUES (2, 'Arnold Rimmer');
|
||||
INSERT INTO `customers` (`customer_id`, `name`) VALUES (3, 'The Cat');
|
||||
INSERT INTO `customers` (`customer_id`, `name`) VALUES (4, 'Holly');
|
||||
INSERT INTO `customers` (`customer_id`, `name`) VALUES (5, 'Kryten');
|
||||
INSERT INTO `customers` (`customer_id`, `name`) VALUES (6, 'Kristine Kochanski');
|
||||
|
||||
CREATE TABLE [orders] (
|
||||
[order_id] INTEGER,
|
||||
[customer_id] INTEGER,
|
||||
[product_id] INTEGER,
|
||||
[amount] FLOAT
|
||||
);
|
||||
|
||||
INSERT INTO `orders` (`order_id`, `customer_id`, `product_id`, `amount`) VALUES (1, 2, 1, 7);
|
||||
INSERT INTO `orders` (`order_id`, `customer_id`, `product_id`, `amount`) VALUES (2, 2, 3, 2);
|
||||
INSERT INTO `orders` (`order_id`, `customer_id`, `product_id`, `amount`) VALUES (3, 1, 2, 3);
|
||||
INSERT INTO `orders` (`order_id`, `customer_id`, `product_id`, `amount`) VALUES (4, 6, 3, 5);
|
52
tests/dibi/data/pgsql.sql
Normal file
52
tests/dibi/data/pgsql.sql
Normal file
@@ -0,0 +1,52 @@
|
||||
SET client_encoding = 'UTF8';
|
||||
DROP SCHEMA IF EXISTS public CASCADE;
|
||||
CREATE SCHEMA public;
|
||||
|
||||
CREATE TABLE products (
|
||||
product_id serial NOT NULL,
|
||||
title varchar(100) DEFAULT NULL,
|
||||
PRIMARY KEY (product_id)
|
||||
);
|
||||
|
||||
INSERT INTO products (product_id, title) VALUES
|
||||
(1, 'Chair'),
|
||||
(2, 'Table'),
|
||||
(3, 'Computer');
|
||||
SELECT setval('products_product_id_seq', 3, TRUE);
|
||||
|
||||
CREATE INDEX title ON products USING btree (title);
|
||||
|
||||
CREATE TABLE customers (
|
||||
customer_id serial NOT NULL,
|
||||
name varchar(100) DEFAULT NULL,
|
||||
PRIMARY KEY (customer_id)
|
||||
);
|
||||
|
||||
INSERT INTO customers (customer_id, name) VALUES
|
||||
(1, 'Dave Lister'),
|
||||
(2, 'Arnold Rimmer'),
|
||||
(3, 'The Cat'),
|
||||
(4, 'Holly'),
|
||||
(5, 'Kryten'),
|
||||
(6, 'Kristine Kochanski');
|
||||
SELECT setval('customers_customer_id_seq', 6, TRUE);
|
||||
|
||||
CREATE TABLE orders (
|
||||
order_id serial NOT NULL,
|
||||
customer_id integer NOT NULL,
|
||||
product_id integer NOT NULL,
|
||||
amount real NOT NULL
|
||||
);
|
||||
|
||||
INSERT INTO orders (order_id, customer_id, product_id, amount) VALUES
|
||||
(1, 2, 1, 7),
|
||||
(2, 2, 3, 2),
|
||||
(3, 1, 2, 3),
|
||||
(4, 6, 3, 5);
|
||||
SELECT setval('orders_order_id_seq', 4, TRUE);
|
||||
|
||||
ALTER TABLE ONLY orders
|
||||
ADD CONSTRAINT orders_customer_id_fkey FOREIGN KEY (customer_id) REFERENCES customers(customer_id) ON UPDATE CASCADE ON DELETE RESTRICT;
|
||||
|
||||
ALTER TABLE ONLY orders
|
||||
ADD CONSTRAINT orders_product_id_fkey FOREIGN KEY (product_id) REFERENCES products(product_id) ON UPDATE CASCADE ON DELETE RESTRICT;
|
34
tests/dibi/data/sqlite.sql
Normal file
34
tests/dibi/data/sqlite.sql
Normal file
@@ -0,0 +1,34 @@
|
||||
CREATE TABLE [products] (
|
||||
[product_id] INTEGER NOT NULL PRIMARY KEY,
|
||||
[title] VARCHAR(100) NULL
|
||||
);
|
||||
|
||||
CREATE INDEX "title" ON "products" ("title");
|
||||
|
||||
INSERT INTO "products" ("product_id", "title") VALUES (1, 'Chair');
|
||||
INSERT INTO "products" ("product_id", "title") VALUES (2, 'Table');
|
||||
INSERT INTO "products" ("product_id", "title") VALUES (3, 'Computer');
|
||||
|
||||
CREATE TABLE [customers] (
|
||||
[customer_id] INTEGER PRIMARY KEY NOT NULL,
|
||||
[name] VARCHAR(100) NULL
|
||||
);
|
||||
|
||||
INSERT INTO "customers" ("customer_id", "name") VALUES (1, 'Dave Lister');
|
||||
INSERT INTO "customers" ("customer_id", "name") VALUES (2, 'Arnold Rimmer');
|
||||
INSERT INTO "customers" ("customer_id", "name") VALUES (3, 'The Cat');
|
||||
INSERT INTO "customers" ("customer_id", "name") VALUES (4, 'Holly');
|
||||
INSERT INTO "customers" ("customer_id", "name") VALUES (5, 'Kryten');
|
||||
INSERT INTO "customers" ("customer_id", "name") VALUES (6, 'Kristine Kochanski');
|
||||
|
||||
CREATE TABLE [orders] (
|
||||
[order_id] INTEGER NOT NULL PRIMARY KEY,
|
||||
[customer_id] INTEGER NOT NULL,
|
||||
[product_id] INTEGER NOT NULL,
|
||||
[amount] FLOAT NOT NULL
|
||||
);
|
||||
|
||||
INSERT INTO "orders" ("order_id", "customer_id", "product_id", "amount") VALUES (1, 2, 1, '7.0');
|
||||
INSERT INTO "orders" ("order_id", "customer_id", "product_id", "amount") VALUES (2, 2, 3, '2.0');
|
||||
INSERT INTO "orders" ("order_id", "customer_id", "product_id", "amount") VALUES (3, 1, 2, '3.0');
|
||||
INSERT INTO "orders" ("order_id", "customer_id", "product_id", "amount") VALUES (4, 6, 3, '5.0');
|
75
tests/dibi/meta.phpt
Normal file
75
tests/dibi/meta.phpt
Normal file
@@ -0,0 +1,75 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* @dataProvider ../databases.ini
|
||||
*/
|
||||
|
||||
use Tester\Assert;
|
||||
|
||||
require __DIR__ . '/bootstrap.php';
|
||||
|
||||
if ($config['system'] === 'odbc' || $config['driver'] === 'pdo') {
|
||||
Tester\Environment::skip('Not supported.');
|
||||
}
|
||||
|
||||
$conn = new DibiConnection($config);
|
||||
$conn->loadFile(__DIR__ . "/data/$config[system].sql");
|
||||
|
||||
|
||||
$meta = $conn->getDatabaseInfo();
|
||||
|
||||
Assert::same(3, count($meta->getTables()));
|
||||
|
||||
$names = $meta->getTableNames();
|
||||
sort($names);
|
||||
Assert::equal(array('customers', 'orders', 'products'), $names);
|
||||
|
||||
Assert::false($meta->hasTable('xxxx'));
|
||||
|
||||
$table = $meta->getTable('products');
|
||||
Assert::same('products', $table->name);
|
||||
Assert::false($table->isView());
|
||||
|
||||
Assert::same(2, count($table->getColumns()));
|
||||
Assert::false($table->hasColumn('xxxx'));
|
||||
Assert::true($table->hasColumn('product_id'));
|
||||
Assert::true($table->hasColumn('Product_id'));
|
||||
Assert::same('product_id', $table->getColumn('Product_id')->name);
|
||||
|
||||
$column = $table->getColumn('product_id');
|
||||
Assert::same('product_id', $column->name);
|
||||
Assert::same('products', $column->table->name);
|
||||
Assert::same('i', $column->type);
|
||||
Assert::type('string', $column->nativeType);
|
||||
Assert::false($column->nullable);
|
||||
Assert::true($column->autoIncrement);
|
||||
|
||||
$column = $table->getColumn('title');
|
||||
Assert::same('title', $column->name);
|
||||
Assert::same('products', $column->table->name);
|
||||
Assert::same('s', $column->type);
|
||||
Assert::type('string', $column->nativeType);
|
||||
Assert::same(100, $column->size);
|
||||
Assert::true($column->nullable);
|
||||
Assert::false($column->autoIncrement);
|
||||
//Assert::null($column->default);
|
||||
|
||||
|
||||
$indexes = $table->getIndexes();
|
||||
$index = reset($indexes);
|
||||
|
||||
if ($config['system'] !== 'sqlite') {
|
||||
Assert::same(2, count($indexes));
|
||||
Assert::true($index->primary);
|
||||
Assert::true($index->unique);
|
||||
Assert::same(1, count($index->getColumns()));
|
||||
Assert::same('product_id', $index->columns[0]->name);
|
||||
|
||||
$index = next($indexes);
|
||||
}
|
||||
|
||||
Assert::same('title', $index->name);
|
||||
Assert::false($index->primary);
|
||||
Assert::false($index->unique);
|
||||
Assert::same(1, count($index->getColumns()));
|
||||
Assert::same('title', $index->columns[0]->name);
|
Reference in New Issue
Block a user