/usr/share/perl5/DBI/Mock.pm is in libdbi-test-perl 0.001-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 | package DBI::Mock;
use strict;
use warnings;
use Carp qw(carp confess);
sub _set_isa
{
my ( $classes, $topclass ) = @_;
foreach my $suffix ( '::db', '::st' )
{
my $previous = $topclass || 'DBI'; # trees are rooted here
foreach my $class (@$classes)
{
my $base_class = $previous . $suffix;
my $sub_class = $class . $suffix;
my $sub_class_isa = "${sub_class}::ISA";
no strict 'refs';
@$sub_class_isa or @$sub_class_isa = ($base_class);
$previous = $class;
}
}
}
sub _make_root_class
{
my ( $ref, $root ) = @_;
$root or return;
(my $c = ref $ref) =~ s/::dr$//g;
no strict 'refs';
eval qq{
package $c;
require $root;
};
$@ and return;
unless ( @{"$root\::db::ISA"} && @{"$root\::st::ISA"} )
{
carp("DBI subclasses '$root\::db' and ::st are not setup, RootClass ignored");
}
else
{
_set_isa( [$root], 'DBI::Mock' );
}
return;
}
my %default_attrs = (
Warn => 1,
Active => 1,
Executed => 0, # set on execute ...
Kids => 0,
ActiveKids => 0,
CachedKids => 0,
Type => "db",
ChildHandles => undef, # XXX improve to fake :/
CompatMode => 0,
InactiveDestroy => 0,
AutoInactiveDestroy => 0,
PrintWarn => $^W,
PrintError => 1,
RaiseError => 0,
HandleError => undef, # XXX no default specified
HandleSetErr => undef, # XXX no default specified
ErrCount => 0,
ShowErrorStatement => undef, # XXX no default specified
TraceLevel => 0, # XXX no default specified
FetchHashKeyName => "NAME", # XXX no default specified
ChopBlanks => undef, # XXX no default specified
LongReadLen => 0,
LongTruncOk => 0,
TaintIn => 0,
TaintOut => 0,
Taint => 0,
Profile => undef, # XXX no default specified
ReadOnly => 1,
Callbacks => undef,
);
sub _make_handle
{
my ( $ref, $name ) = @_;
my $h = bless( { %default_attrs, %$ref }, $name );
return $h;
}
my %drivers;
sub _get_drv
{
my ( $self, $dsn, $attrs ) = @_;
my $class = "DBI::dr"; # XXX maybe extract it from DSN? ...
defined $drivers{$class} or $drivers{$class} = _make_handle( $attrs, $class );
return $drivers{$class};
}
sub connect
{
my ( $self, $dsn, $user, $pass, $attrs ) = @_;
my $drh = $self->_get_drv( $dsn, $attrs );
$drh->connect( $dsn, $user, $pass, $attrs );
}
sub installed_drivers { %drivers; }
sub available_drivers { 'NullP' }
our $stderr = 1;
our $err;
our $errstr;
sub err { $err }
sub errstr { $errstr }
sub set_err
{
my ( $ref, $_err, $_errstr ) = @_;
$_err or do {
$err = undef;
$errstr = '';
return;
};
$err = $_err;
$errstr = $_errstr;
Test::More::diag("Raise: ", $ref->{RaiseError});
$ref->{RaiseError} and $errstr and Carp::croak($errstr);
Test::More::diag("Print: ", $ref->{PrintError});
$ref->{PrintError} and $errstr and Carp::carp($errstr);
return;
}
{
package #
DBI::Mock::dr;
our @ISA;
my %default_db_attrs = (
AutoCommit => 1,
Driver => undef, # set to the driver itself ...
Name => "",
Statement => "",
RowCacheSize => 0,
Username => "",
);
sub connect
{
my ( $drh, $dbname, $user, $auth, $attrs ) = @_;
exists $drh->{RootClass}
and DBI::Mock::_make_root_class( $drh, $drh->{RootClass} );
my $class = $drh->{RootClass} ? $drh->{RootClass} . "::db" : "DBI::db";
my $dbh = DBI::Mock::_make_handle(
{
%default_db_attrs,
%$attrs,
drh => $drh
},
$class
);
return $dbh;
}
our $err;
our $errstr;
sub err { $err }
sub errstr { $errstr }
sub set_err
{
my ( $ref, $_err, $_errstr ) = @_;
$_err or do {
$err = undef;
$errstr = '';
return;
};
$err = $_err;
$errstr = $_errstr;
$ref->{RaiseError} and $errstr and Carp::croak($errstr);
$ref->{PrintError} and $errstr and Carp::carp($errstr);
return;
}
sub FETCH
{
my ( $dbh, $attr ) = @_;
return $dbh->{$attr};
}
sub STORE
{
my ( $dbh, $attr, $val ) = @_;
return $dbh->{$attr} = $val;
}
}
{
package #
DBI::Mock::db;
our @ISA;
my %default_st_attrs = (
NUM_OF_FIELDS => undef,
NUM_OF_PARAMS => undef,
NAME => undef,
NAME_lc => undef,
NAME_uc => undef,
NAME_hash => undef,
NAME_lc_hash => undef,
NAME_uc_hash => undef,
TYPE => undef,
PRECISION => undef,
SCALE => undef,
NULLABLE => undef,
CursorName => undef,
Database => undef,
Statement => undef,
ParamValues => undef,
ParamTypes => undef,
ParamArrays => undef,
RowsInCache => undef,
);
sub _valid_stmt
{
1;
}
sub disconnect
{
$_[0]->STORE( Active => 0 );
return 1;
}
sub prepare
{
my ( $dbh, $stmt, $attrs ) = @_;
_valid_stmt( $stmt, $attrs ) or return; # error already set by _valid_stmt
defined $attrs or $attrs = {};
ref $attrs eq "HASH" or $attrs = {};
my $class = $dbh->{drh}->{RootClass} ? $dbh->{drh}->{RootClass} . "::st" : "DBI::st";
my $sth = DBI::Mock::_make_handle(
{
%default_st_attrs,
%$attrs,
Statement => $stmt,
dbh => $dbh,
},
$class
);
return $sth;
}
# I don't had a clue how to implement that better
# finally - they are reduce to the max and don't interfer with anything around ...
sub do
{
my ( $dbh, $statement, $attr, @params ) = @_;
my $sth = $dbh->prepare( $statement, $attr ) or return undef;
$sth->execute(@params) or return $dbh->set_err( $sth->err, $sth->errstr );
my $rows = $sth->rows;
( $rows == 0 ) ? "0E0" : $rows;
}
sub _do_selectrow
{
my ( $method, $dbh, $stmt, $attr, @bind ) = @_;
my $sth = ( ( ref $stmt ) ? $stmt : $dbh->prepare( $stmt, $attr ) )
or return;
$sth->execute(@bind)
or return;
my $row = $sth->$method()
and $sth->finish;
return $row;
}
sub selectrow_hashref { return _do_selectrow( 'fetchrow_hashref', @_ ); }
sub selectrow_arrayref { return _do_selectrow( 'fetchrow_arrayref', @_ ); }
sub selectrow_array
{
my $row = _do_selectrow( 'fetchrow_arrayref', @_ ) or return;
return $row->[0] unless wantarray;
return @$row;
}
sub selectall_arrayref
{
my ( $dbh, $stmt, $attr, @bind ) = @_;
my $sth = ( ref $stmt ) ? $stmt : $dbh->prepare( $stmt, $attr )
or return;
$sth->execute(@bind) || return;
my $slice = $attr->{Slice}; # typically undef, else hash or array ref
if ( !$slice and $slice = $attr->{Columns} )
{
if ( ref $slice eq 'ARRAY' )
{ # map col idx to perl array idx
$slice = [ @{ $attr->{Columns} } ]; # take a copy
for (@$slice) { $_-- }
}
}
my $rows = $sth->fetchall_arrayref( $slice, my $MaxRows = $attr->{MaxRows} );
$sth->finish if defined $MaxRows;
return $rows;
}
sub selectall_hashref
{
my ( $dbh, $stmt, $key_field, $attr, @bind ) = @_;
my $sth = ( ref $stmt ) ? $stmt : $dbh->prepare( $stmt, $attr );
return unless $sth;
$sth->execute(@bind) || return;
return $sth->fetchall_hashref($key_field);
}
sub selectcol_arrayref
{
my ( $dbh, $stmt, $attr, @bind ) = @_;
my $sth = ( ref $stmt ) ? $stmt : $dbh->prepare( $stmt, $attr );
return unless $sth;
$sth->execute(@bind) || return;
my @columns = ( $attr->{Columns} ) ? @{ $attr->{Columns} } : (1);
my @values = (undef) x @columns;
my $idx = 0;
for (@columns)
{
$sth->bind_col( $_, \$values[ $idx++ ] ) || return;
}
my @col;
if ( my $max = $attr->{MaxRows} )
{
push @col, @values while 0 < $max-- && $sth->fetch;
}
else
{
push @col, @values while $sth->fetch;
}
return \@col;
}
our $err;
our $errstr;
sub err { $err }
sub errstr { $errstr }
sub set_err
{
my ( $ref, $_err, $_errstr ) = @_;
$_err or do {
$err = undef;
$errstr = '';
return;
};
$err = $_err;
$errstr = $_errstr;
defined $errstr or Carp::croak("Undefined \$errstr");
$ref->{RaiseError} and $errstr and Carp::croak($errstr);
Test::More::diag("Print: ", $ref->{PrintError});
$ref->{PrintError} and $errstr and Carp::carp($errstr);
return;
}
sub FETCH
{
my ( $dbh, $attr ) = @_;
return $dbh->{$attr};
}
sub STORE
{
my ( $dbh, $attr, $val ) = @_;
return $dbh->{$attr} = $val;
}
}
{
package #
DBI::Mock::st;
our @ISA;
my %default_attrs = ();
sub execute
{
"0E0";
}
our $err;
our $errstr;
sub err { $err }
sub errstr { $errstr }
sub set_err
{
my ( $ref, $_err, $_errstr ) = @_;
$_err or do {
$err = undef;
$errstr = '';
return;
};
$err = $_err;
$errstr = $_errstr;
defined $errstr or Carp::croak("Undefined \$errstr");
$ref->{RaiseError} and $errstr and Carp::croak($errstr);
Test::More::diag("Print: ", $ref->{PrintError});
$ref->{PrintError} and $errstr and Carp::carp($errstr);
}
sub bind_col
{
my ( $h, $col, $value_ref, $from_bind_columns ) = @_;
my $fbav = $h->{'_fbav'} ||= dbih_setup_fbav($h); # from _get_fbav()
my $num_of_fields = @$fbav;
Carp::croak("bind_col: column $col is not a valid column (1..$num_of_fields)")
if $col < 1
or $col > $num_of_fields;
return 1 if not defined $value_ref; # ie caller is just trying to set TYPE
Carp::croak("bind_col($col,$value_ref) needs a reference to a scalar")
unless ref $value_ref eq 'SCALAR';
$h->{'_bound_cols'}->[ $col - 1 ] = $value_ref;
return 1;
}
sub FETCH
{
my ( $dbh, $attr ) = @_;
return $dbh->{$attr};
}
sub STORE
{
my ( $dbh, $attr, $val ) = @_;
return $dbh->{$attr} = $val;
}
}
sub _inject_mock_dbi
{
eval qq{
package #
DBI;
our \@ISA = qw(DBI::Mock);
our \$VERSION = "1.625";
package #
DBI::dr;
our \@ISA = qw(DBI::Mock::dr);
package #
DBI::db;
our \@ISA = qw(DBI::Mock::db);
package #
DBI::st;
our \@ISA = qw(DBI::Mock::st);
1;
};
$@ and die $@;
$INC{'DBI.pm'} = 'mocked';
}
my $_have_dbi;
sub _miss_dbi
{
defined $_have_dbi and return !$_have_dbi;
$_have_dbi = 0;
eval qq{
\$ENV{DBI_PUREPERL} = 2; # we only want to know if it's there ...
require DBI;
\$_have_dbi = 1;
};
return !($_have_dbi = exists $INC{'DBI.pm'}); # XXX maybe riba can help to unload ...
}
BEGIN
{
if ( $ENV{DBI_MOCK} || _miss_dbi() )
{
_inject_mock_dbi();
}
}
1;
=head1 NAME
DBI::Mock - mock a DBI if we can't find the real one
=head1 SYNOPSIS
use DBI::Mock;
my $dbh = DBI::Mock->connect($data_source, $user, $pass, \%attr) or die $DBI::Mock::errstr;
my $sth = $dbh->prepare();
$sth->execute();
... copy some from DBI SYNOPSIS
=head1 DESCRIPTION
=head1 AUTHOR
This module is a team-effort. The current team members are
H.Merijn Brand (Tux)
Jens Rehsack (Sno)
Peter Rabbitson (ribasushi)
Joakim TE<0x00f8>rmoen (trmjoa)
=head1 COPYRIGHT AND LICENSE
Copyright (C)2013 - The DBI development team
You may distribute this module under the terms of either the GNU
General Public License or the Artistic License, as specified in
the Perl README file.
=cut
|