| 1 | package Test::Environment::Plugin::PostgreSQL; | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | =head1 NAME | ||||||||||||||||||||||||||||
| 4 | |||||||||||||||||||||||||||||
| 5 | Test::Environment::Plugin::PostgreSQL - PostreSQL psql function for testing | ||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||
| 7 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||
| 9 | use Test::Environment qw{ | ||||||||||||||||||||||||||||
| 10 | PostgreSQL | ||||||||||||||||||||||||||||
| 11 | }; | ||||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||
| 13 | # set database credentials | ||||||||||||||||||||||||||||
| 14 | psql( | ||||||||||||||||||||||||||||
| 15 | 'database' => $config->{'db'}->{'database'}, | ||||||||||||||||||||||||||||
| 16 | 'hostname' => $config->{'db'}->{'hostname'}, | ||||||||||||||||||||||||||||
| 17 | 'username' => $config->{'db'}->{'username'}, | ||||||||||||||||||||||||||||
| 18 | 'password' => $config->{'db'}->{'password'}, | ||||||||||||||||||||||||||||
| 19 | # or skip hostname and database and set them via | ||||||||||||||||||||||||||||
| 20 | #'dbi_dsn' => 'dbi:Pg:dbname=dsn_test;host=localhost', | ||||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||
| 22 | ); | ||||||||||||||||||||||||||||
| 23 | |||||||||||||||||||||||||||||
| 24 | # execute sql query | ||||||||||||||||||||||||||||
| 25 | my @output = psql( | ||||||||||||||||||||||||||||
| 26 | 'switches' => '--expanded', | ||||||||||||||||||||||||||||
| 27 | 'command' => 'SELECT * FROM Table', | ||||||||||||||||||||||||||||
| 28 | # ..., see psql function description for more | ||||||||||||||||||||||||||||
| 29 | ) | ||||||||||||||||||||||||||||
| 30 | |||||||||||||||||||||||||||||
| 31 | |||||||||||||||||||||||||||||
| 32 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||
| 33 | |||||||||||||||||||||||||||||
| 34 | This plugin will export 'psql' function that can be used to execute PostreSQL psql command | ||||||||||||||||||||||||||||
| 35 | with lot of options for testing. | ||||||||||||||||||||||||||||
| 36 | |||||||||||||||||||||||||||||
| 37 | Module will prepare %ENV for postgres: | ||||||||||||||||||||||||||||
| 38 | |||||||||||||||||||||||||||||
| 39 | 'username' => 'PGUSER', | ||||||||||||||||||||||||||||
| 40 | 'password' => 'PGPASSWORD', | ||||||||||||||||||||||||||||
| 41 | 'database' => 'PGDATABASE', | ||||||||||||||||||||||||||||
| 42 | 'hostname' => 'PGHOST', | ||||||||||||||||||||||||||||
| 43 | 'port' => 'PGPORT', | ||||||||||||||||||||||||||||
| 44 | |||||||||||||||||||||||||||||
| 45 | Any postgres connection settings not listed or undef will be deleted from the %ENV hash. | ||||||||||||||||||||||||||||
| 46 | |||||||||||||||||||||||||||||
| 47 | =cut | ||||||||||||||||||||||||||||
| 48 | |||||||||||||||||||||||||||||
| 49 | |||||||||||||||||||||||||||||
| 50 | 1 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 51 | 1 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 52 | |||||||||||||||||||||||||||||
| 53 | 1 | our $VERSION = "0.07"; | |||||||||||||||||||||||||||
| 54 | |||||||||||||||||||||||||||||
| 55 | 1 | ▶ | use base qw{ Exporter }; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 56 | 1 | our @EXPORT = qw{ | |||||||||||||||||||||||||||
| 57 | psql | ||||||||||||||||||||||||||||
| 58 | }; | ||||||||||||||||||||||||||||
| 59 | 1 | our $debug = 0; | |||||||||||||||||||||||||||
| 60 | |||||||||||||||||||||||||||||
| 61 | 1 | ▶ | use Carp::Clan; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 62 | 1 | ▶ | use String::ShellQuote; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 63 | 1 | ▶ | use List::MoreUtils 'any'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 64 | 1 | ▶ | use DBI; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 65 | |||||||||||||||||||||||||||||
| 66 | =head1 FUNCTIONS | ||||||||||||||||||||||||||||
| 67 | |||||||||||||||||||||||||||||
| 68 | =head2 import | ||||||||||||||||||||||||||||
| 69 | |||||||||||||||||||||||||||||
| 70 | All functions are exported 2 levels up. That is to the use Test::Environment caller. | ||||||||||||||||||||||||||||
| 71 | |||||||||||||||||||||||||||||
| 72 | =cut | ||||||||||||||||||||||||||||
| 73 | |||||||||||||||||||||||||||||
| 74 | 1 | ▶ | sub import { | ||||||||||||||||||||||||||
|
Subroutine
import: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 75 | 1 | my $package = shift; | |||||||||||||||||||||||||||
| 76 | |||||||||||||||||||||||||||||
| 77 | # export symbols two levels up - to the Test::Environment caller | ||||||||||||||||||||||||||||
| 78 | 1 | __PACKAGE__->export_to_level(2, $package, @EXPORT); | |||||||||||||||||||||||||||
| 79 | } | ||||||||||||||||||||||||||||
| 80 | |||||||||||||||||||||||||||||
| 81 | |||||||||||||||||||||||||||||
| 82 | =head2 psql() | ||||||||||||||||||||||||||||
| 83 | |||||||||||||||||||||||||||||
| 84 | psql command executed easily. Here is the list of options that can be used. | ||||||||||||||||||||||||||||
| 85 | |||||||||||||||||||||||||||||
| 86 | Option related to the connection to the database. | ||||||||||||||||||||||||||||
| 87 | |||||||||||||||||||||||||||||
| 88 | username | ||||||||||||||||||||||||||||
| 89 | password | ||||||||||||||||||||||||||||
| 90 | database | ||||||||||||||||||||||||||||
| 91 | hostname | ||||||||||||||||||||||||||||
| 92 | port | ||||||||||||||||||||||||||||
| 93 | |||||||||||||||||||||||||||||
| 94 | By setting this PostreSQL %ENV variables will be set. So for psql command to the | ||||||||||||||||||||||||||||
| 95 | same databse you need to set them only once. | ||||||||||||||||||||||||||||
| 96 | |||||||||||||||||||||||||||||
| 97 | The rest of the option related to the psql command. | ||||||||||||||||||||||||||||
| 98 | |||||||||||||||||||||||||||||
| 99 | command - scalar or array ref of sql commands | ||||||||||||||||||||||||||||
| 100 | switches - scalar or array of additional psql switches | ||||||||||||||||||||||||||||
| 101 | output_filename - the output will be written to this file (-o) | ||||||||||||||||||||||||||||
| 102 | execution_path - before executing psql change to that folder | ||||||||||||||||||||||||||||
| 103 | stderr_redirect - will redirect stderr to stdout so that also error appears in the return value | ||||||||||||||||||||||||||||
| 104 | debug - turn on debug mode, it can be also done globaly by setting "$ENV{'IN_DEBUG_MODE'} = 1" | ||||||||||||||||||||||||||||
| 105 | |||||||||||||||||||||||||||||
| 106 | =cut | ||||||||||||||||||||||||||||
| 107 | |||||||||||||||||||||||||||||
| 108 | 0 | ▶ | sub psql { | ||||||||||||||||||||||||||
|
Subroutine
psql: not called CC=22 SCAR=62.3
Pod
documented
| |||||||||||||||||||||||||||||
| 109 | 0 | my %arg = @_; | |||||||||||||||||||||||||||
| 110 | |||||||||||||||||||||||||||||
| 111 | # deparse dbi_dsn if passed | ||||||||||||||||||||||||||||
| 112 | 0 | ▶ | if (defined $arg{'dbi_dsn'}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 113 | my($scheme, $driver, $attr_string, $attr_hash, $driver_dsn) | ||||||||||||||||||||||||||||
| 114 | 0 | = DBI->parse_dsn($arg{'dbi_dsn'}); | |||||||||||||||||||||||||||
| 115 | |||||||||||||||||||||||||||||
| 116 | 0 | ▶ | croak 'not a Pg dbi dsn "'.$arg{'dbi_dsn'}.'"' | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$scheme ne "dbi" or $driver ne "Pg"
MC/DC0%0/2
$scheme ne "dbi" or $driver ne "Pg"
$scheme ne "dbi"
$driver ne "Pg"
Truth table0%0/3
A or B
A= $scheme ne "dbi"B= $driver ne "Pg"
| |||||||||||||||||||||||||||||
| 117 | if (($scheme ne 'dbi') or ($driver ne 'Pg')); | ||||||||||||||||||||||||||||
| 118 | |||||||||||||||||||||||||||||
| 119 | # contruct hash out of "dbname=dsn_test;host=localhost;port=123" | ||||||||||||||||||||||||||||
| 120 | 0 | my %dsn_options = map { split '=', $_ } split(';',$driver_dsn); | |||||||||||||||||||||||||||
| 121 | |||||||||||||||||||||||||||||
| 122 | $arg{'database'} = $dsn_options{'dbname'} | ||||||||||||||||||||||||||||
| 123 | 0 | ▶ | if exists $dsn_options{'dbname'}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 124 | $arg{'hostname'} = $dsn_options{'host'} | ||||||||||||||||||||||||||||
| 125 | 0 | ▶ | if exists $dsn_options{'host'}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 126 | $arg{'port'} = $dsn_options{'port'} | ||||||||||||||||||||||||||||
| 127 | 0 | ▶ | if exists $dsn_options{'port'}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 128 | } | ||||||||||||||||||||||||||||
| 129 | |||||||||||||||||||||||||||||
| 130 | 0 | my %pg_settings_names = ( | |||||||||||||||||||||||||||
| 131 | 'username' => 'PGUSER', | ||||||||||||||||||||||||||||
| 132 | 'password' => 'PGPASSWORD', | ||||||||||||||||||||||||||||
| 133 | 'database' => 'PGDATABASE', | ||||||||||||||||||||||||||||
| 134 | 'hostname' => 'PGHOST', | ||||||||||||||||||||||||||||
| 135 | 'port' => 'PGPORT', | ||||||||||||||||||||||||||||
| 136 | ); | ||||||||||||||||||||||||||||
| 137 | |||||||||||||||||||||||||||||
| 138 | # change %ENV only if at least one pg connection setting set | ||||||||||||||||||||||||||||
| 139 | 0 | ▶ | if (any { $pg_settings_names{$_} } keys %arg) { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 140 | # set/delete postgres ENV variables | ||||||||||||||||||||||||||||
| 141 | 0 | foreach my $arg_name (keys %pg_settings_names) { | |||||||||||||||||||||||||||
| 142 | 0 | my $env_name = $pg_settings_names{$arg_name}; | |||||||||||||||||||||||||||
| 143 | ( defined $arg{$arg_name} | ||||||||||||||||||||||||||||
| 144 | ? $ENV{$env_name} = $arg{$arg_name} | ||||||||||||||||||||||||||||
| 145 | 0 | ▶ | : delete $ENV{$env_name} | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 146 | ); | ||||||||||||||||||||||||||||
| 147 | } | ||||||||||||||||||||||||||||
| 148 | } | ||||||||||||||||||||||||||||
| 149 | |||||||||||||||||||||||||||||
| 150 | # function paramaters | ||||||||||||||||||||||||||||
| 151 | 0 | my $command = $arg{'command'}; | |||||||||||||||||||||||||||
| 152 | 0 | my $output_filename = $arg{'output_filename'}; | |||||||||||||||||||||||||||
| 153 | 0 | my $execution_path = $arg{'execution_path'}; | |||||||||||||||||||||||||||
| 154 | 0 | my $stderr_redirect = $arg{'stderr_redirect'}; | |||||||||||||||||||||||||||
| 155 | |||||||||||||||||||||||||||||
| 156 | 0 | my @switches = (ref $arg{'switches'} eq 'ARRAY' ? @{$arg{'switches'}} : $arg{'switches'} ) | |||||||||||||||||||||||||||
| 157 | 0 | ▶ | if exists $arg{'switches'}; | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 158 | |||||||||||||||||||||||||||||
| 159 | 0 | ▶ | local $debug = 1 if ($arg{'debug'} or $ENV{'IN_DEBUG_MODE'}); | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$arg{"debug"} or $ENV{"IN_DEBUG_MODE"}
MC/DC0%0/2
$arg{"debug"} or $ENV{"IN_DEBUG_MODE"}
$arg{"debug"}
$ENV{"IN_DEBUG_MODE"}
Truth table0%0/3
A or B
A= $arg{"debug"}B= $ENV{"IN_DEBUG_MODE"}
| |||||||||||||||||||||||||||||
| 160 | |||||||||||||||||||||||||||||
| 161 | # if there is no command return nothink to do more | ||||||||||||||||||||||||||||
| 162 | 0 | ▶ | return if not defined $command; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 163 | |||||||||||||||||||||||||||||
| 164 | # if more commands then join them together | ||||||||||||||||||||||||||||
| 165 | 0 | ▶ | if (ref $command eq 'ARRAY') { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 166 | 0 | $command = join '; ', @{$command}; | |||||||||||||||||||||||||||
| 167 | } | ||||||||||||||||||||||||||||
| 168 | 0 | ▶ | $command .= ';' if ($command !~ m{;\s*$}xms); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 169 | |||||||||||||||||||||||||||||
| 170 | |||||||||||||||||||||||||||||
| 171 | 0 | ▶ | if (defined $output_filename) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 172 | 0 | push(@switches, '-o', '"'.$output_filename.'"') | |||||||||||||||||||||||||||
| 173 | } | ||||||||||||||||||||||||||||
| 174 | |||||||||||||||||||||||||||||
| 175 | # redirect stderr to stdout | ||||||||||||||||||||||||||||
| 176 | 0 | ▶ | open(STDERR, ">&STDOUT") if (defined $stderr_redirect); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 177 | |||||||||||||||||||||||||||||
| 178 | # chdir if needed | ||||||||||||||||||||||||||||
| 179 | 0 | ▶ | if (defined $execution_path) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 180 | 0 | ▶ | print STDERR '+ cd ', $execution_path, "\n" if $debug; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 181 | 0 | chdir($execution_path); | |||||||||||||||||||||||||||
| 182 | } | ||||||||||||||||||||||||||||
| 183 | |||||||||||||||||||||||||||||
| 184 | 0 | my @ret = execute( | |||||||||||||||||||||||||||
| 185 | 'psql', | ||||||||||||||||||||||||||||
| 186 | @switches, | ||||||||||||||||||||||||||||
| 187 | '-c', | ||||||||||||||||||||||||||||
| 188 | $command, | ||||||||||||||||||||||||||||
| 189 | ); | ||||||||||||||||||||||||||||
| 190 | |||||||||||||||||||||||||||||
| 191 | 0 | ▶ | return @ret if defined wantarray; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 192 | 0 | return join('', @ret); | |||||||||||||||||||||||||||
| 193 | } | ||||||||||||||||||||||||||||
| 194 | |||||||||||||||||||||||||||||
| 195 | |||||||||||||||||||||||||||||
| 196 | =head2 execute | ||||||||||||||||||||||||||||
| 197 | |||||||||||||||||||||||||||||
| 198 | Executes command as system and return output. | ||||||||||||||||||||||||||||
| 199 | |||||||||||||||||||||||||||||
| 200 | In debug mode prints command to stderr. | ||||||||||||||||||||||||||||
| 201 | |||||||||||||||||||||||||||||
| 202 | =cut | ||||||||||||||||||||||||||||
| 203 | |||||||||||||||||||||||||||||
| 204 | 0 | ▶ | sub execute { | ||||||||||||||||||||||||||
|
Subroutine
execute: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||||||||||||||||||||
| 205 | 0 | my $cmd = shell_quote @_; | |||||||||||||||||||||||||||
| 206 | 0 | ▶ | print STDERR '+ ', $cmd, "\n" if $debug; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 207 | 0 | my @ret = `$cmd`; | |||||||||||||||||||||||||||
| 208 | } | ||||||||||||||||||||||||||||
| 209 | |||||||||||||||||||||||||||||
| 210 | 1 | 1; | |||||||||||||||||||||||||||
| 211 | |||||||||||||||||||||||||||||
| 212 | |||||||||||||||||||||||||||||
| 213 | =head1 SEE ALSO | ||||||||||||||||||||||||||||
| 214 | |||||||||||||||||||||||||||||
| 215 | Test::Environment L<http://search.cpan.org/perldoc?Test::Environment> | ||||||||||||||||||||||||||||
| 216 | |||||||||||||||||||||||||||||
| 217 | =head1 AUTHOR | ||||||||||||||||||||||||||||
| 218 | |||||||||||||||||||||||||||||
| 219 | Jozef Kutej - E<lt>jozef@kutej.netE<gt> | ||||||||||||||||||||||||||||
| 220 | |||||||||||||||||||||||||||||
| 221 | =cut | ||||||||||||||||||||||||||||