/[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 19 by dpavlin, Sat Feb 22 23:47:15 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 30  sub delete_and_create { Line 31  sub delete_and_create {
31    
32          my $field = shift;          my $field = shift;
33    
34    #print "#### delete_and_create($field)\n";
35    
36          $self->{dbh}->commit;          $self->{dbh}->commit;
         $self->{dbh}->begin_work;  
37    
38          my $sql = "select count(*) from $field";          my $sql = "select count(*) from $field";
39          my $sth = $self->{dbh}->prepare($sql) || die $self->{dbh}->errstr();          my $sth = $self->{dbh}->prepare($sql) || die $self->{dbh}->errstr();
40  # FIX: this is not a good way to check if table exists!  # FIX: this is not a good way to check if table exists!
41          if (1 || $sth->execute() && $sth->fetchrow_hashref) {          if ($sth->execute() && $sth->fetchrow_hashref) {
42                  my $sql = "drop table $field";                  my $sql = "drop table $field";
43  #               my $sth = $self->{dbh}->do($sql) || die "SQL: $sql ".$self->{dbh}->errstr();                  $self->{dbh}->begin_work;
44                    my $sth = $self->{dbh}->do($sql) || die "SQL: $sql ".$self->{dbh}->errstr();
45                    $self->{dbh}->commit;
46          }          }
47          $sql = "create table $field (          $sql = "create table $field (
48                          item varchar(255),                          item varchar(255),
49                          ident varchar(255),                          ident varchar(255),
50                          count int,                          count int,
51                            ord int,
52                          primary key (item,ident)                          primary key (item,ident)
53                  )";                  )";
 #       $sth = $self->{dbh}->do($sql) || die "SQL: $sql ".$self->{dbh}->errstr();  
         $sth = $self->{dbh}->do($sql) || warn "SQL: $sql ".$self->{dbh}->errstr();  
54    
55            $self->{dbh}->begin_work;
56            $sth = $self->{dbh}->do($sql); # || warn "SQL: $sql ".$self->{dbh}->errstr();
57          $self->{dbh}->commit;          $self->{dbh}->commit;
58    
59          $self->{dbh}->begin_work;          $self->{dbh}->begin_work;
60  }  }
61    
# Line 57  sub insert { Line 63  sub insert {
63          my $self = shift;          my $self = shift;
64    
65          my $field = shift;          my $field = shift;
66          my $index_data = shift;          my $index_data = shift || print STDERR "\$index->insert($field,NULL,...)";
67          my $ident = shift;      # e.g. Library ID          my $ident = shift || '';        # e.g. library id
68    
69          if (! $index_data) {          if (! $index_data) {
70                  print STDERR "\$index->insert() -- no value to insert\n";                  print STDERR "\$index->insert() -- no value to insert\n";
# Line 72  sub insert { Line 78  sub insert {
78    
79          my $sql = "select item from $field where upper(item)=upper(?)";          my $sql = "select item from $field where upper(item)=upper(?)";
80          my $sth = $self->{dbh}->prepare($sql) || die $self->{dbh}->errstr();          my $sth = $self->{dbh}->prepare($sql) || die $self->{dbh}->errstr();
81          $sth->execute($index_data) || die "SQL: $sql; ".$self->{dbh}->errstr();          $sth->execute($index_data) || die "sql: $sql; ".$self->{dbh}->errstr();
82          if (! $sth->fetchrow_hashref) {          if (! $sth->fetchrow_hashref) {
83                  my $sql = "insert into $field (item,ident,count) values (?,?,?)";                  my $sql = "insert into $field (item,ident,count) values (?,?,?)";
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,1) || die "SQL: $sql; ".$self->{dbh}->errstr();                  $sth->execute($index_data,$ident,1) || die "sql: $sql; ".$self->{dbh}->errstr();
86  #print STDERR "in index: $index_data\n";  #print stderr "in index: $index_data\n";
87          } else {          } else {
88                  my $sql = "update $field set count = count + 1 where item = ? and ident = ?";                  my $sql = "update $field set count = count + 1 where item = ? and ident = ?";
89                  my $sth = $self->{dbh}->prepare($sql) || die $self->{dbh}->errstr();                  my $sth = $self->{dbh}->prepare($sql) || die $self->{dbh}->errstr();
90                  $sth->execute($index_data,$ident) || die "SQL: $sql; ".$self->{dbh}->errstr();                  $sth->execute($index_data,$ident) || die "sql: $sql; ".$self->{dbh}->errstr();
91          }          }
92  }  }
93    
94    sub check {
95            my $self = shift;
96    
97            my $field = shift;
98    
99            my $sql = "select count(*) from $field";
100    
101            my $sth = $self->{dbh}->prepare($sql) || die $self->{dbh}->errstr();
102            $sth->execute() || die "sql: $sql; ".$self->{dbh}->errstr();
103    
104            my ($total) = $sth->fetchrow_array();
105    
106            return $total;
107    }
108    
109    
110    sub fetch {
111            my $self = shift;
112    
113            my $field = shift;
114            my $what = shift || 'item';     # 'item,ident'
115            my $where = shift;
116    
117            my $from_ord = shift || 0;
118            my $rows = shift || 10;
119    
120            my @sql_args;
121    
122            my $sql = "select $what,ord from $field";
123    
124            if ($where) {
125                    my $sql2 = " select ord from $field where upper($what) like upper(?)||'%'";
126                    my $sth = $self->{dbh}->prepare($sql2) || die "sql2: $sql2; ".$self->{dbh}->errstr();
127    
128                    $sth->execute($where) || die "sql2: $sql2; ".$self->{dbh}->errstr();
129                    if (my $row = $sth->fetchrow_hashref) {
130                            $from_ord += $row->{ord} - 1;
131                    }
132            }
133            $sql .= " order by ord limit $rows offset $from_ord";
134    
135            my $sth = $self->{dbh}->prepare($sql) || die "prepare: $sql; ".$self->{dbh}->errstr();
136            $sth->execute() || die "execute: $sql; ".$self->{dbh}->errstr();
137            my @arr;
138            while (my $row = $sth->fetchrow_hashref) {
139                    $row->{item} = HTML::Entities::encode($row->{item});
140                    push @arr,$row;
141            }
142            return @arr;
143    }
144    
145  sub close {  sub close {
146          my $self = shift;          my $self = shift;
147    
148    
149            # re-create ord column (sorted order) in table
150            sub create_ord {
151    
152                    my $table = shift;
153    
154                    $self->{dbh}->begin_work || die $self->{dbh}->errstr();
155    
156                    my $sql = "select oid from $table order by item";
157                    my $sth = $self->{dbh}->prepare($sql) || die "sql: $sql; ".$self->{dbh}->errstr();
158                    $sql = "update $table set ord=? where oid=?";
159                    my $sth_update = $self->{dbh}->prepare($sql) || die "sql: $sql; ".$self->{dbh}->errstr();
160                    $sth->execute() || die "sql: $sql; ".$self->{dbh}->errstr();
161                    my $ord = 1;
162                    while (my $row = $sth->fetchrow_hashref) {
163                            $sth_update->execute($ord++,$row->{oid});
164                    }
165    
166                    $self->{dbh}->commit || die $self->{dbh}->errstr();
167            }
168            #--- end of sub
169    
170          if ($self->{dbh}) {          if ($self->{dbh}) {
171    
172                    # commit
173                  $self->{dbh}->commit || die $self->{dbh}->errstr();                  $self->{dbh}->commit || die $self->{dbh}->errstr();
174    
175                    foreach my $table (keys %Table) {
176    # FIX
177    print STDERR "creating ord for $table...\n";
178                            create_ord($table);
179                    }
180    
181                  $self->{dbh}->disconnect;                  $self->{dbh}->disconnect;
182                  undef $self->{dbh};                  undef $self->{dbh};
183          }          }
# Line 97  sub close { Line 185  sub close {
185    
186  END {  END {
187          $Count--;          $Count--;
188          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);
189          # FIX: debug output          # FIX: debug output
190          print STDERR "usage\ttable\n";  #       print STDERR "usage\ttable\n";
191          foreach (keys %Table) {  #       foreach (keys %Table) {
192                  print STDERR $Table{$_},"\t$_\n";  #               print STDERR $Table{$_},"\t$_\n";
193          }  #       }
194  }  }
195    
196  1;  1;

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

  ViewVC Help
Powered by ViewVC 1.1.26