| 1 | # Copyright (C) 2008 Ioannis Tambouras <ioannis@cpan.org>. All rights reserved. | ||
| 2 | # LICENSE: GPLv3, eead licensing terms at http://www.fsf.org . | ||
| 3 | |||
| 4 | package Pg::Loader::Columns; | ||
| 5 | |||
| 6 | 19 | ▶ | use 5.010000; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 7 | 19 | ▶ | use Data::Dumper; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 8 | 19 | ▶ | use strict; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 9 | 19 | ▶ | use warnings; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 10 | 19 | ▶ | use Text::CSV; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 11 | 19 | ▶ | use List::MoreUtils qw( firstidx ); |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 12 | 19 | ▶ | use Log::Log4perl qw( :easy ); |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 13 | 19 | ▶ | use base 'Exporter'; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 14 | |||
| 15 | our $VERSION = '0.12'; | ||
| 16 | |||
| 17 | our @EXPORT = qw( | ||
| 18 | range2list ranges2set data_fields | ||
| 19 | init_csv combine requested_cols | ||
| 20 | ); | ||
| 21 | |||
| 22 | sub range2list { | ||
| 23 | my $_ = shift; | ||
| 24 | given ($_) { | ||
| 25 | when (/^(\d)\-(\d)$/o ) { return "$1..$2" } | ||
| 26 | when (/^\d\.\.\d$/o ) { return "$_" } | ||
| 27 | when (/^\d$/o ) { return $_} | ||
| 28 | when (/^(.*?),(.*)$/o ) { return range2list($1). ',' | ||
| 29 | .range2list($2)} | ||
| 30 | default { return ''} | ||
| 31 | } | ||
| 32 | } | ||
| 33 | |||
| 34 | sub ranges2set { | ||
| 35 | my @unit = (0..20); | ||
| 36 | my $tmp = range2list( shift||return) ; | ||
| 37 | { no warnings 'deprecated'; | ||
| 38 | $[=0; no warnings; %_= map { ($_=>undef)} eval ' @unit['.$tmp.']' } | ||
| 39 | [ sort keys %_ ]; | ||
| 40 | } | ||
| 41 | |||
| 42 | sub init_csv { | ||
| 43 | my ($s) = @_ ; | ||
| 44 | new Text::CSV { | ||
| 45 | quote_char => $s->{quotechar} , | ||
| 46 | escape_char => $s->{escapechar} , | ||
| 47 | sep_char => $s->{field_sep} , | ||
| 48 | eol => $s->{eol} , | ||
| 49 | allow_whitespace => $s->{skipinitialspace} , | ||
| 50 | } or die Text::CSV->error_diag ; | ||
| 51 | } | ||
| 52 | |||
| 53 | |||
| 54 | sub combine { | ||
| 55 | my ( $s, $csv, $d, @col ) = @_; | ||
| 56 | return '' unless @col; | ||
| 57 | for ( @col ) { | ||
| 58 | my $h = $s->{rfm}{$_}; | ||
| 59 | my $val = $d->{$_}; | ||
| 60 | exists $s->{ "udc_$_"} and $d->{$_} = $s->{ "udc_$_"}; | ||
| 61 | next unless $h->{ref}; | ||
| 62 | my $ref = UNIVERSAL::can( $h->{pack}, $h->{fun} ); | ||
| 63 | $d->{$_} = $ref->( $val ); | ||
| 64 | } | ||
| 65 | join $csv->{sep_char}//'', map { $_ // '' } @{$d}{@col}; | ||
| 66 | } | ||
| 67 | |||
| 68 | sub field_nums_reqe { | ||
| 69 | my ( $s, $max ) = @_; | ||
| 70 | return 0..$max unless $s->{only_cols} ; | ||
| 71 | return 0..$max if $s->{only_cols} =~ /^\s*[*]\s*$/o ; | ||
| 72 | my $range_str = $s->{only_cols} ; | ||
| 73 | ( ref $range_str eq 'ARRAY') and $range_str = join ',', @$range_str; | ||
| 74 | map { --$_ ; | ||
| 75 | $_>$max and LOGDIE('column index is larger than columns'); | ||
| 76 | $_<0 and LOGDIE('column index is negatve'); | ||
| 77 | $_ ; | ||
| 78 | } @{ranges2set($range_str)}; | ||
| 79 | } | ||
| 80 | |||
| 81 | sub pack_cols { | ||
| 82 | my @col = @_ ; | ||
| 83 | my $col_str = '('. join(', ', @col) . ')'; | ||
| 84 | ( $col_str, @col ); | ||
| 85 | } | ||
| 86 | |||
| 87 | sub requested_cols { | ||
| 88 | # select colomns from $all | ||
| 89 | # Assumption: Only one of $s->{copy_columns} or $s->{only_cols} | ||
| 90 | # Assumption: the user specified a valid "copy" parameter | ||
| 91 | # are defined. If none are defined, it returns all columns. | ||
| 92 | my $s = shift || return; | ||
| 93 | my $all = $s->{attributes}; | ||
| 94 | die 'missing $all columns' unless $all ; | ||
| 95 | die 'missing $all columns' unless @$all; | ||
| 96 | die 'mutually exclusive' if $s->{copy_columns} && $s->{only_cols} ; | ||
| 97 | |||
| 98 | return pack_cols(@$all) unless $s->{copy_columns} || $s->{only_cols}; | ||
| 99 | return pack_cols @{$s->{copy_columns}} if $s->{copy_columns} ; | ||
| 100 | return pack_cols @$all if ($s->{copy_columns}||'') =~ /^\s*[*]\s*$/o; | ||
| 101 | |||
| 102 | pack_cols +($s->{copy_columns}) | ||
| 103 | ? @{$s->{copy_columns}} | ||
| 104 | : @$all[ field_nums_reqe $s, $#{$all} ] ; | ||
| 105 | } | ||
| 106 | |||
| 107 | |||
| 108 | 1; | ||
| 109 | __END__ | ||