diff --git a/dibi/libs/DibiFluent.php b/dibi/libs/DibiFluent.php index 26437958..038c1b36 100644 --- a/dibi/libs/DibiFluent.php +++ b/dibi/libs/DibiFluent.php @@ -315,11 +315,12 @@ class DibiFluent extends DibiObject implements IDataSource */ public function fetch() { - if ($this->command === 'SELECT') { - return $this->query($this->_export(NULL, array('%lmt', 1)))->fetch(); - } else { - return $this->query($this->_export())->fetch(); + if ($this->command === 'SELECT' && !$this->clauses['LIMIT']) { + $result = $this->query($this->limit(1)->_export())->fetch(); + $this->removeClause('LIMIT'); + return $result; } + return $this->query($this->_export())->fetch(); } @@ -329,11 +330,12 @@ class DibiFluent extends DibiObject implements IDataSource */ public function fetchSingle() { - if ($this->command === 'SELECT') { - return $this->query($this->_export(NULL, array('%lmt', 1)))->fetchSingle(); - } else { - return $this->query($this->_export())->fetchSingle(); + if ($this->command === 'SELECT' && !$this->clauses['LIMIT']) { + $result = $this->query($this->limit(1)->_export())->fetchSingle(); + $this->removeClause('LIMIT'); + return $result; } + return $this->query($this->_export())->fetchSingle(); } diff --git a/tests/dibi/DibiFluent.fetch.limit.phpt b/tests/dibi/DibiFluent.fetch.limit.phpt new file mode 100644 index 00000000..548d0312 --- /dev/null +++ b/tests/dibi/DibiFluent.fetch.limit.phpt @@ -0,0 +1,92 @@ +loadFile(__DIR__ . "/data/$config[system].sql"); + + +// fetch & limit +$fluent = $conn->select('*') + ->from('customers') + ->limit(1) + ->offset(3) + ->orderBy('customer_id'); + +Assert::same( + reformat('SELECT * FROM [customers] ORDER BY [customer_id] LIMIT 1 OFFSET 3'), + (string) $fluent +); + + +$fluent->fetch(); +Assert::same( + reformat('SELECT * FROM [customers] ORDER BY [customer_id] LIMIT 1 OFFSET 3'), + dibi::$sql +); +$fluent->fetchSingle(); +Assert::same( + reformat('SELECT * FROM [customers] ORDER BY [customer_id] LIMIT 1 OFFSET 3'), + dibi::$sql +); +Assert::same( + reformat('SELECT * FROM [customers] ORDER BY [customer_id] LIMIT 1 OFFSET 3'), + (string) $fluent +); + + +$fluent->limit(0); +$fluent->fetch(); +Assert::same( + reformat('SELECT * FROM [customers] ORDER BY [customer_id] LIMIT 0 OFFSET 3'), + dibi::$sql +); +$fluent->fetchSingle(); +Assert::same( + reformat('SELECT * FROM [customers] ORDER BY [customer_id] LIMIT 0 OFFSET 3'), + dibi::$sql +); +Assert::same( + reformat('SELECT * FROM [customers] ORDER BY [customer_id] LIMIT 0 OFFSET 3'), + (string) $fluent +); + + +$fluent->removeClause('limit'); +$fluent->fetch(); +Assert::same( + reformat('SELECT * FROM [customers] ORDER BY [customer_id] LIMIT 1 OFFSET 3'), + dibi::$sql +); +$fluent->fetchSingle(); +Assert::same( + reformat('SELECT * FROM [customers] ORDER BY [customer_id] LIMIT 1 OFFSET 3'), + dibi::$sql +); +Assert::same( + reformat('SELECT * FROM [customers] ORDER BY [customer_id] OFFSET 3'), + (string) $fluent +); + + +$fluent->removeClause('offset'); +$fluent->fetch(); +Assert::same( + 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'), + dibi::$sql +); +Assert::same( + reformat('SELECT * FROM [customers] ORDER BY [customer_id]'), + (string) $fluent +); diff --git a/tests/dibi/DibiFluent.select.phpt b/tests/dibi/DibiFluent.select.phpt index 617d02ab..1b58a065 100644 --- a/tests/dibi/DibiFluent.select.phpt +++ b/tests/dibi/DibiFluent.select.phpt @@ -81,7 +81,7 @@ try { } catch (Exception $e) { } Assert::same( - reformat(' SELECT * FROM [table] LIMIT 1'), + reformat('SELECT * FROM [table] LIMIT 1'), dibi::$sql );