Skip to content

Commit

Permalink
DibiFluent::fetch() uses limit only when there is no LIMIT & OFFSET (…
Browse files Browse the repository at this point in the history
…fixes 20f2093 on MSSQL)
  • Loading branch information
dg committed Oct 26, 2015
1 parent 1c3ef5f commit 63c644a
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 18 deletions.
18 changes: 8 additions & 10 deletions dibi/libs/DibiFluent.php
Original file line number Diff line number Diff line change
Expand Up @@ -316,12 +316,11 @@ public function execute($return = NULL)
*/
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();
}


Expand All @@ -331,12 +330,11 @@ public function fetch()
*/
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();
}


Expand Down
20 changes: 13 additions & 7 deletions tests/dibi/DibiFluent.fetch.limit.phpt
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?php

/**
* @dataProvider ../databases.ini
* @dataProvider ../databases.ini !=mssql
*/

use Tester\Assert;
Expand Down Expand Up @@ -59,14 +59,20 @@ Assert::same(


$fluent->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(
Expand All @@ -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(
Expand Down
2 changes: 1 addition & 1 deletion tests/dibi/DibiFluent.select.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ try {
} catch (Exception $e) {
}
Assert::same(
reformat('SELECT * FROM [table] LIMIT 1'),
reformat(' SELECT * FROM [table] LIMIT 1'),
dibi::$sql
);

Expand Down

0 comments on commit 63c644a

Please sign in to comment.