File Coverage

blib/lib/DBD/Mock.pm
Criterion Covered Total %
statement 49 52 94.2
branch 6 8 75.0
condition 3 6 50.0
subroutine 16 18 88.8
pod 0 1 0.0
total 74 85 87.0


line stmt bran cond sub pod time code
1             package DBD::Mock;
2              
3             # --------------------------------------------------------------------------- #
4             # Copyright (c) 2004-2007 Stevan Little, Chris Winters
5             # (spawned from original code Copyright (c) 1994 Tim Bunce)
6             # --------------------------------------------------------------------------- #
7             # You may distribute under the terms of either the GNU General Public
8             # License or the Artistic License, as specified in the Perl README file.
9             # --------------------------------------------------------------------------- #
10              
11 40     40   2396527 use 5.008001;
  40         471  
12              
13 40     40   214 use strict;
  40         71  
  40         963  
14 40     40   228 use warnings;
  40         85  
  40         1281  
15              
16 40     40   46286 use DBI;
  40         540087  
  40         2470  
17              
18 40     40   19631 use DBD::Mock::dr;
  40         107  
  40         1254  
19 40     40   17383 use DBD::Mock::db;
  40         100  
  40         1531  
20 40     40   18040 use DBD::Mock::st;
  40         104  
  40         1533  
21 40     40   16813 use DBD::Mock::StatementTrack;
  40         125  
  40         1372  
22 40     40   16174 use DBD::Mock::StatementTrack::Iterator;
  40         106  
  40         1270  
23 40     40   15824 use DBD::Mock::Session;
  40         105  
  40         1321  
24 40     40   15285 use DBD::Mock::Pool;
  40         95  
  40         1143  
25 40     40   15472 use DBD::Mock::Pool::db;
  40         103  
  40         17580  
26              
27             sub import {
28 34     34   363 shift;
29 34 100 66     4253 $DBI::connect_via = "DBD::Mock::Pool::connect"
30             if ( @_ && lc( $_[0] ) eq "pool" );
31             }
32              
33             our $VERSION = '1.58';
34              
35             our $drh = undef; # will hold driver handle
36             our $err = 0; # will hold any error codes
37             our $errstr = ''; # will hold any error messages
38              
39             # Defaulting a result set's fields to undef changes the way DBD::Mock responds, so we default it to off
40             our $DefaultFieldsToUndef = 0;
41              
42              
43             sub driver {
44 38 50   38 0 13619 return $drh if defined $drh;
45 38         134 my ( $class, $attributes ) = @_;
46 38 50 33     317 $attributes = {}
47             unless ( defined($attributes) && ( ref($attributes) eq 'HASH' ) );
48             $drh = DBI::_new_drh(
49             "${class}::dr",
50             {
51             Name => 'Mock',
52             Version => $DBD::Mock::VERSION,
53             Attribution =>
54             'DBD Mock driver by Chris Winters & Stevan Little (orig. from Tim Bunce)',
55             Err => \$DBD::Mock::err,
56             Errstr => \$DBD::Mock::errstr,
57              
58             # mock attributes
59             mock_connect_fail => 0,
60              
61             # and pass in any extra attributes given
62 38         187 %{$attributes}
  38         375  
63             }
64             );
65 38         2446 return $drh;
66             }
67              
68 0     0   0 sub CLONE { undef $drh }
69              
70             # NOTE:
71             # this feature is still quite experimental. It is defaulted to
72             # be off, but it can be turned on by doing this:
73             # $DBD::Mock::AttributeAliasing++;
74             # and then turned off by doing:
75             # $DBD::Mock::AttributeAliasing = 0;
76             # we shall see how this feature works out.
77              
78             our $AttributeAliasing = 0;
79              
80             my %AttributeAliases = (
81             mysql => {
82             db => {
83              
84             # aliases can either be a string which is obvious
85             mysql_insertid => 'mock_last_insert_id'
86             },
87             st => {
88              
89             # but they can also be a subroutine reference whose
90             # first argument will be either the $dbh or the $sth
91             # depending upon which context it is aliased in.
92             mysql_insertid =>
93             sub { (shift)->{Database}->{'mock_last_insert_id'} }
94             }
95             },
96              
97             mariadb => {
98             db => {
99             mariadb_insertid => 'mock_last_insert_id'
100             },
101             st => {
102             mariadb_insertid =>
103             sub { (shift)->{Database}->{'mock_last_insert_id'} }
104             }
105             }
106             );
107              
108             sub _get_mock_attribute_aliases {
109 7     7   14 my ($dbname) = @_;
110 7 100       71 ( exists $AttributeAliases{ lc($dbname) } )
111             || die "Attribute aliases not available for '$dbname'";
112 6         15 return $AttributeAliases{ lc($dbname) };
113             }
114              
115             sub _set_mock_attribute_aliases {
116 0     0     my ( $dbname, $dbh_or_sth, $key, $value ) = @_;
117 0           return $AttributeAliases{ lc($dbname) }->{$dbh_or_sth}->{$key} = $value;
118             }
119              
120             ## Some useful constants
121              
122 40     40   321 use constant NULL_RESULTSET => [ [] ];
  40         99  
  40         4777  
123              
124             1;
125              
126             __END__