added two missing characters (and still missing one)
[webpac] / index_DBI_cache.pm
index 79e70ba..c544fbf 100644 (file)
@@ -107,19 +107,30 @@ sub insert {
        }
 }
 
-sub check {
+sub count {
        my $self = shift;
 
        my $field = shift;
+       my $where = shift;
 
-       my $sql = "select count(*) from $field";
+       my $sql = "select count(*) from $field where upper(item) like upper(?)||'%'";
 
        my $sth = $self->{dbh}->prepare($sql) || die $self->{dbh}->errstr();
-       $sth->execute() || die "sql: $sql; ".$self->{dbh}->errstr();
+       $sth->execute($where) || die "sql: $sql; ".$self->{dbh}->errstr();
 
        my ($total) = $sth->fetchrow_array();
 
-       return $total;
+       # no results, count all
+       if (! $total) {
+               my $sql = "select count(*) from $field";
+
+               my $sth = $self->{dbh}->prepare($sql) || die $self->{dbh}->errstr();
+               $sth->execute() || die "sql: $sql; ".$self->{dbh}->errstr();
+               $total = $sth->fetchrow_array();
+
+       }
+
+       return $total || 1;
 }
 
 
@@ -127,7 +138,6 @@ sub fetch {
        my $self = shift;
 
        my $field = shift;
-       my $what = shift || 'item';     # 'item,ident'
        my $where = shift;
 
        my $from_ord = shift || 0;
@@ -135,10 +145,10 @@ sub fetch {
 
        my @sql_args;
 
-       my $sql = "select $what,ord from $field";
+       my $sql = "select item,ord from $field";
 
        if ($where) {
-               my $sql2 = "select ord from $field where upper($what) like upper(?)||'%'";
+               my $sql2 = "select ord from $field where upper(item) like upper(?)||'%'";
                my $sth = $self->{dbh}->prepare($sql2) || die "sql2: $sql2; ".$self->{dbh}->errstr();
 
                $sth->execute($where) || die "sql2: $sql2; ".$self->{dbh}->errstr();
@@ -147,7 +157,7 @@ sub fetch {
                } else {
                        # if no match is found when searching from beginning
                        # of word in index, try substring match anywhere
-                       $sql2 = "select ord from $field where upper($what) like '%'||upper(?)||'%'";
+                       $sql2 = "select ord from $field where upper(item) like '%'||upper(?)||'%'";
                        $sth = $self->{dbh}->prepare($sql2) || die "sql2: $sql2; ".$self->{dbh}->errstr();
                        $sth->execute($where) || die "sql2: $sql2; ".$self->{dbh}->errstr();
                        if (my $row = $sth->fetchrow_hashref) {