Skip to content

Commit

Permalink
remove order by in count queries
Browse files Browse the repository at this point in the history
[closes #529]
  • Loading branch information
mrceperka authored and hrach committed Nov 9, 2024
1 parent 9569d36 commit 8785299
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 7 deletions.
8 changes: 7 additions & 1 deletion src/Collection/DbalCollection.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use Nextras\Dbal\IConnection;
use Nextras\Dbal\Platforms\Data\Fqn;
use Nextras\Dbal\Platforms\MySqlPlatform;
use Nextras\Dbal\Platforms\SqlServerPlatform;
use Nextras\Dbal\QueryBuilder\QueryBuilder;
use Nextras\Orm\Collection\Expression\ExpressionContext;
use Nextras\Orm\Collection\Functions\Result\DbalExpressionResult;
Expand Down Expand Up @@ -349,7 +350,12 @@ protected function getIteratorCount(): int
{
if ($this->resultCount === null) {
$builder = clone $this->getQueryBuilder();
if (!$builder->hasLimitOffsetClause()) {

if ($this->connection->getPlatform()->getName() === SqlServerPlatform::NAME) {
if (!$builder->hasLimitOffsetClause()) {
$builder->orderBy(null);
}
} else {
$builder->orderBy(null);
}

Expand Down
12 changes: 12 additions & 0 deletions tests/cases/integration/Collection/collection.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@
namespace NextrasTests\Orm\Integration\Collection;


use Nextras\Dbal\Connection;
use Nextras\Dbal\Drivers\Exception\DriverException;
use Nextras\Dbal\ILogger;
use Nextras\Dbal\Result\Result;
use Nextras\Orm\Collection\ArrayCollection;
use Nextras\Orm\Collection\DbalCollection;
use Nextras\Orm\Collection\EmptyCollection;
Expand Down Expand Up @@ -316,6 +320,14 @@ class CollectionTest extends DataTestCase
}


public function testCountStoredDbalWithoutOrderByClause(): void
{
$collection = $this->orm->books->findAll()->orderBy('title');
Assert::same(4, $collection->countStored());
Assert::same(4, $collection->limitBy(10, 0)->countStored());
}


public function testToArrayCollection(): void
{
$c1 = $this->orm->authors->findAll();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
SELECT "books".* FROM "books" AS "books" ORDER BY "books"."id" ASC LIMIT 1 OFFSET 1;
SELECT "books".* FROM "books" AS "books" ORDER BY "books"."id" ASC LIMIT 1 OFFSET 10;
SELECT COUNT(*) AS count FROM (SELECT "books"."id" FROM "books" AS "books" ORDER BY "books"."id" ASC LIMIT 1 OFFSET 1) temp;
SELECT COUNT(*) AS count FROM (SELECT "books"."id" FROM "books" AS "books" ORDER BY "books"."id" ASC LIMIT 1 OFFSET 10) temp;
SELECT COUNT(*) AS count FROM (SELECT "books"."id" FROM "books" AS "books" LIMIT 1 OFFSET 1) temp;
SELECT COUNT(*) AS count FROM (SELECT "books"."id" FROM "books" AS "books" LIMIT 1 OFFSET 10) temp;
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,6 @@ FROM
)
WHERE
"author"."name" = 'Writer 1'
ORDER BY
"books"."id" ASC
LIMIT
5
) temp;
Expand All @@ -31,8 +29,6 @@ FROM
)
WHERE
"tag"."name" = 'Tag 1'
ORDER BY
"tag_followers"."tag_id" ASC
LIMIT
3
) temp;
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
SELECT COUNT(*) AS count FROM (SELECT "books"."id" FROM "books" AS "books") temp;
SELECT COUNT(*) AS count FROM (SELECT "books"."id" FROM "books" AS "books" LIMIT 10 OFFSET 0) temp;

0 comments on commit 8785299

Please sign in to comment.