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