line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
use strict; |
2
|
56
|
|
|
56
|
|
28626
|
use warnings; |
|
56
|
|
|
|
|
127
|
|
|
56
|
|
|
|
|
1469
|
|
3
|
56
|
|
|
56
|
|
241
|
use utf8; |
|
56
|
|
|
|
|
92
|
|
|
56
|
|
|
|
|
1299
|
|
4
|
56
|
|
|
56
|
|
228
|
|
|
56
|
|
|
|
|
88
|
|
|
56
|
|
|
|
|
256
|
|
5
|
|
|
|
|
|
|
our @EXPORT = qw/count/; |
6
|
|
|
|
|
|
|
|
7
|
|
|
|
|
|
|
my ($self, $table, $column, $where, $opt) = @_; |
8
|
|
|
|
|
|
|
|
9
|
24
|
|
|
24
|
1
|
4533
|
if (ref $column eq 'HASH') { |
10
|
|
|
|
|
|
|
Carp::croak('Do not pass HashRef to second argument. Usage: $db->count($table[, $column[, $where[, $opt]]])'); |
11
|
24
|
50
|
|
|
|
66
|
} |
12
|
0
|
|
|
|
|
0
|
|
13
|
|
|
|
|
|
|
$column ||= '*'; |
14
|
|
|
|
|
|
|
|
15
|
24
|
|
100
|
|
|
51
|
my ($sql, @binds) = $self->sql_builder->select($table, [\"COUNT($column)"], $where, $opt); |
16
|
|
|
|
|
|
|
|
17
|
24
|
|
|
|
|
60
|
my ($cnt) = $self->dbh->selectrow_array($sql, {}, @binds); |
18
|
|
|
|
|
|
|
return $cnt; |
19
|
24
|
|
|
|
|
5589
|
} |
20
|
24
|
|
|
|
|
2207
|
|
21
|
|
|
|
|
|
|
1; |
22
|
|
|
|
|
|
|
|
23
|
|
|
|
|
|
|
=head1 NAME |
24
|
|
|
|
|
|
|
|
25
|
|
|
|
|
|
|
Teng::Plugin::Count - Count rows in database. |
26
|
|
|
|
|
|
|
|
27
|
|
|
|
|
|
|
=head1 NAME |
28
|
|
|
|
|
|
|
|
29
|
|
|
|
|
|
|
package MyDB; |
30
|
|
|
|
|
|
|
use parent qw/Teng/; |
31
|
|
|
|
|
|
|
__PACKAGE__->load_plugin('Count'); |
32
|
|
|
|
|
|
|
|
33
|
|
|
|
|
|
|
package main; |
34
|
|
|
|
|
|
|
my $db = MyDB->new(...); |
35
|
|
|
|
|
|
|
$db->count('user'); # => The number of rows in 'user' table. |
36
|
|
|
|
|
|
|
$db->count('user', '*', {type => 2}); # => SELECT COUNT(*) FROM user WHERE type=2 |
37
|
|
|
|
|
|
|
|
38
|
|
|
|
|
|
|
=head1 DESCRIPTION |
39
|
|
|
|
|
|
|
|
40
|
|
|
|
|
|
|
This plugin provides shorthand for counting rows in database. |
41
|
|
|
|
|
|
|
|
42
|
|
|
|
|
|
|
=head1 METHODS |
43
|
|
|
|
|
|
|
|
44
|
|
|
|
|
|
|
=over 4 |
45
|
|
|
|
|
|
|
|
46
|
|
|
|
|
|
|
=item $db->count($table[, $column[, \%where]]) : Int |
47
|
|
|
|
|
|
|
|
48
|
|
|
|
|
|
|
I<$table> table name for counting |
49
|
|
|
|
|
|
|
|
50
|
|
|
|
|
|
|
I<$column> Column name for C<<< COUNT(...) >>>, the default value is '*'. |
51
|
|
|
|
|
|
|
|
52
|
|
|
|
|
|
|
I<\%where> : HashRef for creating where clause. The format is same as C<< $db->select() >>. This parameter is optional. |
53
|
|
|
|
|
|
|
|
54
|
|
|
|
|
|
|
I<Return:> The number of rows. |
55
|
|
|
|
|
|
|
|
56
|
|
|
|
|
|
|
=back |
57
|
|
|
|
|
|
|
|