line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Aniki::Plugin::Pager; |
2
|
1
|
|
|
1
|
|
89624
|
use 5.014002; |
|
1
|
|
|
|
|
6
|
|
3
|
|
|
|
|
|
|
|
4
|
1
|
|
|
1
|
|
546
|
use namespace::autoclean; |
|
1
|
|
|
|
|
19529
|
|
|
1
|
|
|
|
|
5
|
|
5
|
1
|
|
|
1
|
|
730
|
use Mouse::Role; |
|
1
|
|
|
|
|
18106
|
|
|
1
|
|
|
|
|
6
|
|
6
|
|
|
|
|
|
|
|
7
|
1
|
|
|
1
|
|
401
|
use Carp qw/croak/; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
255
|
|
8
|
|
|
|
|
|
|
|
9
|
|
|
|
|
|
|
requires qw/select/; |
10
|
|
|
|
|
|
|
with qw/Aniki::Plugin::PagerInjector/; |
11
|
|
|
|
|
|
|
with qw/Aniki::Plugin::RangeConditionMaker/; |
12
|
|
|
|
|
|
|
|
13
|
|
|
|
|
|
|
sub select_with_pager { |
14
|
6
|
|
|
6
|
0
|
11780
|
my ($self, $table_name, $where, $opt) = @_; |
15
|
6
|
|
50
|
|
|
57
|
$where //= {}; |
16
|
6
|
|
50
|
|
|
24
|
$opt //= {}; |
17
|
|
|
|
|
|
|
|
18
|
6
|
50
|
|
|
|
22
|
croak '(Aniki::Plugin::Pager#select_with_pager) `where` condition must be a reference.' unless ref $where; |
19
|
|
|
|
|
|
|
|
20
|
6
|
|
|
|
|
40
|
my $range_condition = $self->make_range_condition($opt); |
21
|
6
|
100
|
|
|
|
27
|
if ($range_condition) { |
22
|
2
|
50
|
|
|
|
11
|
ref $where eq 'HASH' |
23
|
|
|
|
|
|
|
or croak "where condition *MUST* be HashRef when using range codition."; |
24
|
|
|
|
|
|
|
|
25
|
2
|
|
|
|
|
11
|
for my $column (keys %$range_condition) { |
26
|
|
|
|
|
|
|
croak "Conflict range condition and where condition for $table_name.$column" |
27
|
2
|
50
|
|
|
|
11
|
if exists $where->{$column}; |
28
|
|
|
|
|
|
|
} |
29
|
|
|
|
|
|
|
|
30
|
2
|
|
|
|
|
10
|
$where = {%$where, %$range_condition}; |
31
|
|
|
|
|
|
|
} |
32
|
|
|
|
|
|
|
|
33
|
6
|
50
|
|
|
|
27
|
my $page = $opt->{page} or Carp::croak("required parameter: page"); |
34
|
6
|
50
|
|
|
|
24
|
my $rows = $opt->{rows} or Carp::croak("required parameter: rows"); |
35
|
6
|
100
|
|
|
|
79
|
my $result = $self->select($table_name => $where, { |
36
|
|
|
|
|
|
|
%$opt, |
37
|
|
|
|
|
|
|
limit => $rows + 1, |
38
|
|
|
|
|
|
|
!$range_condition ? ( |
39
|
|
|
|
|
|
|
offset => $rows * ($page - 1), |
40
|
|
|
|
|
|
|
) : (), |
41
|
|
|
|
|
|
|
}); |
42
|
|
|
|
|
|
|
|
43
|
6
|
|
|
|
|
66
|
return $self->inject_pager_to_result($result => { |
44
|
|
|
|
|
|
|
rows => $rows, |
45
|
|
|
|
|
|
|
page => $page, |
46
|
|
|
|
|
|
|
}); |
47
|
|
|
|
|
|
|
} |
48
|
|
|
|
|
|
|
|
49
|
|
|
|
|
|
|
|
50
|
|
|
|
|
|
|
1; |
51
|
|
|
|
|
|
|
__END__ |
52
|
|
|
|
|
|
|
|
53
|
|
|
|
|
|
|
=pod |
54
|
|
|
|
|
|
|
|
55
|
|
|
|
|
|
|
=encoding utf-8 |
56
|
|
|
|
|
|
|
|
57
|
|
|
|
|
|
|
=head1 NAME |
58
|
|
|
|
|
|
|
|
59
|
|
|
|
|
|
|
Aniki::Plugin::Pager - SELECT with pager |
60
|
|
|
|
|
|
|
|
61
|
|
|
|
|
|
|
=head1 SYNOPSIS |
62
|
|
|
|
|
|
|
|
63
|
|
|
|
|
|
|
package MyDB; |
64
|
|
|
|
|
|
|
use Mouse v2.4.5; |
65
|
|
|
|
|
|
|
extends qw/Aniki/; |
66
|
|
|
|
|
|
|
with qw/Aniki::Plugin::Pager/; |
67
|
|
|
|
|
|
|
|
68
|
|
|
|
|
|
|
package main; |
69
|
|
|
|
|
|
|
my $db = MyDB->new(...); |
70
|
|
|
|
|
|
|
my $result = $db->select_with_pager('user', { type => 2 }, { page => 1, rows => 10 }); # => Aniki::Result::Collection(+Aniki::Result::Role::Pager) |
71
|
|
|
|
|
|
|
$result->pager; # => Data::Page::NoTotalEntries |
72
|
|
|
|
|
|
|
|
73
|
|
|
|
|
|
|
=head1 SEE ALSO |
74
|
|
|
|
|
|
|
|
75
|
|
|
|
|
|
|
L<perl> |
76
|
|
|
|
|
|
|
|
77
|
|
|
|
|
|
|
=head1 LICENSE |
78
|
|
|
|
|
|
|
|
79
|
|
|
|
|
|
|
Copyright (C) karupanerura. |
80
|
|
|
|
|
|
|
|
81
|
|
|
|
|
|
|
This library is free software; you can redistribute it and/or modify |
82
|
|
|
|
|
|
|
it under the same terms as Perl itself. |
83
|
|
|
|
|
|
|
|
84
|
|
|
|
|
|
|
=head1 AUTHOR |
85
|
|
|
|
|
|
|
|
86
|
|
|
|
|
|
|
karupanerura E<lt>karupa@cpan.orgE<gt> |
87
|
|
|
|
|
|
|
|
88
|
|
|
|
|
|
|
=cut |