line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package DBIx::RewriteDSN; |
2
|
|
|
|
|
|
|
|
3
|
3
|
|
|
3
|
|
74096
|
use strict; |
|
3
|
|
|
|
|
8
|
|
|
3
|
|
|
|
|
104
|
|
4
|
3
|
|
|
3
|
|
16
|
use warnings; |
|
3
|
|
|
|
|
6
|
|
|
3
|
|
|
|
|
128
|
|
5
|
|
|
|
|
|
|
our $VERSION = '0.05'; |
6
|
|
|
|
|
|
|
|
7
|
3
|
|
|
3
|
|
6984
|
use DBI; |
|
3
|
|
|
|
|
66979
|
|
|
3
|
|
|
|
|
238
|
|
8
|
3
|
|
|
3
|
|
3118
|
use File::Slurp; |
|
3
|
|
|
|
|
47023
|
|
|
3
|
|
|
|
|
651
|
|
9
|
|
|
|
|
|
|
|
10
|
|
|
|
|
|
|
my $orig_connect = \&DBI::connect; |
11
|
|
|
|
|
|
|
my $filename; |
12
|
|
|
|
|
|
|
my $RULES = ""; |
13
|
|
|
|
|
|
|
|
14
|
|
|
|
|
|
|
sub import { |
15
|
3
|
|
|
3
|
|
55
|
my ($class, %opts) = @_; |
16
|
3
|
100
|
|
|
|
20
|
if ($opts{-file}) { |
17
|
2
|
|
|
|
|
4
|
$filename = $opts{-file}; |
18
|
2
|
|
|
|
|
9
|
$RULES .= File::Slurp::slurp($filename) . "\n"; |
19
|
|
|
|
|
|
|
} |
20
|
3
|
50
|
|
|
|
239
|
if ($opts{-rules}) { |
21
|
0
|
|
|
|
|
0
|
$RULES .= $opts{-rules} . "\n"; |
22
|
|
|
|
|
|
|
} |
23
|
|
|
|
|
|
|
|
24
|
3
|
100
|
100
|
|
|
2547
|
if ($RULES && $ENV{DBI_REWRITE_DSN}) { |
25
|
1
|
|
|
|
|
12
|
$class->enable; |
26
|
|
|
|
|
|
|
} |
27
|
|
|
|
|
|
|
} |
28
|
|
|
|
|
|
|
|
29
|
|
|
|
|
|
|
sub enable { |
30
|
1
|
|
|
1
|
1
|
2
|
my ($class) = @_; |
31
|
3
|
|
|
3
|
|
30
|
no warnings 'redefine'; |
|
3
|
|
|
|
|
7
|
|
|
3
|
|
|
|
|
233
|
|
32
|
1
|
|
|
|
|
2566
|
*DBI::connect = \&_connect; |
33
|
|
|
|
|
|
|
} |
34
|
|
|
|
|
|
|
|
35
|
|
|
|
|
|
|
sub disable { |
36
|
0
|
|
|
0
|
1
|
|
my ($class) = @_; |
37
|
3
|
|
|
3
|
|
20
|
no warnings 'redefine'; |
|
3
|
|
|
|
|
14
|
|
|
3
|
|
|
|
|
1330
|
|
38
|
0
|
|
|
|
|
|
*DBI::connect = $orig_connect; |
39
|
|
|
|
|
|
|
} |
40
|
|
|
|
|
|
|
|
41
|
|
|
|
|
|
|
sub prepend_rules { |
42
|
0
|
|
|
0
|
1
|
|
my ($class, $rules) = @_; |
43
|
0
|
|
|
|
|
|
$RULES = $rules . "\n" . $RULES; |
44
|
|
|
|
|
|
|
} |
45
|
|
|
|
|
|
|
|
46
|
|
|
|
|
|
|
|
47
|
|
|
|
|
|
|
sub rewrite { |
48
|
0
|
|
|
0
|
0
|
|
my ($dsn) = @_; |
49
|
|
|
|
|
|
|
|
50
|
0
|
|
|
|
|
|
my $new_dsn; |
51
|
0
|
|
|
|
|
|
for (split /\n/, $RULES) { |
52
|
0
|
|
|
|
|
|
chomp; |
53
|
0
|
|
|
|
|
|
$_ =~ s/^\s+|\s+$//g; |
54
|
0
|
0
|
|
|
|
|
$_ or next; |
55
|
0
|
0
|
|
|
|
|
$_ =~ /^#/ and next; |
56
|
|
|
|
|
|
|
|
57
|
0
|
|
|
|
|
|
my ($match, $rewrite) = split(/\s+/, $_); |
58
|
0
|
0
|
|
|
|
|
if ($dsn =~ $match) { |
59
|
0
|
|
|
|
|
|
$rewrite =~ s{\\}{\\\\}g; |
60
|
0
|
|
0
|
|
|
|
$new_dsn = eval(sprintf('qq{%s}', $rewrite || "")); ## no critic |
61
|
0
|
|
|
|
|
|
last; |
62
|
|
|
|
|
|
|
} |
63
|
|
|
|
|
|
|
} |
64
|
|
|
|
|
|
|
|
65
|
0
|
0
|
0
|
|
|
|
if ($new_dsn && $new_dsn ne $dsn) { |
66
|
0
|
0
|
0
|
|
|
|
print STDERR sprintf("Rewrote '%s' to '%s'\n", $dsn, $new_dsn) if ($ENV{DBI_REWRITE_DSN} || "") eq 'verbose'; |
67
|
0
|
|
|
|
|
|
$dsn = $new_dsn; |
68
|
|
|
|
|
|
|
} else { |
69
|
0
|
0
|
0
|
|
|
|
print STDERR sprintf("Didn't rewrite %s\n", $dsn) if ($ENV{DBI_REWRITE_DSN} || "") eq 'verbose'; |
70
|
|
|
|
|
|
|
} |
71
|
|
|
|
|
|
|
|
72
|
0
|
|
|
|
|
|
$dsn; |
73
|
|
|
|
|
|
|
} |
74
|
|
|
|
|
|
|
|
75
|
|
|
|
|
|
|
sub _connect { |
76
|
0
|
|
|
0
|
|
|
my ($class, $dsn, @rest) = @_; |
77
|
0
|
|
|
|
|
|
$dsn = DBIx::RewriteDSN::rewrite($dsn); |
78
|
0
|
|
|
|
|
|
$orig_connect->($class, $dsn, @rest); |
79
|
|
|
|
|
|
|
} |
80
|
|
|
|
|
|
|
|
81
|
|
|
|
|
|
|
|
82
|
|
|
|
|
|
|
1; |
83
|
|
|
|
|
|
|
__END__ |