2 # this file implements index functions using DBI
3 # and huge amounts of memory for cache speedup
13 my %Table; # index tables which where visited in this run
14 my %sth_cache; # cache prepared statements
28 my $dbd = shift || die "need dbi_dbd= in [global] section of configuration file";
29 my $dsn = shift || die "need dbi_dsn= in [global] section of configuration file";
30 my $user = shift || die "need dbi_user= in [global] section of configuration file";
31 my $passwd = shift || die "need dbi_passwd= in [global] section of configuration file";
33 $self->{dbh} = DBI->connect("DBI:$dbd:$dsn",$user,$passwd) || die $DBI::errstr;
35 $self->{dbh}->begin_work || die $self->{dbh}->errstr();
42 sub delete_and_create {
47 #print "#### delete_and_create($field)\n";
51 my $sql = "select count(*) from $field";
52 my $sth = $self->{dbh}->prepare($sql) || die $self->{dbh}->errstr();
53 # FIX: this is not a good way to check if table exists!
54 if ($sth->execute() && $sth->fetchrow_hashref) {
55 my $sql = "drop table $field";
56 $self->{dbh}->begin_work;
57 my $sth = $self->{dbh}->do($sql) || die "SQL: $sql ".$self->{dbh}->errstr();
60 $sql = "create table $field (
65 primary key (item,ident)
68 $self->{dbh}->begin_work;
69 $sth = $self->{dbh}->do($sql); # || warn "SQL: $sql ".$self->{dbh}->errstr();
72 $self->{dbh}->begin_work;
79 my $index_data = shift || print STDERR "\$index->insert($field,NULL,...)";
80 my $ident = shift || ''; # e.g. library id
83 print STDERR "\$index->insert() -- no value to insert\n";
87 if (! $Table{$field}) {
88 $self->delete_and_create($field);
90 my $sql = "select item from $field where upper(item)=upper(?)";
91 $sth_cache{$field."select"} = $self->{dbh}->prepare($sql) || die $self->{dbh}->errstr();
93 $sql = "insert into $field (item,ident,count) values (?,?,?)";
94 $sth_cache{$field."insert"} = $self->{dbh}->prepare($sql) || die $self->{dbh}->errstr();
96 $sql = "update $field set count = count + 1 where item = ? and ident = ?";
97 $sth_cache{$field."update"} = $self->{dbh}->prepare($sql) || die $self->{dbh}->errstr();
101 #$sth_cache{$field."select"}->execute($index_data) || die "cache: $field select; ".$self->{dbh}->errstr();
102 $index_data = substr($index_data,0,255);
103 my $uc = uc($index_data);
104 if (! $c_table->{$field}->{$ident}->{$uc}) {
105 $sth_cache{$field."insert"}->execute($index_data,$ident,0) || warn "cache: $field insert ($index_data,$ident); ".$self->{dbh}->errstr();
106 #print stderr "in index: $index_data\n";
107 $c_table->{$field}->{$ident}->{$uc} = $index_data;
108 $c_count->{$field}->{$ident}->{$uc} = 1;
110 $c_count->{$field}->{$ident}->{$uc}++;
119 my $sql = "select count(*) from $field";
121 my $sth = $self->{dbh}->prepare($sql) || die $self->{dbh}->errstr();
122 $sth->execute() || die "sql: $sql; ".$self->{dbh}->errstr();
124 my ($total) = $sth->fetchrow_array();
134 my $what = shift || 'item'; # 'item,ident'
137 my $from_ord = shift || 0;
138 my $rows = shift || 10;
142 my $sql = "select $what,ord from $field";
145 my $sql2 = " select ord from $field where upper($what) like upper(?)||'%'";
146 my $sth = $self->{dbh}->prepare($sql2) || die "sql2: $sql2; ".$self->{dbh}->errstr();
148 $sth->execute($where) || die "sql2: $sql2; ".$self->{dbh}->errstr();
149 if (my $row = $sth->fetchrow_hashref) {
150 $from_ord += $row->{ord} - 1;
153 $sql .= " order by ord limit $rows offset $from_ord";
155 my $sth = $self->{dbh}->prepare($sql) || die "prepare: $sql; ".$self->{dbh}->errstr();
156 $sth->execute() || die "execute: $sql; ".$self->{dbh}->errstr();
158 while (my $row = $sth->fetchrow_hashref) {
159 $row->{item} = HTML::Entities::encode($row->{item},'<>&"');
169 # re-create ord column (sorted order) in table
174 $self->{dbh}->begin_work || die $self->{dbh}->errstr();
176 my $sql = "select oid from $table order by upper(item)";
177 my $sth = $self->{dbh}->prepare($sql) || die "sql: $sql; ".$self->{dbh}->errstr();
178 $sql = "update $table set ord=? where oid=?";
179 my $sth_update = $self->{dbh}->prepare($sql) || die "sql: $sql; ".$self->{dbh}->errstr();
180 $sth->execute() || die "sql: $sql; ".$self->{dbh}->errstr();
182 while (my $row = $sth->fetchrow_hashref) {
183 $sth_update->execute($ord++,$row->{oid});
186 $self->{dbh}->commit || die $self->{dbh}->errstr();
193 $self->{dbh}->commit || die $self->{dbh}->errstr();
195 foreach my $table (keys %Table) {
197 print STDERR "last operation took ",time()-$t," seconds...\n";
199 print STDERR "creating ord for $table...\n";
201 undef $sth_cache{$table."select"};
202 undef $sth_cache{$table."insert"};
203 undef $sth_cache{$table."update"};
205 # $sth_cache{$field."update"}->execute($index_data,$ident) || die "cache: $field update; ".$self->{dbh}->errstr();
208 $self->{dbh}->disconnect;
215 print STDERR "index_DBI fatal error: \$index->close() not called... $Count references left!\n" if ($Count > 0);
217 # print STDERR "usage\ttable\n";
218 # foreach (keys %Table) {
219 # print STDERR $Table{$_},"\t$_\n";