/[webpac]/trunk2/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 /trunk2/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 12 by dpavlin, Wed Jan 22 22:27:19 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 44  sub delete_and_create { Line 45  sub delete_and_create {
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) || die "SQL: $sql ".$self->{dbh}->errstr();
# Line 91  sub check { Line 93  sub check {
93          my $field = shift;          my $field = shift;
94    
95          my $sql = "select count(*) from $field";          my $sql = "select count(*) from $field";
96          return $self->{dbh}->do($sql);  
97            my $sth = $self->{dbh}->prepare($sql) || die $self->{dbh}->errstr();
98            $sth->execute() || die "sql: $sql; ".$self->{dbh}->errstr();
99    
100            my ($total) = $sth->fetchrow_array();
101    
102            return $total;
103  }  }
104    
105    
# Line 102  sub fetch { Line 110  sub fetch {
110          my $what = shift || 'item';     # 'item,ident'          my $what = shift || 'item';     # 'item,ident'
111          my $where = shift;          my $where = shift;
112    
113            my $from_ord = shift || 0;
114            my $rows = shift || 10;
115    
116          my @sql_args;          my @sql_args;
117    
118          my $sql = "select $what from $field";          my $sql = "select $what from $field";
119    
120          if ($where) {          if ($where) {
121                  $sql .= " where upper(item) like upper(?)||'%'";                  my $sql2 = " select ord from $field where upper($what) like upper(?)||'%'";
122                  push @sql_args,$where;                  my $sth = $self->{dbh}->prepare($sql2) || die "sql2: $sql2; ".$self->{dbh}->errstr();
123    
124                    $sth->execute($where) || die "sql2: $sql2; ".$self->{dbh}->errstr();
125                    if (my $row = $sth->fetchrow_hashref) {
126                            $from_ord += $row->{ord};
127                    }
128          }          }
129          $sql .= " order by item";          $sql .= " order by ord limit $rows offset $from_ord";
130    
131          my $sth = $self->{dbh}->prepare($sql) || die "sql: $sql; ".$self->{dbh}->errstr();          my $sth = $self->{dbh}->prepare($sql) || die "prepare: $sql; ".$self->{dbh}->errstr();
132          $sth->execute(@sql_args) || die "sql: $sql; ".$self->{dbh}->errstr();          $sth->execute() || die "execute: $sql; ".$self->{dbh}->errstr();
133          my @arr;          my @arr;
134          while (my $row = $sth->fetchrow_hashref) {          while (my $row = $sth->fetchrow_hashref) {
135                    $row->{item} = HTML::Entities::encode($row->{item});
136                  push @arr,$row;                  push @arr,$row;
137          }          }
138          return @arr;          return @arr;
# Line 123  sub fetch { Line 141  sub fetch {
141  sub close {  sub close {
142          my $self = shift;          my $self = shift;
143    
144    
145            # re-create ord column (sorted order) in table
146            sub create_ord {
147    
148                    my $table = shift;
149    
150                    $self->{dbh}->begin_work || die $self->{dbh}->errstr();
151    
152                    my $sql = "select oid from $table order by item";
153                    my $sth = $self->{dbh}->prepare($sql) || die "sql: $sql; ".$self->{dbh}->errstr();
154                    $sql = "update $table set ord=? where oid=?";
155                    my $sth_update = $self->{dbh}->prepare($sql) || die "sql: $sql; ".$self->{dbh}->errstr();
156                    $sth->execute() || die "sql: $sql; ".$self->{dbh}->errstr();
157                    my $ord = 1;
158                    while (my $row = $sth->fetchrow_hashref) {
159                            $sth_update->execute($ord++,$row->{oid});
160                    }
161    
162                    $self->{dbh}->commit || die $self->{dbh}->errstr();
163            }
164            #--- end of sub
165    
166          if ($self->{dbh}) {          if ($self->{dbh}) {
167    
168                    # commit
169                  $self->{dbh}->commit || die $self->{dbh}->errstr();                  $self->{dbh}->commit || die $self->{dbh}->errstr();
170    
171                    foreach my $table (keys %Table) {
172    # FIX
173    print STDERR "creating ord for $table...\n";
174                            create_ord($table);
175                    }
176    
177                  $self->{dbh}->disconnect;                  $self->{dbh}->disconnect;
178                  undef $self->{dbh};                  undef $self->{dbh};
179          }          }

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

  ViewVC Help
Powered by ViewVC 1.1.26