Async/ResultSet/Pager.pm

filter: Statementstmt 91.9% 57 / 62 Branchbran 57.6% 15 / 26 Conditioncond 46.1% 6 / 13 MC/DCmcdc 0.0% 0 / 7 Subroutinesub 89.4% 17 / 19 Podpod 83.3% 10 / 12 totaltotal 75.5% 105 / 139 CC 20 SCAR 12.0
CC
20
Cov
77%
entries_on_this_page
6.7
has_previous
6.0
previous_page_rs
6.0
CRAP 3.3
« Async/Exception.pm ↑ Summary Async/Storage/DBI/Cursor.pm »
1 package DBIx::Class::Async::ResultSet::Pager;
2  
3 4 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 4 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 4 ▶use version;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 4 our $VERSION = qv('v1.2.0');
8 4 our $AUTHORITY = 'cpan:MANWAR';
9  
10 4 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 4 ▶use Future;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 4 ▶use POSIX qw(ceil);
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 =head1 NAME
15  
16 DBIx::Class::Async::ResultSet::Pager - Asynchronous pagination handling for Async ResultSets
17  
18 =head1 VERSION
19  
20 Version v1.2.0
21  
22 =cut
23  
24 =head1 SYNOPSIS
25  
26 # Obtain a pager from a paged ResultSet
27 my $rs = $schema->resultset('User')->search({}, {
28 rows => 20,
29 page => 1
30 });
31  
32 my $pager = $rs->pager;
33  
34 # Synchronous Metadata (available immediately)
35 say "Page: " . $pager->current_page;
36 say "Entries per page: ". $pager->entries_per_page;
37  
38 # Asynchronous Metadata (returns a Future)
39 # total_entries performs a COUNT(*) in the background
40 $pager->total_entries->then(sub {
41 my $total = shift;
42  
43 say "Total entries in DB: $total";
44  
45 # After total_entries resolves, these become available
46 say "First page: " . $pager->first_page;
47 say "Last page: " . $pager->last_page;
48 say "Previous: " . ($pager->previous_page // 'N/A');
49 say "Next: " . ($pager->next_page // 'N/A');
50  
51 return Future->done;
52 })->catch(sub {
53 warn "Failed to fetch counts: @_";
54 });
55  
56 =head1 DESCRIPTION
57  
58 This module provides a pagination object for L<DBIx::Class::Async::ResultSet>. It
59 is designed to work similarly to L<Data::Page>, but with the critical difference
60 that the total entry count is fetched asynchronously from the database worker.
61  
62 =head1 CONSTRUCTOR
63  
64 =head2 new
65  
66 my $pager = DBIx::Class::Async::ResultSet::Pager->new(
67 resultset => $rs
68 );
69  
70 Instantiates a new pager. Usually called via C<< $rs->pager >>.
71  
72 =cut
73  
74 8 ▶sub new {
Subroutine new: called CC=3 SCAR=11.7
Pod documented
75 8 my ($class, %args) = @_;
76 8 my $rs = $args{resultset};
77  
78 return bless {
79 _rs => $rs,
80 _total_entries => undef,
81 _rows => $rs->{_attrs}->{rows} // 10,
82 8 ▶ _page => $rs->{_attrs}->{page} // 1,
Condition50%2/4
$rs->{"_attrs"}{"rows"} // 10
l!l
80
$rs->{"_attrs"}{"page"} // 1
l!l
80
MC/DC0%0/1
$rs->{"_attrs"}{"rows"} // 10
$rs->{"_attrs"}{"rows"}
MC/DC0%0/1
$rs->{"_attrs"}{"page"} // 1
$rs->{"_attrs"}{"page"}
Truth table50%1/2
A
A= $rs->{"_attrs"}{"rows"}
Aresult
11
00
Truth table50%1/2
A
A= $rs->{"_attrs"}{"page"}
Aresult
11
00
83 }, $class;
84 }
85  
86 =head1 METHODS
87  
88 =cut
89  
90 1 ▶sub has_next {
Subroutine has_next: called CC=2 SCAR=7.2
Pod undocumented
91 1 my $self = shift;
92 # If next_page returns a number (2, 3, etc), this becomes true.
93 # If it returns undef, it stays false.
94 1 ▶ return $self->next_page ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$self->next_page ? : 1 0 1
95 }
96  
97 0 ▶sub has_previous {
Subroutine has_previous: not called CC=2 SCAR=17.9
Pod undocumented
98 0 my $self = shift;
99 0 ▶ return $self->previous_page ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$self->previous_page ? : 0 0 0
100 }
101  
102  
103  
104 =head2 total_entries
105  
106 Returns a L<Future> that resolves to the total number of entries matching the
107 ResultSet condition, ignoring pagination limits. The result is cached after
108 the first call.
109  
110 =cut
111  
112 9 ▶sub total_entries {
Subroutine total_entries: called CC=2 SCAR=7.0
Pod documented
113 9 my $self = shift;
114  
115 9 ▶ if (defined $self->{_total_entries}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{"_total_entries"}) 3 6 9
116 3 return Future->done($self->{_total_entries});
117 }
118  
119 6 ▶ return $self->{_rs}->count_total->then(sub {
Subroutine __ANON__: called CC=3 SCAR=11.4
120 6 my $val = shift;
121 6 ▶ my $count = ref($val) eq 'ARRAY' ? $val->[0] : $val;
Branch: 1 branch
BranchTrueFalseTotal
ref $val eq 'ARRAY' ? : 0 6 6
122  
123 6 ▶ $self->{_total_entries} = $count // 0;
Condition50%1/2
$count // 0
l!l
60
MC/DC0%0/1
$count // 0
$count
Truth table50%1/2
A
A= $count
Aresult
11
00
124 6 return Future->done($self->{_total_entries});
125 6 });
126 }
127  
128 =head2 entries_per_page
129  
130 Returns the number of entries per page (C<rows> attribute).
131  
132 =cut
133  
134 1 ▶sub entries_per_page { shift->{_rows} }
Subroutine entries_per_page: called CC=1 SCAR=0.0
Pod documented
135  
136 =head2 current_page
137  
138 Returns the current page number.
139  
140 =cut
141  
142 3 ▶sub current_page { shift->{_page} }
Subroutine current_page: called CC=1 SCAR=0.0
Pod documented
143  
144 =head2 last_page
145  
146 Returns the total number of pages. Note: This assumes C<total_entries> has
147 been resolved. If not, it defaults to 1.
148  
149 =cut
150  
151 11 ▶sub last_page {
Subroutine last_page: called CC=3 SCAR=11.3
Pod documented
152 11 my $self = shift;
153 croak "Pager not initialised. Call ->total_entries first"
154 11 ▶ unless defined $self->{_total_entries};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->{"_total_entries"} 0 11 11
155  
156 11 my $total = $self->{_total_entries};
157 11 ▶ return 1 if $total == 0;
Branch: 1 branch
BranchTrueFalseTotal
if $total == 0 0 11 11
158 11 return ceil($total / $self->{_rows});
159 }
160  
161 =head2 entries_on_this_page
162  
163 Returns the number of entries on the current page.
164  
165 =cut
166  
167 3 ▶sub entries_on_this_page {
Subroutine entries_on_this_page: called CC=6 SCAR=19.0
Pod documented
168 3 my $self = shift;
169 3 ▶ my $total = $self->{_total_entries} // 0;
Condition50%1/2
$self->{"_total_entries"} // 0
l!l
30
MC/DC0%0/1
$self->{"_total_entries"} // 0
$self->{"_total_entries"}
Truth table50%1/2
A
A= $self->{"_total_entries"}
Aresult
11
00
170 3 ▶ return 0 if $total == 0;
Branch: 1 branch
BranchTrueFalseTotal
if $total == 0 0 3 3
171  
172 3 my $last = $self->last_page;
173 3 ▶ return 0 if $self->{_page} > $last;
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"_page"} > $last 0 3 3
174  
175 3 ▶ if ($self->{_page} < $last) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'_page'} < $last) { } 1 2 3
176 1 return $self->{_rows};
177 } else {
178 2 ▶ return $total % $self->{_rows} || $self->{_rows};
Condition33%1/3
$total % $self->{'_rows'} || $self->{'_rows'}
l!l&&r!l&&!r
200
MC/DC0%0/2
$total % $self->{'_rows'} || $self->{'_rows'}
$total % $self->{'_rows'} $self->{'_rows'}
Truth table33%1/3
A || B
A= $total % $self->{'_rows'}B= $self->{'_rows'}
ABresult
1X1
011
000
179 }
180 }
181  
182 =head2 previous_page
183  
184 Returns the previous page number or C<undef> if on the first page.
185  
186 =cut
187  
188 3 ▶sub previous_page {
Subroutine previous_page: called CC=2 SCAR=6.9
Pod documented
189 3 my $self = shift;
190 3 ▶ return ($self->{_page} > 1) ? $self->{_page} - 1 : undef;
Branch: 1 branch
BranchTrueFalseTotal
$self->{'_page'} > 1 ? : 2 1 3
191 }
192  
193 =head2 next_page
194  
195 Returns the next page number or C<undef> if on the last page.
196  
197 =cut
198  
199 5 ▶sub next_page {
Subroutine next_page: called CC=3 SCAR=11.1
Pod documented
200 5 my $self = shift;
201 5 ▶ my $total = $self->{_total_entries} // 0;
Condition50%1/2
$self->{"_total_entries"} // 0
l!l
50
MC/DC0%0/1
$self->{"_total_entries"} // 0
$self->{"_total_entries"}
Truth table50%1/2
A
A= $self->{"_total_entries"}
Aresult
11
00
202 5 ▶ return ($self->{_page} < $self->last_page) ? $self->{_page} + 1 : undef;
Branch: 1 branch
BranchTrueFalseTotal
$self->{'_page'} < $self->last_page ? : 3 2 5
203 }
204  
205 =head2 next_page_rs
206  
207 Returns a new L<DBIx::Class::Async::ResultSet> for the next page, or C<undef>.
208  
209 =cut
210  
211 1 ▶sub next_page_rs {
Subroutine next_page_rs: called CC=2 SCAR=7.1
Pod documented
212 1 my $self = shift;
213 1 ▶ my $next = $self->next_page or return undef;
Branch: 1 branch
BranchTrueFalseTotal
my $next = $self->next_page or return undef 1 0 1
214 1 return $self->{_rs}->page($next);
215 }
216  
217 =head2 previous_page_rs
218  
219 Returns a new L<DBIx::Class::Async::ResultSet> for the previous page, or C<undef>.
220  
221 =cut
222  
223 0 ▶sub previous_page_rs {
Subroutine previous_page_rs: not called CC=2 SCAR=17.9
Pod documented
224 0 my $self = shift;
225 0 ▶ my $prev = $self->previous_page or return undef;
Branch: 1 branch
BranchTrueFalseTotal
my $prev = $self->previous_page or return undef 0 0 0
226 0 return $self->{_rs}->page($prev);
227 }
228  
229 =head1 AUTHOR
230  
231 Mohammad Sajid Anwar, C<< <mohammad.anwar at yahoo.com> >>
232  
233 =head1 REPOSITORY
234  
235 L<https://github.com/manwar/DBIx-Class-Async>
236  
237 =head1 BUGS
238  
239 Please report any bugs or feature requests through the web interface at L<https://github.com/manwar/DBIx-Class-Async/issues>.
240 I will be notified and then you'll automatically be notified of progress on your
241 bug as I make changes.
242  
243 =head1 SUPPORT
244  
245 You can find documentation for this module with the perldoc command.
246  
247 perldoc DBIx::Class::Async::ResultSet::Pager
248  
249 You can also look for information at:
250  
251 =over 4
252  
253 =item * BUG Report
254  
255 L<https://github.com/manwar/DBIx-Class-Async/issues>
256  
257 =item * CPAN Ratings
258  
259 L<http://cpanratings.perl.org/d/DBIx-Class-Async>
260  
261 =item * Search MetaCPAN
262  
263 L<https://metacpan.org/dist/DBIx-Class-Async/>
264  
265 =back
266  
267 =head1 LICENSE AND COPYRIGHT
268  
269 Copyright (C) 2026 Mohammad Sajid Anwar.
270  
271 This program is free software; you can redistribute it and / or modify it under
272 the terms of the the Artistic License (2.0). You may obtain a copy of the full
273 license at:
274  
275 L<http://www.perlfoundation.org/artistic_license_2_0>
276  
277 Any use, modification, and distribution of the Standard or Modified Versions is
278 governed by this Artistic License.By using, modifying or distributing the Package,
279 you accept this license. Do not use, modify, or distribute the Package, if you do
280 not accept this license.
281  
282 If your Modified Version has been derived from a Modified Version made by someone
283 other than you,you are nevertheless required to ensure that your Modified Version
284 complies with the requirements of this license.
285  
286 This license does not grant you the right to use any trademark, service mark,
287 tradename, or logo of the Copyright Holder.
288  
289 This license includes the non-exclusive, worldwide, free-of-charge patent license
290 to make, have made, use, offer to sell, sell, import and otherwise transfer the
291 Package with respect to any patent claims licensable by the Copyright Holder that
292 are necessarily infringed by the Package. If you institute patent litigation
293 (including a cross-claim or counterclaim) against any party alleging that the
294 Package constitutes direct or contributory patent infringement,then this Artistic
295 License to you shall terminate on the date that such litigation is filed.
296  
297 Disclaimer of Warranty: THE PACKAGE IS PROVIDED BY THE COPYRIGHT HOLDER AND
298 CONTRIBUTORS "AS IS' AND WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES. THE IMPLIED
299 WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, OR
300 NON-INFRINGEMENT ARE DISCLAIMED TO THE EXTENT PERMITTED BY YOUR LOCAL LAW. UNLESS
301 REQUIRED BY LAW, NO COPYRIGHT HOLDER OR CONTRIBUTOR WILL BE LIABLE FOR ANY DIRECT,
302 INDIRECT, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING IN ANY WAY OUT OF THE USE
303 OF THE PACKAGE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
304  
305 =cut
306  
307 4 1; # End of DBIx::Class::Async::ResultSet::Pager
« Async/Exception.pm ↑ Summary Async/Storage/DBI/Cursor.pm »