Support for record cloning and deletion.
[ZOOM-Perl-moved-to-github.git] / lib / ZOOM.pm
1 # $Id: ZOOM.pm,v 1.13 2005-11-07 15:48:08 mike Exp $
2
3 use strict;
4 use warnings;
5 use Net::Z3950::ZOOM;
6
7
8 package ZOOM;
9
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 # The following are added specifically for this OO interface
44 sub CREATE_QUERY { 20001 }
45 sub QUERY_CQL { 20002 }
46 sub QUERY_PQF { 20003 }
47 sub SORTBY { 20004 }
48 sub CLONE { 20005 }
49
50 # The "Event" package contains constants returned by last_event()
51 package ZOOM::Event;
52 sub NONE { Net::Z3950::ZOOM::EVENT_NONE }
53 sub CONNECT { Net::Z3950::ZOOM::EVENT_CONNECT }
54 sub SEND_DATA { Net::Z3950::ZOOM::EVENT_SEND_DATA }
55 sub RECV_DATA { Net::Z3950::ZOOM::EVENT_RECV_DATA }
56 sub TIMEOUT { Net::Z3950::ZOOM::EVENT_TIMEOUT }
57 sub UNKNOWN { Net::Z3950::ZOOM::EVENT_UNKNOWN }
58 sub SEND_APDU { Net::Z3950::ZOOM::EVENT_SEND_APDU }
59 sub RECV_APDU { Net::Z3950::ZOOM::EVENT_RECV_APDU }
60 sub RECV_RECORD { Net::Z3950::ZOOM::EVENT_RECV_RECORD }
61 sub RECV_SEARCH { Net::Z3950::ZOOM::EVENT_RECV_SEARCH }
62
63 # ----------------------------------------------------------------------------
64
65 package ZOOM;
66
67 sub diag_str {
68     my($code) = @_;
69
70     # Special cases for error specific to the OO layer
71     if ($code == ZOOM::Error::CREATE_QUERY) {
72         return "can't create query object";
73     } elsif ($code == ZOOM::Error::QUERY_CQL) {
74         return "can't set CQL query";
75     } elsif ($code == ZOOM::Error::QUERY_PQF) {
76         return "can't set prefix query";
77     } elsif ($code == ZOOM::Error::SORTBY) {
78         return "can't set sort-specification";
79     } elsif ($code == ZOOM::Error::CLONE) {
80         return "can't clone record";
81     }
82
83     return Net::Z3950::ZOOM::diag_str($code);
84 }
85
86 ### More of the ZOOM::Exception instantiations should use this
87 sub _oops {
88     my($code, $addinfo) = @_;
89
90     die new ZOOM::Exception($code, diag_str($code), $addinfo);
91 }
92
93 # ----------------------------------------------------------------------------
94
95 package ZOOM::Exception;
96
97 sub new {
98     my $class = shift();
99     my($code, $message, $addinfo) = @_;
100     ### support diag-set, too
101
102     return bless {
103         code => $code,
104         message => $message,
105         addinfo => $addinfo,
106     }, $class;
107 }
108
109 sub code {
110     my $this = shift();
111     return $this->{code};
112 }
113
114 sub message {
115     my $this = shift();
116     return $this->{message};
117 }
118
119 sub addinfo {
120     my $this = shift();
121     return $this->{addinfo};
122 }
123
124
125 # ----------------------------------------------------------------------------
126
127 package ZOOM::Options;
128
129 sub new {
130     my $class = shift();
131     my($p1, $p2) = @_;
132
133     my $opts;
134     if (@_ == 0) {
135         $opts = Net::Z3950::ZOOM::options_create();
136     } elsif (@_ == 1) {
137         $opts = Net::Z3950::ZOOM::options_create_with_parent($p1->_opts());
138     } elsif (@_ == 2) {
139         $opts = Net::Z3950::ZOOM::options_create_with_parent2($p1->_opts(),
140                                                               $p2->_opts());
141     } else {
142         die "can't make $class object with more than 2 parents";
143     }
144
145     return bless {
146         _opts => $opts,
147     }, $class;
148 }
149
150 sub _opts {
151     my $this = shift();
152
153     my $_opts = $this->{_opts};
154     die "{_opts} undefined: has this Options block been destroy()ed?"
155         if !defined $_opts;
156
157     return $_opts;
158 }
159
160 sub option {
161     my $this = shift();
162     my($key, $value) = @_;
163
164     my $oldval = Net::Z3950::ZOOM::options_get($this->_opts(), $key);
165     Net::Z3950::ZOOM::options_set($this->_opts(), $key, $value)
166         if defined $value;
167
168     return $oldval;
169 }
170
171 sub option_binary {
172     my $this = shift();
173     my($key, $value) = @_;
174
175     my $dummylen = 0;
176     my $oldval = Net::Z3950::ZOOM::options_getl($this->_opts(),
177                                                 $key, $dummylen);
178     Net::Z3950::ZOOM::options_setl($this->_opts(), $key,
179                                    $value, length($value))
180         if defined $value;
181
182     return $oldval;
183 }
184
185 # This is a bit stupid, since the scalar values that Perl returns from
186 # option() can be used as a boolean; but it's just possible that some
187 # applications will rely on ZOOM_options_get_bool()'s idiosyncratic
188 # interpretation of what constitutes truth.
189 #
190 sub bool {
191     my $this = shift();
192     my($key, $default) = @_;
193
194     return Net::Z3950::ZOOM::options_get_bool($this->_opts(), $key, $default);
195 }
196
197 # .. and the next two are even more stupid
198 sub int {
199     my $this = shift();
200     my($key, $default) = @_;
201
202     return Net::Z3950::ZOOM::options_get_int($this->_opts(), $key, $default);
203 }
204
205 sub set_int {
206     my $this = shift();
207     my($key, $value) = @_;
208
209     Net::Z3950::ZOOM::options_set_int($this->_opts(), $key, $value);
210 }
211
212 #   ### Feel guilty.  Feel very, very guilty.  I've not been able to
213 #       get the callback memory-management right in "ZOOM.xs", with
214 #       the result that the values of $function and $udata passed into
215 #       this function, which are on the stack, have sometimes been
216 #       freed by the time they're used by __ZOOM_option_callback(),
217 #       with hilarious results.  To avoid this, I copy the values into
218 #       module-scoped globals, and pass _those_ into the extension
219 #       function.  To avoid overwriting those globals by subsequent
220 #       calls, I keep all the old ones, pushed onto the @_function and
221 #       @_udata arrays, which means that THIS FUNCTION LEAKS MEMORY
222 #       LIKE IT'S GOING OUT OF FASHION.  Not nice.  One day, I should
223 #       fix this, but for now there's more important fish to fry.
224 #
225 my(@_function, @_udata);
226 sub set_callback {
227     my $o1 = shift();
228     my($function, $udata) = @_;
229
230     push @_function, $function;
231     push @_udata, $udata;
232     Net::Z3950::ZOOM::options_set_callback($o1->_opts(),
233                                            $_function[-1], $_udata[-1]);
234 }
235
236 sub destroy {
237     my $this = shift();
238
239     Net::Z3950::ZOOM::options_destroy($this->_opts());
240     $this->{_opts} = undef;
241 }
242
243
244 # ----------------------------------------------------------------------------
245
246 package ZOOM::Connection;
247
248 sub new {
249     my $class = shift();
250     my($host, $port) = @_;
251
252     my $_conn = Net::Z3950::ZOOM::connection_new($host, $port);
253     my($errcode, $errmsg, $addinfo) = (undef, "dummy", "dummy");
254     $errcode = Net::Z3950::ZOOM::connection_error($_conn, $errmsg, $addinfo);
255     die new ZOOM::Exception($errcode, $errmsg, $addinfo) if $errcode;
256
257     return bless {
258         host => $host,
259         port => $port,
260         _conn => $_conn,
261     };
262 }
263
264 sub create {
265     my $class = shift();
266     my($options) = @_;
267
268     my $_conn = Net::Z3950::ZOOM::connection_create($options->_opts());
269     return bless {
270         host => undef,
271         port => undef,
272         _conn => $_conn,
273     };
274 }
275
276 # PRIVATE to this class
277 sub _conn {
278     my $this = shift();
279
280     my $_conn = $this->{_conn};
281     die "{_conn} undefined: has this Connection been destroy()ed?"
282         if !defined $_conn;
283
284     return $_conn;
285 }
286
287 sub error_x {
288     my $this = shift();
289
290     my($errcode, $errmsg, $addinfo, $diagset) = (undef, "dummy", "dummy", "d");
291     $errcode = Net::Z3950::ZOOM::connection_error_x($this->_conn(), $errmsg,
292                                                     $addinfo, $diagset);
293     return ($errcode, $errmsg, $addinfo, $diagset);
294 }
295
296 sub errcode {
297     my $this = shift();
298     return Net::Z3950::ZOOM::connection_errcode($this->_conn());
299 }
300
301 sub errmsg {
302     my $this = shift();
303     return Net::Z3950::ZOOM::connection_errmsg($this->_conn());
304 }
305
306 sub addinfo {
307     my $this = shift();
308     return Net::Z3950::ZOOM::connection_addinfo($this->_conn());
309 }
310
311 sub connect {
312     my $this = shift();
313     my($host, $port) = @_;
314
315     Net::Z3950::ZOOM::connection_connect($this->_conn(), $host, $port);
316     my($errcode, $errmsg, $addinfo) = (undef, "dummy", "dummy");
317     $errcode = Net::Z3950::ZOOM::connection_error($this->_conn(),
318                                                   $errmsg, $addinfo);
319     die new ZOOM::Exception($errcode, $errmsg, $addinfo) if $errcode;
320     # No return value
321 }
322
323 sub option {
324     my $this = shift();
325     my($key, $value) = @_;
326
327     my $oldval = Net::Z3950::ZOOM::connection_option_get($this->_conn(), $key);
328     Net::Z3950::ZOOM::connection_option_set($this->_conn(), $key, $value)
329         if defined $value;
330
331     return $oldval;
332 }
333
334 sub option_binary {
335     my $this = shift();
336     my($key, $value) = @_;
337
338     my $dummylen = 0;
339     my $oldval = Net::Z3950::ZOOM::connection_option_getl($this->_conn(),
340                                                           $key, $dummylen);
341     Net::Z3950::ZOOM::connection_option_setl($this->_conn(), $key,
342                                              $value, length($value))
343         if defined $value;
344
345     return $oldval;
346 }
347
348 sub search {
349     my $this = shift();
350     my($query) = @_;
351
352     my $_rs = Net::Z3950::ZOOM::connection_search($this->_conn(),
353                                                   $query->_query());
354     my($errcode, $errmsg, $addinfo) = (undef, "dummy", "dummy");
355     $errcode = Net::Z3950::ZOOM::connection_error($this->_conn(),
356                                                   $errmsg, $addinfo);
357     die new ZOOM::Exception($errcode, $errmsg, $addinfo) if $errcode;
358
359     return _new ZOOM::ResultSet($this, $query, $_rs);
360 }
361
362 sub search_pqf {
363     my $this = shift();
364     my($pqf) = @_;
365
366     my $_rs = Net::Z3950::ZOOM::connection_search_pqf($this->_conn(), $pqf);
367     my($errcode, $errmsg, $addinfo) = (undef, "dummy", "dummy");
368     $errcode = Net::Z3950::ZOOM::connection_error($this->_conn(),
369                                                   $errmsg, $addinfo);
370     die new ZOOM::Exception($errcode, $errmsg, $addinfo) if $errcode;
371
372     return _new ZOOM::ResultSet($this, $pqf, $_rs);
373 }
374
375 sub destroy {
376     my $this = shift();
377
378     Net::Z3950::ZOOM::connection_destroy($this->_conn());
379     $this->{_conn} = undef;
380 }
381
382
383 # ----------------------------------------------------------------------------
384
385 package ZOOM::Query;
386
387 sub new {
388     my $class = shift();
389     die "You can't create $class objects: it's a virtual base class";
390 }
391
392 # PRIVATE to this class and ZOOM::Connection::search()
393 sub _query {
394     my $this = shift();
395
396     my $_query = $this->{_query};
397     die "{_query} undefined: has this Query been destroy()ed?"
398         if !defined $_query;
399
400     return $_query;
401 }
402
403 sub sortby {
404     my $this = shift();
405     my($sortby) = @_;
406
407     Net::Z3950::ZOOM::query_sortby($this->_query(), $sortby) == 0
408         or ZOOM::_oops(ZOOM::Error::SORTBY, $sortby);
409 }
410
411 sub destroy {
412     my $this = shift();
413
414     Net::Z3950::ZOOM::query_destroy($this->_query());
415     $this->{_query} = undef;
416 }
417
418
419 package ZOOM::Query::CQL;
420 our @ISA = qw(ZOOM::Query);
421
422 sub new {
423     my $class = shift();
424     my($string) = @_;
425
426     my $q = Net::Z3950::ZOOM::query_create()
427         or ZOOM::_oops(ZOOM::Error::CREATE_QUERY);
428     Net::Z3950::ZOOM::query_cql($q, $string) == 0
429         or ZOOM::_oops(ZOOM::Error::QUERY_CQL, $string);
430
431     return bless {
432         _query => $q,
433     }, $class;
434 }
435
436
437 package ZOOM::Query::PQF;
438 our @ISA = qw(ZOOM::Query);
439
440 sub new {
441     my $class = shift();
442     my($string) = @_;
443
444     my $q = Net::Z3950::ZOOM::query_create()
445         or ZOOM::_oops(ZOOM::Error::CREATE_QUERY);
446     Net::Z3950::ZOOM::query_prefix($q, $string) == 0
447         or ZOOM::_oops(ZOOM::Error::QUERY_PQF, $string);
448
449     return bless {
450         _query => $q,
451     }, $class;
452 }
453
454
455 # ----------------------------------------------------------------------------
456
457 package ZOOM::ResultSet;
458
459 sub new {
460     my $class = shift();
461     die "You can't create $class objects directly";
462 }
463
464 # PRIVATE to ZOOM::Connection::search() and ZOOM::Connection::search_pqf()
465 sub _new {
466     my $class = shift();
467     my($conn, $query, $_rs) = @_;
468
469     return bless {
470         conn => $conn,
471         query => $query,        # This is not currently used, which is
472                                 # just as well since it could be
473                                 # either a string (when the RS is
474                                 # created with search_pqf()) or a
475                                 # ZOOM::Query object (when it's
476                                 # created with search())
477         _rs => $_rs,
478     }, $class;
479 }
480
481 # PRIVATE to this class
482 sub _rs {
483     my $this = shift();
484
485     my $_rs = $this->{_rs};
486     die "{_rs} undefined: has this ResultSet been destroy()ed?"
487         if !defined $_rs;
488
489     return $_rs;
490 }
491
492 sub option {
493     my $this = shift();
494     my($key, $value) = @_;
495
496     my $oldval = Net::Z3950::ZOOM::resultset_option_get($this->_rs(), $key);
497     Net::Z3950::ZOOM::resultset_option_set($this->_rs(), $key, $value)
498         if defined $value;
499
500     return $oldval;
501 }
502
503 sub size {
504     my $this = shift();
505
506     return Net::Z3950::ZOOM::resultset_size($this->_rs());
507 }
508
509 sub record {
510     my $this = shift();
511     my($which) = @_;
512
513     my $_rec = Net::Z3950::ZOOM::resultset_record($this->_rs(), $which);
514     ### Check for error -- but how?
515     return undef if !defined $_rec;
516
517     # For some reason, I have to use the explicit "->" syntax in order
518     # to invoke the ZOOM::Record constructor here, even though I don't
519     # have to do the same for _new ZOOM::ResultSet above.  Weird.
520     return ZOOM::Record->_new($this, $which, $_rec);
521 }
522
523 sub record_immediate {
524     my $this = shift();
525     my($which) = @_;
526
527     my $_rec = Net::Z3950::ZOOM::resultset_record_immediate($this->_rs(),
528                                                             $which);
529     ### Check for error -- but how?
530     return undef if !defined $_rec;
531
532     return ZOOM::Record->_new($this, $which, $_rec);
533 }
534
535 sub cache_reset {
536     my $this = shift();
537
538     Net::Z3950::ZOOM::resultset_cache_reset($this->_rs());
539 }
540
541 sub records {
542     my $this = shift();
543     my($start, $count, $return_records) = @_;
544
545     my $raw = Net::Z3950::ZOOM::resultset_records($this->_rs(), $start, $count,
546                                                   $return_records);
547     return undef if !defined $raw;
548
549     # We need to package up the returned records in ZOOM::Record objects
550     my @res = ();
551     for my $i (0 .. @$raw-1) {
552         my $_rec = $raw->[$i];
553         if (!defined $_rec) {
554             push @res, undef;
555         } else {
556             push @res, ZOOM::Record->_new($this, $start+$i, $_rec);
557         }
558     }
559
560     return \@res;
561 }
562
563 sub sort {
564     my $this = shift();
565     my($sort_type, $sort_spec) = @_;
566
567     Net::Z3950::ZOOM::resultset_sort($this->_rs(), $sort_type, $sort_spec);
568     ### There's no way to check for success, as this is a void function
569 }
570
571 sub destroy {
572     my $this = shift();
573
574     Net::Z3950::ZOOM::resultset_destroy($this->_rs());
575     $this->{_rs} = undef;
576 }
577
578
579 # ----------------------------------------------------------------------------
580
581 package ZOOM::Record;
582
583 sub new {
584     my $class = shift();
585     die "You can't create $class objects directly";
586 }
587
588 # PRIVATE to ZOOM::ResultSet::record(),
589 # ZOOM::ResultSet::record_immediate(), ZOOM::ResultSet::records() and
590 # ZOOM::Record::clone()
591 #
592 sub _new {
593     my $class = shift();
594     my($rs, $which, $_rec) = @_;
595
596     return bless {
597         rs => $rs,
598         which => $which,
599         _rec => $_rec,
600     }, $class;
601 }
602
603 # PRIVATE to this class
604 sub _rec {
605     my $this = shift();
606
607     my $_rec = $this->{_rec};
608     die "{_rec} undefined: has this Record been destroy()ed?"
609         if !defined $_rec;
610
611     return $_rec;
612 }
613
614 sub render {
615     my $this = shift();
616
617     my $len = 0;
618     my $string = Net::Z3950::ZOOM::record_get($this->_rec(), "render", $len);
619     # I don't think we need '$len' at all.  ### Probably the Perl-to-C
620     # glue code should use the value of `len' as well as the opaque
621     # data-pointer returned, to ensure that the SV contains all of the
622     # returned data and does not stop at the first NUL character in
623     # binary data.  Carefully check the ZOOM_record_get() documentation.
624     return $string;
625 }
626
627 sub raw {
628     my $this = shift();
629
630     my $len = 0;
631     my $string = Net::Z3950::ZOOM::record_get($this->_rec(), "raw", $len);
632     # See comment about $len in render()
633     return $string;
634 }
635
636 sub clone {
637     my $this = shift();
638
639     my $raw = Net::Z3950::ZOOM::record_clone($this->_rec())
640         or ZOOM::_oops(ZOOM::Error::CLONE);
641
642     # Arg 1 (rs) is undefined as the new record doesn't belong to an RS
643     return _new ZOOM::Record(undef, undef, $raw);
644 }
645
646 sub destroy {
647     my $this = shift();
648
649     Net::Z3950::ZOOM::record_destroy($this->_rec());
650     $this->{_rec} = undef;
651 }
652
653
654 1;