1 package WebPAC::Input::DBF;
7 use WebPAC::Input::Helper;
8 use base qw/WebPAC::Common WebPAC::Input::Helper/;
10 use Data::Dump qw/dump/;
11 use Encode qw/encode_utf8/;
12 use YAML qw/LoadFile DumpFile/;
16 WebPAC::Input::DBF - support for reading DBF tables
24 our $VERSION = '0.01';
28 Read data from DBF tables (do you remember Clipper applications?) and create
29 pseudo-MARC records from them.
31 my $ll_db = new WebPAC::Input::DBF(
32 path => '/path/to/database.dbf',
39 Returns new low-level input API object
41 my $ll_db = new WebPAC::Input::DBF(
42 path => '/path/to/database.dbf'
43 mapping_path => '/path/to/input/dbf/mapping.yml',
45 my ($l,$field_nr) = @_;
46 # do something with $l which is line of input file
61 path to mapping YAML which will be created on first run
74 my $log = $self->_get_logger();
76 $log->logconfess("this module requires input_config") unless ( $arg->{input_config} );
78 my $db = XBase->new( $arg->{path} ) || $log->logdie("can't open ", $arg->{path}, ": $!");
80 my $size = $db->last_record;
82 $log->info("opening DBF database '$arg->{path}' with $size records");
84 my $mapping_path = $arg->{input_config}->{mapping_path} || $self->{input_config}->{mapping_path};
87 if ( ! $mapping_path || ! -e $mapping_path ) {
88 $log->debug("didn't found any mapping_path in configuration", sub { dump( $arg->{input_config} ) });
90 foreach my $field ( $db->field_names ) {
91 push @$mapping, { $field => { '900' => 'x' } };
94 my $mapping_path = $arg->{path};
95 $mapping_path =~ s!^.+/([^/]+)\.dbf!$1.yml!;
97 $log->logdie("mapping file $mapping_path allready exists, aborting.") if ( -e $mapping_path );
99 DumpFile( $mapping_path, { mapping => $mapping } ) ||
100 $log->logdie("can't write template file for mapping_path $mapping_path: $!");
102 $log->logdie("template file for mapping_path created as $mapping_path");
105 $mapping = LoadFile( $mapping_path ) || $log->logdie("can't open $mapping_path: $!");
106 $log->logdie("missing top-level mapping key in $mapping_path") unless ( $mapping->{mapping} );
107 $mapping = $mapping->{mapping};
108 $log->debug("using mapping from $mapping_path = ", sub { dump($mapping) });
111 foreach my $mfn ( 1 .. $size ) {
113 my $row = $db->get_record_as_hash( $mfn );
115 $log->debug("dbf row = ", sub { dump( $row ) });
121 # fixme -- this *will* break given wrong structure!
122 foreach my $m ( @$mapping ) {
123 my $db_field = (keys %$m)[0];
124 my ( $f, $sf ) = %{ $m->{$db_field} };
125 push @{ $record->{$f} }, '^' . $sf . $row->{$db_field} if ( defined( $row->{$db_field} ) && $row->{$db_field} ne '' );
128 $self->{_rows}->{ $mfn } = $record;
129 $log->debug("created row $mfn ", dump( $record ));
132 $self->{size} = $size;
134 $self ? return $self : return undef;
139 Return record with ID C<$mfn> from database
141 my $rec = $ll_db->fetch_rec( $mfn, $filter_coderef );
148 my ($mfn, $filter_coderef) = @_;
150 my $rec = $self->_to_hash(
152 row => $self->{_rows}->{$mfn},
153 hash_filter => $filter_coderef,
156 my $log = $self->_get_logger();
157 $log->debug("fetch_rec($mfn) = ", dump($rec));
164 Return number of records in database
166 my $size = $ll_db->size;
172 return $self->{size};
177 Dobrica Pavlinusic, C<< <dpavlin@rot13.org> >>
179 =head1 COPYRIGHT & LICENSE
181 Copyright 2007 Dobrica Pavlinusic, All Rights Reserved.
183 This program is free software; you can redistribute it and/or modify it
184 under the same terms as Perl itself.
188 1; # End of WebPAC::Input::DBF