« DBBrowser/Table/Extensions/RearrangeColumns.pm
↑ Summary
DBBrowser/Table/Extensions/ScalarFunctions/Numeric.pm »
| 1 | package # hide from PAUSE | ||
| 2 | App::DBBrowser::CreateDropAttach::DropTable; | ||
| 3 | |||
| 4 | use warnings; | ||
| 5 | use strict; | ||
| 6 | use 5.016; | ||
| 7 | |||
| 8 | use Term::Choose qw(); | ||
| 9 | use Term::Choose::Util qw( insert_sep ); | ||
| 10 | use Term::TablePrint qw(); | ||
| 11 | |||
| 12 | use App::DBBrowser::Auxil; | ||
| 13 | use App::DBBrowser::Table; | ||
| 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 drop_table { | ||
| 27 | my ( $sf ) = @_; | ||
| 28 | return $sf->__choose_drop_item( 'table' ); | ||
| 29 | } | ||
| 30 | |||
| 31 | |||
| 32 | sub drop_view { | ||
| 33 | my ( $sf ) = @_; | ||
| 34 | return $sf->__choose_drop_item( 'view' ); | ||
| 35 | } | ||
| 36 | |||
| 37 | |||
| 38 | sub __choose_drop_item { | ||
| 39 | my ( $sf, $type ) = @_; | ||
| 40 | my $ax = App::DBBrowser::Auxil->new( $sf->{i}, $sf->{o}, $sf->{d} ); | ||
| 41 | my $tc = Term::Choose->new( $sf->{i}{tc_default} ); | ||
| 42 | my $tables = [ grep { $sf->{d}{tables_info}{$_}[3] eq uc $type } @{$sf->{d}{user_table_keys}} ]; | ||
| 43 | if ( ! @$tables ) { | ||
| 44 | my $info = $sf->{d}{db_string}; | ||
| 45 | my $prompt = sprintf 'No user %ss.', $type; | ||
| 46 | #my $prompt = sprintf 'No %ss.', $type; | ||
| 47 | $tc->choose( | ||
| 48 | [ undef ], | ||
| 49 | { info => $info, prompt => $prompt, undef => '<<' } | ||
| 50 | ); | ||
| 51 | return; | ||
| 52 | } | ||
| 53 | my $sql = {}; | ||
| 54 | $ax->reset_sql( $sql ); | ||
| 55 | my $prompt = $sf->{d}{db_string} . "\n" . 'Drop ' . $type; | ||
| 56 | # Choose | ||
| 57 | my $table = $tc->choose( | ||
| 58 | [ undef, map { "- $_" } sort @$tables ], | ||
| 59 | { %{$sf->{i}{lyt_v}}, prompt => $prompt, undef => ' <=' } | ||
| 60 | ); | ||
| 61 | if ( ! defined $table || ! length $table ) { | ||
| 62 | return; | ||
| 63 | } | ||
| 64 | $table =~ s/\-\s//; | ||
| 65 | my $drop_ok = $sf->__drop( $sql, $type, $table ); | ||
| 66 | return $drop_ok; | ||
| 67 | } | ||
| 68 | |||
| 69 | |||
| 70 | sub __drop { | ||
| 71 | my ( $sf, $sql, $type, $table ) = @_; | ||
| 72 | my $ax = App::DBBrowser::Auxil->new( $sf->{i}, $sf->{o}, $sf->{d} ); | ||
| 73 | my $tc = Term::Choose->new( $sf->{i}{tc_default} ); | ||
| 74 | if ( $type ne 'view' ) { | ||
| 75 | $type = 'table'; | ||
| 76 | } | ||
| 77 | $sql->{table} = $ax->qq_table( $sf->{d}{tables_info}{$table} ); | ||
| 78 | my $stmt_type = 'Drop_' . ucfirst( $type ); | ||
| 79 | $sf->{d}{stmt_types} = [ $stmt_type ]; | ||
| 80 | my $info = $ax->get_sql_info( $sql ); | ||
| 81 | # Choose | ||
| 82 | my $ok = $tc->choose( | ||
| 83 | [ undef, $sf->{i}{_confirm} . ' Stmt'], | ||
| 84 | { %{$sf->{i}{lyt_v}}, info => $info } | ||
| 85 | ); | ||
| 86 | $ax->print_sql_info( $info ); | ||
| 87 | if ( ! $ok ) { | ||
| 88 | return; | ||
| 89 | } | ||
| 90 | my $row_count; | ||
| 91 | if ( ! eval { | ||
| 92 | my $tbl = App::DBBrowser::Table->new( $sf->{i}, $sf->{o}, $sf->{d} ); | ||
| 93 | my $all_arrayref = $tbl->select_statement_results( $sql ); | ||
| 94 | $row_count = @$all_arrayref - 1; | ||
| 95 | my $prompt_pt = sprintf "The %s to be deleted:", $type; | ||
| 96 | my $tp = Term::TablePrint->new( $sf->{o}{table} ); | ||
| 97 | if ( ! $sf->{o}{G}{warnings_table_print} ) { | ||
| 98 | local $SIG{__WARN__} = sub {}; | ||
| 99 | $tp->print_table( $all_arrayref, { prompt => $prompt_pt, footer => " '" . $table . "' " } ); | ||
| 100 | } | ||
| 101 | else { | ||
| 102 | $tp->print_table( $all_arrayref, { prompt => $prompt_pt, footer => " '" . $table . "' " } ); | ||
| 103 | } | ||
| 104 | 1; } | ||
| 105 | ) { | ||
| 106 | $ax->print_error_message( $@ ); | ||
| 107 | } | ||
| 108 | if ( $row_count ) { | ||
| 109 | chomp $info; | ||
| 110 | $info .= sprintf " (%s %s)\n", insert_sep( $row_count, $sf->{i}{info_thsd_sep} ), $row_count == 1 ? 'row' : 'rows'; | ||
| 111 | } | ||
| 112 | my $prompt = $sf->{i}{confirm} . ':'; | ||
| 113 | my ( $no, $yes ) = ( '- NO', '- YES' ); | ||
| 114 | # Choose | ||
| 115 | my $choice = $tc->choose( | ||
| 116 | [ undef, $no, $yes ], | ||
| 117 | { %{$sf->{i}{lyt_v}}, info => $info, prompt => $prompt, undef => ' <=' } | ||
| 118 | ); | ||
| 119 | $ax->print_sql_info( $info ); | ||
| 120 | if ( ! defined $choice || $choice eq $no ) { | ||
| 121 | return; | ||
| 122 | } | ||
| 123 | my $stmt = $ax->get_stmt( $sql, $stmt_type, 'prepare' ); | ||
| 124 | $sf->{d}{dbh}->do( $stmt ) or die "$stmt failed!"; | ||
| 125 | return 1; | ||
| 126 | } | ||
| 127 | |||
| 128 | |||
| 129 | |||
| 130 | |||
| 131 | |||
| 132 | 1; | ||
| 133 | |||
| 134 | __END__ |