1 # $Id: ZOOM.pm,v 1.15 2005-11-08 15:55:45 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 }
51 # The "Event" package contains constants returned by last_event()
53 sub NONE { Net::Z3950::ZOOM::EVENT_NONE }
54 sub CONNECT { Net::Z3950::ZOOM::EVENT_CONNECT }
55 sub SEND_DATA { Net::Z3950::ZOOM::EVENT_SEND_DATA }
56 sub RECV_DATA { Net::Z3950::ZOOM::EVENT_RECV_DATA }
57 sub TIMEOUT { Net::Z3950::ZOOM::EVENT_TIMEOUT }
58 sub UNKNOWN { Net::Z3950::ZOOM::EVENT_UNKNOWN }
59 sub SEND_APDU { Net::Z3950::ZOOM::EVENT_SEND_APDU }
60 sub RECV_APDU { Net::Z3950::ZOOM::EVENT_RECV_APDU }
61 sub RECV_RECORD { Net::Z3950::ZOOM::EVENT_RECV_RECORD }
62 sub RECV_SEARCH { Net::Z3950::ZOOM::EVENT_RECV_SEARCH }
64 # ----------------------------------------------------------------------------
71 # Special cases for error specific to the OO layer
72 if ($code == ZOOM::Error::CREATE_QUERY) {
73 return "can't create query object";
74 } elsif ($code == ZOOM::Error::QUERY_CQL) {
75 return "can't set CQL query";
76 } elsif ($code == ZOOM::Error::QUERY_PQF) {
77 return "can't set prefix query";
78 } elsif ($code == ZOOM::Error::SORTBY) {
79 return "can't set sort-specification";
80 } elsif ($code == ZOOM::Error::CLONE) {
81 return "can't clone record";
82 } elsif ($code == ZOOM::Error::PACKAGE) {
83 return "can't create package";
86 return Net::Z3950::ZOOM::diag_str($code);
89 ### More of the ZOOM::Exception instantiations should use this
91 my($code, $addinfo) = @_;
93 die new ZOOM::Exception($code, diag_str($code), $addinfo);
96 # ----------------------------------------------------------------------------
98 package ZOOM::Exception;
102 my($code, $message, $addinfo) = @_;
103 ### support diag-set, too
114 return $this->{code};
119 return $this->{message};
124 return $this->{addinfo};
128 # ----------------------------------------------------------------------------
130 package ZOOM::Options;
138 $opts = Net::Z3950::ZOOM::options_create();
140 $opts = Net::Z3950::ZOOM::options_create_with_parent($p1->_opts());
142 $opts = Net::Z3950::ZOOM::options_create_with_parent2($p1->_opts(),
145 die "can't make $class object with more than 2 parents";
153 # PRIVATE to this class and ZOOM::Connection::create() and
154 # ZOOM::Connection::package()
159 my $_opts = $this->{_opts};
160 die "{_opts} undefined: has this Options block been destroy()ed?"
168 my($key, $value) = @_;
170 my $oldval = Net::Z3950::ZOOM::options_get($this->_opts(), $key);
171 Net::Z3950::ZOOM::options_set($this->_opts(), $key, $value)
179 my($key, $value) = @_;
182 my $oldval = Net::Z3950::ZOOM::options_getl($this->_opts(),
184 Net::Z3950::ZOOM::options_setl($this->_opts(), $key,
185 $value, length($value))
191 # This is a bit stupid, since the scalar values that Perl returns from
192 # option() can be used as a boolean; but it's just possible that some
193 # applications will rely on ZOOM_options_get_bool()'s idiosyncratic
194 # interpretation of what constitutes truth.
198 my($key, $default) = @_;
200 return Net::Z3950::ZOOM::options_get_bool($this->_opts(), $key, $default);
203 # .. and the next two are even more stupid
206 my($key, $default) = @_;
208 return Net::Z3950::ZOOM::options_get_int($this->_opts(), $key, $default);
213 my($key, $value) = @_;
215 Net::Z3950::ZOOM::options_set_int($this->_opts(), $key, $value);
218 # ### Feel guilty. Feel very, very guilty. I've not been able to
219 # get the callback memory-management right in "ZOOM.xs", with
220 # the result that the values of $function and $udata passed into
221 # this function, which are on the stack, have sometimes been
222 # freed by the time they're used by __ZOOM_option_callback(),
223 # with hilarious results. To avoid this, I copy the values into
224 # module-scoped globals, and pass _those_ into the extension
225 # function. To avoid overwriting those globals by subsequent
226 # calls, I keep all the old ones, pushed onto the @_function and
227 # @_udata arrays, which means that THIS FUNCTION LEAKS MEMORY
228 # LIKE IT'S GOING OUT OF FASHION. Not nice. One day, I should
229 # fix this, but for now there's more important fish to fry.
231 my(@_function, @_udata);
234 my($function, $udata) = @_;
236 push @_function, $function;
237 push @_udata, $udata;
238 Net::Z3950::ZOOM::options_set_callback($o1->_opts(),
239 $_function[-1], $_udata[-1]);
245 Net::Z3950::ZOOM::options_destroy($this->_opts());
246 $this->{_opts} = undef;
250 # ----------------------------------------------------------------------------
252 package ZOOM::Connection;
256 my($host, $port) = @_;
258 my $_conn = Net::Z3950::ZOOM::connection_new($host, $port);
259 my($errcode, $errmsg, $addinfo) = (undef, "dummy", "dummy");
260 $errcode = Net::Z3950::ZOOM::connection_error($_conn, $errmsg, $addinfo);
261 die new ZOOM::Exception($errcode, $errmsg, $addinfo) if $errcode;
274 my $_conn = Net::Z3950::ZOOM::connection_create($options->_opts());
282 # PRIVATE to this class
286 my $_conn = $this->{_conn};
287 die "{_conn} undefined: has this Connection been destroy()ed?"
296 my($errcode, $errmsg, $addinfo, $diagset) = (undef, "dummy", "dummy", "d");
297 $errcode = Net::Z3950::ZOOM::connection_error_x($this->_conn(), $errmsg,
299 return ($errcode, $errmsg, $addinfo, $diagset);
304 return Net::Z3950::ZOOM::connection_errcode($this->_conn());
309 return Net::Z3950::ZOOM::connection_errmsg($this->_conn());
314 return Net::Z3950::ZOOM::connection_addinfo($this->_conn());
319 my($host, $port) = @_;
321 Net::Z3950::ZOOM::connection_connect($this->_conn(), $host, $port);
322 my($errcode, $errmsg, $addinfo) = (undef, "dummy", "dummy");
323 $errcode = Net::Z3950::ZOOM::connection_error($this->_conn(),
325 die new ZOOM::Exception($errcode, $errmsg, $addinfo) if $errcode;
331 my($key, $value) = @_;
333 my $oldval = Net::Z3950::ZOOM::connection_option_get($this->_conn(), $key);
334 Net::Z3950::ZOOM::connection_option_set($this->_conn(), $key, $value)
342 my($key, $value) = @_;
345 my $oldval = Net::Z3950::ZOOM::connection_option_getl($this->_conn(),
347 Net::Z3950::ZOOM::connection_option_setl($this->_conn(), $key,
348 $value, length($value))
358 my $_rs = Net::Z3950::ZOOM::connection_search($this->_conn(),
360 my($errcode, $errmsg, $addinfo) = (undef, "dummy", "dummy");
361 $errcode = Net::Z3950::ZOOM::connection_error($this->_conn(),
363 die new ZOOM::Exception($errcode, $errmsg, $addinfo) if $errcode;
365 return _new ZOOM::ResultSet($this, $query, $_rs);
372 my $_rs = Net::Z3950::ZOOM::connection_search_pqf($this->_conn(), $pqf);
373 my($errcode, $errmsg, $addinfo) = (undef, "dummy", "dummy");
374 $errcode = Net::Z3950::ZOOM::connection_error($this->_conn(),
376 die new ZOOM::Exception($errcode, $errmsg, $addinfo) if $errcode;
378 return _new ZOOM::ResultSet($this, $pqf, $_rs);
385 my $_ss = Net::Z3950::ZOOM::connection_scan($this->_conn(), $startterm);
386 my($errcode, $errmsg, $addinfo) = (undef, "dummy", "dummy");
387 $errcode = Net::Z3950::ZOOM::connection_error($this->_conn(),
389 die new ZOOM::Exception($errcode, $errmsg, $addinfo) if $errcode;
391 return _new ZOOM::ScanSet($this, $startterm, $_ss);
398 my $_o = defined $options ? $options->_opts() :
399 Net::Z3950::ZOOM::options_create();
400 my $_p = Net::Z3950::ZOOM::connection_package($this->_conn(), $_o)
401 or ZOOM::_oops(ZOOM::Error::PACKAGE);
403 return _new ZOOM::Package($this, $options, $_p);
409 Net::Z3950::ZOOM::connection_destroy($this->_conn());
410 $this->{_conn} = undef;
414 # ----------------------------------------------------------------------------
420 die "You can't create $class objects: it's a virtual base class";
423 # PRIVATE to this class and ZOOM::Connection::search()
427 my $_query = $this->{_query};
428 die "{_query} undefined: has this Query been destroy()ed?"
438 Net::Z3950::ZOOM::query_sortby($this->_query(), $sortby) == 0
439 or ZOOM::_oops(ZOOM::Error::SORTBY, $sortby);
445 Net::Z3950::ZOOM::query_destroy($this->_query());
446 $this->{_query} = undef;
450 package ZOOM::Query::CQL;
451 our @ISA = qw(ZOOM::Query);
457 my $q = Net::Z3950::ZOOM::query_create()
458 or ZOOM::_oops(ZOOM::Error::CREATE_QUERY);
459 Net::Z3950::ZOOM::query_cql($q, $string) == 0
460 or ZOOM::_oops(ZOOM::Error::QUERY_CQL, $string);
468 package ZOOM::Query::PQF;
469 our @ISA = qw(ZOOM::Query);
475 my $q = Net::Z3950::ZOOM::query_create()
476 or ZOOM::_oops(ZOOM::Error::CREATE_QUERY);
477 Net::Z3950::ZOOM::query_prefix($q, $string) == 0
478 or ZOOM::_oops(ZOOM::Error::QUERY_PQF, $string);
486 # ----------------------------------------------------------------------------
488 package ZOOM::ResultSet;
492 die "You can't create $class objects directly";
495 # PRIVATE to ZOOM::Connection::search() and ZOOM::Connection::search_pqf()
498 my($conn, $query, $_rs) = @_;
502 query => $query, # This is not currently used, which is
503 # just as well since it could be
504 # either a string (when the RS is
505 # created with search_pqf()) or a
506 # ZOOM::Query object (when it's
507 # created with search())
512 # PRIVATE to this class
516 my $_rs = $this->{_rs};
517 die "{_rs} undefined: has this ResultSet been destroy()ed?"
525 my($key, $value) = @_;
527 my $oldval = Net::Z3950::ZOOM::resultset_option_get($this->_rs(), $key);
528 Net::Z3950::ZOOM::resultset_option_set($this->_rs(), $key, $value)
537 return Net::Z3950::ZOOM::resultset_size($this->_rs());
544 my $_rec = Net::Z3950::ZOOM::resultset_record($this->_rs(), $which);
545 ### Check for error -- but how?
546 return undef if !defined $_rec;
548 # For some reason, I have to use the explicit "->" syntax in order
549 # to invoke the ZOOM::Record constructor here, even though I don't
550 # have to do the same for _new ZOOM::ResultSet above. Weird.
551 return ZOOM::Record->_new($this, $which, $_rec);
554 sub record_immediate {
558 my $_rec = Net::Z3950::ZOOM::resultset_record_immediate($this->_rs(),
560 ### Check for error -- but how?
561 return undef if !defined $_rec;
563 return ZOOM::Record->_new($this, $which, $_rec);
569 Net::Z3950::ZOOM::resultset_cache_reset($this->_rs());
574 my($start, $count, $return_records) = @_;
576 my $raw = Net::Z3950::ZOOM::resultset_records($this->_rs(), $start, $count,
578 return undef if !defined $raw;
580 # We need to package up the returned records in ZOOM::Record objects
582 for my $i (0 .. @$raw-1) {
583 my $_rec = $raw->[$i];
584 if (!defined $_rec) {
587 push @res, ZOOM::Record->_new($this, $start+$i, $_rec);
596 my($sort_type, $sort_spec) = @_;
598 Net::Z3950::ZOOM::resultset_sort($this->_rs(), $sort_type, $sort_spec);
599 ### There's no way to check for success, as this is a void function
605 Net::Z3950::ZOOM::resultset_destroy($this->_rs());
606 $this->{_rs} = undef;
610 # ----------------------------------------------------------------------------
612 package ZOOM::Record;
616 die "You can't create $class objects directly";
619 # PRIVATE to ZOOM::ResultSet::record(),
620 # ZOOM::ResultSet::record_immediate(), ZOOM::ResultSet::records() and
621 # ZOOM::Record::clone()
625 my($rs, $which, $_rec) = @_;
634 # PRIVATE to this class
638 my $_rec = $this->{_rec};
639 die "{_rec} undefined: has this Record been destroy()ed?"
649 my $string = Net::Z3950::ZOOM::record_get($this->_rec(), "render", $len);
650 # I don't think we need '$len' at all. ### Probably the Perl-to-C
651 # glue code should use the value of `len' as well as the opaque
652 # data-pointer returned, to ensure that the SV contains all of the
653 # returned data and does not stop at the first NUL character in
654 # binary data. Carefully check the ZOOM_record_get() documentation.
662 my $string = Net::Z3950::ZOOM::record_get($this->_rec(), "raw", $len);
663 # See comment about $len in render()
670 my $raw = Net::Z3950::ZOOM::record_clone($this->_rec())
671 or ZOOM::_oops(ZOOM::Error::CLONE);
673 # Arg 1 (rs) is undefined as the new record doesn't belong to an RS
674 return _new ZOOM::Record(undef, undef, $raw);
680 Net::Z3950::ZOOM::record_destroy($this->_rec());
681 $this->{_rec} = undef;
685 # ----------------------------------------------------------------------------
687 package ZOOM::ScanSet;
691 die "You can't create $class objects directly";
694 # PRIVATE to ZOOM::Connection::scan(),
697 my($conn, $startterm, $_ss) = @_;
701 startterm => $startterm,
706 # PRIVATE to this class
710 my $_ss = $this->{_ss};
711 die "{_ss} undefined: has this ScanSet been destroy()ed?"
720 return Net::Z3950::ZOOM::scanset_size($this->_ss());
727 my($occ, $len) = (0, 0);
728 my $term = Net::Z3950::ZOOM::scanset_term($this->_ss(), $which,
730 return undef if !defined $term;
731 die "length of term '$term' differs from returned len=$len"
732 if length($term) != $len;
734 return ($term, $occ);
741 my($occ, $len) = (0, 0);
742 my $term = Net::Z3950::ZOOM::scanset_display_term($this->_ss(), $which,
744 return undef if !defined $term;
745 die "length of display term '$term' differs from returned len=$len"
746 if length($term) != $len;
748 return ($term, $occ);
754 Net::Z3950::ZOOM::scanset_destroy($this->_ss());
755 $this->{_ss} = undef;
759 # ----------------------------------------------------------------------------
761 package ZOOM::Package;
765 die "You can't create $class objects directly";
768 # PRIVATE to ZOOM::Connection::package(),
771 my($conn, $options, $_p) = @_;
780 # PRIVATE to this class
784 my $_p = $this->{_p};
785 die "{_p} undefined: has this Package been destroy()ed?"
793 my($key, $value) = @_;
795 my $oldval = Net::Z3950::ZOOM::package_option_get($this->_p(), $key);
796 Net::Z3950::ZOOM::package_option_set($this->_p(), $key, $value)
806 Net::Z3950::ZOOM::package_send($this->_p(), $type);
807 my($errcode, $errmsg, $addinfo) = (undef, "dummy", "dummy");
808 $errcode = Net::Z3950::ZOOM::connection_error($this->{conn}->_conn(),
810 die new ZOOM::Exception($errcode, $errmsg, $addinfo) if $errcode;
816 Net::Z3950::ZOOM::package_destroy($this->_p());