line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package DBIx::Connect::FromConfig; |
2
|
1
|
|
|
1
|
|
938
|
use strict; |
|
1
|
|
|
|
|
3
|
|
|
1
|
|
|
|
|
39
|
|
3
|
1
|
|
|
1
|
|
6
|
use warnings; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
32
|
|
4
|
1
|
|
|
1
|
|
16
|
use Carp; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
88
|
|
5
|
1
|
|
|
1
|
|
2342
|
use DBI (); |
|
1
|
|
|
|
|
18529
|
|
|
1
|
|
|
|
|
41
|
|
6
|
|
|
|
|
|
|
|
7
|
|
|
|
|
|
|
|
8
|
|
|
|
|
|
|
{ |
9
|
1
|
|
|
1
|
|
13
|
no strict; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
786
|
|
10
|
|
|
|
|
|
|
$VERSION = '0.07'; |
11
|
|
|
|
|
|
|
} |
12
|
|
|
|
|
|
|
|
13
|
|
|
|
|
|
|
|
14
|
|
|
|
|
|
|
# |
15
|
|
|
|
|
|
|
# import() |
16
|
|
|
|
|
|
|
# ------ |
17
|
|
|
|
|
|
|
sub import { |
18
|
2
|
100
|
|
2
|
|
1742
|
if (grep { /^-in_dbi$/i } @_) { |
|
3
|
|
|
|
|
29
|
|
19
|
1
|
|
|
|
|
17
|
*DBI::connect_from_config = \&connect |
20
|
|
|
|
|
|
|
} |
21
|
|
|
|
|
|
|
} |
22
|
|
|
|
|
|
|
|
23
|
|
|
|
|
|
|
|
24
|
|
|
|
|
|
|
# |
25
|
|
|
|
|
|
|
# connect() |
26
|
|
|
|
|
|
|
# ------- |
27
|
|
|
|
|
|
|
sub connect { |
28
|
0
|
|
|
0
|
1
|
|
my ($class, @args) = @_; |
29
|
0
|
0
|
|
|
|
|
croak "error: No parameter given" unless @args; |
30
|
0
|
0
|
|
|
|
|
croak "error: Odd number of arguments" if @args % 2 != 0; |
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
|
|
0
|
|
|
|
my $section_name = $args{section} || 'database'; |
56
|
|
|
|
|
|
|
|
57
|
0
|
|
|
|
|
|
my $config = $args{config}; |
58
|
|
|
|
|
|
|
|
59
|
|
|
|
|
|
|
# configuration in a Config::IniFiles object |
60
|
0
|
0
|
|
|
|
|
if (eval { $config->isa('Config::IniFiles') }) { |
|
0
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
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
|
0
|
|
|
|
|
$db{driver} or croak "error: Database driver not specified"; |
91
|
0
|
0
|
|
|
|
|
exists $db_param_name{$db{driver}} |
92
|
|
|
|
|
|
|
or croak "error: Database driver \Q$db{driver}\E not supported"; |
93
|
|
|
|
|
|
|
|
94
|
|
|
|
|
|
|
# default values |
95
|
0
|
0
|
|
|
|
|
$db{database} = "" unless defined $db{database}; |
96
|
0
|
0
|
|
|
|
|
$db{host} = "" unless defined $db{host}; |
97
|
0
|
0
|
|
|
|
|
$db{port} = "" unless defined $db{port}; |
98
|
0
|
0
|
|
|
|
|
$db{options} = "" unless defined $db{options}; |
99
|
0
|
0
|
|
|
|
|
$db{username} = "" unless defined $db{username}; |
100
|
0
|
0
|
|
|
|
|
$db{password} = "" unless defined $db{password}; |
101
|
0
|
|
0
|
|
|
|
$db{attributes} ||= {}; |
102
|
|
|
|
|
|
|
|
103
|
|
|
|
|
|
|
# handle DBI attributes |
104
|
0
|
0
|
|
|
|
|
if (ref $db{attributes}) { |
105
|
0
|
0
|
|
|
|
|
croak "error: DBI attributes must be given as a hashref or a string" |
106
|
|
|
|
|
|
|
unless ref $db{attributes} eq "HASH"; |
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
|
0
|
0
|
|
|
|
|
my $dsn = sprintf "dbi:$db{driver}:%s%s%s=%s%s", |
|
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
115
|
|
|
|
|
|
|
( $db{host} ? "host=$db{host};" : '' ), |
116
|
|
|
|
|
|
|
( $db{port} ? "port=$db{port};" : '' ), |
117
|
|
|
|
|
|
|
$db_param_name{$db{driver}}, $db{database}, |
118
|
|
|
|
|
|
|
( $db{options} ? ";$db{options}" : '' ); |
119
|
|
|
|
|
|
|
|
120
|
0
|
|
|
|
|
|
my $dbh = DBI->connect($dsn, $db{username}, $db{password}, $db{attributes}); |
121
|
|
|
|
|
|
|
|
122
|
0
|
|
|
|
|
|
return $dbh |
123
|
|
|
|
|
|
|
} |
124
|
|
|
|
|
|
|
|
125
|
|
|
|
|
|
|
|
126
|
|
|
|
|
|
|
1; # End of DBIx::Connect::FromConfig |
127
|
|
|
|
|
|
|
|
128
|
|
|
|
|
|
|
|
129
|
|
|
|
|
|
|
__END__ |