1 # $Id: ZOOM.pm,v 1.33 2006-04-12 12:00:48 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);
98 return Net::Z3950::ZOOM::event_str(@_);
104 my @_connsref = map { $_->_conn() } @$connsref;
105 return Net::Z3950::ZOOM::event(\@_connsref);
109 my($code, $addinfo, $diagset) = @_;
111 die new ZOOM::Exception($code, diag_str($code), $addinfo, $diagset);
114 # ----------------------------------------------------------------------------
116 package ZOOM::Exception;
120 my($code, $message, $addinfo, $diagset) = @_;
126 diagset => $diagset || "ZOOM",
132 return $this->{code};
137 return $this->{message};
142 return $this->{addinfo};
147 return $this->{diagset};
152 my $res = "ZOOM error " . $this->code() . ' "' . $this->message() . '"';
153 $res .= ' (addinfo: "' . $this->addinfo() . '")' if $this->addinfo();
154 $res .= " from diag-set '" . $this->diagset() . "'" if $this->diagset();
158 # This means that untrapped exceptions render nicely.
159 use overload '""' => \&render;
161 # ----------------------------------------------------------------------------
163 package ZOOM::Options;
171 $opts = Net::Z3950::ZOOM::options_create();
173 $opts = Net::Z3950::ZOOM::options_create_with_parent($p1->_opts());
175 $opts = Net::Z3950::ZOOM::options_create_with_parent2($p1->_opts(),
178 die "can't make $class object with more than 2 parents";
186 # PRIVATE to this class and ZOOM::Connection::create() and
187 # ZOOM::Connection::package()
192 my $_opts = $this->{_opts};
193 die "{_opts} undefined: has this Options block been destroy()ed?"
201 my($key, $value) = @_;
203 my $oldval = Net::Z3950::ZOOM::options_get($this->_opts(), $key);
204 Net::Z3950::ZOOM::options_set($this->_opts(), $key, $value)
212 my($key, $value) = @_;
215 my $oldval = Net::Z3950::ZOOM::options_getl($this->_opts(),
217 Net::Z3950::ZOOM::options_setl($this->_opts(), $key,
218 $value, length($value))
224 # This is a bit stupid, since the scalar values that Perl returns from
225 # option() can be used as a boolean; but it's just possible that some
226 # applications will rely on ZOOM_options_get_bool()'s idiosyncratic
227 # interpretation of what constitutes truth.
231 my($key, $default) = @_;
233 return Net::Z3950::ZOOM::options_get_bool($this->_opts(), $key, $default);
236 # .. and the next two are even more stupid
239 my($key, $default) = @_;
241 return Net::Z3950::ZOOM::options_get_int($this->_opts(), $key, $default);
246 my($key, $value) = @_;
248 Net::Z3950::ZOOM::options_set_int($this->_opts(), $key, $value);
251 # ### Feel guilty. Feel very, very guilty. I've not been able to
252 # get the callback memory-management right in "ZOOM.xs", with
253 # the result that the values of $function and $udata passed into
254 # this function, which are on the stack, have sometimes been
255 # freed by the time they're used by __ZOOM_option_callback(),
256 # with hilarious results. To avoid this, I copy the values into
257 # module-scoped globals, and pass _those_ into the extension
258 # function. To avoid overwriting those globals by subsequent
259 # calls, I keep all the old ones, pushed onto the @_function and
260 # @_udata arrays, which means that THIS FUNCTION LEAKS MEMORY
261 # LIKE IT'S GOING OUT OF FASHION. Not nice. One day, I should
262 # fix this, but for now there's more important fish to fry.
264 my(@_function, @_udata);
267 my($function, $udata) = @_;
269 push @_function, $function;
270 push @_udata, $udata;
271 Net::Z3950::ZOOM::options_set_callback($o1->_opts(),
272 $_function[-1], $_udata[-1]);
278 Net::Z3950::ZOOM::options_destroy($this->_opts());
279 $this->{_opts} = undef;
283 # ----------------------------------------------------------------------------
285 package ZOOM::Connection;
289 my($host, $port, @options) = @_;
291 my $_opts = Net::Z3950::ZOOM::options_create();
292 while (@options >= 2) {
293 my $key = shift(@options);
294 my $val = shift(@options);
295 Net::Z3950::ZOOM::options_set($_opts, $key, $val);
298 die "Odd number of options specified"
301 my $_conn = Net::Z3950::ZOOM::connection_create($_opts);
302 Net::Z3950::ZOOM::connection_connect($_conn, $host, $port || 0);
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);
463 return Net::Z3950::ZOOM::connection_last_event($this->_conn());
469 Net::Z3950::ZOOM::connection_destroy($this->_conn());
470 $this->{_conn} = undef;
474 # ----------------------------------------------------------------------------
480 die "You can't create $class objects: it's a virtual base class";
483 # PRIVATE to this class and ZOOM::Connection::search()
487 my $_query = $this->{_query};
488 die "{_query} undefined: has this Query been destroy()ed?"
498 Net::Z3950::ZOOM::query_sortby($this->_query(), $sortby) == 0
499 or ZOOM::_oops(ZOOM::Error::SORTBY, $sortby);
505 Net::Z3950::ZOOM::query_destroy($this->_query());
506 $this->{_query} = undef;
510 package ZOOM::Query::CQL;
511 our @ISA = qw(ZOOM::Query);
517 my $q = Net::Z3950::ZOOM::query_create()
518 or ZOOM::_oops(ZOOM::Error::CREATE_QUERY);
519 Net::Z3950::ZOOM::query_cql($q, $string) == 0
520 or ZOOM::_oops(ZOOM::Error::QUERY_CQL, $string);
528 package ZOOM::Query::CQL2RPN;
529 our @ISA = qw(ZOOM::Query);
533 my($string, $conn) = @_;
535 my $q = Net::Z3950::ZOOM::query_create()
536 or ZOOM::_oops(ZOOM::Error::CREATE_QUERY);
537 # check() throws the exception we want; but we only want it on failure!
538 Net::Z3950::ZOOM::query_cql2rpn($q, $string, $conn->_conn()) == 0
547 package ZOOM::Query::PQF;
548 our @ISA = qw(ZOOM::Query);
554 my $q = Net::Z3950::ZOOM::query_create()
555 or ZOOM::_oops(ZOOM::Error::CREATE_QUERY);
556 Net::Z3950::ZOOM::query_prefix($q, $string) == 0
557 or ZOOM::_oops(ZOOM::Error::QUERY_PQF, $string);
565 # ----------------------------------------------------------------------------
567 package ZOOM::ResultSet;
571 die "You can't create $class objects directly";
574 # PRIVATE to ZOOM::Connection::search() and ZOOM::Connection::search_pqf()
577 my($conn, $query, $_rs) = @_;
581 query => $query, # This is not currently used, which is
582 # just as well since it could be
583 # either a string (when the RS is
584 # created with search_pqf()) or a
585 # ZOOM::Query object (when it's
586 # created with search())
591 # PRIVATE to this class
595 my $_rs = $this->{_rs};
596 die "{_rs} undefined: has this ResultSet been destroy()ed?"
604 my($key, $value) = @_;
606 my $oldval = Net::Z3950::ZOOM::resultset_option_get($this->_rs(), $key);
607 Net::Z3950::ZOOM::resultset_option_set($this->_rs(), $key, $value)
616 return Net::Z3950::ZOOM::resultset_size($this->_rs());
623 my $_rec = Net::Z3950::ZOOM::resultset_record($this->_rs(), $which);
624 $this->{conn}->_check();
626 # Even if no error has occurred, I think record() might
627 # legitimately return undef if we're running in asynchronous mode
628 # and the record just hasn't been retrieved yet. This goes double
629 # for record_immediate().
630 return undef if !defined $_rec;
632 # For some reason, I have to use the explicit "->" syntax in order
633 # to invoke the ZOOM::Record constructor here, even though I don't
634 # have to do the same for _new ZOOM::ResultSet above. Weird.
635 return ZOOM::Record->_new($this, $which, $_rec);
638 sub record_immediate {
642 my $_rec = Net::Z3950::ZOOM::resultset_record_immediate($this->_rs(),
644 $this->{conn}->_check();
645 # The record might legitimately not be there yet
646 return undef if !defined $_rec;
648 return ZOOM::Record->_new($this, $which, $_rec);
654 Net::Z3950::ZOOM::resultset_cache_reset($this->_rs());
659 my($start, $count, $return_records) = @_;
661 my $raw = Net::Z3950::ZOOM::resultset_records($this->_rs(), $start, $count,
663 # By design, $raw may be undefined (if $return_records is true)
664 return undef if !defined $raw;
666 # We need to package up the returned records in ZOOM::Record objects
668 for my $i (0 .. @$raw-1) {
669 my $_rec = $raw->[$i];
670 if (!defined $_rec) {
673 push @res, ZOOM::Record->_new($this, $start+$i, $_rec);
682 my($sort_type, $sort_spec) = @_;
684 return Net::Z3950::ZOOM::resultset_sort1($this->_rs(),
685 $sort_type, $sort_spec);
691 Net::Z3950::ZOOM::resultset_destroy($this->_rs());
692 $this->{_rs} = undef;
696 # ----------------------------------------------------------------------------
698 package ZOOM::Record;
702 die "You can't create $class objects directly";
705 # PRIVATE to ZOOM::ResultSet::record(),
706 # ZOOM::ResultSet::record_immediate(), ZOOM::ResultSet::records() and
707 # ZOOM::Record::clone()
711 my($rs, $which, $_rec) = @_;
720 # PRIVATE to this class
724 my $_rec = $this->{_rec};
725 die "{_rec} undefined: has this Record been destroy()ed?"
734 return $this->get("render", @_);
740 return $this->get("raw", @_);
745 my($type, $args) = @_;
747 $type = "$type;$args" if defined $args;
749 my $string = Net::Z3950::ZOOM::record_get($this->_rec(), $type, $len);
750 # I don't think we need '$len' at all. ### Probably the Perl-to-C
751 # glue code should use the value of `len' as well as the opaque
752 # data-pointer returned, to ensure that the SV contains all of the
753 # returned data and does not stop at the first NUL character in
754 # binary data. Carefully check the ZOOM_record_get() documentation.
761 my $raw = Net::Z3950::ZOOM::record_clone($this->_rec())
762 or ZOOM::_oops(ZOOM::Error::CLONE);
764 # Arg 1 (rs) is undefined as the new record doesn't belong to an RS
765 return _new ZOOM::Record(undef, undef, $raw);
771 Net::Z3950::ZOOM::record_destroy($this->_rec());
772 $this->{_rec} = undef;
776 # ----------------------------------------------------------------------------
778 package ZOOM::ScanSet;
782 die "You can't create $class objects directly";
785 # PRIVATE to ZOOM::Connection::scan(),
788 my($conn, $startterm, $_ss) = @_;
792 startterm => $startterm,# This is not currently used, which is
793 # just as well since it could be
794 # either a string (when the SS is
795 # created with scan()) or a
796 # ZOOM::Query object (when it's
797 # created with scan1())
802 # PRIVATE to this class
806 my $_ss = $this->{_ss};
807 die "{_ss} undefined: has this ScanSet been destroy()ed?"
815 my($key, $value) = @_;
817 my $oldval = Net::Z3950::ZOOM::scanset_option_get($this->_ss(), $key);
818 Net::Z3950::ZOOM::scanset_option_set($this->_ss(), $key, $value)
827 return Net::Z3950::ZOOM::scanset_size($this->_ss());
834 my($occ, $len) = (0, 0);
835 my $term = Net::Z3950::ZOOM::scanset_term($this->_ss(), $which,
837 or ZOOM::_oops(ZOOM::Error::SCANTERM);
839 die "length of term '$term' differs from returned len=$len"
840 if length($term) != $len;
842 return ($term, $occ);
849 my($occ, $len) = (0, 0);
850 my $term = Net::Z3950::ZOOM::scanset_display_term($this->_ss(), $which,
852 or ZOOM::_oops(ZOOM::Error::SCANTERM);
854 die "length of display term '$term' differs from returned len=$len"
855 if length($term) != $len;
857 return ($term, $occ);
863 Net::Z3950::ZOOM::scanset_destroy($this->_ss());
864 $this->{_ss} = undef;
868 # ----------------------------------------------------------------------------
870 package ZOOM::Package;
874 die "You can't create $class objects directly";
877 # PRIVATE to ZOOM::Connection::package(),
880 my($conn, $options, $_p) = @_;
889 # PRIVATE to this class
893 my $_p = $this->{_p};
894 die "{_p} undefined: has this Package been destroy()ed?"
902 my($key, $value) = @_;
904 my $oldval = Net::Z3950::ZOOM::package_option_get($this->_p(), $key);
905 Net::Z3950::ZOOM::package_option_set($this->_p(), $key, $value)
915 Net::Z3950::ZOOM::package_send($this->_p(), $type);
916 $this->{conn}->_check();
922 Net::Z3950::ZOOM::package_destroy($this->_p());
927 # There follows trivial support for YAZ logging. This is wired out
928 # into the Net::Z3950::ZOOM package, and we here provide wrapper
929 # functions -- nothing more than aliases, really -- in the ZOOM::Log
930 # package. There really is no point in inventing an OO interface.
932 # Passing @_ directly to the underlying Net::Z3950::ZOOM::* functions
933 # doesn't work, for reasons that I can't begin to fathom, and that
934 # don't particularly interest me. Unpacking into scalars and passing
935 # those _does_ work, so that's what we do.
939 sub mask_str { my($a) = @_; Net::Z3950::ZOOM::yaz_log_mask_str($a); }
940 sub module_level { my($a) = @_; Net::Z3950::ZOOM::yaz_log_module_level($a); }
941 sub init { my($a, $b, $c) = @_;
942 Net::Z3950::ZOOM::yaz_log_init($a, $b, $c) }
943 sub init_file { my($a) = @_; Net::Z3950::ZOOM::yaz_log_init_file($a) }
944 sub init_level { my($a) = @_; Net::Z3950::ZOOM::yaz_log_init_level($a) }
945 sub init_prefix { my($a) = @_; Net::Z3950::ZOOM::yaz_log_init_prefix($a) }
946 sub time_format { my($a) = @_; Net::Z3950::ZOOM::yaz_log_time_format($a) }
947 sub init_max_size { my($a) = @_; Net::Z3950::ZOOM::yaz_log_init_max_size($a) }
950 my($level, @message) = @_;
952 if ($level !~ /^(0x)?\d+$/) {
953 # Assuming its log-level name, we look it up.
954 my $num = module_level($level);
955 ZOOM::_oops(ZOOM::Error::LOGLEVEL, $level)
960 Net::Z3950::ZOOM::yaz_log($level, join("", @message));