File Coverage

blib/lib/Pg/CLI/psql.pm
Criterion Covered Total %
statement 25 25 100.0
branch 1 2 50.0
condition n/a
subroutine 8 8 100.0
pod 1 1 100.0
total 35 36 97.2


line stmt bran cond sub pod time code
1             package Pg::CLI::psql;
2             {
3             $Pg::CLI::psql::VERSION = '0.11';
4             }
5              
6 1     1   1907 use Moose;
  1         306510  
  1         7  
7              
8 1     1   5200 use namespace::autoclean;
  1         5990  
  1         4  
9              
10 1     1   460 use MooseX::Params::Validate qw( validated_hash );
  1         52144  
  1         9  
11 1     1   595 use MooseX::SemiAffordanceAccessor;
  1         8397  
  1         3  
12 1     1   5743 use MooseX::Types::Moose qw( ArrayRef Bool Defined Str );
  1         34429  
  1         10  
13 1     1   4078 use MooseX::Types::Path::Class qw( File );
  1         239021  
  1         10  
14              
15             with qw( Pg::CLI::Role::Connects Pg::CLI::Role::Executable );
16              
17             has quiet => (
18             is => 'ro',
19             isa => Bool,
20             default => 1,
21             );
22              
23             sub execute_file {
24 1     1 1 968 my $self = shift;
25 1         6 my %p = validated_hash(
26             \@_,
27             database => { isa => Str },
28             file => { isa => Str | File },
29             options => { isa => ArrayRef [Str], optional => 1 },
30             stdin => { isa => Defined, optional => 1 },
31             stdout => { isa => Defined, optional => 1 },
32             stderr => { isa => Defined, optional => 1 },
33             );
34              
35 1         11299 push @{ $p{options} }, '-f', ( delete $p{file} ) . q{};
  1         5  
36              
37 1         7 $self->run(%p);
38             }
39              
40             sub _run_options {
41 4     4   5 my $self = shift;
42              
43 4 50       88 return ( $self->quiet() ? '-q' : () );
44             }
45              
46             __PACKAGE__->meta()->make_immutable();
47              
48             1;
49              
50             # ABSTRACT: Wrapper for the F<psql> utility
51              
52             __END__
53              
54             =pod
55              
56             =head1 NAME
57              
58             Pg::CLI::psql - Wrapper for the F<psql> utility
59              
60             =head1 VERSION
61              
62             version 0.11
63              
64             =head1 SYNOPSIS
65              
66             my $psql = Pg::CLI::psql->new(
67             username => 'foo',
68             password => 'bar',
69             host => 'pg.example.com',
70             port => 5433,
71             );
72              
73             $psql->run(
74             database => 'database',
75             options => [ '-c', 'DELETE FROM table' ],
76             );
77              
78             $psql->execute_file(
79             database => 'database',
80             file => 'thing.sql',
81             );
82              
83             my $errors;
84             $psql->run(
85             database => 'foo',
86             stdin => \$sql,
87             stderr => \$errors,
88             );
89              
90             =head1 DESCRIPTION
91              
92             This class provides a wrapper for the F<psql> utility.
93              
94             =head1 METHODS
95              
96             This class provides the following methods:
97              
98             =head2 Pg::CLI::psql->new( ... )
99              
100             The constructor accepts a number of parameters:
101              
102             =over 4
103              
104             =item * executable
105              
106             The path to F<psql>. By default, this will look for F<psql> in your path and
107             throw an error if it cannot be found.
108              
109             =item * username
110              
111             The username to use when connecting to the database. Optional.
112              
113             =item * password
114              
115             The password to use when connecting to the database. Optional.
116              
117             =item * host
118              
119             The host to use when connecting to the database. Optional.
120              
121             =item * port
122              
123             The port to use when connecting to the database. Optional.
124              
125             =item * require_ssl
126              
127             If this is true, then the C<PGSSLMODE> environment variable will be set to
128             "require" when connecting to the database.
129              
130             =item * quiet
131              
132             This defaults to true. When true, the "-q" flag is passed to psql whenever it
133             is executed.
134              
135             =back
136              
137             =head2 $psql->run( database => ..., options => [ ... ] )
138              
139             This method runs a command against the specified database. You must pass one
140             or more options that indicate what psql should do.
141              
142             This method also accepts optional C<stdin>, C<stdout>, and C<stderr>
143             parameters. These parameters can be any defined value that could be passed as
144             the relevant parameter to L<IPC::Run3>'s C<run3> subroutine.
145              
146             Most notably, you can pass scalar references to pipe data in via the C<stdin>
147             parameter or capture output sent to C<stdout> or C<stderr>
148              
149             =head2 $psql->execute_file( database => ..., file => ... )
150              
151             This method executes the specified file against the database. You can also
152             pass additional options via the C<options> parameter.
153              
154             This method also accepts optional C<stdin>, C<stdout>, and C<stderr>
155             parameters, just like the C<< $psql->run() >> method.
156              
157             =head2 $psql->version()
158              
159             Returns a the three part version as a string.
160              
161             =head2 $psql->two_part_version()
162              
163             Returns the first two decimal numbers in the version.
164              
165             =head1 BUGS
166              
167             See L<Pg::CLI> for bug reporting details.
168              
169             =head1 AUTHOR
170              
171             Dave Rolsky <autarch@urth.org>
172              
173             =head1 COPYRIGHT AND LICENSE
174              
175             This software is Copyright (c) 2013 by Dave Rolsky.
176              
177             This is free software, licensed under:
178              
179             The Artistic License 2.0 (GPL Compatible)
180              
181             =cut