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
|
|
506
|
use Mojo::Base 'Mojolicious::Plugin'; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
6
|
|
4
|
1
|
|
|
1
|
|
163
|
use Mojo::URL; |
|
1
|
|
|
|
|
3
|
|
|
1
|
|
|
|
|
7
|
|
5
|
|
|
|
|
|
|
|
6
|
|
|
|
|
|
|
our $VERSION = '0.02'; |
7
|
|
|
|
|
|
|
|
8
|
|
|
|
|
|
|
sub register { |
9
|
1
|
|
|
1
|
1
|
38
|
my ($self, $app) = @_; |
10
|
|
|
|
|
|
|
|
11
|
1
|
|
|
|
|
6
|
$app->helper(pg_url => \&_pg_url); |
12
|
|
|
|
|
|
|
} |
13
|
|
|
|
|
|
|
|
14
|
|
|
|
|
|
|
sub _pg_url { |
15
|
13
|
|
|
13
|
|
6479
|
my $c = shift; |
16
|
13
|
|
|
|
|
24
|
my $s = shift; |
17
|
|
|
|
|
|
|
|
18
|
|
|
|
|
|
|
## Check that we have all what we need |
19
|
13
|
|
|
|
|
26
|
my $croak = 0; |
20
|
13
|
100
|
|
|
|
38
|
if (!defined($s->{host})) { |
21
|
2
|
|
|
|
|
8
|
$c->app->log->error('Missing host parameter.'); |
22
|
2
|
|
|
|
|
214
|
$croak++; |
23
|
|
|
|
|
|
|
} |
24
|
13
|
100
|
|
|
|
32
|
if (!defined($s->{database})) { |
25
|
2
|
|
|
|
|
6
|
$c->app->log->error('Missing database parameter.'); |
26
|
2
|
|
|
|
|
50
|
$croak++; |
27
|
|
|
|
|
|
|
} |
28
|
13
|
100
|
|
|
|
442
|
if (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
|
|
|
|
68
|
return undef if ($croak); |
33
|
|
|
|
|
|
|
|
34
|
|
|
|
|
|
|
## Let's go |
35
|
|
|
|
|
|
|
my $addr = Mojo::URL->new |
36
|
|
|
|
|
|
|
->scheme('postgresql') |
37
|
|
|
|
|
|
|
->host($s->{host}) |
38
|
9
|
|
|
|
|
37
|
->path('/'.$s->{database}); |
39
|
9
|
100
|
|
|
|
528
|
$addr->port($s->{port}) if defined $s->{port}; |
40
|
9
|
100
|
|
|
|
42
|
my $user = (defined $s->{user}) ? $s->{user} : ''; |
41
|
9
|
100
|
|
|
|
22
|
my $pwd = (defined $s->{pwd}) ? $s->{pwd} : ''; |
42
|
9
|
100
|
100
|
|
|
51
|
$addr->userinfo($user.':'.$pwd) if ($user && $pwd); |
43
|
9
|
|
|
|
|
59
|
warn $addr->to_unsafe_string; |
44
|
9
|
|
|
|
|
2639
|
return $addr->to_unsafe_string; |
45
|
|
|
|
|
|
|
} |
46
|
|
|
|
|
|
|
|
47
|
|
|
|
|
|
|
1; |
48
|
|
|
|
|
|
|
__END__ |