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...
19 my $bench_time = time();
27 print STDERR "last operation took ",time()-$bench_time," seconds...\n";
29 print STDERR "$msg\n";
37 my $dbd = shift || die "need dbi_dbd= in [global] section of configuration file";
38 my $dsn = shift || die "need dbi_dsn= in [global] section of configuration file";
39 my $user = shift || die "need dbi_user= in [global] section of configuration file";
40 my $passwd = shift || die "need dbi_passwd= in [global] section of configuration file";
44 $self->{dbh} = DBI->connect("DBI:$dbd:$dsn",$user,$passwd) || die $DBI::errstr;
47 $self->bench("connected to $dbd as $user");
49 # force SQLite to support binary 0 in data (which shouldn't
50 # happend, but it did to me)
53 $self->{dbh}->{sqlite_handle_binary_nulls} = 1;
59 sub delete_and_create {
62 my $table = shift || croak "need table name!";
63 my $sql = shift || croak "need sql to create table!";
65 print STDERR "## delete_and_create($table)\n" if ($debug);
67 my $sql_delete = "delete from $table";
68 my $sth = $self->{dbh}->prepare($sql_delete);
70 if ($sth && $sth->execute()) {
71 print STDERR "## deleted rows from table $table\n" if ($debug);
73 # can't delete from table, assume it doesn't exists!
74 $self->{dbh}->rollback;
75 $self->{dbh}->do($sql) || confess "SQL: $sql ".$self->{dbh}->errstr();
76 print STDERR "## creating table $table\n" if ($debug);
77 $self->{dbh}->begin_work;
85 my $index_data = shift || print STDERR "\$index->insert($field,NULL,...)";
86 my $display = shift || $index_data;
90 print STDERR "\$index->insert() -- no value to insert\n";
94 $index_data =~ s#&(\w)(acute|cedil|circ|grave|ring|slash|tilde|uml);#$1#gi;
97 $index_data =~ s#^\s+##;
98 $index_data =~ s#\s+$##;
99 $index_data =~ s#\s\s+# #g;
101 my $uc = uc($index_data);
103 if (! $self->{c}->{$uc}->{$field}) {
104 #print stderr "in index: $index_data\n";
105 $self->{c}->{$uc}->{$field}->{item} = lc($index_data);
106 $self->{c}->{$uc}->{$field}->{display} = $display;
109 $self->{c}->{$uc}->{$field}->{count}++;
110 $self->{c}->{$uc}->{$field}->{filter}->{$filter}++ if ($filter);
121 my $tables_sql = 'data';
123 my @sql_args = ( $field, lc($where) );
126 $tables_sql .= ",filters";
128 and data.ord = filters.ord
131 push @sql_args, $filter;
137 where name = ? and item like ?||'%'
141 my $sth = $self->{dbh}->prepare($sql) || confess $self->{dbh}->errstr();
142 $sth->execute(@sql_args) || confess "sql: $sql; ".$self->{dbh}->errstr();
144 my ($total) = $sth->fetchrow_array();
146 # no results, count all
155 @sql_args = ( $field );
156 push @sql_args, $filter if ($filter);
158 my $sth = $self->{dbh}->prepare($sql) || confess $self->{dbh}->errstr();
159 $sth->execute(@sql_args) || confess "sql: $sql; ".$self->{dbh}->errstr();
160 $total = $sth->fetchrow_array();
164 return $total || '0';
174 my $offset = shift || 0;
175 my $rows = shift || 10;
180 my $tables_sql = 'data';
183 my @sql_args = ( $field, lc($where) );
186 $tables_sql .= ",filters";
188 and data.ord = filters.ord
191 push @sql_args, $filter;
196 select data.ord as ord
198 where name = ? and item like ?||'%'
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 item like '%'||?||'%'
218 $sth = $self->{dbh}->prepare($sql2) || confess "sql2: $sql2; ".$self->{dbh}->errstr();
219 $sth->execute(@sql_args) || confess "sql2: $sql2; ".$self->{dbh}->errstr();
221 if (my $row = $sth->fetchrow_hashref) {
222 $from_ord = $row->{ord} - 1;
227 @sql_args = ( $field, $from_ord );
228 push @sql_args, $filter if ($filter);
231 select item,display,data.count as count
239 # fix SQLite problem which doesn't allow placeholders in limit and offset
240 # http://thread.gmane.org/gmane.comp.db.sqlite.general/9707
241 $sql .= "limit $rows offset $offset";
243 my $sth = $self->{dbh}->prepare($sql) || confess "prepare: $sql; ".$self->{dbh}->errstr();
244 $sth->execute(@sql_args) || confess "execute: $sql; ".join("|",@sql_args)." ".$self->{dbh}->errstr();
246 while (my $row = $sth->fetchrow_hashref) {
247 $row->{item} = HTML::Entities::encode($row->{item},' <>&"');
248 $row->{display} = HTML::Entities::encode($row->{display},'<>&"');
249 $row->{item} =~ s#&(\w)(acute|cedil|circ|grave|ring|slash|tilde|uml);#$1#gi;
250 $row->{display} =~ s#&(\w)(acute|cedil|circ|grave|ring|slash|tilde|uml);#&$1$2;#gi;
259 return if (! $self->{dbh});
261 $self->{dbh}->begin_work || confess $self->{dbh}->errstr();
263 $self->delete_and_create('data', qq{
270 primary key (name,ord)
274 $self->delete_and_create('filters', qq{
275 create table filters (
279 primary key (filter,ord)
283 $self->bench("getting all entries");
284 my @items = keys %{$self->{c}};
285 $self->bench("got ".($#items+1)." items, now sorting");
286 @items = sort @items;
288 my $sql = "insert into data (name,ord,item,display,count) values (?,?,?,?,?)";
289 my $sth_index = $self->{dbh}->prepare($sql) || confess "$sql: ".$self->{dbh}->errstr();
291 $sql = "insert into filters (filter, ord, count) values (?,?,?)";
292 my $sth_filter = $self->{dbh}->prepare($sql) || confess "$sql: ".$self->{dbh}->errstr();
295 foreach my $key (@items) {
297 foreach my $field (keys %{$self->{c}->{$key}}) {
302 $self->{c}->{$key}->{$field}->{item},
303 $self->{c}->{$key}->{$field}->{display},
304 $self->{c}->{$key}->{$field}->{count},
308 next unless ($self->{c}->{$key}->{$field}->{filter});
310 foreach my $filter (keys %{$self->{c}->{$key}->{$field}->{filter}}) {
311 $sth_filter->execute( $filter, $ord, $self->{c}->{$key}->{$field}->{filter}->{$filter} );
318 $self->{dbh}->commit || confess $self->{dbh}->errstr();
320 $self->bench("vacuuming");
322 if ($self->{dbd} =~ m/(Pg|SQLite)/) {
323 $self->{dbh}->do(qq{vacuum}) || carp "vacumming failed. It shouldn't if you are using PostgreSQL or SQLite: ".$self->{dbh}->errstr();
326 $self->bench("disconnecting from database");
328 $self->{dbh}->disconnect;
334 print STDERR "index_DBI fatal error: \$index->close() not called... $Count references left!\n" if ($Count > 0);
336 # print STDERR "usage\ttable\n";
337 # foreach (keys %Table) {
338 # print STDERR $Table{$_},"\t$_\n";