blib/lib/auto/share/dist/Cmd-Dwarf/app/lib/Dwarf/Plugin/Teng.pm untested

filter: Statementstmt 0.0% 0 / 34 Branchbran 0.0% 0 / 4 Conditioncond 0.0% 0 / 14 MC/DCmcdc 0.0% 0 / 8 Subroutinesub 0.0% 0 / 3 Podpod 0.0% 0 / 1 totaltotal 0.0% 0 / 64 CC 3 SCAR 20.8
CC
3
Cov
0%
init
12.0
BEGIN
2.0
BEGIN
2.0
CRAP 8.0
1 package Dwarf::Plugin::Teng;
2 use Dwarf::Pragma;
3 use Dwarf::Util qw/add_method load_class/;
4  
5 sub init {
6 my ($class, $c, $conf) = @_;
7 $conf ||= {};
8  
9 my $db_class = $conf->{db_class} || $c->namespace . '::DB';
10 my $default_db = $conf->{default_db} || 'master';
11  
12 my $connect_info = $c->config->get('db');
13  
14 $c->{'dwarf.db'} ||= {};
15  
16 add_method($c, db => sub {
17 my ($self, $key) = @_;
18 $key ||= $default_db;
19  
20 unless (defined $self->{'dwarf.db'}->{$key}) {
21 $self->connect_db($db_class, $key, $connect_info->{$key});
22 }
23  
24 return $self->{'dwarf.db'}->{$key};
25 });
26  
27 add_method($c, connect_db => sub {
28 my ($self, $db_class, $key, $connect_info) = @_;
29 load_class($db_class);
30 $self->{'dwarf.db'}->{$key} = $db_class->new({
31 connect_info => [
32 $connect_info->{dsn},
33 $connect_info->{username},
34 $connect_info->{password},
35 $connect_info->{opts},
36 ],
37 });
38 });
39  
40 add_method($c, dbh => sub {
41 my $self = shift;
42 $self->db->dbh;
43 });
44  
45 add_method($c, disconnect_db => sub {
46 my $self = shift;
47 my $db = $self->{'dwarf.db'};
48 ref $db eq 'HASH' or return;
49 for my $d (values %$db) {
50 $d->disconnect if defined $d;
51 }
52 $self->{'dwarf.db'} = undef;
53 });
54 }
55  
56 1;