When a connection is in asynchronous mode, failing operations (search,
[ZOOM-Perl-moved-to-github.git] / lib / ZOOM.pm
1 # $Id: ZOOM.pm,v 1.46 2007-02-26 14:36:55 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 ZINIT { 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 (uc($diagset) eq "ZOOM" || uc($diagset) eq "BIB-1") {
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     my($always_die_on_error) = @_;
341
342     my($errcode, $errmsg, $addinfo, $diagset) = (undef, "x", "x", "x");
343     $errcode = Net::Z3950::ZOOM::connection_error_x($this->_conn(), $errmsg,
344                                                     $addinfo, $diagset);
345     if ($errcode) {
346         my $exception = new ZOOM::Exception($errcode, $errmsg, $addinfo,
347                                             $diagset);
348         if (!$this->option("async") || $always_die_on_error) {
349             ZOOM::Log::log("zoom_check", "throwing error $exception");
350             die $exception;
351         } else {
352             ZOOM::Log::log("zoom_check", "not reporting error $exception");
353         }
354     }
355 }
356
357 # This wrapper for _check() is called only from outside the ZOOM
358 # module, and therefore only in situations where an asynchronous
359 # application is actively asking for an exception to be thrown if an
360 # error has been detected.  So it passed always_die_on_error=1 to the
361 # underlying _check() method.
362 #
363 sub check {
364     my $this = shift();
365     return $this->_check(1);
366 }
367
368 sub create {
369     my $class = shift();
370     my(@options) = @_;
371
372     my $_opts;
373     if (@_ == 1) {
374         $_opts = $_[0]->_opts();
375     } else {
376         $_opts = Net::Z3950::ZOOM::options_create();
377         while (@options >= 2) {
378             my $key = shift(@options);
379             my $val = shift(@options);
380             Net::Z3950::ZOOM::options_set($_opts, $key, $val);
381         }
382
383         die "Odd number of options specified"
384             if @options;
385     }
386
387     my $_conn = Net::Z3950::ZOOM::connection_create($_opts);
388     my $conn = bless {
389         host => undef,
390         port => undef,
391         _conn => $_conn,
392     }, $class;
393     return $conn;
394 }
395
396 sub error_x {
397     my $this = shift();
398
399     my($errcode, $errmsg, $addinfo, $diagset) = (undef, "dummy", "dummy", "d");
400     $errcode = Net::Z3950::ZOOM::connection_error_x($this->_conn(), $errmsg,
401                                                     $addinfo, $diagset);
402     return wantarray() ? ($errcode, $errmsg, $addinfo, $diagset) : $errcode;
403 }
404
405 sub errcode {
406     my $this = shift();
407     return Net::Z3950::ZOOM::connection_errcode($this->_conn());
408 }
409
410 sub errmsg {
411     my $this = shift();
412     return Net::Z3950::ZOOM::connection_errmsg($this->_conn());
413 }
414
415 sub addinfo {
416     my $this = shift();
417     return Net::Z3950::ZOOM::connection_addinfo($this->_conn());
418 }
419
420 sub diagset {
421     my $this = shift();
422     return Net::Z3950::ZOOM::connection_diagset($this->_conn());
423 }
424
425 sub connect {
426     my $this = shift();
427     my($host, $port) = @_;
428
429     $port = 0 if !defined $port;
430     Net::Z3950::ZOOM::connection_connect($this->_conn(), $host, $port);
431     $this->_check();
432     # No return value
433 }
434
435 sub option {
436     my $this = shift();
437     my($key, $value) = @_;
438
439     my $oldval = Net::Z3950::ZOOM::connection_option_get($this->_conn(), $key);
440     Net::Z3950::ZOOM::connection_option_set($this->_conn(), $key, $value)
441         if defined $value;
442
443     return $oldval;
444 }
445
446 sub option_binary {
447     my $this = shift();
448     my($key, $value) = @_;
449
450     my $dummylen = 0;
451     my $oldval = Net::Z3950::ZOOM::connection_option_getl($this->_conn(),
452                                                           $key, $dummylen);
453     Net::Z3950::ZOOM::connection_option_setl($this->_conn(), $key,
454                                              $value, length($value))
455         if defined $value;
456
457     return $oldval;
458 }
459
460 sub search {
461     my $this = shift();
462     my($query) = @_;
463
464     my $_rs = Net::Z3950::ZOOM::connection_search($this->_conn(),
465                                                   $query->_query());
466     $this->_check();
467     return _new ZOOM::ResultSet($this, $query, $_rs);
468 }
469
470 sub search_pqf {
471     my $this = shift();
472     my($pqf) = @_;
473
474     my $_rs = Net::Z3950::ZOOM::connection_search_pqf($this->_conn(), $pqf);
475     $this->_check();
476     return _new ZOOM::ResultSet($this, $pqf, $_rs);
477 }
478
479 sub scan_pqf {
480     my $this = shift();
481     my($startterm) = @_;
482
483     my $_ss = Net::Z3950::ZOOM::connection_scan($this->_conn(), $startterm);
484     $this->_check();
485     return _new ZOOM::ScanSet($this, $startterm, $_ss);
486 }
487
488 sub scan {
489     my $this = shift();
490     my($query) = @_;
491
492     my $_ss = Net::Z3950::ZOOM::connection_scan1($this->_conn(),
493                                                  $query->_query());
494     $this->_check();
495     return _new ZOOM::ScanSet($this, $query, $_ss);
496 }
497
498 sub package {
499     my $this = shift();
500     my($options) = @_;
501
502     my $_o = defined $options ? $options->_opts() :
503         Net::Z3950::ZOOM::options_create();
504     my $_p = Net::Z3950::ZOOM::connection_package($this->_conn(), $_o)
505         or ZOOM::_oops(ZOOM::Error::PACKAGE);
506
507     return _new ZOOM::Package($this, $options, $_p);
508 }
509
510 sub last_event {
511     my $this = shift();
512
513     return Net::Z3950::ZOOM::connection_last_event($this->_conn());
514 }
515
516 sub is_idle {
517     my $this = shift();
518
519     return Net::Z3950::ZOOM::connection_is_idle($this->_conn());
520 }
521
522 sub peek_event {
523     my $this = shift();
524
525     return Net::Z3950::ZOOM::connection_peek_event($this->_conn());
526 }
527
528 sub destroy {
529     my $this = shift();
530
531     Net::Z3950::ZOOM::connection_destroy($this->_conn());
532     $this->{_conn} = undef;
533 }
534
535
536 # ----------------------------------------------------------------------------
537
538 package ZOOM::Query;
539
540 sub new {
541     my $class = shift();
542     die "You can't create $class objects: it's a virtual base class";
543 }
544
545 # PRIVATE to this class and ZOOM::Connection::search()
546 sub _query {
547     my $this = shift();
548
549     my $_query = $this->{_query};
550     die "{_query} undefined: has this Query been destroy()ed?"
551         if !defined $_query;
552
553     return $_query;
554 }
555
556 sub sortby {
557     my $this = shift();
558     my($sortby) = @_;
559
560     Net::Z3950::ZOOM::query_sortby($this->_query(), $sortby) == 0
561         or ZOOM::_oops(ZOOM::Error::SORTBY, $sortby);
562 }
563
564 sub destroy {
565     my $this = shift();
566
567     Net::Z3950::ZOOM::query_destroy($this->_query());
568     $this->{_query} = undef;
569 }
570
571
572 package ZOOM::Query::CQL;
573 our @ISA = qw(ZOOM::Query);
574
575 sub new {
576     my $class = shift();
577     my($string) = @_;
578
579     my $q = Net::Z3950::ZOOM::query_create()
580         or ZOOM::_oops(ZOOM::Error::CREATE_QUERY);
581     Net::Z3950::ZOOM::query_cql($q, $string) == 0
582         or ZOOM::_oops(ZOOM::Error::QUERY_CQL, $string);
583
584     return bless {
585         _query => $q,
586     }, $class;
587 }
588
589
590 package ZOOM::Query::CQL2RPN;
591 our @ISA = qw(ZOOM::Query);
592
593 sub new {
594     my $class = shift();
595     my($string, $conn) = @_;
596
597     my $q = Net::Z3950::ZOOM::query_create()
598         or ZOOM::_oops(ZOOM::Error::CREATE_QUERY);
599     # check() throws the exception we want; but we only want it on failure!
600     Net::Z3950::ZOOM::query_cql2rpn($q, $string, $conn->_conn()) == 0
601         or $conn->_check();
602
603     return bless {
604         _query => $q,
605     }, $class;
606 }
607
608
609 # We have to work around the retarded ZOOM_query_ccl2rpn() API
610 package ZOOM::Query::CCL2RPN;
611 our @ISA = qw(ZOOM::Query);
612
613 sub new {
614     my $class = shift();
615     my($string, $conn) = @_;
616
617     my $q = Net::Z3950::ZOOM::query_create()
618         or ZOOM::_oops(ZOOM::Error::CREATE_QUERY);
619
620     my $config = $conn->option("cclqual");
621     if (!defined $config) {
622         my $cclfile = $conn->option("cclfile")
623             or ZOOM::_oops(ZOOM::Error::CCL_CONFIG,
624                            "no 'cclqual' or 'cclfile' specified");
625         my $fh = new IO::File("<$cclfile")
626             or ZOOM::_oops(ZOOM::Error::CCL_CONFIG,
627                            "can't open cclfile '$cclfile': $!");
628         $config = join("", <$fh>);
629         $fh->close();
630     }
631
632     my($ccl_errcode, $ccl_errstr, $ccl_errpos) = (0, "", 0);
633     if (Net::Z3950::ZOOM::query_ccl2rpn($q, $string, $config,
634                                         $ccl_errcode, $ccl_errstr,
635                                         $ccl_errpos) < 0) {
636         # We have no use for $ccl_errcode or $ccl_errpos
637         ZOOM::_oops(ZOOM::Error::CCL_PARSE, $ccl_errstr);
638     }
639
640     return bless {
641         _query => $q,
642     }, $class;
643 }
644
645
646 package ZOOM::Query::PQF;
647 our @ISA = qw(ZOOM::Query);
648
649 sub new {
650     my $class = shift();
651     my($string) = @_;
652
653     my $q = Net::Z3950::ZOOM::query_create()
654         or ZOOM::_oops(ZOOM::Error::CREATE_QUERY);
655     Net::Z3950::ZOOM::query_prefix($q, $string) == 0
656         or ZOOM::_oops(ZOOM::Error::QUERY_PQF, $string);
657
658     return bless {
659         _query => $q,
660     }, $class;
661 }
662
663
664 # ----------------------------------------------------------------------------
665
666 package ZOOM::ResultSet;
667
668 sub new {
669     my $class = shift();
670     die "You can't create $class objects directly";
671 }
672
673 # PRIVATE to ZOOM::Connection::search() and ZOOM::Connection::search_pqf()
674 sub _new {
675     my $class = shift();
676     my($conn, $query, $_rs) = @_;
677
678     return bless {
679         conn => $conn,
680         query => $query,        # This is not currently used, which is
681                                 # just as well since it could be
682                                 # either a string (when the RS is
683                                 # created with search_pqf()) or a
684                                 # ZOOM::Query object (when it's
685                                 # created with search())
686         _rs => $_rs,
687     }, $class;
688 }
689
690 # PRIVATE to this class
691 sub _rs {
692     my $this = shift();
693
694     my $_rs = $this->{_rs};
695     die "{_rs} undefined: has this ResultSet been destroy()ed?"
696         if !defined $_rs;
697
698     return $_rs;
699 }
700
701 sub option {
702     my $this = shift();
703     my($key, $value) = @_;
704
705     my $oldval = Net::Z3950::ZOOM::resultset_option_get($this->_rs(), $key);
706     Net::Z3950::ZOOM::resultset_option_set($this->_rs(), $key, $value)
707         if defined $value;
708
709     return $oldval;
710 }
711
712 sub size {
713     my $this = shift();
714
715     return Net::Z3950::ZOOM::resultset_size($this->_rs());
716 }
717
718 sub record {
719     my $this = shift();
720     my($which) = @_;
721
722     my $_rec = Net::Z3950::ZOOM::resultset_record($this->_rs(), $which);
723     $this->{conn}->_check();
724
725     # Even if no error has occurred, I think record() might
726     # legitimately return undef if we're running in asynchronous mode
727     # and the record just hasn't been retrieved yet.  This goes double
728     # for record_immediate().
729     return undef if !defined $_rec;
730
731     # For some reason, I have to use the explicit "->" syntax in order
732     # to invoke the ZOOM::Record constructor here, even though I don't
733     # have to do the same for _new ZOOM::ResultSet above.  Weird.
734     return ZOOM::Record->_new($this, $which, $_rec);
735 }
736
737 sub record_immediate {
738     my $this = shift();
739     my($which) = @_;
740
741     my $_rec = Net::Z3950::ZOOM::resultset_record_immediate($this->_rs(),
742                                                             $which);
743     $this->{conn}->_check();
744     # The record might legitimately not be there yet
745     return undef if !defined $_rec;
746
747     return ZOOM::Record->_new($this, $which, $_rec);
748 }
749
750 sub cache_reset {
751     my $this = shift();
752
753     Net::Z3950::ZOOM::resultset_cache_reset($this->_rs());
754 }
755
756 sub records {
757     my $this = shift();
758     my($start, $count, $return_records) = @_;
759
760     # If the request is out of range, ZOOM-C will currently (as of YAZ
761     # 2.1.38) no-op: it understandably refuses to build and send a
762     # known-bad APDU, but it doesn't set a diagnostic as it ought.  So
763     # for now, we do it here.  It would be more polite to stash the
764     # error-code in the ZOOM-C connection object for subsequent
765     # discovery (which is what ZOOM-C will presumably do itself when
766     # it's fixed) but since there is no API that allows us to do that,
767     # we just have to throw the exception right now.  That's probably
768     # OK for synchronous applications, but not really for
769     # multiplexers.
770     my $size = $this->size();
771     if ($start + $count-1 >= $size) {
772         # BIB-1 diagnostic 13 is "Present request out-of-range"
773         ZOOM::_oops(13, undef, "BIB-1");
774     }
775
776     my $raw = Net::Z3950::ZOOM::resultset_records($this->_rs(), $start, $count,
777                                                   $return_records);
778     # By design, $raw may be undefined (if $return_records is true)
779     return undef if !defined $raw;
780
781     # We need to package up the returned records in ZOOM::Record objects
782     my @res = ();
783     for my $i (0 .. @$raw-1) {
784         my $_rec = $raw->[$i];
785         if (!defined $_rec) {
786             push @res, undef;
787         } else {
788             push @res, ZOOM::Record->_new($this, $start+$i, $_rec);
789         }
790     }
791
792     return \@res;
793 }
794
795 sub sort {
796     my $this = shift();
797     my($sort_type, $sort_spec) = @_;
798
799     return Net::Z3950::ZOOM::resultset_sort1($this->_rs(),
800                                              $sort_type, $sort_spec);
801 }
802
803 sub destroy {
804     my $this = shift();
805
806     Net::Z3950::ZOOM::resultset_destroy($this->_rs());
807     $this->{_rs} = undef;
808 }
809
810
811 # ----------------------------------------------------------------------------
812
813 package ZOOM::Record;
814
815 sub new {
816     my $class = shift();
817     die "You can't create $class objects directly";
818 }
819
820 # PRIVATE to ZOOM::ResultSet::record(),
821 # ZOOM::ResultSet::record_immediate(), ZOOM::ResultSet::records() and
822 # ZOOM::Record::clone()
823 #
824 sub _new {
825     my $class = shift();
826     my($rs, $which, $_rec) = @_;
827
828     return bless {
829         rs => $rs,
830         which => $which,
831         _rec => $_rec,
832     }, $class;
833 }
834
835 # PRIVATE to this class
836 sub _rec {
837     my $this = shift();
838
839     my $_rec = $this->{_rec};
840     die "{_rec} undefined: has this Record been destroy()ed?"
841         if !defined $_rec;
842
843     return $_rec;
844 }
845
846 sub error {
847     my $this = shift();
848
849     my($errcode, $errmsg, $addinfo, $diagset) = (undef, "dummy", "dummy", "d");
850     $errcode = Net::Z3950::ZOOM::record_error($this->_rec(), $errmsg,
851                                               $addinfo, $diagset);
852
853     return wantarray() ? ($errcode, $errmsg, $addinfo, $diagset) : $errcode;
854 }
855
856 sub exception {
857     my $this = shift();
858
859     my($errcode, $errmsg, $addinfo, $diagset) = $this->error();
860     return undef if $errcode == 0;
861     return new ZOOM::Exception($errcode, $errmsg, $addinfo, $diagset);
862 }
863
864
865 sub render {
866     my $this = shift();
867
868     return $this->get("render", @_);
869 }
870
871 sub raw {
872     my $this = shift();
873
874     return $this->get("raw", @_);
875 }
876
877 sub get {
878     my $this = shift();
879     my($type, $args) = @_;
880
881     $type = "$type;$args" if defined $args;
882     my $len = 0;
883     my $string = Net::Z3950::ZOOM::record_get($this->_rec(), $type, $len);
884     # I don't think we need '$len' at all.  ### Probably the Perl-to-C
885     # glue code should use the value of `len' as well as the opaque
886     # data-pointer returned, to ensure that the SV contains all of the
887     # returned data and does not stop at the first NUL character in
888     # binary data.  Carefully check the ZOOM_record_get() documentation.
889     return $string;
890 }
891
892 sub clone {
893     my $this = shift();
894
895     my $raw = Net::Z3950::ZOOM::record_clone($this->_rec())
896         or ZOOM::_oops(ZOOM::Error::CLONE);
897
898     # Arg 1 (rs) is undefined as the new record doesn't belong to an RS
899     return _new ZOOM::Record(undef, undef, $raw);
900 }
901
902 sub destroy {
903     my $this = shift();
904
905     Net::Z3950::ZOOM::record_destroy($this->_rec());
906     $this->{_rec} = undef;
907 }
908
909
910 # ----------------------------------------------------------------------------
911
912 package ZOOM::ScanSet;
913
914 sub new {
915     my $class = shift();
916     die "You can't create $class objects directly";
917 }
918
919 # PRIVATE to ZOOM::Connection::scan(),
920 sub _new {
921     my $class = shift();
922     my($conn, $startterm, $_ss) = @_;
923
924     return bless {
925         conn => $conn,
926         startterm => $startterm,# This is not currently used, which is
927                                 # just as well since it could be
928                                 # either a string (when the SS is
929                                 # created with scan()) or a
930                                 # ZOOM::Query object (when it's
931                                 # created with scan1())
932         _ss => $_ss,
933     }, $class;
934 }
935
936 # PRIVATE to this class
937 sub _ss {
938     my $this = shift();
939
940     my $_ss = $this->{_ss};
941     die "{_ss} undefined: has this ScanSet been destroy()ed?"
942         if !defined $_ss;
943
944     return $_ss;
945 }
946
947 sub option {
948     my $this = shift();
949     my($key, $value) = @_;
950
951     my $oldval = Net::Z3950::ZOOM::scanset_option_get($this->_ss(), $key);
952     Net::Z3950::ZOOM::scanset_option_set($this->_ss(), $key, $value)
953         if defined $value;
954
955     return $oldval;
956 }
957
958 sub size {
959     my $this = shift();
960
961     return Net::Z3950::ZOOM::scanset_size($this->_ss());
962 }
963
964 sub term {
965     my $this = shift();
966     my($which) = @_;
967
968     my($occ, $len) = (0, 0);
969     my $term = Net::Z3950::ZOOM::scanset_term($this->_ss(), $which,
970                                               $occ, $len)
971         or ZOOM::_oops(ZOOM::Error::SCANTERM);
972
973     die "length of term '$term' differs from returned len=$len"
974         if length($term) != $len;
975
976     return ($term, $occ);
977 }
978
979 sub display_term {
980     my $this = shift();
981     my($which) = @_;
982
983     my($occ, $len) = (0, 0);
984     my $term = Net::Z3950::ZOOM::scanset_display_term($this->_ss(), $which,
985                                                       $occ, $len)
986         or ZOOM::_oops(ZOOM::Error::SCANTERM);
987
988     die "length of display term '$term' differs from returned len=$len"
989         if length($term) != $len;
990
991     return ($term, $occ);
992 }
993
994 sub destroy {
995     my $this = shift();
996
997     Net::Z3950::ZOOM::scanset_destroy($this->_ss());
998     $this->{_ss} = undef;
999 }
1000
1001
1002 # ----------------------------------------------------------------------------
1003
1004 package ZOOM::Package;
1005
1006 sub new {
1007     my $class = shift();
1008     die "You can't create $class objects directly";
1009 }
1010
1011 # PRIVATE to ZOOM::Connection::package(),
1012 sub _new {
1013     my $class = shift();
1014     my($conn, $options, $_p) = @_;
1015
1016     return bless {
1017         conn => $conn,
1018         options => $options,
1019         _p => $_p,
1020     }, $class;
1021 }
1022
1023 # PRIVATE to this class
1024 sub _p {
1025     my $this = shift();
1026
1027     my $_p = $this->{_p};
1028     die "{_p} undefined: has this Package been destroy()ed?"
1029         if !defined $_p;
1030
1031     return $_p;
1032 }
1033
1034 sub option {
1035     my $this = shift();
1036     my($key, $value) = @_;
1037
1038     my $oldval = Net::Z3950::ZOOM::package_option_get($this->_p(), $key);
1039     Net::Z3950::ZOOM::package_option_set($this->_p(), $key, $value)
1040         if defined $value;
1041
1042     return $oldval;
1043 }
1044
1045 sub send {
1046     my $this = shift();
1047     my($type) = @_;
1048
1049     Net::Z3950::ZOOM::package_send($this->_p(), $type);
1050     $this->{conn}->_check();
1051 }
1052
1053 sub destroy {
1054     my $this = shift();
1055
1056     Net::Z3950::ZOOM::package_destroy($this->_p());
1057     $this->{_p} = undef;
1058 }
1059
1060
1061 # There follows trivial support for YAZ logging.  This is wired out
1062 # into the Net::Z3950::ZOOM package, and we here provide wrapper
1063 # functions -- nothing more than aliases, really -- in the ZOOM::Log
1064 # package.  There really is no point in inventing an OO interface.
1065 #
1066 # Passing @_ directly to the underlying Net::Z3950::ZOOM::* functions
1067 # doesn't work, for reasons that I can't begin to fathom, and that
1068 # don't particularly interest me.  Unpacking into scalars and passing
1069 # those _does_ work, so that's what we do.
1070
1071 package ZOOM::Log;
1072
1073 sub mask_str      { my($a) = @_; Net::Z3950::ZOOM::yaz_log_mask_str($a); }
1074 sub module_level  { my($a) = @_; Net::Z3950::ZOOM::yaz_log_module_level($a); }
1075 sub init          { my($a, $b, $c) = @_;
1076                     Net::Z3950::ZOOM::yaz_log_init($a, $b, $c) }
1077 sub init_file     { my($a) = @_; Net::Z3950::ZOOM::yaz_log_init_file($a) }
1078 sub init_level    { my($a) = @_; Net::Z3950::ZOOM::yaz_log_init_level($a) }
1079 sub init_prefix   { my($a) = @_; Net::Z3950::ZOOM::yaz_log_init_prefix($a) }
1080 sub time_format   { my($a) = @_; Net::Z3950::ZOOM::yaz_log_time_format($a) }
1081 sub init_max_size { my($a) = @_; Net::Z3950::ZOOM::yaz_log_init_max_size($a) }
1082
1083 sub log {
1084     my($level, @message) = @_;
1085
1086     if ($level !~ /^(0x)?\d+$/) {
1087         # Assuming its log-level name, we look it up.
1088         my $num = module_level($level);
1089         ZOOM::_oops(ZOOM::Error::LOGLEVEL, $level)
1090             if $num == 0;
1091         $level = $num;
1092     }
1093
1094     Net::Z3950::ZOOM::yaz_log($level, join("", @message));
1095 }
1096
1097
1098 1;