| 1 | package # hide from PAUSE | ||
| 2 | App::DBBrowser::Table::Substatement; | ||
| 3 | |||
| 4 | use warnings; | ||
| 5 | use strict; | ||
| 6 | use 5.016; | ||
| 7 | |||
| 8 | use List::MoreUtils qw( any uniq ); | ||
| 9 | |||
| 10 | use Term::Choose qw(); | ||
| 11 | use Term::Choose::Util qw(); | ||
| 12 | use Term::Form::ReadLine qw(); | ||
| 13 | |||
| 14 | use App::DBBrowser::Auxil; | ||
| 15 | use App::DBBrowser::Table::Extensions; | ||
| 16 | use App::DBBrowser::Table::Substatement::Aggregate; | ||
| 17 | use App::DBBrowser::Table::Substatement::Condition; | ||
| 18 | |||
| 19 | |||
| 20 | sub new { | ||
| 21 | my ( $class, $info, $options, $d ) = @_; | ||
| 22 | bless { | ||
| 23 | i => $info, | ||
| 24 | o => $options, | ||
| 25 | d => $d | ||
| 26 | }, $class; | ||
| 27 | } | ||
| 28 | |||
| 29 | |||
| 30 | sub __avail_cols { | ||
| 31 | my ( $sf, $sql, $clause ) = @_; | ||
| 32 | my $sa = App::DBBrowser::Table::Substatement::Aggregate->new( $sf->{i}, $sf->{o}, $sf->{d} ); | ||
| 33 | my $avail_aggr = $sa->available_aggregate_functions(); | ||
| 34 | my $aliases = $sf->{o}{alias}{'use_in_' . $clause} ? $sql->{alias} : {}; | ||
| 35 | my $cols = []; | ||
| 36 | if ( $sql->{aggregate_mode} ) { | ||
| 37 | if ( $clause eq 'select' ) { | ||
| 38 | $cols = [ @{$sql->{group_by_cols}}, @$avail_aggr ]; | ||
| 39 | } | ||
| 40 | elsif ( $clause eq 'where' ) { | ||
| 41 | $cols = [ @{$sql->{columns}} ]; | ||
| 42 | } | ||
| 43 | elsif ( $clause eq 'group_by' ) { | ||
| 44 | $cols = [ @{$sql->{columns}} ]; | ||
| 45 | # Aliases added later to the group_by_stmt and not to the group_by_cols. | ||
| 46 | } | ||
| 47 | elsif ( $clause eq 'having' || $clause eq 'order_by') { | ||
| 48 | $cols = [ | ||
| 49 | map { length $aliases->{$_} ? $aliases->{$_} : $_ } | ||
| 50 | uniq @{$sql->{selected_cols}}, @{$sql->{group_by_cols}} | ||
| 51 | ]; | ||
| 52 | $cols = [ reverse uniq( reverse @$cols, @$avail_aggr ) ]; | ||
| 53 | } | ||
| 54 | } | ||
| 55 | else { | ||
| 56 | if ( $clause eq 'select' ) { | ||
| 57 | $cols = [ @{$sql->{columns}} ]; | ||
| 58 | } | ||
| 59 | elsif ( $clause eq 'where' ) { | ||
| 60 | my @selected_cols = grep { ! /\)\s*OVER\s*\(/i } @{$sql->{selected_cols}}; | ||
| 61 | $cols = [ reverse uniq( reverse @selected_cols, @{$sql->{columns}} ) ]; | ||
| 62 | } | ||
| 63 | elsif ( $clause eq 'group_by' ) { | ||
| 64 | $cols = [ @{$sql->{columns}} ]; | ||
| 65 | } | ||
| 66 | elsif ( $clause eq 'having' ) { | ||
| 67 | $cols = [ @$avail_aggr ]; | ||
| 68 | } | ||
| 69 | elsif ( $clause eq 'order_by' ) { | ||
| 70 | my @selected = map { length $aliases->{$_} ? $aliases->{$_} : $_ } @{$sql->{selected_cols}}; | ||
| 71 | $cols = [ reverse uniq( reverse @selected, @{$sql->{columns}} ) ]; | ||
| 72 | } | ||
| 73 | } | ||
| 74 | return $cols; | ||
| 75 | } | ||
| 76 | |||
| 77 | |||
| 78 | sub select { | ||
| 79 | my ( $sf, $sql ) = @_; | ||
| 80 | my $clause = 'select'; | ||
| 81 | my $ax = App::DBBrowser::Auxil->new( $sf->{i}, $sf->{o}, $sf->{d} ); | ||
| 82 | my $tc = Term::Choose->new( $sf->{i}{tc_default} ); | ||
| 83 | my @pre = ( undef, $sf->{i}{ok} ); | ||
| 84 | my $add_distinct = 'd*'; | ||
| 85 | if ( $sf->{o}{enable}{extended_cols} ) { | ||
| 86 | push @pre, $sf->{i}{menu_addition}; | ||
| 87 | } | ||
| 88 | else { | ||
| 89 | push @pre, $add_distinct; | ||
| 90 | } | ||
| 91 | my $cols = $sf->__avail_cols( $sql, $clause ); | ||
| 92 | my $menu = [ @pre, @$cols ]; | ||
| 93 | |||
| 94 | COLUMNS: while ( 1 ) { | ||
| 95 | my $info = $ax->get_sql_info( $sql ); | ||
| 96 | # Choose | ||
| 97 | my @idx = $tc->choose( | ||
| 98 | $menu, | ||
| 99 | { %{$sf->{i}{lyt_h}}, info => $info, meta_items => [ 0 .. $#pre - 1 ], no_spacebar => [ $#pre ], | ||
| 100 | include_highlighted => 2, index => 1 } | ||
| 101 | ); | ||
| 102 | $ax->print_sql_info( $info ); | ||
| 103 | if ( ! $idx[0] ) { | ||
| 104 | if ( @{$sql->{selected_cols}} ) { | ||
| 105 | pop @{$sql->{selected_cols}}; | ||
| 106 | next COLUMNS; | ||
| 107 | } | ||
| 108 | if ( $sql->{aggregate_mode} ) { | ||
| 109 | $ax->reset_sql( $sql ); | ||
| 110 | } | ||
| 111 | else { | ||
| 112 | $sql->{alias} = {}; | ||
| 113 | } | ||
| 114 | return; | ||
| 115 | } | ||
| 116 | if ( $menu->[$idx[0]] eq $sf->{i}{ok} ) { | ||
| 117 | shift @idx; | ||
| 118 | if ( @idx ) { | ||
| 119 | $sf->__add_chosen_cols( $sql, $clause, [ @{$menu}[@idx] ] ); | ||
| 120 | } | ||
| 121 | if ( ! @{$sql->{selected_cols}} && $sql->{aggregate_mode} ) { ## | ||
| 122 | return; | ||
| 123 | } | ||
| 124 | $sql->{alias} = { | ||
| 125 | map { $_ => $sql->{alias}{$_} } grep { length $sql->{alias}{$_} } @{$sql->{selected_cols}} | ||
| 126 | }; | ||
| 127 | return 1; | ||
| 128 | } | ||
| 129 | elsif ( $menu->[$idx[0]] eq $add_distinct ) { | ||
| 130 | $sf->__distinct( $sql ); | ||
| 131 | } | ||
| 132 | elsif ( $menu->[$idx[0]] eq $sf->{i}{menu_addition} ) { | ||
| 133 | my $ext = App::DBBrowser::Table::Extensions->new( $sf->{i}, $sf->{o}, $sf->{d} ); | ||
| 134 | my $complex_col = $ext->column( $sql, $clause ); | ||
| 135 | if ( ! defined $complex_col ) { | ||
| 136 | next COLUMNS | ||
| 137 | } | ||
| 138 | elsif ( ref( $complex_col ) eq 'HASH' ) { | ||
| 139 | if ( exists $complex_col->{alias} ) { | ||
| 140 | $sql->{alias} = $complex_col->{alias} ; | ||
| 141 | } | ||
| 142 | elsif ( exists $complex_col->{distinct} ) { | ||
| 143 | $sf->__distinct( $sql ); | ||
| 144 | } | ||
| 145 | #elsif ( exists $complex_col->{rearrange} ) { | ||
| 146 | #} | ||
| 147 | } | ||
| 148 | else { | ||
| 149 | my $alias = $ax->alias( $sql, 'complex_cols_select', $complex_col ); | ||
| 150 | if ( length $alias ) { | ||
| 151 | $sql->{alias}{$complex_col} = $alias; | ||
| 152 | } | ||
| 153 | push @{$sql->{selected_cols}}, $complex_col; | ||
| 154 | } | ||
| 155 | } | ||
| 156 | else { | ||
| 157 | $sf->__add_chosen_cols( $sql, $clause, [ @{$menu}[@idx] ] ); | ||
| 158 | } | ||
| 159 | } | ||
| 160 | } | ||
| 161 | |||
| 162 | |||
| 163 | sub __add_chosen_cols { | ||
| 164 | my ( $sf, $sql, $clause, $chosen_cols ) = @_; | ||
| 165 | my $sa = App::DBBrowser::Table::Substatement::Aggregate->new( $sf->{i}, $sf->{o}, $sf->{d} ); | ||
| 166 | if ( $sql->{aggregate_mode} ) { | ||
| 167 | my $ax = App::DBBrowser::Auxil->new( $sf->{i}, $sf->{o}, $sf->{d} ); | ||
| 168 | my $rx_groub_by_cols = join '|', map { quotemeta } @{$sql->{group_by_cols}}; | ||
| 169 | |||
| 170 | for my $aggr ( @$chosen_cols ) { | ||
| 171 | my $prepared_aggr = $sa->get_prepared_aggr_func( $sql, $clause, $aggr ); | ||
| 172 | if ( ! length $prepared_aggr ) { | ||
| 173 | next; | ||
| 174 | } | ||
| 175 | if ( $prepared_aggr !~ /^(?:$rx_groub_by_cols)\z/ ) { | ||
| 176 | my $alias = $ax->alias( $sql, 'complex_cols_select', $prepared_aggr ); | ||
| 177 | if ( length $alias ) { | ||
| 178 | $sql->{alias}{$prepared_aggr} = $alias; | ||
| 179 | } | ||
| 180 | } | ||
| 181 | push @{$sql->{selected_cols}}, $prepared_aggr; | ||
| 182 | } | ||
| 183 | } | ||
| 184 | else { | ||
| 185 | push @{$sql->{selected_cols}}, @$chosen_cols; | ||
| 186 | } | ||
| 187 | } | ||
| 188 | |||
| 189 | |||
| 190 | sub __distinct { | ||
| 191 | my ( $sf, $sql ) = @_; | ||
| 192 | my $ax = App::DBBrowser::Auxil->new( $sf->{i}, $sf->{o}, $sf->{d} ); | ||
| 193 | my $tc = Term::Choose->new( $sf->{i}{tc_default} ); | ||
| 194 | my @pre = ( undef ); | ||
| 195 | my $yes = 'Yes'; | ||
| 196 | my $menu = [ @pre, $yes, 'No' ]; | ||
| 197 | my $info = $ax->get_sql_info( $sql ); | ||
| 198 | # Choose | ||
| 199 | my $chosen_distinct = $tc->choose( | ||
| 200 | $menu, | ||
| 201 | { %{$sf->{i}{lyt_h}}, info => $info, prompt => 'Select DISTINCT:' } | ||
| 202 | ); | ||
| 203 | $ax->print_sql_info( $info ); | ||
| 204 | if ( ! defined $chosen_distinct ) { | ||
| 205 | return; | ||
| 206 | } | ||
| 207 | elsif ( $chosen_distinct eq $yes ) { | ||
| 208 | $sql->{distinct_stmt} = " DISTINCT"; | ||
| 209 | } | ||
| 210 | else { | ||
| 211 | $sql->{distinct_stmt} = ''; | ||
| 212 | } | ||
| 213 | } | ||
| 214 | |||
| 215 | |||
| 216 | sub set { | ||
| 217 | my ( $sf, $sql ) = @_; | ||
| 218 | my $clause = 'set'; | ||
| 219 | my $stmt = 'set_stmt'; | ||
| 220 | my $ax = App::DBBrowser::Auxil->new( $sf->{i}, $sf->{o}, $sf->{d} ); | ||
| 221 | my $sc = App::DBBrowser::Table::Substatement::Condition->new( $sf->{i}, $sf->{o}, $sf->{d} ); | ||
| 222 | my $tc = Term::Choose->new( $sf->{i}{tc_default} ); | ||
| 223 | $sql->{$stmt} = "SET"; | ||
| 224 | my @bu; | ||
| 225 | my @pre = ( undef, $sf->{i}{ok} ); | ||
| 226 | |||
| 227 | COL: while ( 1 ) { | ||
| 228 | my $info = $ax->get_sql_info( $sql ); | ||
| 229 | # Choose | ||
| 230 | my $col = $tc->choose( | ||
| 231 | [ @pre, @{$sql->{columns}} ], | ||
| 232 | { %{$sf->{i}{lyt_h}}, info => $info } | ||
| 233 | ); | ||
| 234 | $ax->print_sql_info( $info ); | ||
| 235 | if ( ! defined $col ) { | ||
| 236 | if ( @bu ) { | ||
| 237 | $sql->{$stmt} = pop @bu; | ||
| 238 | next COL; | ||
| 239 | } | ||
| 240 | $sql->{$stmt} = ''; | ||
| 241 | return; | ||
| 242 | } | ||
| 243 | if ( $col eq $sf->{i}{ok} ) { | ||
| 244 | if ( ! @bu ) { | ||
| 245 | $sql->{$stmt} = ''; | ||
| 246 | } | ||
| 247 | return 1; | ||
| 248 | } | ||
| 249 | push @bu, $sql->{$stmt}; | ||
| 250 | my $op = '='; | ||
| 251 | if ( @bu == 1 ) { | ||
| 252 | $sql->{$stmt} .= ' ' . $col . ' ' . $op; | ||
| 253 | } | ||
| 254 | else { | ||
| 255 | $sql->{$stmt} .= ', ' . $col . ' ' . $op; | ||
| 256 | } | ||
| 257 | my $ok = $sc->read_and_add_value( $sql, $clause, $stmt, $col, $op ); | ||
| 258 | if ( ! $ok ) { | ||
| 259 | $sql->{$stmt} = pop @bu; | ||
| 260 | next COL; | ||
| 261 | } | ||
| 262 | } | ||
| 263 | } | ||
| 264 | |||
| 265 | |||
| 266 | sub where { | ||
| 267 | my ( $sf, $sql ) = @_; | ||
| 268 | my $clause = 'where'; | ||
| 269 | my $sc = App::DBBrowser::Table::Substatement::Condition->new( $sf->{i}, $sf->{o}, $sf->{d} ); | ||
| 270 | my $cols = $sf->__avail_cols( $sql, $clause ); | ||
| 271 | my $ret = $sc->add_condition( $sql, $clause, $cols ); | ||
| 272 | return $ret; | ||
| 273 | } | ||
| 274 | |||
| 275 | |||
| 276 | |||
| 277 | sub group_by { | ||
| 278 | my ( $sf, $sql ) = @_; | ||
| 279 | my $clause = 'group_by'; | ||
| 280 | my $ax = App::DBBrowser::Auxil->new( $sf->{i}, $sf->{o}, $sf->{d} ); | ||
| 281 | my $tc = Term::Choose->new( $sf->{i}{tc_default} ); | ||
| 282 | my $bu_selected_cols = [ @{$sql->{selected_cols}} ]; | ||
| 283 | my $bu_order_by_stmt = $sql->{order_by_stmt}; | ||
| 284 | my $bu_aggregate_mode = $sql->{aggregate_mode} // 0; | ||
| 285 | if ( ! $bu_aggregate_mode ) { | ||
| 286 | $sql->{order_by_stmt} = ''; | ||
| 287 | $sql->{selected_cols} = []; | ||
| 288 | } | ||
| 289 | $sql->{aggregate_mode} = 1; | ||
| 290 | my @pre = ( undef, $sf->{i}{ok} ); | ||
| 291 | if ( $sf->{o}{enable}{extended_cols} ) { | ||
| 292 | push @pre, $sf->{i}{menu_addition}; | ||
| 293 | } | ||
| 294 | my $cols = $sf->__avail_cols( $sql, $clause ); | ||
| 295 | my $menu = [ @pre, @$cols ]; | ||
| 296 | |||
| 297 | GROUP_BY: while ( 1 ) { | ||
| 298 | @{$sql->{selected_cols}} = uniq @{$sql->{group_by_cols}}, @{$sql->{selected_cols}}; | ||
| 299 | my $info = $ax->get_sql_info( $sql ); | ||
| 300 | # Choose | ||
| 301 | my @idx = $tc->choose( | ||
| 302 | $menu, | ||
| 303 | { %{$sf->{i}{lyt_h}}, info => $info, meta_items => [ 0 .. $#pre - 1 ], no_spacebar => [ $#pre ], | ||
| 304 | include_highlighted => 2, index => 1 } | ||
| 305 | ); | ||
| 306 | $ax->print_sql_info( $info ); | ||
| 307 | if ( ! $idx[0] ) { | ||
| 308 | if ( @{$sql->{group_by_cols}} ) { | ||
| 309 | my $removed_col = pop @{$sql->{group_by_cols}}; | ||
| 310 | @{$sql->{selected_cols}} = grep { ! /^\Q$removed_col\E\z/ } @{$sql->{selected_cols}}; | ||
| 311 | next GROUP_BY; | ||
| 312 | } | ||
| 313 | if ( ! $bu_aggregate_mode ) { | ||
| 314 | $sql->{selected_cols} = [ @$bu_selected_cols ]; | ||
| 315 | $sql->{order_by_stmt} = $bu_order_by_stmt; | ||
| 316 | #$sql->{having_stmt} = ''; | ||
| 317 | $sql->{aggregate_mode} = 0; | ||
| 318 | } | ||
| 319 | elsif ( ! @{$sql->{selected_cols}} ) { | ||
| 320 | $sql->{aggregate_mode} = 0; | ||
| 321 | } | ||
| 322 | return; | ||
| 323 | } | ||
| 324 | if ( $menu->[$idx[0]] eq $sf->{i}{ok} ) { | ||
| 325 | shift @idx; | ||
| 326 | push @{$sql->{group_by_cols}}, @{$menu}[@idx]; | ||
| 327 | if ( @{$sql->{group_by_cols}} ) { | ||
| 328 | @{$sql->{selected_cols}} = uniq @{$sql->{group_by_cols}}, @{$sql->{selected_cols}}; | ||
| 329 | } | ||
| 330 | return 1; | ||
| 331 | } | ||
| 332 | elsif ( $menu->[$idx[0]] eq $sf->{i}{menu_addition} ) { | ||
| 333 | my $ext = App::DBBrowser::Table::Extensions->new( $sf->{i}, $sf->{o}, $sf->{d} ); | ||
| 334 | my $complex_col = $ext->column( $sql, $clause ); | ||
| 335 | if ( defined $complex_col ) { | ||
| 336 | my $alias = $ax->alias( $sql, 'complex_cols_select', $complex_col ); | ||
| 337 | # this alias is used in select | ||
| 338 | if ( length $alias ) { | ||
| 339 | $sql->{alias}{$complex_col} = $alias; | ||
| 340 | } | ||
| 341 | push @{$sql->{group_by_cols}}, $complex_col; | ||
| 342 | } | ||
| 343 | next GROUP_BY; | ||
| 344 | } | ||
| 345 | push @{$sql->{group_by_cols}}, @{$menu}[@idx]; | ||
| 346 | } | ||
| 347 | } | ||
| 348 | |||
| 349 | |||
| 350 | sub having { | ||
| 351 | my ( $sf, $sql ) = @_; | ||
| 352 | my $clause = 'having'; | ||
| 353 | my $sc = App::DBBrowser::Table::Substatement::Condition->new( $sf->{i}, $sf->{o}, $sf->{d} ); | ||
| 354 | my $cols = $sf->__avail_cols( $sql, $clause ); | ||
| 355 | my $ret = $sc->add_condition( $sql, $clause, $cols ); | ||
| 356 | return $ret; | ||
| 357 | } | ||
| 358 | |||
| 359 | |||
| 360 | sub order_by { | ||
| 361 | my ( $sf, $sql ) = @_; | ||
| 362 | my $clause = 'order_by'; | ||
| 363 | my $ax = App::DBBrowser::Auxil->new( $sf->{i}, $sf->{o}, $sf->{d} ); | ||
| 364 | my $tc = Term::Choose->new( $sf->{i}{tc_default} ); | ||
| 365 | my @pre = ( undef, $sf->{i}{ok} ); | ||
| 366 | if ( $sf->{o}{enable}{extended_cols} ) { | ||
| 367 | push @pre, $sf->{i}{menu_addition}; | ||
| 368 | } | ||
| 369 | my $cols = $sf->__avail_cols( $sql, $clause ); | ||
| 370 | |||
| 371 | ORDER_BY: while ( 1 ) { | ||
| 372 | $sql->{order_by_stmt} = "ORDER BY " . join ',', @{$sql->{order_by_cols}}; | ||
| 373 | my $info = $ax->get_sql_info( $sql ); | ||
| 374 | # Choose | ||
| 375 | my $col = $tc->choose( | ||
| 376 | [ @pre, @$cols ], | ||
| 377 | { %{$sf->{i}{lyt_h}}, info => $info } | ||
| 378 | ); | ||
| 379 | $ax->print_sql_info( $info ); | ||
| 380 | if ( ! defined $col ) { | ||
| 381 | if ( @{$sql->{order_by_cols}} ) { | ||
| 382 | pop @{$sql->{order_by_cols}}; | ||
| 383 | next ORDER_BY; | ||
| 384 | } | ||
| 385 | $sql->{order_by_stmt} = ''; | ||
| 386 | return | ||
| 387 | } | ||
| 388 | if ( $col eq $sf->{i}{ok} ) { | ||
| 389 | if ( ! @{$sql->{order_by_cols}} ) { | ||
| 390 | $sql->{order_by_stmt} = ''; | ||
| 391 | } | ||
| 392 | return 1; | ||
| 393 | } | ||
| 394 | elsif ( $col eq $sf->{i}{menu_addition} ) { | ||
| 395 | my $ext = App::DBBrowser::Table::Extensions->new( $sf->{i}, $sf->{o}, $sf->{d} ); | ||
| 396 | my $complex_col = $ext->column( $sql, $clause ); | ||
| 397 | if ( ! defined $complex_col ) { | ||
| 398 | next ORDER_BY; | ||
| 399 | } | ||
| 400 | $col = $complex_col; | ||
| 401 | } | ||
| 402 | elsif ( $sql->{aggregate_mode} ) { | ||
| 403 | my $sa = App::DBBrowser::Table::Substatement::Aggregate->new( $sf->{i}, $sf->{o}, $sf->{d} ); | ||
| 404 | $col = $sa->get_prepared_aggr_func( $sql, $clause, $col ); | ||
| 405 | if ( ! length $col ) { | ||
| 406 | next ORDER_BY; | ||
| 407 | } | ||
| 408 | } | ||
| 409 | push @{$sql->{order_by_cols}}, $col; | ||
| 410 | $sql->{order_by_stmt} = "ORDER BY " . join ',', @{$sql->{order_by_cols}}; | ||
| 411 | $info = $ax->get_sql_info( $sql ); | ||
| 412 | # Choose | ||
| 413 | my $direction = $tc->choose( | ||
| 414 | [ undef, "ASC", "DESC" ], | ||
| 415 | { %{$sf->{i}{lyt_h}}, info => $info } | ||
| 416 | ); | ||
| 417 | $ax->print_sql_info( $info ); | ||
| 418 | if ( ! defined $direction ){ | ||
| 419 | pop @{$sql->{order_by_cols}}; | ||
| 420 | next ORDER_BY; | ||
| 421 | } | ||
| 422 | else { | ||
| 423 | $sql->{order_by_cols}[-1] .= ' ' . $direction; | ||
| 424 | } | ||
| 425 | } | ||
| 426 | } | ||
| 427 | |||
| 428 | |||
| 429 | sub limit { | ||
| 430 | my ( $sf, $sql ) = @_; | ||
| 431 | my $ax = App::DBBrowser::Auxil->new( $sf->{i}, $sf->{o}, $sf->{d} ); | ||
| 432 | my $tu = Term::Choose::Util->new( $sf->{i}{tcu_default} ); | ||
| 433 | my ( $use_limit, $digits ) = $sf->__limit_and_offset_variables(); | ||
| 434 | my $bu_limit = $sql->{bu_limit}; | ||
| 435 | if ( $use_limit ) { | ||
| 436 | $sql->{limit_stmt} = "LIMIT"; | ||
| 437 | $sql->{limit_stmt} .= " " . $bu_limit if length $bu_limit; | ||
| 438 | } | ||
| 439 | else { | ||
| 440 | $sql->{limit_stmt} = "FETCH NEXT"; | ||
| 441 | $sql->{limit_stmt} .= " " . $bu_limit . " ROWS ONLY" if length $bu_limit; | ||
| 442 | } | ||
| 443 | my $info = $ax->get_sql_info( $sql ); | ||
| 444 | # Choose_a_number | ||
| 445 | my $limit = $tu->choose_a_number( | ||
| 446 | $digits, | ||
| 447 | { info => $info, cs_label => 'LIMIT: ', confirm => $sf->{i}{confirm}, back => $sf->{i}{back}, | ||
| 448 | default_number => $bu_limit } | ||
| 449 | ); | ||
| 450 | if ( ! defined $limit ) { | ||
| 451 | $sql->{limit_stmt} = ''; | ||
| 452 | delete $sql->{bu_limit}; | ||
| 453 | return; | ||
| 454 | } | ||
| 455 | if ( $use_limit ) { | ||
| 456 | $sql->{limit_stmt} = "LIMIT " . $limit; | ||
| 457 | } | ||
| 458 | else { | ||
| 459 | $sql->{limit_stmt} = "FETCH NEXT " . $limit . " ROWS ONLY"; | ||
| 460 | if ( ! $sql->{offset_stmt} && $sf->{i}{dbms} eq 'MSSQL' ) { | ||
| 461 | $sql->{offset_stmt} = "OFFSET 0 ROWS"; | ||
| 462 | } | ||
| 463 | } | ||
| 464 | $sql->{bu_limit} = $limit; | ||
| 465 | return 1; | ||
| 466 | |||
| 467 | |||
| 468 | } | ||
| 469 | |||
| 470 | |||
| 471 | sub offset { | ||
| 472 | my ( $sf, $sql ) = @_; | ||
| 473 | my $ax = App::DBBrowser::Auxil->new( $sf->{i}, $sf->{o}, $sf->{d} ); | ||
| 474 | my $tu = Term::Choose::Util->new( $sf->{i}{tcu_default} ); | ||
| 475 | my ( $use_limit, $digits ) = $sf->__limit_and_offset_variables(); | ||
| 476 | my $bu_offset = $sql->{bu_offset}; | ||
| 477 | $sql->{offset_stmt} = "OFFSET"; | ||
| 478 | $sql->{offset_stmt} .= " " . $bu_offset if length $bu_offset; | ||
| 479 | my $info = $ax->get_sql_info( $sql ); | ||
| 480 | # Choose_a_number | ||
| 481 | my $offset = $tu->choose_a_number( | ||
| 482 | $digits, | ||
| 483 | { info => $info, cs_label => 'OFFSET: ', confirm => $sf->{i}{confirm}, back => $sf->{i}{back}, | ||
| 484 | default_number => $bu_offset } | ||
| 485 | ); | ||
| 486 | if ( ! defined $offset ) { | ||
| 487 | $sql->{offset_stmt} = ''; | ||
| 488 | delete $sql->{bu_offset}; | ||
| 489 | if ( ! $sql->{bu_limit} ) { | ||
| 490 | $sql->{limit_stmt} = ''; | ||
| 491 | } | ||
| 492 | return; | ||
| 493 | } | ||
| 494 | if ( $use_limit ) { | ||
| 495 | $sql->{offset_stmt} = "OFFSET " . $offset; | ||
| 496 | } | ||
| 497 | else { | ||
| 498 | $sql->{offset_stmt} = "OFFSET " . $offset . " ROWS"; | ||
| 499 | } | ||
| 500 | $sql->{bu_offset} = $offset; | ||
| 501 | # Informix: no offset | ||
| 502 | if ( ! $sql->{limit_stmt} ) { | ||
| 503 | my $dbms = $sf->{i}{dbms}; | ||
| 504 | # SQLite/mysql/MariaDB: no offset without limit | ||
| 505 | $sql->{limit_stmt} = "LIMIT " . '9223372036854775807' if $dbms eq 'SQLite'; # 2 ** 63 - 1 | ||
| 506 | $sql->{limit_stmt} = "LIMIT " . '18446744073709551615' if $dbms =~ /^(?:mysql|MariaDB)\z/; # 2 ** 64 - 1 | ||
| 507 | # MySQL 8.0 Reference Manual - SQL Statements/Data Manipulation Statements/Select Statement/Limit clause: | ||
| 508 | # SELECT * FROM tbl LIMIT 95,18446744073709551615; -> all rows from the 95th to the last | ||
| 509 | } | ||
| 510 | return 1; | ||
| 511 | } | ||
| 512 | |||
| 513 | |||
| 514 | sub __limit_and_offset_variables { | ||
| 515 | my ( $sf ) = @_; | ||
| 516 | my $use_limit = $sf->{i}{dbms} =~ /^(?:SQLite|mysql|MariaDB|Pg|DuckDB|Informix)\z/ ? 1 : 0; | ||
| 517 | my $max_digits = 7; | ||
| 518 | return $use_limit, $max_digits; | ||
| 519 | } | ||
| 520 | |||
| 521 | |||
| 522 | |||
| 523 | 1; | ||
| 524 | |||
| 525 | __END__ |