2 # this file implements index functions using DBI
3 # and huge amounts of memory for cache speedup
5 # this version doesn't support ident (which sould be location in
6 # library). But, that functionality is not used anyway...
20 my $bench_time = time();
28 print STDERR "last operation took ",time()-$bench_time," seconds...\n";
30 print STDERR "$msg\n";
38 my $dbd = shift || die "need dbi_dbd= in [global] section of configuration file";
39 my $dsn = shift || die "need dbi_dsn= in [global] section of configuration file";
40 my $user = shift || die "need dbi_user= in [global] section of configuration file";
41 my $passwd = shift || die "need dbi_passwd= in [global] section of configuration file";
45 $self->{dbh} = DBI->connect("DBI:$dbd:$dsn",$user,$passwd) || die $DBI::errstr;
48 $self->bench("connected to $dbd as $user");
50 # force SQLite to support binary 0 in data (which shouldn't
51 # happend, but it did to me)
54 $self->{dbh}->{sqlite_handle_binary_nulls} = 1;
60 sub delete_and_create {
63 my $index = shift || croak "need index name!";
64 my $sql = shift || croak "need sql to create table!";
66 print STDERR "## delete_and_create($index)\n" if ($debug);
68 my $sql_delete = "delete from $index";
69 my $sth = $self->{dbh}->prepare($sql_delete) || confess "can't prepare: $sql_delete";
71 unless ($sth->execute()) {
72 # can't delete from table, assume it doesn't exists!
73 $self->{dbh}->rollback;
74 $self->{dbh}->do($sql) || confess "SQL: $sql ".$self->{dbh}->errstr();
75 print STDERR "## creating table $index\n" if ($debug);
76 $self->{dbh}->begin_work;
84 my $index_data = shift || print STDERR "\$index->insert($field,NULL,...)";
85 my $display = shift || $index_data;
89 print STDERR "\$index->insert() -- no value to insert\n";
93 $index_data =~ s#&(\w)(acute|cedil|circ|grave|ring|slash|tilde|uml);#$1#gi;
96 $index_data =~ s#^\s+##;
97 $index_data =~ s#\s+$##;
98 $index_data =~ s#\s\s+# #g;
100 my $uc = uc($index_data);
102 if (! $self->{c}->{$uc}->{$field}) {
103 #print stderr "in index: $index_data\n";
104 $self->{c}->{$uc}->{$field}->{item} = $index_data;
105 $self->{c}->{$uc}->{$field}->{display} = $display;
108 $self->{c}->{$uc}->{$field}->{count}++;
109 $self->{c}->{$uc}->{$field}->{tag}->{$tag}++ if ($tag);
118 my $sql = "select count(*) from index where name = ? and upper(item) like upper(?)||'%'";
120 my $sth = $self->{dbh}->prepare($sql) || die $self->{dbh}->errstr();
121 $sth->execute($field,$where) || die "sql: $sql; ".$self->{dbh}->errstr();
123 my ($total) = $sth->fetchrow_array();
125 # no results, count all
127 my $sql = "select count(*) from index wheere name = ?";
129 my $sth = $self->{dbh}->prepare($sql) || die $self->{dbh}->errstr();
130 $sth->execute($field) || die "sql: $sql; ".$self->{dbh}->errstr();
131 $total = $sth->fetchrow_array();
145 my $offset = shift || 0;
146 my $rows = shift || 10;
152 select item,display,count
161 my $sql2 = "select ord from index where name = ? and upper(item) like upper(?)||'%'";
162 my $sth = $self->{dbh}->prepare($sql2) || die "sql2: $sql2; ".$self->{dbh}->errstr();
164 $sth->execute($field, $where) || die "sql2: $sql2; ".$self->{dbh}->errstr();
165 if (my $row = $sth->fetchrow_hashref) {
166 $from_ord += $row->{ord} - 1;
168 # if no match is found when searching from beginning
169 # of word in index, try substring match anywhere
170 $sql2 = "select ord from index where name = ? and upper(item) like '% '||upper(?)||'%'";
171 $sth = $self->{dbh}->prepare($sql2) || die "sql2: $sql2; ".$self->{dbh}->errstr();
172 $sth->execute($field, $where) || die "sql2: $sql2; ".$self->{dbh}->errstr();
173 if (my $row = $sth->fetchrow_hashref) {
174 $from_ord += $row->{ord} - 1;
179 my $sth = $self->{dbh}->prepare($sql) || die "prepare: $sql; ".$self->{dbh}->errstr();
180 $sth->execute($field,$from_ord,$rows,$offset) || die "execute: $sql; ".$self->{dbh}->errstr();
182 while (my $row = $sth->fetchrow_hashref) {
183 $row->{item} = HTML::Entities::encode($row->{item},' <>&"');
184 $row->{display} = HTML::Entities::encode($row->{display},'<>&"');
185 $row->{item} =~ s#&(\w)(acute|cedil|circ|grave|ring|slash|tilde|uml);#$1#gi;
186 $row->{display} =~ s#&(\w)(acute|cedil|circ|grave|ring|slash|tilde|uml);#&$1$2;#gi;
195 return if (! $self->{dbh});
197 $self->{dbh}->begin_work || die $self->{dbh}->errstr();
199 $self->delete_and_create('index', qq{
206 primary key (name,ord)
210 $self->delete_and_create('tags', qq{
215 primary key (tag,ord)
219 $self->bench("getting all entries");
220 my @items = keys %{$self->{c}};
221 $self->bench("got ".($#items+1)." items, now sorting");
222 @items = sort @items;
224 my $sql = "insert into index (name,ord,item,display,count) values (?,?,?,?,?)";
225 my $sth_index = $self->{dbh}->prepare($sql) || confess "$sql: ".$self->{dbh}->errstr();
227 $sql = "insert into tags (tag, ord, count) values (?,?,?)";
228 my $sth_tag = $self->{dbh}->prepare($sql) || confess "$sql: ".$self->{dbh}->errstr();
231 foreach my $key (@items) {
233 foreach my $field (keys %{$self->{c}->{$key}}) {
238 $self->{c}->{$key}->{$field}->{item},
239 $self->{c}->{$key}->{$field}->{display},
240 $self->{c}->{$key}->{$field}->{count},
244 next unless ($self->{c}->{$key}->{$field}->{tag});
246 foreach my $tag (keys %{$self->{c}->{$key}->{$field}->{tag}}) {
247 $sth_tag->execute( $tag, $ord, $self->{c}->{$key}->{$field}->{tag}->{$tag} );
254 $self->{dbh}->commit || die $self->{dbh}->errstr();
256 $self->bench("vacuuming");
258 if ($self->{dbd} =~ m/(Pg|SQLite)/) {
259 $self->{dbh}->do(qq{vacuum}) || carp "vacumming failed. It shouldn't if you are using PostgreSQL or SQLite: ".$self->{dbh}->errstr();
262 $self->bench("disconnecting from database");
264 $self->{dbh}->disconnect;
270 print STDERR "index_DBI fatal error: \$index->close() not called... $Count references left!\n" if ($Count > 0);
272 # print STDERR "usage\ttable\n";
273 # foreach (keys %Table) {
274 # print STDERR $Table{$_},"\t$_\n";