App-DBBrowser
view release on metacpan or search on metacpan
lib/App/DBBrowser/Table.pm view on Meta::CPAN
my $col_types = $sth->{TYPE};
my $all_arrayref = $sth->fetchall_arrayref;
unshift @$all_arrayref, $col_names;
if ( $sf->{i}{driver} eq 'DB2' && length $sf->{o}{G}{db2_encoding} ) {
print 'Decoding: ...' . "\r" if $sf->{o}{table}{progress_bar};
my $encoding = Encode::find_encoding( $sf->{o}{G}{db2_encoding} );
if ( ! ref $encoding ) {
die qq(encoding "$sf->{o}{G}{db2_encoding}" not found);
}
my $is_text = [ map { /^(?:1|12|2005|-15)\z/ ? 1 : 0 } @$col_types ];
for my $row ( @$all_arrayref ) {
for my $i ( 0 .. $#$row ) {
if ( $is_text->[$i] ) {
$row->[$i] = $encoding->decode( $row->[$i] )
}
}
}
}
elsif ( $sf->{i}{driver} eq 'DuckDB' ) {
my $duckdb_encoding = 'UTF-8';
my $encoding = Encode::find_encoding( $duckdb_encoding );
if ( ! ref $encoding ) {
die qq(encoding "$duckdb_encoding" not found);
}
for my $row ( @$all_arrayref ) {
for my $i ( 0 .. $#$row ) {
if ( $col_types->[$i] == 12 ) {
$row->[$i] = $encoding->decode( $row->[$i] );
#Encode::_utf8_on( $row->[$i] );
}
}
}
}
return $all_arrayref;
}
sub __export {
my ( $sf, $sql ) = @_;
my $file_fs = $sf->__get_filename_fs( $sql );
if ( ! length $file_fs ) {
return;
}
print 'Working ...' . "\r" if $sf->{o}{table}{progress_bar};
my $all_arrayref = $sf->select_statement_results( $sql );
my $open_mode;
if ( length $sf->{o}{export}{file_encoding} ) {
$open_mode = '>:encoding(' . $sf->{o}{export}{file_encoding} . ')';
}
else {
$open_mode = '>';
}
open my $fh, $open_mode, $file_fs or die "$file_fs: $!";
require String::Unescape;
my $options = {
map { $_ => String::Unescape::unescape( $sf->{o}{csv_out}{$_} ) }
grep { length $sf->{o}{csv_out}{$_} } # keep the default value if the option is set to ''
keys %{$sf->{o}{csv_out}}
};
if ( ! length $options->{eol} ) {
$options->{eol} = $/; # for `eol` use `$/` as the default value
}
require Text::CSV_XS;
my $csv = Text::CSV_XS->new( $options ) or die Text::CSV_XS->error_diag();
$csv->print( $fh, $_ ) for @$all_arrayref;
close $fh;
return 1;
}
sub __get_filename_fs {
my ( $sf, $sql ) = @_;
my $tc = Term::Choose->new( $sf->{i}{tc_default} );
my $tr = Term::Form::ReadLine->new( $sf->{i}{tr_default} );
my $ax = App::DBBrowser::Auxil->new( $sf->{i}, $sf->{o}, $sf->{d} );
my $file_name;
if ( $sf->{o}{export}{default_filename} ) {
$file_name = $sf->{d}{table_key};
}
my $count = 0;
FILE_NAME: while ( 1 ) {
if ( ++$count > 2 ) {
$file_name = '';
}
my $info = $ax->get_sql_info( $sql );
# Readline
$file_name = $tr->readline(
'File name: ',
{ info => $info, default => $file_name, hide_cursor => 2, history => [] }
);
$ax->print_sql_info( $info );
if ( ! length $file_name ) {
return;
}
FULL_FILE_NAME: while ( 1 ) {
my $file_name_plus = $file_name;
if ( $sf->{o}{export}{add_extension} && $file_name !~ /\.csv\z/i ) {
$file_name_plus .= '.csv';
}
my $export_dir = $sf->{o}{export}{export_dir};
my $dir_fs = realpath( encode( 'locale_fs', $export_dir ) ) or die "$export_dir: $!";
my $file_fs = catfile $dir_fs, encode( 'locale_fs', $file_name_plus );
my ( $new_name, $overwrite ) = ( '- New name', '- Overwrite' );
my $chosen;
if ( -e $file_fs ) {
my $menu;
my $prompt;
if ( -d $file_fs ) {
$prompt = 'A directory with name "' . $file_name_plus . '" already exists.';
$menu = [ undef, $new_name ];
}
else {
$prompt = 'A file with name "' . $file_name_plus . '" already exists.';
$menu = [ undef, $new_name, $overwrite ];
}
# Choose
$chosen = $tc->choose(
$menu,
{ %{$sf->{i}{lyt_v}}, info => $info, prompt => $prompt, keep => scalar( @$menu ) }
( run in 1.010 second using v1.01-cache-2.11-cpan-ceb78f64989 )