| 1 | package # hide from PAUSE | ||
| 2 | App::DBBrowser::CreateDropAttach::CreateTable; | ||
| 3 | |||
| 4 | use warnings; | ||
| 5 | use strict; | ||
| 6 | use 5.016; | ||
| 7 | |||
| 8 | use Encode qw( decode ); | ||
| 9 | use File::Basename qw( basename ); | ||
| 10 | |||
| 11 | use List::MoreUtils qw( none any duplicates ); | ||
| 12 | #use SQL::Type::Guess qw(); # required | ||
| 13 | |||
| 14 | use Term::Choose qw(); | ||
| 15 | use Term::Choose::Util qw( insert_sep ); | ||
| 16 | use Term::Form qw(); | ||
| 17 | use Term::Form::ReadLine qw(); | ||
| 18 | |||
| 19 | use App::DBBrowser::Auxil; | ||
| 20 | use App::DBBrowser::GetContent; | ||
| 21 | use App::DBBrowser::Options; | ||
| 22 | use App::DBBrowser::Options::ReadWrite; | ||
| 23 | use App::DBBrowser::From::Subquery; | ||
| 24 | use App::DBBrowser::Table::CommitWriteSQL; | ||
| 25 | |||
| 26 | |||
| 27 | sub new { | ||
| 28 | my ( $class, $info, $options, $d ) = @_; | ||
| 29 | bless { | ||
| 30 | i => $info, | ||
| 31 | o => $options, | ||
| 32 | d => $d | ||
| 33 | }, $class; | ||
| 34 | } | ||
| 35 | |||
| 36 | |||
| 37 | sub create_view { | ||
| 38 | my ( $sf ) = @_; | ||
| 39 | my $ax = App::DBBrowser::Auxil->new( $sf->{i}, $sf->{o}, $sf->{d} ); | ||
| 40 | my $sq = App::DBBrowser::From::Subquery->new( $sf->{i}, $sf->{o}, $sf->{d} ); | ||
| 41 | my $tc = Term::Choose->new( $sf->{i}{tc_default} ); | ||
| 42 | my $tr = Term::Form::ReadLine->new( $sf->{i}{tr_default} ); | ||
| 43 | my $sql = {}; | ||
| 44 | $ax->reset_sql( $sql ); | ||
| 45 | $sf->{d}{stmt_types} = [ 'Create_View' ]; | ||
| 46 | |||
| 47 | SELECT_STMT: while ( 1 ) { | ||
| 48 | $sql->{table} = ''; | ||
| 49 | $sql->{view_select_stmt} = '?'; | ||
| 50 | my $select_statment = $sq->subquery( $sql ); | ||
| 51 | $ax->print_sql_info( $ax->get_sql_info( $sql ) ); | ||
| 52 | if ( ! defined $select_statment ) { | ||
| 53 | return; | ||
| 54 | } | ||
| 55 | $sql->{view_select_stmt} = $select_statment =~ s/^\((.+)\)\z/$1/r; | ||
| 56 | |||
| 57 | VIEW_NAME: while ( 1 ) { | ||
| 58 | $sql->{table} = '?'; | ||
| 59 | my $info = $ax->get_sql_info( $sql ); | ||
| 60 | # Readline | ||
| 61 | my $view = $tr->readline( | ||
| 62 | 'View name: ' . $sf->{o}{create}{view_name_prefix}, | ||
| 63 | { info => $info, history => [] } | ||
| 64 | ); | ||
| 65 | $ax->print_sql_info( $info ); | ||
| 66 | if ( ! length $view ) { | ||
| 67 | next SELECT_STMT; | ||
| 68 | } | ||
| 69 | $view = $sf->{o}{create}{view_name_prefix} . $view; | ||
| 70 | $sql->{table} = $ax->qq_table( [ undef, $sf->{d}{schema}, $view, 'VIEW' ] ); | ||
| 71 | if ( none { $sql->{table} eq $ax->qq_table( $sf->{d}{tables_info}{$_} ) } keys %{$sf->{d}{tables_info}} ) { | ||
| 72 | my $ok_create_view = $sf->__create( $sql, 'view' ); | ||
| 73 | if ( ! defined $ok_create_view ) { | ||
| 74 | next SELECT_STMT; | ||
| 75 | } | ||
| 76 | elsif( ! $ok_create_view ) { | ||
| 77 | return; | ||
| 78 | } | ||
| 79 | return 1; | ||
| 80 | } | ||
| 81 | my $prompt = "$sql->{table} already exists."; | ||
| 82 | $info = $ax->get_sql_info( $sql ); | ||
| 83 | my $chosen = $tc->choose( | ||
| 84 | [ undef, ' New name' ], | ||
| 85 | { %{$sf->{i}{lyt_v}}, info => $info, prompt => $prompt } | ||
| 86 | ); | ||
| 87 | $ax->print_sql_info( $info ); | ||
| 88 | if ( ! defined $chosen ) { | ||
| 89 | return; | ||
| 90 | } | ||
| 91 | next VIEW_NAME; | ||
| 92 | } | ||
| 93 | } | ||
| 94 | } | ||
| 95 | |||
| 96 | |||
| 97 | sub create_table { | ||
| 98 | my ( $sf ) = @_; | ||
| 99 | my $ax = App::DBBrowser::Auxil->new( $sf->{i}, $sf->{o}, $sf->{d} ); | ||
| 100 | my $gc = App::DBBrowser::GetContent->new( $sf->{i}, $sf->{o}, $sf->{d} ); | ||
| 101 | my $tc = Term::Choose->new( $sf->{i}{tc_default} ); | ||
| 102 | my $sql = {}; | ||
| 103 | $sql->{table} = ''; | ||
| 104 | $ax->reset_sql( $sql ); | ||
| 105 | my $count_table_name_loop = 0; | ||
| 106 | my $goto_filter = 0; | ||
| 107 | my $source = {}; | ||
| 108 | |||
| 109 | GET_CONTENT: while ( 1 ) { | ||
| 110 | $sf->{d}{stmt_types} = [ 'Create_Table', 'Insert' ]; | ||
| 111 | # first use of {stmt_types} in get_content/from_col_by_col | ||
| 112 | my $ok = $gc->get_content( $sql, $source, $goto_filter ); | ||
| 113 | if ( ! $ok ) { | ||
| 114 | return; | ||
| 115 | } | ||
| 116 | if ( ! @{$sql->{insert_args}[0]} ) { | ||
| 117 | # Choose | ||
| 118 | $tc->choose( | ||
| 119 | [ 'No columns!' ], | ||
| 120 | { prompt => 'Continue with ENTER', keep => 1 } | ||
| 121 | ); | ||
| 122 | return; | ||
| 123 | } | ||
| 124 | my $tablename_default = ''; | ||
| 125 | |||
| 126 | GET_TABLE_NAME: while ( 1 ) { | ||
| 127 | $tablename_default = $sf->__set_table_name( $sql, $source, $tablename_default, $count_table_name_loop ); # first time print_sql_info | ||
| 128 | if ( ! $tablename_default ) { | ||
| 129 | $count_table_name_loop = 0; | ||
| 130 | $goto_filter = 1; | ||
| 131 | next GET_CONTENT; | ||
| 132 | } | ||
| 133 | my $bu_first_row = [ @{$sql->{insert_args}[0]} ]; | ||
| 134 | my $orig_row_count = @{$sql->{insert_args}}; | ||
| 135 | |||
| 136 | GET_COLUMN_NAMES: while ( 1 ) { | ||
| 137 | if ( $orig_row_count - 1 == @{$sql->{insert_args}} ) { | ||
| 138 | unshift @{$sql->{insert_args}}, $bu_first_row; | ||
| 139 | } | ||
| 140 | $sql->{ct_column_definitions} = []; ## | ||
| 141 | $sql->{insert_col_names} = []; | ||
| 142 | my $header_row = $sf->__get_column_names( $sql ); | ||
| 143 | if ( ! defined $header_row ) { | ||
| 144 | $sql->{table} = ''; | ||
| 145 | $count_table_name_loop++; | ||
| 146 | next GET_TABLE_NAME; | ||
| 147 | } | ||
| 148 | if ( ! @{$sql->{insert_args}} ) { | ||
| 149 | $sf->{d}{stmt_types} = [ 'Create_Table' ]; | ||
| 150 | } | ||
| 151 | $count_table_name_loop = 0; | ||
| 152 | |||
| 153 | AUTO_INCREMENT: while( 1 ) { | ||
| 154 | $sql->{ct_column_definitions} = [ @$header_row ]; # not quoted | ||
| 155 | $sql->{insert_col_names} = [ @$header_row ]; # not quoted | ||
| 156 | $sf->{auto_increment} = 0; | ||
| 157 | if ( $sf->{o}{create}{option_ai_column_enabled} && $sf->__primary_key_autoincrement_constraint() ) { | ||
| 158 | my $return = $sf->__autoincrement_column( $sql); | ||
| 159 | if ( ! defined $return ) { | ||
| 160 | next GET_COLUMN_NAMES; | ||
| 161 | } | ||
| 162 | } | ||
| 163 | my @unquoted_ct_column_definitions = @{$sql->{ct_column_definitions}}; | ||
| 164 | my @unquoted_insert_col_names = @{$sql->{insert_col_names}}; | ||
| 165 | my $column_names = [ @{$sql->{ct_column_definitions}} ]; # column_names memory | ||
| 166 | |||
| 167 | EDIT_COLUMN_NAMES: while( 1 ) { | ||
| 168 | $column_names = $sf->__edit_column_names( $sql, $column_names ); # now quoted | ||
| 169 | if ( ! defined $column_names ) { | ||
| 170 | next AUTO_INCREMENT if $sf->{auto_increment}; | ||
| 171 | next GET_COLUMN_NAMES; | ||
| 172 | } | ||
| 173 | if ( any { ! length } @{$sql->{ct_column_definitions}} ) { | ||
| 174 | # Choose | ||
| 175 | $tc->choose( | ||
| 176 | [ 'Column with no name!' ], | ||
| 177 | { prompt => 'Continue with ENTER', keep => 1 } | ||
| 178 | ); | ||
| 179 | next EDIT_COLUMN_NAMES; | ||
| 180 | } | ||
| 181 | my @duplicates = duplicates map { lc } @{$sql->{ct_column_definitions}}; | ||
| 182 | # my @duplicates; ## | ||
| 183 | # if ( $sf->{o}{G}{quote_columns} ) { | ||
| 184 | # @duplicates = duplicates @{$sql->{ct_column_definitions}}; | ||
| 185 | # } | ||
| 186 | # else { | ||
| 187 | # @duplicates = duplicates map { lc } @{$sql->{ct_column_definitions}}; | ||
| 188 | # } | ||
| 189 | if ( @duplicates ) { | ||
| 190 | # Choose | ||
| 191 | $tc->choose( | ||
| 192 | [ 'Duplicate column name!' ], | ||
| 193 | { prompt => 'Continue with ENTER', keep => 1 } | ||
| 194 | ); | ||
| 195 | next EDIT_COLUMN_NAMES; | ||
| 196 | } | ||
| 197 | my @unedited_ct_column_definitions = @{$sql->{ct_column_definitions}}; | ||
| 198 | my $data_types = {}; # data_types memory | ||
| 199 | |||
| 200 | EDIT_COLUMN_TYPES: while( 1 ) { | ||
| 201 | $data_types = $sf->__edit_column_types( $sql, $source, $data_types ); | ||
| 202 | if ( ! $data_types ) { | ||
| 203 | $sql->{ct_column_definitions} = [ @unquoted_ct_column_definitions ]; | ||
| 204 | $sql->{insert_col_names} = [ @unquoted_insert_col_names ]; | ||
| 205 | next EDIT_COLUMN_NAMES; | ||
| 206 | } | ||
| 207 | # CREATE_TABLE | ||
| 208 | my $ok_create_table = $sf->__create( $sql, 'table' ); | ||
| 209 | if ( ! defined $ok_create_table ) { | ||
| 210 | $sql->{ct_column_definitions} = [ @unedited_ct_column_definitions ]; | ||
| 211 | next EDIT_COLUMN_TYPES; | ||
| 212 | } | ||
| 213 | elsif ( ! $ok_create_table ) { | ||
| 214 | return; | ||
| 215 | } | ||
| 216 | elsif ( @{$sql->{insert_args}} ) { | ||
| 217 | # INSERT_DATA | ||
| 218 | my $ok_insert = $sf->__insert_data( $sql ); | ||
| 219 | if ( ! $ok_insert ) { | ||
| 220 | return; | ||
| 221 | } | ||
| 222 | } | ||
| 223 | return 1; | ||
| 224 | } | ||
| 225 | } | ||
| 226 | } | ||
| 227 | } | ||
| 228 | } | ||
| 229 | } | ||
| 230 | } | ||
| 231 | |||
| 232 | |||
| 233 | sub __set_table_name { | ||
| 234 | my ( $sf, $sql, $source, $tablename_default, $count_table_name_loop ) = @_; | ||
| 235 | my $ax = App::DBBrowser::Auxil->new( $sf->{i}, $sf->{o}, $sf->{d} ); | ||
| 236 | my $tc = Term::Choose->new( $sf->{i}{tc_default} ); | ||
| 237 | my $tr = Term::Form::ReadLine->new( $sf->{i}{tr_default} ); | ||
| 238 | $ax->print_sql_info( $ax->get_sql_info( $sql ) ); | ||
| 239 | |||
| 240 | while ( 1 ) { | ||
| 241 | my $file_info; | ||
| 242 | if ( $source->{source_type} eq 'file' ) { | ||
| 243 | my $file_fs = $source->{file_fs}; | ||
| 244 | my $file_name = basename decode( 'locale_fs', $file_fs ); | ||
| 245 | $file_info = sprintf "File: '%s'", $file_name; | ||
| 246 | if ( ! length $tablename_default ) { | ||
| 247 | if ( length $source->{sheet_name} ) { | ||
| 248 | $file_name =~ s/\.[^.]{1,4}\z//; | ||
| 249 | $tablename_default = $file_name . '_' . $source->{sheet_name}; | ||
| 250 | } | ||
| 251 | else { | ||
| 252 | $tablename_default = $file_name =~ s/\.[^.]{1,4}\z//r; | ||
| 253 | } | ||
| 254 | $tablename_default =~ s/ /_/g; | ||
| 255 | } | ||
| 256 | } | ||
| 257 | if ( $count_table_name_loop > 1 ) { # to avoid infinite loop when going back with `ENTER` | ||
| 258 | $tablename_default = ''; | ||
| 259 | } | ||
| 260 | my $info = $ax->get_sql_info( $sql ) . ( $file_info ? "\n" . $file_info : '' ); | ||
| 261 | # Readline | ||
| 262 | my $table_name = $tr->readline( | ||
| 263 | 'Table name: ', | ||
| 264 | { info => $info, default => $tablename_default, history => [] } | ||
| 265 | ); | ||
| 266 | $ax->print_sql_info( $info ); | ||
| 267 | if ( ! length $table_name ) { | ||
| 268 | return; | ||
| 269 | } | ||
| 270 | $sql->{table} = $ax->qq_table( [ undef, $sf->{d}{schema}, $table_name ] ); | ||
| 271 | if ( none { $sql->{table} eq $ax->qq_table( $sf->{d}{tables_info}{$_} ) } keys %{$sf->{d}{tables_info}} ) { | ||
| 272 | return $table_name; | ||
| 273 | } | ||
| 274 | my $prompt = "Table $sql->{table} already exists."; | ||
| 275 | my $menu = [ undef, ' New name' ]; | ||
| 276 | $info = $ax->get_sql_info( $sql ); | ||
| 277 | # Choose | ||
| 278 | my $chosen = $tc->choose( | ||
| 279 | $menu, | ||
| 280 | { %{$sf->{i}{lyt_v}}, info => $info, prompt => $prompt, keep => scalar( @$menu ) } | ||
| 281 | ); | ||
| 282 | $ax->print_sql_info( $info ); | ||
| 283 | if ( ! defined $chosen ) { | ||
| 284 | return; | ||
| 285 | } | ||
| 286 | $tablename_default = $tablename_default ? $table_name : ''; | ||
| 287 | $count_table_name_loop++; | ||
| 288 | } | ||
| 289 | } | ||
| 290 | |||
| 291 | |||
| 292 | sub __get_column_names { | ||
| 293 | my ( $sf, $sql ) = @_; | ||
| 294 | my $tc = Term::Choose->new( $sf->{i}{tc_default} ); | ||
| 295 | my $ax = App::DBBrowser::Auxil->new( $sf->{i}, $sf->{o}, $sf->{d} ); | ||
| 296 | my ( $first_row, $user_input ) = ( '- YES', '- NO' ); | ||
| 297 | my $hidden = 'Use the first Data Row as the Table Header:'; | ||
| 298 | my @pre = ( $hidden, undef ); | ||
| 299 | my $menu = [ @pre, $first_row, $user_input ]; | ||
| 300 | |||
| 301 | while ( 1 ) { | ||
| 302 | my $info = $ax->get_sql_info( $sql ); | ||
| 303 | # Choose | ||
| 304 | my $idx = $tc->choose( | ||
| 305 | $menu, | ||
| 306 | { %{$sf->{i}{lyt_v}}, info => $info, prompt => '', index => 1, default => 1, undef => ' <=', | ||
| 307 | keep => scalar( @$menu ) } | ||
| 308 | ); | ||
| 309 | $ax->print_sql_info( $info ); | ||
| 310 | if ( ! defined $idx || ! defined $menu->[$idx] ) { | ||
| 311 | return; | ||
| 312 | } | ||
| 313 | my $header_row; | ||
| 314 | if ( $menu->[$idx] eq $hidden ) { | ||
| 315 | my $op = App::DBBrowser::Options->new( $sf->{i}, $sf->{o} ); | ||
| 316 | my $op_rw = App::DBBrowser::Options::ReadWrite->new( $sf->{i}, $sf->{o} ); | ||
| 317 | $op->config_groups( [ { name => 'group_create_table', text => "- Create Table" } ], $sf->{i}{plugin} ); | ||
| 318 | $op_rw->read_config_file( $sf->{i}{driver}, $sf->{i}{plugin} ); | ||
| 319 | next; | ||
| 320 | } | ||
| 321 | elsif ( $menu->[$idx] eq $first_row ) { | ||
| 322 | $header_row = shift @{$sql->{insert_args}}; | ||
| 323 | $header_row = [ map { defined $_ ? "$_" : '' } @$header_row ]; | ||
| 324 | } | ||
| 325 | else { | ||
| 326 | $header_row = [ ( '' ) x @{$sql->{insert_args}[0]} ]; | ||
| 327 | } | ||
| 328 | return $header_row; | ||
| 329 | } | ||
| 330 | } | ||
| 331 | |||
| 332 | |||
| 333 | sub __autoincrement_column { | ||
| 334 | my ( $sf, $sql ) = @_; | ||
| 335 | my $tc = Term::Choose->new( $sf->{i}{tc_default} ); | ||
| 336 | my $ax = App::DBBrowser::Auxil->new( $sf->{i}, $sf->{o}, $sf->{d} ); | ||
| 337 | my ( $no, $yes ) = ( '- NO ', '- YES' ); | ||
| 338 | my $menu = [ undef, $yes, $no ]; | ||
| 339 | my $info = $ax->get_sql_info( $sql ); | ||
| 340 | # Choose | ||
| 341 | my $chosen = $tc->choose( | ||
| 342 | $menu, | ||
| 343 | { %{$sf->{i}{lyt_v}}, info => $info, prompt => 'Add an AUTO INCREMENT column:', undef => ' <=', | ||
| 344 | keep => scalar( @$menu ) } | ||
| 345 | ); | ||
| 346 | $ax->print_sql_info( $info ); | ||
| 347 | if ( ! defined $chosen ) { | ||
| 348 | return; | ||
| 349 | } | ||
| 350 | elsif ( $chosen eq $no ) { | ||
| 351 | $sf->{auto_increment} = 0; | ||
| 352 | } | ||
| 353 | else { | ||
| 354 | my $auto_increment_column_name = $sf->{o}{create}{default_ai_column_name} // 'Id'; ## | ||
| 355 | unshift @{$sql->{ct_column_definitions}}, $auto_increment_column_name; | ||
| 356 | $sf->{auto_increment} = 1; | ||
| 357 | } | ||
| 358 | return 1; | ||
| 359 | } | ||
| 360 | |||
| 361 | |||
| 362 | sub __primary_key_autoincrement_constraint { | ||
| 363 | # provide "primary_key_autoincrement_constraint" only if also "first_col_is_autoincrement" in InsertUpdateDelete.pm is available | ||
| 364 | my ( $sf ) = @_; | ||
| 365 | my $ax = App::DBBrowser::Auxil->new( $sf->{i}, $sf->{o}, $sf->{d} ); | ||
| 366 | my $dbms = $sf->{i}{dbms}; | ||
| 367 | if ( $dbms eq 'SQLite' ) { | ||
| 368 | return "INTEGER PRIMARY KEY"; | ||
| 369 | } | ||
| 370 | if ( $dbms =~ /^(?:mysql|MariaDB)\z/ ) { | ||
| 371 | return "INT NOT NULL AUTO_INCREMENT PRIMARY KEY"; | ||
| 372 | # mysql: NOT NULL added automatically with AUTO_INCREMENT | ||
| 373 | } | ||
| 374 | if ( $dbms eq 'Pg' ) { | ||
| 375 | my $pg_major_version = $ax->major_server_version(); | ||
| 376 | if ( $pg_major_version >= 10 ) { | ||
| 377 | # since PostgreSQL version 10 | ||
| 378 | return "INT GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY"; | ||
| 379 | } | ||
| 380 | else { | ||
| 381 | return "SERIAL PRIMARY KEY"; | ||
| 382 | } | ||
| 383 | } | ||
| 384 | if ( $dbms eq 'Firebird' ) { | ||
| 385 | my $firebird_major_version = $ax->major_server_version(); | ||
| 386 | if ( $firebird_major_version >= 4 ) { | ||
| 387 | return "INT GENERATED ALWAYS AS IDENTITY PRIMARY KEY"; | ||
| 388 | } | ||
| 389 | elsif ( $firebird_major_version >= 3 ) { | ||
| 390 | return "INT GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY"; | ||
| 391 | } | ||
| 392 | } | ||
| 393 | if ( $dbms eq 'DB2' ) { | ||
| 394 | return "INT NOT NULL GENERATED ALWAYS AS IDENTITY PRIMARY KEY"; | ||
| 395 | } | ||
| 396 | if ( $dbms eq 'Oracle' ) { | ||
| 397 | my $ora_major_server_version = $ax->major_server_version(); | ||
| 398 | if ( $ora_major_server_version >= 12 ) { | ||
| 399 | return "NUMBER GENERATED ALWAYS AS IDENTITY"; # Oracle 12c or greater | ||
| 400 | } | ||
| 401 | } | ||
| 402 | if ( $dbms eq 'MSSQL' ) { | ||
| 403 | return "INT IDENTITY(1,1) PRIMARY KEY"; | ||
| 404 | } | ||
| 405 | } | ||
| 406 | |||
| 407 | |||
| 408 | sub __edit_column_names { | ||
| 409 | my ( $sf, $sql, $column_names ) = @_; | ||
| 410 | my $tf = Term::Form->new( $sf->{i}{tf_default} ); | ||
| 411 | my $ax = App::DBBrowser::Auxil->new( $sf->{i}, $sf->{o}, $sf->{d} ); | ||
| 412 | my $col_number = 0; | ||
| 413 | my $fields = [ map { [ ++$col_number, $_ ] } @$column_names ]; | ||
| 414 | my $info = $ax->get_sql_info( $sql ); | ||
| 415 | # Fill_form | ||
| 416 | my $form = $tf->fill_form( | ||
| 417 | $fields, | ||
| 418 | { info => $info, prompt => 'Edit column names:', confirm => $sf->{i}{confirm}, back => $sf->{i}{back} . ' ' } | ||
| 419 | ); | ||
| 420 | $ax->print_sql_info( $info ); | ||
| 421 | if ( ! defined $form ) { | ||
| 422 | return; | ||
| 423 | } | ||
| 424 | $column_names = [ map { $_->[1] } @$form ]; | ||
| 425 | $sql->{ct_column_definitions} = $ax->quote_cols( $column_names ); # now quoted | ||
| 426 | $sql->{insert_col_names} = [ @{$sql->{ct_column_definitions}} ]; # now quoted | ||
| 427 | if ( $sf->{auto_increment} ) { | ||
| 428 | shift @{$sql->{insert_col_names}}; | ||
| 429 | } | ||
| 430 | $sql->{ct_table_constraints} = []; | ||
| 431 | $sql->{ct_table_options} = []; | ||
| 432 | return $column_names; | ||
| 433 | } | ||
| 434 | |||
| 435 | |||
| 436 | sub __edit_column_types { | ||
| 437 | my ( $sf, $sql, $source, $data_types ) = @_; | ||
| 438 | my $tf = Term::Form->new( $sf->{i}{tf_default} ); | ||
| 439 | my $ax = App::DBBrowser::Auxil->new( $sf->{i}, $sf->{o}, $sf->{d} ); | ||
| 440 | my $dbms = $sf->{i}{dbms}; | ||
| 441 | my $fields; | ||
| 442 | if ( ! %$data_types && $sf->{o}{create}{data_type_guessing} ) { | ||
| 443 | $ax->print_sql_info( $ax->get_sql_info( $sql ), 'Column data types: guessing ... ' ); | ||
| 444 | my $header = $sql->{insert_col_names}; # | ||
| 445 | my $table = $sql->{insert_args}; | ||
| 446 | my $encoding; | ||
| 447 | if ( $sf->{o}{create}{encode_for_data_type_guessing} ) { # MSSQL DB2 Oracle | ||
| 448 | $encoding = $source->{source_type} eq 'plain' ? 'locale' : $sf->{o}{insert}{file_encoding}; | ||
| 449 | } | ||
| 450 | my @aoh; | ||
| 451 | if ( length $encoding ) { | ||
| 452 | for my $row ( @$table ) { | ||
| 453 | push @aoh, { map { $header->[$_] => Encode::encode( $encoding, $row->[$_] ) } 0 .. $#$header }; | ||
| 454 | } | ||
| 455 | } | ||
| 456 | else { | ||
| 457 | for my $row ( @$table ) { | ||
| 458 | push @aoh, { map { $header->[$_] => $row->[$_] } 0 .. $#$header }; | ||
| 459 | } | ||
| 460 | } | ||
| 461 | require SQL::Type::Guess; | ||
| 462 | my $g = SQL::Type::Guess->new(); | ||
| 463 | $g->guess( @aoh ); | ||
| 464 | my $tmp = $g->column_type; | ||
| 465 | $data_types = { map { $_ => uc( $tmp->{$_} ) } keys %$tmp }; | ||
| 466 | } | ||
| 467 | if ( defined $data_types ) { | ||
| 468 | $fields = [ map { [ $_, $data_types->{$_} ] } @{$sql->{insert_col_names}} ]; | ||
| 469 | } | ||
| 470 | else { | ||
| 471 | $fields = [ map { [ $_, '' ] } @{$sql->{insert_col_names}} ]; | ||
| 472 | } | ||
| 473 | my $read_only = []; ## | ||
| 474 | if ( $sf->{auto_increment} ) { | ||
| 475 | unshift @$fields, [ $sql->{ct_column_definitions}[0], $sf->__primary_key_autoincrement_constraint() ]; | ||
| 476 | $read_only = [ 0 ]; | ||
| 477 | } | ||
| 478 | if ( $dbms =~ /^(?:Pg|DuckDB|Firebird|Informix|Oracle)\z/ ) { | ||
| 479 | for my $field ( @$fields ) { | ||
| 480 | if ( defined $field->[1] && $field->[1] eq 'DATETIME' ) { | ||
| 481 | $field->[1] = 'TIMESTAMP' if $dbms =~ /^(?:Pg|DuckDB|Firebird)\z/; | ||
| 482 | $field->[1] = 'DATE' if $dbms eq 'Oracle'; | ||
| 483 | $field->[1] = 'DATETIME YEAR TO FRACTION' if $dbms eq 'Informix'; | ||
| 484 | # Informix: DATETIME largest_qualifier TO smallest_qualifier | ||
| 485 | } | ||
| 486 | } | ||
| 487 | } | ||
| 488 | my $constraint_rows = $sf->{o}{create}{table_constraint_rows}; | ||
| 489 | my $tbl_option_rows = $sf->{o}{create}{table_option_rows}; | ||
| 490 | my $skip = ' '; | ||
| 491 | if ( $constraint_rows ) { | ||
| 492 | push @$fields, [ $skip ]; | ||
| 493 | for my $i ( 0 .. $constraint_rows - 1 ) { | ||
| 494 | push @$fields, [ 'Constraint', $sql->{ct_table_constraints}[$i] // '' ]; | ||
| 495 | } | ||
| 496 | } | ||
| 497 | if ( $tbl_option_rows ) { | ||
| 498 | push @$fields, [ $skip ]; | ||
| 499 | for my $i ( 0 .. $tbl_option_rows - 1 ) { | ||
| 500 | push @$fields, [ 'Tbl Option', $sql->{ct_table_options}[$i] // '' ]; | ||
| 501 | } | ||
| 502 | } | ||
| 503 | my $info = $ax->get_sql_info( $sql ); | ||
| 504 | # Fill_form | ||
| 505 | my $filled_form = $tf->fill_form( | ||
| 506 | $fields, | ||
| 507 | { info => $info, prompt => 'Column data types:', read_only => $read_only, | ||
| 508 | confirm => $sf->{i}{confirm}, back => $sf->{i}{back} . ' ' } | ||
| 509 | ); | ||
| 510 | $ax->print_sql_info( $info ); | ||
| 511 | if ( ! $filled_form ) { | ||
| 512 | return; | ||
| 513 | } | ||
| 514 | if ( $tbl_option_rows ) { | ||
| 515 | $sql->{ct_table_options} = [ | ||
| 516 | grep { length } map { $_->[1] } splice @$filled_form, -$tbl_option_rows, $tbl_option_rows | ||
| 517 | ]; | ||
| 518 | pop @$filled_form; | ||
| 519 | } | ||
| 520 | if ( $constraint_rows ) { | ||
| 521 | $sql->{ct_table_constraints} = [ | ||
| 522 | grep { length } map { $_->[1] } splice @$filled_form, -$constraint_rows, $constraint_rows | ||
| 523 | ]; | ||
| 524 | pop @$filled_form; | ||
| 525 | } | ||
| 526 | $sql->{ct_column_definitions} = [ | ||
| 527 | map { length $_->[1] ? join ' ', @$_ : $_->[0] } @$filled_form | ||
| 528 | ]; | ||
| 529 | $data_types = { map { $_->[0] => $_->[1] } @$filled_form }; | ||
| 530 | return $data_types; | ||
| 531 | } | ||
| 532 | |||
| 533 | |||
| 534 | sub __create { | ||
| 535 | my ( $sf, $sql, $type ) = @_; | ||
| 536 | my $ax = App::DBBrowser::Auxil->new( $sf->{i}, $sf->{o}, $sf->{d} ); | ||
| 537 | my $tc = Term::Choose->new( $sf->{i}{tc_default} ); | ||
| 538 | my ( $no, $yes ) = ( '- NO', '- YES' ); | ||
| 539 | my $menu = [ undef, $yes, $no ]; ## | ||
| 540 | my $prompt = "Create $type $sql->{table}"; | ||
| 541 | if ( @{$sql->{insert_args}} ) { | ||
| 542 | my $row_count = @{$sql->{insert_args}}; | ||
| 543 | $prompt .= "\nInsert " . insert_sep( $row_count, $sf->{i}{info_thsd_sep} ) . " row"; | ||
| 544 | if ( @{$sql->{insert_args}} > 1 ) { | ||
| 545 | $prompt .= "s"; | ||
| 546 | } | ||
| 547 | } | ||
| 548 | my $info = $ax->get_sql_info( $sql ); | ||
| 549 | # Choose | ||
| 550 | my $create_table_ok = $tc->choose( | ||
| 551 | $menu, | ||
| 552 | { %{$sf->{i}{lyt_v}}, info => $info, prompt => $prompt, undef => ' <=', keep => scalar( @$menu ) } | ||
| 553 | ); | ||
| 554 | $ax->print_sql_info( $info ); | ||
| 555 | if ( ! defined $create_table_ok ) { | ||
| 556 | return; | ||
| 557 | } | ||
| 558 | if ( $create_table_ok eq $no ) { | ||
| 559 | return 0; | ||
| 560 | } | ||
| 561 | my $stmt = $ax->get_stmt( $sql, 'Create_' . ucfirst( $type ), 'prepare' ); | ||
| 562 | # don't reset `$sql->{ct_column_definitions}` and `$sf->{d}{stmt_types}`: | ||
| 563 | # to get a consistent print_sql_info output in CommitSQL | ||
| 564 | # to avoid another confirmation prompt in CommitSQL | ||
| 565 | if ( ! eval { $sf->{d}{dbh}->do( $stmt ); 1 } ) { | ||
| 566 | $ax->print_error_message( $@ ); | ||
| 567 | return; | ||
| 568 | }; | ||
| 569 | return 1; | ||
| 570 | } | ||
| 571 | |||
| 572 | |||
| 573 | sub __insert_data { | ||
| 574 | my ( $sf, $sql ) = @_; | ||
| 575 | my $cs = App::DBBrowser::Table::CommitWriteSQL->new( $sf->{i}, $sf->{o}, $sf->{d} ); | ||
| 576 | my $commit_ok = $cs->commit_sql( $sql ); | ||
| 577 | return $commit_ok; | ||
| 578 | } | ||
| 579 | |||
| 580 | |||
| 581 | |||
| 582 | 1; | ||
| 583 | |||
| 584 | __END__ |