| 1 | package DBIx::dbMan::Extension::Connections; | ||
| 2 | |||
| 3 | use strict; | ||
| 4 | use base 'DBIx::dbMan::Extension'; | ||
| 5 | use Text::FormatTable; | ||
| 6 | use Term::ReadLine; | ||
| 7 | BEGIN { import Term::ReadLine::Gnu qw/:prompt/; } | ||
| 8 | use Term::ANSIColor; | ||
| 9 | use DBI; | ||
| 10 | |||
| 11 | our $VERSION = '0.14'; | ||
| 12 | |||
| 13 | 1; | ||
| 14 | |||
| 15 | sub IDENTIFICATION { return "000001-000005-000014"; } | ||
| 16 | |||
| 17 | sub preference { return 0; } | ||
| 18 | |||
| 19 | sub known_actions { return [ qw/CONNECTION/ ]; } | ||
| 20 | |||
| 21 | sub menu { | ||
| 22 | my $obj = shift; | ||
| 23 | |||
| 24 | my @menu_use = (); | ||
| 25 | my @menu_close = (); | ||
| 26 | my @menu_reopen = (); | ||
| 27 | my @menu_open = (); | ||
| 28 | my @menu_drop = (); | ||
| 29 | |||
| 30 | for (@{$obj->{-dbi}->list('active')}) { | ||
| 31 | my $sel = ' '; | ||
| 32 | $sel = '*' if $_->{name} eq $obj->{-dbi}->current; | ||
| 33 | push @menu_use,{ label => $sel.' '.$_->{name}, | ||
| 34 | action => { action => 'CONNECTION', operation => 'use', | ||
| 35 | what => $_->{name} } }; | ||
| 36 | push @menu_close,{ label => $_->{name}, | ||
| 37 | action => { action => 'CONNECTION', operation => 'close', | ||
| 38 | what => $_->{name} } }; | ||
| 39 | push @menu_reopen,{ label => $_->{name}, | ||
| 40 | action => { action => 'CONNECTION', operation => 'reopen', | ||
| 41 | what => $_->{name} } }; | ||
| 42 | } | ||
| 43 | for (@{$obj->{-dbi}->list('inactive')}) { | ||
| 44 | push @menu_open,{ label => $_->{name}, | ||
| 45 | action => { action => 'CONNECTION', operation => 'open', | ||
| 46 | what => $_->{name} } }; | ||
| 47 | } | ||
| 48 | for (@{$obj->{-dbi}->list()}) { | ||
| 49 | push @menu_drop,{ label => $_->{name}, submenu => [ | ||
| 50 | { label => 'Temporarly', action => { action => 'CONNECTION', | ||
| 51 | operation => 'drop', what => $_->{name} } }, | ||
| 52 | { label => 'Permanently', action => { action => 'CONNECTION', | ||
| 53 | operation => 'drop', what => $_->{name}, permanent => 1 } } | ||
| 54 | ] }; | ||
| 55 | } | ||
| 56 | |||
| 57 | return ( { label => '_Connection', submenu => [ | ||
| 58 | { label => 'Select current', preference => 100, submenu => \@menu_use }, | ||
| 59 | { label => 'Close', preference => 1, submenu => \@menu_close }, | ||
| 60 | { label => 'Reopen', preference => 2, submenu => \@menu_reopen }, | ||
| 61 | { label => 'Open', preference => 3, submenu => \@menu_open }, | ||
| 62 | { label => 'Drop', preference => -10, submenu => \@menu_drop }, | ||
| 63 | { label => 'Show list', preference => 50, submenu => [ | ||
| 64 | { label => 'All', preference => 5, | ||
| 65 | action => { action => 'CONNECTION', operation => 'show' } }, | ||
| 66 | { separator => 1, preference => 2 }, | ||
| 67 | { label => 'Active', | ||
| 68 | action => { action => 'CONNECTION', operation => 'show', | ||
| 69 | what => 'active' } }, | ||
| 70 | { label => 'Inactive', | ||
| 71 | action => { action => 'CONNECTION', operation => 'show', | ||
| 72 | what => 'inactive' } } | ||
| 73 | ] }, | ||
| 74 | { separator => 1, preference => 25 }, | ||
| 75 | { separator => 1, preference => -1 } | ||
| 76 | ] } ); | ||
| 77 | } | ||
| 78 | |||
| 79 | sub init { | ||
| 80 | my $obj = shift; | ||
| 81 | $obj->{prompt_num} = $obj->{-interface}->register_prompt; | ||
| 82 | } | ||
| 83 | |||
| 84 | sub done { | ||
| 85 | my $obj = shift; | ||
| 86 | $obj->{-interface}->deregister_prompt($obj->{-prompt_num}); | ||
| 87 | } | ||
| 88 | |||
| 89 | sub solve_open_error { | ||
| 90 | my ($obj,$error,$name) = @_; | ||
| 91 | |||
| 92 | if ($error == -1) { | ||
| 93 | return "Can't find driver for ".$obj->{connections}->{$name}->{driver}.".\n"; | ||
| 94 | } elsif ($error == -2) { | ||
| 95 | return "Can't connect to $name (reason: ".DBI->errstr.").\n"; | ||
| 96 | } elsif ($error == -3) { | ||
| 97 | return "Unknown connection $name.\n"; | ||
| 98 | } elsif ($error == -4) { | ||
| 99 | return "Already connected to $name.\n"; | ||
| 100 | } elsif (not $error) { | ||
| 101 | return "Connection to $name established.\n"; | ||
| 102 | } | ||
| 103 | } | ||
| 104 | |||
| 105 | sub solve_close_error { | ||
| 106 | my ($obj,$error,$name) = @_; | ||
| 107 | |||
| 108 | if ($error == -1) { | ||
| 109 | return "Unknown connection $name.\n"; | ||
| 110 | } elsif ($error == -2) { | ||
| 111 | return "Not connected to $name.\n"; | ||
| 112 | } elsif (not $error) { | ||
| 113 | return "Disconnected from $name.\n"; | ||
| 114 | } | ||
| 115 | } | ||
| 116 | |||
| 117 | sub solve_use_error { | ||
| 118 | my ($obj,$error,$name) = @_; | ||
| 119 | |||
| 120 | if ($error == 1) { | ||
| 121 | return "Unset current connection.\n"; | ||
| 122 | } elsif ($error == -1) { | ||
| 123 | return "Unknown connection $name.\n"; | ||
| 124 | } elsif ($error == -2) { | ||
| 125 | return "Not connected to $name.\n"; | ||
| 126 | } elsif (not $error) { | ||
| 127 | return "Set current connection to $name.\n"; | ||
| 128 | } | ||
| 129 | } | ||
| 130 | |||
| 131 | sub handle_action { | ||
| 132 | my ($obj,%action) = @_; | ||
| 133 | |||
| 134 | if ($action{action} eq 'CONNECTION') { | ||
| 135 | if ($action{operation} eq 'open') { | ||
| 136 | my $error = $obj->{-dbi}->open($action{what}); | ||
| 137 | $action{action} = 'OUTPUT'; | ||
| 138 | $action{output} = $obj->solve_open_error($error,$action{what}); | ||
| 139 | $obj->{-interface}->rebuild_menu; | ||
| 140 | |||
| 141 | } elsif ($action{operation} eq 'reopen') { | ||
| 142 | my $reuse = 0; | ||
| 143 | $reuse = 1 if $obj->{-dbi}->current eq $action{what}; | ||
| 144 | |||
| 145 | my $was = $obj->{-dbi}->current(); | ||
| 146 | $obj->{-dbi}->set_current($action{what}); | ||
| 147 | |||
| 148 | my $trans_msg = ''; | ||
| 149 | if ($obj->{-dbi}->in_transaction()) { | ||
| 150 | $obj->{-dbi}->rollback; | ||
| 151 | $obj->{-dbi}->trans_end; | ||
| 152 | $trans_msg = "Transaction end with implicit rollback in connection $action{what}.\n"; | ||
| 153 | } | ||
| 154 | |||
| 155 | $action{action} = 'OUTPUT'; | ||
| 156 | my $error = $obj->{-dbi}->close($action{what}); | ||
| 157 | $action{output} = $trans_msg . $obj->solve_close_error($error,$action{what}); | ||
| 158 | |||
| 159 | $error = $obj->{-dbi}->open($action{what}); | ||
| 160 | $action{output} .= $obj->solve_open_error($error,$action{what}); | ||
| 161 | |||
| 162 | $error = $obj->{-dbi}->set_current($was); | ||
| 163 | if ($reuse) { | ||
| 164 | $action{output} .= $obj->solve_use_error($error,$action{what}); | ||
| 165 | } | ||
| 166 | $obj->{-interface}->add_to_actionlist({ action => 'NOTIFY', notify => 'connection_change' }); | ||
| 167 | $obj->{-interface}->rebuild_menu; | ||
| 168 | |||
| 169 | } elsif ($action{operation} eq 'close') { | ||
| 170 | my $trans_msg = ''; | ||
| 171 | my $was = $obj->{-dbi}->current(); | ||
| 172 | $obj->{-dbi}->set_current($action{what}); | ||
| 173 | |||
| 174 | if ($obj->{-dbi}->in_transaction()) { | ||
| 175 | $obj->{-dbi}->rollback; | ||
| 176 | $obj->{-dbi}->trans_end; | ||
| 177 | $trans_msg = "Transaction end with implicit rollback in connection $action{what}.\n"; | ||
| 178 | } | ||
| 179 | |||
| 180 | $action{action} = 'OUTPUT'; | ||
| 181 | my $error = $obj->{-dbi}->close($action{what}); | ||
| 182 | $action{output} = $trans_msg . $obj->solve_close_error($error,$action{what}); | ||
| 183 | |||
| 184 | $obj->{-dbi}->set_current($was); | ||
| 185 | $obj->{-interface}->rebuild_menu; | ||
| 186 | $obj->{-interface}->add_to_actionlist({ action => 'NOTIFY', notify => 'connection_change' }); | ||
| 187 | $obj->{-interface}->add_to_actionlist({ action => 'TRANSACTION', operation => 'change' }); | ||
| 188 | |||
| 189 | } elsif ($action{operation} eq 'use') { | ||
| 190 | $action{action} = 'OUTPUT'; | ||
| 191 | my $error = $obj->{-dbi}->set_current($action{what}); | ||
| 192 | $action{output} = $obj->solve_use_error($error,$action{what}); | ||
| 193 | $obj->{-interface}->rebuild_menu; | ||
| 194 | $obj->{-interface}->add_to_actionlist({ action => 'TRANSACTION', operation => 'change' }); | ||
| 195 | $obj->{-interface}->add_to_actionlist({ action => 'NOTIFY', notify => 'connection_change' }); | ||
| 196 | |||
| 197 | } elsif ($action{operation} eq 'show') { | ||
| 198 | my @list = @{$obj->{-dbi}->list($action{what})}; | ||
| 199 | my $clist = ''; | ||
| 200 | if (@list) { | ||
| 201 | $clist .= ($action{what} eq 'active'?'Active c':'C')."onnections:\n"; | ||
| 202 | my $table = new Text::FormatTable '| l l | l | l | l | l | l | l |'; | ||
| 203 | $table->rule; | ||
| 204 | $table->head('C','NAME','ACTIVE','PERMANENT','DRIVER','LOGIN','DSN','CONFIG'); | ||
| 205 | $table->rule; | ||
| 206 | for (@list) { | ||
| 207 | $table->row((($obj->{-dbi}->current eq $_->{name})?'*':' '),$_->{name},($_->{-logged}?'yes':'no'),($obj->{-dbi}->is_permanent_connection($_->{name})?'yes':'no'),$_->{driver},$_->{login},$_->{dsn},$_->{config} || ''); | ||
| 208 | } | ||
| 209 | $table->rule; | ||
| 210 | $clist .= $table->render($obj->{-interface}->render_size); | ||
| 211 | } else { | ||
| 212 | $action{what} = '' if $action{what} ne 'active'; | ||
| 213 | $clist .= "No".($action{what}?' '.$action{what}:'')." connection.\n"; | ||
| 214 | } | ||
| 215 | $action{action} = 'OUTPUT'; | ||
| 216 | $action{output} = $clist; | ||
| 217 | |||
| 218 | } elsif ($action{operation} eq 'create') { | ||
| 219 | my %parm = (); | ||
| 220 | for (qw/driver dsn login password auto_login config prompt_color/) { $parm{$_} = $action{$_} || ''; } | ||
| 221 | |||
| 222 | $action{action} = 'NONE'; | ||
| 223 | my $error = $obj->{-dbi}->create_connection($action{what},\%parm); | ||
| 224 | if ($error == -1) { | ||
| 225 | $action{action} = 'OUTPUT'; | ||
| 226 | $action{output} = "Connection with name $action{what} already exists.\n"; | ||
| 227 | } elsif ($error >= 0) { | ||
| 228 | $action{action} = 'OUTPUT'; | ||
| 229 | $action{output} = "Connection $action{what} created.\n"; | ||
| 230 | if ($error > 50) { | ||
| 231 | $action{output} .= $obj->solve_open_error($error-100,$action{what}) if $error > 50; | ||
| 232 | } | ||
| 233 | if ($action{permanent}) { | ||
| 234 | $error = $obj->{-dbi}->save_connection($action{what}); | ||
| 235 | if ($error == -1) { | ||
| 236 | $action{output} .= "Connection with name $action{what} not exists.\n"; | ||
| 237 | } elsif (not $error) { | ||
| 238 | $action{output} .= "Making connection $action{what} permanent.\n"; | ||
| 239 | |||
| 240 | } | ||
| 241 | } | ||
| 242 | $obj->{-interface}->rebuild_menu; | ||
| 243 | } | ||
| 244 | } elsif ($action{operation} eq 'drop') { | ||
| 245 | $action{action} = 'NONE'; | ||
| 246 | my $error = $obj->{-dbi}->drop_connection($action{what}); | ||
| 247 | if ($error == -1) { | ||
| 248 | $action{action} = 'OUTPUT'; | ||
| 249 | $action{output} = "Connection with name $action{what} not exists.\n"; | ||
| 250 | } elsif (not $error) { | ||
| 251 | $action{action} = 'OUTPUT'; | ||
| 252 | $action{output} = "Connection $action{what} dropped.\n"; | ||
| 253 | if ($action{permanent}) { | ||
| 254 | $error = $obj->{-dbi}->destroy_connection($action{what}); | ||
| 255 | if ($error == -2) { | ||
| 256 | $action{output} .= "Can't destroy connection $action{what}.\n"; | ||
| 257 | } elsif (not $error) { | ||
| 258 | $action{output} .= "Destroying permanent connection $action{what}.\n"; | ||
| 259 | } | ||
| 260 | } | ||
| 261 | $obj->{-interface}->rebuild_menu; | ||
| 262 | } | ||
| 263 | } | ||
| 264 | |||
| 265 | my $db = ''; | ||
| 266 | if ( $obj->{-dbi}->current ) { | ||
| 267 | $db .= RL_PROMPT_START_IGNORE . color( $obj->{-dbi}->prompt_color ) . RL_PROMPT_END_IGNORE if $obj->{-dbi}->prompt_color; | ||
| 268 | $db .= '<'.$obj->{-dbi}->current.'>'; | ||
| 269 | $db .= RL_PROMPT_START_IGNORE . color( 'reset' ) . RL_PROMPT_END_IGNORE if $obj->{-dbi}->prompt_color; | ||
| 270 | } | ||
| 271 | $obj->{-interface}->prompt($obj->{prompt_num},$db); | ||
| 272 | } | ||
| 273 | |||
| 274 | $action{processed} = 1; | ||
| 275 | return %action; | ||
| 276 | } | ||
| 277 |