| 1 | package # hide from PAUSE | ||
| 2 | App::DBBrowser::DB::Pg; | ||
| 3 | |||
| 4 | use warnings; | ||
| 5 | use strict; | ||
| 6 | use 5.016; | ||
| 7 | |||
| 8 | use DBI qw(); | ||
| 9 | |||
| 10 | use App::DBBrowser::Credentials; | ||
| 11 | |||
| 12 | |||
| 13 | sub new { | ||
| 14 | my ( $class, $info, $opt ) = @_; | ||
| 15 | my $sf = { | ||
| 16 | i => $info, | ||
| 17 | o => $opt | ||
| 18 | }; | ||
| 19 | bless $sf, $class; | ||
| 20 | } | ||
| 21 | |||
| 22 | |||
| 23 | sub get_db_driver { | ||
| 24 | my ( $sf ) = @_; | ||
| 25 | return 'Pg'; | ||
| 26 | } | ||
| 27 | |||
| 28 | |||
| 29 | sub get_db_handle { | ||
| 30 | my ( $sf, $db ) = @_; | ||
| 31 | my $cred = App::DBBrowser::Credentials->new( $sf->{i}, $sf->{o} ); | ||
| 32 | my $dsn = "dbi:Pg:dbname=$db"; | ||
| 33 | my $show_sofar = 'DB '. $db; | ||
| 34 | my $host = $cred->get_login( 'host', $show_sofar ); | ||
| 35 | if ( length $host ) { | ||
| 36 | $show_sofar .= "\n" . 'Host: ' . $host; | ||
| 37 | $dsn .= ";host=$host"; | ||
| 38 | my $port = $cred->get_login( 'port', $show_sofar ); | ||
| 39 | if ( length $port ) { | ||
| 40 | $show_sofar .= "\n" . 'Port: ' . $port; | ||
| 41 | $dsn .= ";port=$port" if length $port; | ||
| 42 | } | ||
| 43 | } | ||
| 44 | my $user = $cred->get_login( 'user', $show_sofar ); | ||
| 45 | $show_sofar .= "\n" . 'User: ' . $user if defined $user; | ||
| 46 | my $passwd = $cred->get_login( 'pass', $show_sofar ); | ||
| 47 | my $dbh = DBI->connect( $dsn, $user, $passwd, { | ||
| 48 | PrintError => 0, | ||
| 49 | RaiseError => 1, | ||
| 50 | AutoCommit => 1, | ||
| 51 | ShowErrorStatement => 1, | ||
| 52 | %{$sf->{o}{connect_attr}//{}}, ## | ||
| 53 | } ); | ||
| 54 | return $dbh; | ||
| 55 | } | ||
| 56 | |||
| 57 | |||
| 58 | sub get_databases { | ||
| 59 | my ( $sf ) = @_; | ||
| 60 | return \@ARGV if @ARGV; | ||
| 61 | |||
| 62 | #my $ax = App::DBBrowser::Auxil->new( {}, {}, {} ); | ||
| 63 | #my $file_Pg_dbs = sprintf $sf->{i}{db_cache_file_fmt}, $sf->{i}{plugin}; | ||
| 64 | #my $cache = $ax->read_json( $file_Pg_dbs ) // {}; | ||
| 65 | #if ( ! $sf->{i}{search} && @{$cache->{user_databases}//[]} ) { | ||
| 66 | # return $cache->{user_databases}, $cache->{system_databases}; | ||
| 67 | #} | ||
| 68 | |||
| 69 | my $dbh = $sf->get_db_handle( 'postgres' ); | ||
| 70 | my $stmt = "SELECT pg_catalog.quote_ident(datname) FROM pg_catalog.pg_database ORDER BY datname"; | ||
| 71 | my $databases = $dbh->selectcol_arrayref( $stmt, {}, () ); | ||
| 72 | $dbh->disconnect(); | ||
| 73 | |||
| 74 | my $user_db = []; | ||
| 75 | my $system_db = []; | ||
| 76 | for my $database ( @$databases ) { | ||
| 77 | if ( $database =~ /^(?:postgres|template0|template1)\z/i ) { | ||
| 78 | push @$system_db, $database; | ||
| 79 | } | ||
| 80 | else { | ||
| 81 | push @$user_db, $database; | ||
| 82 | } | ||
| 83 | } | ||
| 84 | |||
| 85 | #$cache = { | ||
| 86 | # user_databases => $user_db, | ||
| 87 | # system_databases => $system_db, | ||
| 88 | #}; | ||
| 89 | #$ax->write_json( $file_Pg_dbs, $cache ); | ||
| 90 | |||
| 91 | return $user_db, $system_db; | ||
| 92 | } | ||
| 93 | |||
| 94 | |||
| 95 | |||
| 96 | |||
| 97 | |||
| 98 | 1; | ||
| 99 | |||
| 100 | |||
| 101 | __END__ |