Koha-clms/Koha/REST/Plugin
Tomás Cohen Arazi e51334604f
Bug 41950: Make +count embeds sortable via SQL COUNT subqueries
Currently, +count embeds (e.g. claims+count) are computed in Perl via
$object->$relation->count during to_api serialization. This means they
cannot be used in _order_by and each count triggers a separate SQL query
per row (N+1).

This patch makes dbic_merge_prefetch detect +count embeds and, when a
matching DBIC relationship exists, inject a correlated COUNT subquery
via +select/+as instead of adding a prefetch.

The order_by entries referencing count aliases are fixed up to strip the
me. prefix, since +as aliases are virtual columns not bound to a table.

In to_api, the is_count handler now checks for the pre-computed column
via get_column before falling back to the Perl-level ->count call. This
preserves backward compatibility for Koha-level methods that don't have
a corresponding DBIC relationship.

Test plan:
1. Apply patches
2. Run:
   $ ktd --shell
   k$ prove t/Koha/REST/Plugin/Query.t \
       t/db_dependent/Koha/Object.t \
       t/db_dependent/Koha/REST/Plugin/Objects.t
   => SUCCESS: Tests pass!
3. Sign off :-D

Signed-off-by: Tomás Cohen Arazi <tomascohen@theke.io>
Signed-off-by: David Nind <david@davidnind.com>
Signed-off-by: Pedro Amorim <pedro.amorim@openfifth.co.uk>
2026-06-29 16:19:48 +00:00
..
Auth Bug 37711: Add support for staff interface auto-register on IdP 2025-11-05 07:12:32 -07:00
Exceptions.pm Bug 41950: Make +count embeds sortable via SQL COUNT subqueries 2026-06-29 16:19:48 +00:00
Objects.pm Bug 40163: Fix several http addresses in the code docs 2025-09-23 16:21:29 -06:00
Pagination.pm Bug 40163: Fix several http addresses in the code docs 2025-09-23 16:21:29 -06:00
PluginRoutes.pm Bug 40163: Fix several http addresses in the code docs 2025-09-23 16:21:29 -06:00
Query.pm Bug 41950: Make +count embeds sortable via SQL COUNT subqueries 2026-06-29 16:19:48 +00:00
Responses.pm Bug 40163: Fix several http addresses in the code docs 2025-09-23 16:21:29 -06:00