File Coverage

blib/lib/Pg/CLI/Role/Connects.pm
Criterion Covered Total %
statement 35 41 85.3
branch 14 14 100.0
condition 2 9 22.2
subroutine 11 12 91.6
pod 0 1 0.0
total 62 77 80.5


line stmt bran cond sub pod time code
1             package Pg::CLI::Role::Connects;
2             {
3             $Pg::CLI::Role::Connects::VERSION = '0.11';
4             }
5              
6 5     5   2711 use Moose::Role;
  5         8  
  5         34  
7              
8 5     5   17130 use namespace::autoclean;
  5         9  
  5         32  
9              
10 5     5   2504 use IPC::Run3 qw( run3 );
  5         95509  
  5         313  
11 5     5   1673 use MooseX::Params::Validate qw( validated_hash validated_list );
  5         207250  
  5         38  
12 5     5   1148 use MooseX::SemiAffordanceAccessor;
  5         8  
  5         32  
13 5     5   10864 use MooseX::Types::Moose qw( ArrayRef Bool Defined Str );
  5         69068  
  5         46  
14              
15             with 'Pg::CLI::Role::HasVersion';
16              
17             for my $attr (qw( username password host port )) {
18             has $attr => (
19             is => 'rw',
20             isa => Str,
21             predicate => '_has_' . $attr,
22             );
23             }
24              
25             has require_ssl => (
26             is => 'ro',
27             isa => 'Bool',
28             default => 0,
29             );
30              
31             sub run {
32 18     18 0 4934 my $self = shift;
33 18         93 my ( $database, $options, $stdin, $stdout, $stderr ) = validated_list(
34             \@_,
35             database => { isa => Str, optional => 1 },
36             options => {
37             isa => ArrayRef [Str], default => [],
38             },
39             stdin => { isa => Defined, optional => 1 },
40             stdout => { isa => Defined, optional => 1 },
41             stderr => { isa => Defined, optional => 1 },
42             );
43              
44             $self->_execute_command(
45             [
46             $self->executable(),
47             $self->_connect_options(),
48             $self->_run_options($database),
49 18 100 66     125658 @{$options},
  18         93  
50             ( $database && $self->_database_at_end() ? $database : () ),
51             ],
52             $stdin, $stdout, $stderr,
53             );
54             }
55              
56             sub _database_at_end {
57 15     15   121 return 1;
58             }
59              
60             sub _run_options {
61 11     11   19 return;
62             }
63              
64             sub _execute_command {
65 18     18   32 my $self = shift;
66              
67 18 100       474 local $ENV{PGPASSWORD} = $self->password()
68             if $self->_has_password();
69              
70 18 100       406 local $ENV{PGSSLMODE} = 'require'
71             if $self->require_ssl();
72              
73 18         79 $self->_call_run3(@_);
74             }
75              
76             # This is a separate sub to provide something we can override in testing
77             sub _call_run3 {
78 0     0   0 shift;
79 0         0 my $cmd = shift;
80 0   0     0 my $stdin = shift || \undef;
81 0   0     0 my $stdout = shift || \undef;
82 0   0     0 my $stderr = shift || \undef;
83              
84 0         0 run3(
85             $cmd,
86             $stdin,
87             $stdout,
88             $stderr,
89             );
90             }
91              
92             sub _connect_options {
93 18     18   26 my $self = shift;
94              
95 18         27 my @options;
96              
97 18 100       468 push @options, '-U', $self->username()
98             if $self->_has_username();
99              
100 18 100       438 push @options, '-h', $self->host()
101             if $self->_has_host();
102              
103 18 100       454 push @options, '-p', $self->port()
104             if $self->_has_port();
105              
106 18 100       452 push @options, '-w'
107             if $self->two_part_version >= 8.4;
108              
109 18         97 return @options;
110             }
111              
112             1;