| 1 | package DBIx::dbMan::Extension::CmdFormat; | ||
| 2 | |||
| 3 | use strict; | ||
| 4 | use base 'DBIx::dbMan::Extension'; | ||
| 5 | |||
| 6 | our $VERSION = '0.05'; | ||
| 7 | |||
| 8 | 1; | ||
| 9 | |||
| 10 | sub IDENTIFICATION { return "000001-000062-000005"; } | ||
| 11 | |||
| 12 | sub preference { return 2000; } | ||
| 13 | |||
| 14 | sub known_actions { return [ qw/COMMAND SQL_RESULT/ ]; } | ||
| 15 | |||
| 16 | sub handle_action { | ||
| 17 | my ($obj,%action) = @_; | ||
| 18 | |||
| 19 | $action{processed} = 1; | ||
| 20 | if ($action{action} eq 'SQL_RESULT' and ref $action{result} eq 'ARRAY' and not exists $action{old_output_format} and $obj->{-mempool}->get('single_output_format')) { | ||
| 21 | if (scalar @{$action{result}} == 1) { | ||
| 22 | $action{old_output_format} = $obj->{-mempool}->get('output_format'); | ||
| 23 | $obj->{-mempool}->set('output_format',$obj->{-mempool}->get('single_output_format')); | ||
| 24 | delete $action{processed}; | ||
| 25 | } | ||
| 26 | } elsif ($action{action} eq 'COMMAND') { | ||
| 27 | if ($action{cmd} =~ /^set\s+singleoutput\s+to(?:\s+(\S+))?$/i) { | ||
| 28 | my $want = lc $1; | ||
| 29 | if ($1) { | ||
| 30 | my @fmts = $obj->{-mempool}->get_register('output_format'); | ||
| 31 | my %fmts = (); for (@fmts) { ++$fmts{$_}; } | ||
| 32 | if ($fmts{$want}) { | ||
| 33 | $obj->{-mempool}->set('single_output_format',$want); | ||
| 34 | $action{action} = 'OUTPUT'; | ||
| 35 | $action{output} = "Single output format set to $want.\n"; | ||
| 36 | } else { | ||
| 37 | $action{action} = 'OUTPUT'; | ||
| 38 | $obj->{-interface}->error('Unknown output format.'); | ||
| 39 | $action{output} = "Registered formats: ".(join ',',sort @fmts)."\n"; | ||
| 40 | } | ||
| 41 | } else { | ||
| 42 | $obj->{-mempool}->set('single_output_format',''); | ||
| 43 | $action{action} = 'OUTPUT'; | ||
| 44 | $action{output} = "Single output format unset.\n"; | ||
| 45 | } | ||
| 46 | } elsif ($action{cmd} =~ s/^\\f\s*\((.*?)\)\s+//i) { | ||
| 47 | $action{old_output_format} = $obj->{-mempool}->get('output_format'); | ||
| 48 | my $want = lc $1; | ||
| 49 | $want =~ s/^\s+//; | ||
| 50 | my @fmts = $obj->{-mempool}->get_register('output_format'); | ||
| 51 | my %fmts = (); for (@fmts) { ++$fmts{$_}; } | ||
| 52 | if ($fmts{$want}) { | ||
| 53 | $obj->{-mempool}->set('output_format',$want); | ||
| 54 | delete $action{processed}; | ||
| 55 | } else { | ||
| 56 | $action{action} = 'OUTPUT'; | ||
| 57 | $obj->{-interface}->error( 'Unknown output format.' ); | ||
| 58 | $action{output} = "Registered formats: ".(join ',',sort @fmts)."\n"; | ||
| 59 | } | ||
| 60 | } | ||
| 61 | } | ||
| 62 | |||
| 63 | return %action; | ||
| 64 | } | ||
| 65 | |||
| 66 | sub cmdhelp { | ||
| 67 | return [ | ||
| 68 | '\f(<format>) <command>' => 'Format output of <command> as <format>', | ||
| 69 | 'SET SINGLEOUTPUT TO [<format>]' => 'Set output format for one-line output SQL' | ||
| 70 | ]; | ||
| 71 | } | ||
| 72 | |||
| 73 | |||
| 74 | sub restart_complete { | ||
| 75 | my ($obj,$text,$line,$start) = @_; | ||
| 76 | my %action = (action => 'LINE_COMPLETE', text => $text, line => $line, | ||
| 77 | start => $start); | ||
| 78 | do { | ||
| 79 | %action = $obj->{-core}->handle_action(%action); | ||
| 80 | } until ($action{processed}); | ||
| 81 | return @{$action{list}} if ref $action{list} eq 'ARRAY'; | ||
| 82 | return $action{list} if $action{list}; | ||
| 83 | return (); | ||
| 84 | } | ||
| 85 | |||
| 86 | sub formatlist { | ||
| 87 | my $obj = shift; | ||
| 88 | return $obj->{-mempool}->get_register('output_format'); | ||
| 89 | } | ||
| 90 | |||
| 91 | sub cmdcomplete { | ||
| 92 | my ($obj,$text,$line,$start) = @_; | ||
| 93 | return $obj->restart_complete($text,$1,$start-(length($line)-length($1))) if $line =~ /^\s*\\f\s*\(.+?\)\s+(.*)$/i; | ||
| 94 | return $obj->formatlist if $line =~ /^\s*\\f\s*\(\s*\S*$/i; | ||
| 95 | return $obj->formatlist if $line =~ /^\s*SET\s+SINGLEOUTPUT\s+TO\s+\S*$/i; | ||
| 96 | return qw/TO/ if $line =~ /^\s*SET\s+SINGLEOUTPUT\s+\S*$/i; | ||
| 97 | return qw/SINGLEOUTPUT/ if $line =~ /^\s*SET\s+\S*$/i; | ||
| 98 | return ('\f','SET') if $line =~ /^\s*$/i; | ||
| 99 | return ('f(','SET') if $line =~ /^\s*\\[A-Z]*$/i; | ||
| 100 | return (); | ||
| 101 | } |