- Add new function Net::Z3950::ZOOM::query_ccl2rpn(), for
[ZOOM-Perl-moved-to-github.git] / lib / ZOOM.pm
1 # $Id: ZOOM.pm,v 1.34 2006-06-13 16:44:21 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 sub CCL_CONFIG { Net::Z3950::ZOOM::ERROR_CCL_CONFIG }
45 sub CCL_PARSE { Net::Z3950::ZOOM::ERROR_CCL_PARSE }
46 # The following are added specifically for this OO interface
47 sub CREATE_QUERY { 20001 }
48 sub QUERY_CQL { 20002 }
49 sub QUERY_PQF { 20003 }
50 sub SORTBY { 20004 }
51 sub CLONE { 20005 }
52 sub PACKAGE { 20006 }
53 sub SCANTERM { 20007 }
54 sub LOGLEVEL { 20008 }
55
56 # The "Event" package contains constants returned by last_event()
57 package ZOOM::Event;
58 sub NONE { Net::Z3950::ZOOM::EVENT_NONE }
59 sub CONNECT { Net::Z3950::ZOOM::EVENT_CONNECT }
60 sub SEND_DATA { Net::Z3950::ZOOM::EVENT_SEND_DATA }
61 sub RECV_DATA { Net::Z3950::ZOOM::EVENT_RECV_DATA }
62 sub TIMEOUT { Net::Z3950::ZOOM::EVENT_TIMEOUT }
63 sub UNKNOWN { Net::Z3950::ZOOM::EVENT_UNKNOWN }
64 sub SEND_APDU { Net::Z3950::ZOOM::EVENT_SEND_APDU }
65 sub RECV_APDU { Net::Z3950::ZOOM::EVENT_RECV_APDU }
66 sub RECV_RECORD { Net::Z3950::ZOOM::EVENT_RECV_RECORD }
67 sub RECV_SEARCH { Net::Z3950::ZOOM::EVENT_RECV_SEARCH }
68 sub ZEND { Net::Z3950::ZOOM::EVENT_END }
69
70 # ----------------------------------------------------------------------------
71
72 package ZOOM;
73
74 sub diag_str {
75     my($code) = @_;
76
77     # Special cases for error specific to the OO layer
78     if ($code == ZOOM::Error::CREATE_QUERY) {
79         return "can't create query object";
80     } elsif ($code == ZOOM::Error::QUERY_CQL) {
81         return "can't set CQL query";
82     } elsif ($code == ZOOM::Error::QUERY_PQF) {
83         return "can't set prefix query";
84     } elsif ($code == ZOOM::Error::SORTBY) {
85         return "can't set sort-specification";
86     } elsif ($code == ZOOM::Error::CLONE) {
87         return "can't clone record";
88     } elsif ($code == ZOOM::Error::PACKAGE) {
89         return "can't create package";
90     } elsif ($code == ZOOM::Error::SCANTERM) {
91         return "can't retrieve term from scan-set";
92     } elsif ($code == ZOOM::Error::LOGLEVEL) {
93         return "unregistered log-level";
94     }
95
96     return Net::Z3950::ZOOM::diag_str($code);
97 }
98
99 sub event_str {
100     return Net::Z3950::ZOOM::event_str(@_);
101 }
102
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 $_opts = Net::Z3950::ZOOM::options_create();
294     while (@options >= 2) {
295         my $key = shift(@options);
296         my $val = shift(@options);
297         Net::Z3950::ZOOM::options_set($_opts, $key, $val);
298     }
299
300     die "Odd number of options specified"
301         if @options;
302
303     my $_conn = Net::Z3950::ZOOM::connection_create($_opts);
304     Net::Z3950::ZOOM::connection_connect($_conn, $host, $port || 0);
305     my $conn = bless {
306         host => $host,
307         port => $port,
308         _conn => $_conn,
309     };
310
311     $conn->_check();
312     return $conn;
313 }
314
315 # PRIVATE to this class, to ZOOM::event() and to ZOOM::Query::CQL2RPN::new()
316 sub _conn {
317     my $this = shift();
318
319     my $_conn = $this->{_conn};
320     die "{_conn} undefined: has this Connection been destroy()ed?"
321         if !defined $_conn;
322
323     return $_conn;
324 }
325
326 sub _check {
327     my $this = shift();
328
329     my($errcode, $errmsg, $addinfo, $diagset) = (undef, "x", "x", "x");
330     $errcode = Net::Z3950::ZOOM::connection_error_x($this->_conn(), $errmsg,
331                                                     $addinfo, $diagset);
332     die new ZOOM::Exception($errcode, $errmsg, $addinfo, $diagset)
333         if $errcode;
334 }
335
336 sub create {
337     my $class = shift();
338     my($options) = @_;
339
340     my $_conn = Net::Z3950::ZOOM::connection_create($options->_opts());
341     return bless {
342         host => undef,
343         port => undef,
344         _conn => $_conn,
345     };
346 }
347
348 sub error_x {
349     my $this = shift();
350
351     my($errcode, $errmsg, $addinfo, $diagset) = (undef, "dummy", "dummy", "d");
352     $errcode = Net::Z3950::ZOOM::connection_error_x($this->_conn(), $errmsg,
353                                                     $addinfo, $diagset);
354     return ($errcode, $errmsg, $addinfo, $diagset);
355 }
356
357 sub errcode {
358     my $this = shift();
359     return Net::Z3950::ZOOM::connection_errcode($this->_conn());
360 }
361
362 sub errmsg {
363     my $this = shift();
364     return Net::Z3950::ZOOM::connection_errmsg($this->_conn());
365 }
366
367 sub addinfo {
368     my $this = shift();
369     return Net::Z3950::ZOOM::connection_addinfo($this->_conn());
370 }
371
372 sub diagset {
373     my $this = shift();
374     return Net::Z3950::ZOOM::connection_diagset($this->_conn());
375 }
376
377 sub connect {
378     my $this = shift();
379     my($host, $port) = @_;
380
381     $port = 0 if !defined $port;
382     Net::Z3950::ZOOM::connection_connect($this->_conn(), $host, $port);
383     $this->_check();
384     # No return value
385 }
386
387 sub option {
388     my $this = shift();
389     my($key, $value) = @_;
390
391     my $oldval = Net::Z3950::ZOOM::connection_option_get($this->_conn(), $key);
392     Net::Z3950::ZOOM::connection_option_set($this->_conn(), $key, $value)
393         if defined $value;
394
395     return $oldval;
396 }
397
398 sub option_binary {
399     my $this = shift();
400     my($key, $value) = @_;
401
402     my $dummylen = 0;
403     my $oldval = Net::Z3950::ZOOM::connection_option_getl($this->_conn(),
404                                                           $key, $dummylen);
405     Net::Z3950::ZOOM::connection_option_setl($this->_conn(), $key,
406                                              $value, length($value))
407         if defined $value;
408
409     return $oldval;
410 }
411
412 sub search {
413     my $this = shift();
414     my($query) = @_;
415
416     my $_rs = Net::Z3950::ZOOM::connection_search($this->_conn(),
417                                                   $query->_query());
418     $this->_check();
419     return _new ZOOM::ResultSet($this, $query, $_rs);
420 }
421
422 sub search_pqf {
423     my $this = shift();
424     my($pqf) = @_;
425
426     my $_rs = Net::Z3950::ZOOM::connection_search_pqf($this->_conn(), $pqf);
427     $this->_check();
428     return _new ZOOM::ResultSet($this, $pqf, $_rs);
429 }
430
431 sub scan_pqf {
432     my $this = shift();
433     my($startterm) = @_;
434
435     my $_ss = Net::Z3950::ZOOM::connection_scan($this->_conn(), $startterm);
436     $this->_check();
437     return _new ZOOM::ScanSet($this, $startterm, $_ss);
438 }
439
440 sub scan {
441     my $this = shift();
442     my($query) = @_;
443
444     my $_ss = Net::Z3950::ZOOM::connection_scan1($this->_conn(),
445                                                  $query->_query());
446     $this->_check();
447     return _new ZOOM::ScanSet($this, $query, $_ss);
448 }
449
450 sub package {
451     my $this = shift();
452     my($options) = @_;
453
454     my $_o = defined $options ? $options->_opts() :
455         Net::Z3950::ZOOM::options_create();
456     my $_p = Net::Z3950::ZOOM::connection_package($this->_conn(), $_o)
457         or ZOOM::_oops(ZOOM::Error::PACKAGE);
458
459     return _new ZOOM::Package($this, $options, $_p);
460 }
461
462 sub last_event {
463     my $this = shift();
464
465     return Net::Z3950::ZOOM::connection_last_event($this->_conn());
466 }
467
468 sub destroy {
469     my $this = shift();
470
471     Net::Z3950::ZOOM::connection_destroy($this->_conn());
472     $this->{_conn} = undef;
473 }
474
475
476 # ----------------------------------------------------------------------------
477
478 package ZOOM::Query;
479
480 sub new {
481     my $class = shift();
482     die "You can't create $class objects: it's a virtual base class";
483 }
484
485 # PRIVATE to this class and ZOOM::Connection::search()
486 sub _query {
487     my $this = shift();
488
489     my $_query = $this->{_query};
490     die "{_query} undefined: has this Query been destroy()ed?"
491         if !defined $_query;
492
493     return $_query;
494 }
495
496 sub sortby {
497     my $this = shift();
498     my($sortby) = @_;
499
500     Net::Z3950::ZOOM::query_sortby($this->_query(), $sortby) == 0
501         or ZOOM::_oops(ZOOM::Error::SORTBY, $sortby);
502 }
503
504 sub destroy {
505     my $this = shift();
506
507     Net::Z3950::ZOOM::query_destroy($this->_query());
508     $this->{_query} = undef;
509 }
510
511
512 package ZOOM::Query::CQL;
513 our @ISA = qw(ZOOM::Query);
514
515 sub new {
516     my $class = shift();
517     my($string) = @_;
518
519     my $q = Net::Z3950::ZOOM::query_create()
520         or ZOOM::_oops(ZOOM::Error::CREATE_QUERY);
521     Net::Z3950::ZOOM::query_cql($q, $string) == 0
522         or ZOOM::_oops(ZOOM::Error::QUERY_CQL, $string);
523
524     return bless {
525         _query => $q,
526     }, $class;
527 }
528
529
530 package ZOOM::Query::CQL2RPN;
531 our @ISA = qw(ZOOM::Query);
532
533 sub new {
534     my $class = shift();
535     my($string, $conn) = @_;
536
537     my $q = Net::Z3950::ZOOM::query_create()
538         or ZOOM::_oops(ZOOM::Error::CREATE_QUERY);
539     # check() throws the exception we want; but we only want it on failure!
540     Net::Z3950::ZOOM::query_cql2rpn($q, $string, $conn->_conn()) == 0
541         or $conn->_check();
542
543     return bless {
544         _query => $q,
545     }, $class;
546 }
547
548
549 # It's distressing how very similar this is to CQL2RPN
550 package ZOOM::Query::CCL2RPN;
551 our @ISA = qw(ZOOM::Query);
552
553 sub new {
554     my $class = shift();
555     my($string, $conn) = @_;
556
557     my $q = Net::Z3950::ZOOM::query_create()
558         or ZOOM::_oops(ZOOM::Error::CREATE_QUERY);
559     # check() throws the exception we want; but we only want it on failure!
560     Net::Z3950::ZOOM::query_ccl2rpn($q, $string, $conn->_conn()) == 0
561         or $conn->_check();
562
563     return bless {
564         _query => $q,
565     }, $class;
566 }
567
568
569 package ZOOM::Query::PQF;
570 our @ISA = qw(ZOOM::Query);
571
572 sub new {
573     my $class = shift();
574     my($string) = @_;
575
576     my $q = Net::Z3950::ZOOM::query_create()
577         or ZOOM::_oops(ZOOM::Error::CREATE_QUERY);
578     Net::Z3950::ZOOM::query_prefix($q, $string) == 0
579         or ZOOM::_oops(ZOOM::Error::QUERY_PQF, $string);
580
581     return bless {
582         _query => $q,
583     }, $class;
584 }
585
586
587 # ----------------------------------------------------------------------------
588
589 package ZOOM::ResultSet;
590
591 sub new {
592     my $class = shift();
593     die "You can't create $class objects directly";
594 }
595
596 # PRIVATE to ZOOM::Connection::search() and ZOOM::Connection::search_pqf()
597 sub _new {
598     my $class = shift();
599     my($conn, $query, $_rs) = @_;
600
601     return bless {
602         conn => $conn,
603         query => $query,        # This is not currently used, which is
604                                 # just as well since it could be
605                                 # either a string (when the RS is
606                                 # created with search_pqf()) or a
607                                 # ZOOM::Query object (when it's
608                                 # created with search())
609         _rs => $_rs,
610     }, $class;
611 }
612
613 # PRIVATE to this class
614 sub _rs {
615     my $this = shift();
616
617     my $_rs = $this->{_rs};
618     die "{_rs} undefined: has this ResultSet been destroy()ed?"
619         if !defined $_rs;
620
621     return $_rs;
622 }
623
624 sub option {
625     my $this = shift();
626     my($key, $value) = @_;
627
628     my $oldval = Net::Z3950::ZOOM::resultset_option_get($this->_rs(), $key);
629     Net::Z3950::ZOOM::resultset_option_set($this->_rs(), $key, $value)
630         if defined $value;
631
632     return $oldval;
633 }
634
635 sub size {
636     my $this = shift();
637
638     return Net::Z3950::ZOOM::resultset_size($this->_rs());
639 }
640
641 sub record {
642     my $this = shift();
643     my($which) = @_;
644
645     my $_rec = Net::Z3950::ZOOM::resultset_record($this->_rs(), $which);
646     $this->{conn}->_check();
647
648     # Even if no error has occurred, I think record() might
649     # legitimately return undef if we're running in asynchronous mode
650     # and the record just hasn't been retrieved yet.  This goes double
651     # for record_immediate().
652     return undef if !defined $_rec;
653
654     # For some reason, I have to use the explicit "->" syntax in order
655     # to invoke the ZOOM::Record constructor here, even though I don't
656     # have to do the same for _new ZOOM::ResultSet above.  Weird.
657     return ZOOM::Record->_new($this, $which, $_rec);
658 }
659
660 sub record_immediate {
661     my $this = shift();
662     my($which) = @_;
663
664     my $_rec = Net::Z3950::ZOOM::resultset_record_immediate($this->_rs(),
665                                                             $which);
666     $this->{conn}->_check();
667     # The record might legitimately not be there yet
668     return undef if !defined $_rec;
669
670     return ZOOM::Record->_new($this, $which, $_rec);
671 }
672
673 sub cache_reset {
674     my $this = shift();
675
676     Net::Z3950::ZOOM::resultset_cache_reset($this->_rs());
677 }
678
679 sub records {
680     my $this = shift();
681     my($start, $count, $return_records) = @_;
682
683     my $raw = Net::Z3950::ZOOM::resultset_records($this->_rs(), $start, $count,
684                                                   $return_records);
685     # By design, $raw may be undefined (if $return_records is true)
686     return undef if !defined $raw;
687
688     # We need to package up the returned records in ZOOM::Record objects
689     my @res = ();
690     for my $i (0 .. @$raw-1) {
691         my $_rec = $raw->[$i];
692         if (!defined $_rec) {
693             push @res, undef;
694         } else {
695             push @res, ZOOM::Record->_new($this, $start+$i, $_rec);
696         }
697     }
698
699     return \@res;
700 }
701
702 sub sort {
703     my $this = shift();
704     my($sort_type, $sort_spec) = @_;
705
706     return Net::Z3950::ZOOM::resultset_sort1($this->_rs(),
707                                              $sort_type, $sort_spec);
708 }
709
710 sub destroy {
711     my $this = shift();
712
713     Net::Z3950::ZOOM::resultset_destroy($this->_rs());
714     $this->{_rs} = undef;
715 }
716
717
718 # ----------------------------------------------------------------------------
719
720 package ZOOM::Record;
721
722 sub new {
723     my $class = shift();
724     die "You can't create $class objects directly";
725 }
726
727 # PRIVATE to ZOOM::ResultSet::record(),
728 # ZOOM::ResultSet::record_immediate(), ZOOM::ResultSet::records() and
729 # ZOOM::Record::clone()
730 #
731 sub _new {
732     my $class = shift();
733     my($rs, $which, $_rec) = @_;
734
735     return bless {
736         rs => $rs,
737         which => $which,
738         _rec => $_rec,
739     }, $class;
740 }
741
742 # PRIVATE to this class
743 sub _rec {
744     my $this = shift();
745
746     my $_rec = $this->{_rec};
747     die "{_rec} undefined: has this Record been destroy()ed?"
748         if !defined $_rec;
749
750     return $_rec;
751 }
752
753 sub render {
754     my $this = shift();
755
756     return $this->get("render", @_);
757 }
758
759 sub raw {
760     my $this = shift();
761
762     return $this->get("raw", @_);
763 }
764
765 sub get {
766     my $this = shift();
767     my($type, $args) = @_;
768
769     $type = "$type;$args" if defined $args;
770     my $len = 0;
771     my $string = Net::Z3950::ZOOM::record_get($this->_rec(), $type, $len);
772     # I don't think we need '$len' at all.  ### Probably the Perl-to-C
773     # glue code should use the value of `len' as well as the opaque
774     # data-pointer returned, to ensure that the SV contains all of the
775     # returned data and does not stop at the first NUL character in
776     # binary data.  Carefully check the ZOOM_record_get() documentation.
777     return $string;
778 }
779
780 sub clone {
781     my $this = shift();
782
783     my $raw = Net::Z3950::ZOOM::record_clone($this->_rec())
784         or ZOOM::_oops(ZOOM::Error::CLONE);
785
786     # Arg 1 (rs) is undefined as the new record doesn't belong to an RS
787     return _new ZOOM::Record(undef, undef, $raw);
788 }
789
790 sub destroy {
791     my $this = shift();
792
793     Net::Z3950::ZOOM::record_destroy($this->_rec());
794     $this->{_rec} = undef;
795 }
796
797
798 # ----------------------------------------------------------------------------
799
800 package ZOOM::ScanSet;
801
802 sub new {
803     my $class = shift();
804     die "You can't create $class objects directly";
805 }
806
807 # PRIVATE to ZOOM::Connection::scan(),
808 sub _new {
809     my $class = shift();
810     my($conn, $startterm, $_ss) = @_;
811
812     return bless {
813         conn => $conn,
814         startterm => $startterm,# This is not currently used, which is
815                                 # just as well since it could be
816                                 # either a string (when the SS is
817                                 # created with scan()) or a
818                                 # ZOOM::Query object (when it's
819                                 # created with scan1())
820         _ss => $_ss,
821     }, $class;
822 }
823
824 # PRIVATE to this class
825 sub _ss {
826     my $this = shift();
827
828     my $_ss = $this->{_ss};
829     die "{_ss} undefined: has this ScanSet been destroy()ed?"
830         if !defined $_ss;
831
832     return $_ss;
833 }
834
835 sub option {
836     my $this = shift();
837     my($key, $value) = @_;
838
839     my $oldval = Net::Z3950::ZOOM::scanset_option_get($this->_ss(), $key);
840     Net::Z3950::ZOOM::scanset_option_set($this->_ss(), $key, $value)
841         if defined $value;
842
843     return $oldval;
844 }
845
846 sub size {
847     my $this = shift();
848
849     return Net::Z3950::ZOOM::scanset_size($this->_ss());
850 }
851
852 sub term {
853     my $this = shift();
854     my($which) = @_;
855
856     my($occ, $len) = (0, 0);
857     my $term = Net::Z3950::ZOOM::scanset_term($this->_ss(), $which,
858                                               $occ, $len)
859         or ZOOM::_oops(ZOOM::Error::SCANTERM);
860
861     die "length of term '$term' differs from returned len=$len"
862         if length($term) != $len;
863
864     return ($term, $occ);
865 }
866
867 sub display_term {
868     my $this = shift();
869     my($which) = @_;
870
871     my($occ, $len) = (0, 0);
872     my $term = Net::Z3950::ZOOM::scanset_display_term($this->_ss(), $which,
873                                                       $occ, $len)
874         or ZOOM::_oops(ZOOM::Error::SCANTERM);
875
876     die "length of display term '$term' differs from returned len=$len"
877         if length($term) != $len;
878
879     return ($term, $occ);
880 }
881
882 sub destroy {
883     my $this = shift();
884
885     Net::Z3950::ZOOM::scanset_destroy($this->_ss());
886     $this->{_ss} = undef;
887 }
888
889
890 # ----------------------------------------------------------------------------
891
892 package ZOOM::Package;
893
894 sub new {
895     my $class = shift();
896     die "You can't create $class objects directly";
897 }
898
899 # PRIVATE to ZOOM::Connection::package(),
900 sub _new {
901     my $class = shift();
902     my($conn, $options, $_p) = @_;
903
904     return bless {
905         conn => $conn,
906         options => $options,
907         _p => $_p,
908     }, $class;
909 }
910
911 # PRIVATE to this class
912 sub _p {
913     my $this = shift();
914
915     my $_p = $this->{_p};
916     die "{_p} undefined: has this Package been destroy()ed?"
917         if !defined $_p;
918
919     return $_p;
920 }
921
922 sub option {
923     my $this = shift();
924     my($key, $value) = @_;
925
926     my $oldval = Net::Z3950::ZOOM::package_option_get($this->_p(), $key);
927     Net::Z3950::ZOOM::package_option_set($this->_p(), $key, $value)
928         if defined $value;
929
930     return $oldval;
931 }
932
933 sub send {
934     my $this = shift();
935     my($type) = @_;
936
937     Net::Z3950::ZOOM::package_send($this->_p(), $type);
938     $this->{conn}->_check();
939 }
940
941 sub destroy {
942     my $this = shift();
943
944     Net::Z3950::ZOOM::package_destroy($this->_p());
945     $this->{_p} = undef;
946 }
947
948
949 # There follows trivial support for YAZ logging.  This is wired out
950 # into the Net::Z3950::ZOOM package, and we here provide wrapper
951 # functions -- nothing more than aliases, really -- in the ZOOM::Log
952 # package.  There really is no point in inventing an OO interface.
953 #
954 # Passing @_ directly to the underlying Net::Z3950::ZOOM::* functions
955 # doesn't work, for reasons that I can't begin to fathom, and that
956 # don't particularly interest me.  Unpacking into scalars and passing
957 # those _does_ work, so that's what we do.
958
959 package ZOOM::Log;
960
961 sub mask_str      { my($a) = @_; Net::Z3950::ZOOM::yaz_log_mask_str($a); }
962 sub module_level  { my($a) = @_; Net::Z3950::ZOOM::yaz_log_module_level($a); }
963 sub init          { my($a, $b, $c) = @_;
964                     Net::Z3950::ZOOM::yaz_log_init($a, $b, $c) }
965 sub init_file     { my($a) = @_; Net::Z3950::ZOOM::yaz_log_init_file($a) }
966 sub init_level    { my($a) = @_; Net::Z3950::ZOOM::yaz_log_init_level($a) }
967 sub init_prefix   { my($a) = @_; Net::Z3950::ZOOM::yaz_log_init_prefix($a) }
968 sub time_format   { my($a) = @_; Net::Z3950::ZOOM::yaz_log_time_format($a) }
969 sub init_max_size { my($a) = @_; Net::Z3950::ZOOM::yaz_log_init_max_size($a) }
970
971 sub log {
972     my($level, @message) = @_;
973
974     if ($level !~ /^(0x)?\d+$/) {
975         # Assuming its log-level name, we look it up.
976         my $num = module_level($level);
977         ZOOM::_oops(ZOOM::Error::LOGLEVEL, $level)
978             if $num == 0;
979         $level = $num;
980     }
981
982     Net::Z3950::ZOOM::yaz_log($level, join("", @message));
983 }
984
985
986 1;