File Coverage

blib/lib/Exception/Class/DBI.pm
Criterion Covered Total %
statement 67 68 98.5
branch 11 16 68.7
condition 2 6 33.3
subroutine 35 35 100.0
pod 1 1 100.0
total 116 126 92.0


line stmt bran cond sub pod time code
1             package Exception::Class::DBI;
2              
3 5     5   395255 use 5.00500;
  5         22  
  5         853  
4 5     5   38 use strict;
  5         16  
  5         389  
5 5     5   12306 use Exception::Class;
  5         234064  
  5         37  
6 5     5   261 use vars qw($VERSION);
  5         11  
  5         1241  
7             $VERSION = '1.01';
8              
9             use Exception::Class (
10 5         103 'Exception::Class::DBI' => {
11             description => 'DBI exception',
12             fields => [qw(err errstr state retval handle)]
13             },
14              
15             'Exception::Class::DBI::Unknown' => {
16             isa => 'Exception::Class::DBI',
17             description => 'DBI unknown exception'
18             },
19              
20             'Exception::Class::DBI::H' => {
21             isa => 'Exception::Class::DBI',
22             description => 'DBI handle exception',
23             },
24              
25             'Exception::Class::DBI::DRH' => {
26             isa => 'Exception::Class::DBI::H',
27             description => 'DBI driver handle exception',
28             },
29              
30             'Exception::Class::DBI::DBH' => {
31             isa => 'Exception::Class::DBI::H',
32             description => 'DBI database handle exception',
33             },
34              
35             'Exception::Class::DBI::STH' => {
36             isa => 'Exception::Class::DBI::H',
37             description => 'DBI statment handle exception',
38             }
39 5     5   30 );
  5         11  
40              
41             my %handlers;
42             sub handler {
43 11     11 1 134 my $pkg = shift;
44 11 100       88 return $handlers{$pkg} if $handlers{$pkg};
45              
46             # Support subclasses.
47             my %class_for = map {
48 6         23 $_ => do {
  30         36  
49 30         71 my $class = "$pkg\::$_";
50 30         56 my $base = __PACKAGE__ . "::$_";
51 5     5   39067 no strict 'refs';
  5         14  
  5         33045  
52             # Try to load the subclass and check its inheritance.
53 30 50       34 eval "require $class" unless @{"$class\::ISA"};
  30         168  
54 30         40 my $isa = \@{"$class\::ISA"};
  30         81  
55 30 50 33     465 die "$class is not a subclass of $base"
56             if $isa && !$class->isa($base);
57             # If subclass exists and inherits, use it. Otherwise use default.
58 30 50       138 $isa ? $class : $base;
59             }
60             } qw(H DRH DBH STH Unknown);
61              
62             return $handlers{$pkg} = sub {
63 4     4   50835 my ($err, $dbh, $retval) = @_;
64              
65             # No handle, no choice.
66 4 50 33     35 $pkg->throw(
67             error => $err,
68             retval => $retval
69             ) unless ref($dbh ||= $DBI::lasth);
70              
71             # Assemble arguments for a handle exception.
72 4         125 my @params = (
73             error => $err,
74             errstr => $dbh->errstr,
75             err => $dbh->err,
76             state => $dbh->state,
77             retval => $retval,
78             handle => $dbh,
79             );
80              
81             # Throw the proper exception.
82 4 100       180 $class_for{STH}->throw(@params) if eval { $dbh->isa('DBI::st') };
  4         86  
83 2 100       4 $class_for{DBH}->throw(@params) if eval { $dbh->isa('DBI::db') };
  2         75  
84 1 50       3 $class_for{DRH}->throw(@params) if eval { $dbh->isa('DBI::dr') };
  1         19  
85              
86             # Unknown exception. This shouldn't happen.
87 0         0 $class_for{Unknown}->throw(@params);
88 6         168 };
89             }
90              
91             package Exception::Class::DBI::H;
92 3     3   50871 sub warn { shift->handle->{Warn} }
93 3     3   13201 sub active { shift->handle->{Active} }
94 3     3   8178 sub kids { shift->handle->{Kids} }
95 3     3   3538 sub active_kids { shift->handle->{ActiveKids} }
96 1     1   573 sub compat_mode { shift->handle->{CompatMode} }
97 3     3   2827 sub inactive_destroy { shift->handle->{InactiveDestroy} }
98 3     3   1499 sub trace_level { shift->handle->{TraceLevel} }
99 3     3   3554 sub fetch_hash_key_name { shift->handle->{FetchHashKeyName} }
100 3     3   4049 sub chop_blanks { shift->handle->{ChopBlanks} }
101 3     3   7954 sub long_read_len { shift->handle->{LongReadLen} }
102 3     3   5936 sub long_trunc_ok { shift->handle->{LongTruncOk} }
103 3     3   8109 sub taint { shift->handle->{Taint} }
104              
105             package Exception::Class::DBI::DBH;
106 1     1   493 sub auto_commit { shift->handle->{AutoCommit} }
107 1     1   344 sub db_name { shift->handle->{Name} }
108 1     1   459 sub statement { shift->handle->{Statement} }
109 1     1   534 sub row_cache_size { shift->handle->{RowCacheSize} }
110              
111             package Exception::Class::DBI::STH;
112 1     1   490 sub num_of_fields { shift->handle->{NUM_OF_FIELDS} }
113 1     1   598 sub num_of_params { shift->handle->{NUM_OF_PARAMS} }
114 1     1   596 sub field_names { shift->handle->{NAME} }
115 1     1   606 sub type { shift->handle->{TYPE} }
116 1     1   420 sub precision { shift->handle->{PRECISION} }
117 1     1   445 sub scale { shift->handle->{SCALE} }
118 1     1   503 sub nullable { shift->handle->{NULLABLE} }
119 1     1   612 sub cursor_name { shift->handle->{CursorName} }
120 1     1   516 sub param_values { shift->handle->{ParamValues} }
121 1     1   393 sub statement { shift->handle->{Statement} }
122 1     1   457 sub rows_in_cache { shift->handle->{RowsInCache} }
123              
124             1;
125             __END__