/[webpac]/trunk/index_DBI_cache.pm
This is repository of my old source code which isn't updated any more. Go to git.rot13.org for current projects!
ViewVC logotype

Diff of /trunk/index_DBI_cache.pm

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 95 by dpavlin, Sun Jul 13 21:54:22 2003 UTC revision 142 by dpavlin, Thu Oct 30 01:02:22 2003 UTC
# Line 107  sub insert { Line 107  sub insert {
107          }          }
108  }  }
109    
110  sub check {  sub count {
111          my $self = shift;          my $self = shift;
112    
113          my $field = shift;          my $field = shift;
114            my $where = shift;
115    
116          my $sql = "select count(*) from $field";          my $sql = "select count(*) from $field where upper(item) like upper(?)||'%'";
117    
118          my $sth = $self->{dbh}->prepare($sql) || die $self->{dbh}->errstr();          my $sth = $self->{dbh}->prepare($sql) || die $self->{dbh}->errstr();
119          $sth->execute() || die "sql: $sql; ".$self->{dbh}->errstr();          $sth->execute($where) || die "sql: $sql; ".$self->{dbh}->errstr();
120    
121          my ($total) = $sth->fetchrow_array();          my ($total) = $sth->fetchrow_array();
122    
123          return $total;          # no results, count all
124            if (! $total) {
125                    my $sql = "select count(*) from $field";
126    
127                    my $sth = $self->{dbh}->prepare($sql) || die $self->{dbh}->errstr();
128                    $sth->execute() || die "sql: $sql; ".$self->{dbh}->errstr();
129                    $total = $sth->fetchrow_array();
130    
131            }
132    
133            return $total || 1;
134  }  }
135    
136    
# Line 127  sub fetch { Line 138  sub fetch {
138          my $self = shift;          my $self = shift;
139    
140          my $field = shift;          my $field = shift;
         my $what = shift || 'item';     # 'item,ident'  
141          my $where = shift;          my $where = shift;
142    
143          my $from_ord = shift || 0;          my $from_ord = shift || 0;
# Line 135  sub fetch { Line 145  sub fetch {
145    
146          my @sql_args;          my @sql_args;
147    
148          my $sql = "select $what,ord from $field";          my $sql = "select item,ord from $field";
149    
150          if ($where) {          if ($where) {
151                  my $sql2 = " select ord from $field where upper($what) like upper(?)||'%'";                  my $sql2 = "select ord from $field where upper(item) like upper(?)||'%'";
152                  my $sth = $self->{dbh}->prepare($sql2) || die "sql2: $sql2; ".$self->{dbh}->errstr();                  my $sth = $self->{dbh}->prepare($sql2) || die "sql2: $sql2; ".$self->{dbh}->errstr();
153    
154                  $sth->execute($where) || die "sql2: $sql2; ".$self->{dbh}->errstr();                  $sth->execute($where) || die "sql2: $sql2; ".$self->{dbh}->errstr();
155                  if (my $row = $sth->fetchrow_hashref) {                  if (my $row = $sth->fetchrow_hashref) {
156                          $from_ord += $row->{ord} - 1;                          $from_ord += $row->{ord} - 1;
157                    } else {
158                            # if no match is found when searching from beginning
159                            # of word in index, try substring match anywhere
160                            $sql2 = "select ord from $field where upper(item) like '%'||upper(?)||'%'";
161                            $sth = $self->{dbh}->prepare($sql2) || die "sql2: $sql2; ".$self->{dbh}->errstr();
162                            $sth->execute($where) || die "sql2: $sql2; ".$self->{dbh}->errstr();
163                            if (my $row = $sth->fetchrow_hashref) {
164                                    $from_ord += $row->{ord} - 1;
165                            }
166                  }                  }
167          }          }
168          $sql .= " order by ord limit $rows offset $from_ord";          $sql .= " order by ord limit $rows offset $from_ord";

Legend:
Removed from v.95  
changed lines
  Added in v.142

  ViewVC Help
Powered by ViewVC 1.1.26