line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
3
|
|
|
3
|
|
32139
|
use strict; |
|
3
|
|
|
|
|
8
|
|
|
3
|
|
|
|
|
114
|
|
2
|
3
|
|
|
3
|
|
20
|
use warnings; |
|
3
|
|
|
|
|
7
|
|
|
3
|
|
|
|
|
220
|
|
3
|
|
|
|
|
|
|
package Mojolicious::Plugin::Database; |
4
|
|
|
|
|
|
|
$Mojolicious::Plugin::Database::VERSION = '1.10'; |
5
|
3
|
|
|
3
|
|
1414
|
use Mojo::Base 'Mojolicious::Plugin'; |
|
3
|
|
|
|
|
10522
|
|
|
3
|
|
|
|
|
28
|
|
6
|
3
|
|
|
3
|
|
4081
|
use DBI; |
|
3
|
|
|
|
|
45025
|
|
|
3
|
|
|
|
|
1823
|
|
7
|
|
|
|
|
|
|
|
8
|
|
|
|
|
|
|
sub single { |
9
|
1
|
|
|
1
|
0
|
2
|
my $self = shift; |
10
|
1
|
|
|
|
|
2
|
my $app = shift; |
11
|
1
|
|
|
|
|
2
|
my $conf = shift; |
12
|
|
|
|
|
|
|
|
13
|
1
|
50
|
|
|
|
4
|
die ref($self), ': missing dsn parameter', "\n" unless($conf->{dsn}); |
14
|
|
|
|
|
|
|
|
15
|
|
|
|
|
|
|
|
16
|
|
|
|
|
|
|
|
17
|
|
|
|
|
|
|
my $dbh_connect = sub { |
18
|
1
|
|
|
1
|
|
24
|
my $dbh = DBI->connect($conf->{dsn}, $conf->{username}, $conf->{password}, $conf->{options}); |
19
|
1
|
50
|
|
|
|
1972
|
$conf->{on_connect}($dbh) if $conf->{on_connect}; |
20
|
1
|
|
|
|
|
16478
|
return $dbh; |
21
|
1
|
|
|
|
|
4
|
}; |
22
|
|
|
|
|
|
|
|
23
|
1
|
|
50
|
|
|
9
|
my $helper_name = $conf->{helper} || 'db'; |
24
|
|
|
|
|
|
|
|
25
|
1
|
|
|
|
|
11
|
$app->attr("_dbh_$helper_name" => $dbh_connect); |
26
|
|
|
|
|
|
|
|
27
|
|
|
|
|
|
|
$app->helper($helper_name => sub { |
28
|
3
|
|
|
3
|
|
197960
|
my $self = shift; |
29
|
3
|
|
|
|
|
9
|
my $attr = "_dbh_$helper_name"; |
30
|
3
|
|
|
|
|
11
|
return $self->app->$attr(); |
31
|
1
|
|
|
|
|
51
|
}); |
32
|
|
|
|
|
|
|
} |
33
|
|
|
|
|
|
|
|
34
|
|
|
|
|
|
|
sub multi { |
35
|
1
|
|
|
1
|
0
|
2
|
my $self = shift; |
36
|
1
|
|
|
|
|
1
|
my $app = shift; |
37
|
1
|
|
|
|
|
1
|
my $conf = shift; |
38
|
|
|
|
|
|
|
|
39
|
|
|
|
|
|
|
# databases should be a hashref |
40
|
1
|
50
|
|
|
|
4
|
die ref($self), ': databases is not a hash reference', "\n" unless(ref($conf->{databases}) eq 'HASH'); |
41
|
|
|
|
|
|
|
|
42
|
1
|
|
|
|
|
1
|
foreach my $helper (keys(%{$conf->{databases}})) { |
|
1
|
|
|
|
|
4
|
|
43
|
2
|
|
|
|
|
54
|
my $dbconf = $conf->{databases}->{$helper}; |
44
|
2
|
50
|
|
|
|
9
|
die ref($self), ': missing dsn parameter for ' . $helper, "\n" unless(defined($dbconf->{dsn})); |
45
|
2
|
|
|
|
|
6
|
my $attr_name = '_dbh_' . $helper; |
46
|
|
|
|
|
|
|
$app->attr($attr_name => sub { |
47
|
2
|
|
|
2
|
|
54
|
my $dbh = DBI->connect($dbconf->{dsn}, $dbconf->{username}, $dbconf->{password}, $dbconf->{options}); |
48
|
2
|
100
|
|
|
|
2873
|
$dbconf->{on_connect}($dbh) if $dbconf->{on_connect}; |
49
|
2
|
|
|
|
|
1004
|
return $dbh; |
50
|
2
|
|
|
|
|
20
|
}); |
51
|
2
|
|
|
6
|
|
95
|
$app->helper($helper => sub { return shift->app->$attr_name() }); |
|
6
|
|
|
|
|
391795
|
|
52
|
|
|
|
|
|
|
} |
53
|
|
|
|
|
|
|
} |
54
|
|
|
|
|
|
|
|
55
|
|
|
|
|
|
|
sub register { |
56
|
2
|
|
|
2
|
1
|
84
|
my $self = shift; |
57
|
2
|
|
|
|
|
5
|
my $app = shift; |
58
|
2
|
|
50
|
|
|
8
|
my $conf = shift || {}; |
59
|
|
|
|
|
|
|
|
60
|
2
|
100
|
|
|
|
8
|
if(defined($conf->{databases})) { |
61
|
1
|
|
|
|
|
2
|
$self->multi($app, $conf); |
62
|
|
|
|
|
|
|
} else { |
63
|
|
|
|
|
|
|
# old-style connect |
64
|
1
|
|
|
|
|
3
|
$self->single($app, $conf); |
65
|
|
|
|
|
|
|
} |
66
|
|
|
|
|
|
|
} |
67
|
|
|
|
|
|
|
|
68
|
|
|
|
|
|
|
1; |
69
|
|
|
|
|
|
|
__END__ |