| 1 | ######################################################### | ||||||||||||||||||||||||||||||||||||||||||
| 2 | package AnyData::Format::CSV; | ||||||||||||||||||||||||||||||||||||||||||
| 3 | ######################################################### | ||||||||||||||||||||||||||||||||||||||||||
| 4 | # copyright (c) 2000, Jeff Zucker <jeff@vpservices.com> | ||||||||||||||||||||||||||||||||||||||||||
| 5 | ######################################################### | ||||||||||||||||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||||||||||||||||
| 7 | =head1 NAME | ||||||||||||||||||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||||||||||||||||
| 9 | AnyData::Format::CSV - tiedhash & DBI/SQL access to CSV data | ||||||||||||||||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||||||||||||||||
| 11 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||||||||||||||||
| 13 | use AnyData; | ||||||||||||||||||||||||||||||||||||||||||
| 14 | my $table = adTable( 'CSV', $filename,'r',$flags ); | ||||||||||||||||||||||||||||||||||||||||||
| 15 | while (my $row = each %$table) { | ||||||||||||||||||||||||||||||||||||||||||
| 16 | print $row->{name},"\n" if $row->{country} =~ /us|mx|ca/; | ||||||||||||||||||||||||||||||||||||||||||
| 17 | } | ||||||||||||||||||||||||||||||||||||||||||
| 18 | # ... other tied hash operations | ||||||||||||||||||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||||||||||||||||
| 20 | OR | ||||||||||||||||||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||||||||||||||||
| 22 | use DBI | ||||||||||||||||||||||||||||||||||||||||||
| 23 | my $dbh = DBI->connect('dbi:AnyData:'); | ||||||||||||||||||||||||||||||||||||||||||
| 24 | $dbh->func('table1','CSV', $filename,$flags,'ad_catalog'); | ||||||||||||||||||||||||||||||||||||||||||
| 25 | my $hits = $dbh->selectall_arrayref( qq{ | ||||||||||||||||||||||||||||||||||||||||||
| 26 | SELECT name FROM table1 WHERE country = 'us' | ||||||||||||||||||||||||||||||||||||||||||
| 27 | }); | ||||||||||||||||||||||||||||||||||||||||||
| 28 | # ... other DBI/SQL operations | ||||||||||||||||||||||||||||||||||||||||||
| 29 | |||||||||||||||||||||||||||||||||||||||||||
| 30 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||||||||||||||||
| 31 | |||||||||||||||||||||||||||||||||||||||||||
| 32 | This is a plug-in format parser for the AnyData and DBD::AnyData modules. It will read column names from the first row of the file, or accept names passed by the user. In addition to column names, the user may set other options as follows: | ||||||||||||||||||||||||||||||||||||||||||
| 33 | |||||||||||||||||||||||||||||||||||||||||||
| 34 | col_names : a comma separated list of column names | ||||||||||||||||||||||||||||||||||||||||||
| 35 | eol : the end of record mark, \n by default | ||||||||||||||||||||||||||||||||||||||||||
| 36 | quote_char : the character used to quote fields " by default | ||||||||||||||||||||||||||||||||||||||||||
| 37 | escape_char : the character used to escape the quote char, " by default | ||||||||||||||||||||||||||||||||||||||||||
| 38 | |||||||||||||||||||||||||||||||||||||||||||
| 39 | If you are using this with DBD::AnyData, put ad_ in front of the flags, e.g. | ||||||||||||||||||||||||||||||||||||||||||
| 40 | ad_eol. | ||||||||||||||||||||||||||||||||||||||||||
| 41 | |||||||||||||||||||||||||||||||||||||||||||
| 42 | Please refer to the documentation for AnyData.pm and DBD::AnyData.pm | ||||||||||||||||||||||||||||||||||||||||||
| 43 | for further details. | ||||||||||||||||||||||||||||||||||||||||||
| 44 | |||||||||||||||||||||||||||||||||||||||||||
| 45 | =head1 AUTHOR & COPYRIGHT | ||||||||||||||||||||||||||||||||||||||||||
| 46 | |||||||||||||||||||||||||||||||||||||||||||
| 47 | copyright 2000, Jeff Zucker <jeff@vpservices.com> | ||||||||||||||||||||||||||||||||||||||||||
| 48 | all rights reserved | ||||||||||||||||||||||||||||||||||||||||||
| 49 | |||||||||||||||||||||||||||||||||||||||||||
| 50 | |||||||||||||||||||||||||||||||||||||||||||
| 51 | =cut | ||||||||||||||||||||||||||||||||||||||||||
| 52 | |||||||||||||||||||||||||||||||||||||||||||
| 53 | |||||||||||||||||||||||||||||||||||||||||||
| 54 | 3 | ▶ | use strict; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 55 | 3 | ▶ | use warnings; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 56 | 3 | ▶ | use AnyData::Format::Base; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 57 | 3 | ▶ | use vars qw( @ISA $VERSION); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 58 | 3 | @AnyData::Format::CSV::ISA = qw( AnyData::Format::Base ); | |||||||||||||||||||||||||||||||||||||||||
| 59 | |||||||||||||||||||||||||||||||||||||||||||
| 60 | 3 | $VERSION = '0.12'; | |||||||||||||||||||||||||||||||||||||||||
| 61 | |||||||||||||||||||||||||||||||||||||||||||
| 62 | 7 | ▶ | sub new { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
new: called CC=8 SCAR=21.1
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 63 | 7 | my $class = shift; | |||||||||||||||||||||||||||||||||||||||||
| 64 | 7 | ▶ | my $self = shift || {}; | ||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
shift() || {}
MC/DC0%0/1
shift() || {}
shift()
Truth table50%1/2
A
A= shift()
| |||||||||||||||||||||||||||||||||||||||||||
| 65 | 7 | ▶ | my $s = ${self}->{field_rsep} || ${self}->{field_sep} || q(,); | ||||||||||||||||||||||||||||||||||||||||
|
Condition80%4/5
$self->{'field_rsep'} || $self->{'field_sep'} || ','
$self->{"field_rsep"} or $self->{"field_sep"}
MC/DC50%1/2
$self->{'field_rsep'} || $self->{'field_sep'} || ','
$self->{"field_rsep"}
$self->{"field_sep"}
Truth table66%2/3
A or B
A= $self->{"field_rsep"}B= $self->{"field_sep"}
| |||||||||||||||||||||||||||||||||||||||||||
| 66 | 7 | my $s1 = $s; | |||||||||||||||||||||||||||||||||||||||||
| 67 | #$s1 =~ s/\\/\\\\/ if $s1 =~ /\+$/; | ||||||||||||||||||||||||||||||||||||||||||
| 68 | #$s1 =~ s/\+$//; | ||||||||||||||||||||||||||||||||||||||||||
| 69 | #die $s1; | ||||||||||||||||||||||||||||||||||||||||||
| 70 | 7 | ▶ | ${self}->{field_sep} ||= q(,); | ||||||||||||||||||||||||||||||||||||||||
|
Condition100%2/2
$self->{"field_sep"} ||= ","
MC/DC100%1/1
$self->{"field_sep"} ||= ","
$self->{"field_sep"}
Truth table100%2/2
A
A= $self->{"field_sep"}
| |||||||||||||||||||||||||||||||||||||||||||
| 71 | 7 | ▶ | my $q = ${self}->{quote} ||= q("); | ||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$self->{"quote"} ||= "\""
MC/DC0%0/1
$self->{"quote"} ||= "\""
$self->{"quote"}
Truth table50%1/2
A
A= $self->{"quote"}
| |||||||||||||||||||||||||||||||||||||||||||
| 72 | 7 | ▶ | my $e = ${self}->{escape} ||= q("); | ||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$self->{"escape"} ||= "\""
MC/DC0%0/1
$self->{"escape"} ||= "\""
$self->{"escape"}
Truth table50%1/2
A
A= $self->{"escape"}
| |||||||||||||||||||||||||||||||||||||||||||
| 73 | 7 | ▶ | ${self}->{record_sep} ||= qq(\n); | ||||||||||||||||||||||||||||||||||||||||
|
Condition100%2/2
$self->{"record_sep"} ||= "\n"
MC/DC100%1/1
$self->{"record_sep"} ||= "\n"
$self->{"record_sep"}
Truth table100%2/2
A
A= $self->{"record_sep"}
| |||||||||||||||||||||||||||||||||||||||||||
| 74 | $self->{regex} = [ | ||||||||||||||||||||||||||||||||||||||||||
| 75 | 7 | qr/$q((?:(?:$e$q)|[^$q])*)$q$s?|([^$s1]+)$s?|$s/, | |||||||||||||||||||||||||||||||||||||||||
| 76 | "$e$q", | ||||||||||||||||||||||||||||||||||||||||||
| 77 | $q | ||||||||||||||||||||||||||||||||||||||||||
| 78 | ]; | ||||||||||||||||||||||||||||||||||||||||||
| 79 | 7 | return bless $self, $class; | |||||||||||||||||||||||||||||||||||||||||
| 80 | } | ||||||||||||||||||||||||||||||||||||||||||
| 81 | |||||||||||||||||||||||||||||||||||||||||||
| 82 | 38 | ▶ | sub read_fields { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
read_fields: called CC=9 SCAR=22.9
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 83 | 38 | my $self = shift; | |||||||||||||||||||||||||||||||||||||||||
| 84 | 38 | ▶ | my $str = shift || return undef; | ||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
shift() || (return undef)
MC/DC0%0/1
shift() || (return undef)
shift()
Truth table50%1/2
A
A= shift()
| |||||||||||||||||||||||||||||||||||||||||||
| 85 | 38 | my @fields = (); | |||||||||||||||||||||||||||||||||||||||||
| 86 | 38 | my $captured; | |||||||||||||||||||||||||||||||||||||||||
| 87 | 38 | ▶ | my $field_wsep = $self->{field_wsep} || $self->{field_sep}; | ||||||||||||||||||||||||||||||||||||||||
|
Condition33%1/3
$self->{'field_wsep'} || $self->{'field_sep'}
MC/DC0%0/2
$self->{'field_wsep'} || $self->{'field_sep'}
$self->{'field_wsep'}
$self->{'field_sep'}
Truth table33%1/3
A || B
A= $self->{'field_wsep'}B= $self->{'field_sep'}
| |||||||||||||||||||||||||||||||||||||||||||
| 88 | 38 | ▶ | if ($self->{trim}) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 89 | 0 | $str =~ s/\s*($field_wsep)\s*/$1/g; | |||||||||||||||||||||||||||||||||||||||||
| 90 | } | ||||||||||||||||||||||||||||||||||||||||||
| 91 | 38 | ▶ | while ($str =~ m#$self->{regex}->[0]#g) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 92 | 96 | $captured = $+; | |||||||||||||||||||||||||||||||||||||||||
| 93 | 96 | ▶ | $captured =~ s/$self->{regex}[1]/$self->{regex}[2]/g if $captured; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 94 | 96 | ▶ | last if $captured && $captured eq "\n"; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$captured and $captured eq "\n"
MC/DC0%0/2
$captured and $captured eq "\n"
$captured
$captured eq "\n"
Truth table66%2/3
A and B
A= $capturedB= $captured eq "\n"
| |||||||||||||||||||||||||||||||||||||||||||
| 95 | 96 | push(@fields,$captured); | |||||||||||||||||||||||||||||||||||||||||
| 96 | }; | ||||||||||||||||||||||||||||||||||||||||||
| 97 | 38 | ▶ | push(@fields, undef) if substr($str,-1,1) eq $field_wsep; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 98 | 38 | return @fields; | |||||||||||||||||||||||||||||||||||||||||
| 99 | } | ||||||||||||||||||||||||||||||||||||||||||
| 100 | |||||||||||||||||||||||||||||||||||||||||||
| 101 | 24 | ▶ | sub write_fields { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
write_fields: called CC=8 SCAR=22.2
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 102 | 24 | my $self = shift; | |||||||||||||||||||||||||||||||||||||||||
| 103 | 24 | my @fields = @_; | |||||||||||||||||||||||||||||||||||||||||
| 104 | 24 | my $str = ''; | |||||||||||||||||||||||||||||||||||||||||
| 105 | 24 | ▶ | my $field_rsep = $self->{field_rsep} || $self->{field_sep}; | ||||||||||||||||||||||||||||||||||||||||
|
Condition33%1/3
$self->{'field_rsep'} || $self->{'field_sep'}
MC/DC0%0/2
$self->{'field_rsep'} || $self->{'field_sep'}
$self->{'field_rsep'}
$self->{'field_sep'}
Truth table33%1/3
A || B
A= $self->{'field_rsep'}B= $self->{'field_sep'}
| |||||||||||||||||||||||||||||||||||||||||||
| 106 | 24 | $field_rsep = quotemeta($field_rsep); | |||||||||||||||||||||||||||||||||||||||||
| 107 | 24 | my $field_wsep = $self->{field_sep}; | |||||||||||||||||||||||||||||||||||||||||
| 108 | 24 | $field_wsep =~ s/\\//g; | |||||||||||||||||||||||||||||||||||||||||
| 109 | # if ($self->{ChopBlanks}) { | ||||||||||||||||||||||||||||||||||||||||||
| 110 | # $field_wsep =~ " $field_wsep "; | ||||||||||||||||||||||||||||||||||||||||||
| 111 | # } | ||||||||||||||||||||||||||||||||||||||||||
| 112 | 24 | for (@fields) { | |||||||||||||||||||||||||||||||||||||||||
| 113 | 72 | ▶ | $_ = '' if !defined $_; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 114 | 72 | ▶ | if ($self->{field_sep} eq ',') { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 115 | 36 | s/"/""/g; | |||||||||||||||||||||||||||||||||||||||||
| 116 | 36 | ▶ | s/^(.*)$/"$1"/s if /,/ or /\n/s or /"/; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%2/6
/,/ or /\n/s or /"/
/,/ or /\n/s
MC/DC0%0/3
/,/ or /\n/s or /"/
/,/
/\n/s
/"/
Truth table25%1/4
A or B or C
A= /,/B= /\n/sC= /"/
| |||||||||||||||||||||||||||||||||||||||||||
| 117 | } | ||||||||||||||||||||||||||||||||||||||||||
| 118 | 72 | $str .= $_ . $field_wsep; | |||||||||||||||||||||||||||||||||||||||||
| 119 | } | ||||||||||||||||||||||||||||||||||||||||||
| 120 | 24 | $str =~ s/$self->{field_sep}$/$self->{record_sep}/; | |||||||||||||||||||||||||||||||||||||||||
| 121 | 24 | return $str; | |||||||||||||||||||||||||||||||||||||||||
| 122 | } | ||||||||||||||||||||||||||||||||||||||||||
| 123 | 3 | 1; | |||||||||||||||||||||||||||||||||||||||||
| 124 | |||||||||||||||||||||||||||||||||||||||||||
| 125 | |||||||||||||||||||||||||||||||||||||||||||
| 126 | |||||||||||||||||||||||||||||||||||||||||||