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