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 );
82 Records are always returned sequentially, ignoring C<$mfn> and filter
87 my ( $self, $mfn, $filter_coderef ) = @_;
89 my $rec = { '000' => [ $mfn ] };
90 my $row = $self->{_rec}->[$mfn-1] || die "no record $mfn";
91 foreach my $c ( keys %$row ) {
92 $rec->{$c} = [ $row->{$c} ];
100 Return number of records in database
102 my $size = $input->size;
108 return $self->{size};
113 Dobrica Pavlinusic, C<< <dpavlin@rot13.org> >>
115 =head1 COPYRIGHT & LICENSE
117 Copyright 2011 Dobrica Pavlinusic, All Rights Reserved.
119 This program is free software; you can redistribute it and/or modify it
120 under the same terms as Perl itself.
124 1; # End of WebPAC::Input::DBI