NAME
DBIx::TableLoader::CSV - Easily load a CSV into a database table
VERSION
version 1.102
SYNOPSIS
my $dbh = DBI->connect(@connection_args);
DBIx::TableLoader::CSV->new(dbh => $dbh, file => $path_to_csv)->load();
# interact with new database table full of data in $dbh
DESCRIPTION
This is a subclass of DBIx::TableLoader that handles the common operations of reading a CSV file (using the powerful Text::CSV (which uses Text::CSV_XS if available)).
This module simplifies the task of transforming a CSV file into a database table. This functionality was the impetus for the parent module (DBIx::TableLoader).
In most cases simply calling load()
is sufficient (see "load" in DBIx::TableLoader). The methods defined by this subclass are documented for completeness.
METHODS
new
Accepts all options described in "OPTIONS" in DBIx::TableLoader plus some CSV specific options.
See "OPTIONS".
get_raw_row
Returns $csv->getline($io)
.
After the last row is returned this will check "eof" in Text::CSV and croak with the message from "error_diag" in Text::CSV as described by "SYNOPSIS" in Text::CSV. (If you wish to disable this behavior you can set ignore_csv_errors => 1
in the constructor.)
default_name
If the name
option is not provided, and the file
option is, this returns the file basename.
Falls back to 'csv'
.
prepare_data
This is called automatically from the constructor to make things as simple and automatic as possible.
Load
csv_class
if it is not.Instantiate
csv_class
withcsv_defaults
andcsv_opts
.Open the
file
provided unlessio
is passed instead.Discard the first row if
columns
is provided andno_header
is not.
OPTIONS
There are many options available for configuration. Options specific to this module are listed below. Also see "OPTIONS" in DBIx::TableLoader for options from the base module.
Basic usage:
csv_opts
- Hashref of options to pass to thenew
method ofcsv_class
See Text::CSV for its list of accepted options.
file
- Path to a csv fileThe file will be opened (unless
io
is provided) and its basename will be the default table name (which can be overwritten with thename
option).file_encoding
- The encoding of the CSV file.If specified this is appended to the
open
mode as:encoding(ENCODING)
.
Options for more customization/control:
csv
- A Text::CSV compatible object instanceIf not supplied an instance will be created using
$csv_class->new(\%csv_opts)
.csv_class
- The class to instantiate ifcsv
is not suppliedDefaults to
Text::CSV
(which will attempt to load Text::CSV_XS and fall back to Text::CSV_PP).csv_defaults
- Hashref of default options forcsv_class
constructorIncludes
{ binary => 1 }
(as encouraged by Text::CSV); To turn off thebinary
option you can pass{ binary => 0 }
tocsv_opts
. If you are using a differentcsv_class
that does not accept thebinary
option you may need to overwrite this with an empty hash.file_open_layers
- String of arbitrary PerlIO layersto apply when opening the file.
ignore_csv_errors
- Boolean (defaults to false)If Text::CSV fails to parse a row it will abort and skip the rest of the file. This module detects parser errors and will
die
with the message from "error_diag" in Text::CSV upon failure to read the whole file. (This behavior is similar to (but separate from) settingauto_diag => 2
in the csv options.) Set this option to a true value if you want to accept partially read CSV files rather than getting an error. Note that other exceptions can still be thrown (including failure to open the file or if a misconfigured parser or malformed CSV returns a row with an inconsistent number of columns).io
- A filehandle or IO-like object from which to read CSV linesThis will be used as
$csv->getline($io)
. When providing this option you can still providefile
if you want the table name to be determined automatically (but no attempt will be made to openfile
).name
- Table nameIf not given the table name will be set to the file basename or
'csv'
iffile
is not provided.no_header
- BooleanUsually the first row [header] of a CSV is the column names. If you specify
columns
this module assumes you are overwriting the usual header row so the first row of the CSV will be discarded. If there is no header row on the CSV (the first row is data), you must setno_header
to true in order to preserve the first row of the CSV.
SEE ALSO
Text::CSV::Auto - Alternative project automating CSV usage
SUPPORT
Perldoc
You can find documentation for this module with the perldoc command.
perldoc DBIx::TableLoader::CSV
Websites
The following websites have more information about this module, and may be of help to you. As always, in addition to those websites please use your favorite search engine to discover more resources.
Search CPAN
The default CPAN search engine, useful to view POD in HTML format.
RT: CPAN's Bug Tracker
The RT ( Request Tracker ) website is the default bug/issue tracking system for CPAN.
http://rt.cpan.org/NoAuth/Bugs.html?Dist=DBIx-TableLoader-CSV
CPAN Ratings
The CPAN Ratings is a website that allows community ratings and reviews of Perl modules.
CPAN Testers
The CPAN Testers is a network of smokers who run automated tests on uploaded CPAN distributions.
CPAN Testers Matrix
The CPAN Testers Matrix is a website that provides a visual overview of the test results for a distribution on various Perls/platforms.
CPAN Testers Dependencies
The CPAN Testers Dependencies is a website that shows a chart of the test results of all dependencies for a distribution.
Bugs / Feature Requests
Please report any bugs or feature requests by email to bug-dbix-tableloader-csv at rt.cpan.org
, or through the web interface at http://rt.cpan.org/NoAuth/ReportBug.html?Queue=DBIx-TableLoader-CSV. You will be automatically notified of any progress on the request by the system.
Source Code
https://github.com/rwstauner/DBIx-TableLoader-CSV
git clone https://github.com/rwstauner/DBIx-TableLoader-CSV.git
AUTHOR
Randy Stauner <rwstauner@cpan.org>
COPYRIGHT AND LICENSE
This software is copyright (c) 2011 by Randy Stauner.
This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself.