File Coverage

blib/lib/Otogiri/Plugin/Count.pm
Criterion Covered Total %
statement 23 23 100.0
branch 2 2 100.0
condition 5 5 100.0
subroutine 6 6 100.0
pod 0 1 0.0
total 36 37 97.3


line stmt bran cond sub pod time code
1             use 5.008001;
2 2     2   12376 use strict;
  2         4  
3 2     2   7 use warnings;
  2         5  
  2         33  
4 2     2   7  
  2         3  
  2         75  
5             our $VERSION = "0.02";
6              
7             use Otogiri;
8 2     2   354 use Otogiri::Plugin;
  2         35774  
  2         164  
9 2     2   318  
  2         382  
  2         253  
10             our @EXPORT = qw(count);
11              
12             my ($self, $table, $column, $where, $opt) = @_;
13              
14 4     4 0 12997 if ( ref $column eq 'HASH' || ref $column eq 'ARRAY' ) {
15             $opt = $where;
16 4 100 100     23 $where = $column;
17 2         4 $column = '*';
18 2         5 }
19 2         3  
20             $column ||= '*';
21              
22 4   100     10 my ($sql, @binds) = $self->maker->select($table, [\"COUNT($column)"], $where, $opt);
23              
24 4         10 my ($cnt) = $self->dbh->selectrow_array($sql, {}, @binds);
25             return $cnt;
26 4         1420 }
27 4         693  
28             1;
29              
30             =encoding utf-8
31              
32             =head1 NAME
33              
34             Otogiri::Plugin::Count - Otogiri plugin to count rows in database.
35              
36             =head1 SYNOPSIS
37              
38             use Otogiri;
39             use Otogiri::Plugin;
40             my $db = Otogiri->new( connect_info => [ ... ] );
41             $db->load_plugin('Count');
42             my $count = $db->count('some_table'); # SELECT COUNT(*) FROM some_table
43              
44             my $count2 = $db->count('some_table', 'column1', { group_id => 123 }); # SELECT COUNT(column1) WHERE group_id=123
45             my $count3 = $db->count('some_table', { group_id => 123 }); # SELECT COUNT(*) WHERE group_id=123
46              
47             =head1 DESCRIPTION
48              
49             Otogiri::Plugin::Count is plugin for L<Otogiri> to count database rows.
50              
51             =head1 LICENSE
52              
53             Copyright (C) Takuya Tsuchida.
54              
55             This library is free software; you can redistribute it and/or modify
56             it under the same terms as Perl itself.
57              
58             =head1 AUTHOR
59              
60             Takuya Tsuchida E<lt>tsucchi@cpan.orgE<gt>
61              
62             =cut
63