/[webpac2]/trunk/lib/WebPAC/Input/DBI.pm
This is repository of my old source code which isn't updated any more. Go to git.rot13.org for current projects!
ViewVC logotype

Contents of /trunk/lib/WebPAC/Input/DBI.pm

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1360 - (show annotations)
Tue Mar 15 22:50:20 2011 UTC (13 years, 1 month ago) by dpavlin
File size: 2075 byte(s)
we are not ignoring $mfn
1 package WebPAC::Input::DBI;
2
3 use warnings;
4 use strict;
5
6 use WebPAC::Input;
7 use base qw/WebPAC::Common Class::Accessor/;
8 __PACKAGE__->mk_accessors(qw(
9 dsn
10 user
11 passwd
12 path
13 ));
14
15 use Encode;
16 use Data::Dump qw/dump/;
17 use DBI;
18 use File::Slurp;
19
20 =head1 NAME
21
22 WebPAC::Input::DBI - read data from RDBMS using DBI
23
24 =cut
25
26 =head1 FUNCTIONS
27
28 =head2 new
29
30 my $input = new WebPAC::Input::DBI(
31 dsn => 'dbi:SQLite:dbname=/dev/shm/test.sqlite',
32 user => '',
33 passwd => '',
34 path => '/path/to.sql',
35 );
36
37 =back
38
39 =cut
40
41 sub new {
42 my $class = shift;
43 my $self = {@_};
44 bless($self, $class);
45
46 my $arg = {@_};
47
48 my $sql = read_file $self->path;
49
50 my $log = $self->_get_logger;
51 $log->debug( "dsn: ", $self->dsn );
52
53 my $dbh = DBI->connect( $self->dsn, $self->user, $self->passwd, { RaiseError => 1 } );
54
55 $log->debug( "sql ",$self->path, "\n", $sql );
56
57 my $sth = $dbh->prepare( $sql );
58 $sth->execute;
59
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...
62 $self->{size} = 0;
63
64 while ( my $row = $sth->fetchrow_hashref ) {
65 push @{ $self->{_rec} }, $row;
66 $self->{size}++;
67 }
68
69 $log->info( $self->dsn, " query produced ", $self->size, " records");
70
71 $self ? return $self : return undef;
72 }
73
74 =head2 fetch_rec
75
76 Return record with ID C<$mfn> from database
77
78 my $rec = $input->fetch_rec( $mfn, $filter_coderef );
79
80 =cut
81
82 sub fetch_rec {
83 my ( $self, $mfn, $filter_coderef ) = @_;
84
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} ];
89 }
90 return $rec;
91 }
92
93
94 =head2 size
95
96 Return number of records in database
97
98 my $size = $input->size;
99
100 =cut
101
102 sub size {
103 my $self = shift;
104 return $self->{size};
105 }
106
107 =head1 AUTHOR
108
109 Dobrica Pavlinusic, C<< <dpavlin@rot13.org> >>
110
111 =head1 COPYRIGHT & LICENSE
112
113 Copyright 2011 Dobrica Pavlinusic, All Rights Reserved.
114
115 This program is free software; you can redistribute it and/or modify it
116 under the same terms as Perl itself.
117
118 =cut
119
120 1; # End of WebPAC::Input::DBI

  ViewVC Help
Powered by ViewVC 1.1.26