5 | 5 |
use strict;
|
6 | 6 |
use Exception::Class;
|
7 | 7 |
use vars qw($VERSION);
|
8 | |
$VERSION = '0.92';
|
9 | |
|
10 | |
use Exception::Class ( 'Exception::Class::DBI' =>
|
11 | |
{ description => 'DBI exception',
|
12 | |
fields => [qw(err errstr state retval)]
|
13 | |
},
|
14 | |
'Exception::Class::DBI::Unknown' =>
|
15 | |
{ isa => 'Exception::Class::DBI',
|
16 | |
description => 'DBI unknown exception'
|
17 | |
},
|
18 | |
'Exception::Class::DBI::H' =>
|
19 | |
{ isa => 'Exception::Class::DBI',
|
20 | |
description => 'DBI handle exception',
|
21 | |
fields => [qw(warn active kids active_kids compat_mode
|
22 | |
inactive_destroy trace_level
|
23 | |
fetch_hash_key_name chop_blanks
|
24 | |
long_read_len long_trunc_ok taint)]
|
25 | |
},
|
26 | |
'Exception::Class::DBI::DRH' =>
|
27 | |
{ isa => 'Exception::Class::DBI::H',
|
28 | |
description => 'DBI driver handle exception',
|
29 | |
},
|
30 | |
'Exception::Class::DBI::DBH' =>
|
31 | |
{ isa => 'Exception::Class::DBI::H',
|
32 | |
description => 'DBI database handle exception',
|
33 | |
fields => [qw(auto_commit db_name statement
|
34 | |
row_cache_size)]
|
35 | |
},
|
36 | |
'Exception::Class::DBI::STH' =>
|
37 | |
{ isa => 'Exception::Class::DBI::H',
|
38 | |
description => 'DBI statment handle exception',
|
39 | |
fields => [qw(num_of_fields num_of_params field_names
|
40 | |
type precision scale nullable
|
41 | |
cursor_name param_values statement
|
42 | |
rows_in_cache)]
|
43 | |
}
|
44 | |
);
|
|
8 |
$VERSION = '0.93';
|
|
9 |
|
|
10 |
use Exception::Class (
|
|
11 |
'Exception::Class::DBI' => {
|
|
12 |
description => 'DBI exception',
|
|
13 |
fields => [qw(err errstr state retval)]
|
|
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 |
fields => [qw(warn active kids active_kids compat_mode
|
|
25 |
inactive_destroy trace_level fetch_hash_key_name
|
|
26 |
chop_blanks long_read_len long_trunc_ok taint
|
|
27 |
)]
|
|
28 |
},
|
|
29 |
|
|
30 |
'Exception::Class::DBI::DRH' => {
|
|
31 |
isa => 'Exception::Class::DBI::H',
|
|
32 |
description => 'DBI driver handle exception',
|
|
33 |
},
|
|
34 |
|
|
35 |
'Exception::Class::DBI::DBH' => {
|
|
36 |
isa => 'Exception::Class::DBI::H',
|
|
37 |
description => 'DBI database handle exception',
|
|
38 |
fields => [qw(auto_commit db_name statement row_cache_size)]
|
|
39 |
},
|
|
40 |
|
|
41 |
'Exception::Class::DBI::STH' => {
|
|
42 |
isa => 'Exception::Class::DBI::H',
|
|
43 |
description => 'DBI statment handle exception',
|
|
44 |
fields => [qw(num_of_fields num_of_params field_names type precision
|
|
45 |
scale nullable cursor_name param_values statement
|
|
46 |
rows_in_cache
|
|
47 |
)]
|
|
48 |
}
|
|
49 |
);
|
45 | 50 |
|
46 | 51 |
sub handler {
|
47 | 52 |
sub {
|
48 | 53 |
my ($err, $dbh, $retval) = @_;
|
49 | 54 |
if (ref $dbh) {
|
50 | 55 |
# Assemble arguments for a handle exception.
|
51 | |
my @params = ( error => $err,
|
52 | |
errstr => $dbh->errstr,
|
53 | |
err => $dbh->err,
|
54 | |
state => $dbh->state,
|
55 | |
retval => $retval,
|
56 | |
warn => $dbh->{Warn},
|
57 | |
active => $dbh->{Active},
|
58 | |
kids => $dbh->{Kids},
|
59 | |
active_kids => $dbh->{ActiveKids},
|
60 | |
compat_mode => $dbh->{CompatMode},
|
61 | |
inactive_destroy => $dbh->{InactiveDestroy},
|
62 | |
trace_level => $dbh->{TraceLevel},
|
63 | |
fetch_hash_key_name => $dbh->{FetchHashKeyName},
|
64 | |
chop_blanks => $dbh->{ChopBlanks},
|
65 | |
long_read_len => $dbh->{LongReadLen},
|
66 | |
long_trunc_ok => $dbh->{LongTruncOk},
|
67 | |
taint => $dbh->{Taint},
|
68 | |
);
|
|
56 |
my @params = (
|
|
57 |
error => $err,
|
|
58 |
errstr => $dbh->errstr,
|
|
59 |
err => $dbh->err,
|
|
60 |
state => $dbh->state,
|
|
61 |
retval => $retval,
|
|
62 |
warn => $dbh->{Warn},
|
|
63 |
active => $dbh->{Active},
|
|
64 |
kids => $dbh->{Kids},
|
|
65 |
active_kids => $dbh->{ActiveKids},
|
|
66 |
compat_mode => $dbh->{CompatMode},
|
|
67 |
inactive_destroy => $dbh->{InactiveDestroy},
|
|
68 |
trace_level => $dbh->{TraceLevel},
|
|
69 |
fetch_hash_key_name => $dbh->{FetchHashKeyName},
|
|
70 |
chop_blanks => $dbh->{ChopBlanks},
|
|
71 |
long_read_len => $dbh->{LongReadLen},
|
|
72 |
long_trunc_ok => $dbh->{LongTruncOk},
|
|
73 |
taint => $dbh->{Taint},
|
|
74 |
);
|
69 | 75 |
if (UNIVERSAL::isa($dbh, 'DBI::dr')) {
|
70 | 76 |
# Just throw a driver exception. It has no extra attributes.
|
71 | 77 |
Exception::Class::DBI::DRH->throw(@params);
|
72 | 78 |
} elsif (UNIVERSAL::isa($dbh, 'DBI::db')) {
|
73 | 79 |
# Throw a database handle exception.
|
74 | |
Exception::Class::DBI::DBH->throw
|
75 | |
( @params,
|
|
80 |
Exception::Class::DBI::DBH->throw(
|
|
81 |
@params,
|
76 | 82 |
auto_commit => $dbh->{AutoCommit},
|
77 | 83 |
db_name => $dbh->{Name},
|
78 | 84 |
statement => $dbh->{Statement},
|
79 | 85 |
row_cache_size => $dbh->{RowCacheSize}
|
80 | |
);
|
|
86 |
);
|
81 | 87 |
} elsif (UNIVERSAL::isa($dbh, 'DBI::st')) {
|
82 | 88 |
# Throw a statement handle exception.
|
83 | |
Exception::Class::DBI::STH->throw
|
84 | |
( @params,
|
|
89 |
Exception::Class::DBI::STH->throw(
|
|
90 |
@params,
|
85 | 91 |
num_of_fields => $dbh->{NUM_OF_FIELDS},
|
86 | 92 |
num_of_params => $dbh->{NUM_OF_PARAMS},
|
87 | 93 |
field_names => $dbh->{NAME},
|
|
93 | 99 |
param_values => $dbh->{ParamValues},
|
94 | 100 |
statement => $dbh->{Statement},
|
95 | 101 |
rows_in_cache => $dbh->{RowsInCache}
|
96 | |
);
|
|
102 |
);
|
97 | 103 |
} else {
|
98 | 104 |
# Unknown exception. This shouldn't happen.
|
99 | 105 |
Exception::Class::DBI::Unknown->throw(@params);
|
|
107 | 113 |
if ($DBI::lasth) {
|
108 | 114 |
# There was a handle. Get the errors. This may be superfluous,
|
109 | 115 |
# since the handle ought to be in $dbh.
|
110 | |
$exc->throw( error => $err,
|
111 | |
errstr => $DBI::errstr,
|
112 | |
err => $DBI::err,
|
113 | |
state => $DBI::state,
|
114 | |
retval => $retval
|
115 | |
);
|
|
116 |
$exc->throw(
|
|
117 |
error => $err,
|
|
118 |
errstr => $DBI::errstr,
|
|
119 |
err => $DBI::err,
|
|
120 |
state => $DBI::state,
|
|
121 |
retval => $retval
|
|
122 |
);
|
116 | 123 |
} else {
|
117 | 124 |
# No handle, no errors.
|
118 | |
$exc->throw( error => $err,
|
119 | |
retval => $retval
|
120 | |
);
|
|
125 |
$exc->throw(
|
|
126 |
error => $err,
|
|
127 |
retval => $retval
|
|
128 |
);
|
121 | 129 |
}
|
122 | 130 |
}
|
123 | 131 |
};
|
|
135 | 143 |
use DBI;
|
136 | 144 |
use Exception::Class::DBI;
|
137 | 145 |
|
138 | |
my $dbh = DBI->connect( $data_source, $username, $auth,
|
139 | |
{ PrintError => 0,
|
140 | |
RaiseError => 0,
|
141 | |
HandleError => Exception::Class::DBI->handler
|
142 | |
});
|
|
146 |
my $dbh = DBI->connect($data_source, $username, $auth, {
|
|
147 |
PrintError => 0,
|
|
148 |
RaiseError => 0,
|
|
149 |
HandleError => Exception::Class::DBI->handler
|
|
150 |
});
|
143 | 151 |
|
144 | 152 |
eval { $dbh->do($sql) };
|
145 | 153 |
|
|
183 | 191 |
|
184 | 192 |
=item C<handler>
|
185 | 193 |
|
186 | |
my $dbh = DBI->connect( $data_source, $username, $auth,
|
187 | |
{ PrintError => 0,
|
188 | |
RaiseError => 0,
|
189 | |
HandleError => Exception::Class::DBI->handler
|
190 | |
});
|
|
194 |
my $dbh = DBI->connect($data_source, $username, $auth, {
|
|
195 |
PrintError => 0,
|
|
196 |
RaiseError => 0,
|
|
197 |
HandleError => Exception::Class::DBI->handler
|
|
198 |
});
|
191 | 199 |
|
192 | 200 |
This method returns a code reference appropriate for passing to the DBI
|
193 | 201 |
C<HandleError> attribute. When DBI encounters an error, it checks its
|
|
562 | 570 |
|
563 | 571 |
=head1 COPYRIGHT AND LICENSE
|
564 | 572 |
|
565 | |
Copyright (c) 2002-2004, David Wheeler. All Rights Reserved.
|
|
573 |
Copyright (c) 2002-2005, David Wheeler. All Rights Reserved.
|
566 | 574 |
|
567 | 575 |
This module is free software; you can redistribute it and/or modify it under
|
568 | 576 |
the same terms as Perl itself.
|