DBBrowser/Table/Extensions/Columns.pm untested

filter: Statementstmt 0.0% 0 / 92 Branchbran 0.0% 0 / 18 Conditioncond 0.0% 0 / 6 MC/DCmcdc 0.0% 0 / 4 Subroutinesub 0.0% 0 / 7 Podpod 0.0% 0 / 2 totaltotal 0.0% 0 / 129 CC 10 SCAR 42.4
CC
10
Cov
0%
columns
110.0
new
2.0
BEGIN
2.0
CRAP 69.5
1 package # hide from PAUSE
2 App::DBBrowser::Table::Extensions::Columns;
3  
4 use warnings;
5 use strict;
6 use 5.016;
7  
8 use Term::Choose qw();
9  
10 use App::DBBrowser::Auxil;
11  
12  
13 sub new {
14 my ( $class, $info, $options, $d ) = @_;
15 bless {
16 i => $info,
17 o => $options,
18 d => $d
19 }, $class;
20 }
21  
22  
23 sub columns {
24 my ( $sf, $sql, $cols, $ext_info ) = @_;
25 my $ax = App::DBBrowser::Auxil->new( $sf->{i}, $sf->{o}, $sf->{d} );
26 my $tc = Term::Choose->new( $sf->{i}{tc_default} );
27 my @avail_cols = @$cols;
28  
29 COLUMN: while ( 1 ) {
30 my $change_table = '%%';
31 my @pre = ( undef, $change_table );
32 my $menu = [ @pre, @avail_cols ];
33 my $info = $ext_info || $ax->get_sql_info( $sql );
34 # Choose
35 my $column = $tc->choose(
36 $menu,
37 { %{$sf->{i}{lyt_h}}, info => $info, undef => '<<' }
38 );
39 $ax->print_sql_info( $info );
40 if ( ! defined $column ) {
41 return;
42 }
43 if ( $column eq $change_table ) {
44 my $stmt = $ax->get_sql_info( $sql );
45 my $norm_stmt = $ax->normalize_space_in_stmt( $stmt );
46 my @identifier_alias;
47 for my $table ( keys %{$sf->{d}{table_aliases}} ) {
48 my ( $tmp_table, $tmp_stmt );
49 if ( $table =~ /^\s*\(/ ) {
50 $tmp_table = $ax->normalize_space_in_stmt( $table );
51 $tmp_stmt = $norm_stmt;
52 }
53 else {
54 $tmp_table = $table;
55 $tmp_stmt = $stmt;
56 }
57 for my $alias ( @{$sf->{d}{table_aliases}{$table}} ) {
58 if ( $tmp_stmt =~ /\Q$tmp_table\E\s+\Q$alias\E(?:\W|\z)/ ) {
59 push @identifier_alias, [ $alias, "$table $alias" ];
60 }
61 }
62 }
63 @identifier_alias = sort { $a->[0] cmp $b->[0] } @identifier_alias;
64 my @table_key;
65 my $user_table_keys = $sf->{d}{user_table_keys};
66 my $sys_table_keys = $sf->{d}{sys_table_keys};
67 if ( $sf->{o}{G}{metadata} ) {
68 my $sys_prefix = $sf->{d}{is_system_schema} ? '- ' : ' ';
69 @table_key = ( map( "- $_", @$user_table_keys ), map( $sys_prefix . $_, @$sys_table_keys ) );
70 }
71 else {
72 @table_key = ( map( "- $_", @$user_table_keys ) );
73 }
74 my $local_table = ' Local table';
75 my @pre = ( undef, $local_table );
76 my @tmp_id_alias = map { '- ' . $ax->unquote_identifier( $_->[0] ) } @identifier_alias;
77 my $menu_tables = [ @pre, @tmp_id_alias, @table_key ];
78 my $info = $ax->get_sql_info( $sql );
79 # Choose
80 my $idx = $tc->choose(
81 $menu_tables,
82 { %{$sf->{i}{lyt_v}}, prompt => 'Column from:', info => $info, undef => '<<', index => 1 }
83 );
84 $ax->print_sql_info( $info );
85 if ( ! defined $idx || ! defined $menu_tables->[$idx] ) {
86 next COLUMN;
87 }
88 elsif ( $menu_tables->[$idx] eq $local_table ) {
89 @avail_cols = @$cols;
90 next COLUMN;
91 }
92 else {
93 $idx -= @pre;
94 my ( $table, $identifier );
95 if ( $idx < @identifier_alias ) {
96 ( my $alias, $table ) = @{$identifier_alias[$idx]};
97 $identifier = $alias;
98 }
99 else {
100 $idx -= @identifier_alias;
101 my $table_key = $table_key[$idx] =~ s/^[-\ ]\ //r;
102 $table = $ax->qq_table( $sf->{d}{tables_info}{$table_key} );
103 $identifier = $ax->quote_table( $sf->{d}{tables_info}{$table_key}[2] ); # quoted but not qualified
104 }
105 my ( $column_names, undef ) = $ax->column_names_and_types( $table );
106 @avail_cols = ();
107 for my $col ( @$column_names ) {
108 push @avail_cols, $ax->qualified_identifier( $identifier, $col );
109 }
110 next COLUMN;
111 }
112 }
113 else {
114 return $column;
115 }
116 }
117 }
118  
119  
120  
121  
122 1
123  
124 __END__