view release on metacpan or search on metacpan
lib/App/DBBrowser/CreateDropAttach/CreateTable.pm view on Meta::CPAN
my ( $sf, $sql, $source, $tablename_default, $count_table_name_loop ) = @_;
my $ax = App::DBBrowser::Auxil->new( $sf->{i}, $sf->{o}, $sf->{d} );
my $tc = Term::Choose->new( $sf->{i}{tc_default} );
my $tr = Term::Form::ReadLine->new( $sf->{i}{tr_default} );
$ax->print_sql_info( $ax->get_sql_info( $sql ) );
while ( 1 ) {
my $file_info;
if ( $source->{source_type} eq 'file' ) {
my $file_fs = $source->{file_fs};
my $file_name = basename decode( 'locale_fs', $file_fs );
$file_info = sprintf "File: '%s'", $file_name;
if ( ! length $tablename_default ) {
if ( length $source->{sheet_name} ) {
$file_name =~ s/\.[^.]{1,4}\z//;
$tablename_default = $file_name . '_' . $source->{sheet_name};
}
else {
$tablename_default = $file_name =~ s/\.[^.]{1,4}\z//r;
}
$tablename_default =~ s/ /_/g;
lib/App/DBBrowser/CreateDropAttach/CreateTable.pm view on Meta::CPAN
my $tf = Term::Form->new( $sf->{i}{tf_default} );
my $ax = App::DBBrowser::Auxil->new( $sf->{i}, $sf->{o}, $sf->{d} );
my $dbms = $sf->{i}{dbms};
my $fields;
if ( ! %$data_types && $sf->{o}{create}{data_type_guessing} ) {
$ax->print_sql_info( $ax->get_sql_info( $sql ), 'Column data types: guessing ... ' );
my $header = $sql->{insert_col_names}; #
my $table = $sql->{insert_args};
my $encoding;
if ( $sf->{o}{create}{encode_for_data_type_guessing} ) { # MSSQL DB2 Oracle
$encoding = $source->{source_type} eq 'plain' ? 'locale' : $sf->{o}{insert}{file_encoding};
}
my @aoh;
if ( length $encoding ) {
for my $row ( @$table ) {
push @aoh, { map { $header->[$_] => Encode::encode( $encoding, $row->[$_] ) } 0 .. $#$header };
}
}
else {
for my $row ( @$table ) {
push @aoh, { map { $header->[$_] => $row->[$_] } 0 .. $#$header };
lib/App/DBBrowser/DB/SQLite.pm view on Meta::CPAN
}
$dirs = $new_dirs;
}
else {
last;
}
}
$databases = [];
local $| = 1;
print 'Searching ... ';
my $encoding = Encode::find_encoding( 'locale_fs' );
if ( $sf->{o}{G}{file_find_warnings} ) {
my $file;
for my $dir ( @$dirs ) {
File::Find::find( {
wanted => sub {
#my $file_fs = $_;
return if ! -f $_;
$file = $encoding->decode( $_ );
print "$file\n";
if ( ! eval {
lib/App/DBBrowser/GetContent.pm view on Meta::CPAN
use Encode::Locale qw();
use Term::Choose qw();
use App::DBBrowser::GetContent::Filter;
use App::DBBrowser::GetContent::Parse;
use App::DBBrowser::GetContent::Source;
use App::DBBrowser::Options;
use App::DBBrowser::Options::ReadWrite;
use open ':encoding(locale)';
sub new {
my ( $class, $info, $options, $d ) = @_;
my $sf = {
i => $info,
o => $options,
d => $d
};
bless $sf, $class;
lib/App/DBBrowser/GetContent.pm view on Meta::CPAN
elsif ( $menu->[$idx] eq $change_dir ) {
$source->{dir} = $cs->__new_search_dir();
if ( length $source->{dir} ) {
$cs->__add_to_history( $source->{dir} );
$files_in_chosen_dir = $cs->files_in_dir( $source->{dir} );
}
next FILE;
}
else {
my $old_file_fs = $source->{file_fs};
$source->{file_fs} = encode( 'locale_fs', $files_in_chosen_dir->[$idx-@pre] );
if ( ! defined $old_file_fs || $old_file_fs ne $source->{file_fs} ) {
delete $source->{saved_book};
delete $source->{sheet_name};
}
}
}
$source->{old_idx_sheet} = 0;
PARSE: while ( 1 ) {
my $parse_mode_idx = $sf->{o}{insert}{parse_mode_input_file};
lib/App/DBBrowser/GetContent/Filter/ConvertDate.pm view on Meta::CPAN
return $sf;
}
sub convert_date {
my ( $sf, $sql, $bu_insert_args, $filter_str ) = @_;
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 $cf = App::DBBrowser::GetContent::Filter->new( $sf->{i}, $sf->{o}, $sf->{d} );
my $rx_locale_dependent = "\%[aAbBhcpPxX]";
my $aoa = $sql->{insert_args};
my $row_count = @$aoa;
my $busy_text = 'Convert datetime: ';
my $threshold_busy = 5_000;
my $threshold_progress = 100_000;
my ( $working, $fmt, $step );
if ( $row_count > $threshold_busy ) {
$working = $busy_text . '...';
if ( $row_count > $threshold_progress ) {
$step = 1_000;
lib/App/DBBrowser/GetContent/Filter/ConvertDate.pm view on Meta::CPAN
$prompt_patter_in,
{ info => $info, default => $default_pattern_in, history => $sf->__pattern_history( 'in' ) }
);
if ( ! length $pattern_in ) {
next SKIP_HEADER if $sf->{d}{stmt_types}[0] eq 'Create_Table';
next COL;
}
$default_pattern_in = $count_error_in > 1 ? '' : $pattern_in;
push @in_info, $prompt_patter_in . $pattern_in;
my $formatter_args = { pattern => $pattern_in };
if ( $pattern_in =~ /$rx_locale_dependent/ ) {
$info = $cf->__get_filter_info( $sql, join( "\n", @in_info ) );
my $prompt_locale_in = 'Locale in: ';
# Readline
my $locale_in = $tr->readline(
$prompt_locale_in,
{ info => $info, history => [] }
);
if ( length $locale_in ) {
$formatter_args->{locale} = $locale_in;
push @in_info, $prompt_locale_in . $locale_in;
}
}
my $formatter = DateTime::Format::Strptime->new( %$formatter_args, on_error => 'undef' );
TYPE: while ( 1 ) {
$info = $cf->__get_filter_info( $sql, join( "\n", @in_info ) );
my ( $format, $epoch ) = ( '- DateTime', '- Epoch' );
# Choose
my $choice = $tc->choose(
[ @pre, $format, $epoch ],
lib/App/DBBrowser/GetContent/Filter/ConvertDate.pm view on Meta::CPAN
my $prompt_pattern_out = 'Pattern out: ';
# Readline
my $pattern_out = $tr->readline(
$prompt_pattern_out,
{ info => $info, default => $default_pattern_out, history => $sf->__pattern_history( 'out' ) }
);
if ( ! length $pattern_out ) {
next TYPE;
}
$default_pattern_out = $count_error_out > 1 ? '' : $pattern_out;
my $locale_out;
if ( $pattern_out =~ /$rx_locale_dependent/ ) {
push @tmp_info, $prompt_pattern_out . $pattern_out;
$info = $cf->__get_filter_info( $sql, join( "\n", @tmp_info ) );
my $prompt_locale_out = 'Locale out: ';
# ReadLine
$locale_out = $tr->readline(
$prompt_locale_out,
{ info => $info, history => [] }
);
if ( length $locale_out ) {
push @tmp_info, $prompt_locale_out . $locale_out;
}
}
$cf->__print_busy_string( $working );
if ( ! eval {
for my $row ( $row_idx_begin .. $#$aoa ) {
next if ! length $aoa->[$row][$col_idx];
my $dt = $formatter->parse_datetime( $aoa->[$row][$col_idx] );
if ( ! defined $dt ) {
for my $row ( 1 .. $row ) {
$aoa->[$row][$col_idx] = $bu_insert_args->[$row][$col_idx];
}
my $message = $sf->__error_messagte_parse_datetime( $formatter, $row, $aoa->[$row][$col_idx] );
die $message;
}
if ( length $locale_out ) {
$dt->set_locale( $locale_out );
}
$aoa->[$row][$col_idx] = $dt->strftime( $pattern_out );
if ( $fmt && ! ( $row % $step ) ) {
$cf->__print_busy_string( sprintf $fmt, $row / $step );
}
}
1 }
) {
$ax->print_error_message( $@ );
if ( $@ =~ /^Pattern:/ ) {
lib/App/DBBrowser/GetContent/Filter/ConvertDate.pm view on Meta::CPAN
}
}
}
}
}
}
sub __error_messagte_parse_datetime {
my ( $sf, $formatter, $row, $string ) = @_;
my $message = 'Pattern: ' . $formatter->pattern() . ', ' . $formatter->locale();
$message .= "\nRow: $row";
$message .= "\nString: $string";
$message .= "\n\n" . $formatter->errmsg();
return $message;
}
sub __pattern_history {
my ( $sf, $in_out ) = @_;
if ( $in_out eq 'in' ) {
lib/App/DBBrowser/GetContent/Filter/ConvertDate.pm view on Meta::CPAN
'%Y-%m-%d %H:%M:%S',
'%Y-%m-%dT%H:%M:%S',
'%Y-%m-%d %H:%M:%S.%N%z',
'%a %d %b %Y %I:%M:%S %P',
'%d.%m.%Y %H:%M:%S',
'%d/%m/%Y %H:%M:%S',
'%m/%d/%Y %H:%M:%S',
'%y = two digit year (0-99)',
'%b = Sep; %B = September',
'%I = the hour on a 12-hour clock (01-12)',
'%p = the equivalent of AM or PM according to the locale in use',
'%z = timezone (eg. +2000); %Z timezone name (eg. EST)',
'%u = weekday number (1-7) with Monday is 1; %w = weekday number (0-6) with Sunday is 0',
];
return $in;
}
else {
my $out = [
'%Y-%m-%d %H:%M:%S',
'%Y-%m-%d %H:%M:%S.%6N%z',
$sf->{i}{dbms} eq 'Oracle' ? '%d-%b-%y %I.%M.%S.%6N %p %Z' : '%a %d %b %Y %I:%M:%S.%6N %p %Z'
lib/App/DBBrowser/GetContent/Parse.pm view on Meta::CPAN
if ( ! defined $book ) {
if ( ! eval {
$book = Spreadsheet::Read::ReadData( $file_fs, cells => 0, attr => 0, rc => 1, strip => 0 );
1 }
) {
die "Spreadsheet::Read: $@";
}
if ( ! defined $book ) {
$tc->choose(
[ 'Press ENTER' ],
{ prompt => 'No Book in ' . decode( 'locale_fs', $file_fs ) . '!' }
);
return;
}
}
my $sheet_count = @$book - 1; # first sheet in $book contains meta info
if ( $sheet_count == 0 ) {
$tc->choose(
[ 'Press ENTER' ],
{ prompt => 'No Sheets in ' . decode( 'locale_fs', $file_fs ) . '!' }
);
return;
}
my $sheet_idx;
if ( $sheet_count == 1 ) {
$sheet_idx = 1;
}
else {
$source->{saved_book} = $book; # save book if more than one sheet
my @sheets = map { '- ' . ( length $book->[$_]{label} ? $book->[$_]{label} : 'sheet_' . $_ ) } 1 .. $#$book;
lib/App/DBBrowser/GetContent/Source.pm view on Meta::CPAN
my $ax = App::DBBrowser::Auxil->new( $sf->{i}, $sf->{o}, $sf->{d} );
if ( ! defined $dir ) {
return [];
}
if ( ! -d $dir ) {
my $message = "The directory '$dir' does not exist any more.";
$ax->print_error_message( $message );
$sf->__remove_from_history( $dir );
return [];
}
my $dir_fs = realpath( encode( 'locale_fs', $dir ) ) or die "$dir: $!";
my @tmp_files_fs;
if ( length $sf->{o}{insert}{file_filter} ) {
@tmp_files_fs = map { basename $_} grep { -e $_ } glob( catfile( $dir_fs, $sf->{o}{insert}{file_filter} ) );
}
else {
opendir( my $dh, $dir_fs ) or die "$dir_fs: $!";
@tmp_files_fs = readdir $dh;
closedir $dh;
}
my $files = [];
for my $file_fs ( sort @tmp_files_fs ) {
next if $file_fs =~ /^\./ && ! $sf->{o}{insert}{show_hidden_files};
next if -d catdir $dir_fs, $file_fs;
push @$files, decode( 'locale_fs', catfile $dir_fs, $file_fs );
}
return $files;
}
sub __avail_directories {
my ( $sf ) = @_;
my $ax = App::DBBrowser::Auxil->new( $sf->{i}, $sf->{o}, $sf->{d} );
my $h_ref = $ax->read_json( $sf->{i}{f_dir_history} ) // {};
my @dirs = @{$h_ref->{dirs}//[]};
lib/App/DBBrowser/Options.pm view on Meta::CPAN
}
}
}
}
sub __display_info {
my ( $sf ) = @_;
my $tc = Term::Choose->new( $sf->{i}{tc_default} );
my $app_dir = $sf->{i}{app_dir};
eval { $app_dir = decode( 'locale', $app_dir ) };
my $info = 'db-browser' . "\n\n";
$info .= 'Version: ' . $main::VERSION . "\n\n";
$info .= 'Path: ' . catfile( $RealBin, $RealScript ) . "\n\n";
$info .= 'App-Dir: ' . $app_dir . "\n";
$tc->choose( [ ' << ' ], { prompt => $info, color => 1, margin => [ 1, 1, 1, 1 ] } );
}
lib/App/DBBrowser/Table.pm view on Meta::CPAN
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 {
lib/App/DBBrowser/Table.pm view on Meta::CPAN
if ( ! defined $chosen ) {
return;
}
elsif ( $chosen eq $new_name ) {
next FILE_NAME;
}
}
my ( $yes, $no ) = ( '- YES', '- NO' );
my $hidden;
if ( defined $chosen && $chosen eq $overwrite ) {
$hidden = 'Overwrite "' . decode( 'locale_fs', $file_fs ) . '"?';
}
else {
$hidden = 'Write data to "' . decode( 'locale_fs', $file_fs ) . '"?';
}
# Choose
my $choice = $tc->choose(
[ $hidden, undef, $yes, $no ],
{ info => $info, prompt => '', default => 1, layout => 2, undef => ' <<' }
);
$ax->print_sql_info( $info );
if ( ! defined $choice ) {
next FILE_NAME;
}
lib/App/DBBrowser/Table/Extensions/ScalarFunctions/String.pm view on Meta::CPAN
sub __pg_col_to_text {
my ( $sf, $sql, $col ) = @_;
my $ax = App::DBBrowser::Auxil->new( $sf->{i}, $sf->{o}, $sf->{d} );
return $ax->pg_column_to_text( $sql, $col );
}
# string units: $position, $instr, $locate, $left, $right, $length, $substring, $upper, $lower ##
# $round datetime: locale
1;