Skip to content

Commit

Permalink
Fluent::fetch(): fixed limit clause duplication [Closes #188][Closes #…
Browse files Browse the repository at this point in the history
…186][Closes #185]
  • Loading branch information
castamir authored and dg committed Oct 9, 2015
1 parent 0c4a289 commit 20f2093
Show file tree
Hide file tree
Showing 3 changed files with 103 additions and 9 deletions.
18 changes: 10 additions & 8 deletions src/Dibi/Fluent.php
Original file line number Diff line number Diff line change
Expand Up @@ -317,11 +317,12 @@ public function execute($return = NULL)
*/
public function fetch()
{
if ($this->command === 'SELECT') {
return $this->query($this->_export(NULL, ['%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();
}


Expand All @@ -331,11 +332,12 @@ public function fetch()
*/
public function fetchSingle()
{
if ($this->command === 'SELECT') {
return $this->query($this->_export(NULL, ['%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();
}


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

/**
* @dataProvider ../databases.ini
*/

use Tester\Assert;

require __DIR__ . '/bootstrap.php';

$conn = new Dibi\Connection($config);
$conn->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
);
2 changes: 1 addition & 1 deletion tests/dibi/Fluent.select.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,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 20f2093

Please sign in to comment.