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   339616 use 5.006;
  5         54  
4 5     5   27 use strict;
  5         8  
  5         184  
5 5     5   27 use warnings;
  5         19  
  5         166  
6 5     5   2452 use Exception::Class;
  5         54753  
  5         34  
7              
8             our $VERSION = '1.04';
9              
10             use Exception::Class (
11 5         49 'Exception::Class::DBI' => {
12             description => 'DBI exception',
13             fields => [qw(err errstr state retval handle)]
14             },
15              
16             'Exception::Class::DBI::Unknown' => {
17             isa => 'Exception::Class::DBI',
18             description => 'DBI unknown exception'
19             },
20              
21             'Exception::Class::DBI::H' => {
22             isa => 'Exception::Class::DBI',
23             description => 'DBI handle exception',
24             },
25              
26             'Exception::Class::DBI::DRH' => {
27             isa => 'Exception::Class::DBI::H',
28             description => 'DBI driver handle exception',
29             },
30              
31             'Exception::Class::DBI::DBH' => {
32             isa => 'Exception::Class::DBI::H',
33             description => 'DBI database handle exception',
34             },
35              
36             'Exception::Class::DBI::STH' => {
37             isa => 'Exception::Class::DBI::H',
38             description => 'DBI statment handle exception',
39             }
40 5     5   510 );
  5         12  
41              
42             my %handlers;
43             sub handler {
44 11     11 1 509 my $pkg = shift;
45 11 100       65 return $handlers{$pkg} if $handlers{$pkg};
46              
47             # Support subclasses.
48             my %class_for = map {
49 6         23 $_ => do {
  30         48  
50 30         75 my $class = "$pkg\::$_";
51 30         63 my $base = __PACKAGE__ . "::$_";
52 5     5   9274 no strict 'refs';
  5         14  
  5         5022  
53             # Try to load the subclass and check its inheritance.
54 30 50       40 eval "require $class" unless @{"$class\::ISA"};
  30         145  
55 30         40 my $isa = \@{"$class\::ISA"};
  30         76  
56 30 50 33     300 die "$class is not a subclass of $base"
57             if $isa && !$class->isa($base);
58             # If subclass exists and inherits, use it. Otherwise use default.
59 30 50       178 $isa ? $class : $base;
60             }
61             } qw(H DRH DBH STH Unknown);
62              
63             return $handlers{$pkg} = sub {
64 4     4   21073 my ($err, $dbh, $retval) = @_;
65              
66             # No handle, no choice.
67 4 50 33     66 $pkg->throw(
68             error => $err,
69             retval => $retval
70             ) unless ref($dbh ||= $DBI::lasth);
71              
72             # Assemble arguments for a handle exception.
73 4         126 my @params = (
74             error => $err,
75             errstr => $dbh->errstr,
76             err => $dbh->err,
77             state => $dbh->state,
78             retval => $retval,
79             handle => $dbh,
80             );
81              
82             # Throw the proper exception.
83 4 100       238 $class_for{STH}->throw(@params) if eval { $dbh->isa('DBI::st') };
  4         101  
84 2 100       6 $class_for{DBH}->throw(@params) if eval { $dbh->isa('DBI::db') };
  2         35  
85 1 50       4 $class_for{DRH}->throw(@params) if eval { $dbh->isa('DBI::dr') };
  1         16  
86              
87             # Unknown exception. This shouldn't happen.
88 0         0 $class_for{Unknown}->throw(@params);
89 6         82 };
90             }
91              
92             package Exception::Class::DBI::H;
93 3     3 1 15531 sub warn { shift->handle->{Warn} }
94 3     3 1 1751 sub active { shift->handle->{Active} }
95 3     3 1 1461 sub kids { shift->handle->{Kids} }
96 3     3 1 1642 sub active_kids { shift->handle->{ActiveKids} }
97 1     1 1 561 sub compat_mode { shift->handle->{CompatMode} }
98 3     3 1 1403 sub inactive_destroy { shift->handle->{InactiveDestroy} }
99 3     3 1 1054 sub trace_level { shift->handle->{TraceLevel} }
100 3     3 1 1622 sub fetch_hash_key_name { shift->handle->{FetchHashKeyName} }
101 3     3 1 1642 sub chop_blanks { shift->handle->{ChopBlanks} }
102 3     3 1 949 sub long_read_len { shift->handle->{LongReadLen} }
103 3     3 1 1721 sub long_trunc_ok { shift->handle->{LongTruncOk} }
104 3     3 1 968 sub taint { shift->handle->{Taint} }
105              
106             package Exception::Class::DBI::DBH;
107 1     1 1 317 sub auto_commit { shift->handle->{AutoCommit} }
108 1     1 1 314 sub db_name { shift->handle->{Name} }
109 1     1 1 551 sub statement { shift->handle->{Statement} }
110 1     1 1 601 sub row_cache_size { shift->handle->{RowCacheSize} }
111              
112             package Exception::Class::DBI::STH;
113 1     1 1 307 sub num_of_fields { shift->handle->{NUM_OF_FIELDS} }
114 1     1 1 575 sub num_of_params { shift->handle->{NUM_OF_PARAMS} }
115 1     1 1 532 sub field_names { shift->handle->{NAME} }
116 1     1 1 596 sub type { shift->handle->{TYPE} }
117 1     1 1 311 sub precision { shift->handle->{PRECISION} }
118 1     1 1 306 sub scale { shift->handle->{SCALE} }
119 1     1 1 307 sub nullable { shift->handle->{NULLABLE} }
120 1     1 1 554 sub cursor_name { shift->handle->{CursorName} }
121 1     1 1 380 sub param_values { shift->handle->{ParamValues} }
122 1     1 1 308 sub statement { shift->handle->{Statement} }
123 1     1 1 556 sub rows_in_cache { shift->handle->{RowsInCache} }
124              
125             1;
126             __END__