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