| 1 | package # hide from PAUSE | ||
| 2 | App::DBBrowser::Table::Extensions; | ||
| 3 | |||
| 4 | use warnings; | ||
| 5 | use strict; | ||
| 6 | use 5.016; | ||
| 7 | |||
| 8 | use List::MoreUtils qw( uniq ); | ||
| 9 | |||
| 10 | use Term::Choose qw(); | ||
| 11 | use Term::Form::ReadLine qw(); | ||
| 12 | |||
| 13 | use App::DBBrowser::Auxil; | ||
| 14 | use App::DBBrowser::From::Subquery; | ||
| 15 | use App::DBBrowser::Table::Extensions::Maths; | ||
| 16 | use App::DBBrowser::Table::Extensions::Case; | ||
| 17 | use App::DBBrowser::Table::Extensions::ColAliases; | ||
| 18 | use App::DBBrowser::Table::Extensions::Columns; | ||
| 19 | use App::DBBrowser::Table::Extensions::RearrangeColumns; | ||
| 20 | use App::DBBrowser::Table::Extensions::ScalarFunctions; | ||
| 21 | use App::DBBrowser::Table::Extensions::WindowFunctions; | ||
| 22 | use App::DBBrowser::Table::Substatement::Aggregate; | ||
| 23 | |||
| 24 | |||
| 25 | my $e_const = 'Value'; | ||
| 26 | my $e_subquery = 'SQL'; | ||
| 27 | my $e_scalar_func = 'scalar()'; | ||
| 28 | my $e_window_func = 'win()'; | ||
| 29 | my $e_case = 'case'; | ||
| 30 | my $e_math = 'math'; | ||
| 31 | my $e_col = 'column'; | ||
| 32 | my $e_null = 'NULL'; | ||
| 33 | my $e_close_IN = ')end'; | ||
| 34 | my $e_par_open = '('; | ||
| 35 | my $e_par_close = ')'; | ||
| 36 | my $e_skip_col = ''; # no length | ||
| 37 | my $e_multi_col = 'mc'; ## | ||
| 38 | my $e_rearrange_cols = 'rac'; | ||
| 39 | my $e_col_aliases = 'alias'; | ||
| 40 | my $e_distinct = 'distinct'; | ||
| 41 | |||
| 42 | |||
| 43 | sub new { | ||
| 44 | my ( $class, $info, $options, $d ) = @_; | ||
| 45 | my $sf = { | ||
| 46 | i => $info, | ||
| 47 | o => $options, | ||
| 48 | d => $d | ||
| 49 | }; | ||
| 50 | bless $sf, $class; | ||
| 51 | } | ||
| 52 | |||
| 53 | |||
| 54 | sub column { | ||
| 55 | my ( $sf, $sql, $clause, $r_data, $opt ) = @_; | ||
| 56 | $r_data //= []; | ||
| 57 | my $extensions = []; | ||
| 58 | if ( $clause =~ /^(?:select|order_by)\z/i ) { | ||
| 59 | # Window functions are permitted only in SELECT and ORDER BY | ||
| 60 | $extensions = [ $e_subquery, $e_scalar_func, $e_window_func, $e_case, $e_math ]; | ||
| 61 | if ( $clause eq 'select' && ! @$r_data ) { | ||
| 62 | push @$extensions, $e_rearrange_cols; | ||
| 63 | push @$extensions, $e_col_aliases; | ||
| 64 | push @$extensions, $e_distinct; | ||
| 65 | } | ||
| 66 | } | ||
| 67 | else { | ||
| 68 | $extensions = [ $e_subquery, $e_scalar_func, $e_case, $e_math ]; | ||
| 69 | } | ||
| 70 | if ( $clause =~ /^where\z/i ) { | ||
| 71 | push @$extensions, $e_skip_col, $e_multi_col; | ||
| 72 | } | ||
| 73 | if ( $opt->{add_parentheses} ) { | ||
| 74 | push @$extensions, $e_par_open, $e_par_close; | ||
| 75 | delete $opt->{add_parentheses}; | ||
| 76 | } | ||
| 77 | if ( length $opt->{from} ) { | ||
| 78 | # no recursion: | ||
| 79 | if ( $opt->{from} eq 'window_function' ) { | ||
| 80 | $extensions = [ grep { ! /^\Q$e_window_func\E\z/ } @$extensions ]; | ||
| 81 | } | ||
| 82 | } | ||
| 83 | #my $last_idx = $#$r_data; | ||
| 84 | my $ext_column = $sf->__choose_extension( $sql, $clause, $r_data, 'column', $extensions, $opt ); | ||
| 85 | #die if $#$r_data != $last_idx; | ||
| 86 | return $ext_column; | ||
| 87 | } | ||
| 88 | |||
| 89 | |||
| 90 | sub value { | ||
| 91 | my ( $sf, $sql, $clause, $r_data, $operator, $opt ) = @_; | ||
| 92 | $r_data //= []; | ||
| 93 | my $ext_express = $sf->{o}{enable}{extended_values}; | ||
| 94 | my $extensions = []; | ||
| 95 | if ( $ext_express ) { | ||
| 96 | if ( $clause =~ /^(?:select|order_by)\z/i ) { | ||
| 97 | $extensions = [ $e_const, $e_subquery, $e_scalar_func, $e_window_func, $e_case, $e_math, $e_col ]; | ||
| 98 | } | ||
| 99 | else { | ||
| 100 | $extensions = [ $e_const, $e_subquery, $e_scalar_func, $e_case, $e_math, $e_col ]; | ||
| 101 | } | ||
| 102 | if ( $clause eq 'set' ) { | ||
| 103 | push @$extensions, $e_null; | ||
| 104 | } | ||
| 105 | if ( $operator =~ /\s(?:ALL|ANY)\z/ || $operator =~ /^(?:NOT )?EXISTS\z/ ) { | ||
| 106 | $extensions = [ $e_subquery ]; | ||
| 107 | } | ||
| 108 | elsif ( $operator =~ /^(?:NOT\s)?IN\z/ ) { | ||
| 109 | unshift @$extensions, $e_close_IN; | ||
| 110 | } | ||
| 111 | } | ||
| 112 | else { | ||
| 113 | if ( $operator =~ /\s(?:ALL|ANY)\z/ ) { | ||
| 114 | $extensions = [ $e_subquery ]; | ||
| 115 | } | ||
| 116 | else { | ||
| 117 | $extensions = [ $e_const ]; | ||
| 118 | } | ||
| 119 | } | ||
| 120 | #my $last_idx = $#$r_data; | ||
| 121 | my $ext_value = $sf->__choose_extension( $sql, $clause, $r_data, 'value', $extensions, $opt ); | ||
| 122 | #die if $#$r_data != $last_idx; | ||
| 123 | return $ext_value; | ||
| 124 | } | ||
| 125 | |||
| 126 | |||
| 127 | sub argument { | ||
| 128 | my ( $sf, $sql, $clause, $r_data, $opt ) = @_; | ||
| 129 | $r_data //= []; | ||
| 130 | my $ext_express = $sf->{o}{enable}{extended_args}; | ||
| 131 | my $extensions = []; | ||
| 132 | if ( $ext_express ) { | ||
| 133 | $extensions = [ $e_const, $e_subquery, $e_scalar_func, $e_case, $e_math, $e_col ]; | ||
| 134 | } | ||
| 135 | else { | ||
| 136 | $extensions = [ $e_const ]; | ||
| 137 | } | ||
| 138 | #my $last_idx = $#$r_data; | ||
| 139 | my $ext_argument = $sf->__choose_extension( $sql, $clause, $r_data, 'argument', $extensions, $opt ); | ||
| 140 | #die if $#$r_data != $last_idx; | ||
| 141 | return $ext_argument; | ||
| 142 | } | ||
| 143 | |||
| 144 | |||
| 145 | sub enable_extended_arguments { | ||
| 146 | my ( $sf, $info ) = @_; | ||
| 147 | my $tc = Term::Choose->new( $sf->{i}{tc_default} ); | ||
| 148 | my $prompt = 'Extended arguments:'; | ||
| 149 | my $yes = '- YES'; | ||
| 150 | # Choose | ||
| 151 | my $choice = $tc->choose( | ||
| 152 | [ undef, '- NO', $yes ], | ||
| 153 | { %{$sf->{i}{lyt_v}}, info => $info, prompt => $prompt, undef => '<=' } | ||
| 154 | ); | ||
| 155 | if ( ! defined $choice ) { | ||
| 156 | return; | ||
| 157 | } | ||
| 158 | if ( $choice eq $yes ) { | ||
| 159 | $sf->{o}{enable}{extended_args} = 1; | ||
| 160 | } | ||
| 161 | else { | ||
| 162 | $sf->{o}{enable}{extended_args} = 0; | ||
| 163 | } | ||
| 164 | } | ||
| 165 | |||
| 166 | |||
| 167 | sub __avail_cols_in_extenstions { | ||
| 168 | my ( $sf, $sql, $clause, $extension ) = @_; | ||
| 169 | my $cols; | ||
| 170 | # in functions cols are used without aliases | ||
| 171 | if ( $clause eq 'on' ) { | ||
| 172 | $cols = [ @{$sql->{cols_join_condition}} ]; | ||
| 173 | } | ||
| 174 | elsif ( $sql->{aggregate_mode} ) { | ||
| 175 | my $sa = App::DBBrowser::Table::Substatement::Aggregate->new( $sf->{i}, $sf->{o}, $sf->{d} ); | ||
| 176 | my $avail_aggr = $sa->available_aggregate_functions(); | ||
| 177 | if ( $clause =~ /^(?:select|having|order_by)\z/ && ! $sql->{used_in_aggregate_function} ) { | ||
| 178 | $cols = [ uniq( @{$sql->{group_by_cols}}, @$avail_aggr, @{$sql->{selected_cols}} ) ]; | ||
| 179 | } | ||
| 180 | else { | ||
| 181 | $cols = [ @{$sql->{columns}} ]; | ||
| 182 | } | ||
| 183 | } | ||
| 184 | else { | ||
| 185 | my @selected_cols; | ||
| 186 | if ( $extension eq $e_window_func || $clause eq 'where' ) { | ||
| 187 | @selected_cols = grep { ! /\)\s*OVER\s*\(/i } @{$sql->{selected_cols}}; | ||
| 188 | } | ||
| 189 | else { | ||
| 190 | @selected_cols = @{$sql->{selected_cols}}; | ||
| 191 | } | ||
| 192 | if ( $clause =~ /^(?:select|where|order_by)\z/ ) { | ||
| 193 | $cols = [ uniq( @{$sql->{columns}}, @selected_cols ) ]; | ||
| 194 | } | ||
| 195 | else { | ||
| 196 | $cols = [ @{$sql->{columns}} ]; | ||
| 197 | } | ||
| 198 | } | ||
| 199 | return $cols; | ||
| 200 | } | ||
| 201 | |||
| 202 | |||
| 203 | sub __choose_extension { | ||
| 204 | my ( $sf, $sql, $clause, $r_data, $caller, $extensions, $opt ) = @_; | ||
| 205 | my $ax = App::DBBrowser::Auxil->new( $sf->{i}, $sf->{o}, $sf->{d} ); | ||
| 206 | my $tr = Term::Form::ReadLine->new( $sf->{i}{tr_default} ); | ||
| 207 | my $tc = Term::Choose->new( $sf->{i}{tc_default} ); | ||
| 208 | my $tu = Term::Choose::Util->new( $sf->{i}{tcu_default} ); | ||
| 209 | my $old_idx = 0; | ||
| 210 | |||
| 211 | EXTENSION: while ( 1 ) { | ||
| 212 | my $info = $ax->get_sql_info( $sql ) . $sf->nested_func_info( $r_data ); | ||
| 213 | my $extension; | ||
| 214 | if ( @$extensions == 1 && ( | ||
| 215 | ( $caller eq 'value' && ! $sf->{o}{enable}{extended_values} ) | ||
| 216 | || ( $caller eq 'argument' && ! $sf->{o}{enable}{extended_args} ) | ||
| 217 | ) ) { | ||
| 218 | $extension = $extensions->[0]; | ||
| 219 | } | ||
| 220 | else { | ||
| 221 | my $empty; | ||
| 222 | if ( $caller eq 'column' && $clause eq 'where' ) { | ||
| 223 | $empty = 'skip'; ## | ||
| 224 | } | ||
| 225 | my @pre = ( undef ); | ||
| 226 | # Choose | ||
| 227 | my $idx = $tc->choose( | ||
| 228 | [ @pre, @$extensions ], | ||
| 229 | { %{$sf->{i}{lyt_h}}, info => $info, index => 1, default => $old_idx, | ||
| 230 | prompt => $opt->{prompt}, undef => '<<', empty => $empty } | ||
| 231 | ); | ||
| 232 | $ax->print_sql_info( $info ); | ||
| 233 | if ( ! $idx ) { ## | ||
| 234 | return; | ||
| 235 | } | ||
| 236 | if ( $sf->{o}{G}{menu_memory} ) { | ||
| 237 | if ( $old_idx == $idx && ! $ENV{TC_RESET_AUTO_UP} ) { | ||
| 238 | $old_idx = 0; | ||
| 239 | next EXTENSION; | ||
| 240 | } | ||
| 241 | $old_idx = $idx; | ||
| 242 | } | ||
| 243 | $extension = $extensions->[$idx-@pre]; | ||
| 244 | } | ||
| 245 | my $cols = $sf->__avail_cols_in_extenstions( $sql, $clause, $extension ); | ||
| 246 | if ( $extension eq $e_const ) { | ||
| 247 | my $prompt = $opt->{prompt} // 'Value: '; | ||
| 248 | # Readline | ||
| 249 | my $value = $tr->readline( | ||
| 250 | $prompt, | ||
| 251 | { info => $info, history => $opt->{history} } | ||
| 252 | ); | ||
| 253 | $ax->print_sql_info( $info ); | ||
| 254 | if ( ! defined $value ) { | ||
| 255 | return if @$extensions = 1; | ||
| 256 | next EXTENSION; | ||
| 257 | } | ||
| 258 | # return if ! length $value; ## | ||
| 259 | if ( $opt->{is_numeric} ) { | ||
| 260 | if ( $opt->{is_numeric} == -1 ) { | ||
| 261 | # -1: unknown data type | ||
| 262 | return $ax->quote_if_not_numeric( $value ); | ||
| 263 | } | ||
| 264 | else { | ||
| 265 | #return $value; # then change the documentation | ||
| 266 | return $ax->quote_if_not_numeric( $value ); | ||
| 267 | } | ||
| 268 | } | ||
| 269 | else { | ||
| 270 | return $sf->{d}{dbh}->quote( $value ); | ||
| 271 | } | ||
| 272 | } | ||
| 273 | elsif ( $extension eq $e_subquery ) { | ||
| 274 | my $new_sq = App::DBBrowser::From::Subquery->new( $sf->{i}, $sf->{o}, $sf->{d} ); | ||
| 275 | my $subq = $new_sq->subquery( $sql, $info ); | ||
| 276 | if ( ! defined $subq ) { | ||
| 277 | return if @$extensions == 1; | ||
| 278 | next EXTENSION; | ||
| 279 | } | ||
| 280 | return $subq; | ||
| 281 | } | ||
| 282 | elsif ( $extension eq $e_scalar_func ) { | ||
| 283 | my $new_func = App::DBBrowser::Table::Extensions::ScalarFunctions->new( $sf->{i}, $sf->{o}, $sf->{d} ); | ||
| 284 | my $scalar_func_stmt = $new_func->scalar_function( $sql, $clause, $cols, $r_data ); # recursion yes | ||
| 285 | if ( ! defined $scalar_func_stmt ) { | ||
| 286 | return if @$extensions == 1; | ||
| 287 | next EXTENSION; | ||
| 288 | } | ||
| 289 | return $scalar_func_stmt; | ||
| 290 | } | ||
| 291 | elsif ( $extension eq $e_window_func ) { | ||
| 292 | my $wf = App::DBBrowser::Table::Extensions::WindowFunctions->new( $sf->{i}, $sf->{o}, $sf->{d} ); | ||
| 293 | my $win_func_stmt = $wf->window_function( $sql, $clause, $cols, $r_data ); | ||
| 294 | if ( ! defined $win_func_stmt ) { | ||
| 295 | return if @$extensions == 1; | ||
| 296 | next EXTENSION; | ||
| 297 | } | ||
| 298 | return $win_func_stmt; | ||
| 299 | } | ||
| 300 | elsif ( $extension eq $e_case ) { | ||
| 301 | my $new_cs = App::DBBrowser::Table::Extensions::Case->new( $sf->{i}, $sf->{o}, $sf->{d} ); | ||
| 302 | my $case_stmt = $new_cs->case( $sql, $clause, $cols, $r_data ); # recursion yes | ||
| 303 | if ( ! defined $case_stmt ) { | ||
| 304 | return if @$extensions == 1; | ||
| 305 | next EXTENSION; | ||
| 306 | } | ||
| 307 | return $case_stmt; | ||
| 308 | } | ||
| 309 | elsif ( $extension eq $e_math ) { | ||
| 310 | my $new_math = App::DBBrowser::Table::Extensions::Maths->new( $sf->{i}, $sf->{o}, $sf->{d} ); | ||
| 311 | my $arith = $new_math->maths( $sql, $clause, $cols, $r_data ); # recursion yes | ||
| 312 | if ( ! defined $arith ) { | ||
| 313 | return if @$extensions == 1; | ||
| 314 | next EXTENSION; | ||
| 315 | } | ||
| 316 | return $arith; | ||
| 317 | } | ||
| 318 | elsif ( $extension eq $e_col ) { | ||
| 319 | my $new_col = App::DBBrowser::Table::Extensions::Columns->new( $sf->{i}, $sf->{o}, $sf->{d} ); | ||
| 320 | my $col = $new_col->columns( $sql, $cols, $info ); | ||
| 321 | if ( ! defined $col ) { | ||
| 322 | return if @$extensions == 1; | ||
| 323 | next EXTENSION; | ||
| 324 | } | ||
| 325 | return $col; | ||
| 326 | } | ||
| 327 | elsif ( $extension eq $e_null ) { | ||
| 328 | return "NULL"; | ||
| 329 | } | ||
| 330 | elsif ( $extension eq $e_close_IN ) { | ||
| 331 | return "''"; | ||
| 332 | } | ||
| 333 | elsif ( $extension eq $e_par_open ) { | ||
| 334 | return "("; | ||
| 335 | } | ||
| 336 | elsif ( $extension eq $e_par_close ) { | ||
| 337 | return ")"; | ||
| 338 | } | ||
| 339 | elsif ( $extension eq $e_skip_col ) { | ||
| 340 | return ''; | ||
| 341 | } | ||
| 342 | elsif ( $extension eq $e_rearrange_cols ) { | ||
| 343 | my $new_rc = App::DBBrowser::Table::Extensions::RearrangeColumns->new( $sf->{i}, $sf->{o}, $sf->{d} ); | ||
| 344 | my $ok = $new_rc->rearrange_columns( $sql ); | ||
| 345 | if ( ! defined $ok ) { | ||
| 346 | return if @$extensions == 1; | ||
| 347 | next EXTENSION; | ||
| 348 | } | ||
| 349 | return { rearrange => 1 }; | ||
| 350 | } | ||
| 351 | elsif ( $extension eq $e_col_aliases ) { | ||
| 352 | my $new_ca = App::DBBrowser::Table::Extensions::ColAliases->new( $sf->{i}, $sf->{o}, $sf->{d} ); | ||
| 353 | my $col_aliases = $new_ca->column_aliases( $sql ); | ||
| 354 | if ( ! defined $col_aliases ) { | ||
| 355 | return if @$extensions == 1; | ||
| 356 | next EXTENSION; | ||
| 357 | } | ||
| 358 | return { alias => $col_aliases }; | ||
| 359 | } | ||
| 360 | elsif ( $extension eq $e_distinct ) { | ||
| 361 | return { distinct => 1 } | ||
| 362 | } | ||
| 363 | elsif ( $extension eq $e_multi_col ) { | ||
| 364 | # Choose | ||
| 365 | my $chosen_cols = $tu->choose_a_subset( | ||
| 366 | $cols, | ||
| 367 | { info => $info, prompt => '', layout => 1, index => 0, all_by_default => 0, | ||
| 368 | cs_label => 'Columns: ', cs_begin => '(', cs_separator => ',', cs_end => ')' } | ||
| 369 | ); | ||
| 370 | $ax->print_sql_info( $info ); | ||
| 371 | if ( ! defined $chosen_cols ) { | ||
| 372 | return if @$extensions == 1; | ||
| 373 | next EXTENSION; | ||
| 374 | } | ||
| 375 | return "(" . join( ",", @$chosen_cols ) . ")"; | ||
| 376 | } | ||
| 377 | } | ||
| 378 | } | ||
| 379 | |||
| 380 | |||
| 381 | sub nested_func_info { | ||
| 382 | my ( $sf, $r_data ) = @_; | ||
| 383 | if ( ! @$r_data ) { | ||
| 384 | return ''; | ||
| 385 | } | ||
| 386 | my @copy = map { [ @$_ ] } @$r_data; | ||
| 387 | my ( $scalar, $win, $math, $aggr, $case ) = qw(scalar win math aggr case); | ||
| 388 | my $last_type = ''; | ||
| 389 | my $case_count = 0; | ||
| 390 | my @close; | ||
| 391 | my $info = "\n"; | ||
| 392 | for my $i ( 0 .. $#copy ) { | ||
| 393 | my $func_info = $copy[$i]; | ||
| 394 | my $type = shift @$func_info; | ||
| 395 | if ( $type eq $scalar ) { | ||
| 396 | my $func = shift @$func_info; | ||
| 397 | $info .= "$func(" . join ',', @$func_info; | ||
| 398 | if ( $func =~ /^(?:CONCAT|COALESCE)/i && @$func_info && $i != $#copy ) { ## | ||
| 399 | $info .= ','; | ||
| 400 | } | ||
| 401 | push @close, ')'; | ||
| 402 | } | ||
| 403 | elsif ( $type eq $win ) { | ||
| 404 | if ( @$func_info ) { | ||
| 405 | $info .= shift @$func_info; | ||
| 406 | push @close, shift @$func_info; | ||
| 407 | } | ||
| 408 | } | ||
| 409 | elsif ( $type eq $math ) { | ||
| 410 | $info .= "(" . join '', @$func_info; | ||
| 411 | push @close, ')'; | ||
| 412 | } | ||
| 413 | elsif ( $type eq $aggr ) { | ||
| 414 | my ( $aggr_func, $distinct ) = @$func_info; | ||
| 415 | if ( $aggr_func ) { | ||
| 416 | $info .= $aggr_func . ( $distinct ? ' ' . $distinct : '' ); | ||
| 417 | push @close, ')'; | ||
| 418 | } | ||
| 419 | } | ||
| 420 | elsif ( $type eq $case ) { | ||
| 421 | my $new_cs = App::DBBrowser::Table::Extensions::Case->new( $sf->{i}, $sf->{o}, $sf->{d} ); | ||
| 422 | my $case_stmt = $new_cs->format_case( $func_info, $case_count ); | ||
| 423 | $case_stmt =~ s/^\s+// if $info =~ /\(\z/; | ||
| 424 | $info .= $case_stmt . ' '; | ||
| 425 | } | ||
| 426 | $last_type = $type; | ||
| 427 | $case_count = $type eq $case ? ++$case_count : 0; | ||
| 428 | } | ||
| 429 | return '' if $info eq "\n"; | ||
| 430 | $info =~ s/\s\z// if $info =~ /\s\z/; | ||
| 431 | $info .= ' ... '; | ||
| 432 | $info .= join '', reverse @close; | ||
| 433 | $info .= "\n" if $last_type eq $case; | ||
| 434 | return $info; | ||
| 435 | } | ||
| 436 | |||
| 437 | |||
| 438 | |||
| 439 | 1; | ||
| 440 | |||
| 441 | __END__ |