line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
3
|
|
|
3
|
|
32110
|
use strict; |
|
3
|
|
|
|
|
6
|
|
|
3
|
|
|
|
|
116
|
|
2
|
3
|
|
|
3
|
|
18
|
use warnings; |
|
3
|
|
|
|
|
7
|
|
|
3
|
|
|
|
|
202
|
|
3
|
|
|
|
|
|
|
package Mojolicious::Plugin::Database; |
4
|
|
|
|
|
|
|
{ |
5
|
|
|
|
|
|
|
$Mojolicious::Plugin::Database::VERSION = '1.08'; |
6
|
|
|
|
|
|
|
} |
7
|
3
|
|
|
3
|
|
843
|
use Mojo::Base 'Mojolicious::Plugin'; |
|
3
|
|
|
|
|
28354
|
|
|
3
|
|
|
|
|
26
|
|
8
|
3
|
|
|
3
|
|
19918
|
use DBI; |
|
3
|
|
|
|
|
39810
|
|
|
3
|
|
|
|
|
1901
|
|
9
|
|
|
|
|
|
|
|
10
|
|
|
|
|
|
|
sub single { |
11
|
1
|
|
|
1
|
0
|
2
|
my $self = shift; |
12
|
1
|
|
|
|
|
2
|
my $app = shift; |
13
|
1
|
|
|
|
|
1
|
my $conf = shift; |
14
|
|
|
|
|
|
|
|
15
|
1
|
50
|
|
|
|
4
|
die ref($self), ': missing dsn parameter', "\n" unless($conf->{dsn}); |
16
|
|
|
|
|
|
|
|
17
|
1
|
|
|
1
|
|
4
|
my $dbh_connect = sub { DBI->connect($conf->{dsn}, $conf->{username}, $conf->{password}, $conf->{options}) }; |
|
1
|
|
|
|
|
53
|
|
18
|
|
|
|
|
|
|
|
19
|
1
|
|
|
|
|
9
|
$app->attr('dbh' => $dbh_connect); |
20
|
|
|
|
|
|
|
|
21
|
1
|
|
50
|
|
|
189
|
my $helper_name = $conf->{helper} || 'db'; |
22
|
1
|
|
|
2
|
|
10
|
$app->helper($helper_name => sub { return shift->app->dbh }); |
|
2
|
|
|
|
|
104054
|
|
23
|
|
|
|
|
|
|
} |
24
|
|
|
|
|
|
|
|
25
|
|
|
|
|
|
|
sub multi { |
26
|
1
|
|
|
1
|
0
|
2
|
my $self = shift; |
27
|
1
|
|
|
|
|
1
|
my $app = shift; |
28
|
1
|
|
|
|
|
2
|
my $conf = shift; |
29
|
|
|
|
|
|
|
|
30
|
|
|
|
|
|
|
# databases should be a hashref |
31
|
1
|
50
|
|
|
|
4
|
die ref($self), ': databases is not a hash reference', "\n" unless(ref($conf->{databases}) eq 'HASH'); |
32
|
|
|
|
|
|
|
|
33
|
1
|
|
|
|
|
2
|
foreach my $helper (keys(%{$conf->{databases}})) { |
|
1
|
|
|
|
|
6
|
|
34
|
2
|
|
|
|
|
113
|
my $dbconf = $conf->{databases}->{$helper}; |
35
|
2
|
50
|
|
|
|
8
|
die ref($self), ': missing dsn parameter for ' . $helper, "\n" unless(defined($dbconf->{dsn})); |
36
|
2
|
|
|
|
|
4
|
my $attr_name = '_dbh_' . $helper; |
37
|
|
|
|
|
|
|
$app->attr($attr_name => sub { |
38
|
2
|
|
|
2
|
|
101
|
DBI->connect($dbconf->{dsn}, $dbconf->{username}, $dbconf->{password}, $dbconf->{options}); |
39
|
2
|
|
|
|
|
17
|
}); |
40
|
2
|
|
|
4
|
|
366
|
$app->helper($helper => sub { return shift->app->$attr_name() }); |
|
4
|
|
|
|
|
203471
|
|
41
|
|
|
|
|
|
|
} |
42
|
|
|
|
|
|
|
} |
43
|
|
|
|
|
|
|
|
44
|
|
|
|
|
|
|
sub register { |
45
|
2
|
|
|
2
|
1
|
75
|
my $self = shift; |
46
|
2
|
|
|
|
|
4
|
my $app = shift; |
47
|
2
|
|
50
|
|
|
8
|
my $conf = shift || {}; |
48
|
|
|
|
|
|
|
|
49
|
2
|
100
|
|
|
|
9
|
if(defined($conf->{databases})) { |
50
|
1
|
|
|
|
|
2
|
$self->multi($app, $conf); |
51
|
|
|
|
|
|
|
} else { |
52
|
|
|
|
|
|
|
# old-style connect |
53
|
1
|
|
|
|
|
3
|
$self->single($app, $conf); |
54
|
|
|
|
|
|
|
} |
55
|
|
|
|
|
|
|
} |
56
|
|
|
|
|
|
|
|
57
|
|
|
|
|
|
|
1; |
58
|
|
|
|
|
|
|
__END__ |