line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package DBIx::MultiDB; |
2
|
|
|
|
|
|
|
|
3
|
8
|
|
|
8
|
|
391477
|
use warnings; |
|
8
|
|
|
|
|
22
|
|
|
8
|
|
|
|
|
330
|
|
4
|
8
|
|
|
8
|
|
45
|
use strict; |
|
8
|
|
|
|
|
17
|
|
|
8
|
|
|
|
|
263
|
|
5
|
|
|
|
|
|
|
|
6
|
8
|
|
|
8
|
|
48
|
use Carp; |
|
8
|
|
|
|
|
31
|
|
|
8
|
|
|
|
|
620
|
|
7
|
8
|
|
|
8
|
|
1047
|
use Data::Dumper; |
|
8
|
|
|
|
|
9337
|
|
|
8
|
|
|
|
|
337
|
|
8
|
8
|
|
|
8
|
|
21480
|
use DBI; |
|
8
|
|
|
|
|
160562
|
|
|
8
|
|
|
|
|
5818
|
|
9
|
|
|
|
|
|
|
|
10
|
|
|
|
|
|
|
our $VERSION = '0.06'; |
11
|
|
|
|
|
|
|
|
12
|
|
|
|
|
|
|
sub new { |
13
|
6
|
|
|
6
|
1
|
88864
|
my ( $class, %param ) = @_; |
14
|
|
|
|
|
|
|
|
15
|
6
|
|
|
|
|
644
|
bless { base => {%param}, left_join => [] }, $class; |
16
|
|
|
|
|
|
|
} |
17
|
|
|
|
|
|
|
|
18
|
|
|
|
|
|
|
*attach = \&left_join; |
19
|
|
|
|
|
|
|
*join = \&left_join; |
20
|
|
|
|
|
|
|
|
21
|
|
|
|
|
|
|
sub left_join { |
22
|
6
|
|
|
6
|
1
|
668
|
my ( $self, %param ) = @_; |
23
|
|
|
|
|
|
|
|
24
|
6
|
|
66
|
|
|
505
|
my $dbh = $param{dbh} |
25
|
|
|
|
|
|
|
|| DBI->connect( $param{dsn}, $param{user}, $param{password}, { RaiseError => 1 } ); |
26
|
|
|
|
|
|
|
|
27
|
|
|
|
|
|
|
# the preferred approach is to list the primary key on this table ("key") |
28
|
|
|
|
|
|
|
# and the foreign key in the base table ("referenced_by"). |
29
|
|
|
|
|
|
|
|
30
|
|
|
|
|
|
|
# alternatively, we can specify the foreign key in the base table ("key") |
31
|
|
|
|
|
|
|
# and which key it "references" in this table ("references"): |
32
|
6
|
100
|
66
|
|
|
122747
|
if ( $param{references} and $param{key} ) { |
33
|
1
|
|
|
|
|
4
|
$param{referenced_by} = delete $param{key}; |
34
|
1
|
|
|
|
|
5
|
$param{key} = delete $param{references}; |
35
|
|
|
|
|
|
|
} |
36
|
|
|
|
|
|
|
|
37
|
6
|
50
|
|
|
|
34
|
warn "Retrieving left_join data ($param{key} referenced by $param{referenced_by})...\n" if $param{VERBOSE}; |
38
|
|
|
|
|
|
|
|
39
|
|
|
|
|
|
|
# put everything in memory |
40
|
6
|
|
|
|
|
139
|
my $data = $dbh->selectall_hashref( $param{sql}, $param{key}, ); |
41
|
|
|
|
|
|
|
|
42
|
1
|
|
|
|
|
488
|
push @{ $self->{left_join} }, { %param, data => $data }; |
|
1
|
|
|
|
|
17
|
|
43
|
|
|
|
|
|
|
|
44
|
1
|
50
|
33
|
|
|
10
|
if ( $param{VERBOSE} and $param{VERBOSE} >= 2 ) { |
45
|
0
|
|
|
|
|
0
|
require Devel::Size; |
46
|
0
|
|
|
|
|
0
|
warn "Calculating memory usage...\n"; |
47
|
0
|
|
|
|
|
0
|
my $memory_usage = sprintf( '%1.2f', ( Devel::Size::total_size($data) / ( 1024 * 1024 ) ) ); # MB |
48
|
0
|
|
|
|
|
0
|
warn "Memory usage: $memory_usage MB\n\n"; |
49
|
|
|
|
|
|
|
} |
50
|
|
|
|
|
|
|
|
51
|
1
|
|
|
|
|
6
|
return; |
52
|
|
|
|
|
|
|
} |
53
|
|
|
|
|
|
|
|
54
|
|
|
|
|
|
|
sub prepare { |
55
|
1
|
|
|
1
|
1
|
6
|
my ( $self, $sql, $attr ) = @_; |
56
|
|
|
|
|
|
|
|
57
|
|
|
|
|
|
|
my $dbh = $self->{base}->{dbh} |
58
|
1
|
|
33
|
|
|
5
|
|| DBI->connect( @{ $self->{base} }{ 'dsn', 'user', 'password' }, { RaiseError => 1 } ); |
59
|
|
|
|
|
|
|
|
60
|
1
|
|
|
|
|
8
|
my $sth = $dbh->prepare($sql); |
61
|
|
|
|
|
|
|
|
62
|
1
|
50
|
|
|
|
344
|
if ( defined $attr ) { |
63
|
0
|
|
|
|
|
0
|
$self->{base}->{$_} = $attr->{$_} for keys %{$attr}; |
|
0
|
|
|
|
|
0
|
|
64
|
|
|
|
|
|
|
} |
65
|
|
|
|
|
|
|
|
66
|
1
|
|
|
|
|
3
|
$self->{base}->{sth} = $sth; |
67
|
|
|
|
|
|
|
|
68
|
1
|
|
|
|
|
3
|
return $self; |
69
|
|
|
|
|
|
|
} |
70
|
|
|
|
|
|
|
|
71
|
|
|
|
|
|
|
sub execute { |
72
|
1
|
|
|
1
|
1
|
8
|
my $self = shift; |
73
|
|
|
|
|
|
|
|
74
|
1
|
50
|
|
|
|
6
|
if ( !$self->{base}->{sth} ) { |
75
|
1
|
|
|
|
|
6
|
$self->prepare( $self->{base}->{sql} ); |
76
|
|
|
|
|
|
|
} |
77
|
|
|
|
|
|
|
|
78
|
1
|
|
|
|
|
309
|
$self->{base}->{sth}->execute(); |
79
|
|
|
|
|
|
|
|
80
|
0
|
|
|
|
|
|
return $self; |
81
|
|
|
|
|
|
|
} |
82
|
|
|
|
|
|
|
|
83
|
|
|
|
|
|
|
sub fetchrow_hashref { |
84
|
0
|
|
|
0
|
1
|
|
my $self = shift; |
85
|
|
|
|
|
|
|
|
86
|
|
|
|
|
|
|
# get the base row |
87
|
0
|
|
|
|
|
|
my $row = $self->{base}->{sth}->fetchrow_hashref(); |
88
|
|
|
|
|
|
|
|
89
|
0
|
0
|
|
|
|
|
return if !$row; |
90
|
|
|
|
|
|
|
|
91
|
|
|
|
|
|
|
# now we are going to attach the left_join data |
92
|
|
|
|
|
|
|
|
93
|
0
|
|
|
|
|
|
my %row = %{$row}; |
|
0
|
|
|
|
|
|
|
94
|
|
|
|
|
|
|
|
95
|
0
|
|
|
|
|
|
for my $join ( @{ $self->{left_join} } ) { |
|
0
|
|
|
|
|
|
|
96
|
0
|
|
|
|
|
|
my $base_key = $join->{referenced_by}; |
97
|
0
|
|
|
|
|
|
my $base_value = $row{$base_key}; |
98
|
|
|
|
|
|
|
|
99
|
|
|
|
|
|
|
# now, look up the base value in the cached join data... |
100
|
0
|
|
|
|
|
|
for my $joined_key ( keys %{ $join->{data}->{$base_value} } ) { |
|
0
|
|
|
|
|
|
|
101
|
0
|
|
|
|
|
|
my $joined_value = $join->{data}->{$base_value}->{$joined_key}; |
102
|
0
|
|
|
|
|
|
$row{$joined_key} = $joined_value; |
103
|
|
|
|
|
|
|
} |
104
|
|
|
|
|
|
|
} |
105
|
|
|
|
|
|
|
|
106
|
0
|
|
|
|
|
|
return \%row; |
107
|
|
|
|
|
|
|
} |
108
|
|
|
|
|
|
|
|
109
|
|
|
|
|
|
|
1; |
110
|
|
|
|
|
|
|
|
111
|
|
|
|
|
|
|
__END__ |