/[webpac]/trunk/index_DBI.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.pm

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

revision 18 by dpavlin, Sat Feb 22 21:37:25 2003 UTC revision 45 by dpavlin, Sat Mar 22 23:40:14 2003 UTC
# Line 10  use HTML::Entities; Line 10  use HTML::Entities;
10  use DBI;  use DBI;
11    
12  my %Table;      # index tables which where visited in this run  my %Table;      # index tables which where visited in this run
13    my %sth_cache;  # cache prepared statements
14    
15  sub new {  sub new {
16          my $class = shift;          my $class = shift;
# Line 31  sub delete_and_create { Line 32  sub delete_and_create {
32    
33          my $field = shift;          my $field = shift;
34    
35    #print "#### delete_and_create($field)\n";
36    
37          $self->{dbh}->commit;          $self->{dbh}->commit;
         $self->{dbh}->begin_work;  
38    
39          my $sql = "select count(*) from $field";          my $sql = "select count(*) from $field";
40          my $sth = $self->{dbh}->prepare($sql) || die $self->{dbh}->errstr();          my $sth = $self->{dbh}->prepare($sql) || die $self->{dbh}->errstr();
41  # FIX: this is not a good way to check if table exists!  # FIX: this is not a good way to check if table exists!
42          if ($sth->execute() && $sth->fetchrow_hashref) {          if ($sth->execute() && $sth->fetchrow_hashref) {
43                  my $sql = "drop table $field";                  my $sql = "drop table $field";
44                    $self->{dbh}->begin_work;
45                  my $sth = $self->{dbh}->do($sql) || die "SQL: $sql ".$self->{dbh}->errstr();                  my $sth = $self->{dbh}->do($sql) || die "SQL: $sql ".$self->{dbh}->errstr();
46                    $self->{dbh}->commit;
47          }          }
48          $sql = "create table $field (          $sql = "create table $field (
49                          item varchar(255),                          item varchar(255),
# Line 48  sub delete_and_create { Line 52  sub delete_and_create {
52                          ord int,                          ord int,
53                          primary key (item,ident)                          primary key (item,ident)
54                  )";                  )";
         $sth = $self->{dbh}->do($sql); # || warn "SQL: $sql ".$self->{dbh}->errstr();  
55    
56            $self->{dbh}->begin_work;
57            $sth = $self->{dbh}->do($sql); # || warn "SQL: $sql ".$self->{dbh}->errstr();
58          $self->{dbh}->commit;          $self->{dbh}->commit;
59    
60          $self->{dbh}->begin_work;          $self->{dbh}->begin_work;
61  }  }
62    
# Line 68  sub insert { Line 74  sub insert {
74    
75          if (! $Table{$field}) {          if (! $Table{$field}) {
76                  $self->delete_and_create($field);                  $self->delete_and_create($field);
77    
78                    my $sql = "select item from $field where upper(item)=upper(?)";
79                    $sth_cache{$field."select"} = $self->{dbh}->prepare($sql) || die $self->{dbh}->errstr();
80    
81                    $sql = "insert into $field (item,ident,count) values (?,?,?)";
82                    $sth_cache{$field."insert"} = $self->{dbh}->prepare($sql) || die $self->{dbh}->errstr();
83    
84                    $sql = "update $field set count = count + 1 where item = ? and ident = ?";
85                    $sth_cache{$field."update"} = $self->{dbh}->prepare($sql) || die $self->{dbh}->errstr();
86          }          }
87          $Table{$field}++;          $Table{$field}++;
88    
89          my $sql = "select item from $field where upper(item)=upper(?)";          $sth_cache{$field."select"}->execute($index_data) || die "cache: $field select; ".$self->{dbh}->errstr();
90          my $sth = $self->{dbh}->prepare($sql) || die $self->{dbh}->errstr();          if (! $sth_cache{$field."select"}->fetchrow_hashref) {
91          $sth->execute($index_data) || die "sql: $sql; ".$self->{dbh}->errstr();                  $index_data = substr($index_data,0,255);
92          if (! $sth->fetchrow_hashref) {                  $sth_cache{$field."insert"}->execute($index_data,$ident,1) || die "cache: $field insert; ".$self->{dbh}->errstr();
                 my $sql = "insert into $field (item,ident,count) values (?,?,?)";  
                 my $sth = $self->{dbh}->prepare($sql) || die $self->{dbh}->errstr();  
                 $sth->execute($index_data,$ident,1) || die "sql: $sql; ".$self->{dbh}->errstr();  
93  #print stderr "in index: $index_data\n";  #print stderr "in index: $index_data\n";
94          } else {          } else {
95                  my $sql = "update $field set count = count + 1 where item = ? and ident = ?";                  $sth_cache{$field."update"}->execute($index_data,$ident) || die "cache: $field update; ".$self->{dbh}->errstr();
                 my $sth = $self->{dbh}->prepare($sql) || die $self->{dbh}->errstr();  
                 $sth->execute($index_data,$ident) || die "sql: $sql; ".$self->{dbh}->errstr();  
96          }          }
97  }  }
98    
# Line 131  sub fetch { Line 141  sub fetch {
141          $sth->execute() || die "execute: $sql; ".$self->{dbh}->errstr();          $sth->execute() || die "execute: $sql; ".$self->{dbh}->errstr();
142          my @arr;          my @arr;
143          while (my $row = $sth->fetchrow_hashref) {          while (my $row = $sth->fetchrow_hashref) {
144                  $row->{item} = HTML::Entities::encode($row->{item});                  $row->{item} = HTML::Entities::encode($row->{item},'<>&"');
145                  push @arr,$row;                  push @arr,$row;
146          }          }
147          return @arr;          return @arr;
# Line 148  sub close { Line 158  sub close {
158    
159                  $self->{dbh}->begin_work || die $self->{dbh}->errstr();                  $self->{dbh}->begin_work || die $self->{dbh}->errstr();
160    
161                  my $sql = "select oid from $table order by item";                  my $sql = "select oid from $table order by upper(item)";
162                  my $sth = $self->{dbh}->prepare($sql) || die "sql: $sql; ".$self->{dbh}->errstr();                  my $sth = $self->{dbh}->prepare($sql) || die "sql: $sql; ".$self->{dbh}->errstr();
163                  $sql = "update $table set ord=? where oid=?";                  $sql = "update $table set ord=? where oid=?";
164                  my $sth_update = $self->{dbh}->prepare($sql) || die "sql: $sql; ".$self->{dbh}->errstr();                  my $sth_update = $self->{dbh}->prepare($sql) || die "sql: $sql; ".$self->{dbh}->errstr();

Legend:
Removed from v.18  
changed lines
  Added in v.45

  ViewVC Help
Powered by ViewVC 1.1.26