1 # $Id: ZOOM.pm,v 1.20 2005-11-16 17:26:23 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 }
50 sub SCANTERM { 20007 }
52 # The "Event" package contains constants returned by last_event()
54 sub NONE { Net::Z3950::ZOOM::EVENT_NONE }
55 sub CONNECT { Net::Z3950::ZOOM::EVENT_CONNECT }
56 sub SEND_DATA { Net::Z3950::ZOOM::EVENT_SEND_DATA }
57 sub RECV_DATA { Net::Z3950::ZOOM::EVENT_RECV_DATA }
58 sub TIMEOUT { Net::Z3950::ZOOM::EVENT_TIMEOUT }
59 sub UNKNOWN { Net::Z3950::ZOOM::EVENT_UNKNOWN }
60 sub SEND_APDU { Net::Z3950::ZOOM::EVENT_SEND_APDU }
61 sub RECV_APDU { Net::Z3950::ZOOM::EVENT_RECV_APDU }
62 sub RECV_RECORD { Net::Z3950::ZOOM::EVENT_RECV_RECORD }
63 sub RECV_SEARCH { Net::Z3950::ZOOM::EVENT_RECV_SEARCH }
65 # ----------------------------------------------------------------------------
72 # Special cases for error specific to the OO layer
73 if ($code == ZOOM::Error::CREATE_QUERY) {
74 return "can't create query object";
75 } elsif ($code == ZOOM::Error::QUERY_CQL) {
76 return "can't set CQL query";
77 } elsif ($code == ZOOM::Error::QUERY_PQF) {
78 return "can't set prefix query";
79 } elsif ($code == ZOOM::Error::SORTBY) {
80 return "can't set sort-specification";
81 } elsif ($code == ZOOM::Error::CLONE) {
82 return "can't clone record";
83 } elsif ($code == ZOOM::Error::PACKAGE) {
84 return "can't create package";
85 } elsif ($code == ZOOM::Error::SCANTERM) {
86 return "can't retrieve term from scan-set";
89 return Net::Z3950::ZOOM::diag_str($code);
93 my($code, $addinfo, $diagset) = @_;
95 die new ZOOM::Exception($code, diag_str($code), $addinfo, $diagset);
98 # ----------------------------------------------------------------------------
100 package ZOOM::Exception;
104 my($code, $message, $addinfo, $diagset) = @_;
110 diagset => $diagset || "ZOOM",
116 return $this->{code};
121 return $this->{message};
126 return $this->{addinfo};
131 return $this->{diagset};
136 my $res = "ZOOM error " . $this->code() . ' "' . $this->message() . '"';
137 $res .= ' (addinfo: "' . $this->addinfo() . '")' if $this->addinfo();
141 # This means that untrapped exceptions render nicely.
142 use overload '""' => \&render;
144 # ----------------------------------------------------------------------------
146 package ZOOM::Options;
154 $opts = Net::Z3950::ZOOM::options_create();
156 $opts = Net::Z3950::ZOOM::options_create_with_parent($p1->_opts());
158 $opts = Net::Z3950::ZOOM::options_create_with_parent2($p1->_opts(),
161 die "can't make $class object with more than 2 parents";
169 # PRIVATE to this class and ZOOM::Connection::create() and
170 # ZOOM::Connection::package()
175 my $_opts = $this->{_opts};
176 die "{_opts} undefined: has this Options block been destroy()ed?"
184 my($key, $value) = @_;
186 my $oldval = Net::Z3950::ZOOM::options_get($this->_opts(), $key);
187 Net::Z3950::ZOOM::options_set($this->_opts(), $key, $value)
195 my($key, $value) = @_;
198 my $oldval = Net::Z3950::ZOOM::options_getl($this->_opts(),
200 Net::Z3950::ZOOM::options_setl($this->_opts(), $key,
201 $value, length($value))
207 # This is a bit stupid, since the scalar values that Perl returns from
208 # option() can be used as a boolean; but it's just possible that some
209 # applications will rely on ZOOM_options_get_bool()'s idiosyncratic
210 # interpretation of what constitutes truth.
214 my($key, $default) = @_;
216 return Net::Z3950::ZOOM::options_get_bool($this->_opts(), $key, $default);
219 # .. and the next two are even more stupid
222 my($key, $default) = @_;
224 return Net::Z3950::ZOOM::options_get_int($this->_opts(), $key, $default);
229 my($key, $value) = @_;
231 Net::Z3950::ZOOM::options_set_int($this->_opts(), $key, $value);
234 # ### Feel guilty. Feel very, very guilty. I've not been able to
235 # get the callback memory-management right in "ZOOM.xs", with
236 # the result that the values of $function and $udata passed into
237 # this function, which are on the stack, have sometimes been
238 # freed by the time they're used by __ZOOM_option_callback(),
239 # with hilarious results. To avoid this, I copy the values into
240 # module-scoped globals, and pass _those_ into the extension
241 # function. To avoid overwriting those globals by subsequent
242 # calls, I keep all the old ones, pushed onto the @_function and
243 # @_udata arrays, which means that THIS FUNCTION LEAKS MEMORY
244 # LIKE IT'S GOING OUT OF FASHION. Not nice. One day, I should
245 # fix this, but for now there's more important fish to fry.
247 my(@_function, @_udata);
250 my($function, $udata) = @_;
252 push @_function, $function;
253 push @_udata, $udata;
254 Net::Z3950::ZOOM::options_set_callback($o1->_opts(),
255 $_function[-1], $_udata[-1]);
261 Net::Z3950::ZOOM::options_destroy($this->_opts());
262 $this->{_opts} = undef;
266 # ----------------------------------------------------------------------------
268 package ZOOM::Connection;
272 my($host, $port) = @_;
274 my $_conn = Net::Z3950::ZOOM::connection_new($host, $port || 0);
284 # PRIVATE to this class
288 my $_conn = $this->{_conn};
289 die "{_conn} undefined: has this Connection been destroy()ed?"
298 my($errcode, $errmsg, $addinfo, $diagset) = (undef, "x", "x", "x");
299 $errcode = Net::Z3950::ZOOM::connection_error_x($this->_conn(), $errmsg,
301 die new ZOOM::Exception($errcode, $errmsg, $addinfo, $diagset)
309 my $_conn = Net::Z3950::ZOOM::connection_create($options->_opts());
320 my($errcode, $errmsg, $addinfo, $diagset) = (undef, "dummy", "dummy", "d");
321 $errcode = Net::Z3950::ZOOM::connection_error_x($this->_conn(), $errmsg,
323 return ($errcode, $errmsg, $addinfo, $diagset);
328 return Net::Z3950::ZOOM::connection_errcode($this->_conn());
333 return Net::Z3950::ZOOM::connection_errmsg($this->_conn());
338 return Net::Z3950::ZOOM::connection_addinfo($this->_conn());
343 return Net::Z3950::ZOOM::connection_diagset($this->_conn());
348 my($host, $port) = @_;
350 Net::Z3950::ZOOM::connection_connect($this->_conn(), $host, $port);
357 my($key, $value) = @_;
359 my $oldval = Net::Z3950::ZOOM::connection_option_get($this->_conn(), $key);
360 Net::Z3950::ZOOM::connection_option_set($this->_conn(), $key, $value)
368 my($key, $value) = @_;
371 my $oldval = Net::Z3950::ZOOM::connection_option_getl($this->_conn(),
373 Net::Z3950::ZOOM::connection_option_setl($this->_conn(), $key,
374 $value, length($value))
384 my $_rs = Net::Z3950::ZOOM::connection_search($this->_conn(),
387 return _new ZOOM::ResultSet($this, $query, $_rs);
394 my $_rs = Net::Z3950::ZOOM::connection_search_pqf($this->_conn(), $pqf);
396 return _new ZOOM::ResultSet($this, $pqf, $_rs);
403 my $_ss = Net::Z3950::ZOOM::connection_scan($this->_conn(), $startterm);
405 return _new ZOOM::ScanSet($this, $startterm, $_ss);
412 my $_o = defined $options ? $options->_opts() :
413 Net::Z3950::ZOOM::options_create();
414 my $_p = Net::Z3950::ZOOM::connection_package($this->_conn(), $_o)
415 or ZOOM::_oops(ZOOM::Error::PACKAGE);
417 return _new ZOOM::Package($this, $options, $_p);
423 Net::Z3950::ZOOM::connection_destroy($this->_conn());
424 $this->{_conn} = undef;
428 # ----------------------------------------------------------------------------
434 die "You can't create $class objects: it's a virtual base class";
437 # PRIVATE to this class and ZOOM::Connection::search()
441 my $_query = $this->{_query};
442 die "{_query} undefined: has this Query been destroy()ed?"
452 Net::Z3950::ZOOM::query_sortby($this->_query(), $sortby) == 0
453 or ZOOM::_oops(ZOOM::Error::SORTBY, $sortby);
459 Net::Z3950::ZOOM::query_destroy($this->_query());
460 $this->{_query} = undef;
464 package ZOOM::Query::CQL;
465 our @ISA = qw(ZOOM::Query);
471 my $q = Net::Z3950::ZOOM::query_create()
472 or ZOOM::_oops(ZOOM::Error::CREATE_QUERY);
473 Net::Z3950::ZOOM::query_cql($q, $string) == 0
474 or ZOOM::_oops(ZOOM::Error::QUERY_CQL, $string);
482 package ZOOM::Query::PQF;
483 our @ISA = qw(ZOOM::Query);
489 my $q = Net::Z3950::ZOOM::query_create()
490 or ZOOM::_oops(ZOOM::Error::CREATE_QUERY);
491 Net::Z3950::ZOOM::query_prefix($q, $string) == 0
492 or ZOOM::_oops(ZOOM::Error::QUERY_PQF, $string);
500 # ----------------------------------------------------------------------------
502 package ZOOM::ResultSet;
506 die "You can't create $class objects directly";
509 # PRIVATE to ZOOM::Connection::search() and ZOOM::Connection::search_pqf()
512 my($conn, $query, $_rs) = @_;
516 query => $query, # This is not currently used, which is
517 # just as well since it could be
518 # either a string (when the RS is
519 # created with search_pqf()) or a
520 # ZOOM::Query object (when it's
521 # created with search())
526 # PRIVATE to this class
530 my $_rs = $this->{_rs};
531 die "{_rs} undefined: has this ResultSet been destroy()ed?"
539 my($key, $value) = @_;
541 my $oldval = Net::Z3950::ZOOM::resultset_option_get($this->_rs(), $key);
542 Net::Z3950::ZOOM::resultset_option_set($this->_rs(), $key, $value)
551 return Net::Z3950::ZOOM::resultset_size($this->_rs());
558 my $_rec = Net::Z3950::ZOOM::resultset_record($this->_rs(), $which);
559 $this->{conn}->_check();
561 # Even if no error has occurred, I think record() might
562 # legitimately return undef if we're running in asynchronous mode
563 # and the record just hasn't been retrieved yet. This goes double
564 # for record_immediate().
565 return undef if !defined $_rec;
567 # For some reason, I have to use the explicit "->" syntax in order
568 # to invoke the ZOOM::Record constructor here, even though I don't
569 # have to do the same for _new ZOOM::ResultSet above. Weird.
570 return ZOOM::Record->_new($this, $which, $_rec);
573 sub record_immediate {
577 my $_rec = Net::Z3950::ZOOM::resultset_record_immediate($this->_rs(),
579 $this->{conn}->_check();
580 return undef if !defined $_rec;
582 return ZOOM::Record->_new($this, $which, $_rec);
588 Net::Z3950::ZOOM::resultset_cache_reset($this->_rs());
593 my($start, $count, $return_records) = @_;
595 my $raw = Net::Z3950::ZOOM::resultset_records($this->_rs(), $start, $count,
597 # By design, $raw may be undefined (if $return_records is true)
598 return undef if !defined $raw;
600 # We need to package up the returned records in ZOOM::Record objects
602 for my $i (0 .. @$raw-1) {
603 my $_rec = $raw->[$i];
604 if (!defined $_rec) {
607 push @res, ZOOM::Record->_new($this, $start+$i, $_rec);
616 my($sort_type, $sort_spec) = @_;
618 return Net::Z3950::ZOOM::resultset_sort1($this->_rs(),
619 $sort_type, $sort_spec);
625 Net::Z3950::ZOOM::resultset_destroy($this->_rs());
626 $this->{_rs} = undef;
630 # ----------------------------------------------------------------------------
632 package ZOOM::Record;
636 die "You can't create $class objects directly";
639 # PRIVATE to ZOOM::ResultSet::record(),
640 # ZOOM::ResultSet::record_immediate(), ZOOM::ResultSet::records() and
641 # ZOOM::Record::clone()
645 my($rs, $which, $_rec) = @_;
654 # PRIVATE to this class
658 my $_rec = $this->{_rec};
659 die "{_rec} undefined: has this Record been destroy()ed?"
669 my $string = Net::Z3950::ZOOM::record_get($this->_rec(), "render", $len);
670 # I don't think we need '$len' at all. ### Probably the Perl-to-C
671 # glue code should use the value of `len' as well as the opaque
672 # data-pointer returned, to ensure that the SV contains all of the
673 # returned data and does not stop at the first NUL character in
674 # binary data. Carefully check the ZOOM_record_get() documentation.
682 my $string = Net::Z3950::ZOOM::record_get($this->_rec(), "raw", $len);
683 # See comment about $len in render()
690 my $raw = Net::Z3950::ZOOM::record_clone($this->_rec())
691 or ZOOM::_oops(ZOOM::Error::CLONE);
693 # Arg 1 (rs) is undefined as the new record doesn't belong to an RS
694 return _new ZOOM::Record(undef, undef, $raw);
700 Net::Z3950::ZOOM::record_destroy($this->_rec());
701 $this->{_rec} = undef;
705 # ----------------------------------------------------------------------------
707 package ZOOM::ScanSet;
711 die "You can't create $class objects directly";
714 # PRIVATE to ZOOM::Connection::scan(),
717 my($conn, $startterm, $_ss) = @_;
721 startterm => $startterm,
726 # PRIVATE to this class
730 my $_ss = $this->{_ss};
731 die "{_ss} undefined: has this ScanSet been destroy()ed?"
739 my($key, $value) = @_;
741 my $oldval = Net::Z3950::ZOOM::scanset_option_get($this->_ss(), $key);
742 Net::Z3950::ZOOM::scanset_option_set($this->_ss(), $key, $value)
751 return Net::Z3950::ZOOM::scanset_size($this->_ss());
758 my($occ, $len) = (0, 0);
759 my $term = Net::Z3950::ZOOM::scanset_term($this->_ss(), $which,
761 or ZOOM::_oops(ZOOM::Error::SCANTERM);
763 die "length of term '$term' differs from returned len=$len"
764 if length($term) != $len;
766 return ($term, $occ);
773 my($occ, $len) = (0, 0);
774 my $term = Net::Z3950::ZOOM::scanset_display_term($this->_ss(), $which,
776 or ZOOM::_oops(ZOOM::Error::SCANTERM);
778 die "length of display term '$term' differs from returned len=$len"
779 if length($term) != $len;
781 return ($term, $occ);
787 Net::Z3950::ZOOM::scanset_destroy($this->_ss());
788 $this->{_ss} = undef;
792 # ----------------------------------------------------------------------------
794 package ZOOM::Package;
798 die "You can't create $class objects directly";
801 # PRIVATE to ZOOM::Connection::package(),
804 my($conn, $options, $_p) = @_;
813 # PRIVATE to this class
817 my $_p = $this->{_p};
818 die "{_p} undefined: has this Package been destroy()ed?"
826 my($key, $value) = @_;
828 my $oldval = Net::Z3950::ZOOM::package_option_get($this->_p(), $key);
829 Net::Z3950::ZOOM::package_option_set($this->_p(), $key, $value)
839 Net::Z3950::ZOOM::package_send($this->_p(), $type);
840 $this->{conn}->_check();
846 Net::Z3950::ZOOM::package_destroy($this->_p());