| 1 | package Config::DB; | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | 3 | $Config::DB::VERSION = '0.2'; | |||||||||||||||||||||||||||
| 4 | |||||||||||||||||||||||||||||
| 5 | 3 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | 3 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||
| 8 | 3 | ▶ | use Carp; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 9 | 3 | ▶ | use Config::DB::Record; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 10 | 3 | ▶ | use Config::DB::Table; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 11 | 3 | ▶ | use DBI; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||
| 13 | 13 | ▶ | sub new { | ||||||||||||||||||||||||||
|
Subroutine
new: called CC=10 SCAR=23.0
Pod
documented
| |||||||||||||||||||||||||||||
| 14 | 13 | my ( $class, %pars ) = @_; | |||||||||||||||||||||||||||
| 15 | 13 | my $self = { %pars, read => 0, values => {} }; | |||||||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||||||
| 17 | 13 | ▶ | croak __PACKAGE__ . "::new: wrong call" if !defined $class || $class eq ''; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
not defined $class or $class eq ""
MC/DC100%2/2
not defined $class or $class eq ""
not defined $class
$class eq ""
Truth table100%3/3
not(A) or B
A= not defined $classB= $class eq ""
| |||||||||||||||||||||||||||||
| 18 | |||||||||||||||||||||||||||||
| 19 | 11 | eval { | |||||||||||||||||||||||||||
| 20 | 11 | ▶ | die | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 21 | if $class->check_Config_DB_hineritance ne | ||||||||||||||||||||||||||||
| 22 | 'check_Config_DB_hineritance'; | ||||||||||||||||||||||||||||
| 23 | }; | ||||||||||||||||||||||||||||
| 24 | 11 | ▶ | croak __PACKAGE__ . "::new: '$class' does not hinerit 'Config::DB'" | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 25 | if $@ ne ''; | ||||||||||||||||||||||||||||
| 26 | |||||||||||||||||||||||||||||
| 27 | croak __PACKAGE__ . "::new: missing 'connect' paramenter" | ||||||||||||||||||||||||||||
| 28 | 9 | ▶ | if !defined $self->{connect}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 29 | croak __PACKAGE__ | ||||||||||||||||||||||||||||
| 30 | . "::new: 'connect' paramenter is not a reference to ARRAY" | ||||||||||||||||||||||||||||
| 31 | 8 | ▶ | if ref $self->{connect} ne 'ARRAY'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 32 | croak __PACKAGE__ . "::new: missing 'tables' paramenter" | ||||||||||||||||||||||||||||
| 33 | 7 | ▶ | if !defined $self->{tables}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 34 | croak __PACKAGE__ . "::new: 'tables' paramenter is not a reference to HASH" | ||||||||||||||||||||||||||||
| 35 | 6 | ▶ | if ref $self->{tables} ne 'HASH'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 36 | croak __PACKAGE__ . "::new: no tables defined" | ||||||||||||||||||||||||||||
| 37 | 5 | ▶ | if 0 == scalar keys %{ $self->{tables} }; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 38 | |||||||||||||||||||||||||||||
| 39 | 4 | return bless $self, $class; | |||||||||||||||||||||||||||
| 40 | } | ||||||||||||||||||||||||||||
| 41 | |||||||||||||||||||||||||||||
| 42 | 3 | our $AUTOLOAD; | |||||||||||||||||||||||||||
| 43 | |||||||||||||||||||||||||||||
| 44 | 0 | ▶ | sub AUTOLOAD { | ||||||||||||||||||||||||||
|
Subroutine
AUTOLOAD: not called CC=2 SCAR=17.9
| |||||||||||||||||||||||||||||
| 45 | 0 | my ( $self, @pars ) = @_; | |||||||||||||||||||||||||||
| 46 | 0 | my $name = $AUTOLOAD; | |||||||||||||||||||||||||||
| 47 | |||||||||||||||||||||||||||||
| 48 | 0 | $name =~ s/.*://; | |||||||||||||||||||||||||||
| 49 | |||||||||||||||||||||||||||||
| 50 | 0 | ▶ | croak "Can't locate object method \"$name\" via package \"" | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 51 | . __PACKAGE__ . '"' | ||||||||||||||||||||||||||||
| 52 | unless $name =~ /^_/; | ||||||||||||||||||||||||||||
| 53 | |||||||||||||||||||||||||||||
| 54 | 0 | $name =~ s/^_//; | |||||||||||||||||||||||||||
| 55 | |||||||||||||||||||||||||||||
| 56 | 0 | return $self->get( $name, @pars ); | |||||||||||||||||||||||||||
| 57 | } | ||||||||||||||||||||||||||||
| 58 | |||||||||||||||||||||||||||||
| 59 | 0 | ▶ | sub DESTROY { | ||||||||||||||||||||||||||
|
Subroutine
DESTROY: not called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 60 | } | ||||||||||||||||||||||||||||
| 61 | |||||||||||||||||||||||||||||
| 62 | 9 | ▶ | sub check_Config_DB_hineritance { | ||||||||||||||||||||||||||
|
Subroutine
check_Config_DB_hineritance: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 63 | 9 | return 'check_Config_DB_hineritance'; | |||||||||||||||||||||||||||
| 64 | } | ||||||||||||||||||||||||||||
| 65 | |||||||||||||||||||||||||||||
| 66 | 2 | ▶ | sub get { | ||||||||||||||||||||||||||
|
Subroutine
get: called CC=8 SCAR=39.1
Pod
documented
| |||||||||||||||||||||||||||||
| 67 | 2 | my ( $self, $table, $key, $field ) = @_; | |||||||||||||||||||||||||||
| 68 | |||||||||||||||||||||||||||||
| 69 | 2 | ▶ | $self->read unless $self->{read}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 70 | |||||||||||||||||||||||||||||
| 71 | 0 | ▶ | croak __PACKAGE__ . "::get: missing table parameter" unless defined $table; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 72 | croak __PACKAGE__ . "::get: unknown configuration table '$table'" | ||||||||||||||||||||||||||||
| 73 | 0 | ▶ | unless exists $self->{values}->{$table}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 74 | |||||||||||||||||||||||||||||
| 75 | 0 | ▶ | return $self->{values}->{$table} unless defined $key; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 76 | |||||||||||||||||||||||||||||
| 77 | croak __PACKAGE__ | ||||||||||||||||||||||||||||
| 78 | . "::get: missing key '$key' in configuration table '$table'" | ||||||||||||||||||||||||||||
| 79 | 0 | ▶ | unless exists $self->{values}->{$table}->{$key}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 80 | |||||||||||||||||||||||||||||
| 81 | 0 | ▶ | return $self->{values}->{$table}->{$key} unless defined $field; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 82 | |||||||||||||||||||||||||||||
| 83 | croak __PACKAGE__ | ||||||||||||||||||||||||||||
| 84 | . "::get: unknown field '$field' for configuration table '$table'" | ||||||||||||||||||||||||||||
| 85 | 0 | ▶ | unless exists $self->{values}->{$table}->{$key}->{$field}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 86 | |||||||||||||||||||||||||||||
| 87 | 0 | return $self->{values}->{$table}->{$key}->{$field}; | |||||||||||||||||||||||||||
| 88 | } | ||||||||||||||||||||||||||||
| 89 | |||||||||||||||||||||||||||||
| 90 | 7 | ▶ | sub read { | ||||||||||||||||||||||||||
|
Subroutine
read: called CC=6 SCAR=18.7
Pod
documented
| |||||||||||||||||||||||||||||
| 91 | 7 | my ($self) = @_; | |||||||||||||||||||||||||||
| 92 | 7 | my @connect = @{ $self->{connect} }; | |||||||||||||||||||||||||||
| 93 | 7 | ▶ | my %attr = %{ $connect[3] || {} }; | ||||||||||||||||||||||||||
|
Condition100%2/2
$connect[3] || {}
MC/DC100%1/1
$connect[3] || {}
$connect[3]
Truth table100%2/2
A
A= $connect[3]
| |||||||||||||||||||||||||||||
| 94 | 7 | my $dbh; | |||||||||||||||||||||||||||
| 95 | |||||||||||||||||||||||||||||
| 96 | 7 | $attr{PrintError} = 0; | |||||||||||||||||||||||||||
| 97 | 7 | $attr{RaiseError} = 1; | |||||||||||||||||||||||||||
| 98 | 7 | $connect[3] = \%attr; | |||||||||||||||||||||||||||
| 99 | |||||||||||||||||||||||||||||
| 100 | 7 | eval { $dbh = DBI->connect(@connect); }; | |||||||||||||||||||||||||||
| 101 | 7 | ▶ | croak "$@\n" . __PACKAGE__ . "::read: can't connect" if $@; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 102 | |||||||||||||||||||||||||||||
| 103 | 6 | foreach my $table ( keys %{ $self->{tables} } ) { | |||||||||||||||||||||||||||
| 104 | 6 | my $t; | |||||||||||||||||||||||||||
| 105 | |||||||||||||||||||||||||||||
| 106 | 6 | eval { | |||||||||||||||||||||||||||
| 107 | 6 | $t = $dbh->selectall_hashref( | |||||||||||||||||||||||||||
| 108 | "SELECT $self->{tables}->{$table} AS dbcfg_key, $table.* FROM $table", | ||||||||||||||||||||||||||||
| 109 | 'dbcfg_key' | ||||||||||||||||||||||||||||
| 110 | ); | ||||||||||||||||||||||||||||
| 111 | }; | ||||||||||||||||||||||||||||
| 112 | 6 | ▶ | croak "$@\n" . __PACKAGE__ . "::read: reading '$table' table" if $@; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 113 | 0 | $self->{values}->{$table} = $t; | |||||||||||||||||||||||||||
| 114 | |||||||||||||||||||||||||||||
| 115 | 0 | foreach my $key ( keys %$t ) { | |||||||||||||||||||||||||||
| 116 | 0 | delete $t->{$key}->{dbcfg_key}; | |||||||||||||||||||||||||||
| 117 | 0 | bless $t->{$key}, 'Config::DB::Record'; | |||||||||||||||||||||||||||
| 118 | } | ||||||||||||||||||||||||||||
| 119 | |||||||||||||||||||||||||||||
| 120 | 0 | bless $t, 'Config::DB::Table'; | |||||||||||||||||||||||||||
| 121 | } | ||||||||||||||||||||||||||||
| 122 | |||||||||||||||||||||||||||||
| 123 | 0 | $self->{read} = 1; | |||||||||||||||||||||||||||
| 124 | } | ||||||||||||||||||||||||||||
| 125 | |||||||||||||||||||||||||||||
| 126 | 3 | 1; | |||||||||||||||||||||||||||
| 127 | |||||||||||||||||||||||||||||
| 128 | __END__ | ||||||||||||||||||||||||||||