Skip to content

Commit 3ce7eb9

Browse files
committed
Merge pull request #99 from dg/pull-order
SqlBuilder: queries with limit/offset are automatically ordered
2 parents 066186b + 8fcd4cf commit 3ce7eb9

File tree

3 files changed

+14
-7
lines changed

3 files changed

+14
-7
lines changed

src/Database/Table/SqlBuilder.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,13 @@ public function buildDeleteQuery()
124124
*/
125125
public function buildSelectQuery($columns = NULL)
126126
{
127+
if (!$this->order && ($this->limit !== NULL || $this->offset)) {
128+
$this->order = array_map(
129+
function ($col) { return "$this->tableName.$col"; },
130+
(array) $this->conventions->getPrimary($this->tableName)
131+
);
132+
}
133+
127134
$queryCondition = $this->buildConditions();
128135
$queryEnd = $this->buildQueryEnd();
129136

tests/Database/Table/Table.limit.phpt

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,12 @@ Nette\Database\Helpers::loadFromFile($connection, __DIR__ . "/../files/{$driverN
1313

1414

1515
Assert::same(
16-
reformat('SELECT * FROM [author] LIMIT 2'),
16+
reformat('SELECT * FROM [author] ORDER BY [author].[id] LIMIT 2'),
1717
$context->table('author')->limit(2)->getSql()
1818
);
1919

2020
Assert::same(
21-
reformat('SELECT * FROM [author] LIMIT 2 OFFSET 10'),
21+
reformat('SELECT * FROM [author] ORDER BY [author].[id] LIMIT 2 OFFSET 10'),
2222
$context->table('author')->limit(2, 10)->getSql()
2323
);
2424

@@ -28,24 +28,24 @@ Assert::same(
2828
);
2929

3030
Assert::same(
31-
reformat('SELECT * FROM [author] LIMIT 10'),
31+
reformat('SELECT * FROM [author] ORDER BY [author].[id] LIMIT 10'),
3232
$context->table('author')->page(1, 10)->getSql()
3333
);
3434

3535
Assert::same(
36-
reformat('SELECT * FROM [author] LIMIT 0'),
36+
reformat('SELECT * FROM [author] ORDER BY [author].[id] LIMIT 0'),
3737
$context->table('author')->page(0, 10, $count)->getSql()
3838
);
3939
Assert::same(1, $count);
4040

4141
Assert::same(
42-
reformat('SELECT * FROM [author] LIMIT 10 OFFSET 10'),
42+
reformat('SELECT * FROM [author] ORDER BY [author].[id] LIMIT 10 OFFSET 10'),
4343
$context->table('author')->page(2, 10, $count)->getSql()
4444
);
4545
Assert::same(1, $count);
4646

4747
Assert::same(
48-
reformat('SELECT * FROM [author] LIMIT 2 OFFSET 2'),
48+
reformat('SELECT * FROM [author] ORDER BY [author].[id] LIMIT 2 OFFSET 2'),
4949
$context->table('author')->page(2, 2, $count)->getSql()
5050
);
5151
Assert::same(2, $count);

tests/Database/Table/bugs/bug1356.phpt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,4 +24,4 @@ foreach ($books as $book) {
2424
$book->title;
2525
}
2626

27-
Assert::same(reformat('SELECT * FROM [book] LIMIT 1'), $books->getSql());
27+
Assert::same(reformat('SELECT * FROM [book] ORDER BY [book].[id] LIMIT 1'), $books->getSql());

0 commit comments

Comments
 (0)