Release 1.03 (connection constructor allows additional option arguments)
[ZOOM-Perl-moved-to-github.git] / lib / ZOOM.pm
1 # $Id: ZOOM.pm,v 1.27 2006-03-09 12:57:19 mike Exp $
2
3 use strict;
4 use warnings;
5 use Net::Z3950::ZOOM;
6
7
8 package ZOOM;
9
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.)
14 #
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
20 # they are retained.)
21 #
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().
25
26 # ----------------------------------------------------------------------------
27
28 # The "Error" package contains constants returned as error-codes.
29 package ZOOM::Error;
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 }
48 sub SORTBY { 20004 }
49 sub CLONE { 20005 }
50 sub PACKAGE { 20006 }
51 sub SCANTERM { 20007 }
52 sub LOGLEVEL { 20008 }
53
54 # The "Event" package contains constants returned by last_event()
55 package ZOOM::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
67 # ----------------------------------------------------------------------------
68
69 package ZOOM;
70
71 sub diag_str {
72     my($code) = @_;
73
74     # Special cases for error specific to the OO layer
75     if ($code == ZOOM::Error::CREATE_QUERY) {
76         return "can't create query object";
77     } elsif ($code == ZOOM::Error::QUERY_CQL) {
78         return "can't set CQL query";
79     } elsif ($code == ZOOM::Error::QUERY_PQF) {
80         return "can't set prefix query";
81     } elsif ($code == ZOOM::Error::SORTBY) {
82         return "can't set sort-specification";
83     } elsif ($code == ZOOM::Error::CLONE) {
84         return "can't clone record";
85     } elsif ($code == ZOOM::Error::PACKAGE) {
86         return "can't create package";
87     } elsif ($code == ZOOM::Error::SCANTERM) {
88         return "can't retrieve term from scan-set";
89     } elsif ($code == ZOOM::Error::LOGLEVEL) {
90         return "unregistered log-level";
91     }
92
93     return Net::Z3950::ZOOM::diag_str($code);
94 }
95
96 sub _oops {
97     my($code, $addinfo, $diagset) = @_;
98
99     die new ZOOM::Exception($code, diag_str($code), $addinfo, $diagset);
100 }
101
102 # ----------------------------------------------------------------------------
103
104 package ZOOM::Exception;
105
106 sub new {
107     my $class = shift();
108     my($code, $message, $addinfo, $diagset) = @_;
109
110     return bless {
111         code => $code,
112         message => $message,
113         addinfo => $addinfo,
114         diagset => $diagset || "ZOOM",
115     }, $class;
116 }
117
118 sub code {
119     my $this = shift();
120     return $this->{code};
121 }
122
123 sub message {
124     my $this = shift();
125     return $this->{message};
126 }
127
128 sub addinfo {
129     my $this = shift();
130     return $this->{addinfo};
131 }
132
133 sub diagset {
134     my $this = shift();
135     return $this->{diagset};
136 }
137
138 sub render {
139     my $this = shift();
140     my $res = "ZOOM error " . $this->code() . ' "' . $this->message() . '"';
141     $res .= ' (addinfo: "' . $this->addinfo() . '")' if $this->addinfo();
142     $res .= " from diag-set '" . $this->diagset() . "'" if $this->diagset();
143     return $res;
144 }
145
146 # This means that untrapped exceptions render nicely.
147 use overload '""' => \&render;
148
149 # ----------------------------------------------------------------------------
150
151 package ZOOM::Options;
152
153 sub new {
154     my $class = shift();
155     my($p1, $p2) = @_;
156
157     my $opts;
158     if (@_ == 0) {
159         $opts = Net::Z3950::ZOOM::options_create();
160     } elsif (@_ == 1) {
161         $opts = Net::Z3950::ZOOM::options_create_with_parent($p1->_opts());
162     } elsif (@_ == 2) {
163         $opts = Net::Z3950::ZOOM::options_create_with_parent2($p1->_opts(),
164                                                               $p2->_opts());
165     } else {
166         die "can't make $class object with more than 2 parents";
167     }
168
169     return bless {
170         _opts => $opts,
171     }, $class;
172 }
173
174 # PRIVATE to this class and ZOOM::Connection::create() and
175 # ZOOM::Connection::package()
176 #
177 sub _opts {
178     my $this = shift();
179
180     my $_opts = $this->{_opts};
181     die "{_opts} undefined: has this Options block been destroy()ed?"
182         if !defined $_opts;
183
184     return $_opts;
185 }
186
187 sub option {
188     my $this = shift();
189     my($key, $value) = @_;
190
191     my $oldval = Net::Z3950::ZOOM::options_get($this->_opts(), $key);
192     Net::Z3950::ZOOM::options_set($this->_opts(), $key, $value)
193         if defined $value;
194
195     return $oldval;
196 }
197
198 sub option_binary {
199     my $this = shift();
200     my($key, $value) = @_;
201
202     my $dummylen = 0;
203     my $oldval = Net::Z3950::ZOOM::options_getl($this->_opts(),
204                                                 $key, $dummylen);
205     Net::Z3950::ZOOM::options_setl($this->_opts(), $key,
206                                    $value, length($value))
207         if defined $value;
208
209     return $oldval;
210 }
211
212 # This is a bit stupid, since the scalar values that Perl returns from
213 # option() can be used as a boolean; but it's just possible that some
214 # applications will rely on ZOOM_options_get_bool()'s idiosyncratic
215 # interpretation of what constitutes truth.
216 #
217 sub bool {
218     my $this = shift();
219     my($key, $default) = @_;
220
221     return Net::Z3950::ZOOM::options_get_bool($this->_opts(), $key, $default);
222 }
223
224 # .. and the next two are even more stupid
225 sub int {
226     my $this = shift();
227     my($key, $default) = @_;
228
229     return Net::Z3950::ZOOM::options_get_int($this->_opts(), $key, $default);
230 }
231
232 sub set_int {
233     my $this = shift();
234     my($key, $value) = @_;
235
236     Net::Z3950::ZOOM::options_set_int($this->_opts(), $key, $value);
237 }
238
239 #   ### Feel guilty.  Feel very, very guilty.  I've not been able to
240 #       get the callback memory-management right in "ZOOM.xs", with
241 #       the result that the values of $function and $udata passed into
242 #       this function, which are on the stack, have sometimes been
243 #       freed by the time they're used by __ZOOM_option_callback(),
244 #       with hilarious results.  To avoid this, I copy the values into
245 #       module-scoped globals, and pass _those_ into the extension
246 #       function.  To avoid overwriting those globals by subsequent
247 #       calls, I keep all the old ones, pushed onto the @_function and
248 #       @_udata arrays, which means that THIS FUNCTION LEAKS MEMORY
249 #       LIKE IT'S GOING OUT OF FASHION.  Not nice.  One day, I should
250 #       fix this, but for now there's more important fish to fry.
251 #
252 my(@_function, @_udata);
253 sub set_callback {
254     my $o1 = shift();
255     my($function, $udata) = @_;
256
257     push @_function, $function;
258     push @_udata, $udata;
259     Net::Z3950::ZOOM::options_set_callback($o1->_opts(),
260                                            $_function[-1], $_udata[-1]);
261 }
262
263 sub destroy {
264     my $this = shift();
265
266     Net::Z3950::ZOOM::options_destroy($this->_opts());
267     $this->{_opts} = undef;
268 }
269
270
271 # ----------------------------------------------------------------------------
272
273 package ZOOM::Connection;
274
275 sub new {
276     my $class = shift();
277     my($host, $port, @options) = @_;
278
279     my $_conn = Net::Z3950::ZOOM::connection_new($host, $port || 0);
280     my $conn = bless {
281         host => $host,
282         port => $port,
283         _conn => $_conn,
284     };
285
286     while (@options >= 2) {
287         my $key = shift(@options);
288         my $val = shift(@options);
289         $conn->option($key, $val);
290     }
291
292     die "Odd number of options specified"
293         if @options;
294
295     $conn->_check();
296     return $conn;
297 }
298
299 # PRIVATE to this class and to ZOOM::Query::CQL2RPN::new()
300 sub _conn {
301     my $this = shift();
302
303     my $_conn = $this->{_conn};
304     die "{_conn} undefined: has this Connection been destroy()ed?"
305         if !defined $_conn;
306
307     return $_conn;
308 }
309
310 sub _check {
311     my $this = shift();
312
313     my($errcode, $errmsg, $addinfo, $diagset) = (undef, "x", "x", "x");
314     $errcode = Net::Z3950::ZOOM::connection_error_x($this->_conn(), $errmsg,
315                                                     $addinfo, $diagset);
316     die new ZOOM::Exception($errcode, $errmsg, $addinfo, $diagset)
317         if $errcode;
318 }
319
320 sub create {
321     my $class = shift();
322     my($options) = @_;
323
324     my $_conn = Net::Z3950::ZOOM::connection_create($options->_opts());
325     return bless {
326         host => undef,
327         port => undef,
328         _conn => $_conn,
329     };
330 }
331
332 sub error_x {
333     my $this = shift();
334
335     my($errcode, $errmsg, $addinfo, $diagset) = (undef, "dummy", "dummy", "d");
336     $errcode = Net::Z3950::ZOOM::connection_error_x($this->_conn(), $errmsg,
337                                                     $addinfo, $diagset);
338     return ($errcode, $errmsg, $addinfo, $diagset);
339 }
340
341 sub errcode {
342     my $this = shift();
343     return Net::Z3950::ZOOM::connection_errcode($this->_conn());
344 }
345
346 sub errmsg {
347     my $this = shift();
348     return Net::Z3950::ZOOM::connection_errmsg($this->_conn());
349 }
350
351 sub addinfo {
352     my $this = shift();
353     return Net::Z3950::ZOOM::connection_addinfo($this->_conn());
354 }
355
356 sub diagset {
357     my $this = shift();
358     return Net::Z3950::ZOOM::connection_diagset($this->_conn());
359 }
360
361 sub connect {
362     my $this = shift();
363     my($host, $port) = @_;
364
365     $port = 0 if !defined $port;
366     Net::Z3950::ZOOM::connection_connect($this->_conn(), $host, $port);
367     $this->_check();
368     # No return value
369 }
370
371 sub option {
372     my $this = shift();
373     my($key, $value) = @_;
374
375     my $oldval = Net::Z3950::ZOOM::connection_option_get($this->_conn(), $key);
376     Net::Z3950::ZOOM::connection_option_set($this->_conn(), $key, $value)
377         if defined $value;
378
379     return $oldval;
380 }
381
382 sub option_binary {
383     my $this = shift();
384     my($key, $value) = @_;
385
386     my $dummylen = 0;
387     my $oldval = Net::Z3950::ZOOM::connection_option_getl($this->_conn(),
388                                                           $key, $dummylen);
389     Net::Z3950::ZOOM::connection_option_setl($this->_conn(), $key,
390                                              $value, length($value))
391         if defined $value;
392
393     return $oldval;
394 }
395
396 sub search {
397     my $this = shift();
398     my($query) = @_;
399
400     my $_rs = Net::Z3950::ZOOM::connection_search($this->_conn(),
401                                                   $query->_query());
402     $this->_check();
403     return _new ZOOM::ResultSet($this, $query, $_rs);
404 }
405
406 sub search_pqf {
407     my $this = shift();
408     my($pqf) = @_;
409
410     my $_rs = Net::Z3950::ZOOM::connection_search_pqf($this->_conn(), $pqf);
411     $this->_check();
412     return _new ZOOM::ResultSet($this, $pqf, $_rs);
413 }
414
415 sub scan_pqf {
416     my $this = shift();
417     my($startterm) = @_;
418
419     my $_ss = Net::Z3950::ZOOM::connection_scan($this->_conn(), $startterm);
420     $this->_check();
421     return _new ZOOM::ScanSet($this, $startterm, $_ss);
422 }
423
424 sub scan {
425     my $this = shift();
426     my($query) = @_;
427
428     my $_ss = Net::Z3950::ZOOM::connection_scan1($this->_conn(),
429                                                  $query->_query());
430     $this->_check();
431     return _new ZOOM::ScanSet($this, $query, $_ss);
432 }
433
434 sub package {
435     my $this = shift();
436     my($options) = @_;
437
438     my $_o = defined $options ? $options->_opts() :
439         Net::Z3950::ZOOM::options_create();
440     my $_p = Net::Z3950::ZOOM::connection_package($this->_conn(), $_o)
441         or ZOOM::_oops(ZOOM::Error::PACKAGE);
442
443     return _new ZOOM::Package($this, $options, $_p);
444 }
445
446 sub destroy {
447     my $this = shift();
448
449     Net::Z3950::ZOOM::connection_destroy($this->_conn());
450     $this->{_conn} = undef;
451 }
452
453
454 # ----------------------------------------------------------------------------
455
456 package ZOOM::Query;
457
458 sub new {
459     my $class = shift();
460     die "You can't create $class objects: it's a virtual base class";
461 }
462
463 # PRIVATE to this class and ZOOM::Connection::search()
464 sub _query {
465     my $this = shift();
466
467     my $_query = $this->{_query};
468     die "{_query} undefined: has this Query been destroy()ed?"
469         if !defined $_query;
470
471     return $_query;
472 }
473
474 sub sortby {
475     my $this = shift();
476     my($sortby) = @_;
477
478     Net::Z3950::ZOOM::query_sortby($this->_query(), $sortby) == 0
479         or ZOOM::_oops(ZOOM::Error::SORTBY, $sortby);
480 }
481
482 sub destroy {
483     my $this = shift();
484
485     Net::Z3950::ZOOM::query_destroy($this->_query());
486     $this->{_query} = undef;
487 }
488
489
490 package ZOOM::Query::CQL;
491 our @ISA = qw(ZOOM::Query);
492
493 sub new {
494     my $class = shift();
495     my($string) = @_;
496
497     my $q = Net::Z3950::ZOOM::query_create()
498         or ZOOM::_oops(ZOOM::Error::CREATE_QUERY);
499     Net::Z3950::ZOOM::query_cql($q, $string) == 0
500         or ZOOM::_oops(ZOOM::Error::QUERY_CQL, $string);
501
502     return bless {
503         _query => $q,
504     }, $class;
505 }
506
507
508 package ZOOM::Query::CQL2RPN;
509 our @ISA = qw(ZOOM::Query);
510
511 sub new {
512     my $class = shift();
513     my($string, $conn) = @_;
514
515     my $q = Net::Z3950::ZOOM::query_create()
516         or ZOOM::_oops(ZOOM::Error::CREATE_QUERY);
517     # check() throws the exception we want; but we only want it on failure!
518     Net::Z3950::ZOOM::query_cql2rpn($q, $string, $conn->_conn()) == 0
519         or $conn->_check();
520
521     return bless {
522         _query => $q,
523     }, $class;
524 }
525
526
527 package ZOOM::Query::PQF;
528 our @ISA = qw(ZOOM::Query);
529
530 sub new {
531     my $class = shift();
532     my($string) = @_;
533
534     my $q = Net::Z3950::ZOOM::query_create()
535         or ZOOM::_oops(ZOOM::Error::CREATE_QUERY);
536     Net::Z3950::ZOOM::query_prefix($q, $string) == 0
537         or ZOOM::_oops(ZOOM::Error::QUERY_PQF, $string);
538
539     return bless {
540         _query => $q,
541     }, $class;
542 }
543
544
545 # ----------------------------------------------------------------------------
546
547 package ZOOM::ResultSet;
548
549 sub new {
550     my $class = shift();
551     die "You can't create $class objects directly";
552 }
553
554 # PRIVATE to ZOOM::Connection::search() and ZOOM::Connection::search_pqf()
555 sub _new {
556     my $class = shift();
557     my($conn, $query, $_rs) = @_;
558
559     return bless {
560         conn => $conn,
561         query => $query,        # This is not currently used, which is
562                                 # just as well since it could be
563                                 # either a string (when the RS is
564                                 # created with search_pqf()) or a
565                                 # ZOOM::Query object (when it's
566                                 # created with search())
567         _rs => $_rs,
568     }, $class;
569 }
570
571 # PRIVATE to this class
572 sub _rs {
573     my $this = shift();
574
575     my $_rs = $this->{_rs};
576     die "{_rs} undefined: has this ResultSet been destroy()ed?"
577         if !defined $_rs;
578
579     return $_rs;
580 }
581
582 sub option {
583     my $this = shift();
584     my($key, $value) = @_;
585
586     my $oldval = Net::Z3950::ZOOM::resultset_option_get($this->_rs(), $key);
587     Net::Z3950::ZOOM::resultset_option_set($this->_rs(), $key, $value)
588         if defined $value;
589
590     return $oldval;
591 }
592
593 sub size {
594     my $this = shift();
595
596     return Net::Z3950::ZOOM::resultset_size($this->_rs());
597 }
598
599 sub record {
600     my $this = shift();
601     my($which) = @_;
602
603     my $_rec = Net::Z3950::ZOOM::resultset_record($this->_rs(), $which);
604     $this->{conn}->_check();
605
606     # Even if no error has occurred, I think record() might
607     # legitimately return undef if we're running in asynchronous mode
608     # and the record just hasn't been retrieved yet.  This goes double
609     # for record_immediate().
610     return undef if !defined $_rec;
611
612     # For some reason, I have to use the explicit "->" syntax in order
613     # to invoke the ZOOM::Record constructor here, even though I don't
614     # have to do the same for _new ZOOM::ResultSet above.  Weird.
615     return ZOOM::Record->_new($this, $which, $_rec);
616 }
617
618 sub record_immediate {
619     my $this = shift();
620     my($which) = @_;
621
622     my $_rec = Net::Z3950::ZOOM::resultset_record_immediate($this->_rs(),
623                                                             $which);
624     $this->{conn}->_check();
625     # The record might legitimately not be there yet
626     return undef if !defined $_rec;
627
628     return ZOOM::Record->_new($this, $which, $_rec);
629 }
630
631 sub cache_reset {
632     my $this = shift();
633
634     Net::Z3950::ZOOM::resultset_cache_reset($this->_rs());
635 }
636
637 sub records {
638     my $this = shift();
639     my($start, $count, $return_records) = @_;
640
641     my $raw = Net::Z3950::ZOOM::resultset_records($this->_rs(), $start, $count,
642                                                   $return_records);
643     # By design, $raw may be undefined (if $return_records is true)
644     return undef if !defined $raw;
645
646     # We need to package up the returned records in ZOOM::Record objects
647     my @res = ();
648     for my $i (0 .. @$raw-1) {
649         my $_rec = $raw->[$i];
650         if (!defined $_rec) {
651             push @res, undef;
652         } else {
653             push @res, ZOOM::Record->_new($this, $start+$i, $_rec);
654         }
655     }
656
657     return \@res;
658 }
659
660 sub sort {
661     my $this = shift();
662     my($sort_type, $sort_spec) = @_;
663
664     return Net::Z3950::ZOOM::resultset_sort1($this->_rs(),
665                                              $sort_type, $sort_spec);
666 }
667
668 sub destroy {
669     my $this = shift();
670
671     Net::Z3950::ZOOM::resultset_destroy($this->_rs());
672     $this->{_rs} = undef;
673 }
674
675
676 # ----------------------------------------------------------------------------
677
678 package ZOOM::Record;
679
680 sub new {
681     my $class = shift();
682     die "You can't create $class objects directly";
683 }
684
685 # PRIVATE to ZOOM::ResultSet::record(),
686 # ZOOM::ResultSet::record_immediate(), ZOOM::ResultSet::records() and
687 # ZOOM::Record::clone()
688 #
689 sub _new {
690     my $class = shift();
691     my($rs, $which, $_rec) = @_;
692
693     return bless {
694         rs => $rs,
695         which => $which,
696         _rec => $_rec,
697     }, $class;
698 }
699
700 # PRIVATE to this class
701 sub _rec {
702     my $this = shift();
703
704     my $_rec = $this->{_rec};
705     die "{_rec} undefined: has this Record been destroy()ed?"
706         if !defined $_rec;
707
708     return $_rec;
709 }
710
711 sub render {
712     my $this = shift();
713
714     my $len = 0;
715     my $string = Net::Z3950::ZOOM::record_get($this->_rec(), "render", $len);
716     # I don't think we need '$len' at all.  ### Probably the Perl-to-C
717     # glue code should use the value of `len' as well as the opaque
718     # data-pointer returned, to ensure that the SV contains all of the
719     # returned data and does not stop at the first NUL character in
720     # binary data.  Carefully check the ZOOM_record_get() documentation.
721     return $string;
722 }
723
724 sub raw {
725     my $this = shift();
726
727     my $len = 0;
728     my $string = Net::Z3950::ZOOM::record_get($this->_rec(), "raw", $len);
729     # See comment about $len in render()
730     return $string;
731 }
732
733 sub clone {
734     my $this = shift();
735
736     my $raw = Net::Z3950::ZOOM::record_clone($this->_rec())
737         or ZOOM::_oops(ZOOM::Error::CLONE);
738
739     # Arg 1 (rs) is undefined as the new record doesn't belong to an RS
740     return _new ZOOM::Record(undef, undef, $raw);
741 }
742
743 sub destroy {
744     my $this = shift();
745
746     Net::Z3950::ZOOM::record_destroy($this->_rec());
747     $this->{_rec} = undef;
748 }
749
750
751 # ----------------------------------------------------------------------------
752
753 package ZOOM::ScanSet;
754
755 sub new {
756     my $class = shift();
757     die "You can't create $class objects directly";
758 }
759
760 # PRIVATE to ZOOM::Connection::scan(),
761 sub _new {
762     my $class = shift();
763     my($conn, $startterm, $_ss) = @_;
764
765     return bless {
766         conn => $conn,
767         startterm => $startterm,# This is not currently used, which is
768                                 # just as well since it could be
769                                 # either a string (when the SS is
770                                 # created with scan()) or a
771                                 # ZOOM::Query object (when it's
772                                 # created with scan1())
773         _ss => $_ss,
774     }, $class;
775 }
776
777 # PRIVATE to this class
778 sub _ss {
779     my $this = shift();
780
781     my $_ss = $this->{_ss};
782     die "{_ss} undefined: has this ScanSet been destroy()ed?"
783         if !defined $_ss;
784
785     return $_ss;
786 }
787
788 sub option {
789     my $this = shift();
790     my($key, $value) = @_;
791
792     my $oldval = Net::Z3950::ZOOM::scanset_option_get($this->_ss(), $key);
793     Net::Z3950::ZOOM::scanset_option_set($this->_ss(), $key, $value)
794         if defined $value;
795
796     return $oldval;
797 }
798
799 sub size {
800     my $this = shift();
801
802     return Net::Z3950::ZOOM::scanset_size($this->_ss());
803 }
804
805 sub term {
806     my $this = shift();
807     my($which) = @_;
808
809     my($occ, $len) = (0, 0);
810     my $term = Net::Z3950::ZOOM::scanset_term($this->_ss(), $which,
811                                               $occ, $len)
812         or ZOOM::_oops(ZOOM::Error::SCANTERM);
813
814     die "length of term '$term' differs from returned len=$len"
815         if length($term) != $len;
816
817     return ($term, $occ);
818 }
819
820 sub display_term {
821     my $this = shift();
822     my($which) = @_;
823
824     my($occ, $len) = (0, 0);
825     my $term = Net::Z3950::ZOOM::scanset_display_term($this->_ss(), $which,
826                                                       $occ, $len)
827         or ZOOM::_oops(ZOOM::Error::SCANTERM);
828
829     die "length of display term '$term' differs from returned len=$len"
830         if length($term) != $len;
831
832     return ($term, $occ);
833 }
834
835 sub destroy {
836     my $this = shift();
837
838     Net::Z3950::ZOOM::scanset_destroy($this->_ss());
839     $this->{_ss} = undef;
840 }
841
842
843 # ----------------------------------------------------------------------------
844
845 package ZOOM::Package;
846
847 sub new {
848     my $class = shift();
849     die "You can't create $class objects directly";
850 }
851
852 # PRIVATE to ZOOM::Connection::package(),
853 sub _new {
854     my $class = shift();
855     my($conn, $options, $_p) = @_;
856
857     return bless {
858         conn => $conn,
859         options => $options,
860         _p => $_p,
861     }, $class;
862 }
863
864 # PRIVATE to this class
865 sub _p {
866     my $this = shift();
867
868     my $_p = $this->{_p};
869     die "{_p} undefined: has this Package been destroy()ed?"
870         if !defined $_p;
871
872     return $_p;
873 }
874
875 sub option {
876     my $this = shift();
877     my($key, $value) = @_;
878
879     my $oldval = Net::Z3950::ZOOM::package_option_get($this->_p(), $key);
880     Net::Z3950::ZOOM::package_option_set($this->_p(), $key, $value)
881         if defined $value;
882
883     return $oldval;
884 }
885
886 sub send {
887     my $this = shift();
888     my($type) = @_;
889
890     Net::Z3950::ZOOM::package_send($this->_p(), $type);
891     $this->{conn}->_check();
892 }
893
894 sub destroy {
895     my $this = shift();
896
897     Net::Z3950::ZOOM::package_destroy($this->_p());
898     $this->{_p} = undef;
899 }
900
901
902 # There follows trivial support for YAZ logging.  This is wired out
903 # into the Net::Z3950::ZOOM package, and we here provide wrapper
904 # functions -- nothing more than aliases, really -- in the ZOOM::Log
905 # package.  There really is no point in inventing an OO interface.
906 #
907 # Passing @_ directly to the underlying Net::Z3950::ZOOM::* functions
908 # doesn't work, for reasons that I can't begin to fathom, and that
909 # don't particularly interest me.  Unpacking into scalars and passing
910 # those _does_ work, so that's what we do.
911
912 package ZOOM::Log;
913
914 sub mask_str      { my($a) = @_; Net::Z3950::ZOOM::yaz_log_mask_str($a); }
915 sub module_level  { my($a) = @_; Net::Z3950::ZOOM::yaz_log_module_level($a); }
916 sub init          { my($a, $b, $c) = @_;
917                     Net::Z3950::ZOOM::yaz_log_init($a, $b, $c) }
918 sub init_file     { my($a) = @_; Net::Z3950::ZOOM::yaz_log_init_file($a) }
919 sub init_level    { my($a) = @_; Net::Z3950::ZOOM::yaz_log_init_level($a) }
920 sub init_prefix   { my($a) = @_; Net::Z3950::ZOOM::yaz_log_init_prefix($a) }
921 sub time_format   { my($a) = @_; Net::Z3950::ZOOM::yaz_log_time_format($a) }
922 sub init_max_size { my($a) = @_; Net::Z3950::ZOOM::yaz_log_init_max_size($a) }
923
924 sub log {
925     my($level, @message) = @_;
926
927     if ($level !~ /^(0x)?\d+$/) {
928         # Assuming its log-level name, we look it up.
929         my $num = module_level($level);
930         ZOOM::_oops(ZOOM::Error::LOGLEVEL, $level)
931             if $num == 0;
932         $level = $num;
933     }
934
935     Net::Z3950::ZOOM::yaz_log($level, join("", @message));
936 }
937
938
939 1;