File Coverage

blib/lib/Class/ReluctantORM/Monitor/JoinCount.pm
Criterion Covered Total %
statement 9 18 50.0
branch 0 2 0.0
condition n/a
subroutine 3 7 42.8
pod 4 4 100.0
total 16 31 51.6


line stmt bran cond sub pod time code
1             package Class::ReluctantORM::Monitor::JoinCount;
2              
3             =head1 NAME
4              
5             Class::ReluctantORM::Monitor::JoinCount - Track JOINs in queries
6              
7             =head1 SYNOPSIS
8              
9             use aliased 'Class::ReluctantORM::Monitor::JoinCount';
10             my $mon = JoinCounter->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.... logging and highwater scorekeeping happens
15             Pirate->fetch(...);
16              
17             # Read from the monitor
18             my $count = $mon->last_measured_value();
19              
20             =head1 DESCRIPTION
21              
22             Tracks the number of JOINs in the FROM clause in the last query that was attempted to be executed.
23              
24             Note that number of joins does not match number of relationships in the 'with' clause. Some relationship, such as HasLazy, contribute 0 JOINs, while others contribute more than one (HasManyMany).
25              
26             This is a Measuring Monitor.
27              
28             =cut
29              
30 1     1   6 use strict;
  1         3  
  1         29  
31 1     1   6 use warnings;
  1         2  
  1         24  
32 1     1   6 use base 'Class::ReluctantORM::Monitor::Measuring';
  1         1  
  1         209  
33              
34 0     0 1   sub permitted_events { return qw(execute_begin); }
35 0     0 1   sub default_events { return qw(execute_begin); }
36 0     0 1   sub measurement_label { return 'Join Count'; }
37             sub take_measurement {
38 0     0 1   my ($mon, %event) = @_;
39              
40 0           my $sql = $event{sql_obj};
41              
42 0 0         if ($sql->from()) {
43 0           my @joins = $sql->from->joins();
44 0           return scalar(@joins);
45             } else {
46 0           return 0;
47             }
48             }
49              
50             1;