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