| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
Condition33%1/3
defined $class and eval "$class->can('new_or_cached') || require $class"
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"
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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 ||= [{}]
MC/DC0%0/1
$opts ||= [{}]
$opts
Truth table0%0/2
A
A= $opts
| |||||||||||||||||||||||||||||
| 133 | 0 | ▶ | my $sth = $self->execute( @{ $args || [] } ); | ||||||||||||||||||||||||||
|
Condition0%0/2
$args || []
MC/DC0%0/1
$args || []
$args
Truth table0%0/2
A
A= $args
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 146 | 2 | ▶ | unless ( exists $canon->{rdb_class} ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
Condition0%0/3
ref $self and @_
MC/DC0%0/2
ref $self and @_
ref $self
@_
Truth table0%0/3
A and B
A= ref $selfB= @_
| |||||||||||||||||||||||||||||
| 164 | if ref $self and @_; | ||||||||||||||||||||||||||||
| 165 | |||||||||||||||||||||||||||||
| 166 | 0 | ▶ | if (@_) { | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 167 | 0 | ▶ | $Query_cache->clear() if $Query_cache; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 182 | 0 | ▶ | @args = {@args} unless ref $args[0]; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 183 | 0 | my $query = $self->_query_cache->get_query_from_cache($args_for_cache_key); | |||||||||||||||||||||||||||
| 184 | 0 | ▶ | if ( not $query ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 185 | 0 | $query = $self->new(@args); | |||||||||||||||||||||||||||
| 186 | 0 | ▶ | $self->_query_cache->add_query_to_cache( $query, $args_for_cache_key ) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 187 | if $query; | ||||||||||||||||||||||||||||
| 188 | } | ||||||||||||||||||||||||||||
| 189 | 0 | return $query; | |||||||||||||||||||||||||||
| 190 | } | ||||||||||||||||||||||||||||
| 191 | |||||||||||||||||||||||||||||
| 192 | 2 | 1; | |||||||||||||||||||||||||||
| 193 | |||||||||||||||||||||||||||||
| 194 | __END__ | ||||||||||||||||||||||||||||