| 1 | package DBIx::Foo::SimpleQuery; | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | 4 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | |||||||||||||||||||||||||||||
| 5 | 4 | ▶ | use Log::Any qw($log); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||
| 7 | 4 | ▶ | use Exporter; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||
| 9 | 4 | our @ISA = qw(Exporter); | |||||||||||||||||||||||||||
| 10 | 4 | our @EXPORT = qw(selectrow selectrow_array selectrow_hashref selectall selectall_arrayref selectall_hashref dbh_do); | |||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||
| 12 | 4 | our $VERSION = '0.01'; | |||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||
| 15 | sub selectrow | ||||||||||||||||||||||||||||
| 16 | 0 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
selectrow: not called CC=2 SCAR=17.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 17 | 0 | my ($self, $sql, $opts, @args) = @_; | |||||||||||||||||||||||||||
| 18 | |||||||||||||||||||||||||||||
| 19 | 0 | ▶ | nice_params(\$opts, \@args) if scalar @_ > 2; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 20 | |||||||||||||||||||||||||||||
| 21 | 0 | my $row = $self->dbh->selectrow_hashref($sql, $opts, @args); | |||||||||||||||||||||||||||
| 22 | |||||||||||||||||||||||||||||
| 23 | 0 | log_query($self->dbh, $sql, \@args); | |||||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||||||
| 25 | 0 | return $row; | |||||||||||||||||||||||||||
| 26 | } | ||||||||||||||||||||||||||||
| 27 | |||||||||||||||||||||||||||||
| 28 | sub selectrow_hashref | ||||||||||||||||||||||||||||
| 29 | 0 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
selectrow_hashref: not called CC=2 SCAR=17.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 30 | 0 | my ($self, $sql, $opts, @args) = @_; | |||||||||||||||||||||||||||
| 31 | |||||||||||||||||||||||||||||
| 32 | 0 | ▶ | nice_params(\$opts, \@args) if scalar @_ > 2; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 33 | |||||||||||||||||||||||||||||
| 34 | 0 | my $row = $self->dbh->selectrow_hashref($sql, $opts, @args); | |||||||||||||||||||||||||||
| 35 | |||||||||||||||||||||||||||||
| 36 | 0 | log_query($self->dbh, $sql, \@args); | |||||||||||||||||||||||||||
| 37 | |||||||||||||||||||||||||||||
| 38 | 0 | return $row; | |||||||||||||||||||||||||||
| 39 | } | ||||||||||||||||||||||||||||
| 40 | |||||||||||||||||||||||||||||
| 41 | sub selectrow_array | ||||||||||||||||||||||||||||
| 42 | 2 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
selectrow_array: called CC=3 SCAR=11.2
Pod
undocumented
| |||||||||||||||||||||||||||||
| 43 | 2 | my ($self, $sql, $opts, @args) = @_; | |||||||||||||||||||||||||||
| 44 | |||||||||||||||||||||||||||||
| 45 | 2 | ▶ | nice_params(\$opts, \@args) if scalar @_ > 2; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 46 | |||||||||||||||||||||||||||||
| 47 | 2 | my @row = $self->dbh->selectrow_array($sql, $opts, @args); | |||||||||||||||||||||||||||
| 48 | |||||||||||||||||||||||||||||
| 49 | 2 | log_query($self->dbh, $sql, \@args); | |||||||||||||||||||||||||||
| 50 | |||||||||||||||||||||||||||||
| 51 | 2 | ▶ | return $row[0] if scalar @row == 1; # for compatibility | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 52 | |||||||||||||||||||||||||||||
| 53 | 2 | return @row; | |||||||||||||||||||||||||||
| 54 | } | ||||||||||||||||||||||||||||
| 55 | |||||||||||||||||||||||||||||
| 56 | sub selectall | ||||||||||||||||||||||||||||
| 57 | 0 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
selectall: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 58 | 0 | my ($self, $sql, @args) = @_; | |||||||||||||||||||||||||||
| 59 | |||||||||||||||||||||||||||||
| 60 | 0 | my $opts = { Slice => {} }; | |||||||||||||||||||||||||||
| 61 | |||||||||||||||||||||||||||||
| 62 | 0 | my $rows = $self->dbh->selectall_arrayref($sql, $opts, @args); | |||||||||||||||||||||||||||
| 63 | |||||||||||||||||||||||||||||
| 64 | 0 | log_query($self->dbh, $sql, \@args); | |||||||||||||||||||||||||||
| 65 | |||||||||||||||||||||||||||||
| 66 | 0 | return $rows; | |||||||||||||||||||||||||||
| 67 | } | ||||||||||||||||||||||||||||
| 68 | |||||||||||||||||||||||||||||
| 69 | sub selectall_arrayref | ||||||||||||||||||||||||||||
| 70 | 3 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
selectall_arrayref: called CC=2 SCAR=7.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 71 | 3 | my ($self, $sql, $opts, @args) = @_; | |||||||||||||||||||||||||||
| 72 | |||||||||||||||||||||||||||||
| 73 | 3 | ▶ | nice_params(\$opts, \@args) if scalar @_ > 2; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 74 | |||||||||||||||||||||||||||||
| 75 | 3 | my $rows = $self->dbh->selectall_arrayref($sql, $opts, @args); | |||||||||||||||||||||||||||
| 76 | |||||||||||||||||||||||||||||
| 77 | 3 | log_query($self->dbh, $sql, \@args); | |||||||||||||||||||||||||||
| 78 | |||||||||||||||||||||||||||||
| 79 | 3 | return $rows; | |||||||||||||||||||||||||||
| 80 | } | ||||||||||||||||||||||||||||
| 81 | |||||||||||||||||||||||||||||
| 82 | sub selectall_hashref | ||||||||||||||||||||||||||||
| 83 | 0 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
selectall_hashref: not called CC=2 SCAR=17.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 84 | 0 | my ($self, $sql, $key_field, $opts, @args) = @_; | |||||||||||||||||||||||||||
| 85 | |||||||||||||||||||||||||||||
| 86 | 0 | ▶ | nice_params(\$opts, \@args) if scalar @_ > 3; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 87 | |||||||||||||||||||||||||||||
| 88 | 0 | my $rows = $self->dbh->selectall_hashref($sql, $key_field, $opts, @args); | |||||||||||||||||||||||||||
| 89 | |||||||||||||||||||||||||||||
| 90 | 0 | log_query($self->dbh, $sql, \@args); | |||||||||||||||||||||||||||
| 91 | |||||||||||||||||||||||||||||
| 92 | 0 | return $rows; | |||||||||||||||||||||||||||
| 93 | } | ||||||||||||||||||||||||||||
| 94 | |||||||||||||||||||||||||||||
| 95 | sub dbh_do | ||||||||||||||||||||||||||||
| 96 | 24 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
dbh_do: called CC=7 SCAR=20.7
Pod
undocumented
| |||||||||||||||||||||||||||||
| 97 | 24 | my ($self, $sql, $opts, @args) = @_; | |||||||||||||||||||||||||||
| 98 | |||||||||||||||||||||||||||||
| 99 | 24 | ▶ | nice_params(\$opts, \@args) if scalar @_ > 2; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 100 | |||||||||||||||||||||||||||||
| 101 | # MSSQL insert requires extra SCOPE_IDENTITY() call to give inserted value - current best solution MT (only way I can make it work...) | ||||||||||||||||||||||||||||
| 102 | 24 | ▶ | if ($self->dbh->get_info(17) eq 'Microsoft SQL Server' && $sql =~ /^insert/i) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$self->dbh->get_info(17) eq "Microsoft SQL Server" and $sql =~ /^insert/i
MC/DC0%0/2
$self->dbh->get_info(17) eq "Microsoft SQL Server" and $sql =~ /^insert/i
$self->dbh->get_info(17) eq "Microsoft SQL Server"
$sql =~ /^insert/i
Truth table33%1/3
A and B
A= $self->dbh->get_info(17) eq "Microsoft SQL Server"B= $sql =~ /^insert/i
| |||||||||||||||||||||||||||||
| 103 | |||||||||||||||||||||||||||||
| 104 | 0 | return mssql_insert($self->dbh, $sql, $opts, @args); | |||||||||||||||||||||||||||
| 105 | } | ||||||||||||||||||||||||||||
| 106 | |||||||||||||||||||||||||||||
| 107 | 24 | my $result = $self->dbh->do($sql, $opts, @args); | |||||||||||||||||||||||||||
| 108 | |||||||||||||||||||||||||||||
| 109 | 23 | log_query($self->dbh, $sql, \@args); | |||||||||||||||||||||||||||
| 110 | |||||||||||||||||||||||||||||
| 111 | 23 | ▶ | if ($result && $sql =~ /^insert into (\w+)/i) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$result and $sql =~ /^insert into (\w+)/i
MC/DC50%1/2
$result and $sql =~ /^insert into (\w+)/i
$result
$sql =~ /^insert into (\w+)/i
Truth table66%2/3
A and B
A= $resultB= $sql =~ /^insert into (\w+)/i
| |||||||||||||||||||||||||||||
| 112 | |||||||||||||||||||||||||||||
| 113 | 19 | ▶ | if (my $newid = $self->dbh->last_insert_id(undef, undef, $1, undef)) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 114 | |||||||||||||||||||||||||||||
| 115 | 19 | $log->debug("Got insertid : $newid"); | |||||||||||||||||||||||||||
| 116 | |||||||||||||||||||||||||||||
| 117 | 19 | return $newid; | |||||||||||||||||||||||||||
| 118 | } | ||||||||||||||||||||||||||||
| 119 | else { | ||||||||||||||||||||||||||||
| 120 | 0 | return 1; # insert ok, but no insertid (not an auto inc) | |||||||||||||||||||||||||||
| 121 | } | ||||||||||||||||||||||||||||
| 122 | } | ||||||||||||||||||||||||||||
| 123 | else { | ||||||||||||||||||||||||||||
| 124 | |||||||||||||||||||||||||||||
| 125 | 4 | return $result; | |||||||||||||||||||||||||||
| 126 | } | ||||||||||||||||||||||||||||
| 127 | } | ||||||||||||||||||||||||||||
| 128 | |||||||||||||||||||||||||||||
| 129 | sub mssql_insert | ||||||||||||||||||||||||||||
| 130 | 0 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
mssql_insert: not called CC=2 SCAR=17.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 131 | 0 | my ($dbh, $sql, $opts, @args) = @_; | |||||||||||||||||||||||||||
| 132 | |||||||||||||||||||||||||||||
| 133 | 0 | ▶ | $sql .= ';' unless $sql =~ /;\s*/; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 134 | 0 | $sql .= "select SCOPE_IDENTITY();"; | |||||||||||||||||||||||||||
| 135 | |||||||||||||||||||||||||||||
| 136 | 0 | my $newid = $dbh->selectrow_array($sql, $opts, @args); | |||||||||||||||||||||||||||
| 137 | |||||||||||||||||||||||||||||
| 138 | 0 | log_query($dbh, $sql, \@args); | |||||||||||||||||||||||||||
| 139 | |||||||||||||||||||||||||||||
| 140 | 0 | return $newid; | |||||||||||||||||||||||||||
| 141 | } | ||||||||||||||||||||||||||||
| 142 | |||||||||||||||||||||||||||||
| 143 | sub log_query | ||||||||||||||||||||||||||||
| 144 | 28 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
log_query: called CC=2 SCAR=7.4
Pod
undocumented
| |||||||||||||||||||||||||||||
| 145 | 28 | my ($dbh, $sql, $args) = @_; | |||||||||||||||||||||||||||
| 146 | |||||||||||||||||||||||||||||
| 147 | # use the 'caller' function name to work out context | ||||||||||||||||||||||||||||
| 148 | 28 | my $caller = ( caller(2) )[3]; | |||||||||||||||||||||||||||
| 149 | |||||||||||||||||||||||||||||
| 150 | 28 | ▶ | if ($dbh->err) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 151 | |||||||||||||||||||||||||||||
| 152 | 0 | $log->error($dbh->errstr . " - $sql (" . join(",", @$args) . ") called by $caller"); | |||||||||||||||||||||||||||
| 153 | |||||||||||||||||||||||||||||
| 154 | } else { | ||||||||||||||||||||||||||||
| 155 | |||||||||||||||||||||||||||||
| 156 | 28 | $log->debug("$sql (" . join(",", @$args) . ") called by $caller"); | |||||||||||||||||||||||||||
| 157 | } | ||||||||||||||||||||||||||||
| 158 | } | ||||||||||||||||||||||||||||
| 159 | |||||||||||||||||||||||||||||
| 160 | sub nice_params | ||||||||||||||||||||||||||||
| 161 | 15 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
nice_params: called CC=2 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 162 | 15 | my ($opts_ref, $args_ref) = @_; | |||||||||||||||||||||||||||
| 163 | |||||||||||||||||||||||||||||
| 164 | 15 | my $opts = $$opts_ref; | |||||||||||||||||||||||||||
| 165 | |||||||||||||||||||||||||||||
| 166 | #return unless defined $opts; | ||||||||||||||||||||||||||||
| 167 | |||||||||||||||||||||||||||||
| 168 | 15 | ▶ | unless (ref($opts) eq 'HASH') { #allow $opts hashref to be omitted | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 169 | |||||||||||||||||||||||||||||
| 170 | 12 | unshift @$args_ref, $opts; | |||||||||||||||||||||||||||
| 171 | 12 | $$opts_ref = {}; | |||||||||||||||||||||||||||
| 172 | |||||||||||||||||||||||||||||
| 173 | 12 | $opts_ref = \$opts; | |||||||||||||||||||||||||||
| 174 | } | ||||||||||||||||||||||||||||
| 175 | } | ||||||||||||||||||||||||||||
| 176 | |||||||||||||||||||||||||||||
| 177 | 4 | 1; | |||||||||||||||||||||||||||