« DBBrowser/Table/Extensions/ScalarFunctions/String.pm
↑ Summary
DBBrowser/Table/Extensions/ColAliases.pm »
| 1 | package # hide from PAUSE | ||
| 2 | App::DBBrowser::Table::Substatement::Aggregate; | ||
| 3 | |||
| 4 | use warnings; | ||
| 5 | use strict; | ||
| 6 | use 5.016; | ||
| 7 | |||
| 8 | use Term::Choose qw(); | ||
| 9 | use Term::Form::ReadLine qw(); | ||
| 10 | |||
| 11 | use App::DBBrowser::Auxil; | ||
| 12 | use App::DBBrowser::Table::Extensions; | ||
| 13 | |||
| 14 | |||
| 15 | sub new { | ||
| 16 | my ( $class, $info, $options, $d ) = @_; | ||
| 17 | bless { | ||
| 18 | i => $info, | ||
| 19 | o => $options, | ||
| 20 | d => $d | ||
| 21 | }, $class; | ||
| 22 | } | ||
| 23 | |||
| 24 | |||
| 25 | sub __group_concat { | ||
| 26 | my ( $sf ) = @_; | ||
| 27 | my $dbms = $sf->{i}{dbms}; | ||
| 28 | my $group_concat = ''; | ||
| 29 | if ( $dbms =~ /^(?:SQLite|mysql|MariaDB)\z/ ) { | ||
| 30 | $group_concat = "GROUP_CONCAT"; | ||
| 31 | } | ||
| 32 | elsif ( $dbms =~ /^(?:Pg|DuckDB|MSSQL)\z/ ) { | ||
| 33 | $group_concat = "STRING_AGG"; | ||
| 34 | } | ||
| 35 | elsif ( $dbms eq 'Firebird' ) { | ||
| 36 | $group_concat = "LIST"; | ||
| 37 | } | ||
| 38 | elsif ( $dbms =~ /^(?:DB2|Oracle)\z/ ) { | ||
| 39 | $group_concat = "LISTAGG"; | ||
| 40 | } | ||
| 41 | return $group_concat; | ||
| 42 | } | ||
| 43 | |||
| 44 | |||
| 45 | sub available_aggregate_functions { | ||
| 46 | my ( $sf ) = @_; | ||
| 47 | my $avail_aggr = [ "COUNT(*)", "COUNT(X)", "SUM(X)", "AVG(X)", "MIN(X)", "MAX(X)" ]; | ||
| 48 | my $group_concat = $sf->__group_concat(); | ||
| 49 | if ( $group_concat ) { | ||
| 50 | push @$avail_aggr, $group_concat . "(X)"; | ||
| 51 | } | ||
| 52 | return $avail_aggr | ||
| 53 | } | ||
| 54 | |||
| 55 | |||
| 56 | sub get_prepared_aggr_func { | ||
| 57 | my ( $sf, $sql, $clause, $aggr, $r_data ) = @_; | ||
| 58 | my $ax = App::DBBrowser::Auxil->new( $sf->{i}, $sf->{o}, $sf->{d} ); | ||
| 59 | my $tc = Term::Choose->new( $sf->{i}{tc_default} ); | ||
| 60 | my $dbms = $sf->{i}{dbms}; | ||
| 61 | $r_data //= []; | ||
| 62 | push @$r_data, [ 'aggr' ]; | ||
| 63 | my $prepared_aggr; | ||
| 64 | if ( $aggr !~ /\(X\)\z/ ) { | ||
| 65 | $prepared_aggr = $aggr; | ||
| 66 | } | ||
| 67 | else { | ||
| 68 | $aggr =~ s/\(X\)\z//; | ||
| 69 | my @pre = ( undef ); | ||
| 70 | if ( $sf->{o}{enable}{extended_cols} ) { | ||
| 71 | push @pre, $sf->{i}{menu_addition}; | ||
| 72 | } | ||
| 73 | my $group_concat = $sf->__group_concat(); | ||
| 74 | $prepared_aggr = $aggr . "("; | ||
| 75 | if ( $aggr eq 'COUNT' || ( $aggr eq $group_concat && $dbms ne 'MSSQL' ) ) { | ||
| 76 | my ( $all, $distinct ) = ( 'ALL', 'DISTINCT' ); | ||
| 77 | my $info = $sf->__prepared_aggr_info( $sql, $clause, $prepared_aggr, $r_data ); | ||
| 78 | # Choose | ||
| 79 | my $choice = $tc->choose( | ||
| 80 | [ undef, $all, $distinct ], | ||
| 81 | { %{$sf->{i}{lyt_h}}, info => $info } | ||
| 82 | ); | ||
| 83 | $ax->print_sql_info( $info ); | ||
| 84 | if ( ! defined $choice ) { | ||
| 85 | return; | ||
| 86 | } | ||
| 87 | #if ( $choice eq $distinct ) { | ||
| 88 | # $prepared_aggr .= "DISTINCT "; | ||
| 89 | #} | ||
| 90 | $prepared_aggr .= $choice . " "; | ||
| 91 | } | ||
| 92 | |||
| 93 | COLUMN: while ( 1 ) { | ||
| 94 | my $info = $sf->__prepared_aggr_info( $sql, $clause, $prepared_aggr, $r_data ); | ||
| 95 | # Choose | ||
| 96 | my $col = $tc->choose( | ||
| 97 | [ @pre, @{$sql->{columns}} ], | ||
| 98 | { %{$sf->{i}{lyt_h}}, info => $info } | ||
| 99 | ); | ||
| 100 | $ax->print_sql_info( $info ); | ||
| 101 | if ( ! defined $col ) { | ||
| 102 | pop @$r_data; | ||
| 103 | return; | ||
| 104 | } | ||
| 105 | elsif ( $col eq $sf->{i}{menu_addition} ) { | ||
| 106 | my $ext = App::DBBrowser::Table::Extensions->new( $sf->{i}, $sf->{o}, $sf->{d} ); | ||
| 107 | # use normal columns within aggregate functions: | ||
| 108 | $sql->{used_in_aggregate_function} = 1; | ||
| 109 | $r_data->[-1] = [ 'aggr', $prepared_aggr ]; | ||
| 110 | my $complex_col = $ext->column( $sql, $clause, $r_data ); | ||
| 111 | delete $sql->{used_in_aggregate_function}; | ||
| 112 | if ( ! defined $complex_col ) { | ||
| 113 | next COLUMN; | ||
| 114 | } | ||
| 115 | $col = $complex_col; | ||
| 116 | } | ||
| 117 | if ( $prepared_aggr =~ /ALL\s\z/ ) { | ||
| 118 | $prepared_aggr = $aggr . "("; | ||
| 119 | }; | ||
| 120 | if ( $aggr =~ /^$group_concat\z/i ) { | ||
| 121 | my $bu_prepared_aggr = $prepared_aggr; | ||
| 122 | $prepared_aggr = $sf->__op_group_concat( $sql, $clause, $col, $prepared_aggr, $r_data ); | ||
| 123 | if ( ! defined $prepared_aggr ) { | ||
| 124 | $prepared_aggr = $bu_prepared_aggr; | ||
| 125 | next COLUMN; | ||
| 126 | } | ||
| 127 | } | ||
| 128 | else { | ||
| 129 | $prepared_aggr .= "$col)"; | ||
| 130 | } | ||
| 131 | last COLUMN; | ||
| 132 | } | ||
| 133 | |||
| 134 | } | ||
| 135 | pop @$r_data; | ||
| 136 | return $prepared_aggr; | ||
| 137 | } | ||
| 138 | |||
| 139 | |||
| 140 | sub __prepared_aggr_info { | ||
| 141 | my ( $sf, $sql, $clause, $prepared_aggr, $r_data ) = @_; | ||
| 142 | my $ax = App::DBBrowser::Auxil->new( $sf->{i}, $sf->{o}, $sf->{d} ); | ||
| 143 | my $ext = App::DBBrowser::Table::Extensions->new( $sf->{i}, $sf->{o}, $sf->{d} ); | ||
| 144 | $r_data->[-1] = [ 'aggr', $prepared_aggr ]; | ||
| 145 | my $info = $ax->get_sql_info( $sql ) . $ext->nested_func_info( $r_data ); | ||
| 146 | return $info; | ||
| 147 | } | ||
| 148 | |||
| 149 | |||
| 150 | sub __op_group_concat { | ||
| 151 | my ( $sf, $sql, $clause, $col, $prepared_aggr, $r_data ) = @_; | ||
| 152 | my $ax = App::DBBrowser::Auxil->new( $sf->{i}, $sf->{o}, $sf->{d} ); | ||
| 153 | my $tc = Term::Choose->new( $sf->{i}{tc_default} ); | ||
| 154 | my $dbms = $sf->{i}{dbms}; | ||
| 155 | my $is_distinct = $prepared_aggr =~ /DISTINCT\s\z/; | ||
| 156 | if ( $dbms eq 'Pg' ) { | ||
| 157 | $prepared_aggr .= $ax->pg_column_to_text( $sql, $col ); | ||
| 158 | } | ||
| 159 | else { | ||
| 160 | $prepared_aggr .= $col; | ||
| 161 | } | ||
| 162 | my $sep = ','; | ||
| 163 | my $order_by_stmt; | ||
| 164 | if ( $dbms =~ /^(?:mysql|MariaDB|Pg|DuckDB|MSSQL)\z/ | ||
| 165 | || ( $dbms =~ /^(?:DB2|Oracle)\z/ && ! $is_distinct ) | ||
| 166 | ) { | ||
| 167 | my $read = ':Read'; | ||
| 168 | if ( $dbms eq 'Pg' && $is_distinct ) { | ||
| 169 | $col = $ax->pg_column_to_text( $sql, $col ); | ||
| 170 | } | ||
| 171 | my @choices = ( "ASC", "DESC", $read ); | ||
| 172 | my $menu = [ undef, @choices ]; | ||
| 173 | my $info = $sf->__prepared_aggr_info( $sql, $clause, $prepared_aggr, $r_data ); | ||
| 174 | # Choose | ||
| 175 | my $choice = $tc->choose( | ||
| 176 | $menu, | ||
| 177 | { %{$sf->{i}{lyt_h}}, info => $info, undef => '<<', prompt => 'Order:' } | ||
| 178 | ); | ||
| 179 | $ax->print_sql_info( $info ); | ||
| 180 | if ( ! defined $choice ) { | ||
| 181 | # default order | ||
| 182 | } | ||
| 183 | elsif ( $choice eq $read ) { | ||
| 184 | my $tr = Term::Form::ReadLine->new( $sf->{i}{tr_default} ); | ||
| 185 | my $history = [ | ||
| 186 | join( ', ', @{$sql->{columns}} ), | ||
| 187 | join( ' DESC, ', @{$sql->{columns}} ) . ' DESC', | ||
| 188 | ]; | ||
| 189 | my $info = $sf->__prepared_aggr_info( $sql, $clause, $prepared_aggr, $r_data ); | ||
| 190 | # Readline | ||
| 191 | $order_by_stmt = $tr->readline( | ||
| 192 | 'ORDER BY ', | ||
| 193 | { info => $info, history => $history } | ||
| 194 | ); | ||
| 195 | $ax->print_sql_info( $info ); | ||
| 196 | if ( length $order_by_stmt ) { | ||
| 197 | $order_by_stmt = "ORDER BY " . $order_by_stmt; | ||
| 198 | } | ||
| 199 | } | ||
| 200 | else { | ||
| 201 | $order_by_stmt = "ORDER BY $col $choice"; | ||
| 202 | } | ||
| 203 | } | ||
| 204 | if ( $dbms eq 'SQLite' ) { | ||
| 205 | if ( $is_distinct ) { | ||
| 206 | # https://sqlite.org/forum/info/221c2926f5e6f155 | ||
| 207 | # SQLite: GROUP_CONCAT with DISTINCT and custom seperator does not work | ||
| 208 | # default separator is ',' | ||
| 209 | $prepared_aggr .= ")"; | ||
| 210 | } | ||
| 211 | else { | ||
| 212 | $prepared_aggr .= ",'$sep')"; | ||
| 213 | } | ||
| 214 | } | ||
| 215 | elsif ( $dbms =~ /^(?:mysql|MariaDB)\z/ ) { | ||
| 216 | if ( $order_by_stmt ) { | ||
| 217 | $prepared_aggr .= " $order_by_stmt SEPARATOR '$sep')"; | ||
| 218 | } | ||
| 219 | else { | ||
| 220 | $prepared_aggr .= " SEPARATOR '$sep')"; | ||
| 221 | } | ||
| 222 | } | ||
| 223 | elsif ( $dbms =~ /^(?:Pg|DuckDB)\z/ ) { | ||
| 224 | # Pg, STRING_AGG: | ||
| 225 | # - separator mandatory | ||
| 226 | # - expects text type as argument | ||
| 227 | # - with DISTINCT the STRING_AGG col and the ORDER BY col must be identical | ||
| 228 | if ( $order_by_stmt ) { | ||
| 229 | $prepared_aggr .= ",'$sep' $order_by_stmt)"; | ||
| 230 | } | ||
| 231 | else { | ||
| 232 | $prepared_aggr .= ",'$sep')"; | ||
| 233 | } | ||
| 234 | } | ||
| 235 | elsif ( $dbms eq 'Firebird' ) { | ||
| 236 | $prepared_aggr .= ",'$sep')"; | ||
| 237 | } | ||
| 238 | elsif ( $dbms =~ /^(?:DB2|Oracle|MSSQL)\z/ ) { | ||
| 239 | # No order with distinct | ||
| 240 | # DB2 codes: error code -214 - error caused by: | ||
| 241 | # DISTINCT is specified in the SELECT clause, and a column name or sort-key-expression in the | ||
| 242 | # ORDER BY clause cannot be matched exactly with a column name or expression in the select list. | ||
| 243 | if ( $order_by_stmt ) { | ||
| 244 | $prepared_aggr .= ",'$sep') WITHIN GROUP ($order_by_stmt)"; | ||
| 245 | } | ||
| 246 | else { | ||
| 247 | $prepared_aggr .= ",'$sep')"; | ||
| 248 | } | ||
| 249 | } | ||
| 250 | else { | ||
| 251 | $prepared_aggr .= ")"; | ||
| 252 | } | ||
| 253 | return $prepared_aggr; | ||
| 254 | } | ||
| 255 | |||
| 256 | |||
| 257 | |||
| 258 | 1; | ||
| 259 | |||
| 260 | __END__ |