| 1 | package # hide from PAUSE | ||
| 2 | App::DBBrowser::From::Cte; | ||
| 3 | |||
| 4 | use warnings; | ||
| 5 | use strict; | ||
| 6 | use 5.016; | ||
| 7 | |||
| 8 | use List::MoreUtils qw( any ); | ||
| 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 | |||
| 16 | sub new { | ||
| 17 | my ( $class, $info, $options, $d ) = @_; | ||
| 18 | my $sf = { | ||
| 19 | i => $info, | ||
| 20 | o => $options, | ||
| 21 | d => $d | ||
| 22 | }; | ||
| 23 | bless $sf, $class; | ||
| 24 | } | ||
| 25 | |||
| 26 | |||
| 27 | sub cte_as_main_table { | ||
| 28 | my ( $sf ) = @_; | ||
| 29 | my $ax = App::DBBrowser::Auxil->new( $sf->{i}, $sf->{o}, $sf->{d} ); | ||
| 30 | $sf->{d}{stmt_types} = [ 'Cte_Only' ]; | ||
| 31 | my $sql = {}; | ||
| 32 | $ax->reset_sql( $sql ); | ||
| 33 | $ax->print_sql_info( $ax->get_sql_info( $sql ) ); ## | ||
| 34 | my $cte = $sf->cte( $sql ); | ||
| 35 | if ( ! defined $cte ) { | ||
| 36 | return; | ||
| 37 | } | ||
| 38 | return $cte; | ||
| 39 | } | ||
| 40 | |||
| 41 | |||
| 42 | sub cte { | ||
| 43 | my ( $sf, $sql ) = @_; | ||
| 44 | my $ax = App::DBBrowser::Auxil->new( $sf->{i}, $sf->{o}, $sf->{d} ); | ||
| 45 | my $tc = Term::Choose->new( $sf->{i}{tc_default} ); | ||
| 46 | my ( $new_cte, $remove_ctes ) = ( ' New CTE', ' Remove CTEs' ); | ||
| 47 | my $old_idx = 0; | ||
| 48 | |||
| 49 | CHOOSE_CTE: while ( 1 ) { | ||
| 50 | my @avail_ctes = map { '- ' . $_->{name} } @{$sf->{d}{cte_history}} ; | ||
| 51 | my @pre; | ||
| 52 | if ( $sf->{d}{nested_subqueries} ) { | ||
| 53 | @pre = ( undef ); | ||
| 54 | if ( ! @avail_ctes ) { | ||
| 55 | $ax->print_error_message( 'No ctes available.' ); | ||
| 56 | return; | ||
| 57 | } | ||
| 58 | } | ||
| 59 | else { | ||
| 60 | @pre = ( undef, $new_cte, $remove_ctes ); | ||
| 61 | } | ||
| 62 | my $menu = [ @pre, @avail_ctes ]; | ||
| 63 | my $info = $ax->get_sql_info( $sql ); | ||
| 64 | # Choose | ||
| 65 | my $idx = $tc->choose( | ||
| 66 | $menu, | ||
| 67 | { %{$sf->{i}{lyt_v}}, info => $info, index => 1, undef => ' <=', default => $old_idx } | ||
| 68 | ); | ||
| 69 | $ax->print_sql_info( $info ); | ||
| 70 | if ( ! defined $idx || $idx == 0 ) { | ||
| 71 | return; | ||
| 72 | } | ||
| 73 | if ( $sf->{o}{G}{menu_memory} ) { | ||
| 74 | if ( $old_idx == $idx && ! $ENV{TC_RESET_AUTO_UP} ) { | ||
| 75 | $old_idx = 0; | ||
| 76 | next CHOOSE_CTE; | ||
| 77 | } | ||
| 78 | $old_idx = $idx; | ||
| 79 | } | ||
| 80 | if ( $menu->[$idx] eq $new_cte ) { | ||
| 81 | $sf->__prepare_and_add_cte( $sql ); | ||
| 82 | } | ||
| 83 | elsif ( $menu->[$idx] eq $remove_ctes ) { | ||
| 84 | $sf->__remove_ctes( $sql ); | ||
| 85 | } | ||
| 86 | else { | ||
| 87 | $idx -= @pre; | ||
| 88 | my $cte_name = $sf->{d}{cte_history}[$idx]{name}; | ||
| 89 | return $cte_name; | ||
| 90 | } | ||
| 91 | } | ||
| 92 | } | ||
| 93 | |||
| 94 | |||
| 95 | sub __prepare_and_add_cte { | ||
| 96 | my ( $sf, $sql ) = @_; | ||
| 97 | my $ax = App::DBBrowser::Auxil->new( $sf->{i}, $sf->{o}, $sf->{d} ); | ||
| 98 | my $sq = App::DBBrowser::From::Subquery->new( $sf->{i}, $sf->{o}, $sf->{d} ); | ||
| 99 | my $tc = Term::Choose->new( $sf->{i}{tc_default} ); | ||
| 100 | my $tr = Term::Form::ReadLine->new( $sf->{i}{tr_default} ); | ||
| 101 | my $ctes = [ @{$sf->{d}{cte_history}} ]; | ||
| 102 | |||
| 103 | CHOOSE_QUERY: while ( 1 ) { | ||
| 104 | my $selected_stmt = $sq->__choose_query( $sql, 'cte' ); | ||
| 105 | if ( ! defined $selected_stmt ) { | ||
| 106 | return; | ||
| 107 | } | ||
| 108 | my $count_query_loop; | ||
| 109 | |||
| 110 | QUERY: while ( 1 ) { | ||
| 111 | my $info = $ax->get_sql_info( $sql ); ## | ||
| 112 | # Readline | ||
| 113 | my $query = $tr->readline( | ||
| 114 | 'Stmt: ', | ||
| 115 | { default => $selected_stmt, show_context => 1, info => $info } | ||
| 116 | ); | ||
| 117 | $ax->print_sql_info( $info ); | ||
| 118 | if ( ! length $query ) { | ||
| 119 | next CHOOSE_QUERY; | ||
| 120 | } | ||
| 121 | $info .= "\n". 'Stmt: ' . $query; | ||
| 122 | my $default_name; | ||
| 123 | |||
| 124 | NAME: while ( 1 ) { | ||
| 125 | # Readline | ||
| 126 | my $full_cte_name = $tr->readline( | ||
| 127 | 'Name: ', | ||
| 128 | { default => $default_name, show_context => 1, info => $info, history => [ 'cte1' .. 'cte9' ] } | ||
| 129 | ); | ||
| 130 | $ax->print_sql_info( $info ); | ||
| 131 | if ( ! length $full_cte_name ) { | ||
| 132 | next CHOOSE_QUERY if ++$count_query_loop > 1; | ||
| 133 | next QUERY; | ||
| 134 | } | ||
| 135 | my $rx_quoted_identifier = $ax->regex_quoted_identifier(); | ||
| 136 | my $cte_name = $full_cte_name =~ s/^ \s* (?:RECURSIVE\s+)? ( $rx_quoted_identifier | [^\s(]+ ) \s* (?:\(.+)? \z/$1/rix; | ||
| 137 | my $cte = { | ||
| 138 | full_name => $full_cte_name, | ||
| 139 | query => $query, | ||
| 140 | name => $cte_name, | ||
| 141 | }; | ||
| 142 | $default_name = length $default_name ? '' : $full_cte_name; | ||
| 143 | my @table_keys = keys %{$sf->{d}{tables_info}}; | ||
| 144 | my @cte_names = map { $_->{name} } @$ctes; | ||
| 145 | if ( any { $_ eq $cte_name } @table_keys, @cte_names ) { | ||
| 146 | my $type = ( grep { $_ eq $cte_name } @table_keys ) ? 'table' : 'CTE'; | ||
| 147 | my $prompt = "A $type '$cte_name' already exists."; | ||
| 148 | # Choose | ||
| 149 | $tc->choose( | ||
| 150 | [ 'Press ENTER' ], | ||
| 151 | { %{$sf->{i}{lyt_h}}, info => $info, prompt => $prompt } | ||
| 152 | ); | ||
| 153 | $ax->print_sql_info( $info ); | ||
| 154 | next NAME; | ||
| 155 | } | ||
| 156 | else { | ||
| 157 | if ( $cte->{full_name} =~ s/^\s*RECURSIVE\s+//i ) { | ||
| 158 | $cte->{is_recursive} = 1; | ||
| 159 | } | ||
| 160 | push @$ctes, $cte; | ||
| 161 | $sf->{d}{cte_history} = [ @$ctes ]; | ||
| 162 | unshift @{$sf->{d}{subquery_history}}, $query; | ||
| 163 | return 1; | ||
| 164 | } | ||
| 165 | } | ||
| 166 | } | ||
| 167 | } | ||
| 168 | } | ||
| 169 | |||
| 170 | |||
| 171 | sub __remove_ctes { | ||
| 172 | my ( $sf, $sql ) = @_; | ||
| 173 | my $ax = App::DBBrowser::Auxil->new( $sf->{i}, $sf->{o}, $sf->{d} ); | ||
| 174 | my $tc = Term::Choose->new( $sf->{i}{tc_default} ); | ||
| 175 | my $confirm = $sf->{i}{_confirm}; | ||
| 176 | my $all = ' All CTEs'; | ||
| 177 | my $ctes = [ @{$sf->{d}{cte_history}} ]; | ||
| 178 | my @bu; | ||
| 179 | |||
| 180 | REMOVE_CTE: while ( 1 ) { | ||
| 181 | $sf->{d}{cte_history} = [ @$ctes ]; | ||
| 182 | my @pre = ( undef, $confirm ); | ||
| 183 | my $menu = [ @pre, map( '- ' . $_->{name}, @$ctes ) ]; | ||
| 184 | push @$menu, $all; | ||
| 185 | my $info = $ax->get_sql_info( $sql ); | ||
| 186 | # Choose | ||
| 187 | my $idx = $tc->choose( | ||
| 188 | $menu, | ||
| 189 | { %{$sf->{i}{lyt_v}}, info => $info, prompt => 'Remove CTE:', index => 1, undef => $sf->{i}{_back} } | ||
| 190 | ); | ||
| 191 | $ax->print_sql_info( $info ); | ||
| 192 | if ( ! defined $idx || $idx == 0 ) { | ||
| 193 | if ( @bu ) { | ||
| 194 | $ctes = pop @bu; | ||
| 195 | next REMOVE_CTE; | ||
| 196 | } | ||
| 197 | return; | ||
| 198 | } | ||
| 199 | push @bu, [ @$ctes ]; | ||
| 200 | if ( $menu->[$idx] eq $confirm ) { | ||
| 201 | $sf->{d}{cte_history} = [ @$ctes ]; | ||
| 202 | return 1; | ||
| 203 | } | ||
| 204 | elsif ( $menu->[$idx] eq $all ) { | ||
| 205 | $ctes = []; | ||
| 206 | } | ||
| 207 | else { | ||
| 208 | $idx -= @pre; | ||
| 209 | splice( @$ctes, $idx, 1 ); | ||
| 210 | } | ||
| 211 | } | ||
| 212 | } | ||
| 213 | |||
| 214 | |||
| 215 | |||
| 216 | 1; | ||
| 217 | |||
| 218 | __END__ |