| 1 | package # hide from PAUSE | ||
| 2 | App::DBBrowser::CreateDropAttach; | ||
| 3 | |||
| 4 | use warnings; | ||
| 5 | use strict; | ||
| 6 | use 5.016; | ||
| 7 | |||
| 8 | use Term::Choose qw(); | ||
| 9 | |||
| 10 | use App::DBBrowser::Auxil; | ||
| 11 | use App::DBBrowser::CreateDropAttach::AttachDB; | ||
| 12 | use App::DBBrowser::CreateDropAttach::CreateTable; | ||
| 13 | use App::DBBrowser::CreateDropAttach::DropTable; | ||
| 14 | |||
| 15 | |||
| 16 | sub new { | ||
| 17 | my ( $class, $info, $options, $d ) = @_; | ||
| 18 | bless { | ||
| 19 | i => $info, | ||
| 20 | o => $options, | ||
| 21 | d => $d | ||
| 22 | }, $class; | ||
| 23 | } | ||
| 24 | |||
| 25 | |||
| 26 | sub create_drop_or_attach { | ||
| 27 | my ( $sf ) = @_; | ||
| 28 | my $ax = App::DBBrowser::Auxil->new( $sf->{i}, $sf->{o}, $sf->{d} ); | ||
| 29 | my $tc = Term::Choose->new( $sf->{i}{tc_default} ); | ||
| 30 | state $old_idx_cda = 0; | ||
| 31 | |||
| 32 | CREATE_DROP_ATTACH: while ( 1 ) { | ||
| 33 | my ( $create_table, $drop_table, $create_view, $drop_view, $attach_databases, $detach_databases ) = ( | ||
| 34 | '- Create TABLE', '- Drop TABLE', '- Create VIEV', '- Drop VIEW', '- Attach DB', '- Detach DB' | ||
| 35 | ); | ||
| 36 | my @entries; | ||
| 37 | push @entries, $create_table if $sf->{o}{enable}{create_table}; | ||
| 38 | push @entries, $drop_table if $sf->{o}{enable}{drop_table}; | ||
| 39 | push @entries, $create_view if $sf->{o}{enable}{create_view}; | ||
| 40 | push @entries, $drop_view if $sf->{o}{enable}{drop_view}; | ||
| 41 | if ( $sf->{i}{dbms} =~ /^(?:SQLite|DuckDB)\z/ ) { | ||
| 42 | push @entries, $attach_databases; | ||
| 43 | push @entries, $detach_databases; | ||
| 44 | } | ||
| 45 | if ( ! @entries ) { | ||
| 46 | return; | ||
| 47 | } | ||
| 48 | my $prompt = $sf->{d}{db_string}; | ||
| 49 | my @pre = ( undef ); | ||
| 50 | my $menu = [ @pre, @entries ]; | ||
| 51 | # Choose | ||
| 52 | my $idx = $tc->choose( | ||
| 53 | $menu, | ||
| 54 | { %{$sf->{i}{lyt_v}}, prompt => $prompt, index => 1, default => $old_idx_cda, undef => ' <=' } | ||
| 55 | ); | ||
| 56 | if ( ! defined $idx || ! defined $menu->[$idx] ) { | ||
| 57 | return; | ||
| 58 | } | ||
| 59 | if ( $sf->{o}{G}{menu_memory} ) { | ||
| 60 | if ( $old_idx_cda == $idx && ! $ENV{TC_RESET_AUTO_UP} ) { | ||
| 61 | $old_idx_cda = 0; | ||
| 62 | next CREATE_DROP_ATTACH; | ||
| 63 | } | ||
| 64 | $old_idx_cda = $idx; | ||
| 65 | } | ||
| 66 | my $choice = $menu->[$idx]; | ||
| 67 | if ( $choice =~ /^-\ Create/i ) { | ||
| 68 | my $ct = App::DBBrowser::CreateDropAttach::CreateTable->new( $sf->{i}, $sf->{o}, $sf->{d} ); | ||
| 69 | if ( $choice eq $create_table ) { | ||
| 70 | if ( ! eval { $ct->create_table(); 1 } ) { | ||
| 71 | $ax->print_error_message( $@ ); | ||
| 72 | } | ||
| 73 | } | ||
| 74 | elsif ( $choice eq $create_view ) { | ||
| 75 | if ( ! eval { $ct->create_view(); 1 } ) { | ||
| 76 | $ax->print_error_message( $@ ); | ||
| 77 | } | ||
| 78 | } | ||
| 79 | return 1; | ||
| 80 | } | ||
| 81 | elsif ( $choice =~ /^-\ Drop/i ) { | ||
| 82 | my $dt = App::DBBrowser::CreateDropAttach::DropTable->new( $sf->{i}, $sf->{o}, $sf->{d} ); | ||
| 83 | if ( $choice eq $drop_table ) { | ||
| 84 | if ( ! eval { $dt->drop_table(); 1 } ) { | ||
| 85 | $ax->print_error_message( $@ ); | ||
| 86 | } | ||
| 87 | } | ||
| 88 | elsif ( $choice eq $drop_view ) { | ||
| 89 | if ( ! eval { $dt->drop_view(); 1 } ) { | ||
| 90 | $ax->print_error_message( $@ ); | ||
| 91 | } | ||
| 92 | } | ||
| 93 | return 1; | ||
| 94 | } | ||
| 95 | elsif ( $choice =~ /^-\ (?:Attach|Detach)/ ) { | ||
| 96 | my $att = App::DBBrowser::CreateDropAttach::AttachDB->new( $sf->{i}, $sf->{o}, $sf->{d} ); | ||
| 97 | my $changed; | ||
| 98 | if ( $choice eq $attach_databases ) { | ||
| 99 | if ( ! eval { $changed = $att->attach_db(); 1 } ) { | ||
| 100 | $ax->print_error_message( $@ ); | ||
| 101 | } | ||
| 102 | } | ||
| 103 | elsif ( $choice eq $detach_databases ) { | ||
| 104 | if ( ! eval { $changed = $att->detach_db(); 1 } ) { | ||
| 105 | $ax->print_error_message( $@ ); | ||
| 106 | } | ||
| 107 | } | ||
| 108 | if ( $changed ) { | ||
| 109 | return 2; | ||
| 110 | } | ||
| 111 | } | ||
| 112 | } | ||
| 113 | } | ||
| 114 | |||
| 115 | |||
| 116 | |||
| 117 | |||
| 118 | |||
| 119 | |||
| 120 | |||
| 121 | |||
| 122 | |||
| 123 | 1; | ||
| 124 | |||
| 125 | |||
| 126 | __END__ |