DBR/Query/Select.pm

filter: Statementstmt 97.1% 67 / 69 Branchbran 63.6% 14 / 22 Conditioncond 50.0% 11 / 22 MC/DCmcdc 0.0% 0 / 12 Subroutinesub 94.1% 16 / 17 Podpod 0.0% 0 / 8 totaltotal 72.0% 108 / 150 CC 22 SCAR 14.1
CC
22
Cov
81%
fields
8.3
reset
6.0
sql
5.1
CRAP 4.1
« DBR/Config/Trans.pm ↑ Summary DBR/Misc/General.pm »
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::Select;
8  
9 18 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 18 ▶use base 'DBR::Query';
Subroutine BEGIN: called CC=1 SCAR=0.0
11 18 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 18 ▶use DBR::Record::Maker;
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 590 ▶sub _params { qw (fields tables where builder limit lock quiet_error) }
Subroutine _params: called CC=1 SCAR=0.0
15 574 ▶sub _reqparams { qw (fields tables) }
Subroutine _reqparams: called CC=1 SCAR=0.0
16 574 ▶sub _validate_self{ 1 } # If I exist, I'm valid
Subroutine _validate_self: called CC=1 SCAR=0.0
17  
18 603 ▶sub fields{
Subroutine fields: called CC=8 SCAR=21.2
Pod undocumented
19 603 my $self = shift;
20 603 ▶ exists( $_[0] ) or return wantarray?( @{$self->{fields}||[]} ) : $self->{fields} || undef;
Branch: 2 branches
BranchTrueFalseTotal
exists $_[0] or return wantarray ? @{$self->{"fields"} or [];} : $self->{"fields"} || undef 574 29 603
wantarray ? : 27 2 29
Condition50%2/4
$self->{'fields'} || []
l!l
270
$self->{"fields"} or undef
l!l
20
MC/DC0%0/1
$self->{'fields'} || []
$self->{'fields'}
MC/DC0%0/1
$self->{"fields"} or undef
$self->{"fields"}
Truth table50%1/2
A
A= $self->{'fields'}
Aresult
11
00
Truth table50%1/2
A
A= $self->{"fields"}
Aresult
11
00
21  
22 574 my @fields = $self->_arrayify(@_);
23 574 ▶ scalar(@fields) || croak('must provide at least one field');
Branch: 1 branch
BranchTrueFalseTotal
scalar @fields or croak("must provide at least one field") 574 0 574
24  
25 574 my $lastidx = -1;
26 574 for (@fields){
27 2455 ▶ ref($_) =~ /^DBR::Config::Field/ || croak('must specify field as a DBR::Config::Field object'); # Could also be ::Anon
Branch: 1 branch
BranchTrueFalseTotal
ref($_) =~ /^DBR::Config::Field/ or croak("must specify field as a DBR::Config::Field object") 2455 0 2455
28 2455 $_->index( ++$lastidx );
29 }
30 574 $self->{last_idx} = $lastidx;
31 574 $self->{fields} = \@fields;
32  
33 574 return 1;
34 }
35  
36  
37 564 ▶sub sql{
Subroutine sql: called CC=5 SCAR=16.2
Pod undocumented
38 564 my $self = shift;
39 564 ▶ my $conn = $self->instance->connect('conn') or return $self->_error('failed to connect');
Branch: 1 branch
BranchTrueFalseTotal
my $conn = $self->instance->connect("conn") or return $self->_error("failed to connect") 564 0 564
40 564 my $sql;
41  
42 564 my $tables = join(',', map { $_->sql( $conn ) } @{$self->{tables}} );
43 564 my $fields = join(',', map { $_->sql( $conn ) } @{$self->{fields}} );
44  
45 564 $sql = "SELECT $fields FROM $tables";
46 564 ▶ $sql .= ' WHERE ' . $self->{where}->sql($conn) if $self->{where};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"where"} 535 29 564
47 564 ▶ $sql .= ' FOR UPDATE' if $self->{lock};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"lock"} 0 564 564
48 564 ▶ $sql .= ' LIMIT ' . $self->{limit} if $self->{limit};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"limit"} 0 564 564
49  
50 564 $self->_logDebug2( $sql );
51 564 return $sql;
52 }
53  
54 27 ▶sub lastidx { $_[0]{last_idx} }
Subroutine lastidx: called CC=1 SCAR=0.0
Pod undocumented
55 1 ▶sub can_be_subquery { scalar( @{ $_[0]->fields || [] } ) == 1 }; # Must have exactly one field
Subroutine can_be_subquery: called CC=2 SCAR=7.2
Pod undocumented
Condition50%1/2
$_[0]->fields || []
l!l
10
MC/DC0%0/1
$_[0]->fields || []
$_[0]->fields
Truth table50%1/2
A
A= $_[0]->fields
Aresult
11
00
56  
57 567 ▶sub run {
Subroutine run: called CC=3 SCAR=11.7
Pod undocumented
58 567 my $self = shift;
59 567 ▶ return $self->{sth} ||= $self->instance->getconn->prepare( $self->sql ) || confess "Failed to prepare"; # only run once
Condition60%3/5
$self->{"sth"} ||= $self->instance->getconn->prepare($self->sql) || confess("Failed to prepare")
l!l&&r!l&&!r
45630
$self->instance->getconn->prepare($self->sql) || confess('Failed to prepare')
l!l
5630
MC/DC0%0/2
$self->{"sth"} ||= $self->instance->getconn->prepare($self->sql) || confess("Failed to prepare")
$self->{"sth"} $self->instance->getconn->prepare($self->sql)
Truth table66%2/3
A ||= B
A= $self->{"sth"}B= $self->instance->getconn->prepare($self->sql)
ABresult
1X1
011
000
60 }
61 0 ▶sub reset {
Subroutine reset: not called CC=2 SCAR=17.9
Pod undocumented
62 0 my $self = shift;
63 0 ▶ return $self->{sth} && $self->{sth}->finish;
Condition0%0/3
$self->{'sth'} && $self->{'sth'}->finish
!ll&&!rl&&r
000
MC/DC0%0/2
$self->{'sth'} && $self->{'sth'}->finish
$self->{'sth'} $self->{'sth'}->finish
Truth table0%0/3
A && B
A= $self->{'sth'}B= $self->{'sth'}->finish
ABresult
0X0
100
111
64 }
65  
66 # HERE - it's a little funky that we are handling split queries here,
67 # but non-split queries in ResultSet. Not horrible... just funky.
68 10 ▶sub fetch_segment{
Subroutine fetch_segment: called CC=3 SCAR=11.4
Pod undocumented
69 10 my $self = shift;
70 10 my $value = shift;
71  
72 10 ▶ return ( $self->{spvals} ||= $self->_do_split )->{ $value } || [];
Condition60%3/5
+($self->{'spvals'} ||= $self->_do_split)->{$value} || []
l!l
100
$self->{"spvals"} ||= $self->_do_split
l!l&&r!l&&!r
640
MC/DC0%0/1
+($self->{'spvals'} ||= $self->_do_split)->{$value} || []
+($self->{'spvals'} ||= $self->_do_split)->{$value}
MC/DC0%0/2
$self->{"spvals"} ||= $self->_do_split
$self->{"spvals"} $self->_do_split
Truth table50%1/2
A
A= +($self->{'spvals'} ||= $self->_do_split)->{$value}
Aresult
11
00
Truth table66%2/3
A ||= B
A= $self->{"spvals"}B= $self->_do_split
ABresult
1X1
011
000
73 }
74  
75 4 ▶sub _do_split{
Subroutine _do_split: called CC=4 SCAR=14.0
76 4 my $self = shift;
77  
78 # Should have a splitfield if we're getting here. Don't check for it. speeed.
79 4 ▶ defined( my $idx = $self->{splitfield}->index ) or croak 'field object must provide an index';
Branch: 1 branch
BranchTrueFalseTotal
defined(my $idx = $self->{"splitfield"}->index) or croak("field object must provide an index") 4 0 4
80  
81 4 my $sth = $self->run;
82  
83 4 ▶ defined( $sth->execute ) or croak 'failed to execute statement (' . $sth->errstr. ')';
Branch: 1 branch
BranchTrueFalseTotal
defined $sth->execute or croak('failed to execute statement (' . $sth->errstr . ')') 4 0 4
84  
85 4 my $row;
86 4 my $code = 'while($row = $sth->fetch){ push @{$groupby{ $row->[' . $idx . '] }}, [@$row] }';
87 4 $self->_logDebug3($code);
88  
89 4 my %groupby;
90 4 eval $code;
91 4 ▶ $@ && confess $@;
Branch: 1 branch
BranchTrueFalseTotal
$@ and confess($@) 0 4 4
92  
93 4 $sth->finish;
94 4 return \%groupby;
95 }
96  
97  
98 39 ▶sub get_record_obj{
Subroutine get_record_obj: called CC=2 SCAR=7.1
Pod undocumented
99 39 my $self = shift;
100  
101 # Only make the record-maker object once per query. Even split queries should be able to share the same one.
102 return $self->{recordobj} ||= DBR::Record::Maker->new(
103 session => $self->{session},
104 39 ▶ query => $self, # This value is not preserved by the record maker, thus no memory leak
Condition66%2/3
$self->{"recordobj"} ||= "DBR::Record::Maker"->new("session", $self->{"session"}, "query", $self)
l!l&&r!l&&!r
12270
MC/DC0%0/2
$self->{"recordobj"} ||= "DBR::Record::Maker"->new("session", $self->{"session"}, "query", $self)
$self->{"recordobj"} "DBR::Record::Maker"->new("session", $self->{"session"}, "query", $self)
Truth table66%2/3
A ||= B
A= $self->{"recordobj"}B= "DBR::Record::Maker"->new("session", $self->{"session"}, "query", $self)
ABresult
1X1
011
000
105 ) or confess ('failed to create record class');
106 }
107  
108 580 ▶sub DESTROY{
Subroutine DESTROY: called CC=1 SCAR=0.0
109 580 my $self = shift;
110  
111 # Can't finish the sth when going out of scope, it might live longer than this object.
112  
113 580 return 1;
114 }
115 18 1;
« DBR/Config/Trans.pm ↑ Summary DBR/Misc/General.pm »