| line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
|
1
|
|
|
|
|
|
|
# vim:set sw=4 ts=4 sts=4 ft=perl expandtab: |
|
2
|
|
|
|
|
|
|
package Mojolicious::Plugin::PgURLHelper; |
|
3
|
1
|
|
|
1
|
|
537
|
use Mojo::Base 'Mojolicious::Plugin'; |
|
|
1
|
|
|
|
|
3
|
|
|
|
1
|
|
|
|
|
8
|
|
|
4
|
1
|
|
|
1
|
|
186
|
use Mojo::URL; |
|
|
1
|
|
|
|
|
2
|
|
|
|
1
|
|
|
|
|
8
|
|
|
5
|
|
|
|
|
|
|
|
|
6
|
|
|
|
|
|
|
our $VERSION = '0.03'; |
|
7
|
|
|
|
|
|
|
|
|
8
|
|
|
|
|
|
|
sub register { |
|
9
|
1
|
|
|
1
|
1
|
37
|
my ($self, $app) = @_; |
|
10
|
|
|
|
|
|
|
|
|
11
|
1
|
|
|
|
|
7
|
$app->helper(pg_url => \&_pg_url); |
|
12
|
|
|
|
|
|
|
} |
|
13
|
|
|
|
|
|
|
|
|
14
|
|
|
|
|
|
|
sub _pg_url { |
|
15
|
13
|
|
|
13
|
|
5695
|
my $c = shift; |
|
16
|
13
|
|
|
|
|
23
|
my $s = shift; |
|
17
|
|
|
|
|
|
|
|
|
18
|
|
|
|
|
|
|
## Check that we have all what we need |
|
19
|
13
|
|
|
|
|
19
|
my $croak = 0; |
|
20
|
13
|
100
|
|
|
|
37
|
if (!defined($s->{host})) { |
|
21
|
2
|
|
|
|
|
9
|
$c->app->log->error('Missing host parameter.'); |
|
22
|
2
|
|
|
|
|
230
|
$croak++; |
|
23
|
|
|
|
|
|
|
} |
|
24
|
13
|
100
|
|
|
|
33
|
if (!defined($s->{database})) { |
|
25
|
2
|
|
|
|
|
8
|
$c->app->log->error('Missing database parameter.'); |
|
26
|
2
|
|
|
|
|
47
|
$croak++; |
|
27
|
|
|
|
|
|
|
} |
|
28
|
13
|
100
|
100
|
|
|
57
|
if ($s->{user} && index($s->{user}, ':') != -1) { |
|
29
|
1
|
|
|
|
|
4
|
$c->app->log->error('You can\'t have a colon in the user name.'); |
|
30
|
1
|
|
|
|
|
26
|
$croak++; |
|
31
|
|
|
|
|
|
|
} |
|
32
|
13
|
100
|
|
|
|
51
|
return undef if ($croak); |
|
33
|
|
|
|
|
|
|
|
|
34
|
|
|
|
|
|
|
## Let's go |
|
35
|
|
|
|
|
|
|
my $addr = Mojo::URL->new |
|
36
|
|
|
|
|
|
|
->scheme('postgresql') |
|
37
|
|
|
|
|
|
|
->host($s->{host}) |
|
38
|
9
|
|
|
|
|
33
|
->path('/'.$s->{database}); |
|
39
|
9
|
100
|
|
|
|
469
|
$addr->port($s->{port}) if defined $s->{port}; |
|
40
|
9
|
100
|
|
|
|
31
|
my $user = (defined $s->{user}) ? $s->{user} : ''; |
|
41
|
9
|
100
|
|
|
|
19
|
my $pwd = (defined $s->{pwd}) ? $s->{pwd} : ''; |
|
42
|
9
|
100
|
100
|
|
|
44
|
$addr->userinfo($user.':'.$pwd) if ($user && $pwd); |
|
43
|
9
|
|
|
|
|
58
|
return $addr->to_unsafe_string; |
|
44
|
|
|
|
|
|
|
} |
|
45
|
|
|
|
|
|
|
|
|
46
|
|
|
|
|
|
|
1; |
|
47
|
|
|
|
|
|
|
__END__ |