X-Git-Url: http://git.indexdata.com/?a=blobdiff_plain;f=lib%2FZOOM.pm;h=5f87d1ee0966df023d3a5a320b5b062d2512ae57;hb=b81ecf91be67f569ec7a4dbdd65101f65c47a954;hp=b0a3792dec60551fd7bae6419c578f6508ddb86f;hpb=a9d3beaca6d299838a4d9f0fac2cd9de03f63335;p=ZOOM-Perl-moved-to-github.git diff --git a/lib/ZOOM.pm b/lib/ZOOM.pm index b0a3792..5f87d1e 100644 --- a/lib/ZOOM.pm +++ b/lib/ZOOM.pm @@ -1,4 +1,4 @@ -# $Id: ZOOM.pm,v 1.15 2005-11-08 15:55:45 mike Exp $ +# $Id: ZOOM.pm,v 1.19 2005-11-16 16:48:11 mike Exp $ use strict; use warnings; @@ -86,11 +86,10 @@ sub diag_str { return Net::Z3950::ZOOM::diag_str($code); } -### More of the ZOOM::Exception instantiations should use this sub _oops { - my($code, $addinfo) = @_; + my($code, $addinfo, $diagset) = @_; - die new ZOOM::Exception($code, diag_str($code), $addinfo); + die new ZOOM::Exception($code, diag_str($code), $addinfo, $diagset); } # ---------------------------------------------------------------------------- @@ -99,13 +98,13 @@ package ZOOM::Exception; sub new { my $class = shift(); - my($code, $message, $addinfo) = @_; - ### support diag-set, too + my($code, $message, $addinfo, $diagset) = @_; return bless { code => $code, message => $message, addinfo => $addinfo, + diagset => $diagset || "ZOOM", }, $class; } @@ -124,6 +123,20 @@ sub addinfo { return $this->{addinfo}; } +sub diagset { + my $this = shift(); + return $this->{diagset}; +} + +sub render { + my $this = shift(); + my $res = "ZOOM error " . $this->code() . ' "' . $this->message() . '"'; + $res .= ' (addinfo: "' . $this->addinfo() . '")' if $this->addinfo(); + return $res; +} + +# This means that untrapped exceptions render nicely. +use overload '""' => \&render; # ---------------------------------------------------------------------------- @@ -255,16 +268,35 @@ sub new { my $class = shift(); my($host, $port) = @_; - my $_conn = Net::Z3950::ZOOM::connection_new($host, $port); - my($errcode, $errmsg, $addinfo) = (undef, "dummy", "dummy"); - $errcode = Net::Z3950::ZOOM::connection_error($_conn, $errmsg, $addinfo); - die new ZOOM::Exception($errcode, $errmsg, $addinfo) if $errcode; - - return bless { + my $_conn = Net::Z3950::ZOOM::connection_new($host, $port || 0); + my $conn = bless { host => $host, port => $port, _conn => $_conn, }; + $conn->_check(); + return $conn; +} + +# PRIVATE to this class +sub _conn { + my $this = shift(); + + my $_conn = $this->{_conn}; + die "{_conn} undefined: has this Connection been destroy()ed?" + if !defined $_conn; + + return $_conn; +} + +sub _check { + my $this = shift(); + + my($errcode, $errmsg, $addinfo, $diagset) = (undef, "x", "x", "x"); + $errcode = Net::Z3950::ZOOM::connection_error_x($this->_conn(), $errmsg, + $addinfo, $diagset); + die new ZOOM::Exception($errcode, $errmsg, $addinfo, $diagset) + if $errcode; } sub create { @@ -279,17 +311,6 @@ sub create { }; } -# PRIVATE to this class -sub _conn { - my $this = shift(); - - my $_conn = $this->{_conn}; - die "{_conn} undefined: has this Connection been destroy()ed?" - if !defined $_conn; - - return $_conn; -} - sub error_x { my $this = shift(); @@ -314,15 +335,17 @@ sub addinfo { return Net::Z3950::ZOOM::connection_addinfo($this->_conn()); } +sub diagset { + my $this = shift(); + return Net::Z3950::ZOOM::connection_diagset($this->_conn()); +} + sub connect { my $this = shift(); my($host, $port) = @_; Net::Z3950::ZOOM::connection_connect($this->_conn(), $host, $port); - my($errcode, $errmsg, $addinfo) = (undef, "dummy", "dummy"); - $errcode = Net::Z3950::ZOOM::connection_error($this->_conn(), - $errmsg, $addinfo); - die new ZOOM::Exception($errcode, $errmsg, $addinfo) if $errcode; + $this->_check(); # No return value } @@ -357,11 +380,7 @@ sub search { my $_rs = Net::Z3950::ZOOM::connection_search($this->_conn(), $query->_query()); - my($errcode, $errmsg, $addinfo) = (undef, "dummy", "dummy"); - $errcode = Net::Z3950::ZOOM::connection_error($this->_conn(), - $errmsg, $addinfo); - die new ZOOM::Exception($errcode, $errmsg, $addinfo) if $errcode; - + $this->_check(); return _new ZOOM::ResultSet($this, $query, $_rs); } @@ -370,11 +389,7 @@ sub search_pqf { my($pqf) = @_; my $_rs = Net::Z3950::ZOOM::connection_search_pqf($this->_conn(), $pqf); - my($errcode, $errmsg, $addinfo) = (undef, "dummy", "dummy"); - $errcode = Net::Z3950::ZOOM::connection_error($this->_conn(), - $errmsg, $addinfo); - die new ZOOM::Exception($errcode, $errmsg, $addinfo) if $errcode; - + $this->_check(); return _new ZOOM::ResultSet($this, $pqf, $_rs); } @@ -383,11 +398,7 @@ sub scan { my($startterm) = @_; my $_ss = Net::Z3950::ZOOM::connection_scan($this->_conn(), $startterm); - my($errcode, $errmsg, $addinfo) = (undef, "dummy", "dummy"); - $errcode = Net::Z3950::ZOOM::connection_error($this->_conn(), - $errmsg, $addinfo); - die new ZOOM::Exception($errcode, $errmsg, $addinfo) if $errcode; - + $this->_check(); return _new ZOOM::ScanSet($this, $startterm, $_ss); } @@ -575,6 +586,7 @@ sub records { my $raw = Net::Z3950::ZOOM::resultset_records($this->_rs(), $start, $count, $return_records); + ### Why don't we throw an exception if $raw is undefined? return undef if !defined $raw; # We need to package up the returned records in ZOOM::Record objects @@ -595,8 +607,8 @@ sub sort { my $this = shift(); my($sort_type, $sort_spec) = @_; - Net::Z3950::ZOOM::resultset_sort($this->_rs(), $sort_type, $sort_spec); - ### There's no way to check for success, as this is a void function + return Net::Z3950::ZOOM::resultset_sort1($this->_rs(), + $sort_type, $sort_spec); } sub destroy { @@ -714,6 +726,17 @@ sub _ss { return $_ss; } +sub option { + my $this = shift(); + my($key, $value) = @_; + + my $oldval = Net::Z3950::ZOOM::scanset_option_get($this->_ss(), $key); + Net::Z3950::ZOOM::scanset_option_set($this->_ss(), $key, $value) + if defined $value; + + return $oldval; +} + sub size { my $this = shift(); @@ -727,6 +750,7 @@ sub term { my($occ, $len) = (0, 0); my $term = Net::Z3950::ZOOM::scanset_term($this->_ss(), $which, $occ, $len); + ### Throw exception? return undef if !defined $term; die "length of term '$term' differs from returned len=$len" if length($term) != $len; @@ -741,6 +765,7 @@ sub display_term { my($occ, $len) = (0, 0); my $term = Net::Z3950::ZOOM::scanset_display_term($this->_ss(), $which, $occ, $len); + ### Throw exception? return undef if !defined $term; die "length of display term '$term' differs from returned len=$len" if length($term) != $len; @@ -804,10 +829,7 @@ sub send { my($type) = @_; Net::Z3950::ZOOM::package_send($this->_p(), $type); - my($errcode, $errmsg, $addinfo) = (undef, "dummy", "dummy"); - $errcode = Net::Z3950::ZOOM::connection_error($this->{conn}->_conn(), - $errmsg, $addinfo); - die new ZOOM::Exception($errcode, $errmsg, $addinfo) if $errcode; + $this->{conn}->_check(); } sub destroy {