line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package DBIx::DSN::Resolver; |
2
|
|
|
|
|
|
|
|
3
|
3
|
|
|
3
|
|
79896
|
use strict; |
|
3
|
|
|
|
|
7
|
|
|
3
|
|
|
|
|
153
|
|
4
|
3
|
|
|
3
|
|
14
|
use warnings; |
|
3
|
|
|
|
|
5
|
|
|
3
|
|
|
|
|
95
|
|
5
|
3
|
|
|
3
|
|
11044
|
use DBI; |
|
3
|
|
|
|
|
65613
|
|
|
3
|
|
|
|
|
214
|
|
6
|
3
|
|
|
3
|
|
3808
|
use Socket; |
|
3
|
|
|
|
|
13475
|
|
|
3
|
|
|
|
|
2225
|
|
7
|
3
|
|
|
3
|
|
32
|
use Carp; |
|
3
|
|
|
|
|
7
|
|
|
3
|
|
|
|
|
1747
|
|
8
|
|
|
|
|
|
|
|
9
|
|
|
|
|
|
|
our $VERSION = '0.09'; |
10
|
|
|
|
|
|
|
|
11
|
|
|
|
|
|
|
sub new { |
12
|
3
|
|
|
3
|
0
|
108201
|
my $class = shift; |
13
|
3
|
50
|
|
|
|
50
|
my %args = @_ == 1 ? %{$_[0]} : @_; |
|
0
|
|
|
|
|
0
|
|
14
|
|
|
|
|
|
|
bless { |
15
|
|
|
|
|
|
|
resolver => sub { |
16
|
12
|
|
|
12
|
|
6156
|
my $ipaddr = Socket::inet_aton($_[0]); |
17
|
12
|
100
|
|
|
|
578
|
return unless $ipaddr; |
18
|
11
|
|
|
|
|
91
|
Socket::inet_ntoa($ipaddr); |
19
|
|
|
|
|
|
|
}, |
20
|
3
|
|
|
|
|
89
|
%args, |
21
|
|
|
|
|
|
|
}, $class; |
22
|
|
|
|
|
|
|
} |
23
|
|
|
|
|
|
|
|
24
|
|
|
|
|
|
|
sub resolv { |
25
|
15
|
|
|
15
|
0
|
13543
|
my $self = shift; |
26
|
15
|
|
|
|
|
52
|
my $dsn = shift; |
27
|
15
|
50
|
|
|
|
62
|
return unless $dsn; |
28
|
|
|
|
|
|
|
|
29
|
15
|
100
|
|
|
|
91
|
my ($scheme, $driver, $attr_string, $attr_hash, $driver_dsn) |
30
|
|
|
|
|
|
|
= DBI->parse_dsn($dsn) |
31
|
|
|
|
|
|
|
or croak "Can't parse DBI DSN '$dsn'"; |
32
|
|
|
|
|
|
|
|
33
|
14
|
|
|
|
|
379
|
my %driver_hash; |
34
|
|
|
|
|
|
|
my @driver_hash_keys; #to keep order |
35
|
14
|
|
|
|
|
50
|
for my $d ( split /;/, $driver_dsn ) { |
36
|
28
|
|
|
|
|
68
|
my ( $k, $v) = split /=/, $d, 2; |
37
|
28
|
|
|
|
|
66
|
$driver_hash{$k} = $v; |
38
|
28
|
|
|
|
|
61
|
push @driver_hash_keys, $k; |
39
|
|
|
|
|
|
|
} |
40
|
14
|
100
|
|
|
|
48
|
my $host_key = exists $driver_hash{hostname} ? 'hostname' : 'host'; |
41
|
14
|
|
|
|
|
28
|
my $host = $driver_hash{$host_key}; |
42
|
14
|
100
|
|
|
|
52
|
return $dsn unless $host; |
43
|
|
|
|
|
|
|
|
44
|
13
|
|
|
|
|
23
|
my $port = ''; |
45
|
13
|
100
|
|
|
|
52
|
if ( $host =~ m!:(\d+)$! ) { |
46
|
2
|
|
|
|
|
7
|
$port = ':'.$1; |
47
|
2
|
|
|
|
|
15
|
$host =~ s!:(\d+)$!!; |
48
|
|
|
|
|
|
|
} |
49
|
|
|
|
|
|
|
|
50
|
13
|
100
|
|
|
|
41
|
my $ipaddr = $self->{resolver}->($host) |
51
|
|
|
|
|
|
|
or croak "Can't resolv host name: $host, $!"; |
52
|
12
|
|
|
|
|
35
|
$driver_hash{$host_key} = $ipaddr . $port; |
53
|
|
|
|
|
|
|
|
54
|
12
|
100
|
|
|
|
22
|
$driver_dsn = join ';', map { defined $driver_hash{$_} ? $_.'='.$driver_hash{$_} : $_ } @driver_hash_keys; |
|
25
|
|
|
|
|
111
|
|
55
|
12
|
100
|
|
|
|
39
|
$attr_string = defined $attr_string |
56
|
|
|
|
|
|
|
? '('.$attr_string.')' |
57
|
|
|
|
|
|
|
: ''; |
58
|
12
|
|
|
|
|
323
|
sprintf "%s:%s%s:%s", $scheme, $driver, $attr_string, $driver_dsn; |
59
|
|
|
|
|
|
|
} |
60
|
|
|
|
|
|
|
|
61
|
|
|
|
|
|
|
|
62
|
|
|
|
|
|
|
1; |
63
|
|
|
|
|
|
|
__END__ |