| 1 | 54 | ▶ | use strict; | ||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||
| 2 | 54 | ▶ | use warnings; | ||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||
| 3 | { | ||||||||||||||||||||||
| 4 | package DBD::Sponge; | ||||||||||||||||||||||
| 5 | |||||||||||||||||||||||
| 6 | 54 | require DBI; | |||||||||||||||||||||
| 7 | 54 | require Carp; | |||||||||||||||||||||
| 8 | |||||||||||||||||||||||
| 9 | 54 | our @EXPORT = qw(); # Do NOT @EXPORT anything. | |||||||||||||||||||||
| 10 | 54 | our $VERSION = "12.010003"; | |||||||||||||||||||||
| 11 | |||||||||||||||||||||||
| 12 | # $Id: Sponge.pm 10002 2007-09-26 21:03:25Z Tim $ | ||||||||||||||||||||||
| 13 | # | ||||||||||||||||||||||
| 14 | # Copyright (c) 1994-2003 Tim Bunce Ireland | ||||||||||||||||||||||
| 15 | # | ||||||||||||||||||||||
| 16 | # You may distribute under the terms of either the GNU General Public | ||||||||||||||||||||||
| 17 | # License or the Artistic License, as specified in the Perl README file. | ||||||||||||||||||||||
| 18 | |||||||||||||||||||||||
| 19 | 54 | our $drh = undef; # holds driver handle once initialised | |||||||||||||||||||||
| 20 | 54 | my $methods_already_installed; | |||||||||||||||||||||
| 21 | |||||||||||||||||||||||
| 22 | 56 | ▶ | sub driver{ | ||||||||||||||||||||
|
Subroutine
driver: called CC=3 SCAR=11.0
Pod
undocumented
| |||||||||||||||||||||||
| 23 | 56 | ▶ | return $drh if $drh; | ||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||
| 24 | |||||||||||||||||||||||
| 25 | 56 | ▶ | DBD::Sponge::db->install_method("sponge_test_installed_method") | ||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||
| 26 | unless $methods_already_installed++; | ||||||||||||||||||||||
| 27 | |||||||||||||||||||||||
| 28 | 56 | my($class, $attr) = @_; | |||||||||||||||||||||
| 29 | 56 | $class .= "::dr"; | |||||||||||||||||||||
| 30 | 56 | ($drh) = DBI::_new_drh($class, { | |||||||||||||||||||||
| 31 | 'Name' => 'Sponge', | ||||||||||||||||||||||
| 32 | 'Version' => $VERSION, | ||||||||||||||||||||||
| 33 | 'Attribution' => "DBD::Sponge $VERSION (fake cursor driver) by Tim Bunce", | ||||||||||||||||||||||
| 34 | }); | ||||||||||||||||||||||
| 35 | 54 | $drh; | |||||||||||||||||||||
| 36 | } | ||||||||||||||||||||||
| 37 | |||||||||||||||||||||||
| 38 | 0 | ▶ | sub CLONE { | ||||||||||||||||||||
|
Subroutine
CLONE: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||
| 39 | 0 | undef $drh; | |||||||||||||||||||||
| 40 | } | ||||||||||||||||||||||
| 41 | } | ||||||||||||||||||||||
| 42 | |||||||||||||||||||||||
| 43 | |||||||||||||||||||||||
| 44 | 54 | { package DBD::Sponge::dr; # ====== DRIVER ====== | |||||||||||||||||||||
| 45 | 54 | our $imp_data_size = 0; | |||||||||||||||||||||
| 46 | # we use default (dummy) connect method | ||||||||||||||||||||||
| 47 | } | ||||||||||||||||||||||
| 48 | |||||||||||||||||||||||
| 49 | |||||||||||||||||||||||
| 50 | 54 | { package DBD::Sponge::db; # ====== DATABASE ====== | |||||||||||||||||||||
| 51 | 54 | our $imp_data_size = 0; | |||||||||||||||||||||
| 52 | 54 | ▶ | use strict; | ||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||
| 53 | |||||||||||||||||||||||
| 54 | 100 | ▶ | sub prepare { | ||||||||||||||||||||
|
Subroutine
prepare: called CC=16 SCAR=28.6
| |||||||||||||||||||||||
| 55 | 100 | my($dbh, $statement, $attribs) = @_; | |||||||||||||||||||||
| 56 | 100 | ▶ | my $rows = delete $attribs->{'rows'} | ||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||
| 57 | or return $dbh->set_err($DBI::stderr,"No rows attribute supplied to prepare"); | ||||||||||||||||||||||
| 58 | my ($outer, $sth) = DBI::_new_sth($dbh, { | ||||||||||||||||||||||
| 59 | 'Statement' => $statement, | ||||||||||||||||||||||
| 60 | 'rows' => $rows, | ||||||||||||||||||||||
| 61 | 100 | ▶ | (map { exists $attribs->{$_} ? ($_=>$attribs->{$_}) : () } | ||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||
| 62 | qw(execute_hook) | ||||||||||||||||||||||
| 63 | ), | ||||||||||||||||||||||
| 64 | }); | ||||||||||||||||||||||
| 65 | 100 | ▶ | if (my $behave_like = $attribs->{behave_like}) { | ||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||
| 66 | $outer->{$_} = $behave_like->{$_} | ||||||||||||||||||||||
| 67 | 0 | foreach (qw(RaiseError PrintError HandleError ShowErrorStatement)); | |||||||||||||||||||||
| 68 | } | ||||||||||||||||||||||
| 69 | |||||||||||||||||||||||
| 70 | 100 | ▶ | if ($statement =~ /^\s*insert\b/) { # very basic, just for testing execute_array() | ||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||
| 71 | 8 | $sth->{is_insert} = 1; | |||||||||||||||||||||
| 72 | my $NUM_OF_PARAMS = $attribs->{NUM_OF_PARAMS} | ||||||||||||||||||||||
| 73 | 8 | ▶ | or return $dbh->set_err($DBI::stderr,"NUM_OF_PARAMS not specified for INSERT statement"); | ||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||
| 74 | 8 | $sth->STORE('NUM_OF_PARAMS' => $attribs->{NUM_OF_PARAMS} ); | |||||||||||||||||||||
| 75 | } | ||||||||||||||||||||||
| 76 | else { #assume select | ||||||||||||||||||||||
| 77 | |||||||||||||||||||||||
| 78 | # we need to set NUM_OF_FIELDS | ||||||||||||||||||||||
| 79 | 92 | my $numFields; | |||||||||||||||||||||
| 80 | 92 | ▶ | if ($attribs->{'NUM_OF_FIELDS'}) { | ||||||||||||||||||||
|
Branch: 4 branches
| |||||||||||||||||||||||
| 81 | 20 | $numFields = $attribs->{'NUM_OF_FIELDS'}; | |||||||||||||||||||||
| 82 | } elsif ($attribs->{'NAME'}) { | ||||||||||||||||||||||
| 83 | 60 | $numFields = @{$attribs->{NAME}}; | |||||||||||||||||||||
| 84 | } elsif ($attribs->{'TYPE'}) { | ||||||||||||||||||||||
| 85 | 0 | $numFields = @{$attribs->{TYPE}}; | |||||||||||||||||||||
| 86 | } elsif (my $firstrow = $rows->[0]) { | ||||||||||||||||||||||
| 87 | 12 | $numFields = scalar @$firstrow; | |||||||||||||||||||||
| 88 | } else { | ||||||||||||||||||||||
| 89 | 0 | return $dbh->set_err($DBI::stderr, 'Cannot determine NUM_OF_FIELDS'); | |||||||||||||||||||||
| 90 | } | ||||||||||||||||||||||
| 91 | 92 | $sth->STORE('NUM_OF_FIELDS' => $numFields); | |||||||||||||||||||||
| 92 | $sth->{NAME} = $attribs->{NAME} | ||||||||||||||||||||||
| 93 | 92 | ▶ | || [ map { "col$_" } 1..$numFields ]; | ||||||||||||||||||||
|
Condition100%2/2
$attribs->{'NAME'} || [map({"col$_";} 1 .. $numFields)]
MC/DC100%1/1
$attribs->{'NAME'} || [map({"col$_";} 1 .. $numFields)]
$attribs->{'NAME'}
Truth table100%2/2
A
A= $attribs->{'NAME'}
| |||||||||||||||||||||||
| 94 | $sth->{TYPE} = $attribs->{TYPE} | ||||||||||||||||||||||
| 95 | 92 | ▶ | || [ (DBI::SQL_VARCHAR()) x $numFields ]; | ||||||||||||||||||||
|
Condition100%2/2
$attribs->{'TYPE'} || [(&DBI::SQL_VARCHAR()) x $numFields]
MC/DC100%1/1
$attribs->{'TYPE'} || [(&DBI::SQL_VARCHAR()) x $numFields]
$attribs->{'TYPE'}
Truth table100%2/2
A
A= $attribs->{'TYPE'}
| |||||||||||||||||||||||
| 96 | $sth->{SCALE} = $attribs->{SCALE} | ||||||||||||||||||||||
| 97 | 92 | ▶ | || [ (0) x $numFields ]; | ||||||||||||||||||||
|
Condition50%1/2
$attribs->{'SCALE'} || [(0) x $numFields]
MC/DC0%0/1
$attribs->{'SCALE'} || [(0) x $numFields]
$attribs->{'SCALE'}
Truth table50%1/2
A
A= $attribs->{'SCALE'}
| |||||||||||||||||||||||
| 98 | $sth->{NULLABLE} = $attribs->{NULLABLE} | ||||||||||||||||||||||
| 99 | 92 | ▶ | || [ (2) x $numFields ]; | ||||||||||||||||||||
|
Condition100%2/2
$attribs->{'NULLABLE'} || [(2) x $numFields]
MC/DC100%1/1
$attribs->{'NULLABLE'} || [(2) x $numFields]
$attribs->{'NULLABLE'}
Truth table100%2/2
A
A= $attribs->{'NULLABLE'}
| |||||||||||||||||||||||
| 100 | # Allow user to specify precision, otherwise | ||||||||||||||||||||||
| 101 | # FETCH will lazily compute if needed | ||||||||||||||||||||||
| 102 | 92 | ▶ | if ($attribs->{PRECISION}) { | ||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||
| 103 | 4 | $sth->{PRECISION} = $attribs->{PRECISION}; | |||||||||||||||||||||
| 104 | } | ||||||||||||||||||||||
| 105 | } | ||||||||||||||||||||||
| 106 | |||||||||||||||||||||||
| 107 | 100 | $outer; | |||||||||||||||||||||
| 108 | } | ||||||||||||||||||||||
| 109 | |||||||||||||||||||||||
| 110 | 0 | ▶ | sub type_info_all { | ||||||||||||||||||||
|
Subroutine
type_info_all: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||
| 111 | 0 | my ($dbh) = @_; | |||||||||||||||||||||
| 112 | 0 | my $ti = [ | |||||||||||||||||||||
| 113 | { TYPE_NAME => 0, | ||||||||||||||||||||||
| 114 | DATA_TYPE => 1, | ||||||||||||||||||||||
| 115 | PRECISION => 2, | ||||||||||||||||||||||
| 116 | LITERAL_PREFIX => 3, | ||||||||||||||||||||||
| 117 | LITERAL_SUFFIX => 4, | ||||||||||||||||||||||
| 118 | CREATE_PARAMS => 5, | ||||||||||||||||||||||
| 119 | NULLABLE => 6, | ||||||||||||||||||||||
| 120 | CASE_SENSITIVE => 7, | ||||||||||||||||||||||
| 121 | SEARCHABLE => 8, | ||||||||||||||||||||||
| 122 | UNSIGNED_ATTRIBUTE=> 9, | ||||||||||||||||||||||
| 123 | MONEY => 10, | ||||||||||||||||||||||
| 124 | AUTO_INCREMENT => 11, | ||||||||||||||||||||||
| 125 | LOCAL_TYPE_NAME => 12, | ||||||||||||||||||||||
| 126 | MINIMUM_SCALE => 13, | ||||||||||||||||||||||
| 127 | MAXIMUM_SCALE => 14, | ||||||||||||||||||||||
| 128 | }, | ||||||||||||||||||||||
| 129 | [ 'VARCHAR', DBI::SQL_VARCHAR(), undef, "'","'", undef, 0, 1, 1, 0, 0,0,undef,0,0 ], | ||||||||||||||||||||||
| 130 | ]; | ||||||||||||||||||||||
| 131 | 0 | return $ti; | |||||||||||||||||||||
| 132 | } | ||||||||||||||||||||||
| 133 | |||||||||||||||||||||||
| 134 | 88 | ▶ | sub FETCH { | ||||||||||||||||||||
|
Subroutine
FETCH: called CC=2 SCAR=6.9
| |||||||||||||||||||||||
| 135 | 88 | my ($dbh, $attrib) = @_; | |||||||||||||||||||||
| 136 | # In reality this would interrogate the database engine to | ||||||||||||||||||||||
| 137 | # either return dynamic values that cannot be precomputed | ||||||||||||||||||||||
| 138 | # or fetch and cache attribute values too expensive to prefetch. | ||||||||||||||||||||||
| 139 | 88 | ▶ | return 1 if $attrib eq 'AutoCommit'; | ||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||
| 140 | # else pass up to DBI to handle | ||||||||||||||||||||||
| 141 | 84 | return $dbh->SUPER::FETCH($attrib); | |||||||||||||||||||||
| 142 | } | ||||||||||||||||||||||
| 143 | |||||||||||||||||||||||
| 144 | 674 | ▶ | sub STORE { | ||||||||||||||||||||
|
Subroutine
STORE: called CC=3 SCAR=11.3
| |||||||||||||||||||||||
| 145 | 674 | my ($dbh, $attrib, $value) = @_; | |||||||||||||||||||||
| 146 | # would normally validate and only store known attributes | ||||||||||||||||||||||
| 147 | # else pass up to DBI to handle | ||||||||||||||||||||||
| 148 | 674 | ▶ | if ($attrib eq 'AutoCommit') { | ||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||
| 149 | 92 | ▶ | return 1 if $value; # is already set | ||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||
| 150 | 0 | Carp::croak("Can't disable AutoCommit"); | |||||||||||||||||||||
| 151 | } | ||||||||||||||||||||||
| 152 | 582 | return $dbh->SUPER::STORE($attrib, $value); | |||||||||||||||||||||
| 153 | } | ||||||||||||||||||||||
| 154 | |||||||||||||||||||||||
| 155 | 8 | ▶ | sub sponge_test_installed_method { | ||||||||||||||||||||
|
Subroutine
sponge_test_installed_method: called CC=2 SCAR=6.9
| |||||||||||||||||||||||
| 156 | 8 | my ($dbh, @args) = @_; | |||||||||||||||||||||
| 157 | 8 | ▶ | return $dbh->set_err(42, "not enough parameters") unless @args >= 2; | ||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||
| 158 | 4 | return \@args; | |||||||||||||||||||||
| 159 | } | ||||||||||||||||||||||
| 160 | |||||||||||||||||||||||
| 161 | } | ||||||||||||||||||||||
| 162 | |||||||||||||||||||||||
| 163 | |||||||||||||||||||||||
| 164 | 54 | { package DBD::Sponge::st; # ====== STATEMENT ====== | |||||||||||||||||||||
| 165 | 54 | our $imp_data_size = 0; | |||||||||||||||||||||
| 166 | 54 | ▶ | use strict; | ||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||
| 167 | |||||||||||||||||||||||
| 168 | 116 | ▶ | sub execute { | ||||||||||||||||||||
|
Subroutine
execute: called CC=7 SCAR=19.5
| |||||||||||||||||||||||
| 169 | 116 | my $sth = shift; | |||||||||||||||||||||
| 170 | |||||||||||||||||||||||
| 171 | # hack to support ParamValues (when not using bind_param) | ||||||||||||||||||||||
| 172 | 116 | ▶ | $sth->{ParamValues} = (@_) ? { map { $_ => $_[$_-1] } 1..@_ } : undef; | ||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||
| 173 | |||||||||||||||||||||||
| 174 | 116 | ▶ | if (my $hook = $sth->{execute_hook}) { | ||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||
| 175 | 60 | ▶ | &$hook($sth, @_) or return; | ||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||
| 176 | } | ||||||||||||||||||||||
| 177 | |||||||||||||||||||||||
| 178 | 112 | ▶ | if ($sth->{is_insert}) { | ||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||
| 179 | 56 | my $row; | |||||||||||||||||||||
| 180 | 56 | ▶ | $row = (@_) ? [ @_ ] : die "bind_param not supported yet" ; | ||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||
| 181 | 56 | my $NUM_OF_PARAMS = $sth->{NUM_OF_PARAMS}; | |||||||||||||||||||||
| 182 | 56 | ▶ | return $sth->set_err($DBI::stderr, @$row." values bound (@$row) but $NUM_OF_PARAMS expected") | ||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||
| 183 | if @$row != $NUM_OF_PARAMS; | ||||||||||||||||||||||
| 184 | 54 | ▶ | { no warnings; $sth->trace_msg("inserting (@$row)\n"); } | ||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||
| 185 | 56 | push @{ $sth->{rows} }, $row; | |||||||||||||||||||||
| 186 | } | ||||||||||||||||||||||
| 187 | else { # mark select sth as Active | ||||||||||||||||||||||
| 188 | 56 | $sth->STORE(Active => 1); | |||||||||||||||||||||
| 189 | } | ||||||||||||||||||||||
| 190 | # else do nothing for select as data is already in $sth->{rows} | ||||||||||||||||||||||
| 191 | 112 | return 1; | |||||||||||||||||||||
| 192 | } | ||||||||||||||||||||||
| 193 | |||||||||||||||||||||||
| 194 | 424 | ▶ | sub fetch { | ||||||||||||||||||||
|
Subroutine
fetch: called CC=2 SCAR=6.9
| |||||||||||||||||||||||
| 195 | 424 | my ($sth) = @_; | |||||||||||||||||||||
| 196 | 424 | my $row = shift @{$sth->{'rows'}}; | |||||||||||||||||||||
| 197 | 424 | ▶ | unless ($row) { | ||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||
| 198 | 72 | $sth->STORE(Active => 0); | |||||||||||||||||||||
| 199 | 72 | return undef; | |||||||||||||||||||||
| 200 | } | ||||||||||||||||||||||
| 201 | 352 | return $sth->_set_fbav($row); | |||||||||||||||||||||
| 202 | } | ||||||||||||||||||||||
| 203 | 54 | *fetchrow_arrayref = \&fetch; | |||||||||||||||||||||
| 204 | |||||||||||||||||||||||
| 205 | 75 | ▶ | sub FETCH { | ||||||||||||||||||||
|
Subroutine
FETCH: called CC=2 SCAR=6.9
| |||||||||||||||||||||||
| 206 | 75 | my ($sth, $attrib) = @_; | |||||||||||||||||||||
| 207 | # would normally validate and only fetch known attributes | ||||||||||||||||||||||
| 208 | # else pass up to DBI to handle | ||||||||||||||||||||||
| 209 | |||||||||||||||||||||||
| 210 | 75 | ▶ | if ($attrib eq 'PRECISION') { | ||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||
| 211 | # prepare() did _not_ specify PRECISION, so lazily compute it now | ||||||||||||||||||||||
| 212 | 14 | return $sth->{PRECISION} = _max_col_lengths(@{$sth}{'NUM_OF_FIELDS', 'rows'}); | |||||||||||||||||||||
| 213 | } | ||||||||||||||||||||||
| 214 | 61 | return $sth->SUPER::FETCH($attrib); | |||||||||||||||||||||
| 215 | } | ||||||||||||||||||||||
| 216 | |||||||||||||||||||||||
| 217 | 288 | ▶ | sub STORE { | ||||||||||||||||||||
|
Subroutine
STORE: called CC=1 SCAR=0.0
| |||||||||||||||||||||||
| 218 | 288 | my ($sth, $attrib, $value) = @_; | |||||||||||||||||||||
| 219 | # would normally validate and only store known attributes | ||||||||||||||||||||||
| 220 | # else pass up to DBI to handle | ||||||||||||||||||||||
| 221 | 288 | return $sth->SUPER::STORE($attrib, $value); | |||||||||||||||||||||
| 222 | } | ||||||||||||||||||||||
| 223 | |||||||||||||||||||||||
| 224 | 14 | ▶ | sub _max_col_lengths { | ||||||||||||||||||||
|
Subroutine
_max_col_lengths: called CC=5 SCAR=16.1
| |||||||||||||||||||||||
| 225 | # compute our columns' PRECISION (data length) by looking for the | ||||||||||||||||||||||
| 226 | # max lengths of each column's data, row by row | ||||||||||||||||||||||
| 227 | 14 | my ($num_of_fields, $rows) = @_; | |||||||||||||||||||||
| 228 | 14 | my @precision = (0,) x $num_of_fields; | |||||||||||||||||||||
| 229 | 14 | my $n = $num_of_fields - 1; | |||||||||||||||||||||
| 230 | 14 | my $len; | |||||||||||||||||||||
| 231 | 14 | for my $row (@$rows) { | |||||||||||||||||||||
| 232 | 34 | for my $i (0 .. $n) { | |||||||||||||||||||||
| 233 | 146 | ▶ | next unless defined($len = length($row->[$i])); | ||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||
| 234 | 90 | ▶ | $precision[$i] = $len if $len > $precision[$i]; | ||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||
| 235 | } | ||||||||||||||||||||||
| 236 | } | ||||||||||||||||||||||
| 237 | 14 | return \@precision; | |||||||||||||||||||||
| 238 | } | ||||||||||||||||||||||
| 239 | } | ||||||||||||||||||||||
| 240 | |||||||||||||||||||||||
| 241 | 54 | 1; | |||||||||||||||||||||
| 242 | |||||||||||||||||||||||
| 243 | __END__ | ||||||||||||||||||||||