| 1 | package DBIx::dbMan::Extension::CmdLongSQL; | ||
| 2 | |||
| 3 | use strict; | ||
| 4 | use base 'DBIx::dbMan::Extension'; | ||
| 5 | |||
| 6 | our $VERSION = '0.08'; | ||
| 7 | |||
| 8 | 1; | ||
| 9 | |||
| 10 | sub IDENTIFICATION { return "000001-000055-000008"; } | ||
| 11 | |||
| 12 | sub preference { return 4000; } | ||
| 13 | |||
| 14 | sub known_actions { return [ qw/COMMAND/ ]; } | ||
| 15 | |||
| 16 | sub init { | ||
| 17 | my $obj = shift; | ||
| 18 | $obj->{prompt_num} = $obj->{-interface}->register_prompt(2000); | ||
| 19 | } | ||
| 20 | |||
| 21 | sub done { | ||
| 22 | my $obj = shift; | ||
| 23 | $obj->{-interface}->deregister_prompt($obj->{-prompt_num}); | ||
| 24 | } | ||
| 25 | |||
| 26 | sub menu { | ||
| 27 | my $obj = shift; | ||
| 28 | |||
| 29 | if ($obj->{-mempool}->get('long_active')) { | ||
| 30 | return ( { label => 'Input', submenu => [ | ||
| 31 | { label => 'Execute long stored SQL now', | ||
| 32 | action => { action => 'COMMAND', | ||
| 33 | cmd => '\\g' } } | ||
| 34 | ] } ); | ||
| 35 | } else { | ||
| 36 | return (); | ||
| 37 | } | ||
| 38 | } | ||
| 39 | |||
| 40 | sub handle_action { | ||
| 41 | my ($obj,%action) = @_; | ||
| 42 | |||
| 43 | $action{processed} = 1; | ||
| 44 | if ($action{action} eq 'COMMAND') { | ||
| 45 | if ($obj->{-mempool}->get('long_active')) { | ||
| 46 | if ($action{cmd} =~ /^\\g$/i) { | ||
| 47 | my $current = $obj->{-mempool}->get('long_buffer'); | ||
| 48 | $obj->{-mempool}->set('long_active',0); | ||
| 49 | $action{cmd} = $current; | ||
| 50 | $obj->{-interface}->prompt($obj->{prompt_num},''); | ||
| 51 | $obj->{-mempool}->set('long_buffer',''); | ||
| 52 | |||
| 53 | my $history = new DBIx::dbMan::History -config => $obj->{-config}, -interface => $obj->{-interface}; | ||
| 54 | my @buffer = $history->load(); | ||
| 55 | pop @buffer while @buffer and $buffer[$#buffer] !~ /^\s*\\l/i; | ||
| 56 | pop @buffer if @buffer; | ||
| 57 | $obj->{-interface}->history_clear(); | ||
| 58 | for (@buffer) { | ||
| 59 | $obj->{-interface}->history_add($_); | ||
| 60 | } | ||
| 61 | $obj->{-interface}->history_add($current); | ||
| 62 | $obj->{-interface}->rebuild_menu(); | ||
| 63 | } else { | ||
| 64 | my $current = $obj->{-mempool}->get('long_buffer'); | ||
| 65 | $current .= ' ' if $current; | ||
| 66 | $current .= $action{cmd}; | ||
| 67 | $obj->{-mempool}->set('long_buffer',$current); | ||
| 68 | $action{action} = 'NONE'; | ||
| 69 | $obj->{-interface}->prompt($obj->{prompt_num},'LONG'); | ||
| 70 | $obj->{-interface}->rebuild_menu(); | ||
| 71 | } | ||
| 72 | delete $action{processed}; | ||
| 73 | } else { | ||
| 74 | if ($action{cmd} =~ s/^\\l\s+//i) { | ||
| 75 | my $current = $obj->{-mempool}->get('long_buffer'); | ||
| 76 | $current .= ' ' if $current; | ||
| 77 | $current .= $action{cmd}; | ||
| 78 | $obj->{-mempool}->set('long_buffer',$current); | ||
| 79 | $obj->{-mempool}->set('long_active',1); | ||
| 80 | delete $action{processed}; | ||
| 81 | $action{action} = 'NONE'; | ||
| 82 | $obj->{-interface}->prompt($obj->{prompt_num},'LONG'); | ||
| 83 | $obj->{-interface}->rebuild_menu(); | ||
| 84 | } | ||
| 85 | } | ||
| 86 | } | ||
| 87 | |||
| 88 | return %action; | ||
| 89 | } | ||
| 90 | |||
| 91 | sub cmdhelp { | ||
| 92 | my $obj = shift; | ||
| 93 | |||
| 94 | if ($obj->{-mempool}->get('long_active')) { | ||
| 95 | return [ | ||
| 96 | '\g' => 'Execute long SQL (multiline command)', | ||
| 97 | ]; | ||
| 98 | } else { | ||
| 99 | return [ | ||
| 100 | '\l <command>' => 'Long SQL (multiline command)', | ||
| 101 | ]; | ||
| 102 | } | ||
| 103 | } | ||
| 104 | |||
| 105 | sub restart_complete { | ||
| 106 | my ($obj,$text,$line,$start) = @_; | ||
| 107 | my %action = (action => 'LINE_COMPLETE', text => $text, line => $line, | ||
| 108 | start => $start); | ||
| 109 | do { | ||
| 110 | %action = $obj->{-core}->handle_action(%action); | ||
| 111 | } until ($action{processed}); | ||
| 112 | return @{$action{list}} if ref $action{list} eq 'ARRAY'; | ||
| 113 | return $action{list} if $action{list}; | ||
| 114 | return (); | ||
| 115 | } | ||
| 116 | |||
| 117 | sub cmdcomplete { | ||
| 118 | my ($obj,$text,$line,$start) = @_; | ||
| 119 | return () if $obj->{-mempool}->get('long_active_complete'); | ||
| 120 | if ($obj->{-mempool}->get('long_active')) { | ||
| 121 | my @base = (); my @res = (); | ||
| 122 | @base = ('g') if $line =~ /^\s*\\[A-Z]*$/i; | ||
| 123 | unless (@base) { | ||
| 124 | @base = ('\g') if $line =~ /^\s*$/i; | ||
| 125 | my $current = $obj->{-mempool}->get('long_buffer'); | ||
| 126 | $current .= ' ' if $current; | ||
| 127 | $start += length($current); | ||
| 128 | $current .= $line; | ||
| 129 | $obj->{-mempool}->set('long_active_complete',1); | ||
| 130 | @res = $obj->restart_complete($text,$current,$start); | ||
| 131 | $obj->{-mempool}->set('long_active_complete',0); | ||
| 132 | } | ||
| 133 | return (@base,@res); | ||
| 134 | } | ||
| 135 | return $obj->restart_complete($text,$1,$start-(length($line)-length($1))) if $line =~ /^\s*\\l\s+(.*)$/i; | ||
| 136 | return ('\l') if $line =~ /^\s*$/i; | ||
| 137 | return ('l') if $line =~ /^\s*\\[A-Z]*$/i; | ||
| 138 | return (); | ||
| 139 | } |