File Coverage

blib/lib/Pg/CLI/psql.pm
Criterion Covered Total %
statement 36 36 100.0
branch 1 2 50.0
condition n/a
subroutine 11 11 100.0
pod 1 1 100.0
total 49 50 98.0


line stmt bran cond sub pod time code
1             package Pg::CLI::psql;
2              
3 1     1   1630 use strict;
  1         7  
  1         27  
4 1     1   5 use warnings;
  1         1  
  1         23  
5 1     1   409 use namespace::autoclean;
  1         18879  
  1         3  
6              
7             our $VERSION = '0.14';
8              
9 1     1   522 use MooseX::Params::Validate qw( validated_hash );
  1         537027  
  1         7  
10 1     1   792 use MooseX::Types qw( as coerce from subtype via where );
  1         37522  
  1         6  
11 1     1   4022 use MooseX::Types::Moose qw( ArrayRef Bool Defined Str );
  1         17559  
  1         12  
12 1     1   6391 use MooseX::Types::Path::Class qw( File );
  1         83426  
  1         8  
13              
14 1     1   1356 use Moose;
  1         2  
  1         9  
15 1     1   6720 use MooseX::SemiAffordanceAccessor;
  1         12940  
  1         5  
16              
17             with qw( Pg::CLI::Role::Connects Pg::CLI::Role::Executable );
18              
19             has quiet => (
20             is => 'ro',
21             isa => Bool,
22             default => 1,
23             );
24              
25             {
26              
27             my $array_of_files = subtype(
28             as ArrayRef [ Str | File ],
29             where { @$_ > 0 },
30             );
31             coerce $array_of_files, from Str | File, via { [$_] };
32              
33             sub execute_file {
34 2     2 1 2502 my $self = shift;
35 2         10 my %p = validated_hash(
36             \@_,
37             database => { isa => Str, optional => 1 },
38             file => { isa => $array_of_files, coerce => 1 },
39             options => { isa => ArrayRef [Str], optional => 1 },
40             stdin => { isa => Defined, optional => 1 },
41             stdout => { isa => Defined, optional => 1 },
42             stderr => { isa => Defined, optional => 1 },
43             );
44              
45 2         1189 push @{ $p{options} }, map { ( '-f', "$_" ) } @{ delete $p{file} };
  2         7  
  3         12  
  2         7  
46              
47 2         12 $self->run(%p);
48             }
49             }
50              
51             ## no critic (Subroutines::ProhibitUnusedPrivateSubroutines)
52             sub _run_options {
53 7     7   12 my $self = shift;
54              
55 7 50       163 return ( $self->quiet() ? '-q' : () );
56             }
57             ## use critic
58              
59             __PACKAGE__->meta()->make_immutable();
60              
61             1;
62              
63             # ABSTRACT: Wrapper for the F<psql> utility
64              
65             __END__
66              
67             =pod
68              
69             =encoding UTF-8
70              
71             =head1 NAME
72              
73             Pg::CLI::psql - Wrapper for the F<psql> utility
74              
75             =head1 VERSION
76              
77             version 0.14
78              
79             =head1 SYNOPSIS
80              
81             my $psql = Pg::CLI::psql->new(
82             username => 'foo',
83             password => 'bar',
84             host => 'pg.example.com',
85             port => 5433,
86             );
87              
88             $psql->run(
89             database => 'database',
90             options => [ '-c', 'DELETE FROM table' ],
91             );
92              
93             $psql->execute_file(
94             database => 'database',
95             file => 'thing.sql',
96             );
97              
98             my $sql = '...';
99             my $errors;
100             $psql->run(
101             database => 'foo',
102             stdin => \$sql,
103             stderr => \$errors,
104             );
105              
106             =head1 DESCRIPTION
107              
108             This class provides a wrapper for the F<psql> utility.
109              
110             =head1 METHODS
111              
112             This class provides the following methods:
113              
114             =head2 Pg::CLI::psql->new( ... )
115              
116             The constructor accepts a number of parameters:
117              
118             =over 4
119              
120             =item * executable
121              
122             The path to F<psql>. By default, this will look for F<psql> in your path and
123             throw an error if it cannot be found.
124              
125             =item * username
126              
127             The username to use when connecting to the database. Optional.
128              
129             =item * password
130              
131             The password to use when connecting to the database. Optional.
132              
133             =item * host
134              
135             The host to use when connecting to the database. Optional.
136              
137             =item * port
138              
139             The port to use when connecting to the database. Optional.
140              
141             =item * require_ssl
142              
143             If this is true, then the C<PGSSLMODE> environment variable will be set to
144             "require" when connecting to the database.
145              
146             =item * quiet
147              
148             This defaults to true. When true, the "-q" flag is passed to psql whenever it
149             is executed.
150              
151             =back
152              
153             =head2 $psql->run( database => ..., options => [ ... ] )
154              
155             This method runs a command against the specified database. You must pass one
156             or more options that indicate what psql should do.
157              
158             This method also accepts optional C<stdin>, C<stdout>, and C<stderr>
159             parameters. These parameters can be any defined value that could be passed as
160             the relevant parameter to L<IPC::Run3>'s C<run3> subroutine.
161              
162             Most notably, you can pass scalar references to pipe data in via the C<stdin>
163             parameter or capture output sent to C<stdout> or C<stderr>
164              
165             =head2 $psql->execute_file( database => ..., file => ... )
166              
167             This method executes the specified file or files against the database. C<file>
168             should either be the path to a single file as a string or C<Path::Class::File>
169             or an arrayref of such paths. You can also pass additional options via the
170             C<options> parameter.
171              
172             This method also accepts optional C<stdin>, C<stdout>, and C<stderr>
173             parameters, just like the C<< $psql->run() >> method.
174              
175             =head2 $psql->version()
176              
177             Returns a the three part version as a string.
178              
179             =head2 $psql->two_part_version()
180              
181             Returns the first two decimal numbers in the version.
182              
183             =head1 SUPPORT
184              
185             Bugs may be submitted at L<http://rt.cpan.org/Public/Dist/Display.html?Name=Pg-CLI> or via email to L<bug-pg-cli@rt.cpan.org|mailto:bug-pg-cli@rt.cpan.org>.
186              
187             I am also usually active on IRC as 'autarch' on C<irc://irc.perl.org>.
188              
189             =head1 SOURCE
190              
191             The source code repository for Pg-CLI can be found at L<https://github.com/houseabsolute/Pg-CLI>.
192              
193             =head1 AUTHOR
194              
195             Dave Rolsky <autarch@urth.org>
196              
197             =head1 COPYRIGHT AND LICENSE
198              
199             This software is Copyright (c) 2018 by Dave Rolsky.
200              
201             This is free software, licensed under:
202              
203             The Artistic License 2.0 (GPL Compatible)
204              
205             The full text of the license can be found in the
206             F<LICENSE> file included with this distribution.
207              
208             =cut