2 # this file implements index functions using DBI
12 my %Table; # index tables which where visited in this run
13 my %sth_cache; # cache prepared statements
20 my $dbd = shift || die "need dbi_dbd= in [global] section of configuration file";
21 my $dsn = shift || die "need dbi_dsn= in [global] section of configuration file";
22 my $user = shift || die "need dbi_user= in [global] section of configuration file";
23 my $passwd = shift || die "need dbi_passwd= in [global] section of configuration file";
25 $self->{dbh} = DBI->connect("DBI:$dbd:$dsn",$user,$passwd) || die $DBI::errstr;
27 $self->{dbh}->begin_work || die $self->{dbh}->errstr();
34 sub delete_and_create {
39 #print "#### delete_and_create($field)\n";
43 my $sql = "select count(*) from $field";
44 my $sth = $self->{dbh}->prepare($sql) || die $self->{dbh}->errstr();
45 # FIX: this is not a good way to check if table exists!
46 if ($sth->execute() && $sth->fetchrow_hashref) {
47 my $sql = "drop table $field";
48 $self->{dbh}->begin_work;
49 my $sth = $self->{dbh}->do($sql) || die "SQL: $sql ".$self->{dbh}->errstr();
52 $sql = "create table $field (
57 primary key (item,ident)
60 $self->{dbh}->begin_work;
61 $sth = $self->{dbh}->do($sql); # || warn "SQL: $sql ".$self->{dbh}->errstr();
64 $self->{dbh}->begin_work;
71 my $index_data = shift || print STDERR "\$index->insert($field,NULL,...)";
72 my $ident = shift || ''; # e.g. library id
75 print STDERR "\$index->insert() -- no value to insert\n";
79 if (! $Table{$field}) {
80 $self->delete_and_create($field);
82 my $sql = "select item from $field where upper(item)=upper(?)";
83 $sth_cache{$field."select"} = $self->{dbh}->prepare($sql) || die $self->{dbh}->errstr();
85 $sql = "insert into $field (item,ident,count) values (?,?,?)";
86 $sth_cache{$field."insert"} = $self->{dbh}->prepare($sql) || die $self->{dbh}->errstr();
88 $sql = "update $field set count = count + 1 where item = ? and ident = ?";
89 $sth_cache{$field."update"} = $self->{dbh}->prepare($sql) || die $self->{dbh}->errstr();
93 $sth_cache{$field."select"}->execute($index_data) || die "cache: $field select; ".$self->{dbh}->errstr();
94 if (! $sth_cache{$field."select"}->fetchrow_hashref) {
95 $index_data = substr($index_data,0,255);
96 $sth_cache{$field."insert"}->execute($index_data,$ident,1) || die "cache: $field insert; ".$self->{dbh}->errstr();
97 #print stderr "in index: $index_data\n";
99 $sth_cache{$field."update"}->execute($index_data,$ident) || die "cache: $field update; ".$self->{dbh}->errstr();
108 my $sql = "select count(*) from $field";
110 my $sth = $self->{dbh}->prepare($sql) || die $self->{dbh}->errstr();
111 $sth->execute() || die "sql: $sql; ".$self->{dbh}->errstr();
113 my ($total) = $sth->fetchrow_array();
123 my $what = shift || 'item'; # 'item,ident'
126 my $from_ord = shift || 0;
127 my $rows = shift || 10;
131 my $sql = "select $what,ord from $field";
134 my $sql2 = " select ord from $field where upper($what) like upper(?)||'%'";
135 my $sth = $self->{dbh}->prepare($sql2) || die "sql2: $sql2; ".$self->{dbh}->errstr();
137 $sth->execute($where) || die "sql2: $sql2; ".$self->{dbh}->errstr();
138 if (my $row = $sth->fetchrow_hashref) {
139 $from_ord += $row->{ord} - 1;
142 $sql .= " order by ord limit $rows offset $from_ord";
144 my $sth = $self->{dbh}->prepare($sql) || die "prepare: $sql; ".$self->{dbh}->errstr();
145 $sth->execute() || die "execute: $sql; ".$self->{dbh}->errstr();
147 while (my $row = $sth->fetchrow_hashref) {
148 $row->{item} = HTML::Entities::encode($row->{item},'<>&"');
158 # re-create ord column (sorted order) in table
163 $self->{dbh}->begin_work || die $self->{dbh}->errstr();
165 my $sql = "select oid from $table order by upper(item)";
166 my $sth = $self->{dbh}->prepare($sql) || die "sql: $sql; ".$self->{dbh}->errstr();
167 $sql = "update $table set ord=? where oid=?";
168 my $sth_update = $self->{dbh}->prepare($sql) || die "sql: $sql; ".$self->{dbh}->errstr();
169 $sth->execute() || die "sql: $sql; ".$self->{dbh}->errstr();
171 while (my $row = $sth->fetchrow_hashref) {
172 $sth_update->execute($ord++,$row->{oid});
175 $self->{dbh}->commit || die $self->{dbh}->errstr();
182 $self->{dbh}->commit || die $self->{dbh}->errstr();
184 foreach my $table (keys %Table) {
186 print STDERR "creating ord for $table...\n";
188 undef $sth_cache{$table."select"};
189 undef $sth_cache{$table."insert"};
190 undef $sth_cache{$table."update"};
193 $self->{dbh}->disconnect;
200 print STDERR "index_DBI fatal error: \$index->close() not called... $Count references left!\n" if ($Count > 0);
202 # print STDERR "usage\ttable\n";
203 # foreach (keys %Table) {
204 # print STDERR $Table{$_},"\t$_\n";