Debian Codegraph @

lib/DBD/Mock/dr.pm

raw
1 package DBD::Mock::dr;
2
3 use strict;
4 use warnings;
5
6 use List::Util qw(reduce);
7
8 our $imp_data_size = 0;
9
10 my @connect_callbacks;
11
12
13 sub connect {
14 my ( $drh, $dbname, $user, $auth, $attributes ) = @_;
15 if ( $drh->{'mock_connect_fail'} == 1 ) {
16 $drh->set_err( 1, "Could not connect to mock database" );
17 return;
18 }
19 $attributes ||= {};
20
21 my %driverParameters = _parse_driver_dsn( $dbname );
22
23 if ( $dbname && $DBD::Mock::AttributeAliasing ) {
24
25 # this is the DB we are mocking
26 $attributes->{mock_attribute_aliases} =
27 DBD::Mock::_get_mock_attribute_aliases($driverParameters{database});
28 $attributes->{mock_database_name} = $driverParameters{database};
29 }
30
31 if (($driverParameters{database} || '') eq 'PostgreSQL') {
32 $attributes->{mock_bound_array_support} = 1;
33 }
34 # holds statement parsing coderefs/objects
35 $attributes->{mock_parser} = [];
36
37 # holds all statements applied to handle until manually cleared
38 $attributes->{mock_statement_history} = [];
39
40 # ability to fake a failed DB connection
41 $attributes->{mock_can_connect} = 1;
42
43 # ability to make other things fail :)
44 $attributes->{mock_can_prepare} = 1;
45 $attributes->{mock_can_execute} = 1;
46 $attributes->{mock_can_fetch} = 1;
47
48 my $dbh = DBI::_new_dbh( $drh, { Name => $dbname } )
49 || return;
50
51 foreach my $callback (@connect_callbacks) {
52 $callback->( $dbh, $dbname, $user, $auth, $attributes );
53 }
54
55 return $dbh;
56 }
57
58 sub FETCH {
59 my ( $drh, $attr ) = @_;
60 if ( $attr =~ /^mock_/ ) {
61 if ( $attr eq 'mock_connect_fail' ) {
62 return $drh->{'mock_connect_fail'};
63 }
64 elsif ( $attr eq 'mock_data_sources' ) {
65 unless ( defined $drh->{'mock_data_sources'} ) {
66 $drh->{'mock_data_sources'} = ['DBI:Mock:'];
67 }
68 return $drh->{'mock_data_sources'};
69 }
70 else {
71 return $drh->SUPER::FETCH($attr);
72 }
73 }
74 else {
75 return $drh->SUPER::FETCH($attr);
76 }
77 }
78
79 sub STORE {
80 my ( $drh, $attr, $value ) = @_;
81 if ( $attr =~ /^mock_/ ) {
82 if ( $attr eq 'mock_connect_fail' ) {
83 return $drh->{'mock_connect_fail'} = $value ? 1 : 0;
84 }
85 elsif ( $attr eq 'mock_data_sources' ) {
86 if ( ref($value) ne 'ARRAY' ) {
87 $drh->set_err( 1,
88 "You must pass an array ref of data sources" );
89 return;
90 }
91 return $drh->{'mock_data_sources'} = $value;
92 }
93 elsif ( $attr eq 'mock_add_data_sources' ) {
94 return push @{ $drh->{'mock_data_sources'} } => $value;
95 }
96 }
97 else {
98 return $drh->SUPER::STORE( $attr, $value );
99 }
100 }
101
102 sub data_sources {
103 my $drh = shift;
104 return
105 map { (/^DBI\:Mock\:/i) ? $_ : "DBI:Mock:$_" }
106 @{ $drh->FETCH('mock_data_sources') };
107 }
108
109 # Necessary to support DBI < 1.34
110 # from CPAN RT bug #7057
111
112 sub disconnect_all {
113
114 # no-op
115 }
116
117 sub DESTROY { undef }
118
119 sub set_connect_callbacks {
120 @connect_callbacks = map { die "connect callbacks needs to be a reference to a function " unless ref $_ eq "CODE"; $_ } @_;
121 }
122
123 sub add_connect_callbacks {
124 push @connect_callbacks, map { die "connect callbacks needs to be a reference to a function " unless ref $_ eq "CODE"; $_ } @_;
125 }
126
127 sub _parse_driver_dsn {
128 my ( $driverDsn ) = @_;
129
130 $driverDsn = $driverDsn ? $driverDsn : '';
131
132 my %driverParameters;
133
134 foreach my $parameter ( split /;/, $driverDsn ) {
135 if ( my ( $key, $value ) = $parameter =~ m/^(.*?)=(.*)$/ ) {
136 $driverParameters{ $key } = $value;
137 }
138 }
139
140 $driverParameters{database} = $driverDsn unless %driverParameters;
141
142 return %driverParameters;
143 }
144
145 1;