1 package WebPAC::Input::DBI;
7 use base qw/WebPAC::Common Class::Accessor/;
8 __PACKAGE__->mk_accessors(qw(
16 use Data::Dump qw/dump/;
22 WebPAC::Input::DBI - read data from RDBMS using DBI
30 my $input = new WebPAC::Input::DBI(
31 dsn => 'dbi:SQLite:dbname=/dev/shm/test.sqlite',
34 path => '/path/to.sql',
48 my $sql = read_file $self->path;
50 my $log = $self->_get_logger;
51 $log->debug( "dsn: ", $self->dsn );
53 my $dbh = DBI->connect( $self->dsn, $self->user, $self->passwd, { RaiseError => 1 } );
55 $log->debug( "sql ",$self->path, "\n", $sql );
57 my $sth = $dbh->prepare( $sql );
60 # XXX this should really be in fetch_rec, but DBD::SQLite doesn't return
61 # $sth->rows correctly, and we really need number of rows...
64 while ( my $row = $sth->fetchrow_hashref ) {
65 push @{ $self->{_rec} }, $row;
69 $log->info( $self->dsn, " query produced ", $self->size, " records");
71 $self ? return $self : return undef;
76 Return record with ID C<$mfn> from database
78 my $rec = $input->fetch_rec( $mfn, $filter_coderef );
83 my ( $self, $mfn, $filter_coderef ) = @_;
85 my $rec = { '000' => [ $mfn ] };
86 my $row = $self->{_rec}->[$mfn-1] || die "no record $mfn";
87 foreach my $c ( keys %$row ) {
88 $rec->{$c} = [ $row->{$c} ];
96 Return number of records in database
98 my $size = $input->size;
104 return $self->{size};
109 Dobrica Pavlinusic, C<< <dpavlin@rot13.org> >>
111 =head1 COPYRIGHT & LICENSE
113 Copyright 2011 Dobrica Pavlinusic, All Rights Reserved.
115 This program is free software; you can redistribute it and/or modify it
116 under the same terms as Perl itself.
120 1; # End of WebPAC::Input::DBI