| 1 | package DBIx::Connect::FromConfig; | ||||||||||||||||||||||
| 2 | 2 | ▶ | use strict; | ||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||
| 3 | 2 | ▶ | use warnings; | ||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||
| 4 | 2 | ▶ | use Carp; | ||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||
| 5 | 2 | ▶ | use DBI (); | ||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||
| 6 | |||||||||||||||||||||||
| 7 | |||||||||||||||||||||||
| 8 | { | ||||||||||||||||||||||
| 9 | 2 | ▶ | no strict; | ||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||
| 10 | 2 | $VERSION = '0.07'; | |||||||||||||||||||||
| 11 | } | ||||||||||||||||||||||
| 12 | |||||||||||||||||||||||
| 13 | |||||||||||||||||||||||
| 14 | # | ||||||||||||||||||||||
| 15 | # import() | ||||||||||||||||||||||
| 16 | # ------ | ||||||||||||||||||||||
| 17 | 2 | ▶ | sub import { | ||||||||||||||||||||
|
Subroutine
import: called CC=2 SCAR=6.9
| |||||||||||||||||||||||
| 18 | 2 | ▶ | if (grep { /^-in_dbi$/i } @_) { | ||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||
| 19 | 1 | *DBI::connect_from_config = \&connect | |||||||||||||||||||||
| 20 | } | ||||||||||||||||||||||
| 21 | } | ||||||||||||||||||||||
| 22 | |||||||||||||||||||||||
| 23 | |||||||||||||||||||||||
| 24 | # | ||||||||||||||||||||||
| 25 | # connect() | ||||||||||||||||||||||
| 26 | # ------- | ||||||||||||||||||||||
| 27 | 0 | ▶ | sub connect { | ||||||||||||||||||||
|
Subroutine
connect: not called CC=26 SCAR=65.5
Pod
documented
| |||||||||||||||||||||||
| 28 | 0 | my ($class, @args) = @_; | |||||||||||||||||||||
| 29 | 0 | ▶ | croak "error: No parameter given" unless @args; | ||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||
| 30 | 0 | ▶ | croak "error: Odd number of arguments" if @args % 2 != 0; | ||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||
| 31 | |||||||||||||||||||||||
| 32 | 0 | my %args = @args; | |||||||||||||||||||||
| 33 | 0 | my @params = qw< | |||||||||||||||||||||
| 34 | driver host port database options username password attributes | ||||||||||||||||||||||
| 35 | >; | ||||||||||||||||||||||
| 36 | |||||||||||||||||||||||
| 37 | 0 | my %db = (); | |||||||||||||||||||||
| 38 | 0 | my %db_param_name = ( | |||||||||||||||||||||
| 39 | CSV => 'f_dir', | ||||||||||||||||||||||
| 40 | DB2 => 'DATABASE', | ||||||||||||||||||||||
| 41 | Excel => 'file', | ||||||||||||||||||||||
| 42 | InterBase => 'database', | ||||||||||||||||||||||
| 43 | Mock => 'dbname', | ||||||||||||||||||||||
| 44 | mysql => 'database', | ||||||||||||||||||||||
| 45 | mysqlPP => 'database', | ||||||||||||||||||||||
| 46 | Oracle => 'sid', | ||||||||||||||||||||||
| 47 | Pg => 'dbname', | ||||||||||||||||||||||
| 48 | PgLite => 'dbname', | ||||||||||||||||||||||
| 49 | PgPP => 'dbname', | ||||||||||||||||||||||
| 50 | SQLite => 'dbname', | ||||||||||||||||||||||
| 51 | SQLite2 => 'dbname', | ||||||||||||||||||||||
| 52 | Sybase => 'database', | ||||||||||||||||||||||
| 53 | ); | ||||||||||||||||||||||
| 54 | |||||||||||||||||||||||
| 55 | 0 | ▶ | my $section_name = $args{section} || 'database'; | ||||||||||||||||||||
|
Condition0%0/2
$args{'section'} || 'database'
MC/DC0%0/1
$args{'section'} || 'database'
$args{'section'}
Truth table0%0/2
A
A= $args{'section'}
| |||||||||||||||||||||||
| 56 | |||||||||||||||||||||||
| 57 | 0 | my $config = $args{config}; | |||||||||||||||||||||
| 58 | |||||||||||||||||||||||
| 59 | # configuration in a Config::IniFiles object | ||||||||||||||||||||||
| 60 | 0 | ▶ | if (eval { $config->isa('Config::IniFiles') }) { | ||||||||||||||||||||
|
Branch: 4 branches
| |||||||||||||||||||||||
| 61 | 0 | for my $param (@params) { | |||||||||||||||||||||
| 62 | 0 | $db{$param} = $config->val($section_name => $param) | |||||||||||||||||||||
| 63 | } | ||||||||||||||||||||||
| 64 | } | ||||||||||||||||||||||
| 65 | # configuration in a Config::Simple object | ||||||||||||||||||||||
| 66 | 0 | elsif (eval { $config->isa('Config::Simple') }) { | |||||||||||||||||||||
| 67 | 0 | my $block = $config->get_block($section_name); | |||||||||||||||||||||
| 68 | |||||||||||||||||||||||
| 69 | 0 | for my $param (@params) { | |||||||||||||||||||||
| 70 | 0 | $db{$param} = $block->{$param}; | |||||||||||||||||||||
| 71 | } | ||||||||||||||||||||||
| 72 | } | ||||||||||||||||||||||
| 73 | # configuration in a Config::Tiny object | ||||||||||||||||||||||
| 74 | 0 | elsif (eval { $config->isa('Config::Tiny') }) { | |||||||||||||||||||||
| 75 | 0 | for my $param (@params) { | |||||||||||||||||||||
| 76 | 0 | $db{$param} = $config->{$section_name}{$param}; | |||||||||||||||||||||
| 77 | } | ||||||||||||||||||||||
| 78 | } | ||||||||||||||||||||||
| 79 | # configuration in a hashref | ||||||||||||||||||||||
| 80 | elsif (ref $config eq 'HASH') { | ||||||||||||||||||||||
| 81 | 0 | for my $param (@params) { | |||||||||||||||||||||
| 82 | 0 | $db{$param} = $config->{$param} | |||||||||||||||||||||
| 83 | } | ||||||||||||||||||||||
| 84 | } | ||||||||||||||||||||||
| 85 | else { | ||||||||||||||||||||||
| 86 | 0 | croak "error: Unknown type of configuration" | |||||||||||||||||||||
| 87 | } | ||||||||||||||||||||||
| 88 | |||||||||||||||||||||||
| 89 | # check mandatory values | ||||||||||||||||||||||
| 90 | 0 | ▶ | $db{driver} or croak "error: Database driver not specified"; | ||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||
| 91 | exists $db_param_name{$db{driver}} | ||||||||||||||||||||||
| 92 | 0 | ▶ | or croak "error: Database driver \Q$db{driver}\E not supported"; | ||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||
| 93 | |||||||||||||||||||||||
| 94 | # default values | ||||||||||||||||||||||
| 95 | 0 | ▶ | $db{database} = "" unless defined $db{database}; | ||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||
| 96 | 0 | ▶ | $db{host} = "" unless defined $db{host}; | ||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||
| 97 | 0 | ▶ | $db{port} = "" unless defined $db{port}; | ||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||
| 98 | 0 | ▶ | $db{options} = "" unless defined $db{options}; | ||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||
| 99 | 0 | ▶ | $db{username} = "" unless defined $db{username}; | ||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||
| 100 | 0 | ▶ | $db{password} = "" unless defined $db{password}; | ||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||
| 101 | 0 | ▶ | $db{attributes} ||= {}; | ||||||||||||||||||||
|
Condition0%0/2
$db{"attributes"} ||= {}
MC/DC0%0/1
$db{"attributes"} ||= {}
$db{"attributes"}
Truth table0%0/2
A
A= $db{"attributes"}
| |||||||||||||||||||||||
| 102 | |||||||||||||||||||||||
| 103 | # handle DBI attributes | ||||||||||||||||||||||
| 104 | 0 | ▶ | if (ref $db{attributes}) { | ||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||
| 105 | croak "error: DBI attributes must be given as a hashref or a string" | ||||||||||||||||||||||
| 106 | 0 | ▶ | unless ref $db{attributes} eq "HASH"; | ||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||
| 107 | } | ||||||||||||||||||||||
| 108 | else { | ||||||||||||||||||||||
| 109 | # copied from DBI::parse_dsn() | ||||||||||||||||||||||
| 110 | 0 | $db{attributes} = { split /\s*=>?\s*|\s*,\s*/, $db{attributes}, -1 }; | |||||||||||||||||||||
| 111 | } | ||||||||||||||||||||||
| 112 | |||||||||||||||||||||||
| 113 | # construct the DSN | ||||||||||||||||||||||
| 114 | my $dsn = sprintf "dbi:$db{driver}:%s%s%s=%s%s", | ||||||||||||||||||||||
| 115 | ( $db{host} ? "host=$db{host};" : '' ), | ||||||||||||||||||||||
| 116 | ( $db{port} ? "port=$db{port};" : '' ), | ||||||||||||||||||||||
| 117 | $db_param_name{$db{driver}}, $db{database}, | ||||||||||||||||||||||
| 118 | 0 | ▶ | ( $db{options} ? ";$db{options}" : '' ); | ||||||||||||||||||||
|
Branch: 3 branches
| |||||||||||||||||||||||
| 119 | |||||||||||||||||||||||
| 120 | 0 | my $dbh = DBI->connect($dsn, $db{username}, $db{password}, $db{attributes}); | |||||||||||||||||||||
| 121 | |||||||||||||||||||||||
| 122 | 0 | return $dbh | |||||||||||||||||||||
| 123 | } | ||||||||||||||||||||||
| 124 | |||||||||||||||||||||||
| 125 | |||||||||||||||||||||||
| 126 | 2 | 1; # End of DBIx::Connect::FromConfig | |||||||||||||||||||||
| 127 | |||||||||||||||||||||||
| 128 | |||||||||||||||||||||||
| 129 | __END__ | ||||||||||||||||||||||