1
0
mirror of https://github.com/dg/dibi.git synced 2025-08-05 05:37:39 +02:00

Fluent: exports limit & offset as %lmt and %ofs [Closes #82]

Also fixes 20f2093 on MSSQL
This commit is contained in:
David Grudl
2015-10-26 15:25:52 +01:00
parent 70c68402ea
commit c26201c75d
4 changed files with 141 additions and 23 deletions

View File

@@ -319,12 +319,11 @@ class Fluent implements IDataSource
public function fetch() public function fetch()
{ {
if ($this->command === 'SELECT' && !$this->clauses['LIMIT']) { if ($this->command === 'SELECT' && !$this->clauses['LIMIT']) {
$result = $this->query($this->limit(1)->_export())->fetch(); return $this->query($this->_export(NULL, ['%lmt', 1]))->fetch();
$this->removeClause('LIMIT'); } else {
return $result;
}
return $this->query($this->_export())->fetch(); return $this->query($this->_export())->fetch();
} }
}
/** /**
@@ -334,12 +333,11 @@ class Fluent implements IDataSource
public function fetchSingle() public function fetchSingle()
{ {
if ($this->command === 'SELECT' && !$this->clauses['LIMIT']) { if ($this->command === 'SELECT' && !$this->clauses['LIMIT']) {
$result = $this->query($this->limit(1)->_export())->fetchSingle(); return $this->query($this->_export(NULL, ['%lmt', 1]))->fetchSingle();
$this->removeClause('LIMIT'); } else {
return $result;
}
return $this->query($this->_export())->fetchSingle(); return $this->query($this->_export())->fetchSingle();
} }
}
/** /**
@@ -459,6 +457,10 @@ class Fluent implements IDataSource
{ {
if ($clause === NULL) { if ($clause === NULL) {
$data = $this->clauses; $data = $this->clauses;
if ($this->command === 'SELECT' && ($data['LIMIT'] || $data['OFFSET'])) {
$args = array_merge(['%lmt %ofs', $data['LIMIT'][0], $data['OFFSET'][0]], $args);
unset($data['LIMIT'], $data['OFFSET']);
}
} else { } else {
$clause = self::$normalizer->$clause; $clause = self::$normalizer->$clause;

View File

@@ -0,0 +1,101 @@
<?php
use Tester\Assert;
require __DIR__ . '/bootstrap.php';
class MockDriver extends Dibi\Drivers\SqlsrvDriver
{
function __construct()
{}
function connect(array & $config)
{}
function query($sql)
{
return $this;
}
function getResultColumns()
{
return [];
}
function fetch($assoc)
{
return FALSE;
}
}
$config['driver'] = new MockDriver;
$conn = new Dibi\Connection($config);
// fetch & limit
$fluent = $conn->select('*')
->from('customers')
->limit(1)
->orderBy('customer_id');
Assert::same(
reformat('SELECT TOP 1 * FROM ( SELECT * FROM [customers] ORDER BY [customer_id]) AS T '),
(string) $fluent
);
$fluent->fetch();
Assert::same(
'SELECT TOP 1 * FROM ( SELECT * FROM [customers] ORDER BY [customer_id]) AS T ',
dibi::$sql
);
$fluent->fetchSingle();
Assert::same(
reformat('SELECT TOP 1 * FROM ( SELECT * FROM [customers] ORDER BY [customer_id]) AS T '),
dibi::$sql
);
$fluent->fetchAll(0, 3);
Assert::same(
reformat('SELECT TOP 3 * FROM ( SELECT * FROM [customers] ORDER BY [customer_id]) AS T '),
dibi::$sql
);
Assert::same(
reformat('SELECT TOP 1 * FROM ( SELECT * FROM [customers] ORDER BY [customer_id]) AS T '),
(string) $fluent
);
$fluent->limit(0);
$fluent->fetch();
Assert::same(
reformat('SELECT TOP 0 * FROM ( SELECT * FROM [customers] ORDER BY [customer_id]) AS T '),
dibi::$sql
);
$fluent->fetchSingle();
Assert::same(
reformat('SELECT TOP 0 * FROM ( SELECT * FROM [customers] ORDER BY [customer_id]) AS T '),
dibi::$sql
);
Assert::same(
reformat('SELECT TOP 0 * FROM ( SELECT * FROM [customers] ORDER BY [customer_id]) AS T '),
(string) $fluent
);
$fluent->removeClause('limit');
$fluent->removeClause('offset');
$fluent->fetch();
Assert::same(
reformat('SELECT TOP 1 * FROM ( SELECT * FROM [customers] ORDER BY [customer_id]) AS T '),
dibi::$sql
);
$fluent->fetchSingle();
Assert::same(
reformat('SELECT TOP 1 * FROM ( SELECT * FROM [customers] ORDER BY [customer_id]) AS T '),
dibi::$sql
);
Assert::same(
reformat('SELECT * FROM [customers] ORDER BY [customer_id]'),
(string) $fluent
);

View File

@@ -1,7 +1,7 @@
<?php <?php
/** /**
* @dataProvider ../databases.ini * @dataProvider ../databases.ini mysql
*/ */
use Tester\Assert; use Tester\Assert;
@@ -35,6 +35,11 @@ Assert::same(
reformat(' SELECT * FROM [customers] ORDER BY [customer_id] LIMIT 1 OFFSET 3'), reformat(' SELECT * FROM [customers] ORDER BY [customer_id] LIMIT 1 OFFSET 3'),
dibi::$sql dibi::$sql
); );
$fluent->fetchAll(2, 3);
Assert::same(
reformat(' SELECT * FROM [customers] ORDER BY [customer_id] LIMIT 3 OFFSET 2'),
dibi::$sql
);
Assert::same( Assert::same(
reformat(' SELECT * FROM [customers] ORDER BY [customer_id] LIMIT 1 OFFSET 3'), reformat(' SELECT * FROM [customers] ORDER BY [customer_id] LIMIT 1 OFFSET 3'),
(string) $fluent (string) $fluent
@@ -70,7 +75,7 @@ Assert::same(
dibi::$sql dibi::$sql
); );
Assert::same( Assert::same(
reformat('SELECT * FROM [customers] ORDER BY [customer_id] OFFSET 3'), reformat(' SELECT * FROM [customers] ORDER BY [customer_id] LIMIT 18446744073709551615 OFFSET 3'),
(string) $fluent (string) $fluent
); );

View File

@@ -102,7 +102,7 @@ $fluent = $conn->select('*')
->offset(0); ->offset(0);
Assert::same( 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'), 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'),
(string) $fluent (string) $fluent
); );
@@ -141,3 +141,13 @@ Assert::same(
reformat('SELECT * FROM [me] AS [t] WHERE col > 10 AND ([x] = \'a\') AND (b) AND (c)'), reformat('SELECT * FROM [me] AS [t] WHERE col > 10 AND ([x] = \'a\') AND (b) AND (c)'),
(string) $fluent (string) $fluent
); );
$fluent = $conn->select('*')
->limit(' 1; DROP TABLE users')
->offset(' 1; DROP TABLE users');
Assert::same(
reformat(' SELECT * LIMIT 1 OFFSET 1'),
(string) $fluent
);