/[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 13 by dpavlin, Sun Feb 16 22:41:37 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 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 57  sub insert { Line 59  sub insert {
59          my $self = shift;          my $self = shift;
60    
61          my $field = shift;          my $field = shift;
62          my $index_data = shift;          my $index_data = shift || return;
63          my $ident = shift;      # e.g. Library ID          my $ident = shift || return;    # e.g. library id
64    
65          if (! $index_data) {          if (! $index_data) {
66                  print STDERR "\$index->insert() -- no value to insert\n";                  print STDERR "\$index->insert() -- no value to insert\n";
# Line 72  sub insert { Line 74  sub insert {
74    
75          my $sql = "select item from $field where upper(item)=upper(?)";          my $sql = "select item from $field where upper(item)=upper(?)";
76          my $sth = $self->{dbh}->prepare($sql) || die $self->{dbh}->errstr();          my $sth = $self->{dbh}->prepare($sql) || die $self->{dbh}->errstr();
77          $sth->execute($index_data) || die "SQL: $sql; ".$self->{dbh}->errstr();          $sth->execute($index_data) || die "sql: $sql; ".$self->{dbh}->errstr();
78          if (! $sth->fetchrow_hashref) {          if (! $sth->fetchrow_hashref) {
79                  my $sql = "insert into $field (item,ident,count) values (?,?,?)";                  my $sql = "insert into $field (item,ident,count) values (?,?,?)";
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,$ident,1) || die "SQL: $sql; ".$self->{dbh}->errstr();                  $sth->execute($index_data,$ident,1) || die "sql: $sql; ".$self->{dbh}->errstr();
82  #print STDERR "in index: $index_data\n";  #print stderr "in index: $index_data\n";
83          } else {          } else {
84                  my $sql = "update $field set count = count + 1 where item = ? and ident = ?";                  my $sql = "update $field set count = count + 1 where item = ? and ident = ?";
85                  my $sth = $self->{dbh}->prepare($sql) || die $self->{dbh}->errstr();                  my $sth = $self->{dbh}->prepare($sql) || die $self->{dbh}->errstr();
86                  $sth->execute($index_data,$ident) || die "SQL: $sql; ".$self->{dbh}->errstr();                  $sth->execute($index_data,$ident) || die "sql: $sql; ".$self->{dbh}->errstr();
87          }          }
88  }  }
89    
90    sub check {
91            my $self = shift;
92    
93            my $field = shift;
94    
95            my $sql = "select count(*) from $field";
96    
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    
106    sub fetch {
107            my $self = shift;
108    
109            my $field = shift;
110            my $what = shift || 'item';     # 'item,ident'
111            my $where = shift;
112    
113            my $from_ord = shift || 0;
114            my $rows = shift || 10;
115    
116            my @sql_args;
117    
118            my $sql = "select $what,ord from $field";
119    
120            if ($where) {
121                    my $sql2 = " select ord from $field where upper($what) like upper(?)||'%'";
122                    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} - 1;
127                    }
128            }
129            $sql .= " order by ord limit $rows offset $from_ord";
130    
131            my $sth = $self->{dbh}->prepare($sql) || die "prepare: $sql; ".$self->{dbh}->errstr();
132            $sth->execute() || die "execute: $sql; ".$self->{dbh}->errstr();
133            my @arr;
134            while (my $row = $sth->fetchrow_hashref) {
135                    $row->{item} = HTML::Entities::encode($row->{item});
136                    push @arr,$row;
137            }
138            return @arr;
139    }
140    
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          }          }
# Line 97  sub close { Line 181  sub close {
181    
182  END {  END {
183          $Count--;          $Count--;
184          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);
185          # FIX: debug output          # FIX: debug output
186          print STDERR "usage\ttable\n";  #       print STDERR "usage\ttable\n";
187          foreach (keys %Table) {  #       foreach (keys %Table) {
188                  print STDERR $Table{$_},"\t$_\n";  #               print STDERR $Table{$_},"\t$_\n";
189          }  #       }
190  }  }
191    
192  1;  1;

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

  ViewVC Help
Powered by ViewVC 1.1.26