File Coverage

blib/lib/Class/ReluctantORM/Monitor/RowCount.pm
Criterion Covered Total %
statement 9 18 50.0
branch n/a
condition n/a
subroutine 3 9 33.3
pod 6 6 100.0
total 18 33 54.5


line stmt bran cond sub pod time code
1             package Class::ReluctantORM::Monitor::RowCount;
2              
3             =head1 NAME
4              
5             Class::ReluctantORM::Monitor::RowCount - Count rows returned
6              
7             =head1 SYNOPSIS
8              
9             use aliased 'Class::ReluctantORM::Monitor::RowCount';
10             my $mon = RowCount->new(highwater_count => N, fatal_threshold => X);
11             Class::ReluctantORM->install_global_monitor($mon);
12             Pirate->install_class_monitor($mon);
13              
14             # Do a query....
15             Pirate->fetch(...);
16              
17             # Read from the monitor
18             my $row_count = $mon->last_measured_value();
19              
20             # Reset counter to 0 if desired - the counter gets reset at
21             # the beginning of every query anyway, but you might have need
22             # to reset it in a fetchrow callback
23             $mon->reset();
24              
25             =head1 DESCRIPTION
26              
27             Tracks the number of rows returned by a query, The counter is reset at the beginning of execution of a query, and it is incremented as each row is fetched.
28              
29             Note that number of rows does not match the number of objects returned by a fetch. Joins can make the row count significantly higher by many factors.
30              
31             This is a Measuring Monitor.
32              
33             =cut
34              
35 1     1   5 use strict;
  1         3  
  1         27  
36 1     1   5 use warnings;
  1         2  
  1         25  
37 1     1   5 use base 'Class::ReluctantORM::Monitor::Measuring';
  1         2  
  1         230  
38              
39 0     0 1   sub permitted_events { return qw(finish); }
40 0     0 1   sub default_events { return qw(finish); }
41 0     0 1   sub measurement_label { return 'Row Count'; }
42             sub take_measurement {
43 0     0 1   my ($mon, %event) = @_;
44             # Nothing to do - actual work performed in fetchrow
45 0           return $mon->last_measured_value();
46             }
47              
48             sub notify_execute_begin {
49 0     0 1   my ($mon, %event) = @_;
50              
51             # Reset on execute
52 0           $mon->reset();
53             }
54             sub notify_fetch_row {
55 0     0 1   my ($mon, %event) = @_;
56 0           $mon->last_measured_value($mon->last_measured_value() + 1);
57             }
58              
59             1;