Bug 6727 - Change on exporting and importing csv to deal with spurious CR/LF

Function _export_table_csv modified to remove CR/LF in the data
from the database to built a correct csv file.

Function _import_table_csv modified to deal with CR/LF in the data
field. When a line is found with an unproper end (a data field has not
end quote), it's concatenated to a temporary buffer until the next
line ends ok and then it's proccessed.

Signed-off-by: Nicole C. Engard <nengard@bywatersolutions.com>
Signed-off-by: Ian Walls <ian.walls@bywatersolutions.com>
Signed-off-by: Chris Cormack <chrisc@catalyst.net.nz>
This commit is contained in:
Salvador Zaragoza Rubio 2011-08-19 12:33:05 +02:00 committed by Chris Cormack
parent 97005758bb
commit 6b69260fbd

View file

@ -322,6 +322,7 @@ sub _export_table_csv
my $data; my $data;
while (my $hashRef = $sth->fetchrow_hashref) { while (my $hashRef = $sth->fetchrow_hashref) {
for (@fields) { for (@fields) {
$hashRef->{$_} =~ s/[\r\n]//g;
$$strCSV .= '"' . $hashRef->{$_} . '",'; $$strCSV .= '"' . $hashRef->{$_} . '",';
} }
chop $$strCSV; chop $$strCSV;
@ -1133,6 +1134,7 @@ sub _import_table_csv
my ($dbh, $table, $frameworkcode, $dom, $PKArray, $fields2Delete, $fields) = @_; my ($dbh, $table, $frameworkcode, $dom, $PKArray, $fields2Delete, $fields) = @_;
my $row = ''; my $row = '';
my $partialRow = '';
my $numFields = @$fields; my $numFields = @$fields;
my $fieldsNameRead = 0; my $fieldsNameRead = 0;
my @arrData; my @arrData;
@ -1145,6 +1147,17 @@ sub _import_table_csv
my $pos = 0; my $pos = 0;
while (<$dom>) { while (<$dom>) {
$row = $_; $row = $_;
# Check whether the line has an unfinished field, i.e., a field with CR/LF in its data
if ($row =~ /,"[^"]*[\r\n]+$/ || $row =~ /^[^"]+[\r\n]+$/) {
$row =~ s/[\r\n]+$//;
$partialRow .= $row;
next;
}
if ($partialRow) {
$row = $partialRow . $row;
$partialRow = '';
}
# Line OK, process it
if ($row =~ /(?:".*?",?)+/) { if ($row =~ /(?:".*?",?)+/) {
@arrData = split('","', $row); @arrData = split('","', $row);
$arrData[0] = substr($arrData[0], 1) if ($arrData[0] =~ /^"/); $arrData[0] = substr($arrData[0], 1) if ($arrData[0] =~ /^"/);