chiark / gitweb /
Revert "In hold" to "Hold:" change
[ypp-sc-tools.db-live.git] / yarrg / web / qtextstringcheck
index 5ef8971f644b63991af5d6eb5379be7fec1da596..686a506aa4c348bfefbc2b0f73fd2e160785937a 100755 (executable)
@@ -43,6 +43,7 @@ $format
 $ctype => undef
 $string
 $what
+$dbh => undef
 </%args>
 
 <%flags>
@@ -58,18 +59,32 @@ use CommodsWeb;
 die if $what =~ m/[^a-z]/;
 my $chk= $m->fetch_comp("check_${what}");
 
-my $dbh= dbw_connect($ocean);
+my $mydbh;
+$dbh ||= ($mydbh= dbw_connect($ocean));
 
-my $sqlstmt= $chk->scall_method("sqlstmt");
-my $sth= $dbh->prepare($sqlstmt);
-my @sqlstmt_qs= $sqlstmt =~ m/\?/g;
+#print STDERR "qtsc string=\`$string'\n";
 
-#die "$sqlstmt @sqlstmt_qs";
+my ($sth, @sqlstmt_qs);
+if ($chk->method_exists('sqlstmt')) {
+       my $sqlstmt= $chk->scall_method("sqlstmt");
+       $sth= $dbh->prepare($sqlstmt);
+       @sqlstmt_qs= $sqlstmt =~ m/\?/g;
+}
 
 my $emsg= '';
 my @results;
-
-my @specs= $chk->attr('multiple') ? (split m#[/|,]#, $string) : ($string);
+my @specs;
+my $canontext;
+my $hooks = {  Emsg => \$emsg,         String => \$string,
+               Results => \@results,   Specs => \@specs,
+               Canon => \$canontext
+           };
+
+if ($chk->method_exists('preparse')) {
+       $chk->call_method('preparse', h => $hooks);
+} else {
+       @specs= $chk->attr('multiple') ? (split m#[/|,]#, $string) : ($string);
+}
 
 no warnings qw(exiting);
 
@@ -79,7 +94,11 @@ foreach my $each (@specs) {
        my $err= sub { $emsg= $_[0]; last; };
        my %m;
        my $results;
-       foreach my $pat ("$each", "$each\%", "\%$each\%") {
+       my @pats= ("$each", "$each\%", "\%$each\%");
+       if ($chk->attr_exists('abbrev_initials')) {
+               push @pats, join ' ', map { "$_%" } split //, $each;
+       }
+       foreach my $pat (@pats) {
                $sth->execute(($pat) x @sqlstmt_qs);
                $results= $sth->fetchall_arrayref();
                last if @$results==1;
@@ -101,8 +120,17 @@ foreach my $each (@specs) {
        push @results, $results->[0];
 };
 
+if (!defined $canontext) {
+       $canontext= join ' | ', map { $_->[0] } @results;
+}
+if ($chk->method_exists('postquery')) {
+       $chk->call_method('postquery', h => $hooks);
+}
+
 $emsg='' if !defined $emsg;
-my $canontext= join ' | ', map { $_->[0] } @results;
+@results=() if length $emsg;
+
+#print STDERR "qtsc emsg=\`$emsg' results=\`@results'\n";
 
 if ($format =~ /json/) {
        $r->content_type($ctype or $format);
@@ -119,7 +147,7 @@ if ($format =~ /dump/) {
        print Dumper($emsg, $canontext, \@results);
 }
 
-$dbh->rollback();
+$mydbh->rollback() if $mydbh;
 
 return  $emsg,
        $canontext,