blib/lib/Teng/Plugin/SearchBySQLAbstractMore/Pager/Count.pm

filter: Statementstmt 98.2% 57 / 58 Branchbran 80.0% 8 / 10 Conditioncond 66.6% 2 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 100% 10 / 10 Podpod 50.0% 1 / 2 totaltotal 91.7% 78 / 85 CC 7 SCAR 11.1
CC
7
Cov
94%
_create_sqls
6.1
search_by_sql_abstract_more_with_pager
2.0
BEGIN
1.0
CRAP 3.0
« blib/lib/Teng/Plugin/SearchBySQLAbstractMore/Pager/CountOrMySQLFoundRows.pm ↑ Summary blib/lib/Teng/Plugin/SearchBySQLAbstractMore/Pager/MySQLFoundRows.pm »
1 package Teng::Plugin::SearchBySQLAbstractMore::Pager::Count;
2  
3 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 3 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 3 ▶use utf8;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 3 ▶use Carp ();
Subroutine BEGIN: called CC=1 SCAR=0.0
7 3 ▶use Teng::Iterator;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 3 ▶use Data::Page;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 3 ▶use Teng::Plugin::SearchBySQLAbstractMore ();
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 3 our @EXPORT = qw/search_by_sql_abstract_more_with_pager/;
12  
13 sub init {
14 2 ▶ $_[1]->Teng::Plugin::SearchBySQLAbstractMore::_init();
Subroutine init: called CC=1 SCAR=0.0
Pod undocumented
15 }
16  
17 # work around
18 3 push @EXPORT, qw/sql_abstract_more_instance/;
19 3 *sql_abstract_more_instance = \&Teng::Plugin::SearchBySQLAbstractMore::sql_abstract_more_instance;
20  
21 sub search_by_sql_abstract_more_with_pager {
22 6 ▶ my ($self, $table_name, $where, $_opt) = @_;
Subroutine search_by_sql_abstract_more_with_pager: called CC=2 SCAR=6.9
Pod documented
23  
24 6 ($table_name, my($args, $rows, $page)) = Teng::Plugin::SearchBySQLAbstractMore::_arrange_args($table_name, $where, $_opt);
25  
26 6 ▶ my $table = $self->schema->get_table($table_name) or Carp::croak("No such table $table_name");
Branch: 1 branch
BranchTrueFalseTotal
my $table = $self->schema->get_table($table_name) or Carp::croak("No such table $table_name") 0 6 6
27 6 my ($sql, $binds, $count_sql, $count_binds) = _create_sqls($self, $args);
28 6 my ($total_entries, $itr);
29 6 do {
30 6 my $txn_scope = $self->txn_scope;
31 6 my $count_sth = $self->execute($count_sql, \@$count_binds);
32 6 my $sth = $self->execute($sql, \@$binds);
33 6 ($total_entries) = $count_sth->fetchrow_array();
34 6 $itr = Teng::Iterator->new(
35 teng => $self,
36 sth => $sth,
37 sql => $sql,
38 row_class => $self->schema->get_row_class($table_name),
39 table_name => $table_name,
40 suppress_object_creation => $self->suppress_row_objects,
41 );
42 6 $txn_scope->commit;
43 };
44 6 my $pager = Data::Page->new();
45 6 $pager->entries_per_page($rows);
46 6 $pager->current_page($page);
47 6 $pager->total_entries($total_entries);
48 6 return ([$itr->all], $pager);
49 }
50  
51 sub _create_sqls {
52 11 ▶ my ($self, $args) = @_;
Subroutine _create_sqls: called CC=6 SCAR=18.0
53  
54 11 my $sql_abstract_more = $self->sql_abstract_more_instance;
55 11 my $hint_columns = delete $args->{-hint_columns};
56 11 my ($sql, @binds) = $sql_abstract_more->select(%$args);
57  
58 11 delete @{$args}{qw/-offset -limit -order_by/};
59 11 ▶ if ($args->{-group_by} and $self->dbh->{Driver}->{Name} eq 'mysql') {
Branch: 1 branch
BranchTrueFalseTotal
if ($args->{"-group_by"} and $self->dbh->{"Driver"}{"Name"} eq "mysql") 0 11 11
Condition66%2/3
$args->{"-group_by"} and $self->dbh->{"Driver"}{"Name"} eq "mysql"
!ll&&!rl&&r
560
MC/DC0%0/2
$args->{"-group_by"} and $self->dbh->{"Driver"}{"Name"} eq "mysql"
$args->{"-group_by"} $self->dbh->{"Driver"}{"Name"} eq "mysql"
Truth table66%2/3
A and B
A= $args->{"-group_by"}B= $self->dbh->{"Driver"}{"Name"} eq "mysql"
ABresult
0X0
100
111
60 0 $args->{-order_by} = 'NULL';
61 }
62  
63 11 ▶ if (not $args->{-group_by}) {
Branch: 2 branches
BranchTrueFalseTotal
if (not $args->{'-group_by'}) { } 5 6 11
elsif ($hint_columns) { } 2 4 6
64 5 $args->{-columns} = ['count(*)'];
65 } elsif ($hint_columns) {
66 2 $args->{-columns} = $hint_columns;
67 }
68 11 my ($count_sql, @count_binds) = $sql_abstract_more->select(%$args);
69 11 ▶ if ($args->{-group_by}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($args->{"-group_by"}) 6 5 11
70 6 $count_sql = "SELECT COUNT(*) AS cnt FROM ($count_sql) AS total_count";
71 }
72 11 return ($sql, \@binds, $count_sql, \@count_binds);
73 }
74  
75 =pod
76  
77 =head1 NAME
78  
79 Teng::Plugin::SearchBySQLAbstractMore::Pager::Count - pager plugin using SQL::AbstractMore. count total entry by count(*)
80  
81 =head1 SYNOPSIS
82  
83 see Teng::Plugin::SearchBySQLAbstractMore
84  
85 =head1 CAUTION
86  
87 This solution is bad when you have many records. You re-consider the implementation where you want to use this module.
88 If you are using MySQL, I recommend to use Pager::CountOrMySQLFoundRows or Pager::MySQLFoundRows.
89  
90 =head1 METHODS
91  
92 =head2 search_by_sql_abstract_more_with_pager
93  
94 C<search_by_sql_abstract_more> with paging feature.
95 additional parameter can be taken, C<page>, C<rows> and C<hint_columns>.
96  
97 =head3 hint_columns
98  
99 If you pass C<hint_columns>, or C<-hint_columns> as option and select using "GROUP BY", it uses these values as select columns for calculating total count.
100  
101 For example:
102  
103 my ($rows, $pager) = $teng->search_by_sql_abstrat_more_with_pager
104 ('clicks',
105 {},
106 {-columns => [qw/user_id count(*) date(clicked_datetime)/],
107 -group_by => [qw/user_id date(clicked_datetime)/],
108 -rows => 20,
109 -page => 1,
110 }
111 );
112  
113 It execute the following 2 SQLs.
114  
115 SELECT COUNT(*) AS cnt FROM (SELECT user_id,DATE(clicked_datetime),COUNT(*) FROM clicks GROUP BY user_id, date(clicked_datetime)) AS total_count;
116 SELECT user_id, date(clicked_datetime), COUNT(*) FROM clicks GROUP BY user_id, date(clicked_datetime) LIMIT 20 OFFSET 0;
117  
118 If you pass -hint_columns option.
119  
120 my ($rows, $pager) = $teng->search_by_sql_abstrat_more_with_pager
121 ('clicks',
122 {},
123 {-columns => [qw/user_id count(*) date(clicked_datetime)/],
124 -group_by => [qw/user_id date(clicked_datetime)/],
125 -hint_columns => [qw/user_id/],
126 -rows => 20,
127 -page => 1,
128 }
129 );
130  
131 It execute the following 2 SQLs.
132  
133 SELECT COUNT(*) AS cnt FROM (SELECT user_id FROM clicks GROUP BY user_id, date(clicked_datetime)) AS total_count;
134 SELECT user_id,date(clicked_datetime) FROM clicks GROUP BY user_id, date(clicked_datetime) LIMIT 20 OFFSET 0;
135  
136 =head1 AUTHOR
137  
138 Ktat, C<< <ktat at cpan.org> >>
139  
140 =head1 BUGS
141  
142 Please report any bugs or feature requests to C<bug-teng-plugin-searchbysqlabstractmore at rt.cpan.org>, or through
143 the web interface at L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Teng-Plugin-SearchBySQLAbstractMore>. I will be notified, and then you'll
144 automatically be notified of progress on your bug as I make changes.
145  
146 =head1 SUPPORT
147  
148 You can find documentation for this module with the perldoc command.
149  
150 perldoc Teng::Plugin::SearchBySQLAbstractMore
151  
152 You can also look for information at:
153  
154 =over 4
155  
156 =item * RT: CPAN's request tracker
157  
158 L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=Teng-Plugin-SearchBySQLAbstractMore>
159  
160 =item * AnnoCPAN: Annotated CPAN documentation
161  
162 L<http://annocpan.org/dist/Teng-Plugin-SearchBySQLAbstractMore>
163  
164 =item * CPAN Ratings
165  
166 L<http://cpanratings.perl.org/d/Teng-Plugin-SearchBySQLAbstractMore>
167  
168 =item * Search CPAN
169  
170 L<http://search.cpan.org/dist/Teng-Plugin-SearchBySQLAbstractMore/>
171  
172 =back
173  
174  
175 =head1 ACKNOWLEDGEMENTS
176  
177  
178 =head1 LICENSE AND COPYRIGHT
179  
180 Copyright 2012 Ktat.
181  
182 This program is free software; you can redistribute it and/or modify it
183 under the terms of either: the GNU General Public License as published
184 by the Free Software Foundation; or the Artistic License.
185  
186 See http://dev.perl.org/licenses/ for more information.
187  
188  
189 =cut
190  
191 3 1; # End of Teng::Plugin::SearchBySQLAbstractMore::Pager::Count
« blib/lib/Teng/Plugin/SearchBySQLAbstractMore/Pager/CountOrMySQLFoundRows.pm ↑ Summary blib/lib/Teng/Plugin/SearchBySQLAbstractMore/Pager/MySQLFoundRows.pm »