2 # this file implements index functions using DBI
12 my %Table; # index tables which where visited in this run
20 $self->{dbh} = DBI->connect("DBI:Pg:dbname=webpac","dpavlin","") || die $DBI::errstr;
22 $self->{dbh}->begin_work || die $self->{dbh}->errstr();
29 sub delete_and_create {
34 #print "#### delete_and_create($field)\n";
38 my $sql = "select count(*) from $field";
39 my $sth = $self->{dbh}->prepare($sql) || die $self->{dbh}->errstr();
40 # FIX: this is not a good way to check if table exists!
41 if ($sth->execute() && $sth->fetchrow_hashref) {
42 my $sql = "drop table $field";
43 $self->{dbh}->begin_work;
44 my $sth = $self->{dbh}->do($sql) || die "SQL: $sql ".$self->{dbh}->errstr();
47 $sql = "create table $field (
52 primary key (item,ident)
55 $self->{dbh}->begin_work;
56 $sth = $self->{dbh}->do($sql); # || warn "SQL: $sql ".$self->{dbh}->errstr();
59 $self->{dbh}->begin_work;
66 my $index_data = shift || print STDERR "\$index->insert($field,NULL,...)";
67 my $ident = shift || ''; # e.g. library id
70 print STDERR "\$index->insert() -- no value to insert\n";
74 if (! $Table{$field}) {
75 $self->delete_and_create($field);
79 my $sql = "select item from $field where upper(item)=upper(?)";
80 my $sth = $self->{dbh}->prepare($sql) || die $self->{dbh}->errstr();
81 $sth->execute($index_data) || die "sql: $sql; ".$self->{dbh}->errstr();
82 if (! $sth->fetchrow_hashref) {
83 my $sql = "insert into $field (item,ident,count) values (?,?,?)";
84 my $sth = $self->{dbh}->prepare($sql) || die $self->{dbh}->errstr();
85 $index_data = substr($index_data,0,255);
86 $sth->execute($index_data,$ident,1) || die "sql: $sql; ".$self->{dbh}->errstr();
87 #print stderr "in index: $index_data\n";
89 my $sql = "update $field set count = count + 1 where item = ? and ident = ?";
90 my $sth = $self->{dbh}->prepare($sql) || die $self->{dbh}->errstr();
91 $sth->execute($index_data,$ident) || die "sql: $sql; ".$self->{dbh}->errstr();
100 my $sql = "select count(*) from $field";
102 my $sth = $self->{dbh}->prepare($sql) || die $self->{dbh}->errstr();
103 $sth->execute() || die "sql: $sql; ".$self->{dbh}->errstr();
105 my ($total) = $sth->fetchrow_array();
115 my $what = shift || 'item'; # 'item,ident'
118 my $from_ord = shift || 0;
119 my $rows = shift || 10;
123 my $sql = "select $what,ord from $field";
126 my $sql2 = " select ord from $field where upper($what) like upper(?)||'%'";
127 my $sth = $self->{dbh}->prepare($sql2) || die "sql2: $sql2; ".$self->{dbh}->errstr();
129 $sth->execute($where) || die "sql2: $sql2; ".$self->{dbh}->errstr();
130 if (my $row = $sth->fetchrow_hashref) {
131 $from_ord += $row->{ord} - 1;
134 $sql .= " order by ord limit $rows offset $from_ord";
136 my $sth = $self->{dbh}->prepare($sql) || die "prepare: $sql; ".$self->{dbh}->errstr();
137 $sth->execute() || die "execute: $sql; ".$self->{dbh}->errstr();
139 while (my $row = $sth->fetchrow_hashref) {
140 $row->{item} = HTML::Entities::encode($row->{item},'<>&"');
150 # re-create ord column (sorted order) in table
155 $self->{dbh}->begin_work || die $self->{dbh}->errstr();
157 my $sql = "select oid from $table order by upper(item)";
158 my $sth = $self->{dbh}->prepare($sql) || die "sql: $sql; ".$self->{dbh}->errstr();
159 $sql = "update $table set ord=? where oid=?";
160 my $sth_update = $self->{dbh}->prepare($sql) || die "sql: $sql; ".$self->{dbh}->errstr();
161 $sth->execute() || die "sql: $sql; ".$self->{dbh}->errstr();
163 while (my $row = $sth->fetchrow_hashref) {
164 $sth_update->execute($ord++,$row->{oid});
167 $self->{dbh}->commit || die $self->{dbh}->errstr();
174 $self->{dbh}->commit || die $self->{dbh}->errstr();
176 foreach my $table (keys %Table) {
178 print STDERR "creating ord for $table...\n";
182 $self->{dbh}->disconnect;
189 print STDERR "index_DBI fatal error: \$index->close() not called... $Count references left!\n" if ($Count > 0);
191 # print STDERR "usage\ttable\n";
192 # foreach (keys %Table) {
193 # print STDERR $Table{$_},"\t$_\n";