| 1 | package DBIx::dbMan::Extension::CmdInputCSV; | ||
| 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-000040-000005"; } | ||
| 11 | |||
| 12 | sub preference { return 1500; } | ||
| 13 | |||
| 14 | sub known_actions { return [ qw/COMMAND/ ]; } | ||
| 15 | |||
| 16 | sub handle_action { | ||
| 17 | my ($obj,%action) = @_; | ||
| 18 | |||
| 19 | if ($action{action} eq 'COMMAND') { | ||
| 20 | if ($action{cmd} =~ /^\\csvin(?:\[(.*)\])?\s*\((.*?)\)\s+(.*)$/i) { | ||
| 21 | $action{action} = 'CSV_IN'; | ||
| 22 | $action{file} = $2; | ||
| 23 | $action{sql} = $3; | ||
| 24 | $action{opt_separator} = ','; | ||
| 25 | $action{opt_quote} = '"'; | ||
| 26 | $action{opt_eol} = "\n"; | ||
| 27 | $action{opt_headings} = 0; | ||
| 28 | $action{opt_escape} = '"'; | ||
| 29 | $action{opt_allow_loose_escapes} = 1; | ||
| 30 | $action{opt_allow_loose_quotes} = 1; | ||
| 31 | my $opt = $1; | ||
| 32 | my @opts = split /\s+/,$opt; | ||
| 33 | for (@opts) { | ||
| 34 | my ($tag,$value) = split /=/,$_; | ||
| 35 | $value =~ s/\\s/ /g; | ||
| 36 | |||
| 37 | $value =~ s/\\(.)/my $v=''; my $src='$v="\\'.$1.'";'; eval $src; $v/eg; | ||
| 38 | |||
| 39 | if ($tag =~ /^(separator|quote|eol|headings|escape|allow_loose_escapes|allow_loose_quotes)$/) { | ||
| 40 | $action{"opt_$tag"} = $value; | ||
| 41 | } else { | ||
| 42 | $action{action} = 'NONE'; | ||
| 43 | $obj->{-interface}->error('Unknown option in \csvin'); | ||
| 44 | } | ||
| 45 | } | ||
| 46 | } | ||
| 47 | } | ||
| 48 | |||
| 49 | $action{processed} = 1; | ||
| 50 | return %action; | ||
| 51 | } | ||
| 52 | |||
| 53 | sub cmdhelp { | ||
| 54 | return [ | ||
| 55 | '\csvin[<options>](<file>) <command>' => 'Import CSV file <file> through placeholders in <command> (with optionable <options> defined like separator=, quote=" eol=\n headings=0 escape=" where headings=0 is no headings, =1 is headings skip, \s means space, \t means tabulator)' | ||
| 56 | ]; | ||
| 57 | } | ||
| 58 | |||
| 59 | sub restart_complete { | ||
| 60 | my ($obj,$text,$line,$start) = @_; | ||
| 61 | my %action = (action => 'LINE_COMPLETE', text => $text, line => $line, | ||
| 62 | start => $start); | ||
| 63 | do { | ||
| 64 | %action = $obj->{-core}->handle_action(%action); | ||
| 65 | } until ($action{processed}); | ||
| 66 | return @{$action{list}} if ref $action{list} eq 'ARRAY'; | ||
| 67 | return $action{list} if $action{list}; | ||
| 68 | return (); | ||
| 69 | } | ||
| 70 | |||
| 71 | sub cmdcomplete { | ||
| 72 | my ($obj,$text,$line,$start) = @_; | ||
| 73 | return () unless $obj->{-dbi}->current; | ||
| 74 | return $obj->restart_complete($text,$1,$start-(length($line)-length($1))) if $line =~ /^\s*\\csvin(?:\[.*?\])?\s*\(.+?\)\s+(.*)$/i; | ||
| 75 | return $obj->{-interface}->filenames_complete($text,$line,$start) if $line =~ /^\s*\\csvin(\[.*?\])?\s*\(\S*$/i; | ||
| 76 | return ('\csvin') if $line =~ /^\s*$/i; | ||
| 77 | return ('csvin(') if $line =~ /^\s*\\[A-Z]*$/i; | ||
| 78 | return (); | ||
| 79 | } |