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 $table = shift || croak "need table name!";
64 my $sql = shift || croak "need sql to create table!";
66 print STDERR "## delete_and_create($table)\n" if ($debug);
68 my $sql_delete = "delete from $table";
69 my $sth = $self->{dbh}->prepare($sql_delete);
71 if ($sth && $sth->execute()) {
72 print STDERR "## deleted rows from table $table\n" if ($debug);
74 # can't delete from table, assume it doesn't exists!
75 $self->{dbh}->rollback;
76 $self->{dbh}->do($sql) || confess "SQL: $sql ".$self->{dbh}->errstr();
77 print STDERR "## creating table $table\n" if ($debug);
78 $self->{dbh}->begin_work;
86 my $index_data = shift || print STDERR "\$index->insert($field,NULL,...)";
87 my $display = shift || $index_data;
91 print STDERR "\$index->insert() -- no value to insert\n";
95 $index_data =~ s#&(\w)(acute|cedil|circ|grave|ring|slash|tilde|uml);#$1#gi;
98 $index_data =~ s#^\s+##;
99 $index_data =~ s#\s+$##;
100 $index_data =~ s#\s\s+# #g;
102 my $uc = uc($index_data);
104 if (! $self->{c}->{$uc}->{$field}) {
105 #print stderr "in index: $index_data\n";
106 $self->{c}->{$uc}->{$field}->{item} = $index_data;
107 $self->{c}->{$uc}->{$field}->{display} = $display;
110 $self->{c}->{$uc}->{$field}->{count}++;
111 $self->{c}->{$uc}->{$field}->{filter}->{$filter}++ if ($filter);
122 my $tables_sql = 'data';
124 my @sql_args = ( $field, $where );
127 $tables_sql .= ",filters";
129 and data.ord = filters.ord
132 push @sql_args, $filter;
138 where name = ? and upper(item) like upper(?)||'%'
142 my $sth = $self->{dbh}->prepare($sql) || confess $self->{dbh}->errstr();
143 $sth->execute(@sql_args) || confess "sql: $sql; ".$self->{dbh}->errstr();
145 my ($total) = $sth->fetchrow_array();
147 # no results, count all
156 @sql_args = ( $field );
157 push @sql_args, $filter if ($filter);
159 my $sth = $self->{dbh}->prepare($sql) || confess $self->{dbh}->errstr();
160 $sth->execute(@sql_args) || confess "sql: $sql; ".$self->{dbh}->errstr();
161 $total = $sth->fetchrow_array();
165 return $total || '0';
175 my $offset = shift || 0;
176 my $rows = shift || 10;
181 my $tables_sql = 'data';
184 my @sql_args = ( $field, $where );
187 $tables_sql .= ",filters";
189 and data.ord = filters.ord
192 push @sql_args, $filter;
197 select data.ord as ord
199 where name = ? and upper(item) like upper(?)||'%'
202 my $sth = $self->{dbh}->prepare($sql2) || confess "sql2: $sql2; ".$self->{dbh}->errstr();
204 $sth->execute(@sql_args) || confess "sql2: $sql2; ".$self->{dbh}->errstr();
205 if (my $row = $sth->fetchrow_hashref) {
206 $from_ord += $row->{ord} - 1;
208 # if no match is found when searching from beginning
209 # of word in index, try substring match anywhere
211 select data.ord as ord
213 where name = ? and upper(item) like '% '||upper(?)||'%'
217 $sth = $self->{dbh}->prepare($sql2) || confess "sql2: $sql2; ".$self->{dbh}->errstr();
218 $sth->execute(@sql_args) || confess "sql2: $sql2; ".$self->{dbh}->errstr();
220 if (my $row = $sth->fetchrow_hashref) {
221 $from_ord += $row->{ord} - 1;
226 @sql_args = ( $field, $from_ord );
227 push @sql_args, $filter if ($filter);
230 select item,display,data.count as count
238 # fix SQLite problem which doesn't allow placeholders in limit and offset
239 # http://thread.gmane.org/gmane.comp.db.sqlite.general/9707
240 $sql .= "limit $rows offset $offset";
242 my $sth = $self->{dbh}->prepare($sql) || confess "prepare: $sql; ".$self->{dbh}->errstr();
243 $sth->execute(@sql_args) || confess "execute: $sql; ".join("|",@sql_args)." ".$self->{dbh}->errstr();
245 while (my $row = $sth->fetchrow_hashref) {
246 $row->{item} = HTML::Entities::encode($row->{item},' <>&"');
247 $row->{display} = HTML::Entities::encode($row->{display},'<>&"');
248 $row->{item} =~ s#&(\w)(acute|cedil|circ|grave|ring|slash|tilde|uml);#$1#gi;
249 $row->{display} =~ s#&(\w)(acute|cedil|circ|grave|ring|slash|tilde|uml);#&$1$2;#gi;
258 return if (! $self->{dbh});
260 $self->{dbh}->begin_work || confess $self->{dbh}->errstr();
262 $self->delete_and_create('data', qq{
269 primary key (name,ord)
273 $self->delete_and_create('filters', qq{
274 create table filters (
278 primary key (filter,ord)
282 $self->bench("getting all entries");
283 my @items = keys %{$self->{c}};
284 $self->bench("got ".($#items+1)." items, now sorting");
285 @items = sort @items;
287 my $sql = "insert into data (name,ord,item,display,count) values (?,?,?,?,?)";
288 my $sth_index = $self->{dbh}->prepare($sql) || confess "$sql: ".$self->{dbh}->errstr();
290 $sql = "insert into filters (filter, ord, count) values (?,?,?)";
291 my $sth_filter = $self->{dbh}->prepare($sql) || confess "$sql: ".$self->{dbh}->errstr();
294 foreach my $key (@items) {
296 foreach my $field (keys %{$self->{c}->{$key}}) {
301 $self->{c}->{$key}->{$field}->{item},
302 $self->{c}->{$key}->{$field}->{display},
303 $self->{c}->{$key}->{$field}->{count},
307 next unless ($self->{c}->{$key}->{$field}->{filter});
309 foreach my $filter (keys %{$self->{c}->{$key}->{$field}->{filter}}) {
310 $sth_filter->execute( $filter, $ord, $self->{c}->{$key}->{$field}->{filter}->{$filter} );
317 $self->{dbh}->commit || confess $self->{dbh}->errstr();
319 $self->bench("vacuuming");
321 if ($self->{dbd} =~ m/(Pg|SQLite)/) {
322 $self->{dbh}->do(qq{vacuum}) || carp "vacumming failed. It shouldn't if you are using PostgreSQL or SQLite: ".$self->{dbh}->errstr();
325 $self->bench("disconnecting from database");
327 $self->{dbh}->disconnect;
333 print STDERR "index_DBI fatal error: \$index->close() not called... $Count references left!\n" if ($Count > 0);
335 # print STDERR "usage\ttable\n";
336 # foreach (keys %Table) {
337 # print STDERR $Table{$_},"\t$_\n";