/[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 18 by dpavlin, Sat Feb 22 21:37:25 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    
# Line 36  sub delete_and_create { Line 37  sub delete_and_create {
37          my $sql = "select count(*) from $field";          my $sql = "select count(*) from $field";
38          my $sth = $self->{dbh}->prepare($sql) || die $self->{dbh}->errstr();          my $sth = $self->{dbh}->prepare($sql) || die $self->{dbh}->errstr();
39  # FIX: this is not a good way to check if table exists!  # FIX: this is not a good way to check if table exists!
40          if (1 || $sth->execute() && $sth->fetchrow_hashref) {          if ($sth->execute() && $sth->fetchrow_hashref) {
41                  my $sql = "drop table $field";                  my $sql = "drop table $field";
42  #               my $sth = $self->{dbh}->do($sql) || die "SQL: $sql ".$self->{dbh}->errstr();                  my $sth = $self->{dbh}->do($sql) || die "SQL: $sql ".$self->{dbh}->errstr();
43          }          }
44          $sql = "create table $field (          $sql = "create table $field (
45                          item varchar(255),                          item varchar(255),
46                          ident varchar(255),                          ident varchar(255),
47                          count int,                          count int,
48                            ord int,
49                          primary key (item,ident)                          primary key (item,ident)
50                  )";                  )";
51  #       $sth = $self->{dbh}->do($sql) || die "SQL: $sql ".$self->{dbh}->errstr();          $sth = $self->{dbh}->do($sql); # || warn "SQL: $sql ".$self->{dbh}->errstr();
         $sth = $self->{dbh}->do($sql) || warn "SQL: $sql ".$self->{dbh}->errstr();  
52    
53          $self->{dbh}->commit;          $self->{dbh}->commit;
54          $self->{dbh}->begin_work;          $self->{dbh}->begin_work;
# Line 57  sub insert { Line 58  sub insert {
58          my $self = shift;          my $self = shift;
59    
60          my $field = shift;          my $field = shift;
61          my $index_data = shift;          my $index_data = shift || print STDERR "\$index->insert($field,NULL,...)";
62          my $ident = shift;      # e.g. library id          my $ident = shift || '';        # e.g. library id
63    
64          if (! $index_data) {          if (! $index_data) {
65                  print STDERR "\$index->insert() -- no value to insert\n";                  print STDERR "\$index->insert() -- no value to insert\n";
# Line 91  sub check { Line 92  sub check {
92          my $field = shift;          my $field = shift;
93    
94          my $sql = "select count(*) from $field";          my $sql = "select count(*) from $field";
95          return $self->{dbh}->do($sql);  
96            my $sth = $self->{dbh}->prepare($sql) || die $self->{dbh}->errstr();
97            $sth->execute() || die "sql: $sql; ".$self->{dbh}->errstr();
98    
99            my ($total) = $sth->fetchrow_array();
100    
101            return $total;
102  }  }
103    
104    
# Line 102  sub fetch { Line 109  sub fetch {
109          my $what = shift || 'item';     # 'item,ident'          my $what = shift || 'item';     # 'item,ident'
110          my $where = shift;          my $where = shift;
111    
112            my $from_ord = shift || 0;
113            my $rows = shift || 10;
114    
115          my @sql_args;          my @sql_args;
116    
117          my $sql = "select $what from $field";          my $sql = "select $what,ord from $field";
118    
119          if ($where) {          if ($where) {
120                  $sql .= " where upper(item) like upper(?)||'%'";                  my $sql2 = " select ord from $field where upper($what) like upper(?)||'%'";
121                  push @sql_args,$where;                  my $sth = $self->{dbh}->prepare($sql2) || die "sql2: $sql2; ".$self->{dbh}->errstr();
122    
123                    $sth->execute($where) || die "sql2: $sql2; ".$self->{dbh}->errstr();
124                    if (my $row = $sth->fetchrow_hashref) {
125                            $from_ord += $row->{ord} - 1;
126                    }
127          }          }
128          $sql .= " order by item";          $sql .= " order by ord limit $rows offset $from_ord";
129    
130          my $sth = $self->{dbh}->prepare($sql) || die "sql: $sql; ".$self->{dbh}->errstr();          my $sth = $self->{dbh}->prepare($sql) || die "prepare: $sql; ".$self->{dbh}->errstr();
131          $sth->execute(@sql_args) || die "sql: $sql; ".$self->{dbh}->errstr();          $sth->execute() || die "execute: $sql; ".$self->{dbh}->errstr();
132          my @arr;          my @arr;
133          while (my $row = $sth->fetchrow_hashref) {          while (my $row = $sth->fetchrow_hashref) {
134                    $row->{item} = HTML::Entities::encode($row->{item});
135                  push @arr,$row;                  push @arr,$row;
136          }          }
137          return @arr;          return @arr;
# Line 123  sub fetch { Line 140  sub fetch {
140  sub close {  sub close {
141          my $self = shift;          my $self = shift;
142    
143    
144            # re-create ord column (sorted order) in table
145            sub create_ord {
146    
147                    my $table = shift;
148    
149                    $self->{dbh}->begin_work || die $self->{dbh}->errstr();
150    
151                    my $sql = "select oid from $table order by item";
152                    my $sth = $self->{dbh}->prepare($sql) || die "sql: $sql; ".$self->{dbh}->errstr();
153                    $sql = "update $table set ord=? where oid=?";
154                    my $sth_update = $self->{dbh}->prepare($sql) || die "sql: $sql; ".$self->{dbh}->errstr();
155                    $sth->execute() || die "sql: $sql; ".$self->{dbh}->errstr();
156                    my $ord = 1;
157                    while (my $row = $sth->fetchrow_hashref) {
158                            $sth_update->execute($ord++,$row->{oid});
159                    }
160    
161                    $self->{dbh}->commit || die $self->{dbh}->errstr();
162            }
163            #--- end of sub
164    
165          if ($self->{dbh}) {          if ($self->{dbh}) {
166    
167                    # commit
168                  $self->{dbh}->commit || die $self->{dbh}->errstr();                  $self->{dbh}->commit || die $self->{dbh}->errstr();
169    
170                    foreach my $table (keys %Table) {
171    # FIX
172    print STDERR "creating ord for $table...\n";
173                            create_ord($table);
174                    }
175    
176                  $self->{dbh}->disconnect;                  $self->{dbh}->disconnect;
177                  undef $self->{dbh};                  undef $self->{dbh};
178          }          }

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

  ViewVC Help
Powered by ViewVC 1.1.26