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

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

revision 643 by dpavlin, Sun Jan 23 15:18:03 2005 UTC revision 651 by dpavlin, Thu Jan 27 20:25:27 2005 UTC
# Line 60  sub new { Line 60  sub new {
60  sub delete_and_create {  sub delete_and_create {
61          my $self = shift;          my $self = shift;
62    
63          my $index = shift || croak "need index name!";          my $table = shift || croak "need table name!";
64          my $sql = shift || croak "need sql to create table!";          my $sql = shift || croak "need sql to create table!";
65    
66          print STDERR "## delete_and_create($index)\n" if ($debug);          print STDERR "## delete_and_create($table)\n" if ($debug);
67    
68          my $sql_delete = "delete from $index";          my $sql_delete = "delete from $table";
69          my $sth = $self->{dbh}->prepare($sql_delete) || confess "can't prepare: $sql_delete";          my $sth = $self->{dbh}->prepare($sql_delete);
70    
71          if ($sth->execute()) {          if ($sth && $sth->execute()) {
72                  print STDERR "## deleted rows from table $index\n" if ($debug);                  print STDERR "## deleted rows from table $table\n" if ($debug);
73          } else {          } else {
74                  # can't delete from table, assume it doesn't exists!                  # can't delete from table, assume it doesn't exists!
75                  $self->{dbh}->rollback;                  $self->{dbh}->rollback;
76                  $self->{dbh}->do($sql) || confess "SQL: $sql ".$self->{dbh}->errstr();                  $self->{dbh}->do($sql) || confess "SQL: $sql ".$self->{dbh}->errstr();
77                  print STDERR "## creating table $index\n" if ($debug);                  print STDERR "## creating table $table\n" if ($debug);
78                  $self->{dbh}->begin_work;                  $self->{dbh}->begin_work;
79          }          }
80  }  }
# Line 119  sub count { Line 119  sub count {
119    
120          my $filter = shift;          my $filter = shift;
121    
122          my $tables_sql = 'index';          my $tables_sql = 'data';
123          my $where_sql = '';          my $where_sql = '';
124          my @sql_args = ( $field, $where );          my @sql_args = ( $field, $where );
125    
126          if ($filter) {          if ($filter) {
127                  $tables_sql .= ",filters";                  $tables_sql .= ",filters";
128                  $where_sql .= "                  $where_sql .= "
129                          and index.ord = filters.ord                          and data.ord = filters.ord
130                          and filter = ?                          and filter = ?
131                  ";                  ";
132                  push @sql_args, $filter;                  push @sql_args, $filter;
# Line 149  sub count { Line 149  sub count {
149                  my $sql = qq{                  my $sql = qq{
150                          select count(*)                          select count(*)
151                          from $tables_sql                          from $tables_sql
152                          where index.name = ?                          where data.name = ?
153                          $where_sql                          $where_sql
154                  };                  };
155    
# Line 178  sub fetch { Line 178  sub fetch {
178    
179          my $from_ord = 0;          my $from_ord = 0;
180    
181          my $tables_sql = 'index';          my $tables_sql = 'data';
182          my $where_sql = '';          my $where_sql = '';
183    
184          my @sql_args = ( $field, $where );          my @sql_args = ( $field, $where );
# Line 186  sub fetch { Line 186  sub fetch {
186          if ($filter) {          if ($filter) {
187                  $tables_sql .= ",filters";                  $tables_sql .= ",filters";
188                  $where_sql .= "                  $where_sql .= "
189                          and index.ord = filters.ord                          and data.ord = filters.ord
190                          and filter = ?                          and filter = ?
191                  ";                  ";
192                  push @sql_args, $filter;                  push @sql_args, $filter;
# Line 194  sub fetch { Line 194  sub fetch {
194    
195          if ($where) {          if ($where) {
196                  my $sql2 = qq{                  my $sql2 = qq{
197                          select index.ord as ord                          select data.ord as ord
198                          from $tables_sql                          from $tables_sql
199                          where name = ? and upper(item) like upper(?)||'%'                          where name = ? and upper(item) like upper(?)||'%'
200                          $where_sql                          $where_sql
# Line 208  sub fetch { Line 208  sub fetch {
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 upper(item) like '% '||upper(?)||'%'
214                                  $where_sql                                  $where_sql
# Line 225  sub fetch { Line 225  sub fetch {
225    
226          @sql_args = ( $field, $from_ord );          @sql_args = ( $field, $from_ord );
227          push @sql_args, $filter if ($filter);          push @sql_args, $filter if ($filter);
         push @sql_args, ( $rows, $offset );  
228    
229          my $sql = qq{          my $sql = qq{
230                  select item,display,index.count as count                  select item,display,data.count as count
231                  from $tables_sql                  from $tables_sql
232                  where name = ?                  where name = ?
233                          and index.ord > ?                          and data.ord > ?
234                  $where_sql                  $where_sql
235                          order by index.ord                          order by data.ord
                         limit ? offset ?  
236          };          };
237    
238            # fix SQLite problem which doesn't allow placeholders in limit and offset
239            # http://thread.gmane.org/gmane.comp.db.sqlite.general/9707
240            $sql .= "limit $rows offset $offset";
241    
242          my $sth = $self->{dbh}->prepare($sql) || confess "prepare: $sql; ".$self->{dbh}->errstr();          my $sth = $self->{dbh}->prepare($sql) || confess "prepare: $sql; ".$self->{dbh}->errstr();
243          $sth->execute(@sql_args) || confess "execute: $sql; ".$self->{dbh}->errstr();          $sth->execute(@sql_args) || confess "execute: $sql; ".join("|",@sql_args)." ".$self->{dbh}->errstr();
244          my @arr;          my @arr;
245          while (my $row = $sth->fetchrow_hashref) {          while (my $row = $sth->fetchrow_hashref) {
246                  $row->{item} = HTML::Entities::encode($row->{item},' <>&"');                  $row->{item} = HTML::Entities::encode($row->{item},' <>&"');
# Line 257  sub close { Line 259  sub close {
259    
260          $self->{dbh}->begin_work || confess $self->{dbh}->errstr();          $self->{dbh}->begin_work || confess $self->{dbh}->errstr();
261    
262          $self->delete_and_create('index', qq{          $self->delete_and_create('data', qq{
263                  create table index (                  create table data (
264                          name varchar(255),                          name varchar(255),
265                          ord int,                          ord int,
266                          item text,                          item text,
# Line 282  sub close { Line 284  sub close {
284          $self->bench("got ".($#items+1)." items, now sorting");          $self->bench("got ".($#items+1)." items, now sorting");
285          @items = sort @items;          @items = sort @items;
286    
287          my $sql = "insert into index  (name,ord,item,display,count) values (?,?,?,?,?)";          my $sql = "insert into data (name,ord,item,display,count) values (?,?,?,?,?)";
288          my $sth_index = $self->{dbh}->prepare($sql) || confess "$sql: ".$self->{dbh}->errstr();          my $sth_index = $self->{dbh}->prepare($sql) || confess "$sql: ".$self->{dbh}->errstr();
289    
290          $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.651

  ViewVC Help
Powered by ViewVC 1.1.26