| 1 | package # hide from PAUSE | ||
| 2 | App::DBBrowser::Table; | ||
| 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::Spec::Functions qw( catfile ); | ||
| 11 | |||
| 12 | #use String::Unescape qw( unescape ); # required | ||
| 13 | #use Text::CSV_XS qw(); # required | ||
| 14 | |||
| 15 | use Term::Choose qw(); | ||
| 16 | use Term::Choose::Screen qw( hide_cursor clear_screen ); | ||
| 17 | use Term::Form::ReadLine qw(); | ||
| 18 | use Term::TablePrint qw(); | ||
| 19 | |||
| 20 | use App::DBBrowser::Auxil; | ||
| 21 | use App::DBBrowser::Options; | ||
| 22 | use App::DBBrowser::Options::ReadWrite; | ||
| 23 | |||
| 24 | use App::DBBrowser::Table::Substatement; | ||
| 25 | #use App::DBBrowser::Table::InsertUpdateDelete; # required | ||
| 26 | |||
| 27 | |||
| 28 | sub new { | ||
| 29 | my ( $class, $info, $options, $d ) = @_; | ||
| 30 | bless { | ||
| 31 | i => $info, | ||
| 32 | o => $options, | ||
| 33 | d => $d | ||
| 34 | }, $class; | ||
| 35 | } | ||
| 36 | |||
| 37 | |||
| 38 | sub browse_the_table { | ||
| 39 | my ( $sf, $sql, $return_stmt_prompt ) = @_; | ||
| 40 | my $tc = Term::Choose->new( $sf->{i}{tc_default} ); | ||
| 41 | my $ax = App::DBBrowser::Auxil->new( $sf->{i}, $sf->{o}, $sf->{d} ); | ||
| 42 | my $sb = App::DBBrowser::Table::Substatement->new( $sf->{i}, $sf->{o}, $sf->{d} ); | ||
| 43 | $sf->{d}{stmt_types} = [ 'Select' ]; | ||
| 44 | my $changed = {}; | ||
| 45 | my $hidden = 'Customize:'; | ||
| 46 | my ( $print_table, $select, $where, $group_by, $having, $order_by, $limit, $offset, $export ) = | ||
| 47 | ( 'Print TABLE', | ||
| 48 | '- SELECT', | ||
| 49 | '- WHERE', | ||
| 50 | '- GROUP BY', | ||
| 51 | '- HAVING', | ||
| 52 | '- ORDER BY', | ||
| 53 | '- LIMIT', | ||
| 54 | '- OFFSET', | ||
| 55 | ' Export', | ||
| 56 | ); | ||
| 57 | my @choices = ( $print_table, $select, $where, $group_by, $having, $order_by, $limit, $offset, $export ); | ||
| 58 | my @pre = ( $hidden, undef ); | ||
| 59 | my $back = $sf->{i}{back}; | ||
| 60 | my ( $return_statement, $hidden_print ) = ( $sf->{i}{_confirm}, $return_stmt_prompt ); | ||
| 61 | if ( $return_stmt_prompt ) { | ||
| 62 | $choices[0] = $return_statement; | ||
| 63 | @pre = ( $hidden_print, undef ); | ||
| 64 | $back = $sf->{i}{_back}; | ||
| 65 | } | ||
| 66 | my $footer = $sf->{d}{table_origin} eq 'ordinary' ? $sf->{d}{table_key} : ucfirst $sf->{d}{table_origin}; | ||
| 67 | $sf->{d}{table_footer} = " '$footer' "; | ||
| 68 | my $old_idx = 1; | ||
| 69 | |||
| 70 | CUSTOMIZE: while ( 1 ) { | ||
| 71 | my $menu = [ @pre, @choices ]; | ||
| 72 | my $info = $ax->get_sql_info( $sql ); | ||
| 73 | # Choose | ||
| 74 | my $idx = $tc->choose( | ||
| 75 | $menu, | ||
| 76 | { %{$sf->{i}{lyt_v}}, info => $info, prompt => '', index => 1, default => $old_idx, undef => $back } | ||
| 77 | ); | ||
| 78 | $ax->print_sql_info( $info ); | ||
| 79 | if ( ! defined $idx || ! defined $menu->[$idx] ) { | ||
| 80 | for my $key ( keys %{$changed} ) { | ||
| 81 | if ( $changed->{$key} ) { | ||
| 82 | $changed = {}; | ||
| 83 | $ax->reset_sql( $sql ); | ||
| 84 | next CUSTOMIZE; | ||
| 85 | } | ||
| 86 | } | ||
| 87 | return; | ||
| 88 | } | ||
| 89 | if ( $sf->{o}{G}{menu_memory} ) { | ||
| 90 | if ( $old_idx == $idx && ! $ENV{TC_RESET_AUTO_UP} ) { | ||
| 91 | $old_idx = 1; | ||
| 92 | next CUSTOMIZE; | ||
| 93 | } | ||
| 94 | $old_idx = $idx; | ||
| 95 | } | ||
| 96 | my $sub_stmt = $menu->[$idx]; | ||
| 97 | if ( $sub_stmt eq $select ) { | ||
| 98 | my $ret = $sb->select( $sql ); | ||
| 99 | $changed->{$sub_stmt} = $ret; | ||
| 100 | } | ||
| 101 | elsif ( $sub_stmt eq $where ) { | ||
| 102 | my $ret = $sb->where( $sql ); | ||
| 103 | $changed->{$sub_stmt} = $ret; | ||
| 104 | } | ||
| 105 | elsif ( $sub_stmt eq $group_by ) { | ||
| 106 | my $ret = $sb->group_by( $sql ); | ||
| 107 | $changed->{$sub_stmt} = $ret; | ||
| 108 | } | ||
| 109 | elsif ( $sub_stmt eq $having ) { | ||
| 110 | my $ret = $sb->having( $sql ); | ||
| 111 | $changed->{$sub_stmt} = $ret; | ||
| 112 | } | ||
| 113 | elsif ( $sub_stmt eq $order_by ) { | ||
| 114 | my $ret = $sb->order_by( $sql ); | ||
| 115 | $changed->{$sub_stmt} = $ret; | ||
| 116 | } | ||
| 117 | elsif ( $sub_stmt eq $limit ) { | ||
| 118 | my $ret = $sb->limit( $sql ); | ||
| 119 | $changed->{$sub_stmt} = $ret; | ||
| 120 | } | ||
| 121 | elsif ( $sub_stmt eq $offset ) { | ||
| 122 | my $ret = $sb->offset( $sql ); | ||
| 123 | $changed->{$sub_stmt} = $ret; | ||
| 124 | } | ||
| 125 | elsif ( $sql->{aggregate_mode} && ! @{$sql->{selected_cols}} ) { ## | ||
| 126 | $ax->print_error_message( "No columns selected!", $info ); | ||
| 127 | next CUSTOMIZE; | ||
| 128 | } | ||
| 129 | elsif ( $sub_stmt eq $hidden ) { | ||
| 130 | if ( ! eval { | ||
| 131 | require App::DBBrowser::Table::InsertUpdateDelete; | ||
| 132 | my $write = App::DBBrowser::Table::InsertUpdateDelete->new( $sf->{i}, $sf->{o}, $sf->{d} ); | ||
| 133 | my $backup_sql = $ax->clone_data( $sql ); | ||
| 134 | $write->table_write_access( $sql ); | ||
| 135 | # reset $sql else 'bu_where_stmt' from a delete/update could remain in $sql | ||
| 136 | $ax->reset_sql( $sql ); | ||
| 137 | for my $key ( keys %$backup_sql ) { | ||
| 138 | $sql->{$key} = $backup_sql->{$key}; | ||
| 139 | } | ||
| 140 | 1 } | ||
| 141 | ) { | ||
| 142 | $ax->print_error_message( $@ ); | ||
| 143 | } | ||
| 144 | $sf->{d}{stmt_types} = [ 'Select' ]; | ||
| 145 | $old_idx = 1; | ||
| 146 | } | ||
| 147 | elsif ( $sub_stmt eq $return_statement ) { | ||
| 148 | my $bu_cte_history = [ @{$sf->{d}{cte_history}} ]; | ||
| 149 | $sf->{d}{cte_history} = []; | ||
| 150 | # empty the cte_history else every substatemnt | ||
| 151 | # would contain the cte clauses. | ||
| 152 | my $statement = $ax->get_stmt( $sql, 'Select', 'prepare' ); | ||
| 153 | $sf->{d}{cte_history} = $bu_cte_history; | ||
| 154 | return $statement; | ||
| 155 | } | ||
| 156 | elsif ( $sub_stmt eq $export ) { | ||
| 157 | if ( ! eval { | ||
| 158 | $sf->__export( $sql ); | ||
| 159 | 1 } | ||
| 160 | ) { | ||
| 161 | $ax->print_error_message( $@ ); | ||
| 162 | next CUSTOMIZE; | ||
| 163 | } | ||
| 164 | } | ||
| 165 | elsif ( $sub_stmt eq $print_table || $sub_stmt eq $hidden_print ) { | ||
| 166 | local $| = 1; | ||
| 167 | print hide_cursor(); # safety | ||
| 168 | print clear_screen(); | ||
| 169 | print 'Computing:' . "\r" if $sf->{o}{table}{progress_bar}; | ||
| 170 | my $all_arrayref; | ||
| 171 | if ( ! eval { | ||
| 172 | $all_arrayref = $sf->select_statement_results( $sql ); | ||
| 173 | 1 } | ||
| 174 | ) { | ||
| 175 | $ax->print_error_message( $@ ); | ||
| 176 | next CUSTOMIZE; | ||
| 177 | } | ||
| 178 | my $tp = Term::TablePrint->new( $sf->{o}{table} ); | ||
| 179 | if ( ! $sf->{o}{G}{warnings_table_print} ) { | ||
| 180 | local $SIG{__WARN__} = sub {}; | ||
| 181 | $tp->print_table( $all_arrayref, { footer => $sf->{d}{table_footer} } ); | ||
| 182 | } | ||
| 183 | else { | ||
| 184 | $tp->print_table( $all_arrayref, { footer => $sf->{d}{table_footer} } ); | ||
| 185 | } | ||
| 186 | } | ||
| 187 | } | ||
| 188 | } | ||
| 189 | |||
| 190 | |||
| 191 | sub select_statement_results { | ||
| 192 | my ( $sf, $sql ) = @_; | ||
| 193 | my $ax = App::DBBrowser::Auxil->new( $sf->{i}, $sf->{o}, $sf->{d} ); | ||
| 194 | my $driver = $sf->{i}{driver}; | ||
| 195 | my $statement = $ax->get_stmt( $sql, 'Select', 'prepare' ); | ||
| 196 | unshift @{$sf->{d}{table_print_history}}, $statement; | ||
| 197 | if ( $#{$sf->{d}{table_print_history}} > 50 ) { | ||
| 198 | $#{$sf->{d}{table_print_history}} = 50; | ||
| 199 | } | ||
| 200 | my $sth = $sf->{d}{dbh}->prepare( $statement ); | ||
| 201 | $sth->execute(); | ||
| 202 | my $col_names = $sth->{NAME}; # not quoted | ||
| 203 | my $col_types = $sth->{TYPE}; # array_ref | ||
| 204 | my $all_arrayref = $sth->fetchall_arrayref; | ||
| 205 | if ( $driver eq 'DB2' && length $sf->{o}{G}{db2_encoding} ) { | ||
| 206 | print 'Decoding: ...' . "\r" if $sf->{o}{table}{progress_bar}; | ||
| 207 | my $encoding = Encode::find_encoding( $sf->{o}{G}{db2_encoding} ); | ||
| 208 | if ( ! ref $encoding ) { | ||
| 209 | die "Unknown encoding: $sf->{o}{G}{db2_encoding}"; | ||
| 210 | } | ||
| 211 | my @text_cols = grep { $col_types->[$_] =~ /^(?:1|12|2005|-15)\z/ } 0 .. $#$col_types; | ||
| 212 | |||
| 213 | for my $row ( @$all_arrayref ) { | ||
| 214 | for my $i ( @text_cols ) { | ||
| 215 | $row->[$i] = $encoding->decode( $row->[$i] ) | ||
| 216 | } | ||
| 217 | } | ||
| 218 | $col_names = [ map { $encoding->decode( $_ ) } @$col_names ]; | ||
| 219 | } | ||
| 220 | elsif ( $driver eq 'DuckDB' && $sf->{o}{G}{duckdb_utf8_decode} ) { | ||
| 221 | print 'Decoding: ...' . "\r" if $sf->{o}{table}{progress_bar}; | ||
| 222 | my @text_cols = grep { $col_types->[$_] == 12 } 0 .. $#$col_types; | ||
| 223 | no warnings 'uninitialized'; | ||
| 224 | |||
| 225 | for my $row ( @$all_arrayref ) { | ||
| 226 | utf8::decode( $row->[$_] ) for @text_cols; | ||
| 227 | } | ||
| 228 | utf8::decode( $_ ) for @$col_names; | ||
| 229 | } | ||
| 230 | unshift @$all_arrayref, $col_names; | ||
| 231 | return $all_arrayref; | ||
| 232 | } | ||
| 233 | |||
| 234 | |||
| 235 | sub __export { | ||
| 236 | my ( $sf, $sql ) = @_; | ||
| 237 | my $file_fs = $sf->__get_filename_fs( $sql ); | ||
| 238 | if ( ! length $file_fs ) { | ||
| 239 | return; | ||
| 240 | } | ||
| 241 | print 'Working ...' . "\r" if $sf->{o}{table}{progress_bar}; | ||
| 242 | my $all_arrayref = $sf->select_statement_results( $sql ); | ||
| 243 | my $open_mode; | ||
| 244 | if ( length $sf->{o}{export}{file_encoding} ) { | ||
| 245 | $open_mode = '>:encoding(' . $sf->{o}{export}{file_encoding} . ')'; | ||
| 246 | } | ||
| 247 | else { | ||
| 248 | $open_mode = '>'; | ||
| 249 | } | ||
| 250 | open my $fh, $open_mode, $file_fs or die "$file_fs: $!"; | ||
| 251 | require String::Unescape; | ||
| 252 | my $options = { | ||
| 253 | map { $_ => String::Unescape::unescape( $sf->{o}{csv_out}{$_} ) } | ||
| 254 | grep { length $sf->{o}{csv_out}{$_} } # keep the default value if the option is set to '' | ||
| 255 | keys %{$sf->{o}{csv_out}} | ||
| 256 | }; | ||
| 257 | if ( ! length $options->{eol} ) { | ||
| 258 | $options->{eol} = $/; # for `eol` use `$/` as the default value | ||
| 259 | } | ||
| 260 | require Text::CSV_XS; | ||
| 261 | my $csv = Text::CSV_XS->new( $options ) or die Text::CSV_XS->error_diag(); | ||
| 262 | $csv->print( $fh, $_ ) for @$all_arrayref; | ||
| 263 | close $fh; | ||
| 264 | return 1; | ||
| 265 | } | ||
| 266 | |||
| 267 | |||
| 268 | sub __get_filename_fs { | ||
| 269 | my ( $sf, $sql ) = @_; | ||
| 270 | my $tc = Term::Choose->new( $sf->{i}{tc_default} ); | ||
| 271 | my $tr = Term::Form::ReadLine->new( $sf->{i}{tr_default} ); | ||
| 272 | my $ax = App::DBBrowser::Auxil->new( $sf->{i}, $sf->{o}, $sf->{d} ); | ||
| 273 | my $tmp_file_name; | ||
| 274 | if ( $sf->{o}{export}{default_filename} ) { | ||
| 275 | $tmp_file_name = $sf->{d}{table_key}; | ||
| 276 | } | ||
| 277 | my $count = 0; | ||
| 278 | |||
| 279 | FILE_NAME: while ( 1 ) { | ||
| 280 | if ( ++$count > 2 ) { | ||
| 281 | $tmp_file_name = ''; | ||
| 282 | } | ||
| 283 | my $info = $ax->get_sql_info( $sql ); | ||
| 284 | # Readline | ||
| 285 | $tmp_file_name = $tr->readline( | ||
| 286 | 'File name: ', | ||
| 287 | { info => $info, default => $tmp_file_name, hide_cursor => 2, history => [] } | ||
| 288 | ); | ||
| 289 | $ax->print_sql_info( $info ); | ||
| 290 | if ( ! length $tmp_file_name ) { | ||
| 291 | return; | ||
| 292 | } | ||
| 293 | |||
| 294 | FULL_FILE_NAME: while ( 1 ) { | ||
| 295 | my $file_name = $tmp_file_name; | ||
| 296 | if ( $sf->{o}{export}{add_extension} && $tmp_file_name !~ /\.csv\z/i ) { | ||
| 297 | $file_name .= '.csv'; | ||
| 298 | } | ||
| 299 | my $export_dir = $sf->{o}{export}{export_dir}; | ||
| 300 | my $dir_fs = realpath( encode( 'locale_fs', $export_dir ) ) or die "$export_dir: $!"; | ||
| 301 | my $file_fs = catfile $dir_fs, encode( 'locale_fs', $file_name ); | ||
| 302 | my ( $new_name, $overwrite ) = ( '- New name', '- Overwrite' ); | ||
| 303 | my $chosen; | ||
| 304 | if ( -e $file_fs ) { | ||
| 305 | my $menu; | ||
| 306 | my $prompt; | ||
| 307 | if ( -d $file_fs ) { | ||
| 308 | $prompt = 'A directory \'' . decode( 'locale_fs', $file_fs ) . '\' already exists:'; | ||
| 309 | $menu = [ undef, $new_name ]; | ||
| 310 | } | ||
| 311 | else { | ||
| 312 | $prompt = 'A file \'' . decode( 'locale_fs', $file_fs ) . '\' already exists:'; | ||
| 313 | $menu = [ undef, $new_name, $overwrite ]; | ||
| 314 | } | ||
| 315 | # Choose | ||
| 316 | $chosen = $tc->choose( | ||
| 317 | $menu, | ||
| 318 | { %{$sf->{i}{lyt_v}}, info => $info, prompt => $prompt, keep => scalar( @$menu ) } | ||
| 319 | ); | ||
| 320 | $ax->print_sql_info( $info ); | ||
| 321 | if ( ! defined $chosen ) { | ||
| 322 | return; | ||
| 323 | } | ||
| 324 | elsif ( $chosen eq $new_name ) { | ||
| 325 | next FILE_NAME; | ||
| 326 | } | ||
| 327 | } | ||
| 328 | my ( $yes, $no ) = ( '- YES', '- NO' ); | ||
| 329 | my $hidden; | ||
| 330 | if ( defined $chosen && $chosen eq $overwrite ) { | ||
| 331 | $hidden = 'Overwrite "' . decode( 'locale_fs', $file_fs ) . '"?'; | ||
| 332 | } | ||
| 333 | else { | ||
| 334 | $hidden = 'Write data to "' . decode( 'locale_fs', $file_fs ) . '"?'; | ||
| 335 | } | ||
| 336 | # Choose | ||
| 337 | my $choice = $tc->choose( | ||
| 338 | [ $hidden, undef, $yes, $no ], | ||
| 339 | { info => $info, prompt => '', default => 1, layout => 2, undef => ' <<' } | ||
| 340 | ); | ||
| 341 | $ax->print_sql_info( $info ); | ||
| 342 | if ( ! defined $choice ) { | ||
| 343 | next FILE_NAME; | ||
| 344 | } | ||
| 345 | elsif ( $choice eq $hidden ) { | ||
| 346 | my $op = App::DBBrowser::Options->new( $sf->{i}, $sf->{o} ); | ||
| 347 | my $op_rw = App::DBBrowser::Options::ReadWrite->new( $sf->{i}, $sf->{o} ); | ||
| 348 | $op->config_groups( [ { name => 'group_export', text => "- Export" } ], $sf->{i}{plugin} ); | ||
| 349 | $op_rw->read_config_file( $sf->{i}{driver}, $sf->{i}{plugin} ); | ||
| 350 | next FULL_FILE_NAME; | ||
| 351 | } | ||
| 352 | elsif ( $choice eq $no ) { | ||
| 353 | return; | ||
| 354 | } | ||
| 355 | else { | ||
| 356 | return $file_fs; | ||
| 357 | } | ||
| 358 | } | ||
| 359 | } | ||
| 360 | } | ||
| 361 | |||
| 362 | |||
| 363 | |||
| 364 | 1; | ||
| 365 | |||
| 366 | |||
| 367 | __END__ |