| 1 | package App::DBBrowser; | ||
| 2 | |||
| 3 | use warnings; | ||
| 4 | use strict; | ||
| 5 | use 5.016; | ||
| 6 | |||
| 7 | our $VERSION = '2.447'; | ||
| 8 | |||
| 9 | use File::Basename qw( basename ); | ||
| 10 | use File::Spec::Functions qw( catfile catdir ); | ||
| 11 | use Getopt::Long qw( GetOptions ); | ||
| 12 | |||
| 13 | use DBI::Const::GetInfoType; | ||
| 14 | use Encode::Locale qw( decode_argv ); | ||
| 15 | use File::HomeDir qw(); | ||
| 16 | use File::Which qw( which ); | ||
| 17 | |||
| 18 | use Term::Choose qw(); | ||
| 19 | use Term::Choose::Screen qw( clear_screen ); | ||
| 20 | |||
| 21 | use App::DBBrowser::Auxil; | ||
| 22 | #use App::DBBrowser::CreateDropAttach; # required | ||
| 23 | use App::DBBrowser::DB; | ||
| 24 | #use App::DBBrowser::From # required | ||
| 25 | #use App::DBBrowser::Options; # required | ||
| 26 | use App::DBBrowser::Options::Defaults; | ||
| 27 | use App::DBBrowser::Options::ReadWrite; | ||
| 28 | #use App::DBBrowser::Table; # required | ||
| 29 | |||
| 30 | |||
| 31 | BEGIN { decode_argv() } | ||
| 32 | |||
| 33 | |||
| 34 | sub new { | ||
| 35 | my ( $class ) = @_; | ||
| 36 | my $info = { | ||
| 37 | dots => [ '...', 3 ], | ||
| 38 | quit => 'Quit', | ||
| 39 | back => 'Back', | ||
| 40 | # continue => 'Continue' | ||
| 41 | confirm => 'Confirm', | ||
| 42 | reset => 'Reset', | ||
| 43 | _quit => ' Quit', | ||
| 44 | _back => ' Back', | ||
| 45 | _continue => ' Continue', | ||
| 46 | _confirm => ' Confirm', | ||
| 47 | _reset => ' Reset', | ||
| 48 | s_back => '<<', | ||
| 49 | ok => '-OK-', | ||
| 50 | menu_addition => '%%', | ||
| 51 | info_thsd_sep => ',', | ||
| 52 | }; | ||
| 53 | $info->{tc_default} = { hide_cursor => 0, clear_screen => 1, page => 2, keep => 8, undef => $info->{s_back}, prompt => 'Your choice:' }; ## | ||
| 54 | $info->{tcu_default} = { hide_cursor => 0, clear_screen => 1, page => 2, keep => 8, confirm => $info->{ok}, back => $info->{s_back} }; | ||
| 55 | $info->{tf_default} = { hide_cursor => 2, clear_screen => 1, page => 2, keep => 8, auto_up => 1, skip_items => qr/^\s*\z/ }; | ||
| 56 | $info->{tr_default} = { hide_cursor => 2, clear_screen => 1, page => 2, history => [ 0 .. 1000 ] }; | ||
| 57 | $info->{lyt_h} = { order => 0, alignment => 2 }; | ||
| 58 | $info->{lyt_v} = { undef => $info->{_back}, layout => 2 }; | ||
| 59 | return bless { i => $info }, $class; | ||
| 60 | } | ||
| 61 | |||
| 62 | |||
| 63 | sub __init { | ||
| 64 | my ( $sf ) = @_; | ||
| 65 | my $home = File::HomeDir->my_home(); | ||
| 66 | if ( ! $home ) { | ||
| 67 | print "'File::HomeDir->my_home()' could not find the home directory!\n"; | ||
| 68 | print "'db-browser' requires a home directory\n"; | ||
| 69 | exit; | ||
| 70 | } | ||
| 71 | $sf->{i}{home_dir} = $home; | ||
| 72 | my $config_home; | ||
| 73 | if ( which( 'xdg-user-dir' ) ) { | ||
| 74 | $config_home = File::HomeDir::FreeDesktop->my_config(); | ||
| 75 | } | ||
| 76 | else { | ||
| 77 | $config_home = File::HomeDir->my_data(); | ||
| 78 | } | ||
| 79 | my $app_dir = catdir( $config_home // $home, 'db_browser' ); | ||
| 80 | mkdir $app_dir or die $! if ! -d $app_dir; | ||
| 81 | $sf->{i}{app_dir} = $app_dir; | ||
| 82 | $sf->{i}{f_attached_db} = catfile $app_dir, 'attached_DB.json'; | ||
| 83 | $sf->{i}{f_dir_history} = catfile $app_dir, 'dir_history.json'; | ||
| 84 | $sf->{i}{f_subqueries} = catfile $app_dir, 'subqueries.json'; | ||
| 85 | $sf->{i}{f_search_and_replace} = catfile $app_dir, 'search_and_replace.json'; | ||
| 86 | $sf->{i}{f_global_settings} = catfile $app_dir, 'global_settins.json'; | ||
| 87 | my $db_cache_dir = catdir( $app_dir, 'cache_database_names' ); | ||
| 88 | mkdir $db_cache_dir or die $! if ! -d $db_cache_dir; | ||
| 89 | $sf->{i}{db_cache_file_fmt} = catfile $db_cache_dir, 'databases_%s.json'; | ||
| 90 | my $plugin_config_dir = catdir( $app_dir, 'config_plugins' ); | ||
| 91 | mkdir $plugin_config_dir or die $! if ! -d $plugin_config_dir; | ||
| 92 | $sf->{i}{plugin_config_file_fmt} = catfile $plugin_config_dir, 'config_%s.json'; | ||
| 93 | $sf->{i}{db_config_file_fmt} = catfile $plugin_config_dir, 'config_%s_Databases.json'; | ||
| 94 | } | ||
| 95 | |||
| 96 | |||
| 97 | sub __options { | ||
| 98 | my ( $sf ) = @_; | ||
| 99 | if ( ! eval { | ||
| 100 | my $help; | ||
| 101 | GetOptions ( | ||
| 102 | 'h|?|help' => \$help, | ||
| 103 | 's|search' => \$sf->{i}{search}, | ||
| 104 | ); | ||
| 105 | if ( $help ) { | ||
| 106 | print clear_screen(); | ||
| 107 | require App::DBBrowser::Options; | ||
| 108 | my $op = App::DBBrowser::Options->new( $sf->{i}, $sf->{o} ); | ||
| 109 | $op->set_options(); | ||
| 110 | } | ||
| 111 | 1 } | ||
| 112 | ) { | ||
| 113 | my $ax = App::DBBrowser::Auxil->new( $sf->{i}, {}, {} ); | ||
| 114 | $ax->print_error_message( $@ ); | ||
| 115 | while ( $ARGV[0] && $ARGV[0] =~ /^-/ ) { | ||
| 116 | my $arg = shift @ARGV; | ||
| 117 | last if $arg eq '--'; | ||
| 118 | } | ||
| 119 | } | ||
| 120 | my $op_rw = App::DBBrowser::Options::ReadWrite->new( $sf->{i}, {} ); | ||
| 121 | $sf->{o} = $op_rw->read_config_file(); | ||
| 122 | } | ||
| 123 | |||
| 124 | |||
| 125 | sub run { | ||
| 126 | my ( $sf ) = @_; | ||
| 127 | local $| = 1; | ||
| 128 | $sf->__init(); | ||
| 129 | $sf->__options(); | ||
| 130 | my $tc = Term::Choose->new( $sf->{i}{tc_default} ); | ||
| 131 | my $ax = App::DBBrowser::Auxil->new( $sf->{i}, $sf->{o}, {} ); | ||
| 132 | my $skipped_menus = 0; | ||
| 133 | my $old_idx_plugin = 0; | ||
| 134 | my $available_plugins = $sf->{o}{G}{plugins}; | ||
| 135 | |||
| 136 | PLUGIN: while ( 1 ) { | ||
| 137 | |||
| 138 | my $plugin; | ||
| 139 | if ( @$available_plugins == 1 ) { | ||
| 140 | $skipped_menus++; | ||
| 141 | $plugin = $available_plugins->[0]; | ||
| 142 | print clear_screen(); | ||
| 143 | } | ||
| 144 | else { | ||
| 145 | my $menu_plugins = [ undef, map( "- $_", @$available_plugins ) ]; | ||
| 146 | # Choose | ||
| 147 | my $idx_plugin = $tc->choose( | ||
| 148 | $menu_plugins, | ||
| 149 | { %{$sf->{i}{lyt_v}}, prompt => 'DB Plugin: ', index => 1, default => $old_idx_plugin, | ||
| 150 | undef => $sf->{i}{_quit} } | ||
| 151 | ); | ||
| 152 | if ( defined $idx_plugin ) { | ||
| 153 | $plugin = $menu_plugins->[$idx_plugin]; | ||
| 154 | } | ||
| 155 | if ( ! defined $plugin ) { | ||
| 156 | last PLUGIN; | ||
| 157 | } | ||
| 158 | if ( $sf->{o}{G}{menu_memory} ) { | ||
| 159 | if ( $old_idx_plugin == $idx_plugin && ! $ENV{TC_RESET_AUTO_UP} ) { | ||
| 160 | $old_idx_plugin = 0; | ||
| 161 | next PLUGIN; | ||
| 162 | } | ||
| 163 | $old_idx_plugin = $idx_plugin; | ||
| 164 | } | ||
| 165 | $plugin =~ s/^[-\ ]\s//; | ||
| 166 | } | ||
| 167 | $sf->{i}{plugin} = $plugin; | ||
| 168 | |||
| 169 | my $plui = App::DBBrowser::DB->new( $sf->{i}, $sf->{o} ); | ||
| 170 | my $driver; | ||
| 171 | if ( ! eval { | ||
| 172 | $driver = $sf->{i}{driver} = $plui->get_db_driver(); | ||
| 173 | #die "No database driver!" if ! $driver; | ||
| 174 | 1 } | ||
| 175 | ) { | ||
| 176 | $ax->print_error_message( $@ ); | ||
| 177 | next PLUGIN if @$available_plugins > 1; | ||
| 178 | last PLUGIN; | ||
| 179 | } | ||
| 180 | my $op_rw = App::DBBrowser::Options::ReadWrite->new( $sf->{i}, $sf->{o} ); | ||
| 181 | delete $sf->{o}{connect_data}; | ||
| 182 | delete $sf->{o}{connect_attr}; | ||
| 183 | $op_rw->read_config_file( $driver, $plugin ); | ||
| 184 | |||
| 185 | # DATABASES | ||
| 186 | |||
| 187 | my @databases; | ||
| 188 | my ( $user_dbs, $sys_dbs ) = ( [], [] ); | ||
| 189 | if ( ! eval { | ||
| 190 | ( $user_dbs, $sys_dbs ) = $plui->get_databases(); | ||
| 191 | 1 } | ||
| 192 | ) { | ||
| 193 | $ax->print_error_message( $@ ); | ||
| 194 | $sf->{i}{login_error} = 1; | ||
| 195 | next PLUGIN if @$available_plugins > 1; | ||
| 196 | last PLUGIN; | ||
| 197 | } | ||
| 198 | my $prefix = $driver =~ /^(?:SQLite|DuckDB|Firebird)\z/ ? '' : '- '; | ||
| 199 | if ( $sf->{o}{G}{metadata} ) { | ||
| 200 | if ( $prefix ) { | ||
| 201 | @databases = ( map( $prefix . $_, @$user_dbs ), map( ' ' . $_, @$sys_dbs ) ); | ||
| 202 | } | ||
| 203 | else { | ||
| 204 | @databases = ( @$user_dbs, @$sys_dbs ); | ||
| 205 | } | ||
| 206 | } | ||
| 207 | else { | ||
| 208 | if ( $prefix ) { | ||
| 209 | @databases = ( map( $prefix . $_, @$user_dbs ) ); | ||
| 210 | } | ||
| 211 | else { | ||
| 212 | @databases = @$user_dbs; | ||
| 213 | } | ||
| 214 | } | ||
| 215 | $sf->{i}{search} = 0 if $sf->{i}{search}; | ||
| 216 | if ( ! @databases ) { | ||
| 217 | $ax->print_error_message( "$plugin: no databases found\n" ); ## | ||
| 218 | next PLUGIN if @$available_plugins > 1; | ||
| 219 | last PLUGIN; | ||
| 220 | } | ||
| 221 | my $old_idx_db = 0; | ||
| 222 | |||
| 223 | DATABASE: while ( 1 ) { | ||
| 224 | |||
| 225 | my $db; | ||
| 226 | my $is_system_db = 0; | ||
| 227 | if ( $sf->{redo_db} ) { | ||
| 228 | $db = delete $sf->{redo_db}; | ||
| 229 | $is_system_db = delete $sf->{redo_is_system_db}; | ||
| 230 | } | ||
| 231 | elsif ( @databases == 1 ) { | ||
| 232 | $db = $databases[0]; | ||
| 233 | $db =~ s/^[-\ ]\s// if $prefix; | ||
| 234 | if ( ! @$user_dbs ) { | ||
| 235 | $is_system_db = 1; | ||
| 236 | } | ||
| 237 | $skipped_menus++ if $skipped_menus == 1; | ||
| 238 | } | ||
| 239 | else { | ||
| 240 | my $back; | ||
| 241 | if ( $prefix ) { | ||
| 242 | $back = $skipped_menus ? $sf->{i}{_quit} : $sf->{i}{_back}; | ||
| 243 | } | ||
| 244 | else { | ||
| 245 | $back = $skipped_menus ? $sf->{i}{quit} : $sf->{i}{back}; | ||
| 246 | } | ||
| 247 | my $prompt = 'Choose Database:'; | ||
| 248 | my $menu_db = [ undef, @databases ]; | ||
| 249 | # Choose | ||
| 250 | my $idx_db = $tc->choose( | ||
| 251 | $menu_db, | ||
| 252 | { %{$sf->{i}{lyt_v}}, prompt => $prompt, index => 1, default => $old_idx_db, undef => $back } | ||
| 253 | ); | ||
| 254 | if ( defined $idx_db ) { | ||
| 255 | $db = $menu_db->[$idx_db]; | ||
| 256 | } | ||
| 257 | if ( ! defined $db ) { | ||
| 258 | next PLUGIN if @$available_plugins > 1; | ||
| 259 | last PLUGIN; | ||
| 260 | } | ||
| 261 | if ( $sf->{o}{G}{menu_memory} ) { | ||
| 262 | if ( $old_idx_db == $idx_db && ! $ENV{TC_RESET_AUTO_UP} ) { | ||
| 263 | $old_idx_db = 0; | ||
| 264 | next DATABASE; | ||
| 265 | } | ||
| 266 | $old_idx_db = $idx_db; | ||
| 267 | } | ||
| 268 | $db =~ s/^[-\ ]\s// if $prefix; | ||
| 269 | if ( $idx_db - 1 > $#$user_dbs ) { | ||
| 270 | $is_system_db = 1; | ||
| 271 | } | ||
| 272 | } | ||
| 273 | $sf->{d} = { | ||
| 274 | db => $db, | ||
| 275 | user_dbs => $user_dbs, | ||
| 276 | sys_dbs => $sys_dbs, | ||
| 277 | }; | ||
| 278 | |||
| 279 | # DB-HANDLE | ||
| 280 | |||
| 281 | my $dbh; | ||
| 282 | if ( ! eval { | ||
| 283 | $dbh = $plui->get_db_handle( $db ); | ||
| 284 | $sf->{d}{identifier_quote_char} = $dbh->get_info( $GetInfoType{SQL_IDENTIFIER_QUOTE_CHAR} ) // '"'; | ||
| 285 | #$sf->{d}{catalog_name_sep} = $dbh->get_info( $GetInfoType{SQL_CATALOG_NAME_SEPARATOR} ) // '.'; | ||
| 286 | #$sf->{d}{catalog_location} = $dbh->get_info( $GetInfoType{SQL_CATALOG_LOCATION} ) || 1; | ||
| 287 | 1 } | ||
| 288 | ) { | ||
| 289 | $ax->print_error_message( $@ ); | ||
| 290 | # remove database from @databases | ||
| 291 | $sf->{i}{login_error} = 1; | ||
| 292 | $dbh->disconnect() if defined $dbh && $dbh->{Active}; | ||
| 293 | next DATABASE if @databases > 1; | ||
| 294 | next PLUGIN if @$available_plugins > 1; | ||
| 295 | last PLUGIN; | ||
| 296 | } | ||
| 297 | $sf->{d}{dbh} = $dbh; | ||
| 298 | if ( $driver eq 'ODBC' ) { | ||
| 299 | my $dbms_name = $dbh->get_info( $GetInfoType{SQL_DBMS_NAME} ); | ||
| 300 | if ( $dbms_name =~ /sql server/i ) { | ||
| 301 | $sf->{i}{dbms} = 'MSSQL'; | ||
| 302 | } | ||
| 303 | elsif ( $dbms_name =~ /postgresql/i ) { | ||
| 304 | $sf->{i}{dbms} = 'Pg'; | ||
| 305 | } | ||
| 306 | else { | ||
| 307 | for my $type ( qw( Oracle DB2 Informix MariaDB mysql SQLite DuckDB Firebird ) ) { | ||
| 308 | if ( $dbms_name =~ /$type/i ) { | ||
| 309 | $sf->{i}{dbms} = $type; | ||
| 310 | last; | ||
| 311 | } | ||
| 312 | } | ||
| 313 | } | ||
| 314 | } | ||
| 315 | else { | ||
| 316 | $sf->{i}{dbms} = $driver; | ||
| 317 | } | ||
| 318 | if ( $sf->{i}{dbms} =~ /^(?:SQLite|DuckDB)\z/ && -f $sf->{i}{f_attached_db} ) { | ||
| 319 | if ( ! eval { | ||
| 320 | require App::DBBrowser::CreateDropAttach::AttachDB; | ||
| 321 | my $att = App::DBBrowser::CreateDropAttach::AttachDB->new( $sf->{i}, $sf->{o}, $sf->{d} ); | ||
| 322 | my $h_ref = $ax->read_json( $sf->{i}{f_attached_db} ) // {}; | ||
| 323 | my $attached_db = $h_ref->{$plugin}{$db} // {}; | ||
| 324 | if ( %$attached_db ) { | ||
| 325 | for my $alias ( sort keys %$attached_db ) { | ||
| 326 | my $stmt = $att->attach_stmt( $dbh, $attached_db->{$alias}, $alias ); | ||
| 327 | $dbh->do( $stmt ); | ||
| 328 | } | ||
| 329 | $sf->{d}{db_attached} = 1; | ||
| 330 | } | ||
| 331 | 1 } | ||
| 332 | ) { | ||
| 333 | $ax->print_error_message( $@ ); | ||
| 334 | $dbh->disconnect() if defined $dbh && $dbh->{Active}; | ||
| 335 | next DATABASE if @databases > 1; | ||
| 336 | next PLUGIN if @$available_plugins > 1; | ||
| 337 | last PLUGIN; | ||
| 338 | } | ||
| 339 | } | ||
| 340 | |||
| 341 | # SCHEMAS | ||
| 342 | |||
| 343 | my @schemas; | ||
| 344 | my ( $user_schemas, $sys_schemas ) = ( [], [] ); | ||
| 345 | if ( ! eval { | ||
| 346 | ( $user_schemas, $sys_schemas ) = $plui->get_schemas( $dbh, $db, $is_system_db ); | ||
| 347 | 1 } | ||
| 348 | ) { | ||
| 349 | $ax->print_error_message( $@ ); | ||
| 350 | $dbh->disconnect(); | ||
| 351 | next DATABASE if @databases > 1; | ||
| 352 | next PLUGIN if @$available_plugins > 1; | ||
| 353 | last PLUGIN; | ||
| 354 | } | ||
| 355 | my $undef_str = ''; | ||
| 356 | if ( $sf->{o}{G}{metadata} ) { | ||
| 357 | @schemas = ( map( '- ' . ( $_ // $undef_str ), @$user_schemas ), | ||
| 358 | map( ' ' . ( $_ // $undef_str ), @$sys_schemas ) ); | ||
| 359 | } | ||
| 360 | else { | ||
| 361 | @schemas = ( map( '- ' . ( $_ // $undef_str ), @$user_schemas ) ); | ||
| 362 | } | ||
| 363 | my $old_idx_sch = 0; | ||
| 364 | |||
| 365 | SCHEMA: while ( 1 ) { | ||
| 366 | |||
| 367 | my $db_string = 'DB ' . basename( $db ) . ''; | ||
| 368 | my $schema; | ||
| 369 | my $is_system_schema = 0; | ||
| 370 | if ( $sf->{redo_schema} ) { | ||
| 371 | $schema = delete $sf->{redo_schema}; | ||
| 372 | $is_system_schema = delete $sf->{redo_is_system_schema}; | ||
| 373 | } | ||
| 374 | elsif ( ! @schemas ) { | ||
| 375 | # `$schema` remains undefined | ||
| 376 | } | ||
| 377 | elsif ( @schemas == 1 ) { | ||
| 378 | $schema = ( @$user_schemas, @$sys_schemas )[0]; # to preserve unstringified `undef` | ||
| 379 | $skipped_menus++ if $skipped_menus == 2; | ||
| 380 | } | ||
| 381 | else { | ||
| 382 | my $back = $skipped_menus == 2 ? $sf->{i}{_quit} : $sf->{i}{_back}; | ||
| 383 | my $prompt = $db_string . ':'; | ||
| 384 | my $menu_schema = [ undef, @schemas ]; | ||
| 385 | # Choose | ||
| 386 | my $idx_sch = $tc->choose( | ||
| 387 | $menu_schema, | ||
| 388 | { %{$sf->{i}{lyt_v}}, prompt => $prompt, index => 1, default => $old_idx_sch, undef => $back } | ||
| 389 | ); | ||
| 390 | if ( defined $idx_sch ) { | ||
| 391 | $schema = $menu_schema->[$idx_sch]; | ||
| 392 | } | ||
| 393 | if ( ! defined $schema ) { | ||
| 394 | $dbh->disconnect(); | ||
| 395 | next DATABASE if @databases > 1; | ||
| 396 | next PLUGIN if @$available_plugins > 1; | ||
| 397 | last PLUGIN; | ||
| 398 | } | ||
| 399 | if ( $sf->{o}{G}{menu_memory} ) { | ||
| 400 | if ( $old_idx_sch == $idx_sch && ! $ENV{TC_RESET_AUTO_UP} ) { | ||
| 401 | $old_idx_sch = 0; | ||
| 402 | next SCHEMA; | ||
| 403 | } | ||
| 404 | $old_idx_sch = $idx_sch; | ||
| 405 | } | ||
| 406 | $schema = ( @$user_schemas, @$sys_schemas )[ $idx_sch - 1 ]; # to preserve unstringified `undef` | ||
| 407 | if ( $idx_sch - 1 > $#$user_schemas ) { | ||
| 408 | $is_system_schema = 1; | ||
| 409 | } | ||
| 410 | } | ||
| 411 | $db_string = 'DB ' . basename( $db ) . ( @schemas > 1 ? '.' . ( $schema // $undef_str ) : '' ) . ':'; | ||
| 412 | $sf->{d}{schema} = $schema; | ||
| 413 | if ( $sf->{i}{dbms} eq 'SQLite' ) { | ||
| 414 | $sf->{d}{schema} //= 'main'; | ||
| 415 | # $schema remains as it is | ||
| 416 | } | ||
| 417 | $sf->{d}{is_system_schema} = $is_system_schema; | ||
| 418 | $sf->{d}{db_string} = $db_string; | ||
| 419 | |||
| 420 | # TABLES | ||
| 421 | |||
| 422 | my ( $tables_info, $user_table_keys, $sys_table_keys ); | ||
| 423 | if ( ! eval { | ||
| 424 | ( $tables_info, $user_table_keys, $sys_table_keys ) = $plui->tables_info( $dbh, $schema, $is_system_schema, $sf->{d}{db_attached} ); | ||
| 425 | 1 } | ||
| 426 | ) { | ||
| 427 | $ax->print_error_message( $@ ); | ||
| 428 | next SCHEMA if @schemas > 1; | ||
| 429 | $dbh->disconnect(); | ||
| 430 | next DATABASE if @databases > 1; | ||
| 431 | next PLUGIN if @$available_plugins > 1; | ||
| 432 | last PLUGIN; | ||
| 433 | } | ||
| 434 | $sf->{d}{tables_info} = $tables_info; | ||
| 435 | $sf->{d}{user_table_keys} = $user_table_keys; | ||
| 436 | $sf->{d}{sys_table_keys} = $sys_table_keys; | ||
| 437 | $sf->{d}{cte_history} = []; | ||
| 438 | my $old_idx_tbl = 1; | ||
| 439 | |||
| 440 | TABLE: while ( 1 ) { | ||
| 441 | |||
| 442 | my ( $from_join, $from_union, $from_subquery, $from_cte ) = ( ' Join', ' Union', ' Subquery', ' Cte' ); ## | ||
| 443 | my $hidden = $db_string; | ||
| 444 | my $table_key; | ||
| 445 | if ( $sf->{redo_table} ) { | ||
| 446 | $table_key = delete $sf->{redo_table}; | ||
| 447 | } | ||
| 448 | else { | ||
| 449 | my @pre = ( $hidden, undef ); | ||
| 450 | my $menu_table; | ||
| 451 | if ( $sf->{o}{G}{metadata} ) { | ||
| 452 | my $sys_prefix = $is_system_schema ? '- ' : ' '; | ||
| 453 | $menu_table = [ @pre, map( "- $_", @$user_table_keys ), map( $sys_prefix . $_, @$sys_table_keys ) ]; | ||
| 454 | } | ||
| 455 | else { | ||
| 456 | $menu_table = [ @pre, map( "- $_", @$user_table_keys ) ]; | ||
| 457 | } | ||
| 458 | push @$menu_table, $from_subquery if $sf->{o}{enable}{m_derived}; | ||
| 459 | push @$menu_table, $from_cte if $sf->{o}{enable}{m_cte}; | ||
| 460 | push @$menu_table, $from_join if $sf->{o}{enable}{join}; | ||
| 461 | push @$menu_table, $from_union if $sf->{o}{enable}{union}; | ||
| 462 | my $back = $skipped_menus == 3 ? $sf->{i}{_quit} : $sf->{i}{_back}; | ||
| 463 | # Choose | ||
| 464 | my $idx_tbl = $tc->choose( | ||
| 465 | $menu_table, | ||
| 466 | { %{$sf->{i}{lyt_v}}, prompt => '', index => 1, default => $old_idx_tbl, undef => $back } | ||
| 467 | ); | ||
| 468 | if ( defined $idx_tbl ) { | ||
| 469 | $table_key = $menu_table->[$idx_tbl]; | ||
| 470 | } | ||
| 471 | if ( ! defined $table_key ) { | ||
| 472 | $sf->{d}{cte_history} = []; | ||
| 473 | next SCHEMA if @schemas > 1; | ||
| 474 | $dbh->disconnect(); | ||
| 475 | next DATABASE if @databases > 1; | ||
| 476 | next PLUGIN if @$available_plugins > 1; | ||
| 477 | last PLUGIN; | ||
| 478 | } | ||
| 479 | if ( $sf->{o}{G}{menu_memory} ) { | ||
| 480 | if ( $old_idx_tbl == $idx_tbl && ! $ENV{TC_RESET_AUTO_UP} ) { | ||
| 481 | $old_idx_tbl = 1; | ||
| 482 | next TABLE; | ||
| 483 | } | ||
| 484 | $old_idx_tbl = $idx_tbl; | ||
| 485 | } | ||
| 486 | } | ||
| 487 | if ( $table_key eq $hidden ) { | ||
| 488 | require App::DBBrowser::CreateDropAttach; | ||
| 489 | my $cda = App::DBBrowser::CreateDropAttach->new( $sf->{i}, $sf->{o}, $sf->{d} ); | ||
| 490 | my $ret = $cda->create_drop_or_attach(); | ||
| 491 | if ( ! $ret ) { | ||
| 492 | next TABLE; | ||
| 493 | } | ||
| 494 | elsif ( $ret == 1 ) { | ||
| 495 | # update the list of available tables | ||
| 496 | $sf->{redo_schema} = $schema; | ||
| 497 | $sf->{redo_is_system_schema} = $is_system_schema; | ||
| 498 | $sf->{redo_table} = $table_key; # stay in the $hidden submenu | ||
| 499 | next SCHEMA; | ||
| 500 | } | ||
| 501 | elsif ( $ret == 2 ) { | ||
| 502 | # attached/dedached databases and therefore redo attache database stmts | ||
| 503 | $sf->{redo_db} = $db; | ||
| 504 | $sf->{redo_is_system_db} = $is_system_db; | ||
| 505 | $sf->{redo_table} = $table_key; # stay in the $hidden submenu | ||
| 506 | next DATABASE; | ||
| 507 | } | ||
| 508 | elsif ( $ret == 3 ) { | ||
| 509 | # new db-settings and therefore reconnect to the database | ||
| 510 | $sf->{redo_db} = $db; | ||
| 511 | $sf->{redo_is_system_db} = $is_system_db; | ||
| 512 | $sf->{redo_schema} = $schema; | ||
| 513 | $sf->{redo_is_system_schema} = $is_system_schema; | ||
| 514 | $sf->{redo_table} = $table_key; # stay in the $hidden submenu | ||
| 515 | $dbh->disconnect(); # reconnects | ||
| 516 | next DATABASE; | ||
| 517 | } | ||
| 518 | } | ||
| 519 | $sf->{d}{default_table_alias_count} = 0; | ||
| 520 | $sf->{d}{table_aliases} = {}; | ||
| 521 | require App::DBBrowser::From; ## | ||
| 522 | my $fr = App::DBBrowser::From->new( $sf->{i}, $sf->{o}, $sf->{d} ); | ||
| 523 | my $sql = $fr->from_sql( $table_key =~ s/[-\ ]\ //r ); | ||
| 524 | if ( ! defined $sql ) { | ||
| 525 | next TABLE; | ||
| 526 | } | ||
| 527 | $ax->print_sql_info( $ax->get_sql_info( $sql ) ); ## | ||
| 528 | require App::DBBrowser::Table; | ||
| 529 | my $tbl = App::DBBrowser::Table->new( $sf->{i}, $sf->{o}, $sf->{d} ); | ||
| 530 | $tbl->browse_the_table( $sql ); | ||
| 531 | } | ||
| 532 | } | ||
| 533 | } | ||
| 534 | } | ||
| 535 | # END of App | ||
| 536 | } | ||
| 537 | |||
| 538 | |||
| 539 | |||
| 540 | 1; | ||
| 541 | |||
| 542 | |||
| 543 | __END__ |