| line | stmt | bran | cond | sub | pod | time | code | 
| 1 |  |  |  |  |  |  | package App::Environ::Mojo::Pg; | 
| 2 |  |  |  |  |  |  |  | 
| 3 |  |  |  |  |  |  | our $VERSION = '0.1'; | 
| 4 |  |  |  |  |  |  |  | 
| 5 | 1 |  |  | 1 |  | 541 | use strict; | 
|  | 1 |  |  |  |  | 2 |  | 
|  | 1 |  |  |  |  | 24 |  | 
| 6 | 1 |  |  | 1 |  | 3 | use warnings; | 
|  | 1 |  |  |  |  | 2 |  | 
|  | 1 |  |  |  |  | 24 |  | 
| 7 | 1 |  |  | 1 |  | 19 | use v5.10; | 
|  | 1 |  |  |  |  | 2 |  | 
| 8 | 1 |  |  | 1 |  | 3 | use utf8; | 
|  | 1 |  |  |  |  | 1 |  | 
|  | 1 |  |  |  |  | 13 |  | 
| 9 |  |  |  |  |  |  |  | 
| 10 | 1 |  |  | 1 |  | 219 | use App::Environ; | 
|  | 0 |  |  |  |  |  |  | 
|  | 0 |  |  |  |  |  |  | 
| 11 |  |  |  |  |  |  | use App::Environ::Config; | 
| 12 |  |  |  |  |  |  | use Params::Validate qw(validate_pos); | 
| 13 |  |  |  |  |  |  | use URI; | 
| 14 |  |  |  |  |  |  | use Mojo::Pg; | 
| 15 |  |  |  |  |  |  |  | 
| 16 |  |  |  |  |  |  | my %PG; | 
| 17 |  |  |  |  |  |  |  | 
| 18 |  |  |  |  |  |  | App::Environ::Config->register(qw(pg.yml)); | 
| 19 |  |  |  |  |  |  |  | 
| 20 |  |  |  |  |  |  | sub pg { | 
| 21 |  |  |  |  |  |  | my $class = shift; | 
| 22 |  |  |  |  |  |  |  | 
| 23 |  |  |  |  |  |  | my ($connector) = validate_pos( @_, 1 ); | 
| 24 |  |  |  |  |  |  |  | 
| 25 |  |  |  |  |  |  | unless ( defined $PG{$connector} ) { | 
| 26 |  |  |  |  |  |  | my $pg_string = $class->pg_string($connector); | 
| 27 |  |  |  |  |  |  | $PG{$connector} = Mojo::Pg->new($pg_string); | 
| 28 |  |  |  |  |  |  |  | 
| 29 |  |  |  |  |  |  | my $conf = App::Environ::Config->instance->{pg}{connectors}{$connector}; | 
| 30 |  |  |  |  |  |  |  | 
| 31 |  |  |  |  |  |  | if ( $conf->{max_connections} ) { | 
| 32 |  |  |  |  |  |  | $PG{$connector}->max_connections( $conf->{max_connections} ); | 
| 33 |  |  |  |  |  |  | } | 
| 34 |  |  |  |  |  |  |  | 
| 35 |  |  |  |  |  |  | if ( $conf->{migrations} && defined $conf->{migrations}{file} ) { | 
| 36 |  |  |  |  |  |  | if ( defined $conf->{migrations}{name} ) { | 
| 37 |  |  |  |  |  |  | $PG{$connector}->migrations->name( $conf->{migrations}{name} ); | 
| 38 |  |  |  |  |  |  | } | 
| 39 |  |  |  |  |  |  |  | 
| 40 |  |  |  |  |  |  | $PG{$connector}->migrations->from_file( $conf->{migrations}{file} ); | 
| 41 |  |  |  |  |  |  | } | 
| 42 |  |  |  |  |  |  | } | 
| 43 |  |  |  |  |  |  |  | 
| 44 |  |  |  |  |  |  | return $PG{$connector}; | 
| 45 |  |  |  |  |  |  | } | 
| 46 |  |  |  |  |  |  |  | 
| 47 |  |  |  |  |  |  | sub pg_string { | 
| 48 |  |  |  |  |  |  | my $class = shift; | 
| 49 |  |  |  |  |  |  |  | 
| 50 |  |  |  |  |  |  | my ($connector) = validate_pos( @_, 1 ); | 
| 51 |  |  |  |  |  |  |  | 
| 52 |  |  |  |  |  |  | my $conf = App::Environ::Config->instance->{pg}{connectors}{$connector}; | 
| 53 |  |  |  |  |  |  |  | 
| 54 |  |  |  |  |  |  | my $url = URI->new(); | 
| 55 |  |  |  |  |  |  |  | 
| 56 |  |  |  |  |  |  | ## Non standart schema workaround | 
| 57 |  |  |  |  |  |  | ## For URI objects that do not belong to one of these, you can only use the common and generic methods. | 
| 58 |  |  |  |  |  |  | $url->scheme('https'); | 
| 59 |  |  |  |  |  |  |  | 
| 60 |  |  |  |  |  |  | $url->userinfo("$conf->{user}:$conf->{password}"); | 
| 61 |  |  |  |  |  |  | $url->host( $conf->{host} ); | 
| 62 |  |  |  |  |  |  | $url->port( $conf->{port} ); | 
| 63 |  |  |  |  |  |  | $url->path( $conf->{dbname} ); | 
| 64 |  |  |  |  |  |  | $url->query_form( %{ $conf->{options} } ); | 
| 65 |  |  |  |  |  |  |  | 
| 66 |  |  |  |  |  |  | $url->scheme('postgresql'); | 
| 67 |  |  |  |  |  |  |  | 
| 68 |  |  |  |  |  |  | return $url->as_string; | 
| 69 |  |  |  |  |  |  | } | 
| 70 |  |  |  |  |  |  |  | 
| 71 |  |  |  |  |  |  | 1; | 
| 72 |  |  |  |  |  |  |  | 
| 73 |  |  |  |  |  |  | __END__ |