Teng/Plugin/Count.pm untested

filter: Statementstmt 0.0% 0 / 17 Branchbran n/a Conditioncond 0.0% 0 / 2 MC/DCmcdc n/a Subroutinesub 0.0% 0 / 4 Podpod 0.0% 0 / 1 totaltotal 0.0% 0 / 24 CC 1 SCAR 6.9
CC
1
Cov
0%
count
2.0
BEGIN
2.0
BEGIN
2.0
CRAP 2.0
1 package Teng::Plugin::Count;
2 use strict;
3 use warnings;
4 use utf8;
5  
6 our @EXPORT = qw/count/;
7  
8 sub count {
9 my ($self, $table, $column, $where, $opt) = @_;
10 $column ||= '*';
11  
12 my ($sql, @binds) = $self->sql_builder->select($table, [\"COUNT($column)"], $where, $opt);
13  
14 my ($cnt) = $self->dbh->selectrow_array($sql, {}, @binds);
15 return $cnt;
16 }
17  
18 1;
19 __END__
20  
21 =head1 NAME
22  
23 Teng::Plugin::Count - Count rows in database.
24  
25 =head1 NAME
26  
27 package MyDB;
28 use parent qw/Teng/;
29 __PACKAGE__->load_plugin('Count');
30  
31 package main;
32 my $db = MyDB->new(...);
33 $db->count('user'); # => The number of rows in 'user' table.
34 $db->count('user', '*', {type => 2}); # => SELECT COUNT(*) FROM user WHERE type=2
35  
36 =head1 DESCRIPTION
37  
38 This plugin provides shorthand for counting rows in database.
39  
40 =head1 METHODS
41  
42 =over 4
43  
44 =item $db->count($table[, $column[, \%where]]) : Int
45  
46 I<$table> table name for counting
47  
48 I<$column> Column name for C<<< COUNT(...) >>>, the default value is '*'.
49  
50 I<\%where> : HashRef for creating where clause. The format is same as C<< $db->select() >>. This parameter is optional.
51  
52 I<Return:> The number of rows.
53  
54 =back
55