1 # $Id: ZOOM.pm,v 1.5 2005-10-12 14:33:40 mike Exp $
8 # Member naming convention: hash-element names which begin with an
9 # underscore represent underlying ZOOM-C object descriptors; those
10 # which lack them represent Perl's ZOOM objects. (The same convention
11 # is used in naming local variables where appropriate.)
13 # So, for example, the ZOOM::Connection class has an {_conn} element,
14 # which is a pointer to the ZOOM-C Connection object; but the
15 # ZOOM::ResultSet class has a {conn} element, which is a reference to
16 # the Perl-level Connection object by which it was created. (It may
17 # be that we find we have no need for these references, but for now
20 # To get at the underlying ZOOM-C connection object of a result-set
21 # (if you ever needed to do such a thing, which you probably don't)
22 # you'd use $rs->{conn}->_conn().
24 # ----------------------------------------------------------------------------
26 # The "Error" package contains constants returned as error-codes.
28 sub NONE { Net::Z3950::ZOOM::ERROR_NONE }
29 sub CONNECT { Net::Z3950::ZOOM::ERROR_CONNECT }
30 sub MEMORY { Net::Z3950::ZOOM::ERROR_MEMORY }
31 sub ENCODE { Net::Z3950::ZOOM::ERROR_ENCODE }
32 sub DECODE { Net::Z3950::ZOOM::ERROR_DECODE }
33 sub CONNECTION_LOST { Net::Z3950::ZOOM::ERROR_CONNECTION_LOST }
34 sub INIT { Net::Z3950::ZOOM::ERROR_INIT }
35 sub INTERNAL { Net::Z3950::ZOOM::ERROR_INTERNAL }
36 sub TIMEOUT { Net::Z3950::ZOOM::ERROR_TIMEOUT }
37 sub UNSUPPORTED_PROTOCOL { Net::Z3950::ZOOM::ERROR_UNSUPPORTED_PROTOCOL }
38 sub UNSUPPORTED_QUERY { Net::Z3950::ZOOM::ERROR_UNSUPPORTED_QUERY }
39 sub INVALID_QUERY { Net::Z3950::ZOOM::ERROR_INVALID_QUERY }
41 # The "Event" package contains constants returned by last_event()
43 sub NONE { Net::Z3950::ZOOM::EVENT_NONE }
44 sub CONNECT { Net::Z3950::ZOOM::EVENT_CONNECT }
45 sub SEND_DATA { Net::Z3950::ZOOM::EVENT_SEND_DATA }
46 sub RECV_DATA { Net::Z3950::ZOOM::EVENT_RECV_DATA }
47 sub TIMEOUT { Net::Z3950::ZOOM::EVENT_TIMEOUT }
48 sub UNKNOWN { Net::Z3950::ZOOM::EVENT_UNKNOWN }
49 sub SEND_APDU { Net::Z3950::ZOOM::EVENT_SEND_APDU }
50 sub RECV_APDU { Net::Z3950::ZOOM::EVENT_RECV_APDU }
51 sub RECV_RECORD { Net::Z3950::ZOOM::EVENT_RECV_RECORD }
52 sub RECV_SEARCH { Net::Z3950::ZOOM::EVENT_RECV_SEARCH }
55 # ----------------------------------------------------------------------------
57 package ZOOM::Exception;
61 my($code, $message, $addinfo) = @_;
77 return $this->{message};
82 return $this->{addinfo};
86 # ----------------------------------------------------------------------------
88 package ZOOM::Connection;
92 my($host, $port) = @_;
94 my $_conn = Net::Z3950::ZOOM::connection_new($host, $port);
95 my($errcode, $errmsg, $addinfo) = (undef, "dummy", "dummy");
96 $errcode = Net::Z3950::ZOOM::connection_error($_conn, $errmsg, $addinfo);
97 die new ZOOM::Exception($errcode, $errmsg, $addinfo) if $errcode;
106 # PRIVATE within this class
110 my $_conn = $this->{_conn};
111 die "{_conn} undefined: has this ResultSet been destroy()ed?"
119 my($key, $value) = @_;
121 my $oldval = Net::Z3950::ZOOM::connection_option_get($this->_conn(), $key);
122 Net::Z3950::ZOOM::connection_option_set($this->_conn(), $key, $value)
132 my $_rs = Net::Z3950::ZOOM::connection_search_pqf($this->_conn(), $query);
133 my($errcode, $errmsg, $addinfo) = (undef, "dummy", "dummy");
134 $errcode = Net::Z3950::ZOOM::connection_error($this->_conn(),
136 die new ZOOM::Exception($errcode, $errmsg, $addinfo) if $errcode;
138 return _new ZOOM::ResultSet($this, $query, $_rs);
144 Net::Z3950::ZOOM::connection_destroy($this->_conn());
145 $this->{_conn} = undef;
149 # ----------------------------------------------------------------------------
151 package ZOOM::ResultSet;
155 die "You can't create $class objects directly";
158 # PRIVATE to ZOOM::Connection::search()
161 my($conn, $query, $_rs) = @_;
170 # PRIVATE within this class
174 my $_rs = $this->{_rs};
175 die "{_rs} undefined: has this ResultSet been destroy()ed?"
184 return Net::Z3950::ZOOM::resultset_size($this->_rs());
191 my $_rec = Net::Z3950::ZOOM::resultset_record($this->_rs(), $which);
192 ### Check for error -- but how?
194 # For some reason, I have to use the explicit "->" syntax in order
195 # to invoke the ZOOM::Record constructor here, even though I don't
196 # have to do the same for _new ZOOM::ResultSet above. Weird.
197 return ZOOM::Record->_new($this, $which, $_rec);
203 Net::Z3950::ZOOM::resultset_destroy($this->_rs());
204 $this->{_rs} = undef;
208 # ----------------------------------------------------------------------------
210 package ZOOM::Record;
214 die "You can't create $class objects directly";
217 # PRIVATE to ZOOM::ResultSet::record()
220 my($rs, $which, $_rec) = @_;
229 # PRIVATE within this class
233 return $this->{_rec};
240 my $string = Net::Z3950::ZOOM::record_get($this->_rec(), "render", $len);
241 # I don't think we need '$len' at all. ### Probably the Perl-to-C
242 # glue code should use the value of `len' as well as the opaque
243 # data-pointer returned, to ensure that the SV contains all of the
244 # returned data and does not stop at the first NUL character in
245 # binary data. Carefully check the ZOOM_record_get() documentation.
253 my $string = Net::Z3950::ZOOM::record_get($this->_rec(), "raw", $len);
254 # See comment about $len in render()