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