/[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 10 by dpavlin, Thu Jan 16 17:35:54 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 16  sub new { Line 17  sub new {
17          bless($self, $class);          bless($self, $class);
18    
19          # FIX: config params          # FIX: config params
20          $self->{dbh} = DBI->connect("DBI:Pg:dbname=webpac","","") || die $DBI::errstr;          $self->{dbh} = DBI->connect("DBI:Pg:dbname=webpac","dpavlin","") || die $DBI::errstr;
21          # begin transaction          # begin transaction
22          $self->{dbh}->begin_work || die $self->{dbh}->errstr();          $self->{dbh}->begin_work || die $self->{dbh}->errstr();
23    
# 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 72  sub insert { Line 73  sub insert {
73    
74          my $sql = "select item from $field where upper(item)=upper(?)";          my $sql = "select item from $field where upper(item)=upper(?)";
75          my $sth = $self->{dbh}->prepare($sql) || die $self->{dbh}->errstr();          my $sth = $self->{dbh}->prepare($sql) || die $self->{dbh}->errstr();
76          $sth->execute($index_data) || die "SQL: $sql; ".$self->{dbh}->errstr();          $sth->execute($index_data) || die "sql: $sql; ".$self->{dbh}->errstr();
77          if (! $sth->fetchrow_hashref) {          if (! $sth->fetchrow_hashref) {
78                  my $sql = "insert into $field (item,ident,count) values (?,?,?)";                  my $sql = "insert into $field (item,ident,count) values (?,?,?)";
79                  my $sth = $self->{dbh}->prepare($sql) || die $self->{dbh}->errstr();                  my $sth = $self->{dbh}->prepare($sql) || die $self->{dbh}->errstr();
80                  $sth->execute($index_data,$ident,1) || die "SQL: $sql; ".$self->{dbh}->errstr();                  $sth->execute($index_data,$ident,1) || die "sql: $sql; ".$self->{dbh}->errstr();
81  #print STDERR "in index: $index_data\n";  #print stderr "in index: $index_data\n";
82          } else {          } else {
83                  my $sql = "update $field set count = count + 1 where item = ? and ident = ?";                  my $sql = "update $field set count = count + 1 where item = ? and ident = ?";
84                  my $sth = $self->{dbh}->prepare($sql) || die $self->{dbh}->errstr();                  my $sth = $self->{dbh}->prepare($sql) || die $self->{dbh}->errstr();
85                  $sth->execute($index_data,$ident) || die "SQL: $sql; ".$self->{dbh}->errstr();                  $sth->execute($index_data,$ident) || die "sql: $sql; ".$self->{dbh}->errstr();
86          }          }
87  }  }
88    
89    sub check {
90            my $self = shift;
91    
92            my $field = shift;
93    
94            my $sql = "select count(*) from $field";
95    
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    
105    sub fetch {
106            my $self = shift;
107    
108            my $field = shift;
109            my $what = shift || 'item';     # 'item,ident'
110            my $where = shift;
111    
112            my $from_ord = shift || 0;
113            my $rows = shift || 10;
114    
115            my @sql_args;
116    
117            my $sql = "select $what,ord from $field";
118    
119            if ($where) {
120                    my $sql2 = " select ord from $field where upper($what) like upper(?)||'%'";
121                    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 ord limit $rows offset $from_ord";
129    
130            my $sth = $self->{dbh}->prepare($sql) || die "prepare: $sql; ".$self->{dbh}->errstr();
131            $sth->execute() || die "execute: $sql; ".$self->{dbh}->errstr();
132            my @arr;
133            while (my $row = $sth->fetchrow_hashref) {
134                    $row->{item} = HTML::Entities::encode($row->{item});
135                    push @arr,$row;
136            }
137            return @arr;
138    }
139    
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          }          }
# Line 97  sub close { Line 180  sub close {
180    
181  END {  END {
182          $Count--;          $Count--;
183          print STDERR "index_DBI fatal error: \$index->close() not called... $Count references left!\n" if ($Count != 0);          print STDERR "index_DBI fatal error: \$index->close() not called... $Count references left!\n" if ($Count > 0);
184          # FIX: debug output          # FIX: debug output
185          print STDERR "usage\ttable\n";  #       print STDERR "usage\ttable\n";
186          foreach (keys %Table) {  #       foreach (keys %Table) {
187                  print STDERR $Table{$_},"\t$_\n";  #               print STDERR $Table{$_},"\t$_\n";
188          }  #       }
189  }  }
190    
191  1;  1;

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

  ViewVC Help
Powered by ViewVC 1.1.26