/[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 11 by dpavlin, Wed Jan 22 20:24:32 2003 UTC revision 45 by dpavlin, Sat Mar 22 23:40:14 2003 UTC
# Line 5  Line 5 
5  package index_DBI;  package index_DBI;
6  use strict qw(vars);  use strict qw(vars);
7  use vars qw($Count);  use vars qw($Count);
8    use HTML::Entities;
9    
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 30  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 (1 || $sth->execute() && $sth->fetchrow_hashref) {          if ($sth->execute() && $sth->fetchrow_hashref) {
43                  my $sql = "drop table $field";                  my $sql = "drop table $field";
44  #               my $sth = $self->{dbh}->do($sql) || die "SQL: $sql ".$self->{dbh}->errstr();                  $self->{dbh}->begin_work;
45                    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),
50                          ident varchar(255),                          ident varchar(255),
51                          count int,                          count int,
52                            ord int,
53                          primary key (item,ident)                          primary key (item,ident)
54                  )";                  )";
 #       $sth = $self->{dbh}->do($sql) || die "SQL: $sql ".$self->{dbh}->errstr();  
         $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 57  sub insert { Line 64  sub insert {
64          my $self = shift;          my $self = shift;
65    
66          my $field = shift;          my $field = shift;
67          my $index_data = shift;          my $index_data = shift || print STDERR "\$index->insert($field,NULL,...)";
68          my $ident = shift;      # e.g. library id          my $ident = shift || '';        # e.g. library id
69    
70          if (! $index_data) {          if (! $index_data) {
71                  print STDERR "\$index->insert() -- no value to insert\n";                  print STDERR "\$index->insert() -- no value to insert\n";
# Line 67  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 91  sub check { Line 102  sub check {
102          my $field = shift;          my $field = shift;
103    
104          my $sql = "select count(*) from $field";          my $sql = "select count(*) from $field";
105          return $self->{dbh}->do($sql);  
106            my $sth = $self->{dbh}->prepare($sql) || die $self->{dbh}->errstr();
107            $sth->execute() || die "sql: $sql; ".$self->{dbh}->errstr();
108    
109            my ($total) = $sth->fetchrow_array();
110    
111            return $total;
112  }  }
113    
114    
# Line 102  sub fetch { Line 119  sub fetch {
119          my $what = shift || 'item';     # 'item,ident'          my $what = shift || 'item';     # 'item,ident'
120          my $where = shift;          my $where = shift;
121    
122            my $from_ord = shift || 0;
123            my $rows = shift || 10;
124    
125          my @sql_args;          my @sql_args;
126    
127          my $sql = "select $what from $field";          my $sql = "select $what,ord from $field";
128    
129          if ($where) {          if ($where) {
130                  $sql .= " where upper(item) like upper(?)||'%'";                  my $sql2 = " select ord from $field where upper($what) like upper(?)||'%'";
131                  push @sql_args,$where;                  my $sth = $self->{dbh}->prepare($sql2) || die "sql2: $sql2; ".$self->{dbh}->errstr();
132    
133                    $sth->execute($where) || die "sql2: $sql2; ".$self->{dbh}->errstr();
134                    if (my $row = $sth->fetchrow_hashref) {
135                            $from_ord += $row->{ord} - 1;
136                    }
137          }          }
138          $sql .= " order by item";          $sql .= " order by ord limit $rows offset $from_ord";
139    
140          my $sth = $self->{dbh}->prepare($sql) || die "sql: $sql; ".$self->{dbh}->errstr();          my $sth = $self->{dbh}->prepare($sql) || die "prepare: $sql; ".$self->{dbh}->errstr();
141          $sth->execute(@sql_args) || die "sql: $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},'<>&"');
145                  push @arr,$row;                  push @arr,$row;
146          }          }
147          return @arr;          return @arr;
# Line 123  sub fetch { Line 150  sub fetch {
150  sub close {  sub close {
151          my $self = shift;          my $self = shift;
152    
153    
154            # re-create ord column (sorted order) in table
155            sub create_ord {
156    
157                    my $table = shift;
158    
159                    $self->{dbh}->begin_work || die $self->{dbh}->errstr();
160    
161                    my $sql = "select oid from $table order by upper(item)";
162                    my $sth = $self->{dbh}->prepare($sql) || die "sql: $sql; ".$self->{dbh}->errstr();
163                    $sql = "update $table set ord=? where oid=?";
164                    my $sth_update = $self->{dbh}->prepare($sql) || die "sql: $sql; ".$self->{dbh}->errstr();
165                    $sth->execute() || die "sql: $sql; ".$self->{dbh}->errstr();
166                    my $ord = 1;
167                    while (my $row = $sth->fetchrow_hashref) {
168                            $sth_update->execute($ord++,$row->{oid});
169                    }
170    
171                    $self->{dbh}->commit || die $self->{dbh}->errstr();
172            }
173            #--- end of sub
174    
175          if ($self->{dbh}) {          if ($self->{dbh}) {
176    
177                    # commit
178                  $self->{dbh}->commit || die $self->{dbh}->errstr();                  $self->{dbh}->commit || die $self->{dbh}->errstr();
179    
180                    foreach my $table (keys %Table) {
181    # FIX
182    print STDERR "creating ord for $table...\n";
183                            create_ord($table);
184                    }
185    
186                  $self->{dbh}->disconnect;                  $self->{dbh}->disconnect;
187                  undef $self->{dbh};                  undef $self->{dbh};
188          }          }

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

  ViewVC Help
Powered by ViewVC 1.1.26