line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
3
|
|
|
3
|
|
19399
|
use strict; |
|
3
|
|
|
|
|
5
|
|
|
3
|
|
|
|
|
128
|
|
2
|
3
|
|
|
3
|
|
16
|
use warnings; |
|
3
|
|
|
|
|
6
|
|
|
3
|
|
|
|
|
170
|
|
3
|
|
|
|
|
|
|
package Mojolicious::Plugin::Database; |
4
|
|
|
|
|
|
|
$Mojolicious::Plugin::Database::VERSION = '1.09'; |
5
|
3
|
|
|
3
|
|
441
|
use Mojo::Base 'Mojolicious::Plugin'; |
|
3
|
|
|
|
|
6979
|
|
|
3
|
|
|
|
|
22
|
|
6
|
3
|
|
|
3
|
|
2544
|
use DBI; |
|
3
|
|
|
|
|
12587
|
|
|
3
|
|
|
|
|
1849
|
|
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
|
|
|
|
5
|
die ref($self), ': missing dsn parameter', "\n" unless($conf->{dsn}); |
14
|
|
|
|
|
|
|
|
15
|
1
|
|
|
1
|
|
6
|
my $dbh_connect = sub { DBI->connect($conf->{dsn}, $conf->{username}, $conf->{password}, $conf->{options}) }; |
|
1
|
|
|
|
|
21
|
|
16
|
|
|
|
|
|
|
|
17
|
1
|
|
50
|
|
|
11
|
my $helper_name = $conf->{helper} || 'db'; |
18
|
|
|
|
|
|
|
|
19
|
1
|
|
|
|
|
15
|
$app->attr("_dbh_$helper_name" => $dbh_connect); |
20
|
|
|
|
|
|
|
|
21
|
|
|
|
|
|
|
$app->helper($helper_name => sub { |
22
|
2
|
|
|
2
|
|
63077
|
my $self = shift; |
23
|
2
|
|
|
|
|
8
|
my $attr = "_dbh_$helper_name"; |
24
|
2
|
|
|
|
|
6
|
return $self->app->$attr(); |
25
|
1
|
|
|
|
|
63
|
}); |
26
|
|
|
|
|
|
|
} |
27
|
|
|
|
|
|
|
|
28
|
|
|
|
|
|
|
sub multi { |
29
|
1
|
|
|
1
|
0
|
1
|
my $self = shift; |
30
|
1
|
|
|
|
|
1
|
my $app = shift; |
31
|
1
|
|
|
|
|
1
|
my $conf = shift; |
32
|
|
|
|
|
|
|
|
33
|
|
|
|
|
|
|
# databases should be a hashref |
34
|
1
|
50
|
|
|
|
3
|
die ref($self), ': databases is not a hash reference', "\n" unless(ref($conf->{databases}) eq 'HASH'); |
35
|
|
|
|
|
|
|
|
36
|
1
|
|
|
|
|
1
|
foreach my $helper (keys(%{$conf->{databases}})) { |
|
1
|
|
|
|
|
4
|
|
37
|
2
|
|
|
|
|
34
|
my $dbconf = $conf->{databases}->{$helper}; |
38
|
2
|
50
|
|
|
|
6
|
die ref($self), ': missing dsn parameter for ' . $helper, "\n" unless(defined($dbconf->{dsn})); |
39
|
2
|
|
|
|
|
3
|
my $attr_name = '_dbh_' . $helper; |
40
|
|
|
|
|
|
|
$app->attr($attr_name => sub { |
41
|
2
|
|
|
2
|
|
29
|
DBI->connect($dbconf->{dsn}, $dbconf->{username}, $dbconf->{password}, $dbconf->{options}); |
42
|
2
|
|
|
|
|
12
|
}); |
43
|
2
|
|
|
4
|
|
56
|
$app->helper($helper => sub { return shift->app->$attr_name() }); |
|
4
|
|
|
|
|
300776
|
|
44
|
|
|
|
|
|
|
} |
45
|
|
|
|
|
|
|
} |
46
|
|
|
|
|
|
|
|
47
|
|
|
|
|
|
|
sub register { |
48
|
2
|
|
|
2
|
1
|
93
|
my $self = shift; |
49
|
2
|
|
|
|
|
3
|
my $app = shift; |
50
|
2
|
|
50
|
|
|
10
|
my $conf = shift || {}; |
51
|
|
|
|
|
|
|
|
52
|
2
|
100
|
|
|
|
9
|
if(defined($conf->{databases})) { |
53
|
1
|
|
|
|
|
2
|
$self->multi($app, $conf); |
54
|
|
|
|
|
|
|
} else { |
55
|
|
|
|
|
|
|
# old-style connect |
56
|
1
|
|
|
|
|
3
|
$self->single($app, $conf); |
57
|
|
|
|
|
|
|
} |
58
|
|
|
|
|
|
|
} |
59
|
|
|
|
|
|
|
|
60
|
|
|
|
|
|
|
1; |
61
|
|
|
|
|
|
|
__END__ |