X-Git-Url: http://www.chiark.greenend.org.uk/ucgi/~yarrgweb/git?p=ypp-sc-tools.db-live.git;a=blobdiff_plain;f=yarrg%2Fweb%2Fqtextstringcheck;h=4adb606ea0b2c3e25dc8251dac335a47ee24e7e6;hp=337ed31f2deb156e831b491f1d6883abda40900a;hb=422fab9f34f08090bca02f67cb41bba31f816c7e;hpb=b6d8c4a781c0bedf79a4b13af5afe9ad47de97ed diff --git a/yarrg/web/qtextstringcheck b/yarrg/web/qtextstringcheck index 337ed31..4adb606 100755 --- a/yarrg/web/qtextstringcheck +++ b/yarrg/web/qtextstringcheck @@ -43,6 +43,8 @@ $format $ctype => undef $string $what +$dbh => undef +$debug => 0 <%flags> @@ -54,53 +56,77 @@ use JSON; use Data::Dumper; use HTML::Entities; use CommodsWeb; +use Scalar::Util qw(blessed); die if $what =~ m/[^a-z]/; -my $specifics= "check_${what}"; -my $specific= $m->fetch_comp($specifics); +my $chk= $m->fetch_comp("check_${what}"); -my $dbh= dbw_connect($ocean); -my $sqlstmt= $specific->scall_method("sqlstmt"); -my $sth= $dbh->prepare($sqlstmt); -my @sqlstmt_qs= $sqlstmt =~ m/\?/g; +my $mydbh; +$dbh ||= ($mydbh= dbw_connect($ocean)); -#die "$sqlstmt @sqlstmt_qs"; +my $debugf= !$debug ? sub { } : sub { + print "@_\n"; +}; + +$debugf->("QTSC STRING '$string'"); my $emsg= ''; my @results; +my $canontext; + +$string =~ s/^\s*//; +$string =~ s/\s$//; +$string =~ s/\s+/ /g; -my @specs= $specific->attr('multiple') ? (split m#[/|,]#, $string) : ($string); - -foreach my $each (@specs) { - $each =~ s/^\s*//; $each =~ s/\s*$//; $each =~ s/\s+/ /g; - next if !length $each; - my $err= sub { $emsg= $_[0]; last; }; - my %m; - my $results; - foreach my $pat ("$each", "$each\%", "\%$each\%") { - $sth->execute(($pat) x @sqlstmt_qs); - $results= $sth->fetchall_arrayref(); - last if @$results==1; - map { $m{ $_->[0] }=1 } @$results; - $results= undef; +if ($chk->method_exists('execute')) { + ($canontext, @results)= eval { + $chk->call_method('execute', + dbh => $dbh, string => $string, + debugf => $debugf); + }; + if ($@) { + die unless blessed $@ && $@->isa('CommodsWeb::ExpectedError'); + $emsg= $@->emsg(); } - if (!$results) { - if (!%m) { - $err->($specific->scall_method("nomatch", - spec => $each)); - } elsif (keys(%m) > 5) { - $err->(' '); - } else { - $err->($specific->scall_method("ambiguous", - spec => $each, - couldbe => join(', ', sort keys %m))); +} else { + my $sqlstmt= $chk->scall_method("sqlstmt"); + my $sth= $dbh->prepare($sqlstmt); + my @sqlstmt_nqs= $sqlstmt =~ m/\?/g; + my $sqlstmt_nqs= @sqlstmt_nqs; + + my @specs= $chk->attr('multiple') + ? (split m#[/|,]#, $string) + : ($string); + + foreach my $each (@specs) { + next unless $each =~ m/\S/; + my ($temsg, @tresults) = + dbw_lookup_string($each, + $sth, $sqlstmt_nqs, + $chk->attr_exists('abbrev_initials'), + $chk->attr('maxambig'), + $chk->scall_method("nomatch", spec => $each), + $chk->scall_method("manyambig"), + sub { + $chk->scall_method("ambiguous", + spec => $each, couldbe => $_[0]) + }); + if (defined $temsg) { + $emsg= $temsg; + last; } - } - push @results, $results->[0]; -}; + push @results, [ @tresults ]; + }; +} + +if (!defined $canontext) { + $canontext= join ' | ', map { $_->[0] } @results; +} $emsg='' if !defined $emsg; -my $canontext= join ' | ', map { $_->[0] } @results; +@results=() if length $emsg; + +$debugf->("QTSC EMSG='$emsg' RESULTS='@results'"); if ($format =~ /json/) { $r->content_type($ctype or $format); @@ -117,10 +143,10 @@ if ($format =~ /dump/) { print Dumper($emsg, $canontext, \@results); } -$dbh->rollback(); +$mydbh->rollback() if $mydbh; return $emsg, $canontext, - [ @results ]; + @results;