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

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

trunk/index_DBI_filter.pm revision 643 by dpavlin, Sun Jan 23 15:18:03 2005 UTC branches/ffzg/index_DBI_filter.pm revision 697 by dpavlin, Sun Mar 13 02:03:30 2005 UTC
# Line 11  use strict qw(vars); Line 11  use strict qw(vars);
11  use vars qw($Count);  use vars qw($Count);
12  use HTML::Entities;  use HTML::Entities;
13  use URI::Escape;  use URI::Escape;
 use locale;  
14  use Carp;  use Carp;
   
15  use DBI;  use DBI;
16    use locale;
17    
18  # bench time  # bench time
19  my $bench_time = time();  my $bench_time = time();
# Line 60  sub new { Line 59  sub new {
59  sub delete_and_create {  sub delete_and_create {
60          my $self = shift;          my $self = shift;
61    
62          my $index = shift || croak "need index name!";          my $table = shift || croak "need table name!";
63          my $sql = shift || croak "need sql to create table!";          my $sql = shift || croak "need sql to create table!";
64    
65          print STDERR "## delete_and_create($index)\n" if ($debug);          print STDERR "## delete_and_create($table)\n" if ($debug);
66    
67          my $sql_delete = "delete from $index";          my $sql_delete = "delete from $table";
68          my $sth = $self->{dbh}->prepare($sql_delete) || confess "can't prepare: $sql_delete";          my $sth = $self->{dbh}->prepare($sql_delete);
69    
70          if ($sth->execute()) {          if ($sth && $sth->execute()) {
71                  print STDERR "## deleted rows from table $index\n" if ($debug);                  print STDERR "## deleted rows from table $table\n" if ($debug);
72          } else {          } else {
73                  # can't delete from table, assume it doesn't exists!                  # can't delete from table, assume it doesn't exists!
74                  $self->{dbh}->rollback;                  $self->{dbh}->rollback;
75                  $self->{dbh}->do($sql) || confess "SQL: $sql ".$self->{dbh}->errstr();                  $self->{dbh}->do($sql) || confess "SQL: $sql ".$self->{dbh}->errstr();
76                  print STDERR "## creating table $index\n" if ($debug);                  print STDERR "## creating table $table\n" if ($debug);
77                  $self->{dbh}->begin_work;                  $self->{dbh}->begin_work;
78          }          }
79  }  }
# Line 103  sub insert { Line 102  sub insert {
102    
103          if (! $self->{c}->{$uc}->{$field}) {          if (! $self->{c}->{$uc}->{$field}) {
104  #print stderr "in index: $index_data\n";  #print stderr "in index: $index_data\n";
105                  $self->{c}->{$uc}->{$field}->{item} = $index_data;                  $self->{c}->{$uc}->{$field}->{item} = lc($index_data);
106                  $self->{c}->{$uc}->{$field}->{display} = $display;                  $self->{c}->{$uc}->{$field}->{display} = $display;
107          }          }
108    
# Line 119  sub count { Line 118  sub count {
118    
119          my $filter = shift;          my $filter = shift;
120    
121          my $tables_sql = 'index';          my $tables_sql = 'data';
122          my $where_sql = '';          my $where_sql = '';
123          my @sql_args = ( $field, $where );          my @sql_args = ( $field, lc($where) );
124    
125          if ($filter) {          if ($filter) {
126                  $tables_sql .= ",filters";                  $tables_sql .= ",filters";
127                  $where_sql .= "                  $where_sql .= "
128                          and index.ord = filters.ord                          and data.ord = filters.ord
129                          and filter = ?                          and filter = ?
130                  ";                  ";
131                  push @sql_args, $filter;                  push @sql_args, $filter;
# Line 135  sub count { Line 134  sub count {
134          my $sql = qq{          my $sql = qq{
135                  select count(*)                  select count(*)
136                  from $tables_sql                  from $tables_sql
137                  where name = ? and upper(item) like upper(?)||'%'                  where name = ? and item like ?||'%'
138                  $where_sql                  $where_sql
139          };          };
140    
# Line 149  sub count { Line 148  sub count {
148                  my $sql = qq{                  my $sql = qq{
149                          select count(*)                          select count(*)
150                          from $tables_sql                          from $tables_sql
151                          where index.name = ?                          where data.name = ?
152                          $where_sql                          $where_sql
153                  };                  };
154    
# Line 178  sub fetch { Line 177  sub fetch {
177    
178          my $from_ord = 0;          my $from_ord = 0;
179    
180          my $tables_sql = 'index';          my $tables_sql = 'data';
181          my $where_sql = '';          my $where_sql = '';
182    
183          my @sql_args = ( $field, $where );          my @sql_args = ( $field, lc($where) );
184    
185          if ($filter) {          if ($filter) {
186                  $tables_sql .= ",filters";                  $tables_sql .= ",filters";
187                  $where_sql .= "                  $where_sql .= "
188                          and index.ord = filters.ord                          and data.ord = filters.ord
189                          and filter = ?                          and filter = ?
190                  ";                  ";
191                  push @sql_args, $filter;                  push @sql_args, $filter;
# Line 194  sub fetch { Line 193  sub fetch {
193    
194          if ($where) {          if ($where) {
195                  my $sql2 = qq{                  my $sql2 = qq{
196                          select index.ord as ord                          select data.ord as ord
197                          from $tables_sql                          from $tables_sql
198                          where name = ? and upper(item) like upper(?)||'%'                          where name = ? and item like ?||'%'
199                          $where_sql                          $where_sql
200                            order by data.ord
201                  };                  };
202                  my $sth = $self->{dbh}->prepare($sql2) || confess "sql2: $sql2; ".$self->{dbh}->errstr();                  my $sth = $self->{dbh}->prepare($sql2) || confess "sql2: $sql2; ".$self->{dbh}->errstr();
203    
204                  $sth->execute(@sql_args) || confess "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 = qq{                          $sql2 = qq{
211                                  select index.ord as ord                                  select data.ord as ord
212                                  from $tables_sql                                  from $tables_sql
213                                  where name = ? and upper(item) like '% '||upper(?)||'%'                                  where name = ? and item like '%'||?||'%'
214                                  $where_sql                                  $where_sql
215                                    order by data.ord
216                          };                          };
217                    
218                          $sth = $self->{dbh}->prepare($sql2) || confess "sql2: $sql2; ".$self->{dbh}->errstr();                          $sth = $self->{dbh}->prepare($sql2) || confess "sql2: $sql2; ".$self->{dbh}->errstr();
219                          $sth->execute(@sql_args) || confess "sql2: $sql2; ".$self->{dbh}->errstr();                          $sth->execute(@sql_args) || confess "sql2: $sql2; ".$self->{dbh}->errstr();
220    
221                          if (my $row = $sth->fetchrow_hashref) {                          if (my $row = $sth->fetchrow_hashref) {
222                                  $from_ord += $row->{ord} - 1;                                  $from_ord = $row->{ord} - 1;
223                          }                          }
224                  }                  }
225          }          }
226    
227          @sql_args = ( $field, $from_ord );          @sql_args = ( $field, $from_ord );
228          push @sql_args, $filter if ($filter);          push @sql_args, $filter if ($filter);
         push @sql_args, ( $rows, $offset );  
229    
230          my $sql = qq{          my $sql = qq{
231                  select item,display,index.count as count                  select item,display,data.count as count
232                  from $tables_sql                  from $tables_sql
233                  where name = ?                  where name = ?
234                          and index.ord > ?                          and data.ord > ?
235                  $where_sql                  $where_sql
236                          order by index.ord                          order by data.ord
                         limit ? offset ?  
237          };          };
238    
239            # fix SQLite problem which doesn't allow placeholders in limit and offset
240            # http://thread.gmane.org/gmane.comp.db.sqlite.general/9707
241            $sql .= "limit $rows offset $offset";
242    
243          my $sth = $self->{dbh}->prepare($sql) || confess "prepare: $sql; ".$self->{dbh}->errstr();          my $sth = $self->{dbh}->prepare($sql) || confess "prepare: $sql; ".$self->{dbh}->errstr();
244          $sth->execute(@sql_args) || confess "execute: $sql; ".$self->{dbh}->errstr();          $sth->execute(@sql_args) || confess "execute: $sql; ".join("|",@sql_args)." ".$self->{dbh}->errstr();
245          my @arr;          my @arr;
246          while (my $row = $sth->fetchrow_hashref) {          while (my $row = $sth->fetchrow_hashref) {
247                  $row->{item} = HTML::Entities::encode($row->{item},' <>&"');                  $row->{item} = HTML::Entities::encode($row->{item},' <>&"');
# Line 257  sub close { Line 260  sub close {
260    
261          $self->{dbh}->begin_work || confess $self->{dbh}->errstr();          $self->{dbh}->begin_work || confess $self->{dbh}->errstr();
262    
263          $self->delete_and_create('index', qq{          $self->delete_and_create('data', qq{
264                  create table index (                  create table data (
265                          name varchar(255),                          name varchar(255),
266                          ord int,                          ord int,
267                          item text,                          item text,
# Line 282  sub close { Line 285  sub close {
285          $self->bench("got ".($#items+1)." items, now sorting");          $self->bench("got ".($#items+1)." items, now sorting");
286          @items = sort @items;          @items = sort @items;
287    
288          my $sql = "insert into index  (name,ord,item,display,count) values (?,?,?,?,?)";          my $sql = "insert into data (name,ord,item,display,count) values (?,?,?,?,?)";
289          my $sth_index = $self->{dbh}->prepare($sql) || confess "$sql: ".$self->{dbh}->errstr();          my $sth_index = $self->{dbh}->prepare($sql) || confess "$sql: ".$self->{dbh}->errstr();
290    
291          $sql = "insert into filters (filter, ord, count) values (?,?,?)";          $sql = "insert into filters (filter, ord, count) values (?,?,?)";

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

  ViewVC Help
Powered by ViewVC 1.1.26