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
|
package DBD::Mock::dr;
use strict;
use warnings;
use List::Util qw(reduce);
our $imp_data_size = 0;
my @connect_callbacks;
sub connect {
my ( $drh, $dbname, $user, $auth, $attributes ) = @_;
if ( $drh->{'mock_connect_fail'} == 1 ) {
$drh->set_err( 1, "Could not connect to mock database" );
return;
}
$attributes ||= {};
my %driverParameters = _parse_driver_dsn( $dbname );
if ( $dbname && $DBD::Mock::AttributeAliasing ) {
# this is the DB we are mocking
$attributes->{mock_attribute_aliases} =
DBD::Mock::_get_mock_attribute_aliases($driverParameters{database});
$attributes->{mock_database_name} = $driverParameters{database};
}
# holds statement parsing coderefs/objects
$attributes->{mock_parser} = [];
# holds all statements applied to handle until manually cleared
$attributes->{mock_statement_history} = [];
# ability to fake a failed DB connection
$attributes->{mock_can_connect} = 1;
# ability to make other things fail :)
$attributes->{mock_can_prepare} = 1;
$attributes->{mock_can_execute} = 1;
$attributes->{mock_can_fetch} = 1;
my $dbh = DBI::_new_dbh( $drh, { Name => $dbname } )
|| return;
foreach my $callback (@connect_callbacks) {
$callback->( $dbh, $dbname, $user, $auth, $attributes );
}
return $dbh;
}
sub FETCH {
my ( $drh, $attr ) = @_;
if ( $attr =~ /^mock_/ ) {
if ( $attr eq 'mock_connect_fail' ) {
return $drh->{'mock_connect_fail'};
}
elsif ( $attr eq 'mock_data_sources' ) {
unless ( defined $drh->{'mock_data_sources'} ) {
$drh->{'mock_data_sources'} = ['DBI:Mock:'];
}
return $drh->{'mock_data_sources'};
}
else {
return $drh->SUPER::FETCH($attr);
}
}
else {
return $drh->SUPER::FETCH($attr);
}
}
sub STORE {
my ( $drh, $attr, $value ) = @_;
if ( $attr =~ /^mock_/ ) {
if ( $attr eq 'mock_connect_fail' ) {
return $drh->{'mock_connect_fail'} = $value ? 1 : 0;
}
elsif ( $attr eq 'mock_data_sources' ) {
if ( ref($value) ne 'ARRAY' ) {
$drh->set_err( 1,
"You must pass an array ref of data sources" );
return;
}
return $drh->{'mock_data_sources'} = $value;
}
elsif ( $attr eq 'mock_add_data_sources' ) {
return push @{ $drh->{'mock_data_sources'} } => $value;
}
}
else {
return $drh->SUPER::STORE( $attr, $value );
}
}
sub data_sources {
my $drh = shift;
return
map { (/^DBI\:Mock\:/i) ? $_ : "DBI:Mock:$_" }
@{ $drh->FETCH('mock_data_sources') };
}
# Necessary to support DBI < 1.34
# from CPAN RT bug #7057
sub disconnect_all {
# no-op
}
sub DESTROY { undef }
sub set_connect_callbacks {
@connect_callbacks = map { die "connect callbacks needs to be a reference to a function " unless ref $_ eq "CODE"; $_ } @_;
}
sub add_connect_callbacks {
push @connect_callbacks, map { die "connect callbacks needs to be a reference to a function " unless ref $_ eq "CODE"; $_ } @_;
}
sub _parse_driver_dsn {
my ( $driverDsn ) = @_;
$driverDsn = $driverDsn ? $driverDsn : '';
my %driverParameters;
foreach my $parameter ( split /;/, $driverDsn ) {
if ( my ( $key, $value ) = $parameter =~ m/^(.*?)=(.*)$/ ) {
$driverParameters{ $key } = $value;
}
}
$driverParameters{database} = $driverDsn unless %driverParameters;
return %driverParameters;
}
1;
|