| 1 | package DB::Introspector; | ||||||||||||||||||||
| 2 | |||||||||||||||||||||
| 3 | 1 | ▶ | use strict; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 4 | |||||||||||||||||||||
| 5 | 1 | $DB::Introspector::VERSION = '0.08'; | |||||||||||||||||||
| 6 | |||||||||||||||||||||
| 7 | 1 | ▶ | use DBI; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 8 | 1 | ▶ | use Config::Properties; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 9 | 1 | ▶ | use IO::File; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 10 | |||||||||||||||||||||
| 11 | 1 | ▶ | use constant DRIVER_PROPERTIES_FILE => 'driver.properties'; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 12 | |||||||||||||||||||||
| 13 | 1 | ▶ | use vars qw( %REGISTRY %INC ); | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 14 | |||||||||||||||||||||
| 15 | 1 | DB::Introspector->register_drivers(); | |||||||||||||||||||
| 16 | |||||||||||||||||||||
| 17 | 0 | ▶ | sub lookup_table { | ||||||||||||||||||
|
Subroutine
lookup_table: not called CC=2 SCAR=17.9
Pod
undocumented
| |||||||||||||||||||||
| 18 | 0 | my $self = shift; | |||||||||||||||||||
| 19 | 0 | ▶ | die("lookup_table method not defined in ". (ref($self) || $self)); | ||||||||||||||||||
|
Condition0%0/3
ref $self || $self
MC/DC0%0/2
ref $self || $self
ref $self
$self
Truth table0%0/3
A || B
A= ref $selfB= $self
| |||||||||||||||||||||
| 20 | } | ||||||||||||||||||||
| 21 | |||||||||||||||||||||
| 22 | 0 | ▶ | sub lookup_all_tables { | ||||||||||||||||||
|
Subroutine
lookup_all_tables: not called CC=2 SCAR=17.9
Pod
undocumented
| |||||||||||||||||||||
| 23 | 0 | my $self = shift; | |||||||||||||||||||
| 24 | 0 | ▶ | die("lookup_all_tables method not defined in ". (ref($self) || $self)); | ||||||||||||||||||
|
Condition0%0/3
ref $self || $self
MC/DC0%0/2
ref $self || $self
ref $self
$self
Truth table0%0/3
A || B
A= ref $selfB= $self
| |||||||||||||||||||||
| 25 | } | ||||||||||||||||||||
| 26 | |||||||||||||||||||||
| 27 | |||||||||||||||||||||
| 28 | 0 | ▶ | sub find_all_tables { | ||||||||||||||||||
|
Subroutine
find_all_tables: not called CC=3 SCAR=24.8
Pod
documented
| |||||||||||||||||||||
| 29 | 0 | my $self = shift; | |||||||||||||||||||
| 30 | |||||||||||||||||||||
| 31 | 0 | ▶ | unless( $self->_looked_up_all_tables ) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 32 | 0 | foreach my $table ($self->lookup_all_tables) { | |||||||||||||||||||
| 33 | 0 | $self->_cache_table($table); | |||||||||||||||||||
| 34 | } | ||||||||||||||||||||
| 35 | 0 | $self->_looked_up_all_tables(1); | |||||||||||||||||||
| 36 | } | ||||||||||||||||||||
| 37 | 0 | return $self->_get_all_cached_tables; | |||||||||||||||||||
| 38 | } | ||||||||||||||||||||
| 39 | |||||||||||||||||||||
| 40 | 0 | ▶ | sub _get_all_cached_tables { | ||||||||||||||||||
|
Subroutine
_get_all_cached_tables: not called CC=1 SCAR=6.9
| |||||||||||||||||||||
| 41 | 0 | my $self = shift; | |||||||||||||||||||
| 42 | 0 | my @tables = values %{$self->{_table_cache}}; | |||||||||||||||||||
| 43 | 0 | return @tables; | |||||||||||||||||||
| 44 | } | ||||||||||||||||||||
| 45 | |||||||||||||||||||||
| 46 | 0 | ▶ | sub _looked_up_all_tables { | ||||||||||||||||||
|
Subroutine
_looked_up_all_tables: not called CC=2 SCAR=17.9
| |||||||||||||||||||||
| 47 | 0 | my $self = shift; | |||||||||||||||||||
| 48 | 0 | ▶ | if(@_) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 49 | 0 | $self->{_looked_up_all_tables} = shift; | |||||||||||||||||||
| 50 | } | ||||||||||||||||||||
| 51 | 0 | return $self->{_looked_up_all_tables}; | |||||||||||||||||||
| 52 | } | ||||||||||||||||||||
| 53 | |||||||||||||||||||||
| 54 | 0 | ▶ | sub find_table { | ||||||||||||||||||
|
Subroutine
find_table: not called CC=3 SCAR=24.8
Pod
documented
| |||||||||||||||||||||
| 55 | 0 | my $self = shift; | |||||||||||||||||||
| 56 | 0 | my $table_name = shift; | |||||||||||||||||||
| 57 | |||||||||||||||||||||
| 58 | 0 | ▶ | unless(defined $self->_cached_table($table_name)) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 59 | 0 | my $table = $self->lookup_table($table_name); | |||||||||||||||||||
| 60 | 0 | ▶ | $self->_cache_table($table) if( defined $table ); | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 61 | } | ||||||||||||||||||||
| 62 | 0 | return $self->_cached_table($table_name); | |||||||||||||||||||
| 63 | } | ||||||||||||||||||||
| 64 | |||||||||||||||||||||
| 65 | 1 | ▶ | sub register_drivers { | ||||||||||||||||||
|
Subroutine
register_drivers: called CC=4 SCAR=13.9
Pod
undocumented
| |||||||||||||||||||||
| 66 | 1 | my $class = shift; | |||||||||||||||||||
| 67 | |||||||||||||||||||||
| 68 | 1 | my $driver_filename = $INC{'DB/Introspector.pm'}; | |||||||||||||||||||
| 69 | 1 | $driver_filename =~ s@.pm$@/@g; | |||||||||||||||||||
| 70 | 1 | $driver_filename .= DRIVER_PROPERTIES_FILE; | |||||||||||||||||||
| 71 | |||||||||||||||||||||
| 72 | 1 | ▶ | my $properties_fh = new IO::File("<$driver_filename") | ||||||||||||||||||
|
Condition50%1/2
'IO::File'->new("<$driver_filename") || die("Can't open $driver_filename")
MC/DC0%0/1
'IO::File'->new("<$driver_filename") || die("Can't open $driver_filename")
'IO::File'->new("<$driver_filename")
Truth table50%1/2
A
A= 'IO::File'->new("<$driver_filename")
| |||||||||||||||||||||
| 73 | || die("Can't open $driver_filename"); | ||||||||||||||||||||
| 74 | |||||||||||||||||||||
| 75 | 1 | my $properties = new Config::Properties(); | |||||||||||||||||||
| 76 | 1 | $properties->load($properties_fh); | |||||||||||||||||||
| 77 | |||||||||||||||||||||
| 78 | 1 | foreach my $name ($properties->propertyNames) { | |||||||||||||||||||
| 79 | 3 | my $class_name = $properties->getProperty($name); | |||||||||||||||||||
| 80 | 1 | ▶ | eval("use $class_name;"); | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
Subroutine
BEGIN: called CC=1 SCAR=0.0
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 81 | 3 | ▶ | if($@) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 82 | 0 | warn("Could not register $class_name because: \n $@"); | |||||||||||||||||||
| 83 | } | ||||||||||||||||||||
| 84 | 3 | $class->register_introspector_class( $name, $class_name ); | |||||||||||||||||||
| 85 | } | ||||||||||||||||||||
| 86 | } | ||||||||||||||||||||
| 87 | |||||||||||||||||||||
| 88 | 0 | ▶ | sub get_instance { | ||||||||||||||||||
|
Subroutine
get_instance: not called CC=3 SCAR=24.8
Pod
documented
| |||||||||||||||||||||
| 89 | 0 | my $class = shift; | |||||||||||||||||||
| 90 | 0 | my $dbh = shift; | |||||||||||||||||||
| 91 | |||||||||||||||||||||
| 92 | 0 | ▶ | die("$class->new requires a dbh.") unless( UNIVERSAL::isa($dbh, 'DBI::db')); | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 93 | |||||||||||||||||||||
| 94 | 0 | my $driver_name = $dbh->{Driver}->{Name}; | |||||||||||||||||||
| 95 | |||||||||||||||||||||
| 96 | |||||||||||||||||||||
| 97 | 0 | ▶ | my $introspector_class = $class->_lookup_introspector_class($driver_name) | ||||||||||||||||||
|
Condition0%0/2
$class->_lookup_introspector_class($driver_name) || die("no introspector found for driver: $driver_name")
MC/DC0%0/1
$class->_lookup_introspector_class($driver_name) || die("no introspector found for driver: $driver_name")
$class->_lookup_introspector_class($driver_name)
Truth table0%0/2
A
A= $class->_lookup_introspector_class($driver_name)
| |||||||||||||||||||||
| 98 | || die("no introspector found for driver: $driver_name"); | ||||||||||||||||||||
| 99 | |||||||||||||||||||||
| 100 | 0 | return $introspector_class->new($dbh); | |||||||||||||||||||
| 101 | } | ||||||||||||||||||||
| 102 | |||||||||||||||||||||
| 103 | 0 | ▶ | sub _lookup_introspector_class { | ||||||||||||||||||
|
Subroutine
_lookup_introspector_class: not called CC=1 SCAR=6.9
| |||||||||||||||||||||
| 104 | 0 | my $class = shift; | |||||||||||||||||||
| 105 | 0 | my $driver_name = lc(shift); | |||||||||||||||||||
| 106 | |||||||||||||||||||||
| 107 | 0 | $REGISTRY{$driver_name}; | |||||||||||||||||||
| 108 | } | ||||||||||||||||||||
| 109 | |||||||||||||||||||||
| 110 | 3 | ▶ | sub register_introspector_class { | ||||||||||||||||||
|
Subroutine
register_introspector_class: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||
| 111 | 3 | my $class = shift; | |||||||||||||||||||
| 112 | 3 | my $driver_name = lc(shift); | |||||||||||||||||||
| 113 | 3 | my $introspector_class = shift; | |||||||||||||||||||
| 114 | |||||||||||||||||||||
| 115 | 3 | $REGISTRY{$driver_name} = $introspector_class; | |||||||||||||||||||
| 116 | } | ||||||||||||||||||||
| 117 | |||||||||||||||||||||
| 118 | 1 | ▶ | sub registered_drivers { | ||||||||||||||||||
|
Subroutine
registered_drivers: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||
| 119 | 1 | return keys %REGISTRY; | |||||||||||||||||||
| 120 | } | ||||||||||||||||||||
| 121 | |||||||||||||||||||||
| 122 | |||||||||||||||||||||
| 123 | |||||||||||||||||||||
| 124 | |||||||||||||||||||||
| 125 | 0 | ▶ | sub new { | ||||||||||||||||||
|
Subroutine
new: not called CC=3 SCAR=24.8
Pod
undocumented
| |||||||||||||||||||||
| 126 | 0 | my $class = shift; | |||||||||||||||||||
| 127 | 0 | my $dbh = shift; | |||||||||||||||||||
| 128 | |||||||||||||||||||||
| 129 | 0 | ▶ | unless( UNIVERSAL::isa($dbh, 'DBI::db') ) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 130 | 0 | die("$class->new requires a dbh."); | |||||||||||||||||||
| 131 | } | ||||||||||||||||||||
| 132 | |||||||||||||||||||||
| 133 | 0 | ▶ | my $self = bless({ | ||||||||||||||||||
|
Condition0%0/3
ref $class || $class
MC/DC0%0/2
ref $class || $class
ref $class
$class
Truth table0%0/3
A || B
A= ref $classB= $class
| |||||||||||||||||||||
| 134 | _dbh => $dbh | ||||||||||||||||||||
| 135 | }, ref($class) || $class); | ||||||||||||||||||||
| 136 | |||||||||||||||||||||
| 137 | 0 | $self->{_table_cache} = {}; | |||||||||||||||||||
| 138 | |||||||||||||||||||||
| 139 | 0 | $self; | |||||||||||||||||||
| 140 | } | ||||||||||||||||||||
| 141 | |||||||||||||||||||||
| 142 | 0 | ▶ | sub dbh { | ||||||||||||||||||
|
Subroutine
dbh: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||
| 143 | 0 | my $self = shift; | |||||||||||||||||||
| 144 | 0 | return $self->{_dbh}; | |||||||||||||||||||
| 145 | } | ||||||||||||||||||||
| 146 | |||||||||||||||||||||
| 147 | 0 | ▶ | sub _clear_dbh { | ||||||||||||||||||
|
Subroutine
_clear_dbh: not called CC=1 SCAR=6.9
| |||||||||||||||||||||
| 148 | 0 | my $self = shift; | |||||||||||||||||||
| 149 | 0 | delete $self->{_dbh}; | |||||||||||||||||||
| 150 | } | ||||||||||||||||||||
| 151 | |||||||||||||||||||||
| 152 | 0 | ▶ | sub _set_dbh { | ||||||||||||||||||
|
Subroutine
_set_dbh: not called CC=3 SCAR=24.8
| |||||||||||||||||||||
| 153 | 0 | my $self = shift; | |||||||||||||||||||
| 154 | 0 | ▶ | if( @_ ) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 155 | 0 | my $dbh = shift; | |||||||||||||||||||
| 156 | 0 | ▶ | unless( UNIVERSAL::isa($dbh, 'DBI::db') ) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 157 | 0 | die("$dbh is not a DBI::db"); | |||||||||||||||||||
| 158 | } | ||||||||||||||||||||
| 159 | 0 | $self->{_dbh} = $dbh; | |||||||||||||||||||
| 160 | } | ||||||||||||||||||||
| 161 | |||||||||||||||||||||
| 162 | } | ||||||||||||||||||||
| 163 | |||||||||||||||||||||
| 164 | 0 | ▶ | sub _cached_table { | ||||||||||||||||||
|
Subroutine
_cached_table: not called CC=1 SCAR=6.9
| |||||||||||||||||||||
| 165 | 0 | my $self = shift; | |||||||||||||||||||
| 166 | 0 | my $table_name = shift; | |||||||||||||||||||
| 167 | 0 | return $self->{_table_cache}{$table_name}; | |||||||||||||||||||
| 168 | } | ||||||||||||||||||||
| 169 | |||||||||||||||||||||
| 170 | 1 | ▶ | use Carp qw( cluck ); | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 171 | 0 | ▶ | sub _cache_table { | ||||||||||||||||||
|
Subroutine
_cache_table: not called CC=3 SCAR=24.8
| |||||||||||||||||||||
| 172 | 0 | my $self = shift; | |||||||||||||||||||
| 173 | |||||||||||||||||||||
| 174 | # TODO: Add some type checking here | ||||||||||||||||||||
| 175 | 0 | ▶ | if(@_) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 176 | 0 | my $table = shift; | |||||||||||||||||||
| 177 | 0 | ▶ | cluck($table) unless ref $table; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 178 | 0 | $self->{_table_cache}{$table->name} = $table; | |||||||||||||||||||
| 179 | } | ||||||||||||||||||||
| 180 | } | ||||||||||||||||||||
| 181 | |||||||||||||||||||||
| 182 | 1 | 1; | |||||||||||||||||||
| 183 | __END__ | ||||||||||||||||||||