1 # $Id: ZOOM.pm,v 1.12 2005-11-04 17:08:00 mike Exp $
11 # Member naming convention: hash-element names which begin with an
12 # underscore represent underlying ZOOM-C object descriptors; those
13 # which lack them represent Perl's ZOOM objects. (The same convention
14 # is used in naming local variables where appropriate.)
16 # So, for example, the ZOOM::Connection class has an {_conn} element,
17 # which is a pointer to the ZOOM-C Connection object; but the
18 # ZOOM::ResultSet class has a {conn} element, which is a reference to
19 # the Perl-level Connection object by which it was created. (It may
20 # be that we find we have no need for these references, but for now
23 # To get at the underlying ZOOM-C connection object of a result-set
24 # (if you ever needed to do such a thing, which you probably don't)
25 # you'd use $rs->{conn}->_conn().
27 # ----------------------------------------------------------------------------
29 # The "Error" package contains constants returned as error-codes.
31 sub NONE { Net::Z3950::ZOOM::ERROR_NONE }
32 sub CONNECT { Net::Z3950::ZOOM::ERROR_CONNECT }
33 sub MEMORY { Net::Z3950::ZOOM::ERROR_MEMORY }
34 sub ENCODE { Net::Z3950::ZOOM::ERROR_ENCODE }
35 sub DECODE { Net::Z3950::ZOOM::ERROR_DECODE }
36 sub CONNECTION_LOST { Net::Z3950::ZOOM::ERROR_CONNECTION_LOST }
37 sub INIT { Net::Z3950::ZOOM::ERROR_INIT }
38 sub INTERNAL { Net::Z3950::ZOOM::ERROR_INTERNAL }
39 sub TIMEOUT { Net::Z3950::ZOOM::ERROR_TIMEOUT }
40 sub UNSUPPORTED_PROTOCOL { Net::Z3950::ZOOM::ERROR_UNSUPPORTED_PROTOCOL }
41 sub UNSUPPORTED_QUERY { Net::Z3950::ZOOM::ERROR_UNSUPPORTED_QUERY }
42 sub INVALID_QUERY { Net::Z3950::ZOOM::ERROR_INVALID_QUERY }
43 # The following are added specifically for this OO interface
44 sub CREATE_QUERY { 20001 }
45 sub QUERY_CQL { 20002 }
46 sub QUERY_PQF { 20003 }
49 # The "Event" package contains constants returned by last_event()
51 sub NONE { Net::Z3950::ZOOM::EVENT_NONE }
52 sub CONNECT { Net::Z3950::ZOOM::EVENT_CONNECT }
53 sub SEND_DATA { Net::Z3950::ZOOM::EVENT_SEND_DATA }
54 sub RECV_DATA { Net::Z3950::ZOOM::EVENT_RECV_DATA }
55 sub TIMEOUT { Net::Z3950::ZOOM::EVENT_TIMEOUT }
56 sub UNKNOWN { Net::Z3950::ZOOM::EVENT_UNKNOWN }
57 sub SEND_APDU { Net::Z3950::ZOOM::EVENT_SEND_APDU }
58 sub RECV_APDU { Net::Z3950::ZOOM::EVENT_RECV_APDU }
59 sub RECV_RECORD { Net::Z3950::ZOOM::EVENT_RECV_RECORD }
60 sub RECV_SEARCH { Net::Z3950::ZOOM::EVENT_RECV_SEARCH }
62 # ----------------------------------------------------------------------------
69 # Special cases for error specific to the OO layer
70 if ($code == ZOOM::Error::CREATE_QUERY) {
71 return "can't create query object";
72 } elsif ($code == ZOOM::Error::QUERY_CQL) {
73 return "can't set CQL query";
74 } elsif ($code == ZOOM::Error::QUERY_PQF) {
75 return "can't set prefix query";
76 } elsif ($code == ZOOM::Error::SORTBY) {
77 return "can't set sort-specification";
80 return Net::Z3950::ZOOM::diag_str($code);
83 ### More of the ZOOM::Exception instantiations should use this
85 my($code, $addinfo) = @_;
87 die new ZOOM::Exception($code, diag_str($code), $addinfo);
90 # ----------------------------------------------------------------------------
92 package ZOOM::Exception;
96 my($code, $message, $addinfo) = @_;
97 ### support diag-set, too
108 return $this->{code};
113 return $this->{message};
118 return $this->{addinfo};
122 # ----------------------------------------------------------------------------
124 package ZOOM::Options;
132 $opts = Net::Z3950::ZOOM::options_create();
134 $opts = Net::Z3950::ZOOM::options_create_with_parent($p1->_opts());
136 $opts = Net::Z3950::ZOOM::options_create_with_parent2($p1->_opts(),
139 die "can't make $class object with more than 2 parents";
150 my $_opts = $this->{_opts};
151 die "{_opts} undefined: has this Options block been destroy()ed?"
159 my($key, $value) = @_;
161 my $oldval = Net::Z3950::ZOOM::options_get($this->_opts(), $key);
162 Net::Z3950::ZOOM::options_set($this->_opts(), $key, $value)
170 my($key, $value) = @_;
173 my $oldval = Net::Z3950::ZOOM::options_getl($this->_opts(),
175 Net::Z3950::ZOOM::options_setl($this->_opts(), $key,
176 $value, length($value))
182 # This is a bit stupid, since the scalar values that Perl returns from
183 # option() can be used as a boolean; but it's just possible that some
184 # applications will rely on ZOOM_options_get_bool()'s idiosyncratic
185 # interpretation of what constitutes truth.
189 my($key, $default) = @_;
191 return Net::Z3950::ZOOM::options_get_bool($this->_opts(), $key, $default);
194 # .. and the next two are even more stupid
197 my($key, $default) = @_;
199 return Net::Z3950::ZOOM::options_get_int($this->_opts(), $key, $default);
204 my($key, $value) = @_;
206 Net::Z3950::ZOOM::options_set_int($this->_opts(), $key, $value);
209 # ### Feel guilty. Feel very, very guilty. I've not been able to
210 # get the callback memory-management right in "ZOOM.xs", with
211 # the result that the values of $function and $udata passed into
212 # this function, which are on the stack, have sometimes been
213 # freed by the time they're used by __ZOOM_option_callback(),
214 # with hilarious results. To avoid this, I copy the values into
215 # module-scoped globals, and pass _those_ into the extension
216 # function. To avoid overwriting those globals by subsequent
217 # calls, I keep all the old ones, pushed onto the @_function and
218 # @_udata arrays, which means that THIS FUNCTION LEAKS MEMORY
219 # LIKE IT'S GOING OUT OF FASHION. Not nice. One day, I should
220 # fix this, but for now there's more important fish to fry.
222 my(@_function, @_udata);
225 my($function, $udata) = @_;
227 push @_function, $function;
228 push @_udata, $udata;
229 Net::Z3950::ZOOM::options_set_callback($o1->_opts(),
230 $_function[-1], $_udata[-1]);
236 Net::Z3950::ZOOM::options_destroy($this->_opts());
237 $this->{_opts} = undef;
241 # ----------------------------------------------------------------------------
243 package ZOOM::Connection;
247 my($host, $port) = @_;
249 my $_conn = Net::Z3950::ZOOM::connection_new($host, $port);
250 my($errcode, $errmsg, $addinfo) = (undef, "dummy", "dummy");
251 $errcode = Net::Z3950::ZOOM::connection_error($_conn, $errmsg, $addinfo);
252 die new ZOOM::Exception($errcode, $errmsg, $addinfo) if $errcode;
265 my $_conn = Net::Z3950::ZOOM::connection_create($options->_opts());
273 # PRIVATE within this class
277 my $_conn = $this->{_conn};
278 die "{_conn} undefined: has this Connection been destroy()ed?"
287 my($errcode, $errmsg, $addinfo, $diagset) = (undef, "dummy", "dummy", "d");
288 $errcode = Net::Z3950::ZOOM::connection_error_x($this->_conn(), $errmsg,
290 return ($errcode, $errmsg, $addinfo, $diagset);
295 return Net::Z3950::ZOOM::connection_errcode($this->_conn());
300 return Net::Z3950::ZOOM::connection_errmsg($this->_conn());
305 return Net::Z3950::ZOOM::connection_addinfo($this->_conn());
310 my($host, $port) = @_;
312 Net::Z3950::ZOOM::connection_connect($this->_conn(), $host, $port);
313 my($errcode, $errmsg, $addinfo) = (undef, "dummy", "dummy");
314 $errcode = Net::Z3950::ZOOM::connection_error($this->_conn(),
316 die new ZOOM::Exception($errcode, $errmsg, $addinfo) if $errcode;
322 my($key, $value) = @_;
324 my $oldval = Net::Z3950::ZOOM::connection_option_get($this->_conn(), $key);
325 Net::Z3950::ZOOM::connection_option_set($this->_conn(), $key, $value)
333 my($key, $value) = @_;
336 my $oldval = Net::Z3950::ZOOM::connection_option_getl($this->_conn(),
338 Net::Z3950::ZOOM::connection_option_setl($this->_conn(), $key,
339 $value, length($value))
349 my $_rs = Net::Z3950::ZOOM::connection_search($this->_conn(),
351 my($errcode, $errmsg, $addinfo) = (undef, "dummy", "dummy");
352 $errcode = Net::Z3950::ZOOM::connection_error($this->_conn(),
354 die new ZOOM::Exception($errcode, $errmsg, $addinfo) if $errcode;
356 return _new ZOOM::ResultSet($this, $query, $_rs);
363 my $_rs = Net::Z3950::ZOOM::connection_search_pqf($this->_conn(), $pqf);
364 my($errcode, $errmsg, $addinfo) = (undef, "dummy", "dummy");
365 $errcode = Net::Z3950::ZOOM::connection_error($this->_conn(),
367 die new ZOOM::Exception($errcode, $errmsg, $addinfo) if $errcode;
369 return _new ZOOM::ResultSet($this, $pqf, $_rs);
375 Net::Z3950::ZOOM::connection_destroy($this->_conn());
376 $this->{_conn} = undef;
380 # ----------------------------------------------------------------------------
386 die "You can't create $class objects: it's a virtual base class";
392 my $_query = $this->{_query};
393 die "{_query} undefined: has this Query been destroy()ed?"
403 Net::Z3950::ZOOM::query_sortby($this->_query(), $sortby) == 0
404 or ZOOM::_oops(ZOOM::Error::SORTBY, $sortby);
410 Net::Z3950::ZOOM::query_destroy($this->_query());
411 $this->{_query} = undef;
415 package ZOOM::Query::CQL;
416 our @ISA = qw(ZOOM::Query);
422 my $q = Net::Z3950::ZOOM::query_create()
423 or ZOOM::_oops(ZOOM::Error::CREATE_QUERY);
424 Net::Z3950::ZOOM::query_cql($q, $string) == 0
425 or ZOOM::_oops(ZOOM::Error::QUERY_CQL, $string);
433 package ZOOM::Query::PQF;
434 our @ISA = qw(ZOOM::Query);
440 my $q = Net::Z3950::ZOOM::query_create()
441 or ZOOM::_oops(ZOOM::Error::CREATE_QUERY);
442 Net::Z3950::ZOOM::query_prefix($q, $string) == 0
443 or ZOOM::_oops(ZOOM::Error::QUERY_PQF, $string);
451 # ----------------------------------------------------------------------------
453 package ZOOM::ResultSet;
457 die "You can't create $class objects directly";
460 # PRIVATE to ZOOM::Connection::search()
463 my($conn, $query, $_rs) = @_;
467 query => $query, # This is not currently used, which is
468 # just as well since it could be
469 # either a string (when the RS is
470 # created with search_pqf()) or a
471 # ZOOM::Query object (when it's
472 # created with search())
477 # PRIVATE within this class
481 my $_rs = $this->{_rs};
482 die "{_rs} undefined: has this ResultSet been destroy()ed?"
490 my($key, $value) = @_;
492 my $oldval = Net::Z3950::ZOOM::resultset_option_get($this->_rs(), $key);
493 Net::Z3950::ZOOM::resultset_option_set($this->_rs(), $key, $value)
502 return Net::Z3950::ZOOM::resultset_size($this->_rs());
509 my $_rec = Net::Z3950::ZOOM::resultset_record($this->_rs(), $which);
510 ### Check for error -- but how?
511 return undef if !defined $_rec;
513 # For some reason, I have to use the explicit "->" syntax in order
514 # to invoke the ZOOM::Record constructor here, even though I don't
515 # have to do the same for _new ZOOM::ResultSet above. Weird.
516 return ZOOM::Record->_new($this, $which, $_rec);
519 sub record_immediate {
523 my $_rec = Net::Z3950::ZOOM::resultset_record_immediate($this->_rs(),
525 ### Check for error -- but how?
526 return undef if !defined $_rec;
528 return ZOOM::Record->_new($this, $which, $_rec);
534 Net::Z3950::ZOOM::resultset_cache_reset($this->_rs());
539 my($start, $count, $return_records) = @_;
541 my $raw = Net::Z3950::ZOOM::resultset_records($this->_rs(), $start, $count,
543 return undef if !defined $raw;
545 # We need to package up the returned records in ZOOM::Record objects
547 for my $i (0 .. @$raw-1) {
548 my $_rec = $raw->[$i];
549 if (!defined $_rec) {
552 push @res, ZOOM::Record->_new($this, $start+$i, $_rec);
561 my($sort_type, $sort_spec) = @_;
563 Net::Z3950::ZOOM::resultset_sort($this->_rs(), $sort_type, $sort_spec);
564 ### There's no way to check for success, as this is a void function
570 Net::Z3950::ZOOM::resultset_destroy($this->_rs());
571 $this->{_rs} = undef;
575 # ----------------------------------------------------------------------------
577 package ZOOM::Record;
581 die "You can't create $class objects directly";
584 # PRIVATE to ZOOM::ResultSet::record()
587 my($rs, $which, $_rec) = @_;
596 # PRIVATE within this class
600 return $this->{_rec};
607 my $string = Net::Z3950::ZOOM::record_get($this->_rec(), "render", $len);
608 # I don't think we need '$len' at all. ### Probably the Perl-to-C
609 # glue code should use the value of `len' as well as the opaque
610 # data-pointer returned, to ensure that the SV contains all of the
611 # returned data and does not stop at the first NUL character in
612 # binary data. Carefully check the ZOOM_record_get() documentation.
620 my $string = Net::Z3950::ZOOM::record_get($this->_rec(), "raw", $len);
621 # See comment about $len in render()