| 1 | package # hide from PAUSE | ||
| 2 | App::DBBrowser::Table::Extensions::ScalarFunctions::String; | ||
| 3 | |||
| 4 | use warnings; | ||
| 5 | use strict; | ||
| 6 | use 5.016; | ||
| 7 | |||
| 8 | use App::DBBrowser::Auxil; | ||
| 9 | use App::DBBrowser::Table::Extensions::ScalarFunctions::GetArguments; | ||
| 10 | |||
| 11 | |||
| 12 | sub new { | ||
| 13 | my ( $class, $info, $options, $d ) = @_; | ||
| 14 | bless { | ||
| 15 | i => $info, | ||
| 16 | o => $options, | ||
| 17 | d => $d | ||
| 18 | }, $class; | ||
| 19 | } | ||
| 20 | |||
| 21 | |||
| 22 | sub function_left { | ||
| 23 | my ( $sf, $sql, $clause, $func, $cols, $r_data ) = @_; | ||
| 24 | $sf->__left_rigtht( $sql, $clause, $func, $cols, $r_data ); | ||
| 25 | } | ||
| 26 | |||
| 27 | |||
| 28 | sub function_right { | ||
| 29 | my ( $sf, $sql, $clause, $func, $cols, $r_data ) = @_; | ||
| 30 | $sf->__left_rigtht( $sql, $clause, $func, $cols, $r_data ); | ||
| 31 | } | ||
| 32 | |||
| 33 | |||
| 34 | sub __left_rigtht { | ||
| 35 | my ( $sf, $sql, $clause, $func, $cols, $r_data ) = @_; | ||
| 36 | my $dbms = $sf->{i}{dbms}; | ||
| 37 | my $ga = App::DBBrowser::Table::Extensions::ScalarFunctions::GetArguments->new( $sf->{i}, $sf->{o}, $sf->{d} ); | ||
| 38 | my $col = $ga->choose_a_column( $sql, $clause, $cols, $r_data ); | ||
| 39 | if ( ! defined $col ) { | ||
| 40 | return; | ||
| 41 | } | ||
| 42 | if ( $dbms eq 'Pg' ) { | ||
| 43 | $col = $sf->__pg_col_to_text( $sql, $col ); | ||
| 44 | } | ||
| 45 | my $args_data = [ | ||
| 46 | { prompt => 'Length: ', is_numeric => 1 } | ||
| 47 | ]; | ||
| 48 | my ( $length ) = $ga->get_arguments( $sql, $clause, $func, $args_data, $r_data ); | ||
| 49 | if ( ! defined $length ) { | ||
| 50 | return; | ||
| 51 | } | ||
| 52 | if ( $dbms eq 'SQLite' ) { | ||
| 53 | return "SUBSTR($col,-$length)" if $func eq 'RIGHT'; | ||
| 54 | return "SUBSTR($col,1,$length)"; | ||
| 55 | } | ||
| 56 | return "$func($col,$length)"; | ||
| 57 | } | ||
| 58 | |||
| 59 | |||
| 60 | sub function_lpad { | ||
| 61 | my ( $sf, $sql, $clause, $func, $cols, $r_data ) = @_; | ||
| 62 | return $sf->__rpad_lpad( $sql, $clause, $func, $cols, $r_data ); | ||
| 63 | } | ||
| 64 | |||
| 65 | |||
| 66 | sub function_rpad { | ||
| 67 | my ( $sf, $sql, $clause, $func, $cols, $r_data ) = @_; | ||
| 68 | return $sf->__rpad_lpad( $sql, $clause, $func, $cols, $r_data ); | ||
| 69 | } | ||
| 70 | |||
| 71 | |||
| 72 | sub __rpad_lpad { | ||
| 73 | my ( $sf, $sql, $clause, $func, $cols, $r_data ) = @_; | ||
| 74 | my $dbms = $sf->{i}{dbms}; | ||
| 75 | my $ax = App::DBBrowser::Auxil->new( $sf->{i}, $sf->{o}, $sf->{d} ); | ||
| 76 | my $ga = App::DBBrowser::Table::Extensions::ScalarFunctions::GetArguments->new( $sf->{i}, $sf->{o}, $sf->{d} ); | ||
| 77 | my $col = $ga->choose_a_column( $sql, $clause, $cols, $r_data ); | ||
| 78 | if ( ! defined $col ) { | ||
| 79 | return; | ||
| 80 | } | ||
| 81 | if ( $dbms eq 'Pg' ) { | ||
| 82 | $col = $sf->__pg_col_to_text( $sql, $col ); | ||
| 83 | } | ||
| 84 | my $args_data = [ | ||
| 85 | { prompt => 'Length: ', is_numeric => 1 }, | ||
| 86 | { prompt => 'Fill: ' } | ||
| 87 | ]; | ||
| 88 | my ( $length, $fill ) = $ga->get_arguments( $sql, $clause, $func, $args_data, $r_data ); | ||
| 89 | if ( ! defined $length ) { | ||
| 90 | return; | ||
| 91 | } | ||
| 92 | if ( $dbms eq 'DuckDB' ) { | ||
| 93 | $fill = length $fill ? $fill : "' '"; | ||
| 94 | } | ||
| 95 | if ( $dbms eq 'SQLite' ) { | ||
| 96 | $fill = defined $fill ? $ax->unquote_constant( $fill ) : ' '; | ||
| 97 | $fill = $sf->{d}{dbh}->quote( $fill x $length ); | ||
| 98 | return "SUBSTR($fill||$col,-$length,$length)" if $func eq 'LPAD'; | ||
| 99 | return "SUBSTR($col||$fill,1,$length)"; | ||
| 100 | } | ||
| 101 | else { | ||
| 102 | return "$func($col,$length)" if ! defined $fill; | ||
| 103 | return "$func($col,$length,$fill)"; | ||
| 104 | } | ||
| 105 | } | ||
| 106 | |||
| 107 | |||
| 108 | sub function_position { | ||
| 109 | my ( $sf, $sql, $clause, $func, $cols, $r_data ) = @_; | ||
| 110 | return $sf->__position_instr_locate_charindex( $sql, $clause, $func, $cols, $r_data ); | ||
| 111 | } | ||
| 112 | |||
| 113 | |||
| 114 | sub function_instr { | ||
| 115 | my ( $sf, $sql, $clause, $func, $cols, $r_data ) = @_; | ||
| 116 | return $sf->__position_instr_locate_charindex( $sql, $clause, $func, $cols, $r_data ); | ||
| 117 | } | ||
| 118 | |||
| 119 | |||
| 120 | sub function_locate { | ||
| 121 | my ( $sf, $sql, $clause, $func, $cols, $r_data ) = @_; | ||
| 122 | return $sf->__position_instr_locate_charindex( $sql, $clause, $func, $cols, $r_data ); | ||
| 123 | } | ||
| 124 | |||
| 125 | |||
| 126 | sub function_charindex { | ||
| 127 | my ( $sf, $sql, $clause, $func, $cols, $r_data ) = @_; | ||
| 128 | return $sf->__position_instr_locate_charindex( $sql, $clause, $func, $cols, $r_data ); | ||
| 129 | } | ||
| 130 | |||
| 131 | |||
| 132 | sub __position_instr_locate_charindex { | ||
| 133 | my ( $sf, $sql, $clause, $func, $cols, $r_data ) = @_; | ||
| 134 | my $dbms = $sf->{i}{dbms}; | ||
| 135 | my $ga = App::DBBrowser::Table::Extensions::ScalarFunctions::GetArguments->new( $sf->{i}, $sf->{o}, $sf->{d} ); | ||
| 136 | my $col = $ga->choose_a_column( $sql, $clause, $cols, $r_data ); | ||
| 137 | if ( ! defined $col ) { | ||
| 138 | return; | ||
| 139 | } | ||
| 140 | if ( $dbms eq 'Pg' ) { | ||
| 141 | $col = $sf->__pg_col_to_text( $sql, $col ); | ||
| 142 | } | ||
| 143 | my $args_data = [ | ||
| 144 | { prompt => 'Substring: ', empty_ok => 1 } | ||
| 145 | ]; | ||
| 146 | if ( $func eq 'POSITION' ) { | ||
| 147 | if ( $dbms eq 'Firebird' ) { | ||
| 148 | push @$args_data, { prompt => 'Start: ', is_numeric => 1 }; | ||
| 149 | } | ||
| 150 | } | ||
| 151 | elsif ( $func eq 'INSTR' ) { | ||
| 152 | if ( $dbms =~ /^(?:DB2|Informix|Oracle)\z/ ) { | ||
| 153 | push @$args_data, { prompt => 'Start: ', is_numeric => 1 }, { prompt => 'Count: ', is_numeric => 1 }; # 'Occurrence:' | ||
| 154 | } | ||
| 155 | } | ||
| 156 | elsif ( $func eq 'LOCATE' ) { | ||
| 157 | push @$args_data, { prompt => 'Start: ', is_numeric => 1 }; | ||
| 158 | } | ||
| 159 | elsif ( $func eq 'CHARINDEX' ) { | ||
| 160 | push @$args_data, { prompt => 'Start: ', is_numeric => 1 }, { prompt => 'Count: ', is_numeric => 1 }; # 'Occurrence:' | ||
| 161 | } | ||
| 162 | my ( $substring, $start, $count ) = $ga->get_arguments( $sql, $clause, $func, $args_data, $r_data ); | ||
| 163 | if ( ! defined $substring ) { | ||
| 164 | return; | ||
| 165 | } | ||
| 166 | if ( $func eq 'POSITION' ) { | ||
| 167 | return "$func($substring IN $col)" if ! length $start; | ||
| 168 | return "$func($substring,$col,$start)"; | ||
| 169 | } | ||
| 170 | elsif ( $func eq 'INSTR' ) { | ||
| 171 | return "$func($col,$substring)" if ! defined $start; | ||
| 172 | return "$func($col,$substring,$start)" if ! defined $count; | ||
| 173 | return "$func($col,$substring,$start,$count)"; | ||
| 174 | } | ||
| 175 | elsif ( $func eq 'LOCATE' ) { | ||
| 176 | return "$func($substring,$col)" if ! defined $start; | ||
| 177 | return "$func($substring,$col,$start)"; | ||
| 178 | } | ||
| 179 | elsif ( $func eq 'CHARINDEX' ) { | ||
| 180 | return "$func($substring,$col)" if ! defined $start; | ||
| 181 | return "$func($substring,$col,$start)" if ! defined $count; | ||
| 182 | return "$func($substring,$col,$start,$count)"; | ||
| 183 | } | ||
| 184 | } | ||
| 185 | |||
| 186 | |||
| 187 | sub function_substr { | ||
| 188 | my ( $sf, $sql, $clause, $func, $cols, $r_data ) = @_; | ||
| 189 | return $sf->__substr_substring( $sql, $clause, $func, $cols, $r_data ); | ||
| 190 | } | ||
| 191 | |||
| 192 | |||
| 193 | sub function_substring { | ||
| 194 | my ( $sf, $sql, $clause, $func, $cols, $r_data ) = @_; | ||
| 195 | return $sf->__substr_substring( $sql, $clause, $func, $cols, $r_data ); | ||
| 196 | } | ||
| 197 | |||
| 198 | sub __substr_substring { | ||
| 199 | my ( $sf, $sql, $clause, $func, $cols, $r_data ) = @_; | ||
| 200 | my $dbms = $sf->{i}{dbms}; | ||
| 201 | my $ga = App::DBBrowser::Table::Extensions::ScalarFunctions::GetArguments->new( $sf->{i}, $sf->{o}, $sf->{d} ); | ||
| 202 | my $col = $ga->choose_a_column( $sql, $clause, $cols, $r_data ); | ||
| 203 | if ( ! defined $col ) { | ||
| 204 | return; | ||
| 205 | } | ||
| 206 | if ( $dbms eq 'Pg' ) { | ||
| 207 | $col = $sf->__pg_col_to_text( $sql, $col ); | ||
| 208 | } | ||
| 209 | my $args_data = [ | ||
| 210 | { prompt => 'StartPos: ', is_numeric => 1 }, | ||
| 211 | { prompt => 'Length: ', is_numeric => 1 }, | ||
| 212 | ]; | ||
| 213 | my ( $startpos, $length ) = $ga->get_arguments( $sql, $clause, $func, $args_data, $r_data ); | ||
| 214 | return if ! defined $startpos; | ||
| 215 | if ( $func eq 'SUBSTRING' ) { | ||
| 216 | if ( $dbms eq 'Firebird' ) { | ||
| 217 | return "SUBSTRING($col FROM $startpos)" if ! defined $length; | ||
| 218 | return "SUBSTRING($col FROM $startpos FOR $length)"; | ||
| 219 | } | ||
| 220 | else { | ||
| 221 | return "SUBSTRING($col, $startpos)" if ! defined $length; | ||
| 222 | return "SUBSTRING($col, $startpos, $length)"; | ||
| 223 | } | ||
| 224 | } | ||
| 225 | else { | ||
| 226 | return "SUBSTR($col,$startpos)" if ! defined $length; | ||
| 227 | return "SUBSTR($col,$startpos,$length)"; | ||
| 228 | } | ||
| 229 | } | ||
| 230 | |||
| 231 | |||
| 232 | sub function_replace { | ||
| 233 | my ( $sf, $sql, $clause, $func, $cols, $r_data ) = @_; | ||
| 234 | my $dbms = $sf->{i}{dbms}; | ||
| 235 | my $ga = App::DBBrowser::Table::Extensions::ScalarFunctions::GetArguments->new( $sf->{i}, $sf->{o}, $sf->{d} ); | ||
| 236 | my $col = $ga->choose_a_column( $sql, $clause, $cols, $r_data ); | ||
| 237 | if ( ! defined $col ) { | ||
| 238 | return; | ||
| 239 | } | ||
| 240 | if ( $dbms eq 'Pg' ) { | ||
| 241 | $col = $sf->__pg_col_to_text( $sql, $col ); | ||
| 242 | } | ||
| 243 | my $args_data = [ | ||
| 244 | { prompt => 'From string: ' }, | ||
| 245 | { prompt => 'New string: ', empty_ok => 1 }, | ||
| 246 | ]; | ||
| 247 | my ( $from_string, $new_string ) = $ga->get_arguments( $sql, $clause, $func, $args_data, $r_data ); | ||
| 248 | if ( ! defined $from_string || ! defined $new_string ) { | ||
| 249 | return; | ||
| 250 | } | ||
| 251 | return "REPLACE($col,$from_string,$new_string)"; | ||
| 252 | } | ||
| 253 | |||
| 254 | |||
| 255 | sub function_trim { | ||
| 256 | my ( $sf, $sql, $clause, $func, $cols, $r_data ) = @_; | ||
| 257 | my $dbms = $sf->{i}{dbms}; | ||
| 258 | my $ga = App::DBBrowser::Table::Extensions::ScalarFunctions::GetArguments->new( $sf->{i}, $sf->{o}, $sf->{d} ); | ||
| 259 | my $col = $ga->choose_a_column( $sql, $clause, $cols, $r_data ); | ||
| 260 | if ( ! defined $col ) { | ||
| 261 | return; | ||
| 262 | } | ||
| 263 | if ( $dbms eq 'Pg' ) { | ||
| 264 | $col = $sf->__pg_col_to_text( $sql, $col ); | ||
| 265 | } | ||
| 266 | my $args_data; | ||
| 267 | if ( $dbms eq 'SQLite' ) { | ||
| 268 | $args_data = [ | ||
| 269 | { prompt => 'What: ' } | ||
| 270 | ]; | ||
| 271 | } | ||
| 272 | else { | ||
| 273 | $args_data = [ | ||
| 274 | { prompt => 'Where: ', unquote => 1, history => [ qw(BOTH LEADING TRAILING) ], history_only => 1, skip_ok => 1 }, | ||
| 275 | { prompt => 'What: ' } | ||
| 276 | ]; | ||
| 277 | } | ||
| 278 | my @args = $ga->get_arguments( $sql, $clause, $func, $args_data, $r_data ); | ||
| 279 | if ( $dbms eq 'SQLite' ) { | ||
| 280 | my ( $what ) = @args; | ||
| 281 | return "TRIM($col)" if ! defined $what; | ||
| 282 | return "TRIM($col,$what)"; | ||
| 283 | } | ||
| 284 | else { | ||
| 285 | my ( $where, $what ) = @args; | ||
| 286 | my $tmp = join ' ', grep { length } $where, $what; | ||
| 287 | return "TRIM($col)" if ! length $tmp; | ||
| 288 | return "TRIM($tmp FROM $col)"; | ||
| 289 | } | ||
| 290 | } | ||
| 291 | |||
| 292 | |||
| 293 | sub function_concat { | ||
| 294 | my ( $sf, $sql, $clause, $func, $cols, $r_data ) = @_; | ||
| 295 | my $dbms = $sf->{i}{dbms}; | ||
| 296 | my $ga = App::DBBrowser::Table::Extensions::ScalarFunctions::GetArguments->new( $sf->{i}, $sf->{o}, $sf->{d} ); | ||
| 297 | my $chosen_cols = $ga->choose_columns( $sql, $clause, $func, $cols, $r_data ); | ||
| 298 | if ( ! defined $chosen_cols ) { | ||
| 299 | return; | ||
| 300 | } | ||
| 301 | if ( $dbms eq 'Pg' ) { | ||
| 302 | for my $col ( @$chosen_cols ) { | ||
| 303 | $col = $sf->__pg_col_to_text( $sql, $col ); | ||
| 304 | } | ||
| 305 | } | ||
| 306 | my $args_data = [ | ||
| 307 | { prompt => 'Separator: ', history => [ '-', ' ', '_', ',', '/', '=', '+', '|' ] }, | ||
| 308 | ]; | ||
| 309 | my ( $sep ) = $ga->get_arguments( $sql, $clause, $func, $args_data, $r_data ); | ||
| 310 | my $arg; | ||
| 311 | if ( length $sep ) { | ||
| 312 | for ( @$chosen_cols ) { | ||
| 313 | push @$arg, $_, $sep; | ||
| 314 | } | ||
| 315 | pop @$arg; | ||
| 316 | } | ||
| 317 | else { | ||
| 318 | $arg = $chosen_cols | ||
| 319 | } | ||
| 320 | return "CONCAT(" . join( ',', @$arg ) . ")" if $dbms =~ /^(?:mysql|MariaDB|MSSQL)\z/; | ||
| 321 | return join( " || ", @$arg ); # ansi 2003 | ||
| 322 | } | ||
| 323 | |||
| 324 | |||
| 325 | sub __pg_col_to_text { | ||
| 326 | my ( $sf, $sql, $col ) = @_; | ||
| 327 | my $ax = App::DBBrowser::Auxil->new( $sf->{i}, $sf->{o}, $sf->{d} ); | ||
| 328 | return $ax->pg_column_to_text( $sql, $col ); | ||
| 329 | } | ||
| 330 | |||
| 331 | |||
| 332 | # string units: $position, $instr, $locate, $left, $right, $length, $substring, $upper, $lower ## | ||
| 333 | # $round datetime: locale | ||
| 334 | |||
| 335 | 1; |