1 # $Id: ZOOM.pm,v 1.31 2006-04-07 12:58:19 mike Exp $
10 # Member naming convention: hash-element names which begin with an
11 # underscore represent underlying ZOOM-C object descriptors; those
12 # which lack them represent Perl's ZOOM objects. (The same convention
13 # is used in naming local variables where appropriate.)
15 # So, for example, the ZOOM::Connection class has an {_conn} element,
16 # which is a pointer to the ZOOM-C Connection object; but the
17 # ZOOM::ResultSet class has a {conn} element, which is a reference to
18 # the Perl-level Connection object by which it was created. (It may
19 # be that we find we have no need for these references, but for now
22 # To get at the underlying ZOOM-C connection object of a result-set
23 # (if you ever needed to do such a thing, which you probably don't)
24 # you'd use $rs->{conn}->_conn().
26 # ----------------------------------------------------------------------------
28 # The "Error" package contains constants returned as error-codes.
30 sub NONE { Net::Z3950::ZOOM::ERROR_NONE }
31 sub CONNECT { Net::Z3950::ZOOM::ERROR_CONNECT }
32 sub MEMORY { Net::Z3950::ZOOM::ERROR_MEMORY }
33 sub ENCODE { Net::Z3950::ZOOM::ERROR_ENCODE }
34 sub DECODE { Net::Z3950::ZOOM::ERROR_DECODE }
35 sub CONNECTION_LOST { Net::Z3950::ZOOM::ERROR_CONNECTION_LOST }
36 sub INIT { Net::Z3950::ZOOM::ERROR_INIT }
37 sub INTERNAL { Net::Z3950::ZOOM::ERROR_INTERNAL }
38 sub TIMEOUT { Net::Z3950::ZOOM::ERROR_TIMEOUT }
39 sub UNSUPPORTED_PROTOCOL { Net::Z3950::ZOOM::ERROR_UNSUPPORTED_PROTOCOL }
40 sub UNSUPPORTED_QUERY { Net::Z3950::ZOOM::ERROR_UNSUPPORTED_QUERY }
41 sub INVALID_QUERY { Net::Z3950::ZOOM::ERROR_INVALID_QUERY }
42 sub CQL_PARSE { Net::Z3950::ZOOM::ERROR_CQL_PARSE }
43 sub CQL_TRANSFORM { Net::Z3950::ZOOM::ERROR_CQL_TRANSFORM }
44 # The following are added specifically for this OO interface
45 sub CREATE_QUERY { 20001 }
46 sub QUERY_CQL { 20002 }
47 sub QUERY_PQF { 20003 }
51 sub SCANTERM { 20007 }
52 sub LOGLEVEL { 20008 }
54 # The "Event" package contains constants returned by last_event()
56 sub NONE { Net::Z3950::ZOOM::EVENT_NONE }
57 sub CONNECT { Net::Z3950::ZOOM::EVENT_CONNECT }
58 sub SEND_DATA { Net::Z3950::ZOOM::EVENT_SEND_DATA }
59 sub RECV_DATA { Net::Z3950::ZOOM::EVENT_RECV_DATA }
60 sub TIMEOUT { Net::Z3950::ZOOM::EVENT_TIMEOUT }
61 sub UNKNOWN { Net::Z3950::ZOOM::EVENT_UNKNOWN }
62 sub SEND_APDU { Net::Z3950::ZOOM::EVENT_SEND_APDU }
63 sub RECV_APDU { Net::Z3950::ZOOM::EVENT_RECV_APDU }
64 sub RECV_RECORD { Net::Z3950::ZOOM::EVENT_RECV_RECORD }
65 sub RECV_SEARCH { Net::Z3950::ZOOM::EVENT_RECV_SEARCH }
66 sub ZEND { Net::Z3950::ZOOM::EVENT_END }
68 # ----------------------------------------------------------------------------
75 # Special cases for error specific to the OO layer
76 if ($code == ZOOM::Error::CREATE_QUERY) {
77 return "can't create query object";
78 } elsif ($code == ZOOM::Error::QUERY_CQL) {
79 return "can't set CQL query";
80 } elsif ($code == ZOOM::Error::QUERY_PQF) {
81 return "can't set prefix query";
82 } elsif ($code == ZOOM::Error::SORTBY) {
83 return "can't set sort-specification";
84 } elsif ($code == ZOOM::Error::CLONE) {
85 return "can't clone record";
86 } elsif ($code == ZOOM::Error::PACKAGE) {
87 return "can't create package";
88 } elsif ($code == ZOOM::Error::SCANTERM) {
89 return "can't retrieve term from scan-set";
90 } elsif ($code == ZOOM::Error::LOGLEVEL) {
91 return "unregistered log-level";
94 return Net::Z3950::ZOOM::diag_str($code);
99 return Net::Z3950::ZOOM::event_str(@_);
106 my @_connsref = map { $_->_conn() } @$connsref;
107 return Net::Z3950::ZOOM::event(\@_connsref);
111 my($code, $addinfo, $diagset) = @_;
113 die new ZOOM::Exception($code, diag_str($code), $addinfo, $diagset);
116 # ----------------------------------------------------------------------------
118 package ZOOM::Exception;
122 my($code, $message, $addinfo, $diagset) = @_;
128 diagset => $diagset || "ZOOM",
134 return $this->{code};
139 return $this->{message};
144 return $this->{addinfo};
149 return $this->{diagset};
154 my $res = "ZOOM error " . $this->code() . ' "' . $this->message() . '"';
155 $res .= ' (addinfo: "' . $this->addinfo() . '")' if $this->addinfo();
156 $res .= " from diag-set '" . $this->diagset() . "'" if $this->diagset();
160 # This means that untrapped exceptions render nicely.
161 use overload '""' => \&render;
163 # ----------------------------------------------------------------------------
165 package ZOOM::Options;
173 $opts = Net::Z3950::ZOOM::options_create();
175 $opts = Net::Z3950::ZOOM::options_create_with_parent($p1->_opts());
177 $opts = Net::Z3950::ZOOM::options_create_with_parent2($p1->_opts(),
180 die "can't make $class object with more than 2 parents";
188 # PRIVATE to this class and ZOOM::Connection::create() and
189 # ZOOM::Connection::package()
194 my $_opts = $this->{_opts};
195 die "{_opts} undefined: has this Options block been destroy()ed?"
203 my($key, $value) = @_;
205 my $oldval = Net::Z3950::ZOOM::options_get($this->_opts(), $key);
206 Net::Z3950::ZOOM::options_set($this->_opts(), $key, $value)
214 my($key, $value) = @_;
217 my $oldval = Net::Z3950::ZOOM::options_getl($this->_opts(),
219 Net::Z3950::ZOOM::options_setl($this->_opts(), $key,
220 $value, length($value))
226 # This is a bit stupid, since the scalar values that Perl returns from
227 # option() can be used as a boolean; but it's just possible that some
228 # applications will rely on ZOOM_options_get_bool()'s idiosyncratic
229 # interpretation of what constitutes truth.
233 my($key, $default) = @_;
235 return Net::Z3950::ZOOM::options_get_bool($this->_opts(), $key, $default);
238 # .. and the next two are even more stupid
241 my($key, $default) = @_;
243 return Net::Z3950::ZOOM::options_get_int($this->_opts(), $key, $default);
248 my($key, $value) = @_;
250 Net::Z3950::ZOOM::options_set_int($this->_opts(), $key, $value);
253 # ### Feel guilty. Feel very, very guilty. I've not been able to
254 # get the callback memory-management right in "ZOOM.xs", with
255 # the result that the values of $function and $udata passed into
256 # this function, which are on the stack, have sometimes been
257 # freed by the time they're used by __ZOOM_option_callback(),
258 # with hilarious results. To avoid this, I copy the values into
259 # module-scoped globals, and pass _those_ into the extension
260 # function. To avoid overwriting those globals by subsequent
261 # calls, I keep all the old ones, pushed onto the @_function and
262 # @_udata arrays, which means that THIS FUNCTION LEAKS MEMORY
263 # LIKE IT'S GOING OUT OF FASHION. Not nice. One day, I should
264 # fix this, but for now there's more important fish to fry.
266 my(@_function, @_udata);
269 my($function, $udata) = @_;
271 push @_function, $function;
272 push @_udata, $udata;
273 Net::Z3950::ZOOM::options_set_callback($o1->_opts(),
274 $_function[-1], $_udata[-1]);
280 Net::Z3950::ZOOM::options_destroy($this->_opts());
281 $this->{_opts} = undef;
285 # ----------------------------------------------------------------------------
287 package ZOOM::Connection;
291 my($host, $port, @options) = @_;
293 my $_conn = Net::Z3950::ZOOM::connection_new($host, $port || 0);
300 while (@options >= 2) {
301 my $key = shift(@options);
302 my $val = shift(@options);
303 $conn->option($key, $val);
306 die "Odd number of options specified"
313 # PRIVATE to this class, to ZOOM::event() and to ZOOM::Query::CQL2RPN::new()
317 my $_conn = $this->{_conn};
318 die "{_conn} undefined: has this Connection been destroy()ed?"
327 my($errcode, $errmsg, $addinfo, $diagset) = (undef, "x", "x", "x");
328 $errcode = Net::Z3950::ZOOM::connection_error_x($this->_conn(), $errmsg,
330 die new ZOOM::Exception($errcode, $errmsg, $addinfo, $diagset)
338 my $_conn = Net::Z3950::ZOOM::connection_create($options->_opts());
349 my($errcode, $errmsg, $addinfo, $diagset) = (undef, "dummy", "dummy", "d");
350 $errcode = Net::Z3950::ZOOM::connection_error_x($this->_conn(), $errmsg,
352 return ($errcode, $errmsg, $addinfo, $diagset);
357 return Net::Z3950::ZOOM::connection_errcode($this->_conn());
362 return Net::Z3950::ZOOM::connection_errmsg($this->_conn());
367 return Net::Z3950::ZOOM::connection_addinfo($this->_conn());
372 return Net::Z3950::ZOOM::connection_diagset($this->_conn());
377 my($host, $port) = @_;
379 $port = 0 if !defined $port;
380 Net::Z3950::ZOOM::connection_connect($this->_conn(), $host, $port);
387 my($key, $value) = @_;
389 my $oldval = Net::Z3950::ZOOM::connection_option_get($this->_conn(), $key);
390 Net::Z3950::ZOOM::connection_option_set($this->_conn(), $key, $value)
398 my($key, $value) = @_;
401 my $oldval = Net::Z3950::ZOOM::connection_option_getl($this->_conn(),
403 Net::Z3950::ZOOM::connection_option_setl($this->_conn(), $key,
404 $value, length($value))
414 my $_rs = Net::Z3950::ZOOM::connection_search($this->_conn(),
417 return _new ZOOM::ResultSet($this, $query, $_rs);
424 my $_rs = Net::Z3950::ZOOM::connection_search_pqf($this->_conn(), $pqf);
426 return _new ZOOM::ResultSet($this, $pqf, $_rs);
433 my $_ss = Net::Z3950::ZOOM::connection_scan($this->_conn(), $startterm);
435 return _new ZOOM::ScanSet($this, $startterm, $_ss);
442 my $_ss = Net::Z3950::ZOOM::connection_scan1($this->_conn(),
445 return _new ZOOM::ScanSet($this, $query, $_ss);
452 my $_o = defined $options ? $options->_opts() :
453 Net::Z3950::ZOOM::options_create();
454 my $_p = Net::Z3950::ZOOM::connection_package($this->_conn(), $_o)
455 or ZOOM::_oops(ZOOM::Error::PACKAGE);
457 return _new ZOOM::Package($this, $options, $_p);
464 return Net::Z3950::ZOOM::connection_last_event($this->_conn());
470 Net::Z3950::ZOOM::connection_destroy($this->_conn());
471 $this->{_conn} = undef;
475 # ----------------------------------------------------------------------------
481 die "You can't create $class objects: it's a virtual base class";
484 # PRIVATE to this class and ZOOM::Connection::search()
488 my $_query = $this->{_query};
489 die "{_query} undefined: has this Query been destroy()ed?"
499 Net::Z3950::ZOOM::query_sortby($this->_query(), $sortby) == 0
500 or ZOOM::_oops(ZOOM::Error::SORTBY, $sortby);
506 Net::Z3950::ZOOM::query_destroy($this->_query());
507 $this->{_query} = undef;
511 package ZOOM::Query::CQL;
512 our @ISA = qw(ZOOM::Query);
518 my $q = Net::Z3950::ZOOM::query_create()
519 or ZOOM::_oops(ZOOM::Error::CREATE_QUERY);
520 Net::Z3950::ZOOM::query_cql($q, $string) == 0
521 or ZOOM::_oops(ZOOM::Error::QUERY_CQL, $string);
529 package ZOOM::Query::CQL2RPN;
530 our @ISA = qw(ZOOM::Query);
534 my($string, $conn) = @_;
536 my $q = Net::Z3950::ZOOM::query_create()
537 or ZOOM::_oops(ZOOM::Error::CREATE_QUERY);
538 # check() throws the exception we want; but we only want it on failure!
539 Net::Z3950::ZOOM::query_cql2rpn($q, $string, $conn->_conn()) == 0
548 package ZOOM::Query::PQF;
549 our @ISA = qw(ZOOM::Query);
555 my $q = Net::Z3950::ZOOM::query_create()
556 or ZOOM::_oops(ZOOM::Error::CREATE_QUERY);
557 Net::Z3950::ZOOM::query_prefix($q, $string) == 0
558 or ZOOM::_oops(ZOOM::Error::QUERY_PQF, $string);
566 # ----------------------------------------------------------------------------
568 package ZOOM::ResultSet;
572 die "You can't create $class objects directly";
575 # PRIVATE to ZOOM::Connection::search() and ZOOM::Connection::search_pqf()
578 my($conn, $query, $_rs) = @_;
582 query => $query, # This is not currently used, which is
583 # just as well since it could be
584 # either a string (when the RS is
585 # created with search_pqf()) or a
586 # ZOOM::Query object (when it's
587 # created with search())
592 # PRIVATE to this class
596 my $_rs = $this->{_rs};
597 die "{_rs} undefined: has this ResultSet been destroy()ed?"
605 my($key, $value) = @_;
607 my $oldval = Net::Z3950::ZOOM::resultset_option_get($this->_rs(), $key);
608 Net::Z3950::ZOOM::resultset_option_set($this->_rs(), $key, $value)
617 return Net::Z3950::ZOOM::resultset_size($this->_rs());
624 my $_rec = Net::Z3950::ZOOM::resultset_record($this->_rs(), $which);
625 $this->{conn}->_check();
627 # Even if no error has occurred, I think record() might
628 # legitimately return undef if we're running in asynchronous mode
629 # and the record just hasn't been retrieved yet. This goes double
630 # for record_immediate().
631 return undef if !defined $_rec;
633 # For some reason, I have to use the explicit "->" syntax in order
634 # to invoke the ZOOM::Record constructor here, even though I don't
635 # have to do the same for _new ZOOM::ResultSet above. Weird.
636 return ZOOM::Record->_new($this, $which, $_rec);
639 sub record_immediate {
643 my $_rec = Net::Z3950::ZOOM::resultset_record_immediate($this->_rs(),
645 $this->{conn}->_check();
646 # The record might legitimately not be there yet
647 return undef if !defined $_rec;
649 return ZOOM::Record->_new($this, $which, $_rec);
655 Net::Z3950::ZOOM::resultset_cache_reset($this->_rs());
660 my($start, $count, $return_records) = @_;
662 my $raw = Net::Z3950::ZOOM::resultset_records($this->_rs(), $start, $count,
664 # By design, $raw may be undefined (if $return_records is true)
665 return undef if !defined $raw;
667 # We need to package up the returned records in ZOOM::Record objects
669 for my $i (0 .. @$raw-1) {
670 my $_rec = $raw->[$i];
671 if (!defined $_rec) {
674 push @res, ZOOM::Record->_new($this, $start+$i, $_rec);
683 my($sort_type, $sort_spec) = @_;
685 return Net::Z3950::ZOOM::resultset_sort1($this->_rs(),
686 $sort_type, $sort_spec);
692 Net::Z3950::ZOOM::resultset_destroy($this->_rs());
693 $this->{_rs} = undef;
697 # ----------------------------------------------------------------------------
699 package ZOOM::Record;
703 die "You can't create $class objects directly";
706 # PRIVATE to ZOOM::ResultSet::record(),
707 # ZOOM::ResultSet::record_immediate(), ZOOM::ResultSet::records() and
708 # ZOOM::Record::clone()
712 my($rs, $which, $_rec) = @_;
721 # PRIVATE to this class
725 my $_rec = $this->{_rec};
726 die "{_rec} undefined: has this Record been destroy()ed?"
735 return $this->get("render", @_);
741 return $this->get("raw", @_);
746 my($type, $args) = @_;
748 $type = "$type;$args" if defined $args;
750 my $string = Net::Z3950::ZOOM::record_get($this->_rec(), $type, $len);
751 # I don't think we need '$len' at all. ### Probably the Perl-to-C
752 # glue code should use the value of `len' as well as the opaque
753 # data-pointer returned, to ensure that the SV contains all of the
754 # returned data and does not stop at the first NUL character in
755 # binary data. Carefully check the ZOOM_record_get() documentation.
762 my $raw = Net::Z3950::ZOOM::record_clone($this->_rec())
763 or ZOOM::_oops(ZOOM::Error::CLONE);
765 # Arg 1 (rs) is undefined as the new record doesn't belong to an RS
766 return _new ZOOM::Record(undef, undef, $raw);
772 Net::Z3950::ZOOM::record_destroy($this->_rec());
773 $this->{_rec} = undef;
777 # ----------------------------------------------------------------------------
779 package ZOOM::ScanSet;
783 die "You can't create $class objects directly";
786 # PRIVATE to ZOOM::Connection::scan(),
789 my($conn, $startterm, $_ss) = @_;
793 startterm => $startterm,# This is not currently used, which is
794 # just as well since it could be
795 # either a string (when the SS is
796 # created with scan()) or a
797 # ZOOM::Query object (when it's
798 # created with scan1())
803 # PRIVATE to this class
807 my $_ss = $this->{_ss};
808 die "{_ss} undefined: has this ScanSet been destroy()ed?"
816 my($key, $value) = @_;
818 my $oldval = Net::Z3950::ZOOM::scanset_option_get($this->_ss(), $key);
819 Net::Z3950::ZOOM::scanset_option_set($this->_ss(), $key, $value)
828 return Net::Z3950::ZOOM::scanset_size($this->_ss());
835 my($occ, $len) = (0, 0);
836 my $term = Net::Z3950::ZOOM::scanset_term($this->_ss(), $which,
838 or ZOOM::_oops(ZOOM::Error::SCANTERM);
840 die "length of term '$term' differs from returned len=$len"
841 if length($term) != $len;
843 return ($term, $occ);
850 my($occ, $len) = (0, 0);
851 my $term = Net::Z3950::ZOOM::scanset_display_term($this->_ss(), $which,
853 or ZOOM::_oops(ZOOM::Error::SCANTERM);
855 die "length of display term '$term' differs from returned len=$len"
856 if length($term) != $len;
858 return ($term, $occ);
864 Net::Z3950::ZOOM::scanset_destroy($this->_ss());
865 $this->{_ss} = undef;
869 # ----------------------------------------------------------------------------
871 package ZOOM::Package;
875 die "You can't create $class objects directly";
878 # PRIVATE to ZOOM::Connection::package(),
881 my($conn, $options, $_p) = @_;
890 # PRIVATE to this class
894 my $_p = $this->{_p};
895 die "{_p} undefined: has this Package been destroy()ed?"
903 my($key, $value) = @_;
905 my $oldval = Net::Z3950::ZOOM::package_option_get($this->_p(), $key);
906 Net::Z3950::ZOOM::package_option_set($this->_p(), $key, $value)
916 Net::Z3950::ZOOM::package_send($this->_p(), $type);
917 $this->{conn}->_check();
923 Net::Z3950::ZOOM::package_destroy($this->_p());
928 # There follows trivial support for YAZ logging. This is wired out
929 # into the Net::Z3950::ZOOM package, and we here provide wrapper
930 # functions -- nothing more than aliases, really -- in the ZOOM::Log
931 # package. There really is no point in inventing an OO interface.
933 # Passing @_ directly to the underlying Net::Z3950::ZOOM::* functions
934 # doesn't work, for reasons that I can't begin to fathom, and that
935 # don't particularly interest me. Unpacking into scalars and passing
936 # those _does_ work, so that's what we do.
940 sub mask_str { my($a) = @_; Net::Z3950::ZOOM::yaz_log_mask_str($a); }
941 sub module_level { my($a) = @_; Net::Z3950::ZOOM::yaz_log_module_level($a); }
942 sub init { my($a, $b, $c) = @_;
943 Net::Z3950::ZOOM::yaz_log_init($a, $b, $c) }
944 sub init_file { my($a) = @_; Net::Z3950::ZOOM::yaz_log_init_file($a) }
945 sub init_level { my($a) = @_; Net::Z3950::ZOOM::yaz_log_init_level($a) }
946 sub init_prefix { my($a) = @_; Net::Z3950::ZOOM::yaz_log_init_prefix($a) }
947 sub time_format { my($a) = @_; Net::Z3950::ZOOM::yaz_log_time_format($a) }
948 sub init_max_size { my($a) = @_; Net::Z3950::ZOOM::yaz_log_init_max_size($a) }
951 my($level, @message) = @_;
953 if ($level !~ /^(0x)?\d+$/) {
954 # Assuming its log-level name, we look it up.
955 my $num = module_level($level);
956 ZOOM::_oops(ZOOM::Error::LOGLEVEL, $level)
961 Net::Z3950::ZOOM::yaz_log($level, join("", @message));