File Coverage

blib/lib/DBIx/Introspector/Driver.pm
Criterion Covered Total %
statement 44 54 81.4
branch 18 26 69.2
condition 2 6 33.3
subroutine 9 11 81.8
pod n/a
total 73 97 75.2


line stmt bran cond sub pod time code
1             package
2             DBIx::Introspector::Driver;
3              
4 4     4   26 use Moo;
  4         13  
  4         164  
5              
6             has name => (
7             is => 'ro',
8             required => 1,
9             );
10              
11             has _connected_determination_strategy => (
12             is => 'ro',
13             default => sub { sub { 1 } },
14             init_arg => 'connected_determination_strategy',
15             );
16              
17             has _unconnected_determination_strategy => (
18             is => 'ro',
19             default => sub { sub { 1 } },
20             init_arg => 'unconnected_determination_strategy',
21             );
22              
23             has _connected_options => (
24             is => 'ro',
25             builder => sub {
26             +{
27 3     3   27 _introspector_driver => sub { $_[0]->name },
28             }
29 55     55   10362 },
30             init_arg => 'connected_options',
31             );
32              
33             has _unconnected_options => (
34             is => 'ro',
35             builder => sub {
36             +{
37 3     3   17 _introspector_driver => sub { $_[0]->name },
38             }
39 55     55   1507 },
40             init_arg => 'unconnected_options',
41             );
42              
43             has _parents => (
44             is => 'ro',
45             default => sub { +[] },
46             init_arg => 'parents',
47             );
48              
49             sub _add_connected_option {
50 1     1   4 my ($self, $key, $value) = @_;
51              
52 1         9 $self->_connected_options->{$key} = $value
53             }
54              
55             sub _add_unconnected_option {
56 0     0   0 my ($self, $key, $value) = @_;
57              
58 0         0 $self->_unconnected_options->{$key} = $value
59             }
60              
61             sub _determine {
62 43     43   62 my ($self, $dbh, $dsn) = @_;
63              
64 43         95 my $connected_strategy = $self->_connected_determination_strategy;
65              
66 43 100       121 return $self->$connected_strategy($dbh, $dsn) if $dbh;
67              
68 20         46 my $unconnected_strategy = $self->_unconnected_determination_strategy;
69 20         52 $self->$unconnected_strategy($dsn)
70             }
71              
72             sub _get_when_unconnected {
73 17     17   55 my ($self, $args) = @_;
74              
75 17         26 my $drivers_by_name = $args->{drivers_by_name};
76 17         25 my $key = $args->{key};
77              
78 17         41 my $option = $self->_unconnected_options->{$key};
79              
80 17 100       30 if ($option) {
  11 100       36  
81 6 50 33     63 return $option->($self, $args->{dbh})
82             if ref $option && ref $option eq 'CODE';
83 0         0 return $option;
84             }
85             elsif (@{$self->_parents}) {
86 8         9 my @p = @{$self->_parents};
  8         23  
87 8         14 for my $parent (@p) {
88 8         12 my $driver = $drivers_by_name->{$parent};
89 8 50       20 die "no such driver <$parent>" unless $driver;
90 8         35 my $ret = $driver->_get_when_unconnected($args);
91 8 100       50 return $ret if defined $ret
92             }
93             }
94             return undef
95 9         20 }
96              
97             sub _get_when_connected {
98 18     18   76 my ($self, $args) = @_;
99              
100 18         29 my $drivers_by_name = $args->{drivers_by_name};
101 18         50 my $key = $args->{key};
102              
103 18         49 my $option = $self->_connected_options->{$key};
104              
105 18 100       41 if ($option) {
  10 100       46  
106 8 50 33     85 return $option->($self, $args->{dbh}, $args->{dsn})
107             if ref $option && ref $option eq 'CODE';
108 0         0 return $option;
109             }
110             elsif (@{$self->_parents}) {
111 8         11 my @p = @{$self->_parents};
  8         34  
112 8         16 for my $parent (@p) {
113 8         15 my $driver = $drivers_by_name->{$parent};
114 8 50       19 die "no such driver <$parent>" unless $driver;
115 8         35 my $ret = $driver->_get_when_connected($args);
116 8 100       57 return $ret if $ret
117             }
118             }
119             return undef
120 6         11 }
121              
122             sub _get_info_from_dbh {
123 0     0     my ($self, $dbh, $info) = @_;
124              
125 0 0         if ($info =~ /[^0-9]/) {
126 0           require DBI::Const::GetInfoType;
127 0           $info = $DBI::Const::GetInfoType::GetInfoType{$info};
128 0 0         die "Info type '$_[1]' not provided by DBI::Const::GetInfoType"
129             unless defined $info;
130             }
131              
132 0           $dbh->get_info($info);
133             }
134              
135             1;