Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 11 additions & 13 deletions src/DataSource/NetteDatabaseTableDataSource.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@
use Contributte\Datagrid\Filter\FilterSelect;
use Contributte\Datagrid\Filter\FilterText;
use Contributte\Datagrid\Utils\DateTimeHelper;
use Contributte\Datagrid\Utils\NetteDatabaseSelectionHelper;
use Contributte\Datagrid\Utils\Sorting;
use LogicException;
use Nette\Database\Table\Selection;
use Nette\Utils\Strings;

class NetteDatabaseTableDataSource extends FilterableDataSource implements IDataSource
{
Expand All @@ -29,25 +29,23 @@ public function getCount(): int
{
$dataSourceSqlBuilder = $this->dataSource->getSqlBuilder();

if ($dataSourceSqlBuilder->getGroup() !== '') {
$query = sprintf('SELECT COUNT(*) FROM (%s) AS datagrid_count', $this->dataSource->getSql());
$explorer = NetteDatabaseSelectionHelper::getContext($this->dataSource);

/** @phpstan-ignore argument.type */
$result = $explorer->query($query, ...$dataSourceSqlBuilder->getParameters());

return (int) $result->fetchField();
}

try {
$primary = $this->dataSource->getPrimary();

} catch (LogicException) {
if ($dataSourceSqlBuilder->getGroup() !== '') {
return $this->dataSource->count(
'DISTINCT ' . Strings::replace($dataSourceSqlBuilder->getGroup(), '~ (DESC|ASC)~')
);
}

return $this->dataSource->count('*');
}

if ($dataSourceSqlBuilder->getGroup() !== '') {
return $this->dataSource->count(
'DISTINCT ' . Strings::replace($dataSourceSqlBuilder->getGroup(), '~ (DESC|ASC)~')
);
}

return $this->dataSource->count(
$this->dataSource->getName() . '.' . (is_array($primary) ? reset($primary) : $primary)
);
Expand Down
33 changes: 33 additions & 0 deletions tests/Cases/DataSources/NetteDatabaseTableDataSourceTest.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ use Nette\Database\Conventions\DiscoveredConventions;
use Nette\Database\Explorer;
use Nette\Database\Structure;
use Nette\Database\Table\Selection;
use Tester\Assert;

require __DIR__ . '/BaseDataSourceTest.phpt';

Expand All @@ -27,6 +28,38 @@ final class NetteDatabaseTableDataSourceTest extends BaseDataSourceTest
$this->grid = $factory->createTestingDatagrid();
}

public function testGetCountWithGroupByMultipleColumns(): void
{
$this->db->query('CREATE TABLE orders_products (
id INTEGER PRIMARY KEY AUTOINCREMENT,
title VARCHAR (50),
variant VARCHAR (50),
course_city VARCHAR (50),
course_date VARCHAR (50)
);
');
$this->db->getStructure()->rebuild();

$rows = [
['title' => 'A', 'variant' => 'x', 'course_city' => null, 'course_date' => null],
['title' => 'A', 'variant' => 'x', 'course_city' => null, 'course_date' => null],
['title' => 'B', 'variant' => 'y', 'course_city' => null, 'course_date' => null],
['title' => 'C', 'variant' => 'z', 'course_city' => 'Prague', 'course_date' => '2021-01-01'],
];

foreach ($rows as $row) {
$this->db->query('INSERT INTO orders_products', $row);
}

$selection = $this->db->table('orders_products')
->group('title, variant, course_city, course_date');

$ds = new NetteDatabaseTableDataSource($selection, 'id');

Assert::same(3, $ds->getCount());
Assert::same(3, count($ds->getData()));
}

protected function setUpDatabase(): void
{
$connection = new Connection('sqlite::memory:');
Expand Down
Loading