| 1 | package # hide from PAUSE | ||
| 2 | App::DBBrowser::GetContent::Source; | ||
| 3 | |||
| 4 | use warnings; | ||
| 5 | use strict; | ||
| 6 | use 5.016; | ||
| 7 | |||
| 8 | use Cwd qw( realpath ); | ||
| 9 | use Encode qw( encode decode ); | ||
| 10 | use File::Basename qw( basename ); | ||
| 11 | use File::Spec::Functions qw( catfile catdir ); | ||
| 12 | |||
| 13 | use List::MoreUtils qw( uniq firstidx ); | ||
| 14 | use Encode::Locale qw(); | ||
| 15 | |||
| 16 | use Term::Choose qw(); | ||
| 17 | use Term::Choose::Constants qw( EXTRA_W ); | ||
| 18 | use Term::Choose::LineFold qw( line_fold ); | ||
| 19 | use Term::Choose::Util qw( get_term_width ); | ||
| 20 | use Term::Form qw(); | ||
| 21 | |||
| 22 | use App::DBBrowser::Auxil; | ||
| 23 | |||
| 24 | |||
| 25 | sub new { | ||
| 26 | my ( $class, $info, $options, $d ) = @_; | ||
| 27 | my $sf = { | ||
| 28 | i => $info, | ||
| 29 | o => $options, | ||
| 30 | d => $d | ||
| 31 | }; | ||
| 32 | bless $sf, $class; | ||
| 33 | } | ||
| 34 | |||
| 35 | |||
| 36 | sub __get_read_info { | ||
| 37 | my ( $sf, $aoa ) = @_; | ||
| 38 | my $term_w = get_term_width() + EXTRA_W; | ||
| 39 | my @tmp = ( 'DATA:' ); | ||
| 40 | for my $row ( @$aoa ) { | ||
| 41 | no warnings 'uninitialized'; | ||
| 42 | push @tmp, line_fold( join( ', ', @$row ), { width => $term_w, subseq_tab => ' ' x 4, join => 0 } ); | ||
| 43 | } | ||
| 44 | return join( "\n", @tmp ) . "\n"; | ||
| 45 | } | ||
| 46 | |||
| 47 | |||
| 48 | sub from_col_by_col { | ||
| 49 | my ( $sf, $sql ) = @_; | ||
| 50 | my $ax = App::DBBrowser::Auxil->new( $sf->{i}, $sf->{o}, $sf->{d} ); | ||
| 51 | my $tf = Term::Form->new( $sf->{i}{tf_default} ); | ||
| 52 | my $tc = Term::Choose->new( $sf->{i}{tc_default} ); | ||
| 53 | my $back = $sf->{i}{back}; | ||
| 54 | my $confirm = $sf->{i}{confirm}; | ||
| 55 | my $stmt_type = $sf->{d}{stmt_types}[0]; | ||
| 56 | |||
| 57 | COL_BY_COL: while( 1 ) { | ||
| 58 | my $aoa = []; | ||
| 59 | my $col_names; | ||
| 60 | if ( $stmt_type eq 'Create_Table' ) { | ||
| 61 | my $col_count = $sf->{o}{insert}{max_cols_plain} || 25; ## | ||
| 62 | my $width = length $col_count; | ||
| 63 | my $info = 'CREATE TABLE'; | ||
| 64 | my $fields = [ map { [ sprintf( "%*d", $width, $_ ), '' ] } 1 .. $col_count ]; | ||
| 65 | # Fill_form | ||
| 66 | my $form = $tf->fill_form( | ||
| 67 | $fields, | ||
| 68 | { info => $info, prompt => 'Column names:', confirm => $confirm, back => $back . ' ' } | ||
| 69 | ); | ||
| 70 | if ( ! $form ) { | ||
| 71 | return; | ||
| 72 | } | ||
| 73 | $col_names = [ map { $_->[1] } grep { length $_->[1] } @$form ]; # not quoted | ||
| 74 | unshift @$aoa, $col_names; | ||
| 75 | } | ||
| 76 | else { | ||
| 77 | $col_names = $sql->{insert_col_names}; | ||
| 78 | } | ||
| 79 | my $default; | ||
| 80 | my $count; | ||
| 81 | |||
| 82 | WHAT_NEXT: while ( 1 ) { | ||
| 83 | my $add = 'Add Data'; | ||
| 84 | my @pre = ( undef, $sf->{i}{ok} ); | ||
| 85 | my $menu = [ @pre, $add ]; | ||
| 86 | my $info = $sf->__get_read_info( $aoa ); | ||
| 87 | my $choice; | ||
| 88 | if ( $stmt_type eq 'Insert' && ! $count ) { | ||
| 89 | $choice = $add; | ||
| 90 | } | ||
| 91 | else { | ||
| 92 | # Choose | ||
| 93 | $choice = $tc->choose( | ||
| 94 | $menu, | ||
| 95 | { %{$sf->{i}{lyt_h}}, info => $info, prompt => '', default => $default } | ||
| 96 | ); | ||
| 97 | $ax->print_sql_info( $info ); | ||
| 98 | if ( ! defined $choice ) { | ||
| 99 | if ( $stmt_type eq 'Create_Table' && @$aoa == 1 ) { | ||
| 100 | next COL_BY_COL; | ||
| 101 | } | ||
| 102 | elsif ( $stmt_type eq 'Insert' && @$aoa == 0 ) { | ||
| 103 | return; | ||
| 104 | } | ||
| 105 | else { | ||
| 106 | $default = 0; | ||
| 107 | $#$aoa--; | ||
| 108 | next WHAT_NEXT; | ||
| 109 | } | ||
| 110 | } | ||
| 111 | } | ||
| 112 | $count++; | ||
| 113 | if ( $choice eq $sf->{i}{ok} ) { | ||
| 114 | if ( ! @$aoa ) { | ||
| 115 | if ( $stmt_type eq 'Create_Table' ) { | ||
| 116 | next COL_BY_COL; | ||
| 117 | } | ||
| 118 | elsif ( $stmt_type eq 'Insert' ) { | ||
| 119 | return; | ||
| 120 | } | ||
| 121 | } | ||
| 122 | else { | ||
| 123 | $sql->{insert_args} = $aoa; | ||
| 124 | return 1; | ||
| 125 | } | ||
| 126 | } | ||
| 127 | elsif ( $choice eq $add ) { | ||
| 128 | my $info = $sf->__get_read_info( $aoa ); | ||
| 129 | my $fields = [ map { [ $_, ] } @$col_names ]; | ||
| 130 | # Fill_form | ||
| 131 | my $data = $tf->fill_form( | ||
| 132 | $fields, | ||
| 133 | { info => $info, confirm => $confirm, back => $back . ' ', prompt => 'Enter Data:' } | ||
| 134 | ); | ||
| 135 | $ax->print_sql_info( $info ); | ||
| 136 | if ( ! defined $data ) { | ||
| 137 | if ( ! @$aoa && $stmt_type eq 'Create_Table' ) { | ||
| 138 | next COL_BY_COL; | ||
| 139 | } | ||
| 140 | $default = 0; | ||
| 141 | } | ||
| 142 | else { | ||
| 143 | if ( $sf->{o}{insert}{empty_to_null_plain} ) { | ||
| 144 | push @{$aoa}, [ map { length( $_->[1] ) ? $_->[1] : undef } @$data ]; | ||
| 145 | } | ||
| 146 | else { | ||
| 147 | push @{$aoa}, [ map { $_->[1] } @$data ]; | ||
| 148 | } | ||
| 149 | $default = 2; | ||
| 150 | } | ||
| 151 | } | ||
| 152 | } | ||
| 153 | } | ||
| 154 | } | ||
| 155 | |||
| 156 | |||
| 157 | sub files_in_dir { | ||
| 158 | my ( $sf, $dir ) = @_; | ||
| 159 | my $ax = App::DBBrowser::Auxil->new( $sf->{i}, $sf->{o}, $sf->{d} ); | ||
| 160 | if ( ! defined $dir ) { | ||
| 161 | return []; | ||
| 162 | } | ||
| 163 | if ( ! -d $dir ) { | ||
| 164 | my $message = "The directory '$dir' does not exist any more."; | ||
| 165 | $ax->print_error_message( $message ); | ||
| 166 | $sf->__remove_from_history( $dir ); | ||
| 167 | return []; | ||
| 168 | } | ||
| 169 | my $dir_fs = realpath( encode( 'locale_fs', $dir ) ) or die "$dir: $!"; | ||
| 170 | my @tmp_files_fs; | ||
| 171 | if ( length $sf->{o}{insert}{file_filter} ) { | ||
| 172 | @tmp_files_fs = map { basename $_} grep { -e $_ } glob( catfile( $dir_fs, $sf->{o}{insert}{file_filter} ) ); | ||
| 173 | } | ||
| 174 | else { | ||
| 175 | opendir( my $dh, $dir_fs ) or die "$dir_fs: $!"; | ||
| 176 | @tmp_files_fs = readdir $dh; | ||
| 177 | closedir $dh; | ||
| 178 | } | ||
| 179 | my $files = []; | ||
| 180 | for my $file_fs ( sort @tmp_files_fs ) { | ||
| 181 | next if $file_fs =~ /^\./ && ! $sf->{o}{insert}{show_hidden_files}; | ||
| 182 | next if -d catdir $dir_fs, $file_fs; | ||
| 183 | push @$files, decode( 'locale_fs', catfile $dir_fs, $file_fs ); | ||
| 184 | } | ||
| 185 | return $files; | ||
| 186 | } | ||
| 187 | |||
| 188 | |||
| 189 | sub __avail_directories { | ||
| 190 | my ( $sf ) = @_; | ||
| 191 | my $ax = App::DBBrowser::Auxil->new( $sf->{i}, $sf->{o}, $sf->{d} ); | ||
| 192 | my $h_ref = $ax->read_json( $sf->{i}{f_dir_history} ) // {}; | ||
| 193 | my @dirs = @{$h_ref->{dirs}//[]}; | ||
| 194 | if ( @dirs > $sf->{o}{insert}{history_dirs} ) { | ||
| 195 | $#dirs = $sf->{o}{insert}{history_dirs} - 1; | ||
| 196 | $h_ref->{dirs} = \@dirs; | ||
| 197 | $ax->write_json( $sf->{i}{f_dir_history}, $h_ref ); | ||
| 198 | } | ||
| 199 | return [ sort @dirs ]; ## | ||
| 200 | } | ||
| 201 | |||
| 202 | |||
| 203 | sub __add_to_history { | ||
| 204 | my ( $sf, $dir ) = @_; | ||
| 205 | my $ax = App::DBBrowser::Auxil->new( $sf->{i}, $sf->{o}, $sf->{d} ); | ||
| 206 | my $h_ref = $ax->read_json( $sf->{i}{f_dir_history} ) // {}; | ||
| 207 | my @dirs = @{$h_ref->{dirs}//[]}; | ||
| 208 | unshift @dirs, $dir; | ||
| 209 | @dirs = uniq @dirs; | ||
| 210 | if ( @dirs > $sf->{o}{insert}{history_dirs} ) { | ||
| 211 | $#dirs = $sf->{o}{insert}{history_dirs} - 1; | ||
| 212 | } | ||
| 213 | $h_ref->{dirs} = \@dirs; | ||
| 214 | $ax->write_json( $sf->{i}{f_dir_history}, $h_ref ); | ||
| 215 | } | ||
| 216 | |||
| 217 | |||
| 218 | sub __remove_from_history { | ||
| 219 | my ( $sf, $dir ) = @_; | ||
| 220 | my $ax = App::DBBrowser::Auxil->new( $sf->{i}, $sf->{o}, $sf->{d} ); | ||
| 221 | my $h_ref = $ax->read_json( $sf->{i}{f_dir_history} ) // {}; | ||
| 222 | my @dirs = @{$h_ref->{dirs}//[]}; | ||
| 223 | my $idx = firstidx { $_ eq $dir } @dirs; | ||
| 224 | splice( @dirs, $idx, 1 ); | ||
| 225 | $h_ref->{dirs} = \@dirs; | ||
| 226 | $ax->write_json( $sf->{i}{f_dir_history}, $h_ref ); | ||
| 227 | } | ||
| 228 | |||
| 229 | |||
| 230 | sub __new_search_dir { | ||
| 231 | my ( $sf ) = @_; | ||
| 232 | my $tu = Term::Choose::Util->new( $sf->{i}{tcu_default} ); | ||
| 233 | my $default_dir = $sf->{d}{default_search_dir} // $sf->{i}{home_dir}; | ||
| 234 | # Choose | ||
| 235 | my $dir = $tu->choose_a_directory( | ||
| 236 | { init_dir => $default_dir, decoded => 1, clear_screen => 1, confirm => '-OK-', back => '<<' } | ||
| 237 | ); | ||
| 238 | if ( $dir ) { | ||
| 239 | $sf->{d}{default_search_dir} = $dir; | ||
| 240 | } | ||
| 241 | return $dir; | ||
| 242 | } | ||
| 243 | |||
| 244 | |||
| 245 | |||
| 246 | |||
| 247 | 1; | ||
| 248 | |||
| 249 | |||
| 250 | __END__ |