r1900@llin: dpavlin | 2009-05-29 23:37:57 +0200
authorDobrica Pavlinusic <dpavlin@rot13.org>
Fri, 29 May 2009 21:37:58 +0000 (21:37 +0000)
committerDobrica Pavlinusic <dpavlin@rot13.org>
Fri, 29 May 2009 21:37:58 +0000 (21:37 +0000)
 WebPAC::Output::DBI to dump row from normalize into database
 (so each input record can create multiple rows)

git-svn-id: svn+ssh://mjesec/home/dpavlin/svn/webpac2/trunk@1207 07558da8-63fa-0310-ba24-9fe276d99e06

TODO
lib/WebPAC/Output/DBI.pm [new file with mode: 0644]
t/5-output-dbi.t [new file with mode: 0755]
t/conf/schema.sql [new file with mode: 0644]

diff --git a/TODO b/TODO
index 601ad12..f13d117 100644 (file)
--- a/TODO
+++ b/TODO
@@ -56,6 +56,7 @@
 + 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
diff --git a/lib/WebPAC/Output/DBI.pm b/lib/WebPAC/Output/DBI.pm
new file mode 100644 (file)
index 0000000..836ef49
--- /dev/null
@@ -0,0 +1,128 @@
+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
diff --git a/t/5-output-dbi.t b/t/5-output-dbi.t
new file mode 100755 (executable)
index 0000000..3aaea66
--- /dev/null
@@ -0,0 +1,33 @@
+#!/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' );
+
diff --git a/t/conf/schema.sql b/t/conf/schema.sql
new file mode 100644 (file)
index 0000000..e5e9621
--- /dev/null
@@ -0,0 +1,8 @@
+-- drop table webpac2;
+
+create table webpac2 (
+       id serial,
+       foo int not null,
+       bar int,
+       baz text
+);