/[webpac]/branches/fsb/index_DBI_filter.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 /branches/fsb/index_DBI_filter.pm

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

revision 642 by dpavlin, Sun Jan 23 14:31:02 2005 UTC revision 643 by dpavlin, Sun Jan 23 15:18:03 2005 UTC
# Line 117  sub count { Line 117  sub count {
117          my $field = shift;          my $field = shift;
118          my $where = shift;          my $where = shift;
119    
120          my $sql = "select count(*) from index where name = ? and upper(item) like upper(?)||'%'";          my $filter = shift;
121    
122          my $sth = $self->{dbh}->prepare($sql) || die $self->{dbh}->errstr();          my $tables_sql = 'index';
123          $sth->execute($field,$where) || die "sql: $sql; ".$self->{dbh}->errstr();          my $where_sql = '';
124            my @sql_args = ( $field, $where );
125    
126            if ($filter) {
127                    $tables_sql .= ",filters";
128                    $where_sql .= "
129                            and index.ord = filters.ord
130                            and filter = ?
131                    ";
132                    push @sql_args, $filter;
133            }
134    
135            my $sql = qq{
136                    select count(*)
137                    from $tables_sql
138                    where name = ? and upper(item) like upper(?)||'%'
139                    $where_sql
140            };
141    
142            my $sth = $self->{dbh}->prepare($sql) || confess $self->{dbh}->errstr();
143            $sth->execute(@sql_args) || confess "sql: $sql; ".$self->{dbh}->errstr();
144    
145          my ($total) = $sth->fetchrow_array();          my ($total) = $sth->fetchrow_array();
146    
147          # no results, count all          # no results, count all
148          if (! $total) {          if (! $total) {
149                  my $sql = "select count(*) from index wheere name = ?";                  my $sql = qq{
150                            select count(*)
151                            from $tables_sql
152                            where index.name = ?
153                            $where_sql
154                    };
155    
156                  my $sth = $self->{dbh}->prepare($sql) || die $self->{dbh}->errstr();                  @sql_args = ( $field );
157                  $sth->execute($field) || die "sql: $sql; ".$self->{dbh}->errstr();                  push @sql_args, $filter if ($filter);
158    
159                    my $sth = $self->{dbh}->prepare($sql) || confess $self->{dbh}->errstr();
160                    $sth->execute(@sql_args) || confess "sql: $sql; ".$self->{dbh}->errstr();
161                  $total = $sth->fetchrow_array();                  $total = $sth->fetchrow_array();
162    
163          }          }
164    
165          return $total || 1;          return $total || '0';
166  }  }
167    
168    
# Line 146  sub fetch { Line 174  sub fetch {
174    
175          my $offset = shift || 0;          my $offset = shift || 0;
176          my $rows = shift || 10;          my $rows = shift || 10;
177            my $filter = shift;
178    
179          my $from_ord = 0;          my $from_ord = 0;
180    
181          my @sql_args;          my $tables_sql = 'index';
182            my $where_sql = '';
183    
184          my $sql = qq{          my @sql_args = ( $field, $where );
185                  select item,display,count  
186                  from index          if ($filter) {
187                  where name = ?                  $tables_sql .= ",filters";
188                  and ord > ?                  $where_sql .= "
189                  order by ord                          and index.ord = filters.ord
190                  limit ? offset ?                          and filter = ?
191          };                  ";
192                    push @sql_args, $filter;
193            }
194    
195          if ($where) {          if ($where) {
196                  my $sql2 = "select ord from index where name = ? and upper(item) like upper(?)||'%'";                  my $sql2 = qq{
197                  my $sth = $self->{dbh}->prepare($sql2) || die "sql2: $sql2; ".$self->{dbh}->errstr();                          select index.ord as ord
198                            from $tables_sql
199                            where name = ? and upper(item) like upper(?)||'%'
200                            $where_sql
201                    };
202                    my $sth = $self->{dbh}->prepare($sql2) || confess "sql2: $sql2; ".$self->{dbh}->errstr();
203    
204                  $sth->execute($field, $where) || die "sql2: $sql2; ".$self->{dbh}->errstr();                  $sth->execute(@sql_args) || confess "sql2: $sql2; ".$self->{dbh}->errstr();
205                  if (my $row = $sth->fetchrow_hashref) {                  if (my $row = $sth->fetchrow_hashref) {
206                          $from_ord += $row->{ord} - 1;                          $from_ord += $row->{ord} - 1;
207                  } else {                  } else {
208                          # if no match is found when searching from beginning                          # if no match is found when searching from beginning
209                          # of word in index, try substring match anywhere                          # of word in index, try substring match anywhere
210                          $sql2 = "select ord from index where name = ? and upper(item) like '% '||upper(?)||'%'";                          $sql2 = qq{
211                          $sth = $self->{dbh}->prepare($sql2) || die "sql2: $sql2; ".$self->{dbh}->errstr();                                  select index.ord as ord
212                          $sth->execute($field, $where) || die "sql2: $sql2; ".$self->{dbh}->errstr();                                  from $tables_sql
213                                    where name = ? and upper(item) like '% '||upper(?)||'%'
214                                    $where_sql
215                            };
216            
217                            $sth = $self->{dbh}->prepare($sql2) || confess "sql2: $sql2; ".$self->{dbh}->errstr();
218                            $sth->execute(@sql_args) || confess "sql2: $sql2; ".$self->{dbh}->errstr();
219    
220                          if (my $row = $sth->fetchrow_hashref) {                          if (my $row = $sth->fetchrow_hashref) {
221                                  $from_ord += $row->{ord} - 1;                                  $from_ord += $row->{ord} - 1;
222                          }                          }
223                  }                  }
224          }          }
225    
226          my $sth = $self->{dbh}->prepare($sql) || die "prepare: $sql; ".$self->{dbh}->errstr();          @sql_args = ( $field, $from_ord );
227          $sth->execute($field,$from_ord,$rows,$offset) || die "execute: $sql; ".$self->{dbh}->errstr();          push @sql_args, $filter if ($filter);
228            push @sql_args, ( $rows, $offset );
229    
230            my $sql = qq{
231                    select item,display,index.count as count
232                    from $tables_sql
233                    where name = ?
234                            and index.ord > ?
235                    $where_sql
236                            order by index.ord
237                            limit ? offset ?
238            };
239    
240            my $sth = $self->{dbh}->prepare($sql) || confess "prepare: $sql; ".$self->{dbh}->errstr();
241            $sth->execute(@sql_args) || confess "execute: $sql; ".$self->{dbh}->errstr();
242          my @arr;          my @arr;
243          while (my $row = $sth->fetchrow_hashref) {          while (my $row = $sth->fetchrow_hashref) {
244                  $row->{item} = HTML::Entities::encode($row->{item},' <>&"');                  $row->{item} = HTML::Entities::encode($row->{item},' <>&"');
# Line 196  sub close { Line 255  sub close {
255    
256          return if (! $self->{dbh});          return if (! $self->{dbh});
257    
258          $self->{dbh}->begin_work || die $self->{dbh}->errstr();          $self->{dbh}->begin_work || confess $self->{dbh}->errstr();
259    
260          $self->delete_and_create('index', qq{          $self->delete_and_create('index', qq{
261                  create table index (                  create table index (
# Line 253  sub close { Line 312  sub close {
312    
313          }          }
314    
315          $self->{dbh}->commit || die $self->{dbh}->errstr();          $self->{dbh}->commit || confess $self->{dbh}->errstr();
316    
317          $self->bench("vacuuming");          $self->bench("vacuuming");
318    

Legend:
Removed from v.642  
changed lines
  Added in v.643

  ViewVC Help
Powered by ViewVC 1.1.26