CannedQuery.pm

filter: Statementstmt 42.5% 40 / 94 Branchbran 13.3% 4 / 30 Conditioncond 10.0% 1 / 10 MC/DCmcdc 0.0% 0 / 6 Subroutinesub 52.6% 10 / 19 Podpod 100% 6 / 6 totaltotal 36.9% 61 / 165 CC 20 SCAR 25.8
CC
20
Cov
34%
_query_cache
42.0
new_or_cached
25.8
resultref
12.0
CRAP 13.2
↑ Summary CannedQuery/SimpleQueryCache.pm »
1 #!perl
2 #
3  
4 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 package Rose::DBx::CannedQuery;
8  
9 2 our ($VERSION) = '1.00';
10  
11 2 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 2 ▶use Scalar::Util;
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 2 ▶use Moo 2;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 2 ▶use Rose::DB; # Marker for prerequisite
Subroutine BEGIN: called CC=1 SCAR=0.0
16 2 ▶use Types::Standard qw/ Str HashRef InstanceOf Object /;
Subroutine BEGIN: called CC=1 SCAR=0.0
17  
18 2 has 'sql' => ( is => 'ro', isa => Str, required => 1 );
19  
20 2 has 'rdb_class' => (
21 is => 'ro',
22 isa => Str,
23 required => 0,
24 lazy => 1,
25 builder => '_retcon_rdb_class'
26 );
27  
28 0 ▶sub _retcon_rdb_class {
Subroutine _retcon_rdb_class: not called CC=2 SCAR=17.9
29 0 my $self = shift;
30 0 ▶ Carp::croak "Can't recover Rose::DB class information without a handle"
Branch: 1 branch
BranchTrueFalseTotal
unless $self->_has_rdb 0 0 0
31 unless $self->_has_rdb; # Use predicate to avoid recursion with _init_rdb
32 0 my $class = Scalar::Util::blessed $self->rdb;
33  
34 # Ugly hack
35 # Rose::DB creates handles in private classes; this may fail in the
36 # future, as it relies on undocumented Rose::DB behavior
37 0 $class =~ s/::__RoseDBPrivate__.*$//;
38 0 return $class;
39 }
40  
41 2 has 'rdb_params' => (
42 is => 'ro',
43 isa => HashRef,
44 required => 0,
45 lazy => 1,
46 builder => '_retcon_rdb_params'
47 );
48  
49 0 ▶sub _retcon_rdb_params {
Subroutine _retcon_rdb_params: not called CC=2 SCAR=17.9
50 0 my $self = shift;
51 0 ▶ Carp::croak "Can't recover Rose::DB datasource information without a handle"
Branch: 1 branch
BranchTrueFalseTotal
unless $self->_has_rdb 0 0 0
52 unless $self->_has_rdb; # Use predicate to avoid recursion with _init_rdb
53 0 my $rdb = $self->rdb;
54 0 return { type => $rdb->type, domain => $rdb->domain };
55 }
56  
57 2 has 'rdb' => (
58 is => 'ro',
59 isa => InstanceOf ['Rose::DB'],
60 required => 0,
61 lazy => 1,
62 predicate => '_has_rdb',
63 builder => '_init_rdb',
64 handles => ['dbh']
65 );
66  
67 1 ▶sub _default_rdb_params {
Subroutine _default_rdb_params: called CC=1 SCAR=0.0
68 return {
69 1 connect_options => {
70 RaiseError => 1,
71 PrintError => 0,
72 AutoCommit => 1
73 }
74 };
75 }
76  
77 1 ▶sub _init_rdb {
Subroutine _init_rdb: called CC=3 SCAR=15.0
78 1 my $self = shift;
79 1 my $class = $self->rdb_class;
80 1 ▶ defined $class and eval "$class->can('new_or_cached') || require $class"
Branch: 1 branch
BranchTrueFalseTotal
defined $class and eval "$class->can('new_or_cached') || require $class" or Carp::croak("Failed to load class ${class}: $@") 0 1 1
Condition33%1/3
defined $class and eval "$class->can('new_or_cached') || require $class"
!ll&&!rl&&r
010
MC/DC0%0/2
defined $class and eval "$class->can('new_or_cached') || require $class"
defined $class eval "$class->can('new_or_cached') || require $class"
Truth table33%1/3
A and B
A= defined $classB= eval "$class->can('new_or_cached') || require $class"
ABresult
0X0
100
111
81 or Carp::croak "Failed to load class $class: $@";
82 0 return $self->rdb_class->new_or_cached( %{ $self->_default_rdb_params },
83 0 %{ $self->rdb_params } );
84 }
85  
86 2 has 'sth' => (
87 is => 'ro',
88 isa => Object, # DBI's sth class is private
89 required => 0,
90 init_arg => undef,
91 lazy => 1,
92 builder => '_init_sth'
93 );
94  
95 0 ▶sub setup_dbh_for_query {
Subroutine setup_dbh_for_query: not called CC=1 SCAR=6.9
Pod documented
96 0 my ($self) = @_;
97 0 my $dbh = $self->rdb->dbh;
98 0 $dbh->{FetchHashKeyName} = 'NAME_lc';
99 0 return $dbh;
100 }
101  
102 0 ▶sub _init_sth {
Subroutine _init_sth: not called CC=2 SCAR=17.9
103 0 my $self = shift;
104 0 my $dbh = $self->setup_dbh_for_query;
105 0 my $sth = $dbh->prepare( $self->sql );
106 0 ▶ unless ($sth) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($sth) 0 0 0
107 0 Carp::croak 'Error preparing query: '
108 . $dbh->errstr
109 . "\nSQL was:\n\t"
110 . join( "\n\t", split( /\n/, $self->sql ) ) . "\n";
111 0 return;
112 }
113 0 return $sth;
114 }
115  
116 0 ▶sub execute {
Subroutine execute: not called CC=2 SCAR=17.9
Pod documented
117 0 my ( $self, @args ) = @_;
118 0 my $sth = $self->sth;
119  
120 0 ▶ unless ( $sth->execute(@args) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($sth->execute(@args)) 0 0 0
121 0 Carp::croak 'Error executing query: '
122 . $sth->errstr
123 . "\nArguments were:\n\t"
124 . join( "\n\t", @args ) . "\n";
125 0 return;
126 }
127 0 return $sth;
128 }
129  
130 0 ▶sub resultref {
Subroutine resultref: not called CC=3 SCAR=24.8
Pod documented
131 0 my ( $self, $args, $opts ) = @_;
132 0 ▶ $opts ||= [ {} ];
Condition0%0/2
$opts ||= [{}]
l!l
00
MC/DC0%0/1
$opts ||= [{}]
$opts
Truth table0%0/2
A
A= $opts
Aresult
11
00
133 0 ▶ my $sth = $self->execute( @{ $args || [] } );
Condition0%0/2
$args || []
l!l
00
MC/DC0%0/1
$args || []
$args
Truth table0%0/2
A
A= $args
Aresult
11
00
134 0 return $sth->fetchall_arrayref(@$opts);
135 }
136  
137 0 ▶sub results {
Subroutine results: not called CC=1 SCAR=6.9
Pod documented
138 0 my ( $self, @args ) = @_;
139 0 return @{ $self->resultref( \@args ) };
140 }
141  
142 2 ▶sub BUILDARGS {
Subroutine BUILDARGS: called CC=3 SCAR=11.2
Pod documented
143 2 my ( $self, @args ) = @_;
144 2 my $canon = $self->SUPER::BUILDARGS(@args);
145 2 ▶ if ( not exists $canon->{rdb} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $canon->{"rdb"}) 2 0 2
146 2 ▶ unless ( exists $canon->{rdb_class} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $canon->{"rdb_class"}) 1 1 2
147 1 Carp::croak
148 'Need either Rose::DB object or information to construct one';
149 0 return;
150 }
151 }
152 1 return $canon;
153 }
154  
155 # Simple class data - at least for now, the added dependency of
156 # MooX::ClassAttribute seems more weight than it's worth
157 {
158 2 my $Query_cache;
159  
160 0 ▶ sub _query_cache {
Subroutine _query_cache: not called CC=6 SCAR=37.4
161 0 my $self = shift;
162  
163 0 ▶ Carp::croak("_query_cache() may only be set via a class method call")
Branch: 1 branch
BranchTrueFalseTotal
if ref $self and @_ 0 0 0
Condition0%0/3
ref $self and @_
!ll&&!rl&&r
000
MC/DC0%0/2
ref $self and @_
ref $self @_
Truth table0%0/3
A and B
A= ref $selfB= @_
ABresult
0X0
100
111
164 if ref $self and @_;
165  
166 0 ▶ if (@_) {
Branch: 2 branches
BranchTrueFalseTotal
if (@_) { } 0 0 0
elsif (not defined $Query_cache) { } 0 0 0
167 0 ▶ $Query_cache->clear() if $Query_cache;
Branch: 1 branch
BranchTrueFalseTotal
if $Query_cache 0 0 0
168 0 $Query_cache = $_[0];
169 }
170 elsif ( not defined $Query_cache ) {
171 0 require Rose::DBx::CannedQuery::SimpleQueryCache;
172 0 $Query_cache = Rose::DBx::CannedQuery::SimpleQueryCache->new();
173 }
174  
175 0 return $Query_cache;
176 }
177 }
178  
179 2 ▶sub new_or_cached {
Subroutine new_or_cached: not called CC=5 SCAR=32.5
Pod documented
180 0 my ( $self, @args ) = @_;
181 0 ▶ my $args_for_cache_key = ref $args[0] ? $args[0] : {@args};
Branch: 1 branch
BranchTrueFalseTotal
ref $args[0] ? : 0 0 0
182 0 ▶ @args = {@args} unless ref $args[0];
Branch: 1 branch
BranchTrueFalseTotal
unless ref $args[0] 0 0 0
183 0 my $query = $self->_query_cache->get_query_from_cache($args_for_cache_key);
184 0 ▶ if ( not $query ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($query) 0 0 0
185 0 $query = $self->new(@args);
186 0 ▶ $self->_query_cache->add_query_to_cache( $query, $args_for_cache_key )
Branch: 1 branch
BranchTrueFalseTotal
if $query 0 0 0
187 if $query;
188 }
189 0 return $query;
190 }
191  
192 2 1;
193  
194 __END__
↑ Summary CannedQuery/SimpleQueryCache.pm »