/[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 19 by dpavlin, Sat Feb 22 23:47:15 2003 UTC revision 50 by dpavlin, Sun Jun 1 13:46:42 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;
17          my $self = {};          my $self = {};
18          bless($self, $class);          bless($self, $class);
19    
20          # FIX: config params          my $dbd = shift || die "need dbi_dbd= in [global] section of configuration file";
21          $self->{dbh} = DBI->connect("DBI:Pg:dbname=webpac","dpavlin","") || die $DBI::errstr;          my $dsn = shift || die "need dbi_dsn= in [global] section of configuration file";
22            my $user = shift || die "need dbi_user= in [global] section of configuration file";
23            my $passwd = shift || die "need dbi_passwd= in [global] section of configuration file";
24    
25            $self->{dbh} = DBI->connect("DBI:$dbd:$dsn",$user,$passwd) || die $DBI::errstr;
26          # begin transaction          # begin transaction
27          $self->{dbh}->begin_work || die $self->{dbh}->errstr();          $self->{dbh}->begin_work || die $self->{dbh}->errstr();
28    
# Line 73  sub insert { Line 78  sub insert {
78    
79          if (! $Table{$field}) {          if (! $Table{$field}) {
80                  $self->delete_and_create($field);                  $self->delete_and_create($field);
81    
82                    my $sql = "select item from $field where upper(item)=upper(?)";
83                    $sth_cache{$field."select"} = $self->{dbh}->prepare($sql) || die $self->{dbh}->errstr();
84    
85                    $sql = "insert into $field (item,ident,count) values (?,?,?)";
86                    $sth_cache{$field."insert"} = $self->{dbh}->prepare($sql) || die $self->{dbh}->errstr();
87    
88                    $sql = "update $field set count = count + 1 where item = ? and ident = ?";
89                    $sth_cache{$field."update"} = $self->{dbh}->prepare($sql) || die $self->{dbh}->errstr();
90          }          }
91          $Table{$field}++;          $Table{$field}++;
92    
93          my $sql = "select item from $field where upper(item)=upper(?)";          $sth_cache{$field."select"}->execute($index_data) || die "cache: $field select; ".$self->{dbh}->errstr();
94          my $sth = $self->{dbh}->prepare($sql) || die $self->{dbh}->errstr();          if (! $sth_cache{$field."select"}->fetchrow_hashref) {
95          $sth->execute($index_data) || die "sql: $sql; ".$self->{dbh}->errstr();                  $index_data = substr($index_data,0,255);
96          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();  
97  #print stderr "in index: $index_data\n";  #print stderr "in index: $index_data\n";
98          } else {          } else {
99                  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();  
100          }          }
101  }  }
102    
# Line 136  sub fetch { Line 145  sub fetch {
145          $sth->execute() || die "execute: $sql; ".$self->{dbh}->errstr();          $sth->execute() || die "execute: $sql; ".$self->{dbh}->errstr();
146          my @arr;          my @arr;
147          while (my $row = $sth->fetchrow_hashref) {          while (my $row = $sth->fetchrow_hashref) {
148                  $row->{item} = HTML::Entities::encode($row->{item});                  $row->{item} = HTML::Entities::encode($row->{item},'<>&"');
149                  push @arr,$row;                  push @arr,$row;
150          }          }
151          return @arr;          return @arr;
# Line 153  sub close { Line 162  sub close {
162    
163                  $self->{dbh}->begin_work || die $self->{dbh}->errstr();                  $self->{dbh}->begin_work || die $self->{dbh}->errstr();
164    
165                  my $sql = "select oid from $table order by item";                  my $sql = "select oid from $table order by upper(item)";
166                  my $sth = $self->{dbh}->prepare($sql) || die "sql: $sql; ".$self->{dbh}->errstr();                  my $sth = $self->{dbh}->prepare($sql) || die "sql: $sql; ".$self->{dbh}->errstr();
167                  $sql = "update $table set ord=? where oid=?";                  $sql = "update $table set ord=? where oid=?";
168                  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();
# Line 176  sub close { Line 185  sub close {
185  # FIX  # FIX
186  print STDERR "creating ord for $table...\n";  print STDERR "creating ord for $table...\n";
187                          create_ord($table);                          create_ord($table);
188                            undef $sth_cache{$table."select"};
189                            undef $sth_cache{$table."insert"};
190                            undef $sth_cache{$table."update"};
191                  }                  }
192    
193                  $self->{dbh}->disconnect;                  $self->{dbh}->disconnect;

Legend:
Removed from v.19  
changed lines
  Added in v.50

  ViewVC Help
Powered by ViewVC 1.1.26