line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package DBD::Simulated; |
2
|
|
|
|
|
|
|
|
3
|
|
|
|
|
|
|
=pod |
4
|
|
|
|
|
|
|
|
5
|
|
|
|
|
|
|
=head1 NAME |
6
|
|
|
|
|
|
|
|
7
|
|
|
|
|
|
|
DBD::Simulated - Fake DBI database driver simulating a database for testing error checks |
8
|
|
|
|
|
|
|
|
9
|
|
|
|
|
|
|
=head1 SYNOPSIS |
10
|
|
|
|
|
|
|
|
11
|
|
|
|
|
|
|
Do not use this directly without DBI. See L for more information about using a DBD module. |
12
|
|
|
|
|
|
|
|
13
|
|
|
|
|
|
|
=head1 DESCRIPTION |
14
|
|
|
|
|
|
|
|
15
|
|
|
|
|
|
|
Every major project has it's own database access module, I never saw one directly using DBI all the time. |
16
|
|
|
|
|
|
|
|
17
|
|
|
|
|
|
|
Custom source must be tested but it isn't easy to create predefined error cases using real databases for testing |
18
|
|
|
|
|
|
|
your own database error handling source. |
19
|
|
|
|
|
|
|
|
20
|
|
|
|
|
|
|
This fake database driver simulates a real database usable via DBI. It can neither store nor fetch data or run any |
21
|
|
|
|
|
|
|
kind of real SQL queries but it could return any error state you want. |
22
|
|
|
|
|
|
|
|
23
|
|
|
|
|
|
|
=head1 connect |
24
|
|
|
|
|
|
|
|
25
|
|
|
|
|
|
|
Use DBI->connect as usual. |
26
|
|
|
|
|
|
|
|
27
|
|
|
|
|
|
|
my $dbh = DBI->connect('DBI:Simulated:database=success;simulated_error=256'); |
28
|
|
|
|
|
|
|
|
29
|
|
|
|
|
|
|
Only one dsn named argument is supported right now: I expects a positive or negative number which is returned |
30
|
|
|
|
|
|
|
as connect error code and no connection object is returned. All other arguments are simply ignored. |
31
|
|
|
|
|
|
|
|
32
|
|
|
|
|
|
|
You may pass any real-life DSN string to get a simulated database handle and simply add the I to get a simulated |
33
|
|
|
|
|
|
|
error code. |
34
|
|
|
|
|
|
|
|
35
|
|
|
|
|
|
|
=head1 prepare |
36
|
|
|
|
|
|
|
|
37
|
|
|
|
|
|
|
Any query will be accepted by prepare, all arguments are ignored. |
38
|
|
|
|
|
|
|
|
39
|
|
|
|
|
|
|
Add I to your query to trigger a prepare-error. XXX must be a number. |
40
|
|
|
|
|
|
|
|
41
|
|
|
|
|
|
|
my $dbh = DBI->connect('DBI:Simulated:database=success'); |
42
|
|
|
|
|
|
|
$dbh->prepare('SELECT * FROM MyTable WHERE simulated_prepare_error=1024'); |
43
|
|
|
|
|
|
|
|
44
|
|
|
|
|
|
|
=head1 execute |
45
|
|
|
|
|
|
|
|
46
|
|
|
|
|
|
|
Prepare any statement containing I and execute it to trigger error code XXX (must be a number - as always). |
47
|
|
|
|
|
|
|
|
48
|
|
|
|
|
|
|
my $dbh = DBI->connect('DBI:Simulated:database=success'); |
49
|
|
|
|
|
|
|
my $sth = $dbh->prepare('SELECT * FROM MyTable WHERE simulated_execute_error=1024'); |
50
|
|
|
|
|
|
|
$sth->execute; |
51
|
|
|
|
|
|
|
|
52
|
|
|
|
|
|
|
=head1 fetch* |
53
|
|
|
|
|
|
|
|
54
|
|
|
|
|
|
|
Prepare any statement containing I and to trigger error code XXX (must be a number - as always). |
55
|
|
|
|
|
|
|
|
56
|
|
|
|
|
|
|
my $dbh = DBI->connect('DBI:Simulated:database=success'); |
57
|
|
|
|
|
|
|
my $sth = $dbh->prepare('SELECT * FROM MyTable WHERE simulated_fetch_error=65535'); |
58
|
|
|
|
|
|
|
$sth->execute; |
59
|
|
|
|
|
|
|
$sth->fetchall_arrayref; |
60
|
|
|
|
|
|
|
|
61
|
|
|
|
|
|
|
Notice that fetch always returns undef (looks like to data returned by the query) but also sets the error code if requested. |
62
|
|
|
|
|
|
|
|
63
|
|
|
|
|
|
|
=head1 Other DBI methods |
64
|
|
|
|
|
|
|
|
65
|
|
|
|
|
|
|
Other DBI methods like I and I fall back to the default methods shown above. All error strings may be used when calling them. |
66
|
|
|
|
|
|
|
|
67
|
|
|
|
|
|
|
=cut |
68
|
|
|
|
|
|
|
|
69
|
1
|
|
|
1
|
|
38650
|
use 5.010; |
|
1
|
|
|
|
|
4
|
|
|
1
|
|
|
|
|
134
|
|
70
|
1
|
|
|
1
|
|
7
|
use strict; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
45
|
|
71
|
1
|
|
|
1
|
|
7
|
use warnings; |
|
1
|
|
|
|
|
12
|
|
|
1
|
|
|
|
|
119
|
|
72
|
|
|
|
|
|
|
|
73
|
1
|
|
|
1
|
|
1259
|
use DBD::Simulated::dr; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
29
|
|
74
|
1
|
|
|
1
|
|
1267
|
use DBD::Simulated::db; |
|
1
|
|
|
|
|
3
|
|
|
1
|
|
|
|
|
34
|
|
75
|
1
|
|
|
1
|
|
1357
|
use DBD::Simulated::st; |
|
1
|
|
|
|
|
80
|
|
|
1
|
|
|
|
|
247
|
|
76
|
|
|
|
|
|
|
|
77
|
|
|
|
|
|
|
our $VERSION = '0.01'; |
78
|
|
|
|
|
|
|
our $drh; |
79
|
|
|
|
|
|
|
|
80
|
|
|
|
|
|
|
sub driver { |
81
|
0
|
0
|
|
0
|
0
|
|
return $drh if $drh; # already created - return same one |
82
|
0
|
|
|
|
|
|
my ($class, $attr) = @_; |
83
|
|
|
|
|
|
|
|
84
|
0
|
|
|
|
|
|
$class .= "::dr"; |
85
|
|
|
|
|
|
|
|
86
|
0
|
0
|
|
|
|
|
$drh = DBI::_new_drh( |
87
|
|
|
|
|
|
|
$class, |
88
|
|
|
|
|
|
|
{ |
89
|
|
|
|
|
|
|
'Name' => 'Simulated', |
90
|
|
|
|
|
|
|
'Version' => $VERSION, |
91
|
|
|
|
|
|
|
'Attribution' => 'DBD::Simulated by Sebastian Willing', |
92
|
|
|
|
|
|
|
} |
93
|
|
|
|
|
|
|
) or return undef; |
94
|
|
|
|
|
|
|
|
95
|
0
|
|
|
|
|
|
return $drh; |
96
|
|
|
|
|
|
|
} |
97
|
|
|
|
|
|
|
|
98
|
|
|
|
|
|
|
1; |
99
|
|
|
|
|
|
|
|
100
|
|
|
|
|
|
|
=pod |
101
|
|
|
|
|
|
|
|
102
|
|
|
|
|
|
|
=head1 SUPPORT |
103
|
|
|
|
|
|
|
|
104
|
|
|
|
|
|
|
No support is available |
105
|
|
|
|
|
|
|
|
106
|
|
|
|
|
|
|
=head1 AUTHOR |
107
|
|
|
|
|
|
|
|
108
|
|
|
|
|
|
|
Copyright 2012 Sebastian Willing, eGENTIC Systems L |
109
|
|
|
|
|
|
|
|
110
|
|
|
|
|
|
|
=cut |