X-Git-Url: http://www.chiark.greenend.org.uk/ucgi/~yarrgweb/git?a=blobdiff_plain;f=yarrg%2FCommodsWeb.pm;h=adcff3492c154682263fe00698cf6fc6d8c7863a;hb=0145dc7f4fcaf62090a77fb2d69d5d7807c8d48d;hp=3ad8590fa9313cab26b1b7e225d745a146193e94;hpb=f59ea54fd2dea04b6dfc91d56d7056b61d5f3b17;p=ypp-sc-tools.db-test.git diff --git a/yarrg/CommodsWeb.pm b/yarrg/CommodsWeb.pm index 3ad8590..adcff34 100644 --- a/yarrg/CommodsWeb.pm +++ b/yarrg/CommodsWeb.pm @@ -51,6 +51,7 @@ BEGIN { @EXPORT = qw(&dbw_connect &ocean_list &sourcebasedir &to_json_shim &to_json_protecttags &set_ctype_utf8 + &expected_error &dbw_lookup_string &prettyprint_age &meta_prettyprint_age); %EXPORT_TAGS = ( ); @@ -91,7 +92,7 @@ my @ocean_list; sub ocean_list () { my $datadir= datadir(); if (!@ocean_list) { - my $fn= "$datadir/master-info.txt"; + my $fn= "$datadir/source-info.txt"; my $f= new IO::File $fn or die "$fn $!"; my @r; while (<$f>) { @@ -151,4 +152,48 @@ BEGIN { eval ' } +sub dbw_lookup_string ($$$$$$$$) { # => ( $emsg, @dbresults ) + my ($each, + $sth, $stmt_nqs, $abbrev_initials, $maxambig, + $em_nomatch, $em_manyambig, $emf_ambiguous) = @_; + + $each =~ s/^\s*//; $each =~ s/\s*$//; $each =~ s/\s+/ /g; + my %m; + my $results; + my @pats= ("$each", "$each \%", "$each\%", "\%$each\%"); + if ($abbrev_initials) { + push @pats, join ' ', map { "$_%" } split //, $each; + } + foreach my $pat (@pats) { + $sth->execute(($pat) x $stmt_nqs); + $results= $sth->fetchall_arrayref(); + last if @$results==1; + $m{ $_->[0] }=1 for @$results; + $results= undef; + } + if (!$results) { + if (!%m) { + return $em_nomatch; + } elsif (keys(%m) > $maxambig) { + return $em_manyambig; + } else { + return $emf_ambiguous->($each, join(', ', sort keys %m)); + } + } + return (undef, @{ $results->[0] }); +} + +sub expected_error ($) { + my $r= { Emsg => $_[0] }; + bless $r, 'CommodsWeb::ExpectedError'; + die $r; +} + +package CommodsWeb::ExpectedError; + +sub emsg ($) { + my ($self) = @_; + return $self->{Emsg}; +} + 1;