Many changes to accomodate modified ZOOM_query_ccl2rpn() API
[ZOOM-Perl-moved-to-github.git] / lib / ZOOM.pm
index 8ca0ad9..28dc285 100644 (file)
@@ -1,7 +1,8 @@
-# $Id: ZOOM.pm,v 1.29 2006-04-07 11:05:14 mike Exp $
+# $Id: ZOOM.pm,v 1.35 2006-06-15 15:43:14 mike Exp $
 
 use strict;
 use warnings;
+use IO::File;
 use Net::Z3950::ZOOM;
 
 
@@ -41,6 +42,8 @@ sub UNSUPPORTED_QUERY { Net::Z3950::ZOOM::ERROR_UNSUPPORTED_QUERY }
 sub INVALID_QUERY { Net::Z3950::ZOOM::ERROR_INVALID_QUERY }
 sub CQL_PARSE { Net::Z3950::ZOOM::ERROR_CQL_PARSE }
 sub CQL_TRANSFORM { Net::Z3950::ZOOM::ERROR_CQL_TRANSFORM }
+sub CCL_CONFIG { Net::Z3950::ZOOM::ERROR_CCL_CONFIG }
+sub CCL_PARSE { Net::Z3950::ZOOM::ERROR_CCL_PARSE }
 # The following are added specifically for this OO interface
 sub CREATE_QUERY { 20001 }
 sub QUERY_CQL { 20002 }
@@ -51,6 +54,22 @@ sub PACKAGE { 20006 }
 sub SCANTERM { 20007 }
 sub LOGLEVEL { 20008 }
 
+# Separate space for CCL errors.  Great.
+package ZOOM::CCL::Error;
+sub OK { Net::Z3950::ZOOM::CCL_ERR_OK }
+sub TERM_EXPECTED { Net::Z3950::ZOOM::CCL_ERR_TERM_EXPECTED }
+sub RP_EXPECTED { Net::Z3950::ZOOM::CCL_ERR_RP_EXPECTED }
+sub SETNAME_EXPECTED { Net::Z3950::ZOOM::CCL_ERR_SETNAME_EXPECTED }
+sub OP_EXPECTED { Net::Z3950::ZOOM::CCL_ERR_OP_EXPECTED }
+sub BAD_RP { Net::Z3950::ZOOM::CCL_ERR_BAD_RP }
+sub UNKNOWN_QUAL { Net::Z3950::ZOOM::CCL_ERR_UNKNOWN_QUAL }
+sub DOUBLE_QUAL { Net::Z3950::ZOOM::CCL_ERR_DOUBLE_QUAL }
+sub EQ_EXPECTED { Net::Z3950::ZOOM::CCL_ERR_EQ_EXPECTED }
+sub BAD_RELATION { Net::Z3950::ZOOM::CCL_ERR_BAD_RELATION }
+sub TRUNC_NOT_LEFT { Net::Z3950::ZOOM::CCL_ERR_TRUNC_NOT_LEFT }
+sub TRUNC_NOT_BOTH { Net::Z3950::ZOOM::CCL_ERR_TRUNC_NOT_BOTH }
+sub TRUNC_NOT_RIGHT { Net::Z3950::ZOOM::CCL_ERR_TRUNC_NOT_RIGHT }
+
 # The "Event" package contains constants returned by last_event()
 package ZOOM::Event;
 sub NONE { Net::Z3950::ZOOM::EVENT_NONE }
@@ -63,6 +82,7 @@ sub SEND_APDU { Net::Z3950::ZOOM::EVENT_SEND_APDU }
 sub RECV_APDU { Net::Z3950::ZOOM::EVENT_RECV_APDU }
 sub RECV_RECORD { Net::Z3950::ZOOM::EVENT_RECV_RECORD }
 sub RECV_SEARCH { Net::Z3950::ZOOM::EVENT_RECV_SEARCH }
+sub ZEND { Net::Z3950::ZOOM::EVENT_END }
 
 # ----------------------------------------------------------------------------
 
@@ -93,12 +113,10 @@ sub diag_str {
     return Net::Z3950::ZOOM::diag_str($code);
 }
 
-### Undocumented
 sub event_str {
     return Net::Z3950::ZOOM::event_str(@_);
 }
 
-### Undocumented
 sub event {
     my($connsref) = @_;
 
@@ -109,7 +127,7 @@ sub event {
 sub _oops {
     my($code, $addinfo, $diagset) = @_;
 
-    die new ZOOM::Exception($code, diag_str($code), $addinfo, $diagset);
+    die new ZOOM::Exception($code, undef, $addinfo, $diagset);
 }
 
 # ----------------------------------------------------------------------------
@@ -120,11 +138,18 @@ sub new {
     my $class = shift();
     my($code, $message, $addinfo, $diagset) = @_;
 
+    $diagset ||= "ZOOM";
+    if ($diagset eq "ZOOM") {
+       $message ||= ZOOM::diag_str($code);
+    } else {
+       # Should fill in messages for other diagsets, too.
+    }
+
     return bless {
        code => $code,
        message => $message,
        addinfo => $addinfo,
-       diagset => $diagset || "ZOOM",
+       diagset => $diagset,
     }, $class;
 }
 
@@ -289,22 +314,24 @@ sub new {
     my $class = shift();
     my($host, $port, @options) = @_;
 
-    my $_conn = Net::Z3950::ZOOM::connection_new($host, $port || 0);
-    my $conn = bless {
-       host => $host,
-       port => $port,
-       _conn => $_conn,
-    };
-
+    my $_opts = Net::Z3950::ZOOM::options_create();
     while (@options >= 2) {
        my $key = shift(@options);
        my $val = shift(@options);
-       $conn->option($key, $val);
+       Net::Z3950::ZOOM::options_set($_opts, $key, $val);
     }
 
     die "Odd number of options specified"
        if @options;
 
+    my $_conn = Net::Z3950::ZOOM::connection_create($_opts);
+    Net::Z3950::ZOOM::connection_connect($_conn, $host, $port || 0);
+    my $conn = bless {
+       host => $host,
+       port => $port,
+       _conn => $_conn,
+    };
+
     $conn->_check();
     return $conn;
 }
@@ -456,7 +483,6 @@ sub package {
     return _new ZOOM::Package($this, $options, $_p);
 }
 
-### Undocumented
 sub last_event {
     my $this = shift();
 
@@ -544,6 +570,43 @@ sub new {
 }
 
 
+# We have to work around the retarded ZOOM_query_ccl2rpn() API
+package ZOOM::Query::CCL2RPN;
+our @ISA = qw(ZOOM::Query);
+
+sub new {
+    my $class = shift();
+    my($string, $conn) = @_;
+
+    my $q = Net::Z3950::ZOOM::query_create()
+       or ZOOM::_oops(ZOOM::Error::CREATE_QUERY);
+
+    my $config = $conn->option("cclqual");
+    if (!defined $config) {
+       my $cclfile = $conn->option("cclfile")
+           or ZOOM::_oops(ZOOM::Error::CCL_CONFIG,
+                          "no 'cclqual' or 'cclfile' specified");
+       my $fh = new IO::File("<$cclfile")
+           or ZOOM::_oops(ZOOM::Error::CCL_CONFIG,
+                          "can't open cclfile '$cclfile': $!");
+       $config = join("", <$fh>);
+       $fh->close();
+    }
+
+    my($ccl_errcode, $ccl_errstr, $ccl_errpos) = (0, "", 0);
+    if (Net::Z3950::ZOOM::query_ccl2rpn($q, $string, $config,
+                                       $ccl_errcode, $ccl_errstr,
+                                       $ccl_errpos) < 0) {
+       # We have no use for $ccl_errcode or $ccl_errpos
+       ZOOM::_oops(ZOOM::Error::CCL_PARSE, $ccl_errstr);
+    }
+
+    return bless {
+       _query => $q,
+    }, $class;
+}
+
+
 package ZOOM::Query::PQF;
 our @ISA = qw(ZOOM::Query);