diff --git a/dibi/libs/DibiFluent.php b/dibi/libs/DibiFluent.php index 266d954b..d13bff76 100644 --- a/dibi/libs/DibiFluent.php +++ b/dibi/libs/DibiFluent.php @@ -316,12 +316,11 @@ class DibiFluent extends DibiObject implements IDataSource */ public function fetch() { - if ($this->command === 'SELECT' && !$this->clauses['LIMIT']) { - $result = $this->query($this->limit(1)->_export())->fetch(); - $this->removeClause('LIMIT'); - return $result; + if ($this->command === 'SELECT' && !$this->clauses['LIMIT'] && !$this->clauses['OFFSET']) { + return $this->query($this->_export(NULL, array('%lmt', 1)))->fetch(); + } else { + 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() { - if ($this->command === 'SELECT' && !$this->clauses['LIMIT']) { - $result = $this->query($this->limit(1)->_export())->fetchSingle(); - $this->removeClause('LIMIT'); - return $result; + if ($this->command === 'SELECT' && !$this->clauses['LIMIT'] && !$this->clauses['OFFSET']) { + return $this->query($this->_export(NULL, array('%lmt', 1)))->fetchSingle(); + } else { + return $this->query($this->_export())->fetchSingle(); } - return $this->query($this->_export())->fetchSingle(); } diff --git a/tests/dibi/DibiFluent.fetch.limit.phpt b/tests/dibi/DibiFluent.fetch.limit.phpt index 548d0312..a1fe1bcb 100644 --- a/tests/dibi/DibiFluent.fetch.limit.phpt +++ b/tests/dibi/DibiFluent.fetch.limit.phpt @@ -1,7 +1,7 @@ removeClause('limit'); -$fluent->fetch(); +try { + $fluent->fetch(); +} catch (DibiException $e) { +} 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 ); -$fluent->fetchSingle(); +try { + $fluent->fetchSingle(); +} catch (DibiException $e) { +} 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 ); Assert::same( @@ -78,12 +84,12 @@ Assert::same( $fluent->removeClause('offset'); $fluent->fetch(); Assert::same( - reformat('SELECT * FROM [customers] ORDER BY [customer_id] LIMIT 1'), + reformat(' SELECT * FROM [customers] ORDER BY [customer_id] LIMIT 1'), dibi::$sql ); $fluent->fetchSingle(); Assert::same( - reformat('SELECT * FROM [customers] ORDER BY [customer_id] LIMIT 1'), + reformat(' SELECT * FROM [customers] ORDER BY [customer_id] LIMIT 1'), dibi::$sql ); Assert::same( diff --git a/tests/dibi/DibiFluent.select.phpt b/tests/dibi/DibiFluent.select.phpt index dd03bd10..95c573e5 100644 --- a/tests/dibi/DibiFluent.select.phpt +++ b/tests/dibi/DibiFluent.select.phpt @@ -85,7 +85,7 @@ try { } catch (Exception $e) { } Assert::same( - reformat('SELECT * FROM [table] LIMIT 1'), + reformat(' SELECT * FROM [table] LIMIT 1'), dibi::$sql );