| 1 | # $Header: /home/mike/cvs/mike/zSQLgate/lib/Net/Z3950/DBIServer/GRS1.pm,v 1.4 2005-04-11 18:48:20 mike Exp $ | ||
| 2 | |||
| 3 | package Net::Z3950::DBIServer::GRS1; | ||
| 4 | use Net::Z3950::GRS1; | ||
| 5 | use strict; | ||
| 6 | |||
| 7 | =head1 NAME | ||
| 8 | |||
| 9 | Net::Z3950::DBIServer::GRS1 - build GRS-1 records for DBIServer | ||
| 10 | |||
| 11 | =head1 SYNOPSIS | ||
| 12 | |||
| 13 | $rec = Net::Z3950::DBIServer::GRS1::format( | ||
| 14 | { title=>'Mr', forename=>'Eric', surname=>'1/2 Bee' }, | ||
| 15 | $config); | ||
| 16 | |||
| 17 | =head1 DESCRIPTION | ||
| 18 | |||
| 19 | This non-OO module exists only to provide a single function which | ||
| 20 | formats a set of fields as a GRS-1 record. | ||
| 21 | |||
| 22 | =head1 METHODS | ||
| 23 | |||
| 24 | =head2 format() | ||
| 25 | |||
| 26 | $rec = Net::Z3950::DBIServer::GRS1::format($hashref, $config); | ||
| 27 | |||
| 28 | Creates and returns, as an unblessed string, a new GRS-1 record | ||
| 29 | containing the specified fields according to the configuration | ||
| 30 | specified in the database-and-record-syntax-specific configuration | ||
| 31 | segment I<$config>, of type C<Net::Z3950::DBIServer::Config::PerDB>. | ||
| 32 | |||
| 33 | Because of the way the underlying C<SimpleServer> module deals with | ||
| 34 | GRS-1 records, the record is returned as a human-readable string. | ||
| 35 | That's surprising - at least it surprises me. But I've had to deal | ||
| 36 | with it, so I don't see why you shouldn't too. | ||
| 37 | |||
| 38 | =cut | ||
| 39 | |||
| 40 | sub format { | ||
| 41 | my($hashref, $config) = @_; | ||
| 42 | |||
| 43 | my $grs = new Net::Z3950::GRS1(); | ||
| 44 | foreach my $field ($config->fields()) { | ||
| 45 | my $fieldName = $field->tagname(); | ||
| 46 | my $sqlField = $field->columnName(); | ||
| 47 | my $data = Net::Z3950::DBIServer::XML::make_data($sqlField, $hashref); | ||
| 48 | next if !defined $data || $data eq ""; | ||
| 49 | my $tagpath = $field->tagpath(); | ||
| 50 | #{ use Data::Dumper; warn "tagpath($fieldName) = " . Dumper($tagpath) } | ||
| 51 | |||
| 52 | # Actually, our internal record and the SimpleServer module's | ||
| 53 | # GRS1 record object are not very good matches: the former | ||
| 54 | # does not support sub-records and the latter does not support | ||
| 55 | # tag-paths of more than one element. | ||
| 56 | # ### We should implement long tag-paths with sub-records. | ||
| 57 | my $first = $tagpath->[0]; | ||
| 58 | warn "ignoring second and subsequent elements of tagpath for '$fieldName'" | ||
| 59 | if @$tagpath > 1; | ||
| 60 | $grs->AddElement(@$first, Net::Z3950::GRS1::ElementData::String, | ||
| 61 | $data); | ||
| 62 | } | ||
| 63 | |||
| 64 | # For reasons too bizarre to contemplate, we return the GRS-1 | ||
| 65 | # record rendered as a string, which is then re-parsed in | ||
| 66 | # SimpleServer to yield a GRS-1 record in C structs. Clearly we | ||
| 67 | # should have a better arrangement. | ||
| 68 | |||
| 69 | my $res; | ||
| 70 | $grs->Render(POOL => \$res); | ||
| 71 | #warn "Record is: '$res'"; | ||
| 72 | return $res; | ||
| 73 | } | ||
| 74 | |||
| 75 | |||
| 76 | =head1 AUTHOR | ||
| 77 | |||
| 78 | Mike Taylor E<lt>mike@miketaylor.org.ukE<gt> | ||
| 79 | |||
| 80 | First version Tuesday 9th July 2002. | ||
| 81 | |||
| 82 | =head1 SEE ALSO | ||
| 83 | |||
| 84 | C<Net::Z3950::DBIServer> | ||
| 85 | is the module that uses this. | ||
| 86 | |||
| 87 | =cut | ||
| 88 | |||
| 89 | |||
| 90 | 1; |