From f3cb49738bed8fc4680d71273cb52a71d8f6776a Mon Sep 17 00:00:00 2001 From: JurianArie <28654085+JurianArie@users.noreply.github.com> Date: Thu, 22 Aug 2024 18:24:22 +0200 Subject: [PATCH 1/2] Add skip total records back --- src/DataTableAbstract.php | 8 ++++++-- src/QueryDataTable.php | 21 +-------------------- tests/Integration/QueryDataTableTest.php | 24 +++++++++++++++++++++++- 3 files changed, 30 insertions(+), 23 deletions(-) diff --git a/src/DataTableAbstract.php b/src/DataTableAbstract.php index d0c3ba21..c4667d05 100644 --- a/src/DataTableAbstract.php +++ b/src/DataTableAbstract.php @@ -68,6 +68,11 @@ abstract class DataTableAbstract implements DataTable */ protected ?int $filteredRecords = null; + /** + * Flag to check if the total records count should be skipped. + */ + protected bool $skipTotalRecords = false; + /** * Auto-filter flag. */ @@ -533,12 +538,11 @@ public function setTotalRecords(int $total): static * This will improve the performance by skipping the total count query. * * @return $this - * - * @deprecated Just use setTotalRecords instead. */ public function skipTotalRecords(): static { $this->totalRecords = 0; + $this->skipTotalRecords = true; return $this; } diff --git a/src/QueryDataTable.php b/src/QueryDataTable.php index aea42844..7856a03f 100644 --- a/src/QueryDataTable.php +++ b/src/QueryDataTable.php @@ -25,11 +25,6 @@ class QueryDataTable extends DataTableAbstract */ protected bool $prepared = false; - /** - * Flag to check if the total records count query has been performed. - */ - protected bool $performedTotalRecordsCount = false; - /** * Query callback for custom pagination using limit without offset. * @@ -162,20 +157,6 @@ public function prepareQuery(): static return $this; } - /** - * Count total items. - */ - public function totalCount(): int - { - if ($this->totalRecords !== null) { - return $this->totalRecords; - } - - $this->performedTotalRecordsCount = true; - - return $this->totalRecords = $this->count(); - } - /** * Counts current query. */ @@ -272,7 +253,7 @@ protected function filterRecords(): void // If no modification between the original query and the filtered one has been made // the filteredRecords equals the totalRecords - if ($this->query == $initialQuery && $this->performedTotalRecordsCount) { + if (! $this->skipTotalRecords && $this->query == $initialQuery) { $this->filteredRecords ??= $this->totalRecords; } else { $this->filteredCount(); diff --git a/tests/Integration/QueryDataTableTest.php b/tests/Integration/QueryDataTableTest.php index d2b4a028..b5afba81 100644 --- a/tests/Integration/QueryDataTableTest.php +++ b/tests/Integration/QueryDataTableTest.php @@ -26,7 +26,7 @@ public function it_can_set_total_records() $crawler->assertJson([ 'draw' => 0, 'recordsTotal' => 10, - 'recordsFiltered' => 20, + 'recordsFiltered' => 10, ]); } @@ -34,11 +34,29 @@ public function it_can_set_total_records() public function it_can_set_zero_total_records() { $crawler = $this->call('GET', '/zero-total-records'); + $crawler->assertJson([ + 'draw' => 0, + 'recordsTotal' => 0, + 'recordsFiltered' => 0, + ]); + } + + #[Test] + public function it_can_set_skip_total_records() + { + DB::enableQueryLog(); + + $crawler = $this->call('GET', '/skip-total-records'); $crawler->assertJson([ 'draw' => 0, 'recordsTotal' => 0, 'recordsFiltered' => 20, ]); + + DB::disableQueryLog(); + $queryLog = DB::getQueryLog(); + + $this->assertCount(2, $queryLog); } #[Test] @@ -463,6 +481,10 @@ protected function setUp(): void ->setTotalRecords(0) ->toJson()); + $router->get('/skip-total-records', fn (DataTables $dataTable) => $dataTable->query(DB::table('users')) + ->skipTotalRecords() + ->toJson()); + $router->get('/set-filtered-records', fn (DataTables $dataTable) => $dataTable->query(DB::table('users')) ->setFilteredRecords(10) ->toJson()); From 5a56e317a7eb485cd0cdba883d1613a08e0b6c7d Mon Sep 17 00:00:00 2001 From: JurianArie <28654085+JurianArie@users.noreply.github.com> Date: Sat, 24 Aug 2024 10:04:41 +0200 Subject: [PATCH 2/2] Set total records equal to filtered records when skipping total records --- src/QueryDataTable.php | 4 ++ tests/Integration/QueryDataTableTest.php | 49 +++++++++++++----------- 2 files changed, 31 insertions(+), 22 deletions(-) diff --git a/src/QueryDataTable.php b/src/QueryDataTable.php index 7856a03f..946f1ccf 100644 --- a/src/QueryDataTable.php +++ b/src/QueryDataTable.php @@ -257,6 +257,10 @@ protected function filterRecords(): void $this->filteredRecords ??= $this->totalRecords; } else { $this->filteredCount(); + + if ($this->skipTotalRecords) { + $this->totalRecords = $this->filteredRecords; + } } } diff --git a/tests/Integration/QueryDataTableTest.php b/tests/Integration/QueryDataTableTest.php index b5afba81..24aeb8f2 100644 --- a/tests/Integration/QueryDataTableTest.php +++ b/tests/Integration/QueryDataTableTest.php @@ -41,24 +41,6 @@ public function it_can_set_zero_total_records() ]); } - #[Test] - public function it_can_set_skip_total_records() - { - DB::enableQueryLog(); - - $crawler = $this->call('GET', '/skip-total-records'); - $crawler->assertJson([ - 'draw' => 0, - 'recordsTotal' => 0, - 'recordsFiltered' => 20, - ]); - - DB::disableQueryLog(); - $queryLog = DB::getQueryLog(); - - $this->assertCount(2, $queryLog); - } - #[Test] public function it_can_set_total_filtered_records() { @@ -109,7 +91,27 @@ public function it_can_perform_global_search() #[Test] public function it_can_skip_total_records_count_query() { - $crawler = $this->call('GET', '/query/simple', [ + DB::enableQueryLog(); + + $crawler = $this->call('GET', '/skip-total-records'); + $crawler->assertJson([ + 'draw' => 0, + 'recordsTotal' => 20, + 'recordsFiltered' => 20, + ]); + + DB::disableQueryLog(); + $queryLog = DB::getQueryLog(); + + $this->assertCount(2, $queryLog); + } + + #[Test] + public function it_can_skip_total_records_count_query_with_filter_applied() + { + DB::enableQueryLog(); + + $crawler = $this->call('GET', '/skip-total-records', [ 'columns' => [ ['data' => 'name', 'name' => 'name', 'searchable' => 'true', 'orderable' => 'true'], ['data' => 'email', 'name' => 'email', 'searchable' => 'true', 'orderable' => 'true'], @@ -119,9 +121,14 @@ public function it_can_skip_total_records_count_query() $crawler->assertJson([ 'draw' => 0, - 'recordsTotal' => 0, + 'recordsTotal' => 1, 'recordsFiltered' => 1, ]); + + DB::disableQueryLog(); + $queryLog = DB::getQueryLog(); + + $this->assertCount(2, $queryLog); } #[Test] @@ -411,8 +418,6 @@ protected function setUp(): void ->formatColumn('created_at', new DateFormatter('Y-m-d')) ->toJson()); - $router->get('/query/simple', fn (DataTables $dataTable) => $dataTable->query(DB::table('users'))->skipTotalRecords()->toJson()); - $router->get('/query/addColumn', fn (DataTables $dataTable) => $dataTable->query(DB::table('users')) ->addColumn('foo', 'bar') ->toJson());