| 1 | 1 | ▶ | use 5.10.0; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 2 | package DBIx::Oracle::UpgradeUtf8; | ||||||||||||||||||||||||||||||||||||||||||
| 3 | 1 | ▶ | use utf8; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 4 | 1 | ▶ | use strict; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 5 | 1 | ▶ | use warnings; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 6 | 1 | ▶ | use Scalar::Util qw/looks_like_number/; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||||||||||||||||
| 8 | 1 | our $VERSION = 1.01; | |||||||||||||||||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||||||||||||||||
| 10 | 1 | my @default_dbh_methods = qw/do | |||||||||||||||||||||||||||||||||||||||||
| 11 | prepare | ||||||||||||||||||||||||||||||||||||||||||
| 12 | selectrow_array | ||||||||||||||||||||||||||||||||||||||||||
| 13 | selectrow_arrayref | ||||||||||||||||||||||||||||||||||||||||||
| 14 | selectrow_hashref | ||||||||||||||||||||||||||||||||||||||||||
| 15 | selectall_arrayref | ||||||||||||||||||||||||||||||||||||||||||
| 16 | selectall_array | ||||||||||||||||||||||||||||||||||||||||||
| 17 | selectall_hashref | ||||||||||||||||||||||||||||||||||||||||||
| 18 | selectcol_arrayref/; | ||||||||||||||||||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||||||||||||||||
| 20 | 1 | my @default_sth_methods = qw/bind_param | |||||||||||||||||||||||||||||||||||||||||
| 21 | bind_param_array | ||||||||||||||||||||||||||||||||||||||||||
| 22 | execute | ||||||||||||||||||||||||||||||||||||||||||
| 23 | execute_array/; | ||||||||||||||||||||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||||||||||||||||||||
| 25 | sub new { | ||||||||||||||||||||||||||||||||||||||||||
| 26 | 0 | ▶ | my ($class, %options) = @_; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
new: not called CC=13 SCAR=52.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 27 | |||||||||||||||||||||||||||||||||||||||||||
| 28 | # check validity of args | ||||||||||||||||||||||||||||||||||||||||||
| 29 | 0 | my $error = __PACKAGE__ . "->new()"; | |||||||||||||||||||||||||||||||||||||||||
| 30 | 0 | ▶ | for ($options{debug}) {!$_ or ref $_ eq 'CODE' or die "$error: 'debug' should be a coderef"} | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
not $_ or ref $_ eq "CODE"
MC/DC0%0/2
not $_ or ref $_ eq "CODE"
not $_
ref $_ eq "CODE"
Truth table0%0/3
not(A) or B
A= not $_B= ref $_ eq "CODE"
| |||||||||||||||||||||||||||||||||||||||||||
| 31 | 0 | ▶ | for ($options{dbh_methods}) {!$_ or ref $_ eq 'ARRAY' or die "$error: 'dbh_methods' should be an arrayref"} | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
not $_ or ref $_ eq "ARRAY"
MC/DC0%0/2
not $_ or ref $_ eq "ARRAY"
not $_
ref $_ eq "ARRAY"
Truth table0%0/3
not(A) or B
A= not $_B= ref $_ eq "ARRAY"
| |||||||||||||||||||||||||||||||||||||||||||
| 32 | 0 | ▶ | for ($options{sth_methods}) {!$_ or ref $_ eq 'ARRAY' or die "$error: 'sth_methods' should be an arrayref"} | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
not $_ or ref $_ eq "ARRAY"
MC/DC0%0/2
not $_ or ref $_ eq "ARRAY"
not $_
ref $_ eq "ARRAY"
Truth table0%0/3
not(A) or B
A= not $_B= ref $_ eq "ARRAY"
| |||||||||||||||||||||||||||||||||||||||||||
| 33 | |||||||||||||||||||||||||||||||||||||||||||
| 34 | # build object internals | ||||||||||||||||||||||||||||||||||||||||||
| 35 | my $self = { | ||||||||||||||||||||||||||||||||||||||||||
| 36 | debug => delete $options{debug}, | ||||||||||||||||||||||||||||||||||||||||||
| 37 | dbh_methods => delete $options{dbh_methods} // \@default_dbh_methods, | ||||||||||||||||||||||||||||||||||||||||||
| 38 | 0 | ▶ | sth_methods => delete $options{sth_methods} // \@default_sth_methods, | ||||||||||||||||||||||||||||||||||||||||
|
Condition0%0/4
delete $options{"dbh_methods"} // \@default_dbh_methods
delete $options{"sth_methods"} // \@default_sth_methods
MC/DC0%0/1
delete $options{"dbh_methods"} // \@default_dbh_methods
delete $options{"dbh_methods"}
MC/DC0%0/1
delete $options{"sth_methods"} // \@default_sth_methods
delete $options{"sth_methods"}
Truth table0%0/2
A
A= delete $options{"dbh_methods"}
Truth table0%0/2
A
A= delete $options{"sth_methods"}
| |||||||||||||||||||||||||||||||||||||||||||
| 39 | }; | ||||||||||||||||||||||||||||||||||||||||||
| 40 | |||||||||||||||||||||||||||||||||||||||||||
| 41 | # check that all options have been consumed | ||||||||||||||||||||||||||||||||||||||||||
| 42 | 0 | my @invalid_options = keys %options; | |||||||||||||||||||||||||||||||||||||||||
| 43 | 0 | ▶ | die "$error: invalid options : " . join " / ", @invalid_options if @invalid_options; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 44 | |||||||||||||||||||||||||||||||||||||||||||
| 45 | # return object | ||||||||||||||||||||||||||||||||||||||||||
| 46 | 0 | bless $self, $class; | |||||||||||||||||||||||||||||||||||||||||
| 47 | } | ||||||||||||||||||||||||||||||||||||||||||
| 48 | |||||||||||||||||||||||||||||||||||||||||||
| 49 | |||||||||||||||||||||||||||||||||||||||||||
| 50 | sub inject_callbacks { | ||||||||||||||||||||||||||||||||||||||||||
| 51 | 0 | ▶ | my ($self, $dbh, @invalid_args) = @_; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
inject_callbacks: not called CC=7 SCAR=40.3
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 52 | |||||||||||||||||||||||||||||||||||||||||||
| 53 | # check input args | ||||||||||||||||||||||||||||||||||||||||||
| 54 | 0 | ▶ | $dbh->isa('DBI::db') or die '->inject_callbacks() : arg is not a database handle'; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 55 | 0 | ▶ | !@invalid_args or die '->inject_callbacks() : too many args'; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 56 | |||||||||||||||||||||||||||||||||||||||||||
| 57 | # coderef to be installed as common callback for all methods. This is a closure on $debug. | ||||||||||||||||||||||||||||||||||||||||||
| 58 | 0 | my $debug = $self->{debug}; # Copy for easier reference. The coderef will be a closure on $debug. | |||||||||||||||||||||||||||||||||||||||||
| 59 | my $upgrade_string_args = sub { | ||||||||||||||||||||||||||||||||||||||||||
| 60 | 0 | ▶ | $debug->("$_ callback") if $debug; # Note: $_ is the method name | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=14 SCAR=53.5
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 61 | |||||||||||||||||||||||||||||||||||||||||||
| 62 | # all strings in @_ will be upgraded (in-place, not copies) | ||||||||||||||||||||||||||||||||||||||||||
| 63 | ARG: | ||||||||||||||||||||||||||||||||||||||||||
| 64 | 0 | foreach my $i (1 .. $#_) { # start only at 1 because $_[0] is the DBI handle | |||||||||||||||||||||||||||||||||||||||||
| 65 | |||||||||||||||||||||||||||||||||||||||||||
| 66 | # if arg is undef or empty string or 0, there is nothing to do | ||||||||||||||||||||||||||||||||||||||||||
| 67 | 0 | ▶ | next ARG if !$_[$i]; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 68 | |||||||||||||||||||||||||||||||||||||||||||
| 69 | # if arg is a scalar and is a native string, upgrade it | ||||||||||||||||||||||||||||||||||||||||||
| 70 | 0 | ▶ | if (! ref $_[$i]) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||
| 71 | 0 | ▶ | next ARG if looks_like_number($_[$i]) or utf8::is_utf8($_[$i]); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
&looks_like_number($_[$i]) or utf8::is_utf8($_[$i])
MC/DC0%0/2
&looks_like_number($_[$i]) or utf8::is_utf8($_[$i])
&looks_like_number($_[$i])
utf8::is_utf8($_[$i])
Truth table0%0/3
A or B
A= &looks_like_number($_[$i])B= utf8::is_utf8($_[$i])
| |||||||||||||||||||||||||||||||||||||||||||
| 72 | 0 | ▶ | $debug->("upgrading arg [$i] ($_[$i])") if $debug; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 73 | 0 | utf8::upgrade($_[$i]); | |||||||||||||||||||||||||||||||||||||||||
| 74 | } | ||||||||||||||||||||||||||||||||||||||||||
| 75 | |||||||||||||||||||||||||||||||||||||||||||
| 76 | # if arg is an arrayref (used by the *_array methods), upgrade native strings in that array | ||||||||||||||||||||||||||||||||||||||||||
| 77 | elsif (ref $_[$i] eq 'ARRAY') { | ||||||||||||||||||||||||||||||||||||||||||
| 78 | 0 | ▶ | for my $val (grep {$_ && !ref $_ && !looks_like_number($_) && !utf8::is_utf8($_)} @{$_[$i]}) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/6
$_ and not ref $_ and not &looks_like_number($_)
$_ and not ref $_
MC/DC0%0/3
$_ and not ref $_ and not &looks_like_number($_)
$_
not ref $_
not &looks_like_number($_)
Truth table0%0/4
A and not(B) and not(C)
A= $_B= not ref $_C= not &looks_like_number($_)
| |||||||||||||||||||||||||||||||||||||||||||
| 79 | 0 | ▶ | $debug->("upgrading string in array arg [$i] ($val)") if $debug; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 80 | 0 | utf8::upgrade($val); | |||||||||||||||||||||||||||||||||||||||||
| 81 | } | ||||||||||||||||||||||||||||||||||||||||||
| 82 | } | ||||||||||||||||||||||||||||||||||||||||||
| 83 | } | ||||||||||||||||||||||||||||||||||||||||||
| 84 | |||||||||||||||||||||||||||||||||||||||||||
| 85 | 0 | return; # must end with an empty return (see L<DBI> documentation) | |||||||||||||||||||||||||||||||||||||||||
| 86 | 0 | }; | |||||||||||||||||||||||||||||||||||||||||
| 87 | |||||||||||||||||||||||||||||||||||||||||||
| 88 | # inject callbacks for $dbh methods and for $sth methods | ||||||||||||||||||||||||||||||||||||||||||
| 89 | 0 | ▶ | my $parent_callbacks = $dbh->{Callbacks} //= {}; | ||||||||||||||||||||||||||||||||||||||||
|
Condition0%0/2
$dbh->{"Callbacks"} //= {}
MC/DC0%0/1
$dbh->{"Callbacks"} //= {}
$dbh->{"Callbacks"}
Truth table0%0/2
A
A= $dbh->{"Callbacks"}
| |||||||||||||||||||||||||||||||||||||||||||
| 90 | 0 | ▶ | my $child_callbacks = $parent_callbacks->{ChildCallbacks} //= {}; | ||||||||||||||||||||||||||||||||||||||||
|
Condition0%0/2
$parent_callbacks->{"ChildCallbacks"} //= {}
MC/DC0%0/1
$parent_callbacks->{"ChildCallbacks"} //= {}
$parent_callbacks->{"ChildCallbacks"}
Truth table0%0/2
A
A= $parent_callbacks->{"ChildCallbacks"}
| |||||||||||||||||||||||||||||||||||||||||||
| 91 | 0 | inject_callback($parent_callbacks, $_ => $upgrade_string_args) for @{$self->{dbh_methods}}; | |||||||||||||||||||||||||||||||||||||||||
| 92 | 0 | inject_callback($child_callbacks, $_ => $upgrade_string_args) for @{$self->{sth_methods}}; | |||||||||||||||||||||||||||||||||||||||||
| 93 | } | ||||||||||||||||||||||||||||||||||||||||||
| 94 | |||||||||||||||||||||||||||||||||||||||||||
| 95 | |||||||||||||||||||||||||||||||||||||||||||
| 96 | sub inject_callback { | ||||||||||||||||||||||||||||||||||||||||||
| 97 | 0 | ▶ | my ($hash, $key, $coderef) = @_; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
inject_callback: not called CC=2 SCAR=17.9
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 98 | |||||||||||||||||||||||||||||||||||||||||||
| 99 | # in case a previous callback was already installed, we replace it with a sub that combines both | ||||||||||||||||||||||||||||||||||||||||||
| 100 | 0 | my $previous_cb = $hash->{$key}; | |||||||||||||||||||||||||||||||||||||||||
| 101 | 0 | ▶ | my $new_cb = $previous_cb ? sub {&$coderef; &$previous_cb} : $coderef; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 102 | |||||||||||||||||||||||||||||||||||||||||||
| 103 | 0 | $hash->{$key} = $new_cb; | |||||||||||||||||||||||||||||||||||||||||
| 104 | } | ||||||||||||||||||||||||||||||||||||||||||
| 105 | |||||||||||||||||||||||||||||||||||||||||||
| 106 | 1 | 1; | |||||||||||||||||||||||||||||||||||||||||
| 107 | |||||||||||||||||||||||||||||||||||||||||||
| 108 | |||||||||||||||||||||||||||||||||||||||||||
| 109 | __END__ | ||||||||||||||||||||||||||||||||||||||||||