| 1 |
package DBD::Mock::db; |
| 2 |
|
| 3 |
use strict; |
| 4 |
use warnings; |
| 5 |
|
| 6 |
use List::Util qw( first ); |
| 7 |
use DBI; |
| 8 |
|
| 9 |
our $imp_data_size = 0; |
| 10 |
|
| 11 |
sub ping { |
| 12 |
my ($dbh) = @_; |
| 13 |
return $dbh->{mock_can_connect}; |
| 14 |
} |
| 15 |
|
| 16 |
sub last_insert_id { |
| 17 |
my ($dbh) = @_; |
| 18 |
return $dbh->{mock_last_insert_id}; |
| 19 |
} |
| 20 |
|
| 21 |
sub get_info { |
| 22 |
my ( $dbh, $attr ) = @_; |
| 23 |
$dbh->{mock_get_info} ||= {}; |
| 24 |
return $dbh->{mock_get_info}{$attr}; |
| 25 |
} |
| 26 |
|
| 27 |
sub table_info { |
| 28 |
my ( $dbh, @params ) = @_; |
| 29 |
|
| 30 |
my ($cataloge, $schema, $table, $type) = map { $_ || '' } @params[0..4]; |
| 31 |
|
| 32 |
$dbh->{mock_table_info} ||= {}; |
| 33 |
|
| 34 |
my @tables = @{ $dbh->{mock_table_info}->{ $cataloge }->{ $schema }->{ $table }->{ $type } || [] }; |
| 35 |
|
| 36 |
my ($fieldNames, @rows) = map { [ @$_ ] } @tables; |
| 37 |
|
| 38 |
$fieldNames ||= []; |
| 39 |
|
| 40 |
my $sponge = DBI->connect('dbi:Sponge:', '', '' ) |
| 41 |
or return $dbh->DBI::set_err($DBI::err, "DBI::Sponge: $DBI::errstr"); |
| 42 |
|
| 43 |
my $sth = $sponge->prepare("table_info", { |
| 44 |
rows => \@rows, |
| 45 |
NUM_OF_FIELDS => scalar @$fieldNames, |
| 46 |
NAME => $fieldNames |
| 47 |
}) or return $dbh->DBI::set_err( $sponge->err(), $sponge->errstr() ); |
| 48 |
|
| 49 |
return $sth; |
| 50 |
} |
| 51 |
|
| 52 |
sub prepare { |
| 53 |
my ( $dbh, $statement ) = @_; |
| 54 |
|
| 55 |
unless ( $dbh->{mock_can_connect} ) { |
| 56 |
$dbh->set_err( 1, "No connection present" ); |
| 57 |
return; |
| 58 |
} |
| 59 |
unless ( $dbh->{mock_can_prepare} ) { |
| 60 |
$dbh->set_err( 1, "Cannot prepare" ); |
| 61 |
return; |
| 62 |
} |
| 63 |
$dbh->{mock_can_prepare}++ if $dbh->{mock_can_prepare} < 0; |
| 64 |
|
| 65 |
eval { |
| 66 |
foreach my $parser ( @{ $dbh->{mock_parser} } ) |
| 67 |
{ |
| 68 |
if ( ref($parser) eq 'CODE' ) { |
| 69 |
$parser->($statement); |
| 70 |
} |
| 71 |
else { |
| 72 |
$parser->parse($statement); |
| 73 |
} |
| 74 |
} |
| 75 |
}; |
| 76 |
if ($@) { |
| 77 |
my $parser_error = $@; |
| 78 |
chomp $parser_error; |
| 79 |
$dbh->set_err( 1, |
| 80 |
"Failed to parse statement. Error: ${parser_error}. Statement: ${statement}" |
| 81 |
); |
| 82 |
return; |
| 83 |
} |
| 84 |
|
| 85 |
my $sth = DBI::_new_sth( $dbh, { Statement => $statement } ); |
| 86 |
$sth->trace_msg( "Preparing statement '${statement}'\n", 1 ); |
| 87 |
my %track_params = ( statement => $statement ); |
| 88 |
|
| 89 |
if ( my $session = $dbh->{mock_session} ) { |
| 90 |
eval { |
| 91 |
my $rs = $session->results_for($statement); |
| 92 |
if ( ref($rs) eq 'ARRAY' && scalar( @{$rs} ) > 0 ) { |
| 93 |
my $fields = @{$rs}[0]; |
| 94 |
$track_params{return_data} = $rs; |
| 95 |
$track_params{fields} = $fields; |
| 96 |
$sth->STORE( NAME => $fields ); |
| 97 |
$sth->STORE( NUM_OF_FIELDS => scalar @{$fields} ); |
| 98 |
} |
| 99 |
else { |
| 100 |
$sth->trace_msg( "No return data set in DBH\n", 1 ); |
| 101 |
} |
| 102 |
}; |
| 103 |
|
| 104 |
if ($@) { |
| 105 |
$dbh->DBI::set_err( 1, "Session Error: $@. Statement: $statement" ); |
| 106 |
} |
| 107 |
|
| 108 |
} |
| 109 |
|
| 110 |
else { |
| 111 |
# If we have available resultsets seed the tracker with one |
| 112 |
|
| 113 |
my ($rs, $callback, $failure, $prepare_attributes, $execute_attributes); |
| 114 |
|
| 115 |
if ( my $all_rs = $dbh->{mock_rs} ) { |
| 116 |
if ( my $by_name = defined $all_rs->{named}{$statement} ? $all_rs->{named}{$statement} : first { $statement =~ m/$_->{regexp}/ } @{ $all_rs->{matching} } ) { |
| 117 |
# We want to copy this, because it is meant to be reusable |
| 118 |
$rs = [ @{ $by_name->{results} } ]; |
| 119 |
$callback = $by_name->{callback}; |
| 120 |
$failure = $by_name->{failure}; |
| 121 |
$prepare_attributes = $by_name->{prepare_attributes}; |
| 122 |
$execute_attributes = $by_name->{execute_attributes}; |
| 123 |
} |
| 124 |
else { |
| 125 |
$rs = shift @{ $all_rs->{ordered} }; |
| 126 |
if (ref($rs) eq 'HASH') { |
| 127 |
$callback = $rs->{callback}; |
| 128 |
$failure = $rs->{failure}; |
| 129 |
$prepare_attributes = $rs->{prepare_attributes}; |
| 130 |
$execute_attributes = $rs->{execute_attributes}; |
| 131 |
$rs = [ @{ $rs->{results} } ]; |
| 132 |
} |
| 133 |
} |
| 134 |
} |
| 135 |
|
| 136 |
if ( ref($rs) eq 'ARRAY' && ( scalar( @{$rs} ) > 0 || $callback ) ) { |
| 137 |
my $fields = shift @{$rs}; |
| 138 |
$track_params{return_data} = $rs; |
| 139 |
$track_params{fields} = $fields; |
| 140 |
$track_params{callback} = $callback; |
| 141 |
$track_params{failure} = $failure; |
| 142 |
$track_params{driver_attributes} = $prepare_attributes; |
| 143 |
$track_params{execute_attributes} = $execute_attributes; |
| 144 |
|
| 145 |
if( $fields ) { |
| 146 |
$sth->STORE( NAME => $fields ); |
| 147 |
$sth->STORE( NUM_OF_FIELDS => scalar @{$fields}); |
| 148 |
} |
| 149 |
|
| 150 |
} |
| 151 |
else { |
| 152 |
$sth->trace_msg( "No return data set in DBH\n", 1 ); |
| 153 |
} |
| 154 |
|
| 155 |
} |
| 156 |
|
| 157 |
# do not allow a statement handle to be created if there is no |
| 158 |
# connection present. |
| 159 |
|
| 160 |
unless ( $dbh->FETCH('Active') ) { |
| 161 |
$dbh->set_err( 1, "No connection present" ); |
| 162 |
return; |
| 163 |
} |
| 164 |
|
| 165 |
# This history object will track everything done to the statement |
| 166 |
my $history = DBD::Mock::StatementTrack->new(%track_params); |
| 167 |
$sth->STORE( mock_my_history => $history ); |
| 168 |
|
| 169 |
# ...now associate the history object with the database handle so |
| 170 |
# people can browse the entire history at once, even for |
| 171 |
# statements opened and closed in a black box |
| 172 |
|
| 173 |
my $all_history = $dbh->FETCH('mock_statement_history'); |
| 174 |
push @{$all_history}, $history; |
| 175 |
|
| 176 |
return $sth; |
| 177 |
} |
| 178 |
|
| 179 |
*prepare_cached = \&prepare; |
| 180 |
|
| 181 |
{ |
| 182 |
my $begin_work_commit; |
| 183 |
|
| 184 |
sub begin_work { |
| 185 |
my $dbh = shift; |
| 186 |
if ( $dbh->FETCH('AutoCommit') ) { |
| 187 |
$dbh->STORE( 'AutoCommit', 0 ); |
| 188 |
$begin_work_commit = 1; |
| 189 |
my $sth = $dbh->prepare('BEGIN WORK') |
| 190 |
or return $dbh->set_err( 1, $DBI::errstr ); |
| 191 |
my $rc = $sth->execute() |
| 192 |
or return $dbh->set_err( 1, $DBI::errstr ); |
| 193 |
$sth->finish(); |
| 194 |
return $rc; |
| 195 |
} |
| 196 |
else { |
| 197 |
return $dbh->set_err( 1, |
| 198 |
'AutoCommit is off, you are already within a transaction' ); |
| 199 |
} |
| 200 |
} |
| 201 |
|
| 202 |
sub commit { |
| 203 |
my $dbh = shift; |
| 204 |
if ( $dbh->FETCH('AutoCommit') && $dbh->FETCH('Warn') ) { |
| 205 |
return $dbh->set_err( 1, "commit ineffective with AutoCommit" ); |
| 206 |
} |
| 207 |
|
| 208 |
my $sth = $dbh->prepare('COMMIT') |
| 209 |
or return $dbh->set_err( 1, $DBI::errstr ); |
| 210 |
my $rc = $sth->execute() |
| 211 |
or return $dbh->set_err( 1, $DBI::errstr ); |
| 212 |
$sth->finish(); |
| 213 |
|
| 214 |
if ($begin_work_commit) { |
| 215 |
$dbh->STORE( 'AutoCommit', 1 ); |
| 216 |
$begin_work_commit = 0; |
| 217 |
} |
| 218 |
|
| 219 |
return $rc; |
| 220 |
} |
| 221 |
|
| 222 |
sub rollback { |
| 223 |
my $dbh = shift; |
| 224 |
if ( $dbh->FETCH('AutoCommit') && $dbh->FETCH('Warn') ) { |
| 225 |
return $dbh->set_err( 1, "rollback ineffective with AutoCommit" ); |
| 226 |
} |
| 227 |
|
| 228 |
my $sth = $dbh->prepare('ROLLBACK') |
| 229 |
or return $dbh->set_err( 1, $DBI::errstr ); |
| 230 |
my $rc = $sth->execute() |
| 231 |
or return $dbh->set_err( 1, $DBI::errstr ); |
| 232 |
$sth->finish(); |
| 233 |
|
| 234 |
if ($begin_work_commit) { |
| 235 |
$dbh->STORE( 'AutoCommit', 1 ); |
| 236 |
$begin_work_commit = 0; |
| 237 |
} |
| 238 |
|
| 239 |
return $rc; |
| 240 |
} |
| 241 |
} |
| 242 |
|
| 243 |
# NOTE: |
| 244 |
# this method should work in most cases, however it does |
| 245 |
# not exactly follow the DBI spec in the case of error |
| 246 |
# handling. I am not sure if that level of detail is |
| 247 |
# really nessecary since it is a weird error conditon |
| 248 |
# which causes it to fail anyway. However if you find you do need it, |
| 249 |
# then please email me about it. I think it would be possible |
| 250 |
# to mimic it by accessing the DBD::Mock::StatementTrack |
| 251 |
# object directly. |
| 252 |
sub selectcol_arrayref { |
| 253 |
my ( $dbh, $query, $attrib, @bindvalues ) = @_; |
| 254 |
|
| 255 |
# get all the columns ... |
| 256 |
my $a_ref = $dbh->selectall_arrayref( $query, $attrib, @bindvalues ); |
| 257 |
|
| 258 |
# if we get nothing back, or dont get an |
| 259 |
# ARRAY ref back, then we can assume |
| 260 |
# something went wrong, and so return undef. |
| 261 |
return undef unless defined $a_ref || ref($a_ref) ne 'ARRAY'; |
| 262 |
|
| 263 |
my @cols = 0; |
| 264 |
if ( ref $attrib->{Columns} eq 'ARRAY' ) { |
| 265 |
@cols = map { $_ - 1 } @{ $attrib->{Columns} }; |
| 266 |
} |
| 267 |
|
| 268 |
# if we do get something then we |
| 269 |
# grab all the columns out of it. |
| 270 |
return [ map { @$_[@cols] } @{$a_ref} ]; |
| 271 |
} |
| 272 |
|
| 273 |
sub FETCH { |
| 274 |
my ( $dbh, $attrib, $value ) = @_; |
| 275 |
$dbh->trace_msg("Fetching DB attrib '$attrib'\n"); |
| 276 |
|
| 277 |
if ( $attrib eq 'Active' ) { |
| 278 |
return $dbh->{mock_can_connect}; |
| 279 |
} |
| 280 |
elsif ( $attrib eq 'mock_all_history' ) { |
| 281 |
return $dbh->{mock_statement_history}; |
| 282 |
} |
| 283 |
elsif ( $attrib eq 'mock_all_history_iterator' ) { |
| 284 |
return DBD::Mock::StatementTrack::Iterator->new( |
| 285 |
$dbh->{mock_statement_history} ); |
| 286 |
} |
| 287 |
elsif ( $attrib =~ /^mock/ ) { |
| 288 |
return $dbh->{$attrib}; |
| 289 |
} |
| 290 |
elsif ( $attrib =~ /^(private_|dbi_|dbd_|[A-Z])/ ) { |
| 291 |
$dbh->trace_msg( |
| 292 |
"... fetching non-driver attribute ($attrib) that DBI handles\n"); |
| 293 |
return $dbh->SUPER::FETCH($attrib); |
| 294 |
} |
| 295 |
else { |
| 296 |
if ( $dbh->{mock_attribute_aliases} ) { |
| 297 |
if ( exists ${ $dbh->{mock_attribute_aliases}->{db} }{$attrib} ) { |
| 298 |
my $mock_attrib = |
| 299 |
$dbh->{mock_attribute_aliases}->{db}->{$attrib}; |
| 300 |
if ( ref($mock_attrib) eq 'CODE' ) { |
| 301 |
return $mock_attrib->($dbh); |
| 302 |
} |
| 303 |
else { |
| 304 |
return $dbh->FETCH($mock_attrib); |
| 305 |
} |
| 306 |
} |
| 307 |
} |
| 308 |
$dbh->trace_msg( |
| 309 |
"... fetching non-driver attribute ($attrib) that DBI doesn't handle\n" |
| 310 |
); |
| 311 |
return $dbh->{$attrib}; |
| 312 |
} |
| 313 |
} |
| 314 |
|
| 315 |
sub STORE { |
| 316 |
my ( $dbh, $attrib, $value ) = @_; |
| 317 |
|
| 318 |
my $printed_value = $value || 'undef'; |
| 319 |
$dbh->trace_msg("Storing DB attribute '$attrib' with '$printed_value'\n"); |
| 320 |
|
| 321 |
if ( $attrib eq 'AutoCommit' ) { |
| 322 |
|
| 323 |
# These are magic DBI values that say we can handle AutoCommit |
| 324 |
# internally as well |
| 325 |
$value = ($value) ? -901 : -900; |
| 326 |
} |
| 327 |
|
| 328 |
if ( $attrib eq 'mock_clear_history' ) { |
| 329 |
if ($value) { |
| 330 |
$dbh->{mock_statement_history} = []; |
| 331 |
} |
| 332 |
return []; |
| 333 |
} |
| 334 |
elsif ( $attrib eq 'mock_add_parser' ) { |
| 335 |
my $parser_type = ref($value); |
| 336 |
my $is_valid_parser; |
| 337 |
|
| 338 |
if ( $parser_type eq 'CODE' ) { |
| 339 |
$is_valid_parser++; |
| 340 |
} |
| 341 |
elsif ( $parser_type && $parser_type !~ /^(ARRAY|HASH|SCALAR)$/ ) { |
| 342 |
$is_valid_parser = eval { $parser_type->can('parse') }; |
| 343 |
} |
| 344 |
|
| 345 |
unless ($is_valid_parser) { |
| 346 |
my $error = |
| 347 |
"Parser must be a code reference or object with 'parse()' " |
| 348 |
. "method (Given type: '$parser_type')"; |
| 349 |
$dbh->set_err( 1, $error ); |
| 350 |
return; |
| 351 |
} |
| 352 |
push @{ $dbh->{mock_parser} }, $value; |
| 353 |
return $value; |
| 354 |
} |
| 355 |
elsif ( $attrib eq 'mock_add_resultset' ) { |
| 356 |
my @copied_values; |
| 357 |
|
| 358 |
$dbh->{mock_rs} ||= { |
| 359 |
named => {}, |
| 360 |
ordered => [], |
| 361 |
matching => [], |
| 362 |
}; |
| 363 |
|
| 364 |
if ( ref $value eq 'ARRAY' ) { |
| 365 |
@copied_values = @{$value}; |
| 366 |
push @{ $dbh->{mock_rs}{ordered} }, \@copied_values; |
| 367 |
} |
| 368 |
elsif ( ref $value eq 'HASH' ) { |
| 369 |
my $name = $value->{sql}; |
| 370 |
|
| 371 |
@copied_values = @{ $value->{results} ? $value->{results} : [] }; |
| 372 |
|
| 373 |
if (not defined $name) { |
| 374 |
push @{ $dbh->{mock_rs}{ordered} }, { |
| 375 |
results => \@copied_values, |
| 376 |
callback => $value->{callback}, |
| 377 |
failure => ref($value->{failure}) ? [ @{ $value->{failure} } ] : undef, |
| 378 |
prepare_attributes => $value->{prepare_attributes}, |
| 379 |
execute_attributes => $value->{execute_attributes}, |
| 380 |
}; |
| 381 |
} |
| 382 |
elsif ( ref $name eq "Regexp" ) { |
| 383 |
my $matching = { |
| 384 |
regexp => $name, |
| 385 |
results => \@copied_values, |
| 386 |
callback => $value->{callback}, |
| 387 |
failure => ref($value->{failure}) ? [ @{ $value->{failure} } ] : undef, |
| 388 |
prepare_attributes => $value->{prepare_attributes}, |
| 389 |
execute_attributes => $value->{execute_attributes}, |
| 390 |
}; |
| 391 |
# either replace existing match or push |
| 392 |
grep { $_->{regexp} eq $name && ($_ = $matching) } @{ $dbh->{mock_rs}{matching} } |
| 393 |
or push @{ $dbh->{mock_rs}{matching} }, $matching; |
| 394 |
} |
| 395 |
else { |
| 396 |
$dbh->{mock_rs}{named}{$name} = { |
| 397 |
results => \@copied_values, |
| 398 |
callback => $value->{callback}, |
| 399 |
failure => ref($value->{failure}) ? [ @{ $value->{failure} } ] : undef, |
| 400 |
prepare_attributes => $value->{prepare_attributes}, |
| 401 |
execute_attributes => $value->{execute_attributes}, |
| 402 |
}; |
| 403 |
} |
| 404 |
} |
| 405 |
else { |
| 406 |
die "Must provide an arrayref or hashref when adding ", |
| 407 |
"resultset via 'mock_add_resultset'.\n"; |
| 408 |
} |
| 409 |
|
| 410 |
return \@copied_values; |
| 411 |
} |
| 412 |
elsif ( $attrib eq 'mock_start_insert_id' ) { |
| 413 |
if ( ref $value eq 'ARRAY' ) { |
| 414 |
$dbh->{mock_last_insert_ids} = {} |
| 415 |
unless $dbh->{mock_last_insert_ids}; |
| 416 |
$dbh->{mock_last_insert_ids}{ $value->[0] } = $value->[1]; |
| 417 |
} |
| 418 |
else { |
| 419 |
|
| 420 |
# we start at one minus the start id |
| 421 |
# so that the increment works |
| 422 |
$dbh->{mock_last_insert_id} = $value - 1; |
| 423 |
} |
| 424 |
|
| 425 |
} |
| 426 |
elsif ( $attrib eq 'mock_session' ) { |
| 427 |
( ref($value) && UNIVERSAL::isa( $value, 'DBD::Mock::Session' ) ) |
| 428 |
|| die |
| 429 |
"Only DBD::Mock::Session objects can be placed into the 'mock_session' slot\n" |
| 430 |
if defined $value; |
| 431 |
$dbh->{mock_session} = $value; |
| 432 |
} |
| 433 |
elsif ( $attrib =~ /^mock_(add_)?data_sources/ ) { |
| 434 |
$dbh->{Driver}->STORE( $attrib, $value ); |
| 435 |
} |
| 436 |
elsif ( $attrib =~ /^mock_add_table_info$/ ) { |
| 437 |
$dbh->{mock_table_info} ||= {}; |
| 438 |
|
| 439 |
if ( ref $value ne "HASH" ) { |
| 440 |
die "mock_add_table_info needs a hash reference" |
| 441 |
} |
| 442 |
|
| 443 |
my ( $cataloge, $schema, $table, $type ) = map { defined $_ ? $_ : '' } @$value{qw( cataloge schema table type )}; |
| 444 |
|
| 445 |
$dbh->{mock_table_info}->{ $cataloge }->{ $schema }->{ $table }->{ $type } = $value->{table_info}; |
| 446 |
} |
| 447 |
elsif ( $attrib =~ /^mock_clear_table_info$/ ) { |
| 448 |
if ( $value ) { |
| 449 |
$dbh->{mock_table_info} = {}; |
| 450 |
} |
| 451 |
|
| 452 |
return {}; |
| 453 |
} |
| 454 |
elsif ( $attrib =~ /^mock/ ) { |
| 455 |
return $dbh->{$attrib} = $value; |
| 456 |
} |
| 457 |
elsif ( $attrib =~ /^(private_|dbi_|dbd_|[A-Z])/ ) { |
| 458 |
$dbh->trace_msg( |
| 459 |
"... storing non-driver attribute ($attrib) with value ($printed_value) that DBI handles\n" |
| 460 |
); |
| 461 |
return $dbh->SUPER::STORE( $attrib, $value ); |
| 462 |
} |
| 463 |
else { |
| 464 |
$dbh->trace_msg( |
| 465 |
"... storing non-driver attribute ($attrib) with value ($printed_value) that DBI won't handle\n" |
| 466 |
); |
| 467 |
return $dbh->{$attrib} = $value; |
| 468 |
} |
| 469 |
} |
| 470 |
|
| 471 |
sub DESTROY { |
| 472 |
my ($dbh) = @_; |
| 473 |
if ( my $session = $dbh->{mock_session} ) { |
| 474 |
if ( $session->has_states_left ) { |
| 475 |
die "DBH->finish called when session still has states left\n"; |
| 476 |
} |
| 477 |
} |
| 478 |
} |
| 479 |
|
| 480 |
sub disconnect { |
| 481 |
my ($dbh) = @_; |
| 482 |
if ( my $session = $dbh->{mock_session} ) { |
| 483 |
if ( $session->has_states_left ) { |
| 484 |
die "DBH->finish called when session still has states left\n"; |
| 485 |
} |
| 486 |
} |
| 487 |
} |
| 488 |
|
| 489 |
1; |