« DBBrowser/Table/Extensions/Case.pm
↑ Summary
DBBrowser/Table/Extensions/ScalarFunctions/GetArguments.pm »
| 1 | package # hide from PAUSE | ||
| 2 | App::DBBrowser::DB::Firebird; | ||
| 3 | |||
| 4 | use warnings; | ||
| 5 | use strict; | ||
| 6 | use 5.016; | ||
| 7 | |||
| 8 | use File::Basename qw( basename ); | ||
| 9 | use File::Spec::Functions qw( catfile ); | ||
| 10 | |||
| 11 | use DBI qw(); | ||
| 12 | use List::MoreUtils qw( uniq ); | ||
| 13 | |||
| 14 | use Term::Choose::Util qw(); | ||
| 15 | |||
| 16 | use App::DBBrowser::Credentials; | ||
| 17 | |||
| 18 | |||
| 19 | sub new { | ||
| 20 | my ( $class, $info, $opt ) = @_; | ||
| 21 | my $sf = { | ||
| 22 | i => $info, | ||
| 23 | o => $opt | ||
| 24 | }; | ||
| 25 | bless $sf, $class; | ||
| 26 | } | ||
| 27 | |||
| 28 | |||
| 29 | sub get_db_driver { | ||
| 30 | my ( $sf ) = @_; | ||
| 31 | return 'Firebird'; | ||
| 32 | } | ||
| 33 | |||
| 34 | |||
| 35 | sub get_db_handle { | ||
| 36 | my ( $sf, $db ) = @_; | ||
| 37 | my $cred = App::DBBrowser::Credentials->new( $sf->{i}, $sf->{o} ); | ||
| 38 | my $dsn = "dbi:Firebird:dbname=$db"; | ||
| 39 | my $show_sofar = 'DB '. basename( $db ); | ||
| 40 | my $host = $cred->get_login( 'host', $show_sofar ); | ||
| 41 | if ( defined $host ) { | ||
| 42 | $show_sofar .= "\n" . 'Host: ' . $host; | ||
| 43 | $dsn .= ";host=$host" if length $host; | ||
| 44 | } | ||
| 45 | my $port = $cred->get_login( 'port', $show_sofar ); | ||
| 46 | if ( defined $port ) { | ||
| 47 | $show_sofar .= "\n" . 'Port: ' . $port; | ||
| 48 | $dsn .= ";port=$port" if length $port; | ||
| 49 | } | ||
| 50 | my $connect_attr = $sf->{o}{connect_attr}; | ||
| 51 | my $dbh_attributes = {}; | ||
| 52 | |||
| 53 | for my $key ( keys %$connect_attr ) { | ||
| 54 | if ( ! length $connect_attr->{$key} ) { | ||
| 55 | next; | ||
| 56 | } | ||
| 57 | elsif ( $key =~ /^(?:ib_dialect|ib_role|ib_charset)\z/ ) { | ||
| 58 | $dsn .= ";$key=$connect_attr->{$key}"; | ||
| 59 | } | ||
| 60 | else { | ||
| 61 | $dbh_attributes->{$key} = $connect_attr->{$key}; | ||
| 62 | } | ||
| 63 | } | ||
| 64 | my $user = $cred->get_login( 'user', $show_sofar ); | ||
| 65 | $show_sofar .= "\n" . 'User: ' . $user if defined $user; | ||
| 66 | my $passwd = $cred->get_login( 'pass', $show_sofar ); | ||
| 67 | my $dbh = DBI->connect( $dsn, $user, $passwd, { | ||
| 68 | PrintError => 0, | ||
| 69 | RaiseError => 1, | ||
| 70 | AutoCommit => 1, | ||
| 71 | ShowErrorStatement => 1, | ||
| 72 | %$dbh_attributes, | ||
| 73 | } ); | ||
| 74 | return $dbh; | ||
| 75 | } | ||
| 76 | |||
| 77 | |||
| 78 | sub get_databases { | ||
| 79 | my ( $sf ) = @_; | ||
| 80 | return \@ARGV if @ARGV; | ||
| 81 | my $file_firebird_dbs = sprintf $sf->{i}{db_cache_file_fmt}, $sf->{i}{plugin}; | ||
| 82 | my $ax = App::DBBrowser::Auxil->new( {}, {}, {} ); | ||
| 83 | my $tc = Term::Choose->new( $sf->{i}{tc_default} ); | ||
| 84 | my $saved_databases = $ax->read_json( $file_firebird_dbs ) // []; | ||
| 85 | my $databases = [ @$saved_databases ]; | ||
| 86 | if ( ! $sf->{i}{search} && @$databases ) { | ||
| 87 | return $databases; | ||
| 88 | } | ||
| 89 | my @pre = ( undef ); | ||
| 90 | my ( $confirm, $add, $remove ) = ( ' Confirm', '- Add', '- Remove' ); | ||
| 91 | my $changed = 0; | ||
| 92 | |||
| 93 | while ( 1 ) { | ||
| 94 | my $info = join( "\n", 'Databases: ', @$databases, '' ); | ||
| 95 | # Choose | ||
| 96 | my $choice = $tc->choose( | ||
| 97 | [ @pre, $confirm, $add, $remove ], | ||
| 98 | { %{$sf->{i}{lyt_v}}, info => $info, undef => ' <=' } | ||
| 99 | ); | ||
| 100 | $ax->print_sql_info( $info ); | ||
| 101 | if ( ! defined $choice ) { | ||
| 102 | return $saved_databases; | ||
| 103 | } | ||
| 104 | elsif ( $choice eq $confirm ) { | ||
| 105 | if ( $changed ) { | ||
| 106 | $ax->write_json( $file_firebird_dbs, $databases ); | ||
| 107 | } | ||
| 108 | return $databases; | ||
| 109 | } | ||
| 110 | elsif ( $choice eq $add ) { | ||
| 111 | my $tu = Term::Choose::Util->new( $sf->{i}{tcu_default} ); | ||
| 112 | my $new = $tu->choose_a_file( { | ||
| 113 | info => $info, | ||
| 114 | prompt => 'Choose the database location:', | ||
| 115 | cs_label => 'Database: ', | ||
| 116 | prompt2 => 'Your choice:' | ||
| 117 | } ); | ||
| 118 | if ( length $new ) { | ||
| 119 | $databases = [ uniq sort( @$databases, $new ) ]; | ||
| 120 | $changed++; | ||
| 121 | } | ||
| 122 | } | ||
| 123 | elsif ( $choice eq $remove ) { | ||
| 124 | my $info = 'Remove database'; | ||
| 125 | my @pre = ( undef ); | ||
| 126 | my $idx = $tc->choose( [ @pre, @$databases ], { info => $info, index => 1, layout => 2 } ); | ||
| 127 | if ( $idx ) { | ||
| 128 | splice( @$databases, $idx - @pre, 1 ); | ||
| 129 | $changed++; | ||
| 130 | } | ||
| 131 | } | ||
| 132 | } | ||
| 133 | } | ||
| 134 | |||
| 135 | |||
| 136 | |||
| 137 | |||
| 138 | |||
| 139 | |||
| 140 | 1; | ||
| 141 | |||
| 142 | __END__ |