line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Aniki::Plugin::SQLPager; |
2
|
2
|
|
|
2
|
|
156846
|
use 5.014002; |
|
2
|
|
|
|
|
8
|
|
3
|
|
|
|
|
|
|
|
4
|
2
|
|
|
2
|
|
704
|
use namespace::autoclean; |
|
2
|
|
|
|
|
30347
|
|
|
2
|
|
|
|
|
8
|
|
5
|
2
|
|
|
2
|
|
817
|
use Mouse::Role; |
|
2
|
|
|
|
|
28275
|
|
|
2
|
|
|
|
|
15
|
|
6
|
|
|
|
|
|
|
|
7
|
|
|
|
|
|
|
requires qw/select_by_sql select_named/; |
8
|
|
|
|
|
|
|
with qw/Aniki::Plugin::PagerInjector/; |
9
|
|
|
|
|
|
|
|
10
|
|
|
|
|
|
|
sub select_by_sql_with_pager { |
11
|
3
|
|
|
3
|
0
|
2186
|
my ($self, $sql, $bind, $opt) = @_; |
12
|
3
|
|
50
|
|
|
12
|
$opt //= {}; |
13
|
|
|
|
|
|
|
|
14
|
3
|
50
|
|
|
|
12
|
my $page = $opt->{page} or Carp::croak("required parameter: page"); |
15
|
3
|
50
|
|
|
|
9
|
my $rows = $opt->{rows} or Carp::croak("required parameter: rows"); |
16
|
|
|
|
|
|
|
|
17
|
3
|
|
|
|
|
8
|
my $limit = $rows + 1; |
18
|
3
|
|
|
|
|
7
|
my $offset = $rows * ($page - 1); |
19
|
3
|
100
|
|
|
|
10
|
if ($opt->{no_offset}) { |
20
|
1
|
|
|
|
|
6
|
$sql .= sprintf ' LIMIT %d', $limit; |
21
|
|
|
|
|
|
|
} |
22
|
|
|
|
|
|
|
else { |
23
|
2
|
|
|
|
|
12
|
$sql .= sprintf ' LIMIT %d OFFSET %d', $limit, $offset; |
24
|
|
|
|
|
|
|
} |
25
|
|
|
|
|
|
|
|
26
|
3
|
|
|
|
|
13
|
my $result = $self->select_by_sql($sql, $bind, $opt); |
27
|
3
|
|
|
|
|
16
|
return $self->inject_pager_to_result($result => $opt); |
28
|
|
|
|
|
|
|
} |
29
|
|
|
|
|
|
|
|
30
|
|
|
|
|
|
|
sub select_named_with_pager { |
31
|
3
|
|
|
3
|
0
|
2308
|
my ($self, $sql, $bind, $opt) = @_; |
32
|
3
|
|
50
|
|
|
13
|
$opt //= {}; |
33
|
|
|
|
|
|
|
|
34
|
3
|
50
|
|
|
|
12
|
my $page = $opt->{page} or Carp::croak("required parameter: page"); |
35
|
3
|
50
|
|
|
|
20
|
my $rows = $opt->{rows} or Carp::croak("required parameter: rows"); |
36
|
|
|
|
|
|
|
|
37
|
3
|
|
|
|
|
11
|
my $limit = $rows + 1; |
38
|
3
|
|
|
|
|
7
|
my $offset = $rows * ($page - 1); |
39
|
3
|
100
|
|
|
|
11
|
if ($opt->{no_offset}) { |
40
|
1
|
|
|
|
|
7
|
$sql .= sprintf ' LIMIT %d', $limit; |
41
|
|
|
|
|
|
|
} |
42
|
|
|
|
|
|
|
else { |
43
|
2
|
|
|
|
|
11
|
$sql .= sprintf ' LIMIT %d OFFSET %d', $limit, $offset; |
44
|
|
|
|
|
|
|
} |
45
|
|
|
|
|
|
|
|
46
|
3
|
|
|
|
|
18
|
my $result = $self->select_named($sql, $bind, $opt); |
47
|
3
|
|
|
|
|
21
|
return $self->inject_pager_to_result($result => $opt); |
48
|
|
|
|
|
|
|
} |
49
|
|
|
|
|
|
|
|
50
|
|
|
|
|
|
|
1; |
51
|
|
|
|
|
|
|
__END__ |
52
|
|
|
|
|
|
|
|
53
|
|
|
|
|
|
|
=pod |
54
|
|
|
|
|
|
|
|
55
|
|
|
|
|
|
|
=for stopwords sql |
56
|
|
|
|
|
|
|
|
57
|
|
|
|
|
|
|
=encoding utf-8 |
58
|
|
|
|
|
|
|
|
59
|
|
|
|
|
|
|
=head1 NAME |
60
|
|
|
|
|
|
|
|
61
|
|
|
|
|
|
|
Aniki::Plugin::SQLPager - SELECT sql with pager |
62
|
|
|
|
|
|
|
|
63
|
|
|
|
|
|
|
=head1 SYNOPSIS |
64
|
|
|
|
|
|
|
|
65
|
|
|
|
|
|
|
package MyDB; |
66
|
|
|
|
|
|
|
use Mouse v2.4.5; |
67
|
|
|
|
|
|
|
extends qw/Aniki/; |
68
|
|
|
|
|
|
|
with qw/Aniki::Plugin::Pager/; |
69
|
|
|
|
|
|
|
|
70
|
|
|
|
|
|
|
package main; |
71
|
|
|
|
|
|
|
my $db = MyDB->new(...); |
72
|
|
|
|
|
|
|
my $result = $db->select_by_sql_with_pager('SELECT * FROM user WHERE type = ?', [ 2 ], { page => 1, rows => 10 }); # => Aniki::Result::Collection(+Aniki::Result::Role::Pager) |
73
|
|
|
|
|
|
|
# ALSO OK: my $result = $db->select_named_with_pager('SELECT * FROM user WHERE type = :type', { type => 2 }, { page => 1, rows => 10 }); # => Aniki::Result::Collection(+Aniki::Result::Role::Pager) |
74
|
|
|
|
|
|
|
$result->pager; # => Data::Page::NoTotalEntries |
75
|
|
|
|
|
|
|
|
76
|
|
|
|
|
|
|
=head1 SEE ALSO |
77
|
|
|
|
|
|
|
|
78
|
|
|
|
|
|
|
L<perl> |
79
|
|
|
|
|
|
|
|
80
|
|
|
|
|
|
|
=head1 LICENSE |
81
|
|
|
|
|
|
|
|
82
|
|
|
|
|
|
|
Copyright (C) karupanerura. |
83
|
|
|
|
|
|
|
|
84
|
|
|
|
|
|
|
This library is free software; you can redistribute it and/or modify |
85
|
|
|
|
|
|
|
it under the same terms as Perl itself. |
86
|
|
|
|
|
|
|
|
87
|
|
|
|
|
|
|
=head1 AUTHOR |
88
|
|
|
|
|
|
|
|
89
|
|
|
|
|
|
|
karupanerura E<lt>karupa@cpan.orgE<gt> |
90
|
|
|
|
|
|
|
|
91
|
|
|
|
|
|
|
=cut |