DBBrowser/CreateDropAttach/AttachDB.pm untested

filter: Statementstmt 0.0% 0 / 155 Branchbran 0.0% 0 / 34 Conditioncond 0.0% 0 / 26 MC/DCmcdc 0.0% 0 / 15 Subroutinesub 0.0% 0 / 11 Podpod 0.0% 0 / 4 totaltotal 0.0% 0 / 245 CC 18 SCAR 44.0
CC
18
Cov
0%
detach_db
156.0
attach_db
56.0
new
2.0
CRAP 81.5
1 package # hide from PAUSE
2 App::DBBrowser::CreateDropAttach::AttachDB;
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  
15  
16 sub new {
17 my ( $class, $info, $options, $d ) = @_;
18 bless {
19 i => $info,
20 o => $options,
21 d => $d
22 }, $class;
23 }
24  
25  
26 sub attach_db {
27 my ( $sf ) = @_;
28 my $ax = App::DBBrowser::Auxil->new( $sf->{i}, $sf->{o}, $sf->{d} );
29 my $tc = Term::Choose->new( $sf->{i}{tc_default} );
30 my $plugin = $sf->{i}{plugin};
31 my $db = $sf->{d}{db};
32 my $attached_db;
33 if ( -f $sf->{i}{f_attached_db} ) {
34 my $h_ref = $ax->read_json( $sf->{i}{f_attached_db} ) // {};
35 $attached_db = $h_ref->{$plugin}{$db} // {};
36 }
37 my $dbh = $sf->{d}{dbh};
38 my $old_idx = 0;
39  
40 ATTACH: while ( 1 ) {
41 my @tmp_info = ( $sf->{d}{db_string} );
42 for my $key ( sort keys %$attached_db ) {
43 push @tmp_info, $sf->attach_stmt( $dbh, $attached_db->{$key}, $key );
44 }
45 push @tmp_info, '';
46 my @pre = ( undef );
47 my @choices = ( @{$sf->{d}{user_dbs}}, @{$sf->{d}{sys_dbs}} );
48 my $menu = [ @pre, @choices ];
49 my $info = join( "\n", @tmp_info );
50 # Choose
51 my $idx = $tc->choose(
52 $menu,
53 { %{$sf->{i}{lyt_v}}, prompt => "Add database:", info => $info,
54 undef => '<=', index => 1, default => $old_idx }
55 );
56 $ax->print_sql_info( $info );
57 if ( ! defined $idx || ! defined $menu->[$idx] ) {
58 return;
59 }
60 if ( $sf->{o}{G}{menu_memory} ) {
61 if ( $old_idx == $idx && ! $ENV{TC_RESET_AUTO_UP} ) {
62 $old_idx = 0;
63 next ATTACH;
64 }
65 $old_idx = $idx;
66 }
67 my $db_to_attach = $menu->[$idx];
68 my $tr = Term::Form::ReadLine->new( $sf->{i}{tr_default} );
69 push @tmp_info, 'DB: ' . $db_to_attach;
70 my $bu_attached_db = { %$attached_db };
71  
72 ALIAS: while ( 1 ) {
73 my $info = join( "\n", @tmp_info );
74 $ax->print_sql_info( $info );
75 # Readline
76 my $alias = $tr->readline(
77 'Alias: ',
78 { info => $info, clear_screen => 1, history => [ 'a' .. 'z' ] }
79 );
80 $ax->print_sql_info( $info );
81 if ( ! length $alias ) {
82 next ATTACH;
83 }
84 else {
85 $attached_db->{$alias} = $db_to_attach;
86 my @tmp_info = ( $sf->{d}{db_string} );
87 push @tmp_info, map { $sf->attach_stmt( $dbh, $attached_db->{$_}, $_ ) } sort keys %$attached_db;
88 push @tmp_info, '';
89 my $info = join( "\n", @tmp_info );
90 # Choose
91 my $confirm = $tc->choose(
92 [ undef, $sf->{i}{confirm} ],
93 { info => $info, clear_screen => 1, undef => $sf->{i}{back} }
94 );
95 $ax->print_sql_info( $info );
96 if ( ! defined $confirm ) {
97 $attached_db = $bu_attached_db;
98 next ALIAS;
99 }
100 my $h_ref = $ax->read_json( $sf->{i}{f_attached_db} ) // {};
101 $h_ref->{$plugin}{$db} = $attached_db;
102 $ax->write_json( $sf->{i}{f_attached_db}, $h_ref );
103 return 1;
104 }
105 }
106 }
107 }
108  
109  
110 sub attach_stmt {
111 my ( $sf, $dbh, $db_to_attach, $alias ) = @_;
112 my $ax = App::DBBrowser::Auxil->new( $sf->{i}, $sf->{o}, $sf->{d} );
113 return sprintf "ATTACH DATABASE %s AS %s", $dbh->quote( $db_to_attach ), $ax->quote_alias( $alias );
114 }
115  
116  
117 sub detach_db {
118 my ( $sf ) = @_;
119 my $ax = App::DBBrowser::Auxil->new( $sf->{i}, $sf->{o}, $sf->{d} );
120 my $tc = Term::Choose->new( $sf->{i}{tc_default} );
121 my $file_fs = $sf->{i}{f_attached_db};
122 my $plugin = $sf->{i}{plugin};
123 my $db = $sf->{d}{db};
124  
125 my $attached_db;
126 if ( -f $file_fs ) {
127 my $h_ref = $ax->read_json( $file_fs ) // {};
128 $attached_db = $h_ref->{$plugin}{$db} // {};
129 }
130 if ( ! %$attached_db ) {
131 my $info = $sf->{d}{db_string};
132 my $prompt = 'No attached databases.';
133 $tc->choose(
134 [ undef ],
135 { info => $info, prompt => $prompt, undef => '<<' }
136 );
137 return;
138 }
139 my $old_idx = 0;
140  
141 DETACH: while ( 1 ) {
142 my $info = $sf->{d}{db_string};
143 my @choices;
144 my @aliases = ( sort keys %$attached_db );
145 for my $key ( @aliases ) {
146 push @choices, sprintf "%s (%s)", $key, $attached_db->{$key};
147 }
148 my $prompt = 'Detach database:';
149 my @pre = ( undef );
150 # Choose
151 my $idx = $tc->choose(
152 [ @pre, @choices ],
153 { %{$sf->{i}{lyt_v}}, prompt => $prompt, info => $info, index => 1, undef => '<=', default => $old_idx }
154 );
155 $ax->print_sql_info( $info );
156 if ( ! $idx ) {
157 return;
158 }
159 if ( $sf->{o}{G}{menu_memory} ) {
160 if ( $old_idx == $idx && ! $ENV{TC_RESET_AUTO_UP} ) {
161 $old_idx = 0;
162 next DETACH;
163 }
164 $old_idx = $idx;
165 }
166 my $detached_alias = $aliases[$idx - @pre];
167 my $detached_db = delete $attached_db->{$detached_alias};
168 my $dbh = $sf->{d}{dbh};
169 $prompt = sprintf "DETACH DATABASE %s (%s)", $ax->quote_alias( $detached_alias ), $dbh->quote( $detached_db );
170 # Choose
171 my $confirm = $tc->choose(
172 [ undef, 'YES' ],
173 { info => $info, prompt => $prompt, clear_screen => 1, undef => 'NO' }
174 );
175 $ax->print_sql_info( $info );
176 if ( ! defined $confirm ) {
177 $attached_db->{$detached_alias} = $detached_db;
178 next DETACH;
179 }
180 my $h_ref = $ax->read_json( $file_fs ) // {};
181 if ( %$attached_db ) {
182 $h_ref->{$plugin}{$db} = $attached_db;
183 }
184 else {
185 delete $h_ref->{$plugin}{$db};
186 if ( ! %{$h_ref->{$plugin}} ) {
187 delete $h_ref->{$plugin};
188 }
189 }
190 if ( ! %$h_ref ) {
191 if ( -f $file_fs && ! eval { unlink $file_fs or die "unlink '$file_fs': $!" } ) {
192 $ax->print_error_message( $@ );
193 $ax->write_json( $file_fs, $h_ref ) if -f $file_fs;
194 }
195 }
196 else {
197 $ax->write_json( $file_fs, $h_ref );
198 }
199 return 1;
200 }
201 }
202  
203  
204  
205  
206  
207  
208  
209  
210 1;
211  
212 __END__