« DBBrowser/Table/Extensions/ScalarFunctions/Date.pm
↑ Summary
DBBrowser/Table/Extensions/ScalarFunctions/To.pm »
| 1 | package # hide from PAUSE | ||
| 2 | App::DBBrowser::Auxil; | ||
| 3 | |||
| 4 | use warnings; | ||
| 5 | use strict; | ||
| 6 | use 5.016; | ||
| 7 | |||
| 8 | use Encode qw( decode ); | ||
| 9 | use Scalar::Util qw( looks_like_number ); | ||
| 10 | #use Storable qw(); # required | ||
| 11 | |||
| 12 | |||
| 13 | use DBI::Const::GetInfoType; | ||
| 14 | use JSON::MaybeXS qw( decode_json ); | ||
| 15 | use List::MoreUtils qw( none ); | ||
| 16 | |||
| 17 | use Term::Choose qw(); | ||
| 18 | use Term::Choose::Constants qw( EXTRA_W ); | ||
| 19 | use Term::Choose::LineFold qw( line_fold ); | ||
| 20 | use Term::Choose::Screen qw( clear_screen ); | ||
| 21 | use Term::Choose::Util qw( insert_sep get_term_width get_term_height unicode_sprintf ); | ||
| 22 | use Term::Form::ReadLine qw(); | ||
| 23 | |||
| 24 | |||
| 25 | sub new { | ||
| 26 | my ( $class, $info, $options, $d ) = @_; | ||
| 27 | bless { | ||
| 28 | i => $info, | ||
| 29 | o => $options, | ||
| 30 | d => $d | ||
| 31 | }, $class; | ||
| 32 | } | ||
| 33 | |||
| 34 | |||
| 35 | sub reset_sql { | ||
| 36 | my ( $sf, $sql ) = @_; | ||
| 37 | # preserve base data: table name, column names and data types: | ||
| 38 | my $backup = { | ||
| 39 | table => $sql->{table} // '', | ||
| 40 | columns => $sql->{columns} // [], | ||
| 41 | data_types => $sql->{data_types} // {}, | ||
| 42 | }; | ||
| 43 | # reset: | ||
| 44 | delete @{$sql}{ keys %$sql }; # not "$sql = {}" so $sql is still pointing to the outer $sql | ||
| 45 | # initialize: | ||
| 46 | my @string = qw( distinct_stmt set_stmt where_stmt having_stmt order_by_stmt limit_stmt offset_stmt ); | ||
| 47 | my @array = qw( group_by_cols selected_cols set_args order_by_cols | ||
| 48 | ct_column_definitions ct_table_constraints ct_table_options | ||
| 49 | insert_col_names insert_args ); | ||
| 50 | my @hash = qw( alias ); | ||
| 51 | @{$sql}{@string} = ( '' ) x @string; | ||
| 52 | @{$sql}{@array} = map{ [] } @array; | ||
| 53 | @{$sql}{@hash} = map{ {} } @hash; | ||
| 54 | for my $y ( keys %$backup ) { | ||
| 55 | $sql->{$y} = $backup->{$y}; | ||
| 56 | } | ||
| 57 | } | ||
| 58 | |||
| 59 | |||
| 60 | sub __stmt_fold { | ||
| 61 | my ( $sf, $term_w, $used_for, $stmt, $indent ) = @_; | ||
| 62 | if ( $used_for eq 'print' ) { | ||
| 63 | my $in = ' ' x $sf->{o}{G}{base_indent}; | ||
| 64 | my %tabs = ( init_tab => $in x $indent, subseq_tab => $in x ( $indent + 1 ) ); | ||
| 65 | return line_fold( $stmt, { width => $term_w, %tabs, join => 0 } ); | ||
| 66 | } | ||
| 67 | else { | ||
| 68 | return $stmt; | ||
| 69 | } | ||
| 70 | } | ||
| 71 | |||
| 72 | |||
| 73 | sub __select_cols { | ||
| 74 | my ( $sf, $sql ) = @_; | ||
| 75 | my @cols; | ||
| 76 | if ( @{$sql->{selected_cols}} ) { | ||
| 77 | @cols = @{$sql->{selected_cols}}; | ||
| 78 | } | ||
| 79 | elsif ( keys %{$sql->{alias}} && ! $sql->{aggregate_mode} ) { | ||
| 80 | @cols = @{$sql->{columns}}; | ||
| 81 | # use column names and not * if columns have aliases (join) | ||
| 82 | # unless aggregate_mode (columns are aggregate functions and group by columns) ## | ||
| 83 | } | ||
| 84 | if ( ! @cols ) { | ||
| 85 | return "" if $sql->{aggregate_mode}; | ||
| 86 | return " *"; | ||
| 87 | } | ||
| 88 | elsif ( ! keys %{$sql->{alias}} ) { | ||
| 89 | return ' ' . join ', ', @cols; | ||
| 90 | } | ||
| 91 | else { | ||
| 92 | return ' ' . join ', ', map { length $sql->{alias}{$_} ? "$_ AS $sql->{alias}{$_}" : $_ } @cols; | ||
| 93 | } | ||
| 94 | } | ||
| 95 | |||
| 96 | |||
| 97 | sub __group_by_stmt { | ||
| 98 | my ( $sf, $sql ) = @_; | ||
| 99 | my $aliases = $sf->{o}{alias}{use_in_group_by} ? $sql->{alias} : {}; | ||
| 100 | return "GROUP BY " . join ', ', map { length $aliases->{$_} ? $aliases->{$_} : $_ } @{$sql->{group_by_cols}}; | ||
| 101 | } | ||
| 102 | |||
| 103 | |||
| 104 | sub cte_stmts { | ||
| 105 | my ( $sf, $used_for, $indent1 ) = @_; | ||
| 106 | if ( ! @{$sf->{d}{cte_history}//[]} ) { | ||
| 107 | return; | ||
| 108 | } | ||
| 109 | if ( length( $sf->{d}{main_info} ) && $used_for eq 'print' ) { ## | ||
| 110 | # else the cte definitions would be printed twice if a cte is used inside a cte. | ||
| 111 | return; | ||
| 112 | } | ||
| 113 | my $ctes = $sf->{d}{cte_history}; | ||
| 114 | my $with = "WITH"; | ||
| 115 | for my $cte ( @$ctes ) { | ||
| 116 | $with .= " RECURSIVE" and last if $cte->{is_recursive}; | ||
| 117 | } | ||
| 118 | my $term_w = get_term_width() + EXTRA_W; | ||
| 119 | my @tmp = ( $with ); | ||
| 120 | for my $cte ( @$ctes ) { | ||
| 121 | push @tmp, $sf->__stmt_fold( $term_w, $used_for, sprintf( '%s AS (%s),', $cte->{full_name}, $cte->{query} ), $indent1 ); | ||
| 122 | } | ||
| 123 | $tmp[-1] =~ s/,\z//; | ||
| 124 | return join( "\n", @tmp ) . "\n"; | ||
| 125 | |||
| 126 | } | ||
| 127 | |||
| 128 | |||
| 129 | sub get_stmt { | ||
| 130 | my ( $sf, $sql, $stmt_type, $used_for ) = @_; | ||
| 131 | my $term_w = get_term_width() + EXTRA_W; | ||
| 132 | my $in = ' ' x $sf->{o}{G}{base_indent}; | ||
| 133 | my $indent0 = 0; | ||
| 134 | my $indent1 = 1; | ||
| 135 | my $indent2 = 2; | ||
| 136 | my $table = $sql->{table}; | ||
| 137 | my @tmp; | ||
| 138 | my $ctes = $sf->cte_stmts( $used_for, $indent1 ); | ||
| 139 | if ( defined $ctes ) { | ||
| 140 | push @tmp, $ctes; | ||
| 141 | } | ||
| 142 | if ( $stmt_type eq 'Cte_Only' ) { | ||
| 143 | if ( ! @tmp ) { | ||
| 144 | push @tmp, 'WITH'; | ||
| 145 | } | ||
| 146 | else { | ||
| 147 | $tmp[-1] =~ s/\n\z//; | ||
| 148 | } | ||
| 149 | } | ||
| 150 | elsif ( $stmt_type eq 'Drop_Table' ) { | ||
| 151 | @tmp = ( $sf->__stmt_fold( $term_w, $used_for, "DROP TABLE $table", $indent0 ) ); | ||
| 152 | } | ||
| 153 | elsif ( $stmt_type eq 'Drop_View' ) { | ||
| 154 | @tmp = ( $sf->__stmt_fold( $term_w, $used_for, "DROP VIEW $table", $indent0 ) ); | ||
| 155 | } | ||
| 156 | elsif ( $stmt_type eq 'Create_Table' ) { | ||
| 157 | my $stmt = sprintf "CREATE TABLE $table (%s)", join ', ', @{$sql->{ct_column_definitions}}, @{$sql->{ct_table_constraints}}; | ||
| 158 | if ( @{$sql->{ct_table_options}} ) { | ||
| 159 | $stmt .= ' ' . join ', ', @{$sql->{ct_table_options}}; | ||
| 160 | } | ||
| 161 | @tmp = ( $sf->__stmt_fold( $term_w, $used_for, $stmt, $indent0 ) ); | ||
| 162 | } | ||
| 163 | elsif ( $stmt_type eq 'Create_View' ) { | ||
| 164 | @tmp = ( $sf->__stmt_fold( $term_w, $used_for, "CREATE VIEW $table", $indent0 ) ); | ||
| 165 | push @tmp, $sf->__stmt_fold( $term_w, $used_for, "AS " . $sql->{view_select_stmt}, $indent1 ); | ||
| 166 | } | ||
| 167 | elsif ( $stmt_type eq 'Select' ) { | ||
| 168 | push @tmp, $sf->__stmt_fold( $term_w, $used_for, "SELECT" . $sql->{distinct_stmt} . $sf->__select_cols( $sql ), $indent0 ); | ||
| 169 | push @tmp, $sf->__stmt_fold( $term_w, $used_for, "FROM " . $table, $indent1 ); | ||
| 170 | push @tmp, $sf->__stmt_fold( $term_w, $used_for, $sql->{where_stmt}, $indent2 ) if $sql->{where_stmt}; | ||
| 171 | push @tmp, $sf->__stmt_fold( $term_w, $used_for, $sf->__group_by_stmt( $sql ), $indent2 ) if @{$sql->{group_by_cols}}; | ||
| 172 | push @tmp, $sf->__stmt_fold( $term_w, $used_for, $sql->{having_stmt}, $indent2 ) if $sql->{having_stmt}; | ||
| 173 | push @tmp, $sf->__stmt_fold( $term_w, $used_for, $sql->{order_by_stmt}, $indent2 ) if $sql->{order_by_stmt}; | ||
| 174 | if ( $sql->{limit_stmt} =~ /^LIMIT\b/ ) { | ||
| 175 | push @tmp, $sf->__stmt_fold( $term_w, $used_for, $sql->{limit_stmt}, $indent2 ); | ||
| 176 | push @tmp, $sf->__stmt_fold( $term_w, $used_for, $sql->{offset_stmt}, $indent2 ) if $sql->{offset_stmt}; | ||
| 177 | } | ||
| 178 | else { | ||
| 179 | push @tmp, $sf->__stmt_fold( $term_w, $used_for, $sql->{offset_stmt}, $indent2 ) if $sql->{offset_stmt}; | ||
| 180 | push @tmp, $sf->__stmt_fold( $term_w, $used_for, $sql->{limit_stmt}, $indent2 ) if $sql->{limit_stmt}; | ||
| 181 | } | ||
| 182 | } | ||
| 183 | elsif ( $stmt_type eq 'Delete' ) { | ||
| 184 | push @tmp, $sf->__stmt_fold( $term_w, $used_for, "DELETE FROM " . $table, $indent0 ); | ||
| 185 | push @tmp, $sf->__stmt_fold( $term_w, $used_for, $sql->{where_stmt}, $indent1 ) if $sql->{where_stmt}; | ||
| 186 | } | ||
| 187 | elsif ( $stmt_type eq 'Update' ) { | ||
| 188 | push @tmp, $sf->__stmt_fold( $term_w, $used_for, "UPDATE " . $table, $indent0 ); | ||
| 189 | push @tmp, $sf->__stmt_fold( $term_w, $used_for, $sql->{set_stmt}, $indent1 ) if $sql->{set_stmt}; | ||
| 190 | push @tmp, $sf->__stmt_fold( $term_w, $used_for, $sql->{where_stmt}, $indent1 ) if $sql->{where_stmt}; | ||
| 191 | } | ||
| 192 | elsif ( $stmt_type eq 'Insert' ) { | ||
| 193 | my $columns = join ', ', map { $_ // '' } @{$sql->{insert_col_names}}; | ||
| 194 | my $placeholders = join ', ', ( '?' ) x @{$sql->{insert_col_names}}; | ||
| 195 | my $stmt = "INSERT INTO $sql->{table} ($columns) VALUES($placeholders)"; | ||
| 196 | @tmp = ( $sf->__stmt_fold( $term_w, $used_for, $stmt, $indent0 ) ); | ||
| 197 | if ( $used_for eq 'print' ) { | ||
| 198 | my $arg_rows = $sf->info_format_insert_args( $sql, $in x 2 ); | ||
| 199 | push @tmp, @$arg_rows; | ||
| 200 | } | ||
| 201 | } | ||
| 202 | elsif ( $stmt_type eq 'Join' ) { | ||
| 203 | my $select_from; | ||
| 204 | if ( $used_for eq 'prepare' ) { | ||
| 205 | @tmp = (); | ||
| 206 | # prepare: this stmt is used as table in the select stmt | ||
| 207 | # no ctes, they are added in the select stmt | ||
| 208 | $select_from = ""; | ||
| 209 | } | ||
| 210 | else { | ||
| 211 | $select_from = "SELECT * FROM "; | ||
| 212 | } | ||
| 213 | my @join_data = @{$sql->{join_data}//[]}; | ||
| 214 | if ( @join_data ) { | ||
| 215 | my $master_data = shift @join_data; | ||
| 216 | push @tmp, $sf->__stmt_fold( $term_w, $used_for, $select_from . $master_data->{table}, $indent0 ); | ||
| 217 | if ( @join_data ) { | ||
| 218 | my $last_table = pop @join_data; | ||
| 219 | for my $slave_data ( @join_data ) { | ||
| 220 | my $sub_stmt = join ' ', grep { length } @{$slave_data}{ qw(join_type table condition) }; | ||
| 221 | push @tmp, $sf->__stmt_fold( $term_w, $used_for, $sub_stmt, $indent1 ); | ||
| 222 | } | ||
| 223 | my $sub_stmt = join ' ', grep { length } @{$last_table}{ qw(join_type table condition) }, $sql->{on_stmt}; | ||
| 224 | push @tmp, $sf->__stmt_fold( $term_w, $used_for, $sub_stmt, $indent1 ); # either condition or on_stmt | ||
| 225 | } | ||
| 226 | } | ||
| 227 | else { | ||
| 228 | push @tmp, $sf->__stmt_fold( $term_w, $used_for, $select_from, $indent0 ); | ||
| 229 | } | ||
| 230 | } | ||
| 231 | elsif ( $stmt_type eq 'Union' ) { | ||
| 232 | if ( $used_for eq 'prepare' ) { | ||
| 233 | @tmp = (); | ||
| 234 | # prepare: this stmt is used as a table in the select stmt | ||
| 235 | # @tmp = (): no ctes, they are added in the select stmt | ||
| 236 | push @tmp, "("; | ||
| 237 | if ( @{$sql->{subselect_stmts}//[]} ) { | ||
| 238 | my $extra = 0; | ||
| 239 | for my $stmt ( @{$sql->{subselect_stmts}} ) { | ||
| 240 | $extra-- if $stmt eq ")" && $extra; | ||
| 241 | my $indent = $in x ( 1 + $extra ); | ||
| 242 | push @tmp, $indent . $stmt; | ||
| 243 | $extra++ if $stmt eq "("; | ||
| 244 | } | ||
| 245 | } | ||
| 246 | push @tmp, ")"; | ||
| 247 | } | ||
| 248 | else { | ||
| 249 | push @tmp, $sf->__stmt_fold( $term_w, $used_for, "SELECT *", $indent0 ); ## | ||
| 250 | push @tmp, $sf->__stmt_fold( $term_w, $used_for, "FROM ()", $indent1 ); ## | ||
| 251 | if ( @{$sql->{subselect_stmts}//[]} ) { | ||
| 252 | push @tmp, ' '; | ||
| 253 | my $extra = 0; | ||
| 254 | for my $stmt ( @{$sql->{subselect_stmts}} ) { | ||
| 255 | $extra-- if $stmt eq ")" && $extra; | ||
| 256 | push @tmp, $sf->__stmt_fold( $term_w, $used_for, $stmt, $indent0 + $extra ); | ||
| 257 | $extra++ if $stmt eq "("; | ||
| 258 | } | ||
| 259 | } | ||
| 260 | } | ||
| 261 | } | ||
| 262 | my $stmt = join( "\n", @tmp ); | ||
| 263 | if ( $used_for eq 'print' ) { | ||
| 264 | if ( length $sf->{d}{main_info} ) { | ||
| 265 | my $sq_indent = $in; | ||
| 266 | $stmt =~ s/(^|\n)/$1$sq_indent/gs; | ||
| 267 | $stmt = $sf->{d}{main_info} . "\n" . $stmt; | ||
| 268 | } | ||
| 269 | $stmt .= "\n"; | ||
| 270 | } | ||
| 271 | return $stmt; | ||
| 272 | } | ||
| 273 | |||
| 274 | |||
| 275 | sub info_format_insert_args { | ||
| 276 | my ( $sf, $sql, $indent ) = @_; | ||
| 277 | my $term_h = get_term_height(); | ||
| 278 | my $term_w = get_term_width() + EXTRA_W; | ||
| 279 | my $row_count = @{$sql->{insert_args}}; | ||
| 280 | if ( $row_count == 0 ) { | ||
| 281 | return []; | ||
| 282 | } | ||
| 283 | my $col_count = 0; ## | ||
| 284 | if ( $sf->{d}{stmt_types}[0] && $sf->{d}{stmt_types}[0] eq 'Create_Table' ) { | ||
| 285 | $col_count = @{$sql->{insert_args}[0]//[]}; | ||
| 286 | #$col_count = @{$sql->{ct_column_definitions//[]}}; | ||
| 287 | $col_count += 1 + $sf->{o}{create}{table_constraint_rows} if $sf->{o}{create}{table_constraint_rows}; | ||
| 288 | $col_count += 1 + $sf->{o}{create}{table_option_rows} if $sf->{o}{create}{table_option_rows}; | ||
| 289 | $col_count += 12; | ||
| 290 | if ( $col_count < 22 ) { | ||
| 291 | $col_count = 22; | ||
| 292 | } | ||
| 293 | } | ||
| 294 | else { | ||
| 295 | $col_count = 22; | ||
| 296 | } | ||
| 297 | my $avail_h = $term_h - $col_count; | ||
| 298 | if ( $avail_h < $term_h / 3.5 ) { | ||
| 299 | $avail_h = int $term_h / 3.5; | ||
| 300 | } | ||
| 301 | if ( $avail_h < 5) { | ||
| 302 | $avail_h = 5; | ||
| 303 | } | ||
| 304 | my $tmp = []; | ||
| 305 | if ( $row_count > $avail_h ) { | ||
| 306 | $avail_h -= 2; # for "[...]" + "[count rows]" | ||
| 307 | my $count_part_1 = int( $avail_h / 1.5 ); | ||
| 308 | my $count_part_2 = $avail_h - $count_part_1; | ||
| 309 | my $begin_idx_part_1 = 0; | ||
| 310 | my $end___idx_part_1 = $count_part_1 - 1; | ||
| 311 | my $begin_idx_part_2 = $row_count - $count_part_2; | ||
| 312 | my $end___idx_part_2 = $row_count - 1; | ||
| 313 | for my $row ( @{$sql->{insert_args}}[ $begin_idx_part_1 .. $end___idx_part_1 ] ) { | ||
| 314 | push @$tmp, $sf->__prepare_data_row( $row, $indent, $term_w ); | ||
| 315 | } | ||
| 316 | push @$tmp, $indent . '[...]'; | ||
| 317 | for my $row ( @{$sql->{insert_args}}[ $begin_idx_part_2 .. $end___idx_part_2 ] ) { | ||
| 318 | push @$tmp, $sf->__prepare_data_row( $row, $indent, $term_w ); | ||
| 319 | } | ||
| 320 | my $row_count = scalar( @{$sql->{insert_args}} ); | ||
| 321 | push @$tmp, $indent . '[' . insert_sep( $row_count, $sf->{i}{info_thsd_sep} ) . ' rows]'; | ||
| 322 | } | ||
| 323 | else { | ||
| 324 | for my $row ( @{$sql->{insert_args}} ) { | ||
| 325 | push @$tmp, $sf->__prepare_data_row( $row, $indent, $term_w ); | ||
| 326 | } | ||
| 327 | } | ||
| 328 | return $tmp; | ||
| 329 | } | ||
| 330 | |||
| 331 | |||
| 332 | sub __prepare_data_row { | ||
| 333 | my ( $sf, $row, $indent, $term_w ) = @_; | ||
| 334 | my $list_sep = ', '; | ||
| 335 | no warnings 'uninitialized'; | ||
| 336 | my $row_str = join( $list_sep, map { s/\t/ /g; s/\n/\\n/g; s/\v/\\v/g; $_ } @$row ); | ||
| 337 | return unicode_sprintf( $indent . $row_str, $term_w, { suffix_on_truncate => $sf->{i}{dots} } ); | ||
| 338 | } | ||
| 339 | |||
| 340 | |||
| 341 | sub print_sql_info { | ||
| 342 | my ( $sf, $info, $waiting ) = @_; | ||
| 343 | if ( ! defined $info ) { | ||
| 344 | return; | ||
| 345 | } | ||
| 346 | print clear_screen(); | ||
| 347 | print $info, "\n"; | ||
| 348 | if ( defined $waiting ) { | ||
| 349 | print $waiting . "\r"; | ||
| 350 | } | ||
| 351 | } | ||
| 352 | |||
| 353 | |||
| 354 | sub get_sql_info { | ||
| 355 | my ( $sf, $sql ) = @_; | ||
| 356 | my $stmt = ''; | ||
| 357 | for my $stmt_type ( @{$sf->{d}{stmt_types}} ) { | ||
| 358 | $stmt .= $sf->get_stmt( $sql, $stmt_type, 'print' ); | ||
| 359 | } | ||
| 360 | return $stmt; | ||
| 361 | } | ||
| 362 | |||
| 363 | |||
| 364 | sub sql_limit { | ||
| 365 | my ( $sf, $rows ) = @_; | ||
| 366 | my $driver = $sf->{i}{driver}; # Use driver so that dbms remains optional. | ||
| 367 | if ( $driver =~ /^(?:SQLite|mysql|MariaDB|Pg|DuckDB)\z/ ) { | ||
| 368 | return " LIMIT $rows"; | ||
| 369 | } | ||
| 370 | elsif ( $driver =~ /^(?:Firebird|DB2|Oracle)\z/ ) { | ||
| 371 | return " FETCH NEXT $rows ROWS ONLY" | ||
| 372 | } | ||
| 373 | else { | ||
| 374 | return ""; | ||
| 375 | } | ||
| 376 | } | ||
| 377 | #sub sql_limit { | ||
| 378 | # my ( $sf, $rows ) = @_; | ||
| 379 | # my $dbms = $sf->{i}{dbms}; | ||
| 380 | # if ( $dbms =~ /^(?:SQLite|mysql|MariaDB|Pg|DuckDB)\z/ ) { | ||
| 381 | # return " LIMIT $rows"; | ||
| 382 | # } | ||
| 383 | # elsif ( $dbms =~ /^(?:Firebird|DB2|Oracle)\z/ ) { | ||
| 384 | # return " FETCH NEXT $rows ROWS ONLY" | ||
| 385 | # } | ||
| 386 | # elsif ( $dbms eq 'MSSQL' ) { | ||
| 387 | # return " OFFSET 0 ROWS FETCH NEXT $rows ROWS ONLY" | ||
| 388 | # } | ||
| 389 | # else { | ||
| 390 | # return ""; | ||
| 391 | # } | ||
| 392 | #} | ||
| 393 | |||
| 394 | |||
| 395 | |||
| 396 | sub column_names_and_types { | ||
| 397 | my ( $sf, $table ) = @_; | ||
| 398 | my $driver = $sf->{i}{driver}; | ||
| 399 | # without `LIMIT 0` slower with big tables: mysql, MariaDB and Pg | ||
| 400 | # no difference with SQLite, Firebird, DB2 and Informix | ||
| 401 | my $column_names = []; | ||
| 402 | my $column_types = []; | ||
| 403 | if ( ! eval { | ||
| 404 | my $stmt = ''; | ||
| 405 | my $ctes = $sf->cte_stmts( 'prepare', 0 ); | ||
| 406 | if ( defined $ctes ) { | ||
| 407 | $stmt = $ctes; | ||
| 408 | } | ||
| 409 | $stmt .= "SELECT * FROM " . $table . $sf->sql_limit( 0 ); | ||
| 410 | my $sth = $sf->{d}{dbh}->prepare( $stmt ); | ||
| 411 | if ( $driver eq 'SQLite' ) { | ||
| 412 | $column_names = [ @{$sth->{NAME}} ]; | ||
| 413 | if ( $sf->{d}{dbh}{sqlite_see_if_its_a_number} ) { | ||
| 414 | $column_types = []; | ||
| 415 | } | ||
| 416 | else { | ||
| 417 | my $rx_numeric = 'INTEGER|INT$|DOUBLE|REAL|NUM|FLOAT|DEC|BOOL|BIT|MONEY'; | ||
| 418 | $column_types = [ map { ! $_ || $_ =~ /$rx_numeric/i ? 2 : 1 } @{$sth->{TYPE}} ]; | ||
| 419 | } | ||
| 420 | } | ||
| 421 | elsif ( $driver eq 'DB2' && length $sf->{o}{G}{db2_encoding} ) { | ||
| 422 | $sth->execute(); | ||
| 423 | my $encoding = Encode::find_encoding( $sf->{o}{G}{db2_encoding} ); | ||
| 424 | if ( ! ref $encoding ) { | ||
| 425 | die "Unknown encoding: $sf->{o}{G}{db2_encoding}"; | ||
| 426 | } | ||
| 427 | $column_names = [ map { $encoding->decode( $_ ) } @{$sth->{NAME}} ]; | ||
| 428 | $column_types = [ @{$sth->{TYPE}} ]; | ||
| 429 | } | ||
| 430 | elsif ( $driver eq 'DuckDB' && $sf->{o}{G}{duckdb_utf8_decode} ) { | ||
| 431 | $sth->execute(); | ||
| 432 | $column_names = [ map { decode( 'UTF-8', $_ ) } @{$sth->{NAME}} ]; | ||
| 433 | $column_types = [ @{$sth->{TYPE}} ]; | ||
| 434 | } | ||
| 435 | else { | ||
| 436 | $sth->execute(); | ||
| 437 | $column_names = [ @{$sth->{NAME}} ]; | ||
| 438 | $column_types = [ @{$sth->{TYPE}} ]; | ||
| 439 | } | ||
| 440 | 1 } | ||
| 441 | ) { | ||
| 442 | $sf->print_error_message( $@ ); | ||
| 443 | return; | ||
| 444 | } | ||
| 445 | $column_names = $sf->quote_cols( $column_names ); | ||
| 446 | return $column_names, $column_types; | ||
| 447 | } | ||
| 448 | |||
| 449 | |||
| 450 | sub is_numeric { | ||
| 451 | my ( $sf, $sql, $col ) = @_; | ||
| 452 | return -1 if ! length $sql->{data_types}{$col}; | ||
| 453 | return 1 if $sql->{data_types}{$col} >= 2 && $sql->{data_types}{$col} <= 8; | ||
| 454 | return 0; | ||
| 455 | } | ||
| 456 | |||
| 457 | |||
| 458 | sub pg_column_to_text { | ||
| 459 | my ( $sf, $sql, $col ) = @_; | ||
| 460 | return $col if ! $sf->{o}{G}{pg_autocast}; | ||
| 461 | return $col if defined $sql->{data_types}{$col} && ( $sql->{data_types}{$col} == 1 || $sql->{data_types}{$col} == 12 ); | ||
| 462 | return $col if $col =~ /^(?:CONCAT|LEFT|LOWER|LPAD|LTRIM|REPLACE|REVERSE|RIGHT|RPAD|RTRIM|SUBSTRING|SUBSTR|TRIM|UPPER|TO_CHAR)\(/; | ||
| 463 | return $col . "::text"; | ||
| 464 | } | ||
| 465 | |||
| 466 | |||
| 467 | sub table_alias { | ||
| 468 | my ( $sf, $sql, $type, $table, $default ) = @_; | ||
| 469 | # | ||
| 470 | # Aliases mandatory: | ||
| 471 | # JOIN talbes | ||
| 472 | # Derived Tables: mysql, MariaDB, Pg | ||
| 473 | # | ||
| 474 | my $bu_default_table_alias_count = $sf->{d}{default_table_alias_count}; | ||
| 475 | my $auto_default = 't' . ++$sf->{d}{default_table_alias_count}; | ||
| 476 | $default //= $auto_default; | ||
| 477 | my $alias = $sf->alias( $sql, $type, $table, $default ); | ||
| 478 | if ( ! length $alias ) { | ||
| 479 | $sf->{d}{default_table_alias_count} = $bu_default_table_alias_count; | ||
| 480 | return; | ||
| 481 | } | ||
| 482 | if ( $alias ne $sf->quote_alias( $auto_default ) ) { | ||
| 483 | $sf->{d}{default_table_alias_count} = $bu_default_table_alias_count; | ||
| 484 | } | ||
| 485 | if ( none { $_ eq $alias } @{$sf->{d}{table_aliases}{$table}} ) { | ||
| 486 | push @{$sf->{d}{table_aliases}{$table}}, $alias; | ||
| 487 | } | ||
| 488 | return $alias; | ||
| 489 | } | ||
| 490 | |||
| 491 | |||
| 492 | sub alias { | ||
| 493 | my ( $sf, $sql, $type, $identifier, $default ) = @_; | ||
| 494 | # 0 = NO | ||
| 495 | # 1 = AUTO | ||
| 496 | # 2 = ASK | ||
| 497 | # 3 = ASK/AUTO | ||
| 498 | my $alias; | ||
| 499 | if ( $sf->{o}{alias}{$type} == 0 ) { | ||
| 500 | return; | ||
| 501 | } | ||
| 502 | elsif ( $sf->{o}{alias}{$type} == 1 ) { | ||
| 503 | $alias = $default; | ||
| 504 | } | ||
| 505 | elsif ( $sf->{o}{alias}{$type} == 2 || $sf->{o}{alias}{$type} == 3 ) { | ||
| 506 | my $tr = Term::Form::ReadLine->new( $sf->{i}{tr_default} ); | ||
| 507 | my $info = $sf->get_sql_info( $sql ); | ||
| 508 | $info .= $identifier =~ /^\n/ ? $identifier : "\n$identifier"; # case | ||
| 509 | # Readline | ||
| 510 | $alias = $tr->readline( | ||
| 511 | 'as ', | ||
| 512 | #{ info => $info, history => [ 'a' .. 'z' ] } | ||
| 513 | { info => $info, history => [ 'a' .. 'z' ], default => $sql->{alias}{$identifier} } ## | ||
| 514 | ); | ||
| 515 | $sf->print_sql_info( $info ); | ||
| 516 | if ( $sf->{o}{alias}{$type} == 3 && ! length $alias ) { | ||
| 517 | $alias = $default; | ||
| 518 | } | ||
| 519 | } | ||
| 520 | if ( length $alias ) { | ||
| 521 | $alias = $sf->quote_alias( $alias ); | ||
| 522 | return $alias; | ||
| 523 | } | ||
| 524 | return; | ||
| 525 | } | ||
| 526 | |||
| 527 | |||
| 528 | sub qualified_identifier { | ||
| 529 | my ( $sf, @id ) = @_; | ||
| 530 | # my $catalog = ( @id >= 3 ) ? shift @id : undef; # catalog not used (if used, uncomment also catalog_location and catalog_name_sep) | ||
| 531 | my $qualified_id = join '.', grep { defined } @id; | ||
| 532 | # if ( $catalog ) { | ||
| 533 | # if ( $qualified_id ) { | ||
| 534 | # $qualified_id = ( $sf->{d}{catalog_location} == 2 ) | ||
| 535 | # ? $qualified_id . $sf->{d}{catalog_name_sep} . $catalog | ||
| 536 | # : $catalog . $sf->{d}{catalog_name_sep} . $qualified_id; | ||
| 537 | # } else { | ||
| 538 | # $qualified_id = $catalog; | ||
| 539 | # } | ||
| 540 | # } | ||
| 541 | return $qualified_id; | ||
| 542 | } | ||
| 543 | |||
| 544 | |||
| 545 | sub __quote_identifiers { | ||
| 546 | my ( $sf, @identifier ) = @_; | ||
| 547 | my $quote = $sf->{d}{identifier_quote_char}; | ||
| 548 | for ( @identifier ) { | ||
| 549 | if ( ! defined ) { | ||
| 550 | next; | ||
| 551 | } | ||
| 552 | $_ =~ s/$quote/$quote$quote/g; | ||
| 553 | $_ = qq{$quote$_$quote}; | ||
| 554 | } | ||
| 555 | return @identifier; | ||
| 556 | } | ||
| 557 | |||
| 558 | |||
| 559 | sub qq_table { | ||
| 560 | my ( $sf, $table_info ) = @_; | ||
| 561 | my $dbms = $sf->{i}{dbms}; | ||
| 562 | # 0 = catalog, 1 = schema, 2 = table_name, 3 = table_type | ||
| 563 | my @idx; | ||
| 564 | if ( $sf->{d}{db_attached} ) { | ||
| 565 | # If a SQLite database has databases attached, the fully qualified table name is used in SQL code regardless of | ||
| 566 | # the setting of the option 'qualified_table_name' because attached databases could have tables with the same | ||
| 567 | # name. | ||
| 568 | if ( $dbms eq 'SQLite' ) { | ||
| 569 | @idx = ( 1, 2 ); | ||
| 570 | } | ||
| 571 | elsif ( $dbms eq 'DuckDB' ) { | ||
| 572 | @idx = ( 0, 2 ); | ||
| 573 | } | ||
| 574 | #@idx = ( 0, 1, 2 ); # both | ||
| 575 | } | ||
| 576 | elsif ( $sf->{o}{G}{qualified_table_name} ) { | ||
| 577 | @idx = ( 1, 2 ); | ||
| 578 | } | ||
| 579 | else { | ||
| 580 | @idx = ( 2 ); | ||
| 581 | } | ||
| 582 | if ( $sf->{o}{G}{quote_tables} ) { | ||
| 583 | return $sf->qualified_identifier( $sf->__quote_identifiers( @{$table_info}[ @idx ] ) ); | ||
| 584 | } | ||
| 585 | else { | ||
| 586 | return $sf->qualified_identifier( @{$table_info}[@idx] ); | ||
| 587 | } | ||
| 588 | } | ||
| 589 | |||
| 590 | |||
| 591 | sub quote_table { | ||
| 592 | my ( $sf, $table ) = @_; | ||
| 593 | if ( $sf->{o}{G}{quote_tables} ) { | ||
| 594 | ( $table ) = $sf->__quote_identifiers( $table ); | ||
| 595 | } | ||
| 596 | return $table; | ||
| 597 | } | ||
| 598 | |||
| 599 | |||
| 600 | sub quote_column { | ||
| 601 | my ( $sf, $column ) = @_; | ||
| 602 | if ( $sf->{o}{G}{quote_columns} ) { | ||
| 603 | ( $column ) = $sf->__quote_identifiers( $column ); | ||
| 604 | } | ||
| 605 | return $column; | ||
| 606 | } | ||
| 607 | |||
| 608 | |||
| 609 | sub quote_cols { | ||
| 610 | my ( $sf, $cols ) = @_; | ||
| 611 | if ( $sf->{o}{G}{quote_columns} ) { | ||
| 612 | $cols = [ $sf->__quote_identifiers( @$cols ) ]; | ||
| 613 | } | ||
| 614 | return $cols; | ||
| 615 | } | ||
| 616 | |||
| 617 | |||
| 618 | sub quote_alias { ## | ||
| 619 | my ( $sf, $alias ) = @_; | ||
| 620 | #if ( $sf->{o}{G}{quote_aliases} ) { | ||
| 621 | if ( $sf->{o}{G}{quote_columns} ) { | ||
| 622 | ( $alias ) = $sf->__quote_identifiers( $alias ); | ||
| 623 | } | ||
| 624 | return $alias; | ||
| 625 | } | ||
| 626 | |||
| 627 | |||
| 628 | sub unquote_identifier { | ||
| 629 | my ( $sf, $identifier ) = @_; | ||
| 630 | my $qc = quotemeta( $sf->{d}{identifier_quote_char} ); | ||
| 631 | $identifier =~ s/$qc(?=(?:$qc$qc)*(?:[^$qc]|\z))//g; | ||
| 632 | return $identifier; | ||
| 633 | } | ||
| 634 | |||
| 635 | |||
| 636 | sub quote_if_not_numeric { | ||
| 637 | my ( $sf, $value ) = @_; | ||
| 638 | if ( looks_like_number $value ) { | ||
| 639 | return $value; | ||
| 640 | } | ||
| 641 | else { | ||
| 642 | return $sf->{d}{dbh}->quote( $value ); | ||
| 643 | } | ||
| 644 | } | ||
| 645 | |||
| 646 | |||
| 647 | sub unquote_constant { | ||
| 648 | my ( $sf, $constant ) = @_; | ||
| 649 | return if ! defined $constant; | ||
| 650 | if ( $constant =~ /^'(.*)'\z/ ) { | ||
| 651 | $constant = $1; | ||
| 652 | if ( $sf->{i}{dbms} =~ /^(?:mysql|MariaDB)\z/ ) { | ||
| 653 | $constant =~ s/\\(.)/$1/g; | ||
| 654 | } | ||
| 655 | else { | ||
| 656 | $constant =~ s/''/'/g; | ||
| 657 | #$constant =~ s/'(?=(?:'')*(?:[^']|\z))//g; | ||
| 658 | } | ||
| 659 | } | ||
| 660 | return $constant; | ||
| 661 | } | ||
| 662 | |||
| 663 | |||
| 664 | sub regex_quoted_literal { | ||
| 665 | my ( $sf ) = @_; | ||
| 666 | if ( $sf->{i}{dbms} =~ /^(?:mysql|MariaDB)\z/ ) { | ||
| 667 | return qr/(?<!')'(?:[^\\']|\\'|\\\\)*'(?!')/; | ||
| 668 | } | ||
| 669 | else { | ||
| 670 | return qr/(?<!')'(?:[^']|'')*'(?!')/; | ||
| 671 | } | ||
| 672 | } | ||
| 673 | |||
| 674 | |||
| 675 | sub regex_quoted_identifier { | ||
| 676 | my ( $sf ) = @_; | ||
| 677 | my $iqc = $sf->{d}{identifier_quote_char}; | ||
| 678 | return "$iqc(?:[^$iqc]|$iqc$iqc)+$iqc"; | ||
| 679 | } | ||
| 680 | |||
| 681 | |||
| 682 | sub normalize_space_in_stmt { | ||
| 683 | my ( $sf, $stmt ) = @_; | ||
| 684 | my $quoted_literal = $sf->regex_quoted_literal(); | ||
| 685 | my $iqc = $sf->{d}{identifier_quote_char}; | ||
| 686 | my $quoted_identifier = $sf->regex_quoted_identifier(); | ||
| 687 | my $split_rx = qr/ ( $quoted_identifier | $quoted_literal ) /x; | ||
| 688 | $stmt =~ s/^\s+|\s+\z//g; | ||
| 689 | $stmt = join '', map { | ||
| 690 | if ( ! /^[$iqc']/ ) { s/\s+/ /g; s|\(\s|(|; s|\s\)|)| }; | ||
| 691 | $_ | ||
| 692 | } split $split_rx, $stmt; | ||
| 693 | return $stmt; | ||
| 694 | } | ||
| 695 | |||
| 696 | |||
| 697 | sub major_server_version { | ||
| 698 | my ( $sf ) = @_; | ||
| 699 | my $dbms = $sf->{i}{dbms}; | ||
| 700 | my $dbh = $sf->{d}{dbh}; | ||
| 701 | my $major_server_version; | ||
| 702 | if ( $dbms eq 'Firebird' ) { | ||
| 703 | eval { | ||
| 704 | my ( $firebird_version ) = $dbh->selectrow_array( "SELECT RDB\$GET_CONTEXT('SYSTEM', 'ENGINE_VERSION') FROM RDB\$DATABASE" ); | ||
| 705 | ( $major_server_version ) = $firebird_version =~ /^(\d+)\./; | ||
| 706 | }; | ||
| 707 | return $major_server_version // 3; | ||
| 708 | } | ||
| 709 | else { | ||
| 710 | my $database_version = $dbh->get_info( $GetInfoType{SQL_DBMS_VER} ); | ||
| 711 | ( $major_server_version ) = $database_version =~ /^v?(\d+)\D/i; | ||
| 712 | if ( $dbms eq 'Pg' ) { | ||
| 713 | return $major_server_version // 10; | ||
| 714 | } | ||
| 715 | elsif ( $dbms eq 'Oracle' ) { | ||
| 716 | return $major_server_version // 12; | ||
| 717 | } | ||
| 718 | elsif ( $dbms eq 'MSSQL' ) { | ||
| 719 | return $major_server_version // 16; | ||
| 720 | } | ||
| 721 | } | ||
| 722 | } | ||
| 723 | |||
| 724 | |||
| 725 | sub clone_data { | ||
| 726 | my ( $sf, $data ) = @_; | ||
| 727 | require Storable; | ||
| 728 | return Storable::dclone( $data ); | ||
| 729 | } | ||
| 730 | |||
| 731 | |||
| 732 | sub format_list { | ||
| 733 | my ( $sf, $list ) = @_; | ||
| 734 | return if ! defined $list; | ||
| 735 | return '' if ! @$list; | ||
| 736 | my $sep = ', '; | ||
| 737 | my $formated_list = join( $sep, @$list ); | ||
| 738 | $formated_list =~ s/$sep(?=$list->[-1]\z)/ or /; | ||
| 739 | return $formated_list; | ||
| 740 | } | ||
| 741 | |||
| 742 | |||
| 743 | sub print_error_message { | ||
| 744 | my ( $sf, $message, $info ) = @_; | ||
| 745 | utf8::decode( $message ); | ||
| 746 | chomp( $message ); | ||
| 747 | my $tc = Term::Choose->new( $sf->{i}{tc_default} ); | ||
| 748 | $tc->choose( | ||
| 749 | [ 'Press ENTER to continue' ], | ||
| 750 | { prompt => $message, info => $info } | ||
| 751 | ); | ||
| 752 | } | ||
| 753 | |||
| 754 | |||
| 755 | sub write_json { | ||
| 756 | my ( $sf, $file_fs, $ref ) = @_; | ||
| 757 | if ( ! defined $ref ) { ## | ||
| 758 | #open my $fh, '>', $file_fs or die "$file_fs: $!"; | ||
| 759 | #print $fh; | ||
| 760 | #close $fh; | ||
| 761 | return; | ||
| 762 | } | ||
| 763 | my $json = JSON::MaybeXS->new->utf8->pretty->canonical->encode( $ref ); | ||
| 764 | open my $fh, '>', $file_fs or die "$file_fs: $!"; | ||
| 765 | print $fh $json; | ||
| 766 | close $fh; | ||
| 767 | } | ||
| 768 | |||
| 769 | |||
| 770 | sub read_json { | ||
| 771 | my ( $sf, $file_fs ) = @_; | ||
| 772 | if ( ! defined $file_fs || ! -f $file_fs ) { | ||
| 773 | return; | ||
| 774 | } | ||
| 775 | open my $fh, '<', $file_fs or die "$file_fs: $!"; | ||
| 776 | my $json = do { local $/; <$fh> }; | ||
| 777 | close $fh; | ||
| 778 | my $ref; | ||
| 779 | if ( ! eval { | ||
| 780 | $ref = decode_json( $json ) if $json; | ||
| 781 | 1 } | ||
| 782 | ) { | ||
| 783 | die "In '$file_fs':\n$@"; | ||
| 784 | } | ||
| 785 | return $ref; | ||
| 786 | } | ||
| 787 | |||
| 788 | |||
| 789 | |||
| 790 | |||
| 791 | |||
| 792 | 1; | ||
| 793 | |||
| 794 | __END__ |