1
0
mirror of https://github.com/dg/dibi.git synced 2025-08-07 06:36:44 +02:00

DibiFluent::fetch() uses limit only when there is no LIMIT & OFFSET (fixes 20f2093 on MSSQL)

This commit is contained in:
David Grudl
2015-10-26 15:25:52 +01:00
parent 1c3ef5f5cf
commit 63c644a860
3 changed files with 22 additions and 18 deletions

View File

@@ -316,12 +316,11 @@ class DibiFluent extends DibiObject implements IDataSource
*/ */
public function fetch() public function fetch()
{ {
if ($this->command === 'SELECT' && !$this->clauses['LIMIT']) { if ($this->command === 'SELECT' && !$this->clauses['LIMIT'] && !$this->clauses['OFFSET']) {
$result = $this->query($this->limit(1)->_export())->fetch(); return $this->query($this->_export(NULL, array('%lmt', 1)))->fetch();
$this->removeClause('LIMIT'); } else {
return $result; return $this->query($this->_export())->fetch();
} }
return $this->query($this->_export())->fetch();
} }
@@ -331,12 +330,11 @@ class DibiFluent extends DibiObject implements IDataSource
*/ */
public function fetchSingle() public function fetchSingle()
{ {
if ($this->command === 'SELECT' && !$this->clauses['LIMIT']) { if ($this->command === 'SELECT' && !$this->clauses['LIMIT'] && !$this->clauses['OFFSET']) {
$result = $this->query($this->limit(1)->_export())->fetchSingle(); return $this->query($this->_export(NULL, array('%lmt', 1)))->fetchSingle();
$this->removeClause('LIMIT'); } else {
return $result; return $this->query($this->_export())->fetchSingle();
} }
return $this->query($this->_export())->fetchSingle();
} }

View File

@@ -1,7 +1,7 @@
<?php <?php
/** /**
* @dataProvider ../databases.ini * @dataProvider ../databases.ini !=mssql
*/ */
use Tester\Assert; use Tester\Assert;
@@ -59,14 +59,20 @@ Assert::same(
$fluent->removeClause('limit'); $fluent->removeClause('limit');
$fluent->fetch(); try {
$fluent->fetch();
} catch (DibiException $e) {
}
Assert::same( Assert::same(
reformat('SELECT * FROM [customers] ORDER BY [customer_id] LIMIT 1 OFFSET 3'), reformat('SELECT * FROM [customers] ORDER BY [customer_id] OFFSET 3'),
dibi::$sql dibi::$sql
); );
$fluent->fetchSingle(); try {
$fluent->fetchSingle();
} catch (DibiException $e) {
}
Assert::same( Assert::same(
reformat('SELECT * FROM [customers] ORDER BY [customer_id] LIMIT 1 OFFSET 3'), reformat('SELECT * FROM [customers] ORDER BY [customer_id] OFFSET 3'),
dibi::$sql dibi::$sql
); );
Assert::same( Assert::same(
@@ -78,12 +84,12 @@ Assert::same(
$fluent->removeClause('offset'); $fluent->removeClause('offset');
$fluent->fetch(); $fluent->fetch();
Assert::same( Assert::same(
reformat('SELECT * FROM [customers] ORDER BY [customer_id] LIMIT 1'), reformat(' SELECT * FROM [customers] ORDER BY [customer_id] LIMIT 1'),
dibi::$sql dibi::$sql
); );
$fluent->fetchSingle(); $fluent->fetchSingle();
Assert::same( Assert::same(
reformat('SELECT * FROM [customers] ORDER BY [customer_id] LIMIT 1'), reformat(' SELECT * FROM [customers] ORDER BY [customer_id] LIMIT 1'),
dibi::$sql dibi::$sql
); );
Assert::same( Assert::same(

View File

@@ -85,7 +85,7 @@ try {
} catch (Exception $e) { } catch (Exception $e) {
} }
Assert::same( Assert::same(
reformat('SELECT * FROM [table] LIMIT 1'), reformat(' SELECT * FROM [table] LIMIT 1'),
dibi::$sql dibi::$sql
); );