+ dump stats into var/stats.yml
+ WebPAC::Output::SWISH for indexing with swish-e [2.35]
+ output modules can now define add_row as opposed to add to get input row before normalization
++ WebPAC::Output::DBI to dump row from normalize into database (so each input record can create multiple rows)
- implement attribute labels in webpac2.cgi
- fix encoding for swish-e and/or webpac2.cgi
- marc_clone to copy records/fields/indicators from input marc
--- /dev/null
+package WebPAC::Output::DBI;
+
+use warnings;
+use strict;
+
+use base qw/WebPAC::Common WebPAC::Output Class::Accessor/;
+__PACKAGE__->mk_accessors(qw(
+ input
+
+ dsn
+ user
+ passwd
+
+ schema
+
+ table
+));
+
+use Data::Dump qw/dump/;
+use DBI;
+use File::Slurp;
+
+=head1 NAME
+
+WebPAC::Output::DBI - feed data into RDBMS via DBI
+
+=head1 FUNCTIONS
+
+=head2 init
+
+ $out->init;
+
+=cut
+
+sub init {
+ my $self = shift;
+ my $log = $self->_get_logger;
+
+ $log->info($self->dsn);
+
+ $self->{_rows} = [];
+
+ $self->{_dbh} = DBI->connect( $self->dsn, $self->user, $self->passwd, { RaiseError => 1 } );
+
+ eval {
+ $self->{_dbh}->do( scalar read_file( $self->schema ) ) if -e $self->schema;
+ };
+
+ return 1;
+}
+
+
+=head2 add
+
+Adds one entry to database.
+
+ $out->add( 42, $ds );
+
+=cut
+
+sub add {
+ my $self = shift;
+
+ my ( $id, $ds ) = @_;
+
+ return unless defined $ds->{_rows};
+
+ my $log = $self->_get_logger;
+
+ $id = $self->input . '-' . $id if $self->input;
+
+ my @rows = @{ $ds->{_rows} };
+ foreach my $row ( @rows ) {
+
+ my @cols = sort keys %$row;
+
+ my $sql = join( ''
+ , 'insert into '
+ , ( $self->table || $self->input || 'webpac2' )
+ . ' (' . join(',', @cols), ')'
+ , ' values ('
+ , join(',', map { '?' } 0 .. $#cols )
+ , ')'
+ );
+ warn "# SQL: $sql\n";
+ my $sth = $self->{_dbh}->prepare( $sql );
+
+ warn "# row ",dump( $row );
+ $sth->execute( map { $row->{$_} } @cols );
+ }
+
+ push @{ $self->{_rows} }, $_ foreach @rows;
+
+ return 1;
+}
+
+=head2 finish
+
+ $out->finish;
+
+=cut
+
+sub finish {
+ my $self = shift;
+
+ my $log = $self->_get_logger();
+
+ $log->info('finish and dump data into database');
+
+ warn dump( $self->{_rows} );
+
+ return 1;
+}
+
+=head1 AUTHOR
+
+Dobrica Pavlinusic, C<< <dpavlin@rot13.org> >>
+
+=head1 COPYRIGHT & LICENSE
+
+Copyright 2009 Dobrica Pavlinusic, All Rights Reserved.
+
+This program is free software; you can redistribute it and/or modify it
+under the same terms as Perl itself.
+
+=cut
+
+1; # End of WebPAC::Output::CouchDB
--- /dev/null
+#!/usr/bin/perl -w
+
+use strict;
+use blib;
+
+use Test::More tests => 6;
+
+BEGIN {
+ use lib 'lib';
+ use_ok( 'WebPAC::Test' );
+ use_ok( 'WebPAC::Output::DBI' );
+}
+
+ok(my $out = new WebPAC::Output::DBI({
+ dsn => 'dbi:Pg:dbname=webpac2',
+ schema => "$abs_path/conf/schema.sql",
+ %LOG
+}), "new");
+
+ok( $out->init, 'init' );
+
+my $ds = {
+ '_rows' => [
+ { foo => 42 },
+ { foo => 1, bar => 11 },
+ { foo => 99, baz => 'text' },
+ ],
+};
+
+ok( $out->add( 42, $ds ), 'add 42' );
+
+ok( $out->finish, 'finish' );
+