#!/usr/bin/env perl # vim:ts=8 sw=4 sts=4 ai require v5.6.1; use strict; use warnings; =head1 NAME sqlw_import - import a Field:Value datafile into an SQLite database. =head1 VERSION This describes version B<0.1001> of sqlw_import. =cut our $VERSION = '0.1001'; =head1 SYNOPSIS sqlw_import --help | --manpage | --version sqlw_import --datafile I [--row_delim I ] --sqlitedb I --table I =head1 DESCRIPTION This script inserts the contents of a Field:Value format datafile into a table in an SQLite database. The table and the database must have already been created. =head1 OPTIONS =over =item --datafile I The input data file. =item --help Print help message and exit. =item --manpage Print the full help documentation (manual page) and exit. =item --row_delim The end of a row of data is marked by this marker on a line by itself. (default: '=') =item --verbose Print informational messages. =item --version Print version information and exit. =back =head1 CONVERSION =head2 Field:Value Format The input data file is in the form of Field:Value pairs, with each record separated by a line with '=' on it. =head2 SQLite This script expects both the SQLite database and the destination table to have already been created. The table is expected to have the same column names as the fields in the FieldVals file. =head1 REQUIRES Getopt::Long Pod::Usage Getopt::ArgvFile SQLite::Work =head1 SEE ALSO perl(1) Getopt::Long Getopt::ArgvFile Pod::Usage =cut use Getopt::Long 2.34; use Getopt::ArgvFile qw(argvFile); use Pod::Usage; use Data::Dumper; use SQLite::Work; #======================================================== # Subroutines sub init_data ($) { my $data_ref = shift; # options my %default_conf = (); $default_conf{debug} = 0; $default_conf{manpage} = 0; $default_conf{version} = 0; $default_conf{verbose} = 0; $default_conf{datafile} = ''; $default_conf{row_delim} = '='; $data_ref->{options} = \%default_conf; } # init_data sub process_args ($) { my $data_ref = shift; my $ok = 1; argvFile(home=>1,current=>1,startupFilename=>'.sqlw_importrc'); pod2usage(2) unless @ARGV; my $op = new Getopt::Long::Parser; $op->configure(qw(auto_version auto_help)); $op->getoptions($data_ref->{options}, 'verbose!', 'manpage', 'debug!', 'datafile=s', 'row_delim=s', 'sqlitedb=s', 'table=s', ) or pod2usage(2); if ($data_ref->{options}->{'manpage'}) { pod2usage({ -message => "$0 version $VERSION", -exitval => 0, -verbose => 2, }); } if (!$data_ref->{options}->{datafile}) { pod2usage({ -message => "$0 version $VERSION: no datafile", -exitval => 1, -verbose => 0, }); } if (!$data_ref->{options}->{sqlitedb}) { pod2usage({ -message => "$0 version $VERSION: no SQLite database", -exitval => 1, -verbose => 0, }); } if (!$data_ref->{options}->{table}) { pod2usage({ -message => "$0 version $VERSION: no SQLite table", -exitval => 1, -verbose => 0, }); } } # process_args sub convert_file ($) { my $data_ref = shift; print STDERR "datafile: ", $data_ref->{options}->{datafile}, " db: ", $data_ref->{options}->{sqlitedb}, " table: ", $data_ref->{options}->{table}, "\n" if ($data_ref->{options}->{verbose}); if ($data_ref->{options}->{debug}) { print STDERR Data::Dumper->Dump([$data_ref], [qw(sqlw_import)]); } my $sq = SQLite::Work->new(database=>$data_ref->{options}->{sqlitedb}); if (!$sq) { warn "failed to create DB object -- aborting\n"; return 0; } if (!$sq->do_connect()) { warn "failed to connect -- aborting\n"; return 0; } my $num_recs = $sq->do_import_fv(table=>$data_ref->{options}->{table}, row_delim=>$data_ref->{options}->{row_delim}, datafile=>$data_ref->{options}->{datafile}); $sq->do_disconnect(); print STDERR "Imported $num_recs records.\n" if ($data_ref->{options}->{verbose}); } # convert_file #======================================================== # Main MAIN: { my %data = (); init_data(\%data); process_args(\%data); convert_file(\%data); } =head1 BUGS Please report any bugs or feature requests to the author. =head1 AUTHOR Kathryn Andersen (RUBYKAT) perlkat AT katspace dot com http://www.katspace.com =head1 COPYRIGHT AND LICENCE Copyright (c) 2004 by Kathryn Andersen This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself. =cut __END__