#!/usr/bin/env perl
use warnings;
use strict;
use 5.10.1;
use open qw(:utf8);
no warnings 'utf8';

our $VERSION = '0.016';

use Encode                qw(encode decode);
use File::Basename        qw(basename);
use File::Find            qw(find);
use File::Spec::Functions qw(catfile catdir rel2abs);
use Getopt::Long          qw(GetOptions);
use List::Util            qw(sum);
use Pod::Usage            qw(pod2usage);
use Scalar::Util          qw(looks_like_number);

use Clone               qw(clone);
use DBI                 qw();
use Encode::Locale      qw(decode_argv);
use JSON::XS            qw();
use File::HomeDir       qw(my_home);
use List::MoreUtils     qw(any first_index pairwise);
use Term::ReadKey       qw(ReadKey ReadMode GetTerminalSize);
use Term::ProgressBar   qw();
use Text::LineFold      qw();
use Unicode::GCString   qw();

binmode STDIN,  ':encoding(console_in)';
binmode STDOUT, ':encoding(console_out)';
binmode STDERR, ':encoding(console_out)';

BEGIN {
    decode_argv(); # not at the end of the BEGIN block if perl < 5.16.0
    if ( $^O eq 'MSWin32' ) {
        require Term::Choose::Win32;
        Term::Choose::Win32::->import( 'choose' );
        require Term::Size::Win32;
        Term::Size::Win32::->import( 'chars' );
        require Win32::Console::ANSI;
    }
    else {
        require Term::Choose;
        Term::Choose::->import( 'choose' );
    }
}

sub term_size {
    if ( $^O eq 'MSWin32' ) {
        my ( $width, $heigth ) = chars();
        return $width - 1, $heigth;
    }
    else {
        return GetTerminalSize();
    }
}


END { ReadMode 0 }

use constant {
    BSPACE         => 0x7f,
    GO_TO_TOP_LEFT => "\e[1;1H",
    CLEAR_EOS      => "\e[0J",
};
use constant {
    v   => 0,
    chs => 1,
};


my $home = decode( 'locale', File::HomeDir->my_home() );
if ( ! $home ) {
    say "'File::HomeDir->my_home()' could not find the home directory!";
    say "A home directory is needed to be able to use 'db-browser'";
    exit;
}
my $my_data = decode( 'locale', File::HomeDir->my_data() );
my $app_dir = $my_data ? catdir( $my_data, 'db_browser_conf' ) : catdir( $home, '.db_browser_conf' );
mkdir $app_dir or die $! if ! -d $app_dir;

my $info = {
    home              => $home,
    app_dir           => $app_dir,
    config_file       => catfile( $app_dir, 'config.json' ),
    db_cache_file     => catfile( $app_dir, 'cache_db_search.json' ),
    lyt_1             => { layout => 1, order => 0, justify => 2, clear_screen => 1, undef => '<<'                  },
    lyt_stmt_h        => { layout => 1, order => 0, justify => 2, clear_screen => 1, undef => '<<',     lf => [0,4] },
    lyt_3             => { layout => 3,             justify => 0, clear_screen => 1, undef => '  BACK'              },
    lyt_stmt_v        => { layout => 3,             justify => 0, clear_screen => 1, undef => '  BACK', lf => [0,4] },
    lyt_stop          => {                                        clear_screen => 0                                 },
    back              => 'BACK',
    confirm           => 'CONFIRM',
    ok                => '- OK -',
    _exit             => '  EXIT',
    _help             => '  HELP',
    _back             => '  BACK',
    _confirm          => '  CONFIRM',
    _continue         => '  CONTINUE',
    _info             => '  INFO',
    _reset            => '  RESET',
    line_fold         => { Charset=> 'utf8', OutputCharset => '_UNICODE_', Urgent => 'FORCE' },
    avail_aggregate   => [ "AVG(X)", "COUNT(X)", "COUNT(*)", "MAX(X)", "MIN(X)", "SUM(X)" ],
    cached            => '',
    binary_string     => 'BNRY',
    avail_operators   => [ "REGEXP", "NOT REGEXP", "LIKE", "NOT LIKE", "IS NULL", "IS NOT NULL", "IN", "NOT IN",
                           "BETWEEN", "NOT BETWEEN", " = ", " != ", " <> ", " < ", " > ", " >= ", " <= ", "LIKE col",
                           "NOT LIKE col", "LIKE %col%", "NOT LIKE %col%", " = col", " != col", " <> col", " < col",
                           " > col", " >= col", " <= col" ],
                           # "LIKE col%", "NOT LIKE col%", "LIKE %col", "NOT LIKE %col",
    avail_db_drivers  => [ 'SQLite', 'mysql', 'Pg' ],
    hidd_func_pr      => { Epoch_to_Date => 'DATE', Truncate => 'TRUNC', Epoch_to_DateTime => 'DATETIME',
                           Bit_Length => 'BIT_LENGTH', Char_Length => 'CHAR_LENGTH' },
    keys_hidd_func_pr => [ qw( Epoch_to_Date Bit_Length Truncate Char_Length Epoch_to_DateTime ) ],
};

my $my_db_drivers = [ 'SQLite', 'mysql', 'Pg' ];
my $my_operators  = [ "REGEXP", " = ", " != ", " < ", " > ", "IS NULL", "IS NOT NULL", "IN" ];

my $defaults = {
    print => {
        tab_width      => [ 2,       '- Tabwidth' ],
        min_col_width  => [ 30,      '- Colwidth' ],
        undef          => [ '',      '- Undef' ],
        limit          => [ 50_000,  '- Max Rows' ],
        progress_bar   => [ 20_000,  '- ProgressBar' ],
    },
    sql => {
        lock_stmt      => [ 0, '- Lock' ],
        system_info    => [ 0, '- Metadata' ],
        regexp_case    => [ 0, '- Regexp Case' ],
        parentheses    => [ 1,             '- Parentheses' ],
            w_parentheses  => [ 0, '- Parentheses in WHERE' ],
            h_parentheses  => [ 0, '- Parentheses in HAVING TO' ],
    },
    menu => {
        thsd_sep       => [ ',',           '- Separator' ],
        db_drivers     => [ $my_db_drivers,'- DB Drivers' ],
        operators      => [ $my_operators, '- Operators' ],
        sssc_mode      => [ 0,             '- Sssc Mode' ],
        mouse          => [ 0,             '- Mouse Mode' ],
        expand         => [ 1,             '- Expand' ],
            keep_db_choice     => [ 0, '- Choose Database' ],
            keep_schema_choice => [ 0, '- Choose Schema' ],
            keep_table_choice  => [ 0, '- Choose Table' ],
            table_expand       => [ 1, '- Print  Table' ],
    },
    dbs => {
        login               => [ 0,       '- DB Login' ],
        db_defaults         => [ 'Dummy', '- DB Defaults' ],
    },
    SQLite => {
        reset_cache                => [ 0, '- New Search' ],
        sqlite_unicode             => [ 1, '- Unicode' ],
        sqlite_see_if_its_a_number => [ 1, '- See if its a number' ],
        binary_filter              => [ 0, '- Binary Filter' ],
    },
    mysql => {
        default_user        => [ '', '- Default User' ],
        host                => [ '', '- Host' ],
        port                => [ '', '- Port' ],
        mysql_enable_utf8   => [ 1,  '- Enable utf8' ],
        binary_filter       => [ 0,  '- Binary Filter' ],
    },
    Pg => {
        default_user        => [ '', '- Default User' ],
        host                => [ '', '- Host' ],
        port                => [ '', '- Port' ],
        pg_enable_utf8      => [ 1,  '- Enable utf8' ],
        binary_filter       => [ 0,  '- Binary Filter' ],
    }
};

$info->{option_sections} = [ qw( print sql menu dbs ) ];
$info->{print}{keys}     = [ qw( tab_width min_col_width undef limit progress_bar ) ];
$info->{sql}{keys}       = [ qw( lock_stmt system_info regexp_case parentheses ) ];
$info->{dbs}{keys}       = [ qw( login db_defaults ) ];
$info->{menu}{keys}      = [ qw( thsd_sep db_drivers operators sssc_mode expand  mouse ) ];
$info->{db_sections}     = [ qw( SQLite mysql Pg ) ];
$info->{SQLite}{keys}    = [ qw( reset_cache sqlite_unicode sqlite_see_if_its_a_number binary_filter ) ];
$info->{mysql}{keys}     = [ qw( default_user mysql_enable_utf8 binary_filter host port ) ];
$info->{Pg}{keys}        = [ qw( default_user pg_enable_utf8 binary_filter host port ) ];

$info->{sub_expand}{menu}     = [ qw( keep_db_choice keep_schema_choice keep_table_choice table_expand ) ];
$info->{sub_parentheses}{sql} = [ qw( w_parentheses h_parentheses ) ];
$info->{cmdline_only}{SQLite} = [ qw( reset_cache ) ];

my $opt;
if ( ! eval {
    $opt = read_config_file( $defaults, $info->{config_file} );
    #for my $section ( keys %$opt ) {
    #    for my $key ( keys %{$opt->{$section}} ) {
    #        if ( ! defined $defaults->{$section}{$key}[chs] ) {
    #            delete $opt->{$section}{$key};
    #        }
    #    }
    #}
    for my $lyt ( grep { /^lyt_/ } keys %$info ) {
        $info->{$lyt}{mouse} = $opt->{menu}{mouse}[v];
    }
    my $help;
    GetOptions (
        'h|?|help' => \$help,
        's|search' => \$opt->{SQLite}{reset_cache}[v],
    );
    $opt = options( $info, $opt ) if $help;
    1 }
) {
    say 'Configfile/Options:';
    print_error_message( $@ );
    $opt = $defaults;
}

for my $lyt ( grep { /^lyt_/ } keys %$info ) {
    $info->{$lyt}{mouse} = $opt->{menu}{mouse}[v];
}
$info->{ok} = '<OK>' if $opt->{menu}{sssc_mode}[v];
my $sqlite_search = 0;
$sqlite_search = 1 if $opt->{SQLite}{reset_cache}[v];
$sqlite_search = 1 if @ARGV;


DB_DRIVER: while ( 1 ) {

    if ( $sqlite_search ) {
        $info->{db_driver} = 'SQLite';
        $sqlite_search = 0;
    }
    else {
        if ( @{$opt->{menu}{db_drivers}[v]} == 1 ) {
            $info->{one_db_driver} = 1;
            $info->{db_driver} = $opt->{menu}{db_drivers}[v][0];
        }
        else {
            $info->{one_db_driver} = 0;
            # Choose
            $info->{db_driver} = choose(
                [ undef, @{$opt->{menu}{db_drivers}[v]} ],
                { prompt => 'Database Driver: ', %{$info->{lyt_1}}, undef => 'Quit' }
            );
            last DB_DRIVER if ! defined $info->{db_driver};
        }
    }

    $info->{cached} = '';
    my $db_driver = $info->{db_driver};
    my $databases = [];
    if ( ! eval {
        $databases = available_databases( $info, $opt );
        1 }
    ) {
        say 'Available databases:';
        delete $info->{login}{$db_driver};
        print_error_message( $@ );
        next DB_DRIVER;
    }
    if ( ! @$databases ) {
        print_error_message( "no $db_driver-databases found\n" );
        next DB_DRIVER;
    }
    my $regexp_system;
    $regexp_system = regexp_system( $db_driver, 'database' ) if $opt->{sql}{system_info}[v];
    if ( defined $regexp_system ) {
        my $regexp = join( '|', @$regexp_system );
        my ( @data, @system );
        for my $database ( @{$databases} ) {
            if ( $database =~ /(?:$regexp)/ ) {
                push @system, $database;
            }
            else {
                push @data, $database;
            }
        }
        if ( $db_driver eq 'SQLite' ) {
            $databases = [ @data, @system ];
        }
        else {
            $databases = [ map( "- $_", @data ), map( "  $_", @system ) ];
        }
    }
    else {
        if ( $db_driver ne 'SQLite' ) {
            $databases = [ map{ "- $_" } @$databases ];
        }
    }

    my $data = {};
    my $old_idx_db = 0;
    my $new_db_settings = 0;
    my $db;

    DATABASE: while ( 1 ) {

        if ( $new_db_settings ) {
            $new_db_settings = 0;
            $data = {};
        }
        else {
            my $back = ( $db_driver eq 'SQLite' ? '' : ' ' x 2 ) . ( $info->{one_db_driver} ? 'Quit' : 'BACK' );
            my $prompt = 'Choose Database' . $info->{cached};
            my $choices = [ undef, @$databases ];
            # Choose
            my $idx_db = choose(
                $choices,
                { prompt => $prompt, %{$info->{lyt_3}}, index => 1, default => $old_idx_db, undef => $back }
            );
            $db = undef;
            $db = $choices->[$idx_db] if defined $idx_db;
            if ( ! defined $db ) {
                last DB_DRIVER if   $info->{one_db_driver};
                next DB_DRIVER if ! $info->{one_db_driver};
            }
            if ( $opt->{menu}{keep_db_choice}[v] ) {
                if ( $old_idx_db == $idx_db ) {
                    $old_idx_db = 0;
                    next DATABASE;
                }
                else {
                    $old_idx_db = $idx_db;
                }
            }
            $db =~ s/^[-\ ]\s// if $db_driver ne 'SQLite';
        }

        my $dbh;
        if ( ! eval {
            $dbh = get_db_handle( $info, $opt, $db );
            $data->{$db}{schemas} = get_schema_names( $info, $opt, $dbh, $db ) if ! defined $data->{$db}{schemas};
            1 }
        ) {
            say 'Get database handle and schema names:';
            delete $info->{login}{$db_driver}{$db};
            print_error_message( $@ );
            # remove database from @databases
            next DATABASE;
        }
        my $old_idx_sch = 0;

        SCHEMA: while ( 1 ) {

            my $schema;
            if ( @{$data->{$db}{schemas}} == 1 ) {
                $schema = $data->{$db}{schemas}[0];
            }
            elsif ( @{$data->{$db}{schemas}} > 1 ) {
                my $choices;
                my $idx_sch;
                my $regexp_system;
                $regexp_system = regexp_system( $db_driver, 'schema' ) if $opt->{sql}{system_info}[v];
                if ( defined $regexp_system ) {
                    my $regexp = join( '|', @$regexp_system );
                    my ( @data, @system );
                    for my $schema ( @{$data->{$db}{schemas}} ) {
                        if ( $schema =~ /(?:$regexp)/ ) {
                            push @system, $schema;
                        }
                        else {
                            push @data, $schema;
                        }
                    }
                    $choices = [ undef, map( "- $_", @data ), map( "  $_", @system ) ];
                }
                else {
                    $choices = [ undef, map( "- $_", @{$data->{$db}{schemas}} ) ];
                }
                my $prompt = 'DB "'. basename( $db ) . '" - choose Schema:';
                # Choose
                $idx_sch = choose(
                    $choices,
                    { prompt => $prompt, %{$info->{lyt_3}}, index => 1, default => $old_idx_sch }
                );
                $schema = $choices->[$idx_sch] if defined $idx_sch;
                next DATABASE if ! defined $schema;
                if ( $opt->{menu}{keep_schema_choice}[v] ) {
                    if ( $old_idx_sch == $idx_sch ) {
                        $old_idx_sch = 0;
                        next SCHEMA;
                    }
                    else {
                        $old_idx_sch = $idx_sch;
                    }
                }
                $schema =~ s/^[-\ ]\s//;
            }

            if ( ! eval {
                if ( ! defined $data->{$db}{$schema}{tables} ) {
                    $data->{$db}{$schema}{tables} = get_table_names( $info, $opt, $dbh, $db, $schema );
                }
                1 }
            ) {
                say 'Get table names:';
                print_error_message( $@ );
                next DATABASE;
            }

            my $join       = '  Join';
            my $union      = '  Union';
            my $db_setting = '  Database settings';
            my @tables = ();
            my $regexp_system;
            $regexp_system = regexp_system( $db_driver, 'table' ) if $opt->{sql}{system_info}[v];
            if ( defined $regexp_system ) {
                my $regexp = join( '|', @$regexp_system );
                my ( @data, @system );
                for my $table ( @{$data->{$db}{$schema}{tables}} ) {
                    if ( $table =~ /(?:$regexp)/ ) {
                        push @system, $table;
                    }
                    else {
                        push @data, $table;
                    }
                }
                @tables = ( map( "- $_", @data ), map( "  $_", @system ) );
            }
            else {
                @tables = map { "- $_" } @{$data->{$db}{$schema}{tables}};
            }
            my $old_idx_tbl = 0;

            TABLE: while ( 1 ) {

                my $prompt = 'DB: "'. basename( $db );
                $prompt .= '.' . $schema if defined $data->{$db}{schemas} && @{$data->{$db}{schemas}} > 1;
                $prompt .= '"';
                my $choices = [ undef, @tables, $join, $union, $db_setting ];
                # Choose
                my $idx_tbl = choose(
                    $choices,
                    { prompt => $prompt, %{$info->{lyt_3}}, index => 1, default => $old_idx_tbl }
                );
                my $table;
                $table = $choices->[$idx_tbl] if defined $idx_tbl;
                if ( ! defined $table ) {
                    next SCHEMA if defined $data->{$db}{schemas} && @{$data->{$db}{schemas}} > 1;
                    next DATABASE;
                }
                if ( $opt->{menu}{keep_table_choice}[v] ) {
                    if ( $old_idx_tbl == $idx_tbl ) {
                        $old_idx_tbl = 0;
                        next TABLE;
                    }
                    else {
                        $old_idx_tbl = $idx_tbl;
                    }
                }
                my $multi_table;
                if ( $table eq $db_setting ) {
                    if ( ! eval {
                        $new_db_settings = database_setting( $info, $opt, $db );
                        1 }
                    ) {
                        say 'Database settings:';
                        print_error_message( $@ );
                    }
                    next DATABASE if $new_db_settings;
                    next TABLE;
                }
                elsif ( $table eq $join ) {
                    if ( ! eval {
                        $multi_table = join_tables( $info, $dbh, $db, $schema, $data );
                        $table = 'joined_tables';
                        1 }
                    ) {
                        say 'Join tables:';
                        print_error_message( $@ );
                    }
                    next TABLE if ! defined $multi_table;
                }
                elsif ( $table eq $union ) {
                    if ( ! eval {
                        $multi_table = union_tables( $info, $dbh, $db, $schema, $data );
                        $table = 'union_tables';
                        1 }
                    ) {
                        say 'Union tables:';
                        print_error_message( $@ );
                    }
                    next TABLE if ! defined $multi_table;
                }
                else {
                    $table =~ s/^[-\ ]\s//;
                }
                if ( ! eval {
                    my $qt_columns = {};
                    my $pr_columns = [];
                    my $sql;
                    $sql->{strg_keys} = [ qw( distinct_stmt where_stmt group_by_stmt having_stmt order_by_stmt limit_stmt ) ];
                    $sql->{list_keys} = [ qw( chosen_cols aggr_cols where_args group_by_cols having_args limit_args hidd ) ];
                    reset_sql( $sql, $qt_columns );

                    $info->{lock} = $opt->{sql}{lock_stmt}[v];

                    my $select_from_stmt = '';
                    if ( $multi_table ) {
                        $select_from_stmt = $multi_table->{quote}{stmt};
                        for my $col ( @{$multi_table->{pr_columns}} ) {
                            $qt_columns->{$col} = $multi_table->{qt_columns}{$col};
                            push @$pr_columns, $col;
                        }
                    }
                    else {
                        $select_from_stmt = "SELECT * FROM " . $dbh->quote_identifier( undef, $schema, $table );
                        my $sth = $dbh->prepare( $select_from_stmt . " WHERE 1 = 0" );
                        $sth->execute();
                        for my $col ( @{$sth->{NAME}} ) {
                            $qt_columns->{$col} = $dbh->quote_identifier( $col );
                            push @$pr_columns, $col;
                        }
                    }

                    PRINT_TABLE: while ( 1 ) {
                        my $all_arrayref = read_table( $info, $opt, $sql, $dbh, $table,
                                                       $select_from_stmt, $qt_columns, $pr_columns );
                        last PRINT_TABLE if ! defined $all_arrayref;
                        delete @{$info}{qw(width_head width_cols not_a_number)};
                        func_print_tbl( $info, $opt, $db, $all_arrayref );
                    }

                    1 }
                ) {
                    say 'Print table:';
                    print_error_message( $@ );
                }
            }
        }
        $dbh->disconnect();
    }
}


sub union_tables {
    my ( $info, $dbh, $db, $schema, $data ) = @_;
    my $u = $data->{$db}{$schema};
    if ( ! defined $u->{col_names} || ! defined $u->{col_types} ) {
        $data = column_names_and_types( $info, $dbh, $db, $schema, $data );
    }
    my $union = {
        unused_tables => [ map { "- $_" } @{$u->{tables}} ],
        used_tables   => [],
        used_cols     => {},
        saved_cols    => [],
    };

    UNION_TABLE: while ( 1 ) {
        my $enough_tables = '  Enough TABLES';
        my $prompt = print_union_statement( $info, $union, 'Choose UNION table:' );
        # Choose
        my $union_table = choose(
            [ undef, $enough_tables, map( "+ $_", @{$union->{used_tables}} ), @{$union->{unused_tables}}, $info->{_info} ],
            { prompt => $prompt, %{$info->{lyt_stmt_v}} }
        );
        if ( ! defined $union_table ) {
            return;
        }
        elsif ( $union_table eq $info->{_info} ) {
            if ( ! defined $u->{tables_info} ) {
                $u->{tables_info} = get_tables_info( $info, $dbh, $db, $schema, $data );
            }
            my $tbls_info = print_tables_info( $u );
            choose( $tbls_info, { prompt => '', %{$info->{lyt_3}} } );
            next UNION_TABLE;
        }
        elsif ( $union_table eq $enough_tables ) {
            return if ! @{$union->{used_tables}};
            last UNION_TABLE;
        }
        my $backup_union = clone( $union );
        my $idx = first_index { $_ eq $union_table } @{$union->{unused_tables}};
        $union_table =~ s/^[-+]\s//;
        if ( $idx == -1 ) {
            delete $union->{used_cols}{$union_table};
        }
        else {
            splice( @{$union->{unused_tables}}, $idx, 1 );
            push @{$union->{used_tables}}, $union_table;
        }

        UNION_COLUMN: while ( 1 ) {
            my ( $all_cols, $privious_cols, $void ) = ( q['*'], q['^'], q[' '] );
            my @short_cuts = ( ( @{$union->{saved_cols}} ? $privious_cols : $void ), $all_cols );
            my $prompt = print_union_statement( $info, $union, 'Choose Column:' );
            my $choices = [ $info->{ok}, @short_cuts, @{$u->{col_names}{$union_table}} ];
            unshift @$choices, undef if $opt->{menu}{sssc_mode}[v];
            # Choose
            my $col = choose(
                $choices,
                { prompt => $prompt, %{$info->{lyt_stmt_h}} }
            );
            if ( ! defined $col ) {
                if ( defined $union->{used_cols}{$union_table} ) {
                    delete $union->{used_cols}{$union_table};
                    next UNION_COLUMN;
                }
                else {
                    $union = clone( $backup_union );
                    last UNION_COLUMN;
                }
            }
            if ( $col eq $info->{ok} ) {
                if ( ! defined $union->{used_cols}{$union_table} ) {
                    my $idx = first_index { $_ eq $union_table } @{$union->{used_tables}};
                    my $tbl = splice( @{$union->{used_tables}}, $idx, 1 );
                    push @{$union->{unused_tables}}, "- $tbl";
                }
                last UNION_COLUMN;
            }
            if ( $col eq $void ) {
                next UNION_COLUMN;
            }
            if ( $col eq $privious_cols ) {
                $union->{used_cols}{$union_table} = $union->{saved_cols};
                next UNION_COLUMN if $opt->{menu}{sssc_mode}[v];
                last UNION_COLUMN;
            }
            if ( $col eq $all_cols ) {
                @{$union->{used_cols}{$union_table}} = @{$u->{col_names}{$union_table}};
                next UNION_COLUMN if $opt->{menu}{sssc_mode}[v];
                last UNION_COLUMN;
            }
            else {
                push @{$union->{used_cols}{$union_table}}, $col;
            }
        }
        $union->{saved_cols} = $union->{used_cols}{$union_table} if defined $union->{used_cols}{$union_table};
    }
    # column names in the result-set of a UNION are taken from the first query.
    my $first_table = $union->{used_tables}[0];
    $union->{pr_columns} = $union->{used_cols}{$first_table};
    for my $col ( @{$union->{pr_columns}} ) {
        $union->{qt_columns}{$col} = $dbh->quote_identifier( $col );
    }
    $union->{quote}{stmt} = "SELECT * FROM (";
    my $c;
    for my $table ( @{$union->{used_tables}} ) {
        $c++;
        $union->{quote}{stmt} .= " SELECT ";
        $union->{quote}{stmt} .= join( ', ', map { $dbh->quote_identifier( $_ ) } @{$union->{used_cols}{$table}} );
        $union->{quote}{stmt} .= " FROM " . $dbh->quote_identifier( undef, $schema, $table );
        $union->{quote}{stmt} .= $c < @{$union->{used_tables}} ? " UNION ALL " : " )";
    }
    $union->{quote}{stmt} .= " AS " . join '_', @{$union->{used_tables}};
    return $union;
}


sub print_union_statement {
    my ( $info, $union, $prompt ) = @_;
    my $str = "SELECT * FROM (\n";
    my $c = 0;
    for my $table ( @{$union->{used_tables}} ) {
        ++$c;
        $str .= "  SELECT ";
        $str .= @{$union->{used_cols}{$table}//[]} ? join( ', ', @{$union->{used_cols}{$table}} ) : '?';
        $str .= " FROM $table";
        $str .= $c < @{$union->{used_tables}} ? " UNION ALL\n" : "\n";
    }
    if ( @{$union->{used_tables}} ) {
        $str .= ") AS " . join( '_', @{$union->{used_tables}} ) . " \n";
    }
    return $str . "\n$prompt";
}


sub get_tables_info {
    my ( $info, $dbh, $db, $schema, $data ) = @_;
    my $tables_info = {};
    my $sth;
    my ( $pk, $fk ) = primary_and_foreign_keys( $info, $dbh, $db, $schema, $data );
    for my $table ( @{$data->{$db}{$schema}{tables}} ) {
        push @{$tables_info->{$table}}, [ 'Table: ', '== ' . $table . ' ==' ];
        push @{$tables_info->{$table}}, [
            'Columns: ',
            join( ' | ',
                pairwise { no warnings q(once); lc( $a ) . ' ' . $b }
                    @{$data->{$db}{$schema}{col_types}{$table}},
                    @{$data->{$db}{$schema}{col_names}{$table}}
            )
        ];
        if ( @{$pk->{$table}} ) {
            push @{$tables_info->{$table}}, [ 'PK: ', 'primary key (' . join( ',', @{$pk->{$table}} ) . ')' ];
        }
        for my $fk_name ( sort keys %{$fk->{$table}} ) {
            if ( $fk->{$table}{$fk_name} ) {
                push @{$tables_info->{$table}}, [
                    'FK: ',
                    'foreign key (' . join( ',', @{$fk->{$table}{$fk_name}{foreign_key_col}} ) .
                    ') references ' . $fk->{$table}{$fk_name}{reference_table} .
                    '(' . join( ',', @{$fk->{$table}{$fk_name}{reference_key_col}} ) .')'
                ];
            }
        }
    }
    return $tables_info;
}


sub print_tables_info {
    my ( $ref ) = @_;
    my $len_key = 10;
    my $col_max = ( term_size() )[0] - 1;
    my $line_fold = Text::LineFold->new( %{$info->{line_fold}} );
    $line_fold->config( 'ColMax', $col_max > 140 ? 140 : $col_max );
    my $ch_info = [ 'Close with ENTER' ];
    for my $table ( @{$ref->{tables}} ) {
        push @{$ch_info}, " ";
        for my $line ( @{$ref->{tables_info}{$table}} ) {
            my $text = sprintf "%*s%s", $len_key, @$line;
            $text = $line_fold->fold( '' , ' ' x $len_key, $text );
            push @{$ch_info}, split /\R+/, $text;
        }
    }
    return $ch_info;
}


sub join_tables {
    my ( $info, $dbh, $db, $schema, $data ) = @_;
    my $join = {};
    $join->{quote}{stmt} = "SELECT * FROM";
    $join->{print}{stmt} = "SELECT * FROM";
    my $j = $data->{$db}{$schema};
    if ( ! defined $j->{col_names} || ! defined $j->{col_types} ) {
        $data = column_names_and_types( $info, $dbh, $db, $schema, $data );
    }
    my @tables = map { "- $_" } @{$j->{tables}};

    MASTER: while ( 1 ) {
        my $prompt = print_join_statement( $info, $join->{print}{stmt}, 'Choose MASTER table:' );
        # Choose
        my $master = choose(
            [ undef, @tables, $info->{_info} ],
            { prompt => $prompt, %{$info->{lyt_stmt_v}} }
        );
        if ( ! defined $master ) {
            return;
        }
        if ( $master eq $info->{_info} ) {
            if ( ! defined $j->{tables_info} ) {
                $j->{tables_info} = get_tables_info( $info, $dbh, $db, $schema, $data );
            }
            my $tbls_info = print_tables_info( $j );
            # Choose
            choose( $tbls_info, { prompt => '', %{$info->{lyt_3}} } );
            next MASTER;
        }
        my $idx = first_index { $_ eq $master } @tables;
        splice( @tables, $idx, 1 );
        $master =~ s/^-\s//;
        $join->{used_tables}  = [ $master ];
        $join->{avail_tables} = [ @tables ];
        $join->{quote}{stmt}  = "SELECT * FROM " . $dbh->quote_identifier( undef, $schema, $master );
        $join->{print}{stmt}  = "SELECT * FROM " .                                         $master  ;
        $join->{primary_keys} = [];
        $join->{foreign_keys} = [];
        my $backup_master = clone( $join );

        JOIN: while ( 1 ) {
            my $enough_slaves = '  Enough TABLES';
            my $slave;
            my $backup_join = clone( $join );

            SLAVE: while ( 1 ) {
                my $prompt = print_join_statement( $info, $join->{print}{stmt}, 'Add a SLAVE table:' );
                # Choose
                $slave = choose(
                    [ undef, $enough_slaves, @{$join->{avail_tables}}, $info->{_info} ],
                    { prompt => $prompt, %{$info->{lyt_stmt_v}}, undef => $info->{_reset} }
                );
                if ( ! defined $slave ) {
                    if ( @{$join->{used_tables}} == 1 ) {
                        @tables = map { "- $_" } @{$j->{tables}};
                        $join->{quote}{stmt} = "SELECT * FROM";
                        $join->{print}{stmt} = "SELECT * FROM";
                        next MASTER;
                    }
                    else {
                        $join = clone( $backup_master );
                        next JOIN;
                    }
                }
                elsif ( $slave eq $enough_slaves ) {
                    last JOIN;
                }
                elsif ( $slave eq $info->{_info} ) {
                    if ( ! defined $j->{tables_info} ) {
                        $j->{tables_info} = get_tables_info( $info, $dbh, $db, $schema, $data );
                    }
                    my $tbls_info = print_tables_info( $j );
                    choose( $tbls_info, { prompt => '', %{$info->{lyt_3}} } );
                    next SLAVE;
                }
                else {
                    last SLAVE;
                }
            }
            my $idx = first_index { $_ eq $slave } @{$join->{avail_tables}};
            splice( @{$join->{avail_tables}}, $idx, 1 );
            $slave =~ s/^-\s//;
            $join->{quote}{stmt} .= " LEFT OUTER JOIN " . $dbh->quote_identifier( undef, $schema, $slave ) . " ON";
            $join->{print}{stmt} .= " LEFT OUTER JOIN " .                                         $slave   . " ON";
            my %avail_pk_cols = ();
            for my $used_table ( @{$join->{used_tables}} ) {
                for my $col ( @{$j->{col_names}{$used_table}} ) {
                    $avail_pk_cols{ $used_table . '.' . $col } = $dbh->quote_identifier( undef, $used_table, $col );
                }
            }
            my %avail_fk_cols = ();
            for my $col ( @{$j->{col_names}{$slave}} ) {
                $avail_fk_cols{ $slave . '.' . $col } = $dbh->quote_identifier( undef, $slave, $col );
            }
            my $AND = '';

            ON: while ( 1 ) {
                my $prompt = print_join_statement( $info, $join->{print}{stmt}, 'Choose PRIMARY KEY column:' );
                # Choose
                my $pk_col = choose(
                    [ undef, $info->{_continue}, map( "- $_", sort keys %avail_pk_cols ) ],
                    { prompt => $prompt, %{$info->{lyt_stmt_v}}, undef => $info->{_reset} }
                );
                if ( ! defined $pk_col ) {
                    $join = clone( $backup_join );
                    next JOIN;
                }
                if ( $pk_col eq $info->{_continue} ) {
                    if ( @{$join->{primary_keys}} == @{$backup_join->{primary_keys}} ) {
                        $join = clone( $backup_join );
                        next JOIN;
                    }
                    last ON;
                }
                $pk_col =~ s/^-\s//;
                push @{$join->{primary_keys}}, $avail_pk_cols{$pk_col};
                $join->{quote}{stmt} .= $AND;
                $join->{print}{stmt} .= $AND;
                $join->{quote}{stmt} .= ' ' . $avail_pk_cols{$pk_col} . " =";
                $join->{print}{stmt} .= ' ' .                $pk_col  . " =";
                $prompt = print_join_statement( $info, $join->{print}{stmt}, 'Choose FOREIGN KEY column:' );
                # Choose
                my $fk_col = choose(
                    [ undef, map( "- $_", sort keys %avail_fk_cols ) ],
                    { prompt => $prompt, %{$info->{lyt_stmt_v}}, undef => $info->{_reset} }
                );
                if ( ! defined $fk_col ) {
                    $join = clone( $backup_join );
                    next JOIN;
                }
                $fk_col =~ s/^-\s//;
                push @{$join->{foreign_keys}}, $avail_fk_cols{$fk_col};
                $join->{quote}{stmt} .= ' ' . $avail_fk_cols{$fk_col};
                $join->{print}{stmt} .= ' ' .                $fk_col;
                $AND = " AND";
            }
            push @{$join->{used_tables}}, $slave;
        }
        last MASTER;
    }

    #my @not_unique_col;
    #my %seen;
    #for my $table (@{$join->{used_tables}} ) {
    #    for my $col ( @{$j->{col_names}{$table}} ) {
    #        $seen{$col}++;
    #        push @not_unique_col, $col if $seen{$col} == 2;
    #    }
    #}
    my $col_stmt = '';
    for my $table ( @{$join->{used_tables}} ) {
        for my $col ( @{$j->{col_names}{$table}} ) {
            my $col_qt = $dbh->quote_identifier( undef, $table, $col );
            my $col_pr = $col;
            if ( any { $_ eq $col_qt } @{$join->{foreign_keys}} ) {
                next;
            }
            #if ( any { $_ eq $col_pr } @not_unique_col ) {
                $col_pr .= '_' . $table;
                $col_qt .= " AS " . $col_pr;
            #}
            push @{$join->{pr_columns}}, $col_pr;
            $join->{qt_columns}{$col_pr} = $col_qt;
            $col_stmt .= ', ' . $col_qt;
        }
    }
    $col_stmt =~ s/^,\s//;
    $join->{quote}{stmt} =~ s/\s\*\s/ $col_stmt /;
    return $join;
}


sub print_join_statement {
    my ( $info, $join_stmt_pr, $prompt ) = @_;
    $join_stmt_pr =~ s/(?=\sLEFT\sOUTER\sJOIN)/\n\ /g;
    return $join_stmt_pr . "\n\n$prompt";
}



sub print_select_statement {
    my ( $info, $sql, $table, $prompt ) = @_;
    my $cols_sql;
    if ( $sql->{select_type} eq '*' ) {
        $cols_sql = ' *';
    }
    elsif ( $sql->{select_type} eq 'chosen_cols' ) {
        $cols_sql = ' ' . join( ', ', @{$sql->{print}{chosen_cols}} );
    }
    elsif ( @{$sql->{print}{aggr_cols}} || @{$sql->{print}{group_by_cols}} ) {
        $cols_sql = ' ' . join( ', ', @{$sql->{print}{group_by_cols}}, @{$sql->{print}{aggr_cols}} );
    }
    else {
        $cols_sql = ' *';
    }
    my $str = "SELECT";
    $str .= $sql->{print}{distinct_stmt} if $sql->{print}{distinct_stmt};
    $str .= $cols_sql . "\n";
    $str .= "  FROM $table" . "\n";
    $str .= ' ' . $sql->{print}{where_stmt}    . "\n" if $sql->{print}{where_stmt};
    $str .= ' ' . $sql->{print}{group_by_stmt} . "\n" if $sql->{print}{group_by_stmt};
    $str .= ' ' . $sql->{print}{having_stmt}   . "\n" if $sql->{print}{having_stmt};
    $str .= ' ' . $sql->{print}{order_by_stmt} . "\n" if $sql->{print}{order_by_stmt};
    $str .= ' ' . $sql->{print}{limit_stmt}    . "\n" if $sql->{print}{limit_stmt};
    $str .= "\n";
    $str .= $prompt if $prompt;
    return $str;
}


sub reset_sql {
    my ( $sql, $qt_columns ) = @_;
    $sql->{select_type} = '*';
    @{$sql->{print}}{ @{$sql->{strg_keys}} } = ( '' ) x  @{$sql->{strg_keys}};
    @{$sql->{quote}}{ @{$sql->{strg_keys}} } = ( '' ) x  @{$sql->{strg_keys}};
    @{$sql->{print}}{ @{$sql->{list_keys}} } = map{ [] } @{$sql->{list_keys}};
    @{$sql->{quote}}{ @{$sql->{list_keys}} } = map{ [] } @{$sql->{list_keys}};
    delete $sql->{backup_in_hidd};
}


sub read_table {
    my ( $info, $opt, $sql, $dbh, $table, $select_from_stmt, $qt_columns, $pr_columns ) = @_;
    my %lyt_stmt_h = %{$info->{lyt_stmt_h}};
    my @keys = ( qw( print_table columns aggregate distinct where group_by having order_by limit lock ) );
    my $lk = [ '  Lk0', '  Lk1' ];
    my %customize = (
        hidden          => 'Customize:',
        print_table     => 'Print TABLE',
        columns         => '- COLUMNS',
        aggregate       => '- AGGREGATE',
        distinct        => '- DISTINCT',
        where           => '- WHERE',
        group_by        => '- GROUP BY',
        having          => '- HAVING',
        order_by        => '- ORDER BY',
        limit           => '- LIMIT',
        lock            => $lk->[$info->{lock}],
    );
    my ( $DISTINCT, $ALL, $ASC, $DESC, $AND, $OR ) = ( "DISTINCT", "ALL", "ASC", "DESC", "AND", "OR" );
    if ( $info->{lock} == 0 ) {
        reset_sql( $sql, $qt_columns );
    }

    CUSTOMIZE: while ( 1 ) {
        my $backup_sql = clone( $sql );
        my $prompt = print_select_statement( $info, $sql, $table );
        # Choose
        my $custom = choose(
            [ $customize{hidden}, undef, @customize{@keys} ],
            { prompt => $prompt, %{$info->{lyt_stmt_v}}, default => 1, undef => $info->{back} }
        );
        if ( ! defined $custom ) {
            last CUSTOMIZE;
        }
        elsif ( $custom eq $customize{'lock'} ) {
            if ( $info->{lock} == 1 ) {
                $info->{lock} = 0;
                $customize{lock} = $lk->[0];
                reset_sql( $sql, $qt_columns );
            }
            elsif ( $info->{lock} == 0 )   {
                $info->{lock} = 1;
                $customize{lock} = $lk->[1];
            }
        }
        elsif( $custom eq $customize{'columns'} ) {
            if ( ! ( $sql->{select_type} eq '*' || $sql->{select_type} eq 'chosen_cols' ) ) {
                reset_sql( $sql, $qt_columns );
            }
            my @cols = ( @$pr_columns );
            $sql->{quote}{chosen_cols} = [];
            $sql->{print}{chosen_cols} = [];
            $sql->{select_type} = 'chosen_cols';

            COLUMNS: while ( 1 ) {
                my $prompt = print_select_statement( $info, $sql, $table, 'Choose:' );
                my $choices = [ $info->{ok}, @cols ];
                unshift @$choices, undef if $opt->{menu}{sssc_mode}[v];
                # Choose
                my $print_col = choose(
                    $choices,
                    { prompt => $prompt, %lyt_stmt_h }
                );
                if ( ! defined $print_col ) {
                    if ( @{$sql->{quote}{chosen_cols}} ) {
                        $sql->{quote}{chosen_cols} = [];
                        $sql->{print}{chosen_cols} = [];
                        delete $sql->{backup_in_hidd}{chosen_cols};
                        next COLUMNS;
                    }
                    else {
                        $sql = clone( $backup_sql );
                        last COLUMNS;
                    }
                }
                if ( $print_col eq $info->{ok} ) {
                    if ( ! @{$sql->{quote}{chosen_cols}} ) {
                        $sql->{select_type} = '*';
                    }
                    delete $sql->{backup_in_hidd}{chosen_cols};
                    $sql->{print}{hidd} = [];
                    last COLUMNS;
                }
                push @{$sql->{quote}{chosen_cols}}, $qt_columns->{$print_col};
                push @{$sql->{print}{chosen_cols}}, $print_col;
            }
        }
        elsif( $custom eq $customize{'distinct'} ) {
            $sql->{quote}{distinct_stmt} = '';
            $sql->{print}{distinct_stmt} = '';

            DISTINCT: while ( 1 ) {
                my $prompt = print_select_statement( $info, $sql, $table, 'Choose:' );
                my $choices = [ $info->{ok}, $DISTINCT, $ALL ];
                unshift @$choices, undef if $opt->{menu}{sssc_mode}[v];
                # Choose
                my $select_distinct = choose(
                    $choices,
                    { prompt => $prompt, %lyt_stmt_h }
                );
                if ( ! defined $select_distinct ) {
                    if ( $sql->{quote}{distinct_stmt} ) {
                        $sql->{quote}{distinct_stmt} = '';
                        $sql->{print}{distinct_stmt} = '';
                        next DISTINCT;
                    }
                    else {
                        $sql = clone( $backup_sql );
                        last DISTINCT;
                    }
                }
                if ( $select_distinct eq $info->{ok} ) {
                    last DISTINCT;
                }
                $sql->{quote}{distinct_stmt} = ' ' . $select_distinct;
                $sql->{print}{distinct_stmt} = ' ' . $select_distinct;
            }
        }
        elsif( $custom eq $customize{'aggregate'} ) {
            if ( $sql->{select_type} eq '*' || $sql->{select_type} eq 'chosen_cols' ) {
                reset_sql( $sql, $qt_columns );
            }
            my @cols = ( @$pr_columns );
            $sql->{quote}{aggr_cols} = [];
            $sql->{print}{aggr_cols} = [];
            $sql->{select_type} = 'aggr_cols';

            AGGREGATE: while ( 1 ) {
                my $prompt = print_select_statement( $info, $sql, $table, 'Choose:' );
                my $choices = [ $info->{ok}, @{$info->{avail_aggregate}} ];
                unshift @$choices, undef if $opt->{menu}{sssc_mode}[v];
                # Choose
                my $aggr = choose(
                    $choices,
                    { prompt => $prompt, %lyt_stmt_h }
                );
                if ( ! defined $aggr ) {
                    if ( @{$sql->{quote}{aggr_cols}} ) {
                        $sql->{quote}{aggr_cols} = [];
                        $sql->{print}{aggr_cols} = [];
                        delete $sql->{backup_in_hidd}{aggr_cols};
                        next AGGREGATE;
                    }
                    else {
                        $sql = clone( $backup_sql );
                        last AGGREGATE;
                    }
                }
                if ( $aggr eq $info->{ok} ) {
                    delete $sql->{backup_in_hidd}{aggr_cols};
                    if ( ! @{$sql->{quote}{aggr_cols}} && ! @{$sql->{quote}{group_by_cols}} ) {
                        $sql->{select_type} = '*';
                    }
                    last AGGREGATE;
                }
                my $i = @{$sql->{quote}{aggr_cols}};
                if ( $aggr eq 'COUNT(*)' ) {
                    $sql->{print}{aggr_cols}[$i] = $aggr;
                    $sql->{quote}{aggr_cols}[$i] = $aggr;
                }
                else {
                    $aggr =~ s/\(\S\)\z//;
                    $sql->{quote}{aggr_cols}[$i] = $aggr . "(";
                    $sql->{print}{aggr_cols}[$i] = $aggr . "(";
                    if ( $aggr eq 'COUNT' ) {
                        my $prompt = print_select_statement( $info, $sql, $table, 'Choose:' );
                        my $choices = [ $ALL, $DISTINCT ];
                        unshift @$choices, undef if $opt->{menu}{sssc_mode}[v];
                        # Choose
                        my $all_or_distinct = choose(
                            $choices,
                            { prompt => $prompt, %lyt_stmt_h }
                        );
                        if ( ! defined $all_or_distinct ) {
                            $sql->{quote}{aggr_cols} = [];
                            $sql->{print}{aggr_cols} = [];
                            next AGGREGATE;
                        }
                        if ( $all_or_distinct eq $DISTINCT ) {
                            $sql->{quote}{aggr_cols}[$i] .= $DISTINCT . ' ';
                            $sql->{print}{aggr_cols}[$i] .= $DISTINCT . ' ';
                        }
                    }
                    my $prompt = print_select_statement( $info, $sql, $table, 'Choose:' );
                    my $choices = [ @cols ];
                    unshift @$choices, undef if $opt->{menu}{sssc_mode}[v];
                    # Choose
                    my $print_col = choose(
                        $choices,
                        { prompt => $prompt, %lyt_stmt_h }
                    );
                    if ( ! defined $print_col ) {
                        $sql->{quote}{aggr_cols} = [];
                        $sql->{print}{aggr_cols} = [];
                        next AGGREGATE;
                    }
                    ( my $quote_col = $qt_columns->{$print_col} ) =~ s/\sAS\s\S+\z//;
                    $sql->{print}{aggr_cols}[$i] .= $print_col . ")";
                    $sql->{quote}{aggr_cols}[$i] .= $quote_col . ")";
                }
                $sql->{print}{aggr_cols}[$i] = unambiguous_key( $sql->{print}{aggr_cols}[$i], $pr_columns );
                $sql->{quote}{aggr_cols}[$i] .= " AS " . $dbh->quote_identifier( $sql->{print}{aggr_cols}[$i] );
                my $print_aggr = $sql->{print}{aggr_cols}[$i];
                my $quote_aggr = $sql->{quote}{aggr_cols}[$i];
                ( $qt_columns->{$print_aggr} = $quote_aggr ) =~ s/\sAS\s\S+//;
            }
        }
        elsif ( $custom eq $customize{'where'} ) {
            my @cols = ( @$pr_columns, @{$sql->{print}{hidd}} );
            my $AND_OR = ' ';
            $sql->{quote}{where_args} = [];
            $sql->{quote}{where_stmt} = " WHERE";
            $sql->{print}{where_stmt} = " WHERE";
            my $unclosed = 0;
            my $count = 0;

            WHERE: while ( 1 ) {
                my $prompt = print_select_statement( $info, $sql, $table, 'Choose:' );
                my $choices = [ $info->{ok}, @cols ];
                if ( $opt->{sql}{w_parentheses}[v] ) {
                    push @$choices, $unclosed ? ')' : '(';
                }
                unshift @$choices, undef if $opt->{menu}{sssc_mode}[v];
                # Choose
                my $print_col = choose(
                    $choices,
                    { prompt => $prompt, %lyt_stmt_h }
                );
                if ( ! defined $print_col ) {
                    if ( $sql->{quote}{where_stmt} ne " WHERE" ) {
                        $sql->{quote}{where_args} = [];
                        $sql->{quote}{where_stmt} = " WHERE";
                        $sql->{print}{where_stmt} = " WHERE";
                        $count = 0;
                        $AND_OR = ' ';
                        next WHERE;
                    }
                    else {
                        $sql = clone( $backup_sql );
                        last WHERE;
                    }
                }
                if ( $print_col eq $info->{ok} ) {
                    if ( $count == 0 ) {
                        $sql->{quote}{where_stmt} = '';
                        $sql->{print}{where_stmt} = '';
                    }
                    last WHERE;
                }
                if ( $print_col eq ')' ) {
                    $sql->{quote}{where_stmt} .= ")";
                    $sql->{print}{where_stmt} .= ")";
                    $unclosed--;
                    next WHERE;
                }
                if ( $count > 0 && $sql->{quote}{where_stmt} !~ /\(\z/ ) {
                    my $prompt = print_select_statement( $info, $sql, $table, 'Choose:' );
                    my $choices = [ $AND, $OR ];
                    unshift @$choices, undef if $opt->{menu}{sssc_mode}[v];
                    # Choose
                    $AND_OR = choose(
                        $choices,
                        { prompt => $prompt, %lyt_stmt_h }
                    );
                    if ( ! defined $AND_OR ) {
                        $sql->{quote}{where_args} = [];
                        $sql->{quote}{where_stmt} = " WHERE";
                        $sql->{print}{where_stmt} = " WHERE";
                        $count = 0;
                        $AND_OR = ' ';
                        next WHERE;
                    }
                    $AND_OR = ' ' . $AND_OR . ' ';
                }
                if ( $print_col eq '(' ) {
                    $sql->{quote}{where_stmt} .= $AND_OR . "(";
                    $sql->{print}{where_stmt} .= $AND_OR . "(";
                    $AND_OR = '';
                    $unclosed++;
                    next WHERE;
                }

                ( my $quote_col = $qt_columns->{$print_col} ) =~ s/\sAS\s\S+\z//;
                $sql->{quote}{where_stmt} .= $AND_OR . $quote_col;
                $sql->{print}{where_stmt} .= $AND_OR . $print_col;
                set_operator_sql( $info, $opt, $sql, 'where', $table, \@cols, $qt_columns, $quote_col );
                if ( ! $sql->{quote}{where_stmt} ) {
                    $sql->{quote}{where_args} = [];
                    $sql->{quote}{where_stmt} = " WHERE";
                    $sql->{print}{where_stmt} = " WHERE";
                    $count = 0;
                    $AND_OR = ' ';
                    next WHERE;
                }
                $count++;
            }
        }
        elsif( $custom eq $customize{'group_by'} ) {
            if ( $sql->{select_type} eq '*' || $sql->{select_type} eq 'chosen_cols' ) {
                reset_sql( $sql, $qt_columns );
            }
            my @cols = ( @$pr_columns );
            my $col_sep = ' ';
            $sql->{quote}{group_by_stmt} = " GROUP BY";
            $sql->{print}{group_by_stmt} = " GROUP BY";
            $sql->{quote}{group_by_cols} = [];
            $sql->{print}{group_by_cols} = [];
            $sql->{select_type} = 'group_by_cols';

            GROUP_BY: while ( 1 ) {
                my $prompt = print_select_statement( $info, $sql, $table, 'Choose:' );
                my $choices = [ $info->{ok}, @cols ];
                unshift @$choices, undef if $opt->{menu}{sssc_mode}[v];
                # Choose
                my $print_col = choose(
                    $choices,
                    { prompt => $prompt, %lyt_stmt_h }
                );
                if ( ! defined $print_col ) {
                    if ( @{$sql->{quote}{group_by_cols}} ) {
                        $sql->{quote}{group_by_stmt} = " GROUP BY";
                        $sql->{print}{group_by_stmt} = " GROUP BY";
                        $sql->{quote}{group_by_cols} = [];
                        $sql->{print}{group_by_cols} = [];
                        $col_sep = ' ';
                        delete $sql->{backup_in_hidd}{group_by_cols};
                        next GROUP_BY;
                    }
                    else {
                        $sql = clone( $backup_sql );
                        last GROUP_BY;
                    }
                }
                if ( $print_col eq $info->{ok} ) {
                    if ( $col_sep eq ' ' ) {
                        $sql->{quote}{group_by_stmt} = '';
                        $sql->{print}{group_by_stmt} = '';
                        if ( ! @{$sql->{quote}{aggr_cols}} ) {
                            $sql->{select_type} = '*';
                        }
                        delete $sql->{backup_in_hidd}{group_by_cols};
                    }
                    last GROUP_BY;
                }
                ( my $quote_col = $qt_columns->{$print_col} ) =~ s/\sAS\s\S+\z//;
                push @{$sql->{quote}{group_by_cols}}, $quote_col;
                push @{$sql->{print}{group_by_cols}}, $print_col;
                $sql->{quote}{group_by_stmt} .= $col_sep . $quote_col;
                $sql->{print}{group_by_stmt} .= $col_sep . $print_col;
                $col_sep = ', ';
            }
        }
        elsif( $custom eq $customize{'having'} ) {
            my @cols = ( @$pr_columns );
            my $AND_OR = ' ';
            $sql->{quote}{having_args} = [];
            $sql->{quote}{having_stmt} = " HAVING";
            $sql->{print}{having_stmt} = " HAVING";
            my $unclosed = 0;
            my $count = 0;

            HAVING: while ( 1 ) {
                my $prompt = print_select_statement( $info, $sql, $table, 'Choose:' );
                my $choices = [ $info->{ok}, @{$info->{avail_aggregate}}, map( '@' . $_, @{$sql->{print}{aggr_cols}} ) ];
                if ( $opt->{sql}{h_parentheses}[v] ) {
                    push @$choices, $unclosed ? ')' : '(';
                }
                unshift @$choices, undef if $opt->{menu}{sssc_mode}[v];
                # Choose
                my $aggr = choose(
                    $choices,
                    { prompt => $prompt, %lyt_stmt_h }
                );
                if ( ! defined $aggr ) {
                    if ( $sql->{quote}{having_stmt} ne " HAVING" ) {
                        $sql->{quote}{having_args} = [];
                        $sql->{quote}{having_stmt} = " HAVING";
                        $sql->{print}{having_stmt} = " HAVING";
                        $count = 0;
                        $AND_OR = ' ';
                        next HAVING;
                    }
                    else {
                        $sql = clone( $backup_sql );
                        last HAVING;
                    }
                }
                if ( $aggr eq $info->{ok} ) {
                    if ( $count == 0 ) {
                        $sql->{quote}{having_stmt} = '';
                        $sql->{print}{having_stmt} = '';
                    }
                    last HAVING;
                }
                if ( $aggr eq ')' ) {
                    $sql->{quote}{having_stmt} .= ")";
                    $sql->{print}{having_stmt} .= ")";
                    $unclosed--;
                    next HAVING;
                }
                if ( $count > 0 && $sql->{quote}{having_stmt} !~ /\(\z/ ) {
                    my $prompt = print_select_statement( $info, $sql, $table, 'Choose:' );
                    my $choices = [ $AND, $OR ];
                    unshift @$choices, undef if $opt->{menu}{sssc_mode}[v];
                    # Choose
                    $AND_OR = choose(
                        $choices,
                        { prompt => $prompt, %lyt_stmt_h }
                    );
                    if ( ! defined $AND_OR ) {
                        $sql->{quote}{having_args} = [];
                        $sql->{quote}{having_stmt} = " HAVING";
                        $sql->{print}{having_stmt} = " HAVING";
                        $count = 0;
                        $AND_OR = ' ';
                        next HAVING;
                    }
                    $AND_OR = ' ' . $AND_OR . ' ';
                }
                if ( $aggr eq '(' ) {
                    $sql->{quote}{having_stmt} .= $AND_OR . "(";
                    $sql->{print}{having_stmt} .= $AND_OR . "(";
                    $AND_OR = '';
                    $unclosed++;
                    next HAVING;
                }
                my ( $print_col,  $quote_col );
                my ( $print_aggr, $quote_aggr);
                if ( ( any { '@' . $_ eq $aggr } @{$sql->{print}{aggr_cols}} ) ) {
                    ( $print_aggr = $aggr ) =~ s/^\@//;
                    $quote_aggr = $qt_columns->{$print_aggr};
                    $sql->{quote}{having_stmt} .= $AND_OR . $quote_aggr;
                    $sql->{print}{having_stmt} .= $AND_OR . $print_aggr;
                    $quote_col = $qt_columns->{$print_aggr};
                }
                elsif ( $aggr eq 'COUNT(*)' ) {
                    $print_col = '*';
                    $quote_col = '*';
                    $print_aggr = $aggr;
                    $quote_aggr = $aggr;
                    $sql->{quote}{having_stmt} .= $AND_OR . $quote_aggr;
                    $sql->{print}{having_stmt} .= $AND_OR . $print_aggr;
                }
                else {
                    $aggr =~ s/\(\S\)\z//;
                    $sql->{quote}{having_stmt} .= $AND_OR . $aggr . "(";
                    $sql->{print}{having_stmt} .= $AND_OR . $aggr . "(";
                    $quote_aggr                 =           $aggr . "(";
                    $print_aggr                 =           $aggr . "(";
                    my $prompt = print_select_statement( $info, $sql, $table, 'Choose:' );
                    my $choices = [ @cols ];
                    unshift @$choices, undef if $opt->{menu}{sssc_mode}[v];
                    # Choose
                    $print_col = choose(
                        $choices,
                        { prompt => $prompt, %lyt_stmt_h }
                    );
                    if ( ! defined $print_col ) {
                        $sql->{quote}{having_args} = [];
                        $sql->{quote}{having_stmt} = " HAVING";
                        $sql->{print}{having_stmt} = " HAVING";
                        $count = 0;
                        $AND_OR = ' ';
                        next HAVING;
                    }
                    ( $quote_col = $qt_columns->{$print_col} ) =~ s/\sAS\s\S+\z//;
                    $sql->{quote}{having_stmt} .= $quote_col . ")";
                    $sql->{print}{having_stmt} .= $print_col . ")";
                    $quote_aggr                .= $quote_col . ")";
                    $print_aggr                .= $print_col . ")";
                }
                set_operator_sql( $info, $opt, $sql, 'having', $table, \@cols, $qt_columns, $quote_aggr );
                if ( ! $sql->{quote}{having_stmt} ) {
                    $sql->{quote}{having_args} = [];
                    $sql->{quote}{having_stmt} = " HAVING";
                    $sql->{print}{having_stmt} = " HAVING";
                    $count = 0;
                    $AND_OR = ' ';
                    next HAVING;
                }
                $count++;
            }
        }
        elsif( $custom eq $customize{'order_by'} ) {
            my @functions = @{$info->{hidd_func_pr}}{@{$info->{keys_hidd_func_pr}}};
            my $f = join '|', map quotemeta, @functions;
            my @not_hidd = map { /^(?:$f)\((.*)\)\z/ ? $1 : () } @{$sql->{print}{aggr_cols}};
            my @cols =
                ( $sql->{select_type} eq '*' || $sql->{select_type} eq 'chosen_cols' )
                ? ( @$pr_columns, @{$sql->{print}{hidd}} )
                : ( @{$sql->{print}{group_by_cols}}, @{$sql->{print}{aggr_cols}}, @not_hidd );
            my $col_sep = ' ';
            $sql->{quote}{order_by_stmt} = " ORDER BY";
            $sql->{print}{order_by_stmt} = " ORDER BY";

            ORDER_BY: while ( 1 ) {
                my $prompt = print_select_statement( $info, $sql, $table, 'Choose:' );
                my $choices = [ $info->{ok}, @cols ];
                unshift @$choices, undef if $opt->{menu}{sssc_mode}[v];
                # Choose
                my $print_col = choose(
                    $choices,
                    { prompt => $prompt, %lyt_stmt_h }
                );
                if ( ! defined $print_col ) {
                    if ( $sql->{quote}{order_by_stmt} ne " ORDER BY" ) {
                        $sql->{quote}{order_by_args} = [];
                        $sql->{quote}{order_by_stmt} = " ORDER BY";
                        $sql->{print}{order_by_stmt} = " ORDER BY";
                        $col_sep = ' ';
                        next ORDER_BY;
                    }
                    else {
                        $sql = clone( $backup_sql );
                        last ORDER_BY;
                    }
                }
                if ( $print_col eq $info->{ok} ) {
                    if ( $col_sep eq ' ' ) {
                        $sql->{quote}{order_by_stmt} = '';
                        $sql->{print}{order_by_stmt} = '';
                    }
                    last ORDER_BY;
                }
                ( my $quote_col = $qt_columns->{$print_col} ) =~ s/\sAS\s\S+\z//;
                $sql->{quote}{order_by_stmt} .= $col_sep . $quote_col;
                $sql->{print}{order_by_stmt} .= $col_sep . $print_col;
                $prompt = print_select_statement( $info, $sql, $table, 'Choose:' );
                $choices = [ $ASC, $DESC ];
                unshift @$choices, undef if $opt->{menu}{sssc_mode}[v];
                # Choose
                my $direction = choose(
                    $choices,
                    { prompt => $prompt, %lyt_stmt_h }
                );
                if ( ! defined $direction ){
                    $sql->{quote}{order_by_args} = [];
                    $sql->{quote}{order_by_stmt} = " ORDER BY";
                    $sql->{print}{order_by_stmt} = " ORDER BY";
                    $col_sep = ' ';
                    next ORDER_BY;
                }
                $sql->{quote}{order_by_stmt} .= ' ' . $direction;
                $sql->{print}{order_by_stmt} .= ' ' . $direction;
                $col_sep = ', ';
            }
        }
        elsif( $custom eq $customize{'limit'} ) {
            $sql->{quote}{limit_args} = [];
            $sql->{quote}{limit_stmt} = " LIMIT";
            $sql->{print}{limit_stmt} = " LIMIT";
            my $digits = 7;
            my ( $only_limit, $offset_and_limit ) = ( 'LIMIT', 'OFFSET-LIMIT' );

            LIMIT: while ( 1 ) {
                my $prompt = print_select_statement( $info, $sql, $table, 'Choose:' );
                my $choices = [ $info->{ok}, $only_limit, $offset_and_limit ];
                unshift @$choices, undef if $opt->{menu}{sssc_mode}[v];
                # Choose
                my $choice = choose(
                    $choices,
                    { prompt => $prompt, %lyt_stmt_h }
                );
                if ( ! defined $choice ) {
                    if ( @{$sql->{quote}{limit_args}} ) {
                        $sql->{quote}{limit_args} = [];
                        $sql->{quote}{limit_stmt} = " LIMIT";
                        $sql->{print}{limit_stmt} = " LIMIT";
                        next LIMIT;
                    }
                    else {
                        $sql = clone( $backup_sql );
                        last LIMIT;
                    }
                }
                if ( $choice eq $info->{ok} ) {
                    if ( ! @{$sql->{quote}{limit_args}} ) {
                        $sql->{quote}{limit_stmt} = '';
                        $sql->{quote}{limit_stmt} = '';
                    }
                    last LIMIT;
                }
                $sql->{quote}{limit_args} = [];
                $sql->{quote}{limit_stmt} = " LIMIT";
                $sql->{print}{limit_stmt} = " LIMIT";
                # Choose_a_number
                my $limit = choose_a_number( $info, $opt, $digits, '"LIMIT"' );
                next LIMIT if ! defined $limit || $limit eq '--';
                push @{$sql->{quote}{limit_args}}, $limit;
                $sql->{quote}{limit_stmt} .= ' ' . '?';
                $sql->{print}{limit_stmt} .= ' ' . insert_sep( $limit, $opt->{menu}{thsd_sep}[v] );
                if ( $choice eq $offset_and_limit ) {
                    # Choose_a_number
                    my $offset = choose_a_number( $info, $opt, $digits, '"OFFSET"' );
                    if ( ! defined $offset || $offset eq '--' ) {
                        $sql->{quote}{limit_args} = [];
                        $sql->{quote}{limit_stmt} = " LIMIT";
                        $sql->{print}{limit_stmt} = " LIMIT";
                        next LIMIT;
                    }
                    push @{$sql->{quote}{limit_args}}, $offset;
                    $sql->{quote}{limit_stmt} .= " OFFSET " . '?';
                    $sql->{print}{limit_stmt} .= " OFFSET " . insert_sep( $offset, $opt->{menu}{thsd_sep}[v] );
                }
            }
        }
        elsif ( $custom eq $customize{'hidden'} ) {
            my @functions = @{$info->{keys_hidd_func_pr}};
            my $stmt_key = '';
            if ( $sql->{select_type} eq '*' ) {
                @{$sql->{quote}{chosen_cols}} = map { $qt_columns->{$_} } @$pr_columns;
                @{$sql->{print}{chosen_cols}} = @$pr_columns;
                $stmt_key = 'chosen_cols';
            }
            elsif ( $sql->{select_type} eq 'chosen_cols' ) {
                $stmt_key = 'chosen_cols';
            }
            if ( $stmt_key eq 'chosen_cols' ) {
                if ( ! $sql->{backup_in_hidd}{print}{$stmt_key} ) {
                    @{$sql->{backup_in_hidd}{print}{$stmt_key}} = @{$sql->{print}{$stmt_key}};
                }
            }
            else {
                if ( @{$sql->{print}{aggr_cols}} && ! $sql->{backup_in_hidd}{print}{aggr_cols} ) {
                    @{$sql->{backup_in_hidd}{print}{'aggr_cols'}} = @{$sql->{print}{aggr_cols}};
                }
                if ( @{$sql->{print}{group_by_cols}} && ! $sql->{backup_in_hidd}{print}{group_by_cols} ) {
                    @{$sql->{backup_in_hidd}{print}{'group_by_cols'}} = @{$sql->{print}{group_by_cols}};
                }
            }
            my $changed = 0;

            HIDDEN: while ( 1 ) {
                my @cols = $stmt_key eq 'chosen_cols'
                    ? ( @{$sql->{print}{$stmt_key}}                                  )
                    : ( @{$sql->{print}{aggr_cols}}, @{$sql->{print}{group_by_cols}} );
                my $prompt = print_select_statement( $info, $sql, $table, 'Choose:' );
                my @pre = ( undef, $info->{_confirm} );
                my $choices = [ @pre, map( "- $_", @cols ) ];
                # Choose
                my $i = choose(
                    $choices,
                    { prompt => $prompt, %{$info->{lyt_stmt_v}}, index => 1 }
                );
                if ( ! defined $i ) {
                    $sql = clone( $backup_sql );
                    last HIDDEN;
                }
                my $print_col = $choices->[$i];
                if ( ! defined $print_col ) {
                    $sql = clone( $backup_sql );
                    last HIDDEN;
                }
                if ( $print_col eq $info->{_confirm} ) {
                    if ( ! $changed ) {
                        $sql = clone( $backup_sql );
                        last HIDDEN;
                    }
                    $sql->{select_type} = $stmt_key if $sql->{select_type} eq '*';
                    last HIDDEN;
                }
                $print_col =~ s/^\-\s//;
                $i -= @pre;
                if ( $stmt_key ne 'chosen_cols' ) {
                    if ( $i - @{$sql->{print}{aggr_cols}} >= 0 ) {
                        $i -= @{$sql->{print}{aggr_cols}};
                        $stmt_key = 'group_by_cols';
                    }
                    else {
                        $stmt_key = 'aggr_cols';
                    }
                }
                if ( $sql->{print}{$stmt_key}[$i] ne $sql->{backup_in_hidd}{print}{$stmt_key}[$i] ) {
                    if ( $stmt_key ne 'aggr_cols' ) {
                        my $i = first_index { $sql->{print}{$stmt_key}[$i] eq $_ } @{$sql->{print}{hidd}};
                        splice( @{$sql->{print}{hidd}}, $i, 1 );
                    }
                    $sql->{print}{$stmt_key}[$i] = $sql->{backup_in_hidd}{print}{$stmt_key}[$i];
                    $sql->{quote}{$stmt_key}[$i] = $qt_columns->{$sql->{backup_in_hidd}{print}{$stmt_key}[$i]};
                    $changed++;
                    next HIDDEN;
                }
                $prompt = print_select_statement( $info, $sql, $table, 'Choose:' );
                # Choose
                my $function = choose(
                    [ undef, map( "  $_", @functions ) ],
                    { prompt => $prompt, %{$info->{lyt_stmt_v}} }
                );
                if ( ! defined $function ) {
                    next HIDDEN;
                }
                $function =~ s/^\s\s//;
                ( my $quote_col = $qt_columns->{$print_col} ) =~ s/\sAS\s\S+\z//;
                my ( $quote_hidd, $print_hidd ) = col_functions( $info, $sql, $table, $function, $quote_col, $print_col );
                if ( ! defined $quote_hidd ) {
                    next HIDDEN;
                }
                $print_hidd = unambiguous_key( $print_hidd, $pr_columns );
                if ( $stmt_key eq 'group_by_cols' ) {
                    $sql->{quote}{$stmt_key}[$i] = $quote_hidd;
                    $sql->{quote}{group_by_stmt} = " GROUP BY " . join( ', ', @{$sql->{quote}{$stmt_key}} );
                    $sql->{print}{group_by_stmt} = " GROUP BY " . join( ', ', @{$sql->{print}{$stmt_key}} );
                }
                $sql->{quote}{$stmt_key}[$i] = $quote_hidd . ' AS ' . $dbh->quote_identifier( $print_hidd );
                $sql->{print}{$stmt_key}[$i] = $print_hidd;
                $qt_columns->{$print_hidd} = $quote_hidd;
                if ( $stmt_key ne 'aggr_cols' ) { # WHERE: aggregate functions are not allowed
                    push @{$sql->{print}{hidd}}, $print_hidd;
                }
                $changed++;
                next HIDDEN;
            }
        }
        elsif( $custom eq $customize{'print_table'} ) {
            my ( $default_cols_sql, $from_stmt ) = $select_from_stmt =~ /^SELECT\s(.*?)(\sFROM\s.*)\z/;
            my $cols_sql;
            if ( $sql->{select_type} eq '*' ) {
                $cols_sql = ' ' . $default_cols_sql;
            }
            elsif ( $sql->{select_type} eq 'chosen_cols' ) {
                $cols_sql = ' ' . join( ', ', @{$sql->{quote}{chosen_cols}} );
            }
            elsif ( @{$sql->{quote}{aggr_cols}} || @{$sql->{quote}{group_by_cols}} ) {
                $cols_sql = ' ' . join( ', ', @{$sql->{quote}{group_by_cols}}, @{$sql->{quote}{aggr_cols}} );
            }
            else { #
                $cols_sql = ' ' . $default_cols_sql;
            }
            my $select .= "SELECT" . $sql->{quote}{distinct_stmt} . $cols_sql . $from_stmt;
            $select .= $sql->{quote}{where_stmt};
            $select .= $sql->{quote}{group_by_stmt};
            $select .= $sql->{quote}{having_stmt};
            $select .= $sql->{quote}{order_by_stmt};
            $select .= $sql->{quote}{limit_stmt};
            my @arguments = ( @{$sql->{quote}{where_args}}, @{$sql->{quote}{having_args}}, @{$sql->{quote}{limit_args}} );
            $info->{auto_limit} = 0;
            if ( ! $sql->{quote}{limit_stmt} && defined $opt->{print}{limit}[v] ) {
                $select .= " LIMIT ?";
                push @arguments, $opt->{print}{limit}[v];
                $info->{auto_limit} = 1;
            }
            say 'Computing: ...' if defined $opt->{print}{progress_bar}[v];
            my $sth = $dbh->prepare( $select );
            $sth->execute( @arguments );
            my $col_names = $sth->{NAME};
            my $all_arrayref = $sth->fetchall_arrayref;
            unshift @$all_arrayref, $col_names;
            local $| = 1;
            print GO_TO_TOP_LEFT;
            print CLEAR_EOS;
            return $all_arrayref;
        }
        else {
            die "$custom: no such value in the hash \%customize";
        }
    }
    return;
}

sub unambiguous_key {
    my ( $new_key, $keys ) = @_;
    while ( any { $new_key eq $_ } @$keys ) {
        $new_key .= '_';
    }
    return $new_key;
}

sub set_operator_sql {
    my ( $info, $opt, $sql, $clause, $table, $cols, $qt_columns, $quote_col ) = @_;
    my ( $stmt, $args );
    my %lyt_stmt_h = %{$info->{lyt_stmt_h}};
    if ( $clause eq 'where' ) {
        $stmt = 'where_stmt';
        $args = 'where_args';
    }
    elsif ( $clause eq 'having' ) {
        $stmt = 'having_stmt';
        $args = 'having_args';
    }
    my $prompt = print_select_statement( $info, $sql, $table, 'Choose:' );
    my $choices = [ @{$opt->{menu}{operators}[v]} ];
    unshift @$choices, undef if $opt->{menu}{sssc_mode}[v];
    # Choose
    my $operator = choose(
        $choices,
        { prompt => $prompt, %lyt_stmt_h }
    );
    if ( ! defined $operator ) {
        $sql->{quote}{$args} = [];
        $sql->{quote}{$stmt} = '';
        $sql->{print}{$stmt} = '';
        return;
    }
    $operator =~ s/^\s+|\s+\z//g;
    if ( $operator !~ /\s%?col%?\z/ ) {
        if ( $operator !~ /REGEXP\z/ ) {
            $sql->{quote}{$stmt} .= ' ' . $operator;
            $sql->{print}{$stmt} .= ' ' . $operator;
        }
        if ( $operator =~ /NULL\z/ ) {
            # do nothing
        }
        elsif ( $operator =~ /^(?:NOT\s)?IN\z/ ) {
            my $col_sep = '';
            $sql->{quote}{$stmt} .= '(';
            $sql->{print}{$stmt} .= '(';

            IN: while ( 1 ) {
                my $status = print_select_statement( $info, $sql, $table );
                # Readline
                my $value = local_readline( $info, { status => $status, prompt => 'Value: ' } );
                if ( ! defined $value ) {
                    $sql->{quote}{$args} = [];
                    $sql->{quote}{$stmt} = '';
                    $sql->{print}{$stmt} = '';
                    return;
                }
                if ( $value eq '' ) {
                    if ( $col_sep eq ' ' ) {
                        $sql->{quote}{$args} = [];
                        $sql->{quote}{$stmt} = '';
                        $sql->{print}{$stmt} = '';
                        return;
                    }
                    $sql->{quote}{$stmt} .= ')';
                    $sql->{print}{$stmt} .= ')';
                    last IN;
                }
                $sql->{quote}{$stmt} .= $col_sep . '?';
                $sql->{print}{$stmt} .= $col_sep . $value;
                push @{$sql->{quote}{$args}}, $value;
                $col_sep = ',';
            }
        }
        elsif ( $operator =~ /^(?:NOT\s)?BETWEEN\z/ ) {
            my $status1 = print_select_statement( $info, $sql, $table );
            # Readline
            my $value_1 = local_readline( $info, { status => $status1, prompt => 'Value: ' } );
            if ( ! defined $value_1 ) {
                $sql->{quote}{$args} = [];
                $sql->{quote}{$stmt} = '';
                $sql->{print}{$stmt} = '';
                return;
            }
            $sql->{quote}{$stmt} .= ' ' . '?' .      ' AND';
            $sql->{print}{$stmt} .= ' ' . $value_1 . ' AND';
            push @{$sql->{quote}{$args}}, $value_1;
            my $status2 = print_select_statement( $info, $sql, $table );
            # Readline
            my $value_2 = local_readline( $info, { status => $status2, prompt => 'Value: ' } );
            if ( ! defined $value_2 ) {
                $sql->{quote}{$args} = [];
                $sql->{quote}{$stmt} = '';
                $sql->{print}{$stmt} = '';
                return;
            }
            $sql->{quote}{$stmt} .= ' ' . '?';
            $sql->{print}{$stmt} .= ' ' . $value_2;
            push @{$sql->{quote}{$args}}, $value_2;
        }
        elsif ( $operator =~ /REGEXP\z/ ) {
            $sql->{print}{$stmt} .= ' ' . $operator;
            my $status = print_select_statement( $info, $sql, $table );
            # Readline
            my $value = local_readline( $info, { status => $status, prompt => 'Pattern: ' } );
            if ( ! defined $value ) {
                $sql->{quote}{$args} = [];
                $sql->{quote}{$stmt} = '';
                $sql->{print}{$stmt} = '';
                return;
            }
            $value = '^$' if ! length $value;
            if ( $info->{db_driver} eq 'SQLite' ) {
                $value = qr/$value/i if ! $opt->{sql}{regexp_case}[v];
                $value = qr/$value/  if   $opt->{sql}{regexp_case}[v];
            }
            $sql->{quote}{$stmt} =~ s/.*\K\s\Q$quote_col\E//;
            $sql->{quote}{$stmt} .= sql_regexp( $info, $opt, $quote_col, $operator =~ /^NOT/ ? 1 : 0 );
            $sql->{print}{$stmt} .= ' ' . "'$value'";
            push @{$sql->{quote}{$args}}, $value;
        }
        else {
            my $status = print_select_statement( $info, $sql, $table );
            my $prompt = $operator =~ /LIKE\z/ ? 'Pattern: ' : 'Value: ';
            # Readline
            my $value = local_readline( $info, { status => $status, prompt => $prompt } );
            if ( ! defined $value ) {
                $sql->{quote}{$args} = [];
                $sql->{quote}{$stmt} = '';
                $sql->{print}{$stmt} = '';
                return;
            }
            $sql->{quote}{$stmt} .= ' ' . '?';
            $sql->{print}{$stmt} .= ' ' . "'$value'";
            push @{$sql->{quote}{$args}}, $value;
        }
    }
    elsif ( $operator =~ /\s%?col%?\z/ ) {
        my $arg;
        if ( $operator =~ /^(.+)\s(%?col%?)\z/ ) {
            $operator = $1;
            $arg = $2;
        }
        $operator =~ s/^\s+|\s+\z//g;
        $sql->{quote}{$stmt} .= ' ' . $operator;
        $sql->{print}{$stmt} .= ' ' . $operator;
        my $status = print_select_statement( $info, $sql, $table, "$operator:" );
        my $choices = [ @$cols ];
        unshift @$choices, undef if $opt->{menu}{sssc_mode}[v];
        # Choose
        my $print_col = choose(
            $choices,
            { prompt => $prompt, %lyt_stmt_h }
        );
        if ( ! defined $print_col ) {
            $sql->{quote}{$stmt} = '';
            $sql->{print}{$stmt} = '';
            return;
        }
        ( my $quote_col = $qt_columns->{$print_col} ) =~ s/\sAS\s\S+\z//;
        my ( @qt_args, @pr_args );
        if ( $arg =~ /^(%)col/ ) {
            push @qt_args, "'$1'";
            push @pr_args, "'$1'";
        }
        push @qt_args, $quote_col;
        push @pr_args, $print_col;
        if ( $arg =~ /col(%)\z/ ) {
            push @qt_args, "'$1'";
            push @pr_args, "'$1'";
        }
        if ( $operator =~ /LIKE\z/ ) {
            $sql->{quote}{$stmt} .= ' ' . concatenate( $info->{db_driver}, \@qt_args );
            $sql->{print}{$stmt} .= ' ' . join( '+', @pr_args );
        }
        else {
            $sql->{quote}{$stmt} .= ' ' . $quote_col;
            $sql->{print}{$stmt} .= ' ' . $print_col;
        }
    }
    return;
}


sub calc_col_width {
    my ( $info, $opt, $db, $a_ref, $term_width ) = @_;
    my ( $width_head, $width_cols, $not_a_number );
    if ( defined $info->{width_head} && defined $info->{width_cols} && defined $info->{not_a_number} ) {
        return @{$info}{qw(width_head width_cols not_a_number)};
    }
    my $binary_filter =    $opt->{$info->{db_driver} . '_' . $db}{binary_filter}[v]
                        // $opt->{$info->{db_driver}}{binary_filter}[v];
    my $gcs_bnry = Unicode::GCString->new( $info->{binary_string} );
    my $binary_length = $gcs_bnry->columns;
    my $binray_regexp = qr/[\x00-\x08\x0B-\x0C\x0E-\x1F]/;
    my $count = 0;
    for my $row ( @$a_ref ) {
        ++$count;
        for my $i ( 0 .. $#$row ) {
            $width_cols->[$i] ||= 1;
            $row->[$i] = $opt->{print}{undef}[v] if ! defined $row->[$i];
            if ( ref $row->[$i] ) {
                $row->[$i] = handle_reference( $row->[$i] );
            }
            my $width;
            if ( $binary_filter && substr( $row->[$i], 0, 100 ) =~ $binray_regexp ) {
                $row->[$i] = $info->{binary_string};
                $width = $binary_length;
            }
            else {
                $row->[$i] =~ s/^\p{Space}+//;
                $row->[$i] =~ s/\p{Space}+\z//;
                #$row->[$i] =~ s/(?<=\P{Space})\p{Space}+/ /g;
                $row->[$i] =~ s/\p{Space}+/ /g;
                $row->[$i] =~ s/\p{C}//g;
                my $gcs = Unicode::GCString->new( $row->[$i] );
                $width = $gcs->columns;
            }
            if ( $count == 1 ) {
                # column name
                $width_head->[$i] = $width;
            }
            else {
                # normal row
                $width_cols->[$i] = $width if $width > $width_cols->[$i];
                ++$not_a_number->[$i] if $row->[$i] && ! looks_like_number $row->[$i];
            }
        }
    }
    return @{$info}{qw(width_head width_cols not_a_number)} = ( $width_head, $width_cols, $not_a_number );
}


sub minus_x_percent {
    my ( $value, $percent ) = @_;
    my $new = int( $value - ( $value / 100 * $percent ) );
    return $new > 0 ? $new : 1;
}

sub calc_avail_width {
    my ( $info, $opt, $a_ref, $term_width, $width_head, $width_cols ) = @_;
    my $avail_width = $term_width - $opt->{print}{tab_width}[v] * $#$width_cols;
    my $sum = sum( @$width_cols );
    if ( $sum < $avail_width ) {
        # auto cut
        HEAD: while ( 1 ) {
            my $count = 0;
            for my $i ( 0 .. $#$width_head ) {
                if ( $width_head->[$i] > $width_cols->[$i] ) {
                    ++$width_cols->[$i];
                    ++$count;
                    last HEAD if ( $sum + $count ) == $avail_width;
                }
            }
            last HEAD if $count == 0;
            $sum += $count;
        }
        return $width_head, $width_cols;
    }
    elsif ( $sum > $avail_width ) {
        my $minimum_with = $opt->{print}{min_col_width}[v] || 1;
        if ( @$width_head > $avail_width ) {
            say 'Terminal window is not wide enough to print this table.';
            choose( [ 'Press ENTER to show the column names.' ], { prompt => '', %{$info->{lyt_stop}} } );
            my $prompt = 'To reduce the number of columns choose "- COLUMNS".' . "\n";
            $prompt .= 'Close with ENTER.';
            choose( $a_ref->[0], { prompt => $prompt, %{$info->{lyt_stop}} } );
            return;
        }
        my @width_cols_tmp = @$width_cols;
        my $percent = 0;

        MIN: while ( $sum > $avail_width ) {
            ++$percent;
            my $count = 0;
            for my $i ( 0 .. $#width_cols_tmp ) {
                next if $minimum_with >= $width_cols_tmp[$i];
                if ( $minimum_with >= minus_x_percent( $width_cols_tmp[$i], $percent ) ) {
                    $width_cols_tmp[$i] = $minimum_with;
                }
                else {
                    $width_cols_tmp[$i] = minus_x_percent( $width_cols_tmp[$i], $percent );
                }
                ++$count;
            }
            $sum = sum( @width_cols_tmp );
            $minimum_with-- if $count == 0;
            #last MIN if $minimum_with == 0;
        }
        my $rest = $avail_width - $sum;
        if ( $rest ) {

            REST: while ( 1 ) {
                my $count = 0;
                for my $i ( 0 .. $#width_cols_tmp ) {
                    if ( $width_cols_tmp[$i] < $width_cols->[$i] ) {
                        $width_cols_tmp[$i]++;
                        $rest--;
                        $count++;
                        last REST if $rest == 0;
                    }
                }
                last REST if $count == 0;
            }
        }
        $width_cols = [ @width_cols_tmp ] if @width_cols_tmp;
    }
    return $width_head, $width_cols;
}


sub trunk_col_to_avail_width {
    my ( $info, $opt, $a_ref, $width_cols, $not_a_number, $show_progress ) = @_;
    my $total = $#{$a_ref};                   #
    my $next_update = 0;                      #
    my $c = 0;                                #
    my $progress;                             #
    if ( $show_progress ) {                   #
        local $| = 1;                         #
        print GO_TO_TOP_LEFT;                 #
        print CLEAR_EOS;                      #
        $progress = Term::ProgressBar->new( { #
            name => 'Computing',              #
            count => $total,                  #
            remove => 1 } );                  #
        $progress->minor( 0 );                #
        $show_progress = 1;                   #
    }                                         #
    my $list;
    my $tab = ' ' x $opt->{print}{tab_width}[v];
    for my $row ( @$a_ref ) {
        my $str = '';
        for my $i ( 0 .. $#$width_cols ) {
            $str .= unicode_sprintf( $width_cols->[$i], $row->[$i], $not_a_number->[$i] ? 0 : 1 );
            $str .= $tab if $i != $#$width_cols;
        }
        push @$list, $str;
        if ( $show_progress ) {                                              #
            my $is_power = 0;                                                #
            for ( my $i = 0; 2 ** $i <= $c; ++$i ) {                         #
                $is_power = 1 if 2 ** $i == $c;                              #
            }                                                                #
            $next_update = $progress->update( $c ) if $c >= $next_update;    #
            ++$c;                                                            #
        }                                                                    #
    }
    $progress->update( $total ) if $show_progress && $total >= $next_update; #
    my $len = sum( @$width_cols, $opt->{print}{tab_width}[v] * $#{$width_cols} );
    return $list, $len;
}


sub func_print_tbl {
    my ( $info, $opt, $db, $a_ref ) = @_;
    my ( $term_width ) = term_size();
    my $show_progress = 0;
    if ( defined $opt->{print}{progress_bar}[v] ) {
        say 'Computing: ...';
        if ( @$a_ref * @{$a_ref->[0]} > $opt->{print}{progress_bar}[v] ) {
            $show_progress = 1;
        }
    }
    my ( $width_head, $width_cols, $not_a_number ) = calc_col_width( $info, $opt, $db, $a_ref, $term_width );
    ( $width_head, $width_cols ) = calc_avail_width( $info, $opt, $a_ref, $term_width, $width_head, $width_cols );
    return if ! $width_head || ! $width_cols;
    my ( $list, $len ) = trunk_col_to_avail_width( $info, $opt, $a_ref, $width_cols, $not_a_number, $show_progress );

    my $max_rows = $opt->{print}{limit}[v] // @$list;
    if ( $info->{auto_limit} && @$list > $max_rows ) {
        my $reached_limit = 'REACHED LIMIT MAX_ROWS: ' . insert_sep( $max_rows, $opt->{menu}{thsd_sep}[v] ) . '';
        my $gcs = Unicode::GCString->new( $reached_limit );
        if ( $gcs->columns > $term_width ) {
            $reached_limit = unicode_sprintf( $term_width, 'REACHED LIMIT', 0 );
        }
        push @$list, $reached_limit;
    }

    if ( $opt->{menu}{table_expand}[v] ) {
        my $length_key = 0;
        for my $width ( @$width_head ) {
            $length_key = $width if $width > $length_key;
        }
        $length_key += 1;
        my $separator = ' : ';
        my $gcs = Unicode::GCString->new( $separator );
        my $length_sep = $gcs->columns;
        my $old_row = 0;
        my ( $width ) = term_size();

        while ( 1 ) {
            if ( ( term_size() )[0] != $width ) {
                ( $width ) = term_size();
                func_print_tbl( $info, $opt, $db, $a_ref );
                return;
            }
            my $row = choose(
                $list,
                { prompt => '', %{$info->{lyt_3}}, index => 1, default => $old_row, ll => $len }
            );
            return if ! defined $row;
            return if $row == 0;
            if ( $old_row != 0 && $old_row == $row ) {
                $old_row = 0;
                next;
            }
            $old_row = $row;
            my ( $term_width ) = term_size();
            $length_key = int( $term_width / 100 * 33 ) if $length_key > int( $term_width / 100 * 33 );
            my $col_max = $term_width - ( $length_key + $length_sep + 1 );
            my $line_fold = Text::LineFold->new( %{$info->{line_fold}} );
            $line_fold->config( 'ColMax', $col_max );
            my $row_data = [ ' Close with ENTER' ];
            for my $col ( 0 .. $#{$a_ref->[0]} ) {
                push @{$row_data}, ' ';
                my $key = $a_ref->[0][$col];
                my $sep = $separator;
                if ( ! defined $a_ref->[$row][$col] || $a_ref->[$row][$col] eq '' ) {
                    push @{$row_data}, sprintf "%*.*s%*s%s", $length_key, $length_key, $key, $length_sep, $sep, '';
                }
                else {
                    my $text = $line_fold->fold( $a_ref->[$row][$col], 'PLAIN' );
                    for my $line ( split /\R+/, $text ) {
                        push @{$row_data}, sprintf "%*.*s%*s%s", $length_key, $length_key, $key, $length_sep, $sep, $line;
                        $key = '' if $key;
                        $sep = '' if $sep;
                    }
                }
            }
            choose(
                $row_data,
                { prompt => '', %{$info->{lyt_3}} }
            );
        }
    }
    else {
        choose(
            $list,
            { prompt => '', %{$info->{lyt_3}}, ll => $len }
        );
        return;
    }
}


sub options {
    my ( $info, $opt ) = @_;
    my @choices = ();
    for my $section ( @{$info->{option_sections}} ) {
        for my $key ( @{$info->{$section}{keys}} ) {
            push @choices, $opt->{$section}{$key}[chs];
        }
    }
    my $path = '  Path';

    OPTION: while ( 1 ) {
        # Choose
        my $option = choose(
            [ undef, $info->{_continue}, $info->{_help}, $path, @choices ],
            { %{$info->{lyt_3}}, undef => $info->{_exit} }
        );
        if ( ! defined $option ) {
            if ( $info->{write_config} ) {
                write_config_file( $opt, $info->{config_file} );
                delete $info->{write_config};
            }
            exit();
        }
        elsif ( $option eq $info->{_continue} ) {
            if ( $info->{write_config} ) {
                write_config_file( $opt, $info->{config_file} );
                delete $info->{write_config};
            }
            return $opt;
        }
        elsif ( $option eq $info->{_help} ) {
            pod2usage( { -exitval => 'NOEXIT', -verbose => 2 } );
        }
        elsif ( $option eq $path ) {
            my $bin     = 'bin';
            my $app_dir = 'app dir';
            my $path = {
                $bin     => rel2abs( $0 ),
                $app_dir => $info->{app_dir} . '/',
            };
            my $keys = [ $bin, $app_dir ];
            my $len_key = 7;
            print_hash( $info, $path, $keys, $len_key, ( term_size() )[0] );
        }
        elsif ( $option eq $opt->{"print"}{'tab_width'}[chs] ) {
            my $max = 99;
            my ( $section, $key, $prompt ) = ( 'print', 'tab_width', 'Tab width' );
            opt_number( $info, $opt, $section, $key, $prompt, $max );
        }
        elsif ( $option eq $opt->{"print"}{'min_col_width'}[chs] ) {
            my $max = 99;
            my ( $section, $key, $prompt ) = ( 'print', 'min_col_width', 'Minimum Column width' );
            opt_number( $info, $opt, $section, $key, $prompt, $max );
        }
        elsif ( $option eq $opt->{"print"}{'undef'}[chs] ) {
            my ( $section, $key, $prompt ) = ( 'print', 'undef', 'Print replacement for undefined table vales' );
            opt_readline( $info, $opt, $section, $key, $prompt );
        }
        elsif ( $option eq $opt->{"print"}{'limit'}[chs] ) {
            my $digits = 7;
            my ( $section, $key, $prompt ) = ( 'print', 'limit', '"Max rows"' );
            opt_number_range( $info, $opt, $section, $key, $prompt, $digits );
        }
        elsif ( $option eq $opt->{"print"}{'progress_bar'}[chs] ) {
            my $digits = 7;
            my ( $section, $key, $prompt ) = ( 'print', 'progress_bar', '"Threshold ProgressBar"' );
            opt_number_range( $info, $opt, $section, $key, $prompt, $digits );
        }
        elsif ( $option eq $opt->{"sql"}{'lock_stmt'}[chs] ) {
            my $list = [ 'Lk0', 'Lk1' ];
            my ( $section, $key, $prompt ) = ( 'sql', 'lock_stmt', 'Keep statement' );
            opt_choose_list_index( $info, $opt, $section, $key, $prompt, $list );
        }
        elsif ( $option eq $opt->{"sql"}{'system_info'}[chs] ) {
            my ( $section, $key, $prompt ) = ( 'sql', 'system_info', 'Enable Metadata' );
            opt_yes_no( $info, $opt, $section, $key, $prompt );
        }
        elsif ( $option eq $opt->{"sql"}{'regexp_case'}[chs] ) {
            my ( $section, $key, $prompt ) = ( 'sql', 'regexp_case', 'REGEXP case sensitiv' );
            opt_yes_no( $info, $opt, $section, $key, $prompt );
        }
        elsif ( $option eq $opt->{"sql"}{'parentheses'}[chs] ) {
            my $section = 'sql';
            my $keys = $info->{sub_parentheses}{sql};
            my $two_val = [ 'NO', 'YES' ];
            multi_options( $info, $opt, $section, $keys, $two_val );
        }
        elsif ( $option eq $opt->{"dbs"}{'login'}[chs] ) {
            my $list = [ 'once per DB', 'only once' ];
            my ( $section, $key, $prompt ) = ( 'dbs', 'login', 'Ask for credentials' );
            opt_choose_list_index( $info, $opt, $section, $key, $prompt, $list );
        }
        elsif ( $option eq $opt->{"dbs"}{'db_defaults'}[chs] ) {
            database_setting( $info, $opt );
        }
        elsif ( $option eq $opt->{"menu"}{'thsd_sep'}[chs] ) {
            my $list = [ "','", "'.'", "'_'", "' '", "''" ];
            my ( $section, $key, $prompt ) = ( 'menu', 'thsd_sep', 'Thousands separator' );
            opt_choose_list_value( $info, $opt, $section, $key, $prompt, $list );
        }
        elsif ( $option eq $opt->{"menu"}{'sssc_mode'}[chs] ) {
            my $list = [ 'simple', 'compat' ];
            my ( $section, $key, $prompt ) = ( 'menu', 'sssc_mode', 'Sssc mode' );
            opt_choose_list_index( $info, $opt, $section, $key, $prompt, $list );
        }
        elsif ( $option eq $opt->{"menu"}{'operators'}[chs] ) {
            my $available = $info->{avail_operators};
            my ( $section, $key ) = ( 'menu', 'operators' );
            opt_choose_a_list( $info, $opt, $section, $key, $available );
        }
        elsif ( $option eq $opt->{"menu"}{'db_drivers'}[chs] ) {
            my $available = $info->{avail_db_drivers};
            my ( $section, $key ) = ( 'menu', 'db_drivers' );
            opt_choose_a_list( $info, $opt, $section, $key, $available );
        }
        elsif ( $option eq $opt->{"menu"}{'mouse'}[chs] ) {
            my $max = 4;
            my ( $section, $key, $prompt ) = ( 'menu', 'mouse', 'Mouse mode' );
            opt_number( $info, $opt, $section, $key, $prompt, $max );
        }
        elsif ( $option eq $opt->{"menu"}{'expand'}[chs] ) {
            my $section = 'menu';
            my $keys = $info->{sub_expand}{menu};
            my $two_val = [ 'Simple', 'Enchanted' ];
            multi_options( $info, $opt, $section, $keys, $two_val );
        }
        else { die $option }
    }
}


sub multi_options {
    my ( $info, $opt, $section, $keys, $values ) = @_;
    my $count;
    my $tmp = {};
    my $longest = 0;
    for my $key ( @$keys ) {
        my $gcs = Unicode::GCString->new( $opt->{$section}{$key}[chs] );
        my $length = $gcs->columns();
        $longest = $length if $length > $longest;
        $tmp->{$section}{$key}[v] = $opt->{$section}{$key}[v];
    }
    while ( 1 ) {
        my $choices = [ undef ];
        for my $key ( @$keys ) {
            my $current = $values->[$tmp->{$section}{$key}[v]];
            push @$choices, sprintf "%-*s [%s]", $longest, $opt->{$section}{$key}[chs], $current;
        }
        push @$choices, $info->{_confirm};
        # Choose
        my $idx = choose(
            $choices,
            { prompt => 'Choose:', %{$info->{lyt_3}}, index => 1 }
        );
        return if ! defined $idx;
        my $choice = $choices->[$idx];
        return if ! defined $choice;
        if ( $choice eq $info->{_confirm} ) {
            if ( $count ) {
                for my $key ( @$keys ) {
                    $opt->{$section}{$key}[v] = $tmp->{$section}{$key}[v];
                }
                $info->{write_config}++;
            }
            return;
        }
        my $key = $keys->[$idx-1];
        $tmp->{$section}{$key}[v]++;
        $tmp->{$section}{$key}[v] = 0 if $tmp->{$section}{$key}[v] == @$values;
        $count++;
    }
}

sub opt_yes_no {
    my ( $info, $opt, $section, $key, $prompt ) = @_;
    my ( $yes, $no ) = ( 'YES', 'NO' );
    my $current = $opt->{$section}{$key}[v];
    # Choose
    my $choice = choose(
        [ undef, $yes, $no ],
        { prompt => $prompt . ' [' . ( $current ? 'YES' : 'NO' ) . ']:', %{$info->{lyt_1}} }
    );
    return if ! defined $choice;
    $opt->{$section}{$key}[v] = $choice eq $yes ? 1 : 0;
    $info->{write_config}++;
    return;
}

sub opt_choose_list_value {
    my ( $info, $opt, $section, $key, $prompt, $list ) = @_;
    my $current = $opt->{$section}{$key}[v]; # // '--'
    # Choose
    my $choice = choose(
        [ undef, @$list ],
        { prompt => $prompt . ' [' . $current . ']:', %{$info->{lyt_1}} }
    );
    return if ! defined $choice;
    $choice =~ s/^'|'\z//g;
    $opt->{$section}{$key}[v] = $choice;
    $info->{write_config}++;
    return;
}

sub opt_choose_list_index {
    my ( $info, $opt, $section, $key, $prompt, $list ) = @_;
    my $current = $list->[$opt->{$section}{$key}[v]];
    # Choose
    my $idx = choose(
        [ undef, @$list ],
        { prompt => $prompt . ' [' . $current . ']:', index => 1, %{$info->{lyt_1}} }
    );
    return if ! defined $idx;
    return if $idx == 0;
    $idx--;
    $opt->{$section}{$key}[v] = $idx;
    $info->{write_config}++;
    return;
}

sub opt_choose_a_list {
    my ( $info, $opt, $section, $key, $available ) = @_;
    my $current = $opt->{$section}{$key}[v];
    # Choose_list
    my $list = choose_list( $info, $current, $available );
    return if ! defined $list;
    $opt->{$section}{$key}[v] = $list;
    $info->{write_config}++;
    return;
}

sub opt_number {
    my ( $info, $opt, $section, $key, $prompt, $max ) = @_;
    my $current = $opt->{$section}{$key}[v];
    # Choose
    my $choice = choose(
        [ undef, 0 .. $max ],
        { prompt => $prompt . ' [' . $current . ']:', %{$info->{lyt_1}}, justify => 1 }
    );
    return if ! defined $choice;
    $opt->{$section}{$key}[v] = $choice;
    $info->{write_config}++;
    return;
}

sub opt_number_range {
    my ( $info, $opt, $section, $key, $prompt, $digits ) = @_;
    my $current = $opt->{$section}{$key}[v];
    $current = insert_sep( $current, $opt->{menu}{thsd_sep}[v] );
    # Choose_a_number
    my $choice = choose_a_number( $info, $opt, $digits, $prompt, $current );
    return if ! defined $choice;
    $opt->{$section}{$key}[v] = $choice eq '--' ? undef : $choice;
    $info->{write_config}++;
    return;
}

sub opt_readline {
    my ( $info, $opt, $section, $key, $prompt ) = @_;
    my $current = $opt->{$section}{$key}[v];
    $prompt .= ' ["' . $current . '"]: ';
    # Readline
    my $choice = local_readline( $info, { prompt => $prompt } );
    return if ! defined $choice;
    $opt->{$section}{$key}[v] = $choice;
    $info->{write_config}++;
    return;
}

sub database_setting {
    my ( $info, $opt, $db ) = @_;
    my $db_driver;
    if ( ! defined $db ) {
        # Choose
        $db_driver = choose(
            [ undef, @{$opt->{menu}{db_drivers}[v]} ],
            { %{$info->{lyt_1}} }
        );
        return if ! defined $db_driver;
    }
    else {
        $db_driver = $info->{db_driver};
    }
    my @choices = map { $opt->{$db_driver}{$_}[chs] } @{$info->{$db_driver}{keys}};
    if ( defined $info->{cmdline_only}{$db_driver} ) {
        for my $key ( @{$info->{cmdline_only}{$db_driver}} ) {
            my $idx = first_index { $_ eq $opt->{$db_driver}{$key}[chs] } @choices;
            splice( @choices, $idx, 1 );
        }
    }
    my $section;
    if ( ! defined $db ) {
        $section = $db_driver;
    }
    else {
        $section = $db_driver . '_' . $db;
        for my $key ( keys %{$opt->{$db_driver}} ) {
            $opt->{$section}{$key}[v] //= $opt->{$db_driver}{$key}[v];
        }
    }
    my $orig = clone( $opt );

    DB_OPTION: while ( 1 ) {
        # Choose
        my $option = choose(
            [ undef, @choices, $info->{_confirm} ],
            { %{$info->{lyt_3}} }
        );
        if ( ! defined $option ) {
            if ( $info->{write_config} ) {
                $opt = clone( $orig );
            }
            return;
        }
        if ( $option eq $info->{_confirm} ) {
            if ( $info->{write_config} ) {
                write_config_file( $opt, $info->{config_file} );
                delete $info->{write_config};
                return 1;
            }
            return;
        }
        if ( $db_driver eq 'SQLite' ) {
            if ( $option eq $opt->{"SQLite"}{sqlite_unicode}[chs] ) {
                my ( $key, $prompt ) = ( 'sqlite_unicode', 'Unicode' );
                opt_yes_no( $info, $opt, $section, $key, $prompt );
            }
            elsif ( $option eq $opt->{"SQLite"}{sqlite_see_if_its_a_number}[chs] ) {
                my ( $key, $prompt ) = ( 'sqlite_see_if_its_a_number', 'See if its a number' );
                opt_yes_no( $info, $opt, $section, $key, $prompt );
            }
            elsif ( $option eq $opt->{"SQLite"}{binary_filter}[chs] ) {
                my ( $key, $prompt ) = ( 'binary_filter', 'Enable Binary Filter' );
                opt_yes_no( $info, $opt, $section, $key, $prompt );
            }
            else { die $option }
        }
        elsif ( $db_driver eq 'mysql' ) {
            if ( $option eq $opt->{"mysql"}{mysql_enable_utf8}[chs] ) {
                my ( $key, $prompt ) = ( 'mysql_enable_utf8', 'Enable utf8' );
                opt_yes_no( $info, $opt, $section, $key, $prompt );
            }
            elsif ( $option eq $opt->{"mysql"}{default_user}[chs] ) {
                my ( $key, $prompt ) = ( 'default_user', 'Default user' );
                opt_readline( $info, $opt, $section, $key, $prompt );
            }
            elsif ( $option eq $opt->{"mysql"}{host}[chs] ) {
                my ( $key, $prompt ) = ( 'host', 'Host' );
                opt_readline( $info, $opt, $section, $key, $prompt );
            }
            elsif ( $option eq $opt->{"mysql"}{port}[chs] ) {
                my ( $key, $prompt ) = ( 'port', 'Port' );
                opt_readline( $info, $opt, $section, $key, $prompt );
            }
            elsif ( $option eq $opt->{"mysql"}{binary_filter}[chs] ) {
                my ( $key, $prompt ) = ( 'binary_filter', 'Enable Binary Filter' );
                opt_yes_no( $info, $opt, $section, $key, $prompt );
            }
            else { die $option }
        }
        elsif ( $db_driver eq 'Pg' ) {
            if ( $option eq $opt->{"Pg"}{pg_enable_utf8}[chs] ) {
                #my ( $key, $prompt ) = ( 'pg_enable_utf8', 'Enable utf8' );
                #opt_yes_no( $info, $opt, $section, $key, $prompt );
                my ( $key, $prompt ) = ( 'pg_enable_utf8', 'Enable utf8' );
                my $list = [ 'YES', 'NO' ];
                my $main;
                eval {
                    require DBD::Pg;
                    $main = ( split /\./, $DBD::Pg::VERSION )[0];
                };
                push @$list, 'AUTO' if $main && $main >= 3;
                my @decode_in = ( 1, 0, 2 );
                $opt->{$section}{$key}[v] = $decode_in[$opt->{$section}{$key}[v]];
                opt_choose_list_index( $info, $opt, $section, $key, $prompt, $list );
                my @encode_out = ( 1, 0, -1 );
                $opt->{$section}{$key}[v] = $encode_out[$opt->{$section}{$key}[v]];
            }
            elsif ( $option eq $opt->{"Pg"}{default_user}[chs] ) {
                my ( $key, $prompt ) = ( 'default_user', 'Default user' );
                opt_readline( $info, $opt, $section, $key, $prompt );
            }
            elsif ( $option eq $opt->{"Pg"}{host}[chs] ) {
                my ( $key, $prompt ) = ( 'host', 'Host' );
                opt_readline( $info, $opt, $section, $key, $prompt );
            }
            elsif ( $option eq $opt->{"Pg"}{port}[chs] ) {
                my ( $key, $prompt ) = ( 'port', 'Port' );
                opt_readline( $info, $opt, $section, $key, $prompt );
            }
            elsif ( $option eq $opt->{"Pg"}{binary_filter}[chs] ) {
                my ( $key, $prompt ) = ( 'binary_filter', 'Enable Binary Filter' );
                opt_yes_no( $info, $opt, $section, $key, $prompt );
            }
            else { die $option }
        }
    }
}


sub print_error_message {
    my ( $message ) = @_;
    utf8::decode( $message );
    print $message;
    choose( [ 'Press ENTER to continue' ], { prompt => '', %{$info->{lyt_stop}} } );
}


sub print_hash {
    my ( $info, $hash, $keys, $len_key, $maxcols ) = @_;
    my $s_tab = $len_key + length( ' : ' );
    my $lf = Text::LineFold->new(
        Charset       => 'utf-8',
        ColMax        => $maxcols,
        Newline       => "\n",
        OutputCharset => '_UNICODE_',
        Urgent        => 'FORCE',
    );
    my @vals = ();
    for my $key ( @$keys ) {
        my $pr_key = sprintf "%*.*s : ", $len_key, $len_key, $key;
        my $text = $lf->fold( '' , ' ' x $s_tab, $pr_key . $hash->{$key} );
        $text =~ s/\R+\z//;
        for my $val ( split /\R+/, $text ) {
            push @vals, $val;
        }
    }
    choose( [ @vals ], { %{$info->{lyt_3}} } );
}


sub local_readline {
    my ( $info, $args ) = @_;
    my $str = '';
    local $| = 1;
    local_readline_print( $info, $args, $str );
    ReadMode 'cbreak';
    while ( 1 ) {
        my $key = ReadKey;
        return if ! defined $key;
        if ( $key eq "\cD" ) {
            print "\n";
            return;
        }
        elsif ( $key eq "\n" or $key eq "\r" ) {
            print "\n";
            return $str;
        }
        elsif ( ord( $key ) == BSPACE || $key eq "\cH" ) {
            $str =~ s/.\z// if $str;
            local_readline_print( $info, $args, $str );
            next;
        }
        elsif ( $key !~ /^\p{Print}\z/ ) {
            local_readline_print( $info, $args, $str );
            next;
        }
        $str .= $key;
        local_readline_print( $info, $args, $str );
    }
    ReadMode 0;
    return $str;
}

sub local_readline_print {
    my ( $info, $args, $str ) = @_;
    print GO_TO_TOP_LEFT;
    print CLEAR_EOS;
    if ( defined $args->{status} ) {
        my $line_fold = Text::LineFold->new(
            %{$info->{line_fold}},
            ColMax => ( term_size() )[0],
        );
        print $line_fold->fold( '', ' ' x 4, $args->{status} );
    }
    print $args->{prompt} . ( $args->{no_echo} ? '' : $str );
}


sub write_config_file {
    my ( $opt, $file ) = @_;
    my $tmp = {};
    for my $section ( keys %$opt ) {
        for my $key ( keys %{$opt->{$section}} ) {
            $tmp->{$section}{$key} = $opt->{$section}{$key}[v];
        }
    }
    write_json( $file, $tmp );
}


sub read_config_file {
    my ( $opt, $file ) = @_;
    my $tmp = read_json( $file );
    for my $section ( keys %$tmp ) {
        for my $key ( keys %{$tmp->{$section}} ) {
            $opt->{$section}{$key}[v] = $tmp->{$section}{$key};
        }
    }
    return $opt;
}


sub write_json {
    my ( $file, $h_ref ) = @_;
    my $json = JSON::XS->new->pretty->encode( $h_ref );
    open my $fh, '>', encode( 'locale_fs', $file ) or die $!;
    print $fh $json;
    close $fh;
}


sub read_json {
    my ( $file ) = @_;
    return {} if ! -f encode( 'locale_fs', $file );
    open my $fh, '<', encode( 'locale_fs', $file ) or die $!;
    my $json = do { local $/; <$fh> };
    close $fh;
    my $h_ref = JSON::XS->new->pretty->decode( $json ) if $json;
    return $h_ref;
}


sub choose_a_number {
    my ( $info, $opt, $digits, $name, $current ) = @_;
    my $sep = $opt->{menu}{thsd_sep}[v];
    my $tab = '  -  ';
    my $gcs_tab = Unicode::GCString->new( $tab );
    my $length_tab = $gcs_tab->columns;
    my $longest = $digits;
    $longest += int( ( $digits - 1 ) / 3 ) if $sep ne '';
    my @choices_range = ();
    for my $di ( 0 .. $digits - 1 ) {
        my $begin = 1 . '0' x $di;
        $begin = 0 if $di == 0;
        $begin = insert_sep( $begin, $sep );
        ( my $end = $begin ) =~ s/^[01]/9/;
        unshift @choices_range, sprintf " %*s%s%*s", $longest, $begin, $tab, $longest, $end;
    }
    my $confirm = sprintf "%-*s", $longest * 2 + $length_tab, $info->{confirm};
    my $back    = sprintf "%-*s", $longest * 2 + $length_tab, $info->{back};
    my ( $term_width ) = term_size();
    my $gcs_longest_range = Unicode::GCString->new( $choices_range[0] );
    if ( $gcs_longest_range->columns > $term_width ) {
        @choices_range = ();
        for my $di ( 0 .. $digits - 1 ) {
            my $begin = 1 . '0' x $di;
            $begin = 0 if $di == 0;
            $begin = insert_sep( $begin, $sep );
            unshift @choices_range, sprintf "%*s", $longest, $begin;
        }
        $confirm = $info->{confirm};
        $back    = $info->{back};
    }
    my $reset = 'reset';
    my %numbers;
    my $result;

    NUMBER: while ( 1 ) {
        my $new_result = $result // '--';
        my $prompt = '';
        if ( @_ == 5 ) {
            $prompt .= sprintf "%s%*s\n",   'Current ' . $name . ': ', $longest, $current // '--';
            $prompt .= sprintf "%s%*s\n\n", '    New ' . $name . ': ', $longest, $new_result;
        }
        else {
            $prompt = sprintf "%s%*s\n\n", $name . ': ', $longest, $new_result;
        }
        # Choose
        my $range = choose(
            [ undef, @choices_range, $confirm ],
            { prompt => $prompt, %{$info->{lyt_3}}, justify => 1, undef => $back }
        );
        return if ! defined $range;
        if ( $range eq $confirm ) {
            return '--' if ! defined $result;
            $result =~ s/\Q$sep\E//g if $sep ne '';
            return $result;
        }
        my $zeros = ( split /\s*-\s*/, $range )[0];
        $zeros =~ s/^\s*\d//;
        ( my $zeros_no_sep = $zeros ) =~ s/\Q$sep\E//g if $sep ne '';
        my $count_zeros = length $zeros_no_sep;
        my @choices = $count_zeros ? map( $_ . $zeros, 1 .. 9 ) : ( 0 .. 9 );
        # Choose
        my $number = choose(
            [ undef, @choices, $reset ],
            { prompt => $prompt, %{$info->{lyt_1}} }
        );
        next if ! defined $number;
        if ( $number eq $reset ) {
            delete $numbers{$count_zeros};
        }
        else {
            $number =~ s/\Q$sep\E//g if $sep ne '';
            $numbers{$count_zeros} = $number;
        }
        $result = sum( @numbers{keys %numbers} );
        $result = insert_sep( $result, $sep );
    }
}


sub handle_reference {
    my ( $ref ) = @_;
    if ( ref $ref eq 'ARRAY' ) {
        return 'ref: [' . join( ',', map { '"' . $_ . '"' } @$ref ) . ']';
    }
    elsif ( ref $ref eq 'SCALAR' ) {
        return 'ref: \\' . $$ref;
    }
    elsif ( ref $ref eq 'HASH' ) {
        return 'ref: {' . join( ',', map { $_ . '=>"' . $ref->{$_} . '"' } keys %$ref ) . '}';
    }
    elsif ( ref $ref eq 'Regexp' ) {
        return 'ref: qr/' . $ref . '/';
    }
    elsif ( ref $ref eq 'VSTRING' ) {
        return 'ref: \v' . join '.', unpack 'C*', $$ref;
    }
    elsif ( ref $ref eq 'GLOB' ) {
        return 'ref: \\' . $$ref;
    }
    else {
        return 'ref: ' . ref( $ref );
    }
}


sub insert_sep {
    my ( $number, $separator ) = @_;
    return if ! defined $number;
    return $number if ! $separator;
    $number =~ s/(\d)(?=(?:\d{3})+\b)/$1$separator/g;
    return $number;
}


sub choose_list {
    my ( $info, $current, $available ) = @_;
    my $new = [];
    my $key_cur = 'Current > ';
    my $key_new = '    New > ';
    my $gcs_cur = Unicode::GCString->new( $key_cur );
    my $gcs_new = Unicode::GCString->new( $key_new );
    my $length_key = $gcs_cur->columns > $gcs_new->columns ? $gcs_cur->columns : $gcs_new->columns;
    while ( 1 ) {
        my $prompt = $key_cur . join( ', ', map { "\"$_\"" } @$current ) . "\n";
        $prompt   .= $key_new . join( ', ', map { "\"$_\"" } @$new )     . "\n\n";
        $prompt   .= 'Choose:';
        # Choose
        my $choice = choose(
            [ undef, $info->{_confirm}, map( "- $_", @$available ) ],
            { prompt => $prompt, lf => [0,$length_key], %{$info->{lyt_3}} }
        );
        return if ! defined $choice;
        if ( $choice eq $info->{_confirm} ) {
            return $new if @$new;
            return;
        }
        $choice =~ s/^-\s//;
        push @$new, $choice;
    }
}


sub unicode_sprintf {
    my ( $avail_width, $unicode, $right_justify ) = @_;
    my $gcs = Unicode::GCString->new( $unicode );
    my $colwidth = $gcs->columns;
    if ( $colwidth > $avail_width ) {
        my $pos = $gcs->pos;
        $gcs->pos( 0 );
        my $cols = 0;
        my $gc;
        while ( defined( $gc = $gcs->next ) ) {
            if ( $avail_width < ( $cols += $gc->columns ) ) {
                my $ret = $gcs->substr( 0, $gcs->pos - 1 );
                $gcs->pos( $pos );
                return $ret->as_string;
            }
        }
    }
    elsif ( $colwidth < $avail_width ) {
        if ( $right_justify ) {
            $unicode = " " x ( $avail_width - $colwidth ) . $unicode;
        }
        else {
            $unicode = $unicode . " " x ( $avail_width - $colwidth );
        }
    }
    return $unicode;
}


sub get_credentials {
    my ( $info, $opt, $db ) = @_;
    if ( $info->{db_driver} eq 'SQLite' ) {
        return '', '';
    }
    my ( $user, $passwd );
    if ( length( $opt->{$info->{db_driver}}{default_user}[v] // '' ) ) {
        $user = $opt->{$info->{db_driver}}{default_user}[v];
    }
    my $db_key = $info->{db_driver} . '_' . $db;
    my $login = $opt->{dbs}{login}[v];
    if( $login == 0 ) {
        $user //= $info->{login}{$info->{db_driver}}{$db}{user};
        $passwd = $info->{login}{$info->{db_driver}}{$db}{passwd};
    }
    elsif ( $login == 1 ) {
        $user //= $info->{login}{$info->{db_driver}}{user};
        $passwd = $info->{login}{$info->{db_driver}}{passwd};
    }
    # Readline
    if ( ! defined $user ) {
        $user = local_readline(
            $info,
            { prompt => 'User: ', status => "DB: $db" }
        );
    }
    if ( ! defined $passwd ) {
        $passwd = local_readline(
            $info,
            { prompt => 'Password: ', status => "DB: $db\nUser: $user", no_echo => 1 }
        );
    }
    if ( $login == 0 ) {
        $info->{login}{$info->{db_driver}}{$db}{user}   = $user;
        $info->{login}{$info->{db_driver}}{$db}{passwd} = $passwd;
    }
    elsif ( $login == 1 ) {
        $info->{login}{$info->{db_driver}}{user}   = $user;
        $info->{login}{$info->{db_driver}}{passwd} = $passwd;
    }
    return $user, $passwd;
}


########################################   database specific subroutines   #############################################


sub get_db_handle {
    my ( $info, $opt, $db ) = @_;
    my $db_driver = $info->{db_driver};
    my $db_key    = $info->{db_driver} . '_' . $db;
    my %db_opt = ();
    my $option;
    if ( $db_driver eq 'SQLite' ) {
        die "\"$db\": $!. Maybe the cached data is not up to date." if ! -f $db;
        $option = 'sqlite_unicode';
        $db_opt{$option} = $opt->{$db_key}{$option}[v] // $opt->{$db_driver}{$option}[v];
        $option = 'sqlite_see_if_its_a_number';
        $db_opt{$option} = $opt->{$db_key}{$option}[v] // $opt->{$db_driver}{$option}[v];
    }
    elsif ( $db_driver eq 'mysql' ) {
        $option = 'mysql_enable_utf8';
        $db_opt{$option} = $opt->{$db_key}{$option}[v] // $opt->{$db_driver}{$option}[v];
    }
    elsif ( $db_driver eq 'Pg' ) {
        $option = 'pg_enable_utf8';
        $db_opt{$option} = $opt->{$db_key}{$option}[v] // $opt->{$db_driver}{$option}[v];
    }
    my ( $user, $passwd ) = get_credentials( $info, $opt, $db );
    my $dsn = 'dbi:' . $info->{db_driver} . ':dbname=' . $db;
    my $host = $opt->{$db_key}{host}[v] || $opt->{$db_driver}{host}[v];
    my $port = $opt->{$db_key}{port}[v] || $opt->{$db_driver}{port}[v];
    $dsn .= ';host=' . $host if $host;
    $dsn .= ';port=' . $port if $port;
    my $dbh = DBI->connect( $dsn, $user, $passwd, {
        PrintError => 0,
        RaiseError => 1,
        AutoCommit => 1,
        ShowErrorStatement => 1,
        %db_opt,
    } ) or die DBI->errstr;
    if ( $info->{db_driver} eq 'SQLite' ) {
        $dbh->sqlite_create_function( 'regexp', 2, sub {
                my ( $regex, $string ) = @_;
                $string //= '';
                return $string =~ m/$regex/ism;
            }
        );
        $dbh->sqlite_create_function( 'truncate', 2, sub {
                my ( $number, $places ) = @_;
                return if ! defined $number;
                return $number if ! looks_like_number( $number );
                return sprintf( "%.*f", $places, int( $number * 10 ** $places ) / 10 ** $places );
            }
        );
        $dbh->sqlite_create_function( 'bit_length', 1, sub {
                my ( $string ) = @_;
                use bytes;
                return length( $string );
            }
        );
        $dbh->sqlite_create_function( 'char_length', 1, sub {
                my ( $string ) = @_;
                return length( $string );
            }
        );
    }
    return $dbh;
}


sub available_databases {
    my ( $info, $opt ) = @_;
    my $db_driver   = $info->{db_driver};
    my $system_info = $opt->{sql}{system_info}[v];
    my $databases = [];
    if ( $db_driver eq 'SQLite' ) {
        my @dirs = @ARGV ? @ARGV : ( $info->{home} );
        my $c_dirs = join ' ', @dirs;
        $info->{cache} = read_json( $info->{db_cache_file} );
        if ( $opt->{SQLite}{reset_cache}[v] ) {
            delete $info->{cache}{$c_dirs};
            $info->{cached} = '';
        }
        else {
            if ( $info->{cache}{$c_dirs} ) {
                $databases = $info->{cache}{$c_dirs};
                $info->{cached} = ' (cached)';
            }
            else {
                $info->{cached} = '';
            }
        }
        if ( ! $info->{cached} ) {
            say 'Searching...';
            for my $dir ( @dirs ) {
                find( {
                    wanted     => sub {
                        my $file = $File::Find::name;
                        return if ! -f $file;
                        return if ! -s $file;
                        return if ! -r $file;
                        #say $file;
                        if ( ! eval {
                            open my $fh, '<:raw', $file or die "$file: $!";
                            defined( read $fh, my $string, 13 ) or die "$file: $!";
                            close $fh;
                            push @$databases, decode( 'locale_fs', $file ) if $string eq 'SQLite format';
                            1 }
                        ) {
                            utf8::decode( $@ );
                            print $@;
                        }
                    },
                    no_chdir   => 1,
                },
                encode( 'locale_fs', $dir ) );
            }
            $info->{cache}{$c_dirs} = $databases;
            write_json( $info->{db_cache_file}, $info->{cache} );
            say 'Ended searching';
        }
    }
    elsif( $db_driver eq 'Pg' ) {
        my $dbh = get_db_handle( $info, $opt, 'postgres' );
        my $regexp = [];
        my $stmt = "SELECT datname FROM pg_database";
        if ( ! $system_info ) {
            $regexp = regexp_system( $db_driver, 'database' );
            $stmt .= " WHERE " . join( " AND ", ( "datname !~ ?" ) x @$regexp ) if @$regexp;
        }
        $stmt .= " ORDER BY datname";
        $databases = $dbh->selectcol_arrayref( $stmt, {}, @$regexp );
    }
    elsif( $db_driver eq 'mysql' ) {
        my $dbh = get_db_handle( $info, $opt, 'information_schema' );
        my $regexp = [];
        my $stmt = "SELECT schema_name FROM information_schema.schemata";
        if ( ! $system_info ) {
            $regexp = regexp_system( $db_driver, 'database' );
            $stmt .= " WHERE " . join( " AND ", ( "schema_name NOT REGEXP ?" ) x @$regexp ) if @$regexp;
        }
        $stmt .= " ORDER BY schema_name";
        $databases = $dbh->selectcol_arrayref( $stmt, {}, @$regexp );
    }
    return $databases;
}


sub regexp_system {
    my ( $db_driver, $level ) = @_;
    if ( $db_driver eq 'SQLite' ) {
        return                if $level eq 'database';
        return                if $level eq 'schema';
        return [ '^sqlite_' ] if $level eq 'table';
    }
    elsif ( $db_driver eq 'mysql' ) {
        return [ '^mysql$', '^information_schema$', '^performance_schema$' ] if $level eq 'database';
        return                                                               if $level eq 'schema';
        return                                                               if $level eq 'table';
    }
    elsif ( $db_driver eq 'Pg' ) {
        return [ '^postgres$', '^template0$', '^template1$' ] if $level eq 'database';
        return [ '^pg_', '^information_schema$' ]             if $level eq 'schema';
        return                                                if $level eq 'table';
    }
    else {
        return;
    }
}


sub get_schema_names {
    my ( $info, $opt, $dbh, $db ) = @_;
    my $db_driver   = $info->{db_driver};
    my $system_info = $opt->{sql}{system_info}[v];
    if ( $db_driver eq 'SQLite' ) {
        return [ 'main' ];
    }
    elsif ( $db_driver eq 'Pg' ) {
        my $regexp = [];
        my $stmt = "SELECT schema_name FROM information_schema.schemata";
        if ( ! $system_info ) {
            $regexp = regexp_system( $db_driver, 'schema' );
            $stmt .= " WHERE " . join( " AND ", ( "schema_name !~ ?" ) x @$regexp ) if @$regexp;
        }
        $stmt .= " ORDER BY schema_name";
        my $schemas = $dbh->selectcol_arrayref( $stmt, {}, @$regexp );
        return $schemas;
    }

    else {
        return [ $db ];
    }
}


sub get_table_names {
    my ( $info, $opt, $dbh, $db, $schema ) = @_;
    my $db_driver   = $info->{db_driver};
    my $system_info = $opt->{sql}{system_info}[v];
    my $tables   = [];
    if ( $db_driver eq 'SQLite' ) {
        my $regexp = [];
        my $stmt = "SELECT name FROM sqlite_master WHERE type = 'table'";
        if ( ! $system_info ) {
            $regexp = regexp_system( $db_driver, 'table' );
            $stmt .= " AND " . join( " AND ", ( "name NOT REGEXP ?" ) x @$regexp ) if @$regexp;
        }
        $stmt .= " ORDER BY name";
        $tables = $dbh->selectcol_arrayref( $stmt, {}, @$regexp );
        push @$tables, 'sqlite_master' if $system_info;
    }
    else {
        my $stmt = "SELECT table_name FROM information_schema.tables
                       WHERE table_schema = ?
                       ORDER BY table_name";
                        # AND table_type = 'BASE TABLE'
        $tables = $dbh->selectcol_arrayref( $stmt, {}, ( $schema ) );
    }
    return $tables;
}


sub column_names_and_types {
    my ( $info, $dbh, $db, $schema, $data ) = @_;
    if ( $info->{db_driver} eq 'SQLite' ) {
        for my $table ( @{$data->{$db}{$schema}{tables}} ) {
            my $sth = $dbh->prepare( "SELECT * FROM " . $dbh->quote_identifier( undef, undef, $table ) );
            $data->{$db}{$schema}{col_names}{$table} = $sth->{NAME};
            $data->{$db}{$schema}{col_types}{$table} = $sth->{TYPE};
        }
    }
    else {
        my $stmt;
        if ( $info->{db_driver} eq 'mysql' ) {
            $stmt = "SELECT table_name, column_name, column_type
                        FROM information_schema.columns
                        WHERE table_schema = ?";
        }
        else {
            $stmt = "SELECT table_name, column_name, data_type
                        FROM information_schema.columns
                        WHERE table_schema = ?";
        }
        my $sth = $dbh->prepare( $stmt );
        $sth->execute( $schema );
        while ( my $row = $sth->fetchrow_arrayref() ) {
            my ( $table, $col_name, $col_type ) = @$row;
            push @{$data->{$db}{$schema}{col_names}{$table}}, $col_name;
            push @{$data->{$db}{$schema}{col_types}{$table}}, $col_type;
        }
    }
    return $data;
}


sub primary_and_foreign_keys {
    my ( $info, $dbh, $db, $schema, $data ) = @_;
    my $pk_cols = {};
    my $fks     = {};
    for my $table ( @{$data->{$db}{$schema}{tables}} ) {
        if ( $info->{db_driver} eq 'SQLite' ) {
            for my $c ( @{$dbh->selectall_arrayref( "pragma foreign_key_list( $table )" )} ) {
                $fks->{$table}{$c->[0]}{foreign_key_col}  [$c->[1]] = $c->[3];
                $fks->{$table}{$c->[0]}{reference_key_col}[$c->[1]] = $c->[4];
                $fks->{$table}{$c->[0]}{reference_table} = $c->[2];
            }
        }
        elsif ( $info->{db_driver} eq 'mysql' ) {
            my $stmt = "SELECT constraint_name, table_name, column_name, referenced_table_name,
                               referenced_column_name, position_in_unique_constraint
                           FROM information_schema.key_column_usage
                           WHERE table_schema = ? AND table_name = ? AND referenced_table_name IS NOT NULL";
            my $sth = $dbh->prepare( $stmt );
            $sth->execute( $schema, $table );
            while ( my $row = $sth->fetchrow_hashref ) {
                my $fk_name = $row->{constraint_name};
                my $pos     = $row->{position_in_unique_constraint} - 1;
                $fks->{$table}{$fk_name}{foreign_key_col}  [$pos] = $row->{column_name};
                $fks->{$table}{$fk_name}{reference_key_col}[$pos] = $row->{referenced_column_name};
                if ( ! $fks->{$table}{$fk_name}{reference_table} ) {
                    $fks->{$table}{$fk_name}{reference_table} = $row->{referenced_table_name};
                }
            }
        }
        else {
            my $sth = $dbh->foreign_key_info( undef, undef, undef, undef, $schema, $table );
            if ( defined $sth ) {
                while ( my $row = $sth->fetchrow_hashref ) {
                    my $fk_name = $row->{FK_NAME};
                    push @{$fks->{$table}{$fk_name}{foreign_key_col  }}, $row->{FK_COLUMN_NAME};
                    push @{$fks->{$table}{$fk_name}{reference_key_col}}, $row->{UK_COLUMN_NAME};
                    if ( ! $fks->{$table}{$fk_name}{reference_table} ) {
                        $fks->{$table}{$fk_name}{reference_table} = $row->{UK_TABLE_NAME};
                    }
                }
            }
        }
        $pk_cols->{$table} = [ $dbh->primary_key( undef, $schema, $table ) ];
    }
    return $pk_cols, $fks;
}




sub sql_regexp {
    my ( $info, $opt, $quote_col, $not ) = @_;
    if ( $info->{db_driver} eq 'SQLite' ) {
        if ( $not ) {
            return ' '. $quote_col . ' NOT REGEXP ?';
        }
        else {
            return ' '. $quote_col . ' REGEXP ?';
        }
    }
    elsif ( $info->{db_driver} eq 'mysql' ) {
        if ( $not ) {
            return ' '. $quote_col . ' NOT REGEXP ?'        if ! $opt->{sql}{regexp_case}[v];
            return ' '. $quote_col . ' NOT REGEXP BINARY ?' if   $opt->{sql}{regexp_case}[v];
        }
        else {
            return ' '. $quote_col . ' REGEXP ?'            if ! $opt->{sql}{regexp_case}[v];
            return ' '. $quote_col . ' REGEXP BINARY ?'     if   $opt->{sql}{regexp_case}[v];
        }
    }
    elsif ( $info->{db_driver} eq 'Pg' ) {
        if ( $not ) {
            return ' '. $quote_col . '::text' . ' !~* ?' if ! $opt->{sql}{regexp_case}[v];
            return ' '. $quote_col . '::text' . ' !~ ?'  if   $opt->{sql}{regexp_case}[v];
        }
        else {
            return ' '. $quote_col . '::text' . ' ~* ?'  if ! $opt->{sql}{regexp_case}[v];
            return ' '. $quote_col . '::text' . ' ~ ?'   if   $opt->{sql}{regexp_case}[v];
        }
    }
    elsif ( $info->{db_driver} eq 'oracle' ) {
        if ( $not ) {
            return ' NOT REGEXP_LIKE(' . $quote_col . ',?,\'i\')' if ! $opt->{sql}{regexp_case}[v];
            return ' NOT REGEXP_LIKE(' . $quote_col . ',?)'       if   $opt->{sql}{regexp_case}[v];
        }
        else {
            return ' REGEXP_LIKE(' . $quote_col . ',?,\'i\')'     if ! $opt->{sql}{regexp_case}[v];
            return ' REGEXP_LIKE(' . $quote_col . ',?)'           if   $opt->{sql}{regexp_case}[v];
        }
    }
    die 'No entry for "' . $info->{db_driver} . '"!';
}


sub concatenate {
    my ( $db_driver, $arg ) = @_;
    return 'concat(' . join( ',', @$arg ) . ')' if $db_driver eq 'mysql';
    return join( ' || ', @$arg );
}


sub col_functions {
    my ( $info, $sql, $table, $func, $quote_col, $print_col ) = @_;
    my $db_driver    = $info->{db_driver};
    my $hidd_func_pr = $info->{hidd_func_pr};
    my ( $quote_f, $print_f );
    $print_f = $hidd_func_pr->{$func} . '(' . $print_col . ')';
    if ( $func =~ /^Epoch_to_Date(?:Time)?\z/ ) {
        my $prompt = print_select_statement( $info, $sql, $table, "$print_f\nInterval:" );
        my ( $microseconds, $milliseconds, $seconds ) = (
            '  ****************   Micro-Second',
            '  *************      Milli-Second',
            '  **********               Second' );
        my $choices = [ undef, $microseconds, $milliseconds, $seconds ];
        # Choose
        my $interval = choose( $choices, { prompt => $prompt, %{$info->{lyt_stmt_v}} } );
        return if ! defined $interval;
        my $div = $interval eq $microseconds ? 1000000 :
                  $interval eq $milliseconds ? 1000 : 1;
        if ( $func eq 'Epoch_to_DateTime' ) {
            $quote_f = "FROM_UNIXTIME($quote_col/$div,'%Y-%m-%d %H:%i:%s')"    if $db_driver eq 'mysql';
            $quote_f = "(TO_TIMESTAMP(${quote_col}::bigint/$div))::timestamp"  if $db_driver eq 'Pg';
            $quote_f = "DATETIME($quote_col/$div,'unixepoch','localtime')"     if $db_driver eq 'SQLite';
        }
        else {
            # mysql: FROM_UNIXTIME doesn't work with negative timestamps
            $quote_f = "FROM_UNIXTIME($quote_col/$div,'%Y-%m-%d')"       if $db_driver eq 'mysql';
            $quote_f = "(TO_TIMESTAMP(${quote_col}::bigint/$div))::date" if $db_driver eq 'Pg';
            $quote_f = "DATE($quote_col/$div,'unixepoch','localtime')"   if $db_driver eq 'SQLite';
        }
    }
    elsif ( $func eq 'Truncate' ) {
        my $prompt = print_select_statement( $info, $sql, $table, "TRUNC $print_col\nDecimal places:" );
        my $choices = [ undef, 0 .. 9 ];
        my $precision = choose( $choices, { prompt => $prompt, %{$info->{lyt_stmt_h}} } );
        return if ! defined $precision;
        if ( $db_driver eq 'Pg' ) {
            $quote_f = "TRUNC($quote_col,$precision)";
        }
        else {
            $quote_f = "TRUNCATE($quote_col,$precision)";
        }
    }
    elsif ( $func eq 'Bit_Length' ) {
        $quote_f = "BIT_LENGTH($quote_col)";
    }
    elsif ( $func eq 'Char_Length' ) {
        $quote_f = "CHAR_LENGTH($quote_col)";
    }
    return $quote_f, $print_f;
}





__END__



=pod

=encoding UTF-8

=head1 NAME

C<db-browser> - Search and read in SQLite/MySQL/PostgreSQL databases.

=head1 VERSION

Version 0.016

=cut

=head1 SYNOPSIS

=head2 SQLite/MySQL/PostgreSQL

    db-browser

    db-browser -h|--help

When the C<db-browser> is called with the argument C<-h|--help> it shows a menu - see L</Options>.

=head2 SQLite

    db-browser [-s|--search] [directories to be searched]

If no directories are passed the home directory is searched for SQLite databases.

C<db-browser> called with C<-s|--search> causes a new search of SQLite databases instead of using the cached data.

=head1 DESCRIPTION

Search and read in SQLite/MySQL/PostgreSQL databases.

To be able to browse database-, schema- and table-lists and the content of tables one needs to have the database
privileges required for fetching the respective data.

The C<db-browser> expects an existing home directory with read and write permissions for the user of C<db-browser>.

It is recommended to work with an UTF encoding. Non mappable characters might break the output.

It is needed a terminal with a width of at least 40 print columns.

=head1 USAGE

The best way to find out how C<db-browser> works is calling C<db-browser>.

To be able to use all the features of the C<db-browser> some basic SQL knowledge is required.

=head3 Keys to move around

=over

=item *

the C<Arrow> keys (or C<hjkl>) to move up and down respective to move right and left.

=item *

the C<PageUp> key (or C<Ctrl-B>) to go back one page, the C<PageDown> key (or C<Ctrl-F>) to go forward one page.

=item *

the C<Home> key (or C<Ctrl-A>) to jump to the beginning of the menu, the C<End> key (or C<Ctrl-E>) to jump to the end
of the menu.

=item *

the C<Enter/Return> key to confirm a chosen menu item.

=back

With the option I<mouse mode> enabled it can be used the mouse with the left mouse key to navigate through the menus
instead of the keyboard keys.

The C<q> key goes back (C<Ctrl-D> instead of C<q> if prompted for a string) in the menu hierarchy.

=head2 OPTIONS

=head3 HELP

Show this Info.

=head3 Path

Shows the path of the running C<db-browser> and the application directory.

=head3 Tabwidth

Set the number of spaces between columns.

=head3 Colwidth

Set the width the columns should have at least when printed.

=head3 Undef

Set the string that will be shown on the screen instead of an undefined field.

=head3 Max Rows

Set the maximum number of fetched table rows. This can be overwritten by setting a SQL LIMIT statement.

The fetched table rows are kept in memory.

To disable the automatic limit set I<max rows> to C<undef> (--).

=head3 ProgressBar

Set the progress bar threshold. If the number of fields (rows x columns) is higher than the threshold a progress bar is
shown while preparing the data for the output.

=head3 DB Drivers

Choose the needed database drivers.

=head3 Operators

Choose the needed operators.

=head3 Parentheses

Enable parentheses in WHERE and/or HAVING TO clauses.

=head3 Separator

Choose the thousands separator displayed in menus.

=head3 Sssc Mode

With the I<Sssc> mode "compat" enabled back-arrows are offered in the SQL menus entries. In the "simple" mode it can
be used the C<q> key instead of the back-arrows.

To reset a SQL "sub-statement" (e.g WHERE) re-enter into the respective menu entry and choose C<'- OK -'>.

=head3 Expand

Set the behavior of different menus.

For the menus "Choose Database", "Choose Schema" and "Choose Table" setting I<Expand> to "Enchanted" means: save the
menu position.

For "Print Table" "Enchanted" means: enable printing the chosen table row.

=head3 Mouse Mode

Set the I<mouse mode> (see L<Term::Choose/mouse>).

=head3 Lock

Set the default I<lock> value:

- Lk0: Reset the SQL-statement after each "PrintTable".

- Lk1: Reset the SQL-statement only when a table is selected.

=head3 Metadata

If enabled system tables/schemas/databases are appended to the respective list.

=head3 Regexp Case

If enabled REGEXP will match case sensitive.

With MySQL the sensitive match is achieved by enabling the BINARY operator.

=head3 DB Login

Determine when C<db-browser> asks for the login data:

- once per DB: log in data is asked once per database.

- only once: log in data is asked only once and then used for all connections.

=head3 DB Defaults

Set Database defaults:

=head4 Default User

If set the I<default user> is used as username for database logins.

=head4 See if it's a number

If set to "YES" L<DBD::SQLite> tries to see if the bind values are number or not, and does not quote if they are
numbers.

See L<DBD::SQLite> for details. This is a SQLite-only option.

=head4 Enable utf8

If enabled the utf8 flag will be turned on for character data coming from the database.

For a more driver specific explanation see the documentation of the respective Perl DBI driver.

=head4 "Binary Filter"

Print "BNRY" instead of arbitrary binary data.

If the data matches the repexp C</[\x00-\x08\x0B-\x0C\x0E-\x1F]/> it is considered arbitrary binary data.

Printing arbitrary binary data could break the output.

Database defaults can be overwritten for each Database with the "Database settings".

=head1 HINTS

The following hints refer to the SQL menu - the menu which opens after a table was selected.

If AGGREGATE or/and GROUP BY is set, the SELECT statement is automatically formed.

Changing the lock mode I<Lk[01]> will also reset the entire SQL.

=head3 Scalar functions

There is a hidden menu entry: selecting the prompt "Customize:" makes available some scalar functions.

To remove a chosen scalar function from a column select the column with the function a second time.

With SQLite the function TRUNCATE returns stringified values:

- when comparing in WHERE or HAVING TO clauses with numbers take the non-truncated (original) value for the
comparison if C<sqlite_see_if_its_a_number> is enabled (default).

- also to get a numeric comparison in an ORDER BY clause use the non-truncated (original) values for the ordering.

=head1 AUTHOR

Matthäus Kiem <cuer2s@gmail.com>

=head1 CREDITS

Thanks to the L<Perl-Community.de|http://www.perl-community.de> and the people form
L<stackoverflow|http://stackoverflow.com> for the help.

=head1 LICENSE AND COPYRIGHT

Copyright 2012-2014 Matthäus Kiem.

This program is free software; you can redistribute it and/or modify it under the same terms as Perl 5.10.0. For
details, see the full text of the licenses in the file LICENSE.

=cut
