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 39     39   2400301 use 5.008001;
  39         446  
12              
13 39     39   213 use strict;
  39         73  
  39         888  
14 39     39   267 use warnings;
  39         82  
  39         1268  
15              
16 39     39   47925 use DBI;
  39         542703  
  39         2296  
17              
18 39     39   18014 use DBD::Mock::dr;
  39         103  
  39         1216  
19 39     39   17392 use DBD::Mock::db;
  39         107  
  39         1561  
20 39     39   17877 use DBD::Mock::st;
  39         103  
  39         1403  
21 39     39   16733 use DBD::Mock::StatementTrack;
  39         131  
  39         1273  
22 39     39   16190 use DBD::Mock::StatementTrack::Iterator;
  39         113  
  39         1176  
23 39     39   16671 use DBD::Mock::Session;
  39         98  
  39         1334  
24 39     39   16097 use DBD::Mock::Pool;
  39         99  
  39         1119  
25 39     39   15955 use DBD::Mock::Pool::db;
  39         108  
  39         17639  
26              
27             sub import {
28 33     33   320 shift;
29 33 100 66     4314 $DBI::connect_via = "DBD::Mock::Pool::connect"
30             if ( @_ && lc( $_[0] ) eq "pool" );
31             }
32              
33             our $VERSION = '1.57';
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 37 50   37 0 13723 return $drh if defined $drh;
45 37         137 my ( $class, $attributes ) = @_;
46 37 50 33     320 $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 37         577 %{$attributes}
  37         347  
63             }
64             );
65 37         2938 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   11 my ($dbname) = @_;
110 7 100       45 ( exists $AttributeAliases{ lc($dbname) } )
111             || die "Attribute aliases not available for '$dbname'";
112 6         19 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 39     39   335 use constant NULL_RESULTSET => [ [] ];
  39         89  
  39         5346  
123              
124             1;
125              
126             __END__