DBIx-Easy
view release on metacpan or search on metacpan
Easy/Import.pm view on Meta::CPAN
# Import.pm - Easy to Use DBI import interface
# Copyright (C) 2004-2012 Stefan Hornburg (Racke) <racke@linuxia.de>
# Authors: Stefan Hornburg (Racke) <racke@linuxia.de>
# Maintainer: Stefan Hornburg (Racke) <racke@linuxia.de>
# Version: 0.19
# This file is free software; you can redistribute it and/or modify it
# under the terms of the GNU General Public License as published by the
# Free Software Foundation; either version 2, or (at your option) any
# later version.
# This file is distributed in the hope that it will be
# useful, but WITHOUT ANY WARRANTY; without even the implied warranty
# of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# General Public License for more details.
# You should have received a copy of the GNU General Public License
# along with this file; see the file COPYING. If not, write to the Free
# Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
package DBIx::Easy::Import;
use strict;
use vars qw($VERSION @ISA @EXPORT @EXPORT_OK);
require Exporter;
@ISA = qw(Exporter);
# Items to export into callers namespace by default. Note: do not export
# names by default without a very good reason. Use EXPORT_OK instead.
# Do not simply export all your public functions/methods/constants.
@EXPORT = qw(
);
# Public variables
$VERSION = '0.19';
use DBI;
use DBIx::Easy;
sub new {
my $proto = shift;
my $class = ref($proto) || $proto;
my $self = {driver => shift, database => shift};
bless ($self, $class);
}
sub update {
my ($self, %params) = @_;
$self->_do_import(%params);
}
sub initialize {
my ($self, $file, $format) = @_;
my ($sep_char);
$format = uc($format);
if ($format =~ /^CSV/) {
$format = 'CSV';
if ($') {
$sep_char = $';
$sep_char =~ s/^\s+//;
$sep_char =~ s/\s+$//;
}
eval {
require Text::CSV_XS;
};
if ($@) {
die "$0: couldn't load module Text::CSV_XS\n";
}
$self->{func} = \&get_columns_csv;
$self->{parser} = new Text::CSV_XS ({'binary' => 1, 'sep_char' => $sep_char});
} elsif ($format eq 'XLS') {
eval {
require Spreadsheet::ParseExcel;
};
if ($@) {
die "$0: couldn't load module Spreadsheet::ParseExcel\n";
}
$self->{parser} = new Spreadsheet::ParseExcel;
} elsif ($format eq 'TAB') {
$self->{func} = \&get_columns_tab;
} else {
die qq{$0: unknown format "$format"}, "\n";
}
if ($file) {
# read input from file
require IO::File;
$self->{fd_input} = new IO::File;
$self->{fd_input}->open($file)
|| die "$0: couldn't open $file: $!\n";
} else {
# read input from standard input
require IO::Handle;
$self->{fd_input} = new IO::Handle;
$self->{fd_input}->fdopen(fileno(STDIN),'r');
}
}
sub _do_import {
my ($self, %params) = @_;
my ($format, $sep_char, %colmap, %hcolmap);
$self->{colflag} = 1;
( run in 1.021 second using v1.01-cache-2.11-cpan-5735350b133 )