File Coverage

blib/lib/DBR/Query/Count.pm
Criterion Covered Total %
statement 30 31 96.7
branch 6 10 60.0
condition n/a
subroutine 8 8 100.0
pod 0 2 0.0
total 44 51 86.2


line stmt bran cond sub pod time code
1             # The contents of this file are Copyright (c) 2010 Daniel Norman
2             # This program is free software; you can redistribute it and/or
3             # modify it under the terms of the GNU General Public License as
4             # published by the Free Software Foundation.
5              
6             ###########################################
7             package DBR::Query::Count;
8              
9 18     18   108 use strict;
  18         40  
  18         733  
10 18     18   94 use base 'DBR::Query';
  18         41  
  18         8982  
11 18     18   120 use Carp;
  18         77  
  18         15598  
12              
13 13     13   56 sub _params { qw (tables where limit quiet_error) }
14 13     13   489 sub _reqparams { qw (tables) }
15 13     13   44 sub _validate_self{ 1 } # If I exist, I'm valid
16              
17              
18              
19             sub sql{
20 13     13 0 118 my $self = shift;
21 13 50       45 my $conn = $self->instance->connect('conn') or return $self->_error('failed to connect');
22 13         24 my $sql;
23              
24 13         395 my $tables = join(',', map { $_->sql( $conn ) } @{$self->{tables}} );
  13         68  
  13         38  
25 13         31 my $fields = join(',', map { $_->sql( $conn ) } @{$self->{fields}} );
  0         0  
  13         67  
26              
27 13         31 $sql = "SELECT count(*) FROM $tables";
28 13 100       90 $sql .= ' WHERE ' . $self->{where}->sql($conn) if $self->{where};
29 13 50       54 $sql .= ' LIMIT ' . $self->{limit} if $self->{limit};
30              
31 13         181 $self->_logDebug2( $sql );
32 13         675 return $sql;
33             }
34              
35             sub run {
36 13     13 0 24 my $self = shift;
37              
38 13 50       64 my $conn = $self->instance->connect('conn') or confess 'failed to connect';
39 13         57 my ($count) = $conn->selectrow_array($self->sql);
40 13 50       226 defined($count) or confess "failed to retrieve count";
41              
42 13         304 return $count;
43              
44             }
45              
46              
47             1;