+my $emsg= '';
+my @results;
+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);
+
+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 (!$results) {
+ if (!%m) {
+ $err->($chk->scall_method("nomatch",
+ spec => $each));
+ } elsif (keys(%m) > $chk->attr('maxambig')) {
+ $err->($chk->scall_method("manyambig"));
+ } else {
+ $err->($chk->scall_method("ambiguous",
+ spec => $each,
+ couldbe => join(', ', sort keys %m)));
+ }
+ }
+ push @results, $results->[0];
+};
+
+if (!defined $canontext) {
+ $canontext= join ' | ', map { $_->[0] } @results;
+}
+if ($chk->method_exists('postquery')) {
+ $chk->call_method('postquery', h => $hooks);
+}