Skip to content

Commit 4cb1179

Browse files
author
H.Merijn Brand - Tux
committed
BOM handling
still needs tests
1 parent b20d73a commit 4cb1179

File tree

3 files changed

+45
-24
lines changed

3 files changed

+45
-24
lines changed

.aspell.local.pws

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1-
personal_ws-1.1 en 31
1+
personal_ws-1.1 en 32
22
AutoCommit
3+
BOM
34
ChopBlanks
45
cpan
56
CPAN

ChangeLog

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
1-
0.50 - 2017-02-28, H.Merijn Brand
1+
0.50 - 2017-05-29, H.Merijn Brand
22
* Explain more about header folding
33
* It's 2017
4+
* BOM handling
45

56
0.49 - 2016-05-12, H.Merijn Brand
67
* Simplified test-table-name generation

lib/DBD/CSV.pm

+41-22
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ sub init_valid_attributes {
127127
128128
class tables in csv_in out csv_out skip_first_row
129129
130-
null sep quote escape
130+
null sep quote escape bom
131131
)};
132132

133133
$dbh->{csv_readonly_attrs} = { };
@@ -197,6 +197,8 @@ sub bootstrap_table_meta {
197197
$meta->{csv_eol} ||= $dbh->{csv_eol} || "\r\n";
198198
exists $meta->{csv_skip_first_row} or
199199
$meta->{csv_skip_first_row} = $dbh->{csv_skip_first_row};
200+
exists $meta->{csv_bom} or
201+
$meta->{csv_bom} = exists $dbh->{bom} ? $dbh->{bom} : $dbh->{csv_bom};
200202
$self->SUPER::bootstrap_table_meta ($dbh, $meta, $table);
201203
} # bootstrap_table_meta
202204

@@ -286,6 +288,12 @@ sub open_data {
286288
: exists $meta->{col_names} ? 0 : 1;
287289
defined $meta->{skip_rows} or
288290
$meta->{skip_rows} = $skipRows;
291+
if ($meta->{csv_bom}) {
292+
my @hdr = $attrs->{csv_csv_in}->header ($meta->{fh}) or
293+
croak "Failed using the header row: ".$attrs->{csv_csv_in}->error_diag;
294+
$meta->{col_names} ||= \@hdr;
295+
$skipRows = 0 if $skipRows;
296+
}
289297
if ($skipRows--) {
290298
$array = $attrs->{csv_csv_in}->getline ($meta->{fh}) or
291299
croak "Missing first row due to ".$attrs->{csv_csv_in}->error_diag;
@@ -546,27 +554,28 @@ The preferred way of passing the arguments is by driver attributes:
546554
547555
# specify most possible flags via driver flags
548556
$dbh = DBI->connect ("dbi:CSV:", undef, undef, {
549-
f_schema => undef,
550-
f_dir => "data",
551-
f_dir_search => [],
552-
f_ext => ".csv/r",
553-
f_lock => 2,
554-
f_encoding => "utf8",
555-
556-
csv_eol => "\r\n",
557-
csv_sep_char => ",",
558-
csv_quote_char => '"',
559-
csv_escape_char => '"',
560-
csv_class => "Text::CSV_XS",
561-
csv_null => 1,
562-
csv_tables => {
563-
info => { f_file => "info.csv" }
564-
},
565-
566-
RaiseError => 1,
567-
PrintError => 1,
568-
FetchHashKeyName => "NAME_lc",
569-
}) or die $DBI::errstr;
557+
f_schema => undef,
558+
f_dir => "data",
559+
f_dir_search => [],
560+
f_ext => ".csv/r",
561+
f_lock => 2,
562+
f_encoding => "utf8",
563+
564+
csv_eol => "\r\n",
565+
csv_sep_char => ",",
566+
csv_quote_char => '"',
567+
csv_escape_char => '"',
568+
csv_class => "Text::CSV_XS",
569+
csv_null => 1,
570+
csv_bom => 0,
571+
csv_tables => {
572+
info => { f_file => "info.csv" }
573+
},
574+
575+
RaiseError => 1,
576+
PrintError => 1,
577+
FetchHashKeyName => "NAME_lc",
578+
}) or die $DBI::errstr;
570579
571580
but you may set these attributes in the DSN as well, separated by semicolons.
572581
Pay attention to the semi-colon for C<csv_sep_char> (as seen in many CSV
@@ -956,6 +965,16 @@ reset it with a false value. You can pass it to connect, or set it later:
956965
957966
$dbh->{csv_null} = 1;
958967
968+
=item csv_bom
969+
X<csv_bom>
970+
971+
With this option set, the CSV parser will try to detect BOM (Byte Order Mark)
972+
in the header line. This requires L<Text::CSV_XS> version 1.22 or higher.
973+
974+
$dbh = DBI->connect ("dbi:CSV:", "", "", { csv_bom => 1 });
975+
976+
$dbh->{csv_bom} = 1;
977+
959978
=item csv_tables
960979
X<csv_tables>
961980

0 commit comments

Comments
 (0)