| 1 | # ----------------------------------------------------------------------------- | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | =encoding utf8 | ||||||||||||||||||||||||||||
| 4 | |||||||||||||||||||||||||||||
| 5 | =head1 NAME | ||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||
| 7 | Quiq::PostgreSql::Psql - Wrapper für psql | ||||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||
| 9 | =head1 BASE CLASS | ||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||
| 11 | L<Quiq::Object> | ||||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||
| 13 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||
| 15 | use Quiq::PostgreSql::Psql; | ||||||||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||||||
| 17 | Quiq::PostgreSql::Psql->run($database,@opt); | ||||||||||||||||||||||||||||
| 18 | |||||||||||||||||||||||||||||
| 19 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||
| 20 | |||||||||||||||||||||||||||||
| 21 | Die Klasse stellt einen Wrapper für den PostgreSQL-Client psql dar. | ||||||||||||||||||||||||||||
| 22 | |||||||||||||||||||||||||||||
| 23 | =cut | ||||||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||||||
| 25 | # ----------------------------------------------------------------------------- | ||||||||||||||||||||||||||||
| 26 | |||||||||||||||||||||||||||||
| 27 | package Quiq::PostgreSql::Psql; | ||||||||||||||||||||||||||||
| 28 | 1 | ▶ | use base qw/Quiq::Object/; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 29 | |||||||||||||||||||||||||||||
| 30 | 1 | ▶ | use v5.10; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 31 | 1 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 32 | 1 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 33 | |||||||||||||||||||||||||||||
| 34 | 1 | our $VERSION = '1.240'; | |||||||||||||||||||||||||||
| 35 | |||||||||||||||||||||||||||||
| 36 | 1 | ▶ | use Quiq::Udl; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 37 | 1 | ▶ | use Quiq::CommandLine; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 38 | 1 | ▶ | use Expect (); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 39 | |||||||||||||||||||||||||||||
| 40 | # ----------------------------------------------------------------------------- | ||||||||||||||||||||||||||||
| 41 | |||||||||||||||||||||||||||||
| 42 | =head1 METHODS | ||||||||||||||||||||||||||||
| 43 | |||||||||||||||||||||||||||||
| 44 | =head2 Klassenmethoden | ||||||||||||||||||||||||||||
| 45 | |||||||||||||||||||||||||||||
| 46 | =head3 run() - Starte psql-Sitzung ohne Passworteingabe | ||||||||||||||||||||||||||||
| 47 | |||||||||||||||||||||||||||||
| 48 | =head4 Synopsis | ||||||||||||||||||||||||||||
| 49 | |||||||||||||||||||||||||||||
| 50 | $exitCode = $class->run($database); | ||||||||||||||||||||||||||||
| 51 | |||||||||||||||||||||||||||||
| 52 | =head4 Arguments | ||||||||||||||||||||||||||||
| 53 | |||||||||||||||||||||||||||||
| 54 | =over 4 | ||||||||||||||||||||||||||||
| 55 | |||||||||||||||||||||||||||||
| 56 | =item $database | ||||||||||||||||||||||||||||
| 57 | |||||||||||||||||||||||||||||
| 58 | Name der Datenbank oder der Universal Database Locator (UDL). | ||||||||||||||||||||||||||||
| 59 | Der Name muss in der Datenbank-Konfigurationsdatei definiert sein. | ||||||||||||||||||||||||||||
| 60 | |||||||||||||||||||||||||||||
| 61 | =back | ||||||||||||||||||||||||||||
| 62 | |||||||||||||||||||||||||||||
| 63 | =head4 Options | ||||||||||||||||||||||||||||
| 64 | |||||||||||||||||||||||||||||
| 65 | =over 4 | ||||||||||||||||||||||||||||
| 66 | |||||||||||||||||||||||||||||
| 67 | =item -command => $cmd | ||||||||||||||||||||||||||||
| 68 | |||||||||||||||||||||||||||||
| 69 | Führe Kommando $cmd aus und terminiere die Verbindung. | ||||||||||||||||||||||||||||
| 70 | |||||||||||||||||||||||||||||
| 71 | =item -echo => $bool (Default: I<wenn -script 1, sonst 0>) | ||||||||||||||||||||||||||||
| 72 | |||||||||||||||||||||||||||||
| 73 | Gib alle Kommandos, die an den Server geschickt werden, auf stdout aus. | ||||||||||||||||||||||||||||
| 74 | |||||||||||||||||||||||||||||
| 75 | =item -log => $file | ||||||||||||||||||||||||||||
| 76 | |||||||||||||||||||||||||||||
| 77 | Logge Sitzung nach Datei $file. | ||||||||||||||||||||||||||||
| 78 | |||||||||||||||||||||||||||||
| 79 | =item -script => $file | ||||||||||||||||||||||||||||
| 80 | |||||||||||||||||||||||||||||
| 81 | Führe SQL-Skript $file aus und terminiere die Verbindung. | ||||||||||||||||||||||||||||
| 82 | |||||||||||||||||||||||||||||
| 83 | =item -showInternal => $bool (Default: 0) | ||||||||||||||||||||||||||||
| 84 | |||||||||||||||||||||||||||||
| 85 | Gib die Queries aus, die psql im Zusammenhang mit Backslash-Kommandos | ||||||||||||||||||||||||||||
| 86 | intern ausführt. | ||||||||||||||||||||||||||||
| 87 | |||||||||||||||||||||||||||||
| 88 | =item -stopOnError => $bool (Default: I<wenn -script 1, sonst 0>) | ||||||||||||||||||||||||||||
| 89 | |||||||||||||||||||||||||||||
| 90 | Terminiere beim ersten Fehler. | ||||||||||||||||||||||||||||
| 91 | |||||||||||||||||||||||||||||
| 92 | =item -debug => $bool (Default: 0) | ||||||||||||||||||||||||||||
| 93 | |||||||||||||||||||||||||||||
| 94 | Gib das ausgeführte psql-Kommando auf STDOUT aus. | ||||||||||||||||||||||||||||
| 95 | |||||||||||||||||||||||||||||
| 96 | =back | ||||||||||||||||||||||||||||
| 97 | |||||||||||||||||||||||||||||
| 98 | =head4 Returns | ||||||||||||||||||||||||||||
| 99 | |||||||||||||||||||||||||||||
| 100 | Bei interaktiver Sitzung 0. Bei Skript- oder Kommando-Ausführung | ||||||||||||||||||||||||||||
| 101 | wie bei psql(1). | ||||||||||||||||||||||||||||
| 102 | |||||||||||||||||||||||||||||
| 103 | =head4 Description | ||||||||||||||||||||||||||||
| 104 | |||||||||||||||||||||||||||||
| 105 | Rufe psql auf und führe eine Anmeldung durch, auch bei | ||||||||||||||||||||||||||||
| 106 | Passwort-Authentisierung. Die Datenbank kann per Name kontaktiert werden | ||||||||||||||||||||||||||||
| 107 | kann, wenn der UDL in die Konfiguration (s. Quiq::Database::Config) | ||||||||||||||||||||||||||||
| 108 | eingetragen wurde. | ||||||||||||||||||||||||||||
| 109 | |||||||||||||||||||||||||||||
| 110 | =head4 Example | ||||||||||||||||||||||||||||
| 111 | |||||||||||||||||||||||||||||
| 112 | $ perl -MQuiq::PostgreSql::Psql -E 'Quiq::PostgreSql::Psql->run("test")' | ||||||||||||||||||||||||||||
| 113 | Password for user xv882js: | ||||||||||||||||||||||||||||
| 114 | Pager usage is off. | ||||||||||||||||||||||||||||
| 115 | Timing is on. | ||||||||||||||||||||||||||||
| 116 | psql (8.2.15) | ||||||||||||||||||||||||||||
| 117 | SSL connection (cipher: DHE-RSA-AES256-SHA, bits: 256) | ||||||||||||||||||||||||||||
| 118 | Type "help" for help. | ||||||||||||||||||||||||||||
| 119 | |||||||||||||||||||||||||||||
| 120 | dsstest=> | ||||||||||||||||||||||||||||
| 121 | |||||||||||||||||||||||||||||
| 122 | =cut | ||||||||||||||||||||||||||||
| 123 | |||||||||||||||||||||||||||||
| 124 | # ----------------------------------------------------------------------------- | ||||||||||||||||||||||||||||
| 125 | |||||||||||||||||||||||||||||
| 126 | 0 | ▶ | sub run { | ||||||||||||||||||||||||||
|
Subroutine
run: not called CC=13 SCAR=52.0
Pod
documented
| |||||||||||||||||||||||||||||
| 127 | 0 | my ($class,$database) = splice @_,0,2; | |||||||||||||||||||||||||||
| 128 | |||||||||||||||||||||||||||||
| 129 | # Options | ||||||||||||||||||||||||||||
| 130 | |||||||||||||||||||||||||||||
| 131 | 0 | my $command = undef; | |||||||||||||||||||||||||||
| 132 | 0 | my $debug = 0; | |||||||||||||||||||||||||||
| 133 | 0 | my $echo = 0; | |||||||||||||||||||||||||||
| 134 | 0 | my $log = undef; | |||||||||||||||||||||||||||
| 135 | 0 | my $script = undef; | |||||||||||||||||||||||||||
| 136 | 0 | my $showInternal = 0; | |||||||||||||||||||||||||||
| 137 | 0 | my $stopOnError = 0; | |||||||||||||||||||||||||||
| 138 | |||||||||||||||||||||||||||||
| 139 | 0 | $class->parameters(\@_, | |||||||||||||||||||||||||||
| 140 | -command => \$command, | ||||||||||||||||||||||||||||
| 141 | -debug => \$debug, | ||||||||||||||||||||||||||||
| 142 | -echo => \$echo, | ||||||||||||||||||||||||||||
| 143 | -log => \$log, | ||||||||||||||||||||||||||||
| 144 | -script => \$script, | ||||||||||||||||||||||||||||
| 145 | -showInternal => \$showInternal, | ||||||||||||||||||||||||||||
| 146 | -stopOnError => \$stopOnError, | ||||||||||||||||||||||||||||
| 147 | ); | ||||||||||||||||||||||||||||
| 148 | |||||||||||||||||||||||||||||
| 149 | # Führe Operation aus | ||||||||||||||||||||||||||||
| 150 | |||||||||||||||||||||||||||||
| 151 | 0 | my $udl = Quiq::Udl->new($database); | |||||||||||||||||||||||||||
| 152 | 0 | ▶ | if ($udl->dbms ne 'postgresql') { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 153 | 0 | $class->throw( | |||||||||||||||||||||||||||
| 154 | 'PSQL-00001: Not a PostgeSQL UDL', | ||||||||||||||||||||||||||||
| 155 | Udl => $udl->asString, | ||||||||||||||||||||||||||||
| 156 | ); | ||||||||||||||||||||||||||||
| 157 | } | ||||||||||||||||||||||||||||
| 158 | |||||||||||||||||||||||||||||
| 159 | 0 | my $c = Quiq::CommandLine->new('psql'); | |||||||||||||||||||||||||||
| 160 | 0 | for my $opt (qw/user host port/) { | |||||||||||||||||||||||||||
| 161 | 0 | ▶ | if (my $val = $udl->$opt) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 162 | 0 | $c->addLongOption( | |||||||||||||||||||||||||||
| 163 | "--$opt" => $val, | ||||||||||||||||||||||||||||
| 164 | ); | ||||||||||||||||||||||||||||
| 165 | } | ||||||||||||||||||||||||||||
| 166 | } | ||||||||||||||||||||||||||||
| 167 | 0 | $c->addBoolOption('--echo-hidden'=>$showInternal); | |||||||||||||||||||||||||||
| 168 | 0 | ▶ | if ($command) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 169 | 0 | $c->addOption(-P=>'pager=off'); | |||||||||||||||||||||||||||
| 170 | 0 | $c->addLongOption('--command'=>$command); | |||||||||||||||||||||||||||
| 171 | } | ||||||||||||||||||||||||||||
| 172 | 0 | ▶ | if ($script) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 173 | 0 | $c->addLongOption('--file'=>$script); | |||||||||||||||||||||||||||
| 174 | 0 | $stopOnError = 1; | |||||||||||||||||||||||||||
| 175 | 0 | $echo = 1; | |||||||||||||||||||||||||||
| 176 | } | ||||||||||||||||||||||||||||
| 177 | 0 | ▶ | if ($stopOnError) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 178 | 0 | $c->addLongOption('--set'=>'ON_ERROR_STOP=1'); | |||||||||||||||||||||||||||
| 179 | } | ||||||||||||||||||||||||||||
| 180 | 0 | $c->addBoolOption('--echo-all'=>$echo); | |||||||||||||||||||||||||||
| 181 | 0 | $c->addLongOption('--log-file'=>$log); | |||||||||||||||||||||||||||
| 182 | 0 | ▶ | if (my $database = $udl->db) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 183 | 0 | $c->addArgument($database); | |||||||||||||||||||||||||||
| 184 | } | ||||||||||||||||||||||||||||
| 185 | |||||||||||||||||||||||||||||
| 186 | 0 | my $cmd = $c->command; | |||||||||||||||||||||||||||
| 187 | # if ($debug) { | ||||||||||||||||||||||||||||
| 188 | 0 | say $cmd; | |||||||||||||||||||||||||||
| 189 | # } | ||||||||||||||||||||||||||||
| 190 | |||||||||||||||||||||||||||||
| 191 | 0 | my $exp = Expect->new; | |||||||||||||||||||||||||||
| 192 | 0 | ▶ | if ($debug) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 193 | 0 | $exp->exp_internal(1); | |||||||||||||||||||||||||||
| 194 | } | ||||||||||||||||||||||||||||
| 195 | 0 | ▶ | $exp->spawn($cmd) || do { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 196 | 0 | $class->throw( | |||||||||||||||||||||||||||
| 197 | 'EXPECT-00099: Cannot spawn command', | ||||||||||||||||||||||||||||
| 198 | Command => $cmd, | ||||||||||||||||||||||||||||
| 199 | ); | ||||||||||||||||||||||||||||
| 200 | }; | ||||||||||||||||||||||||||||
| 201 | |||||||||||||||||||||||||||||
| 202 | # Anmeldung. Wir unterscheiden drei Fälle. | ||||||||||||||||||||||||||||
| 203 | |||||||||||||||||||||||||||||
| 204 | 0 | ▶ | my $interact = $command || $script? 0: 1; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$command or $script
MC/DC0%0/2
$command or $script
$command
$script
Truth table0%0/3
A or B
A= $commandB= $script
| |||||||||||||||||||||||||||||
| 205 | $exp->expect(3,[ | ||||||||||||||||||||||||||||
| 206 | # mit Passwort | ||||||||||||||||||||||||||||
| 207 | 0 | ▶ | -re => 'Password.*?:',sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 208 | 0 | my $exp = shift; | |||||||||||||||||||||||||||
| 209 | 0 | $exp->send($udl->password."\n"); | |||||||||||||||||||||||||||
| 210 | }, | ||||||||||||||||||||||||||||
| 211 | ],[ | ||||||||||||||||||||||||||||
| 212 | # ohne Passwort | ||||||||||||||||||||||||||||
| 213 | 0 | ▶ | -re => '^psql ',sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 214 | }, | ||||||||||||||||||||||||||||
| 215 | ],[ | ||||||||||||||||||||||||||||
| 216 | # Verbindung kommt nicht zustande | ||||||||||||||||||||||||||||
| 217 | 0 | ▶ | -re => '^psql:',sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 218 | # Ende der Kommunikation | ||||||||||||||||||||||||||||
| 219 | 0 | $interact = 0; | |||||||||||||||||||||||||||
| 220 | }, | ||||||||||||||||||||||||||||
| 221 | 0 | ]); | |||||||||||||||||||||||||||
| 222 | |||||||||||||||||||||||||||||
| 223 | 0 | ▶ | if ($interact) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 224 | # Benutzer-Interaktion | ||||||||||||||||||||||||||||
| 225 | |||||||||||||||||||||||||||||
| 226 | 0 | $exp->interact; | |||||||||||||||||||||||||||
| 227 | 0 | return 0; | |||||||||||||||||||||||||||
| 228 | } | ||||||||||||||||||||||||||||
| 229 | |||||||||||||||||||||||||||||
| 230 | 0 | $exp->expect(undef); | |||||||||||||||||||||||||||
| 231 | 0 | return $exp->exitstatus/256; | |||||||||||||||||||||||||||
| 232 | } | ||||||||||||||||||||||||||||
| 233 | |||||||||||||||||||||||||||||
| 234 | # ----------------------------------------------------------------------------- | ||||||||||||||||||||||||||||
| 235 | |||||||||||||||||||||||||||||
| 236 | =head1 VERSION | ||||||||||||||||||||||||||||
| 237 | |||||||||||||||||||||||||||||
| 238 | 1.240 | ||||||||||||||||||||||||||||
| 239 | |||||||||||||||||||||||||||||
| 240 | =head1 AUTHOR | ||||||||||||||||||||||||||||
| 241 | |||||||||||||||||||||||||||||
| 242 | Frank Seitz, L<http://fseitz.de/> | ||||||||||||||||||||||||||||
| 243 | |||||||||||||||||||||||||||||
| 244 | =head1 COPYRIGHT | ||||||||||||||||||||||||||||
| 245 | |||||||||||||||||||||||||||||
| 246 | Copyright (C) 2026 Frank Seitz | ||||||||||||||||||||||||||||
| 247 | |||||||||||||||||||||||||||||
| 248 | =head1 LICENSE | ||||||||||||||||||||||||||||
| 249 | |||||||||||||||||||||||||||||
| 250 | This code is free software; you can redistribute it and/or modify | ||||||||||||||||||||||||||||
| 251 | it under the same terms as Perl itself. | ||||||||||||||||||||||||||||
| 252 | |||||||||||||||||||||||||||||
| 253 | =cut | ||||||||||||||||||||||||||||
| 254 | |||||||||||||||||||||||||||||
| 255 | # ----------------------------------------------------------------------------- | ||||||||||||||||||||||||||||
| 256 | |||||||||||||||||||||||||||||
| 257 | 1 | 1; | |||||||||||||||||||||||||||
| 258 | |||||||||||||||||||||||||||||
| 259 | # eof | ||||||||||||||||||||||||||||