App-DBBrowser

 view release on metacpan or  search on metacpan

lib/App/DBBrowser/CreateDropAttach.pm  view on Meta::CPAN

package # hide from PAUSE
App::DBBrowser::CreateDropAttach;
use warnings;
use strict;
use 5.016;

use Term::Choose qw();

use App::DBBrowser::Auxil;
use App::DBBrowser::CreateDropAttach::AttachDB;
use App::DBBrowser::CreateDropAttach::CreateTable;
use App::DBBrowser::CreateDropAttach::DropTable;


sub new {
    my ( $class, $info, $options, $d ) = @_;
    bless {
        i => $info,
        o => $options,
        d => $d
    }, $class;
}


sub create_drop_or_attach {
    my ( $sf ) = @_;
    my $ax = App::DBBrowser::Auxil->new( $sf->{i}, $sf->{o}, $sf->{d} );
    my $tc = Term::Choose->new( $sf->{i}{tc_default} );
    state $old_idx_cda = 0;

    CREATE_DROP_ATTACH: while ( 1 ) {
        my ( $create_table,    $drop_table,      $create_view,    $drop_view,      $attach_databases, $detach_databases ) = (
             '- Create TABLE', '- Drop TABLE',   '- Create VIEV', '- Drop VIEW',   '- Attach DB',     '- Detach DB'
        );
        my @entries;
        push @entries, $create_table if $sf->{o}{enable}{create_table};
        push @entries, $drop_table   if $sf->{o}{enable}{drop_table};
        push @entries, $create_view  if $sf->{o}{enable}{create_view};
        push @entries, $drop_view    if $sf->{o}{enable}{drop_view};
        if ( $sf->{i}{dbms} =~ /^(?:SQLite|DuckDB)\z/ ) {
            push @entries, $attach_databases;
            push @entries, $detach_databases;
        }
        if ( ! @entries ) {
            return;
        }
        my $prompt = $sf->{d}{db_string};
        my @pre = ( undef );
        my $menu = [ @pre, @entries ];
        # Choose
        my $idx = $tc->choose(
            $menu,
            { %{$sf->{i}{lyt_v}}, prompt => $prompt, index => 1, default => $old_idx_cda, undef => '  <=' }
        );
        if ( ! defined $idx || ! defined $menu->[$idx] ) {
            return;
        }
        if ( $sf->{o}{G}{menu_memory} ) {
            if ( $old_idx_cda == $idx && ! $ENV{TC_RESET_AUTO_UP} ) {
                $old_idx_cda = 0;
                next CREATE_DROP_ATTACH;
            }
            $old_idx_cda = $idx;
        }
        my $choice = $menu->[$idx];
        if ( $choice =~ /^-\ Create/i ) {
            my $ct = App::DBBrowser::CreateDropAttach::CreateTable->new( $sf->{i}, $sf->{o}, $sf->{d} );
            if ( $choice eq $create_table ) {
                if ( ! eval { $ct->create_table(); 1 } ) {
                    $ax->print_error_message( $@ );
                }
            }
            elsif ( $choice eq $create_view ) {
                if ( ! eval { $ct->create_view(); 1 } ) {
                    $ax->print_error_message( $@ );
                }
            }
            return 1;
        }
        elsif ( $choice =~ /^-\ Drop/i ) {
            my $dt = App::DBBrowser::CreateDropAttach::DropTable->new( $sf->{i}, $sf->{o}, $sf->{d} );
            if ( $choice eq $drop_table ) {
                if ( ! eval { $dt->drop_table(); 1 } ) {
                    $ax->print_error_message( $@ );
                }
            }
            elsif ( $choice eq $drop_view ) {
                if ( ! eval { $dt->drop_view(); 1 } ) {
                    $ax->print_error_message( $@ );
                }
            }
            return 1;
        }
        elsif ( $choice =~ /^-\ (?:Attach|Detach)/ ) {
            my $att = App::DBBrowser::CreateDropAttach::AttachDB->new( $sf->{i}, $sf->{o}, $sf->{d} );
            my $changed;
            if ( $choice eq $attach_databases ) {
                if ( ! eval { $changed = $att->attach_db(); 1 } ) {
                    $ax->print_error_message( $@ );
                }
            }
            elsif ( $choice eq $detach_databases ) {
                if ( ! eval { $changed = $att->detach_db(); 1 } ) {
                    $ax->print_error_message( $@ );
                }
            }
            if ( $changed ) {
                return 2;
            }
        }
    }
}



( run in 0.877 second using v1.01-cache-2.11-cpan-6aa56a78535 )