File Coverage

blib/lib/Exception/Class/DBI.pm
Criterion Covered Total %
statement 66 67 98.5
branch 11 16 68.7
condition 2 6 33.3
subroutine 35 35 100.0
pod 28 28 100.0
total 142 152 93.4


line stmt bran cond sub pod time code
1             package Exception::Class::DBI;
2              
3 5     5   275714 use 5.006;
  5         44  
4 5     5   22 use strict;
  5         7  
  5         136  
5 5     5   27 use warnings;
  5         15  
  5         131  
6 5     5   2371 use Exception::Class;
  5         43627  
  5         27  
7              
8             our $VERSION = '1.03_01';
9             $VERSION =~ tr/_//d;
10              
11             use Exception::Class (
12 5         41 'Exception::Class::DBI' => {
13             description => 'DBI exception',
14             fields => [qw(err errstr state retval handle)]
15             },
16              
17             'Exception::Class::DBI::Unknown' => {
18             isa => 'Exception::Class::DBI',
19             description => 'DBI unknown exception'
20             },
21              
22             'Exception::Class::DBI::H' => {
23             isa => 'Exception::Class::DBI',
24             description => 'DBI handle exception',
25             },
26              
27             'Exception::Class::DBI::DRH' => {
28             isa => 'Exception::Class::DBI::H',
29             description => 'DBI driver handle exception',
30             },
31              
32             'Exception::Class::DBI::DBH' => {
33             isa => 'Exception::Class::DBI::H',
34             description => 'DBI database handle exception',
35             },
36              
37             'Exception::Class::DBI::STH' => {
38             isa => 'Exception::Class::DBI::H',
39             description => 'DBI statment handle exception',
40             }
41 5     5   432 );
  5         8  
42              
43             my %handlers;
44             sub handler {
45 11     11 1 406 my $pkg = shift;
46 11 100       61 return $handlers{$pkg} if $handlers{$pkg};
47              
48             # Support subclasses.
49             my %class_for = map {
50 6         18 $_ => do {
  30         37  
51 30         60 my $class = "$pkg\::$_";
52 30         51 my $base = __PACKAGE__ . "::$_";
53 5     5   7482 no strict 'refs';
  5         10  
  5         3729  
54             # Try to load the subclass and check its inheritance.
55 30 50       77 eval "require $class" unless @{"$class\::ISA"};
  30         126  
56 30         39 my $isa = \@{"$class\::ISA"};
  30         62  
57 30 50 33     246 die "$class is not a subclass of $base"
58             if $isa && !$class->isa($base);
59             # If subclass exists and inherits, use it. Otherwise use default.
60 30 50       100 $isa ? $class : $base;
61             }
62             } qw(H DRH DBH STH Unknown);
63              
64             return $handlers{$pkg} = sub {
65 4     4   16919 my ($err, $dbh, $retval) = @_;
66              
67             # No handle, no choice.
68 4 50 33     22 $pkg->throw(
69             error => $err,
70             retval => $retval
71             ) unless ref($dbh ||= $DBI::lasth);
72              
73             # Assemble arguments for a handle exception.
74 4         107 my @params = (
75             error => $err,
76             errstr => $dbh->errstr,
77             err => $dbh->err,
78             state => $dbh->state,
79             retval => $retval,
80             handle => $dbh,
81             );
82              
83             # Throw the proper exception.
84 4 100       161 $class_for{STH}->throw(@params) if eval { $dbh->isa('DBI::st') };
  4         71  
85 2 100       5 $class_for{DBH}->throw(@params) if eval { $dbh->isa('DBI::db') };
  2         19  
86 1 50       2 $class_for{DRH}->throw(@params) if eval { $dbh->isa('DBI::dr') };
  1         11  
87              
88             # Unknown exception. This shouldn't happen.
89 0         0 $class_for{Unknown}->throw(@params);
90 6         69 };
91             }
92              
93             package Exception::Class::DBI::H;
94 3     3 1 11919 sub warn { shift->handle->{Warn} }
95 3     3 1 1378 sub active { shift->handle->{Active} }
96 3     3 1 1163 sub kids { shift->handle->{Kids} }
97 3     3 1 1352 sub active_kids { shift->handle->{ActiveKids} }
98 1     1 1 482 sub compat_mode { shift->handle->{CompatMode} }
99 3     3 1 1214 sub inactive_destroy { shift->handle->{InactiveDestroy} }
100 3     3 1 786 sub trace_level { shift->handle->{TraceLevel} }
101 3     3 1 1316 sub fetch_hash_key_name { shift->handle->{FetchHashKeyName} }
102 3     3 1 1416 sub chop_blanks { shift->handle->{ChopBlanks} }
103 3     3 1 797 sub long_read_len { shift->handle->{LongReadLen} }
104 3     3 1 1355 sub long_trunc_ok { shift->handle->{LongTruncOk} }
105 3     3 1 776 sub taint { shift->handle->{Taint} }
106              
107             package Exception::Class::DBI::DBH;
108 1     1 1 254 sub auto_commit { shift->handle->{AutoCommit} }
109 1     1 1 252 sub db_name { shift->handle->{Name} }
110 1     1 1 444 sub statement { shift->handle->{Statement} }
111 1     1 1 423 sub row_cache_size { shift->handle->{RowCacheSize} }
112              
113             package Exception::Class::DBI::STH;
114 1     1 1 250 sub num_of_fields { shift->handle->{NUM_OF_FIELDS} }
115 1     1 1 438 sub num_of_params { shift->handle->{NUM_OF_PARAMS} }
116 1     1 1 436 sub field_names { shift->handle->{NAME} }
117 1     1 1 460 sub type { shift->handle->{TYPE} }
118 1     1 1 256 sub precision { shift->handle->{PRECISION} }
119 1     1 1 252 sub scale { shift->handle->{SCALE} }
120 1     1 1 261 sub nullable { shift->handle->{NULLABLE} }
121 1     1 1 486 sub cursor_name { shift->handle->{CursorName} }
122 1     1 1 313 sub param_values { shift->handle->{ParamValues} }
123 1     1 1 253 sub statement { shift->handle->{Statement} }
124 1     1 1 457 sub rows_in_cache { shift->handle->{RowsInCache} }
125              
126             1;
127             __END__