2 # this file implements index functions using DBI
11 my %Table; # index tables which where visited in this run
19 $self->{dbh} = DBI->connect("DBI:Pg:dbname=webpac","dpavlin","") || die $DBI::errstr;
21 $self->{dbh}->begin_work || die $self->{dbh}->errstr();
28 sub delete_and_create {
34 $self->{dbh}->begin_work;
36 my $sql = "select count(*) from $field";
37 my $sth = $self->{dbh}->prepare($sql) || die $self->{dbh}->errstr();
38 # FIX: this is not a good way to check if table exists!
39 if (1 || $sth->execute() && $sth->fetchrow_hashref) {
40 my $sql = "drop table $field";
41 # my $sth = $self->{dbh}->do($sql) || die "SQL: $sql ".$self->{dbh}->errstr();
43 $sql = "create table $field (
47 primary key (item,ident)
49 # $sth = $self->{dbh}->do($sql) || die "SQL: $sql ".$self->{dbh}->errstr();
50 $sth = $self->{dbh}->do($sql) || warn "SQL: $sql ".$self->{dbh}->errstr();
53 $self->{dbh}->begin_work;
60 my $index_data = shift;
61 my $ident = shift; # e.g. library id
64 print STDERR "\$index->insert() -- no value to insert\n";
68 if (! $Table{$field}) {
69 $self->delete_and_create($field);
73 my $sql = "select item from $field where upper(item)=upper(?)";
74 my $sth = $self->{dbh}->prepare($sql) || die $self->{dbh}->errstr();
75 $sth->execute($index_data) || die "sql: $sql; ".$self->{dbh}->errstr();
76 if (! $sth->fetchrow_hashref) {
77 my $sql = "insert into $field (item,ident,count) values (?,?,?)";
78 my $sth = $self->{dbh}->prepare($sql) || die $self->{dbh}->errstr();
79 $sth->execute($index_data,$ident,1) || die "sql: $sql; ".$self->{dbh}->errstr();
80 #print stderr "in index: $index_data\n";
82 my $sql = "update $field set count = count + 1 where item = ? and ident = ?";
83 my $sth = $self->{dbh}->prepare($sql) || die $self->{dbh}->errstr();
84 $sth->execute($index_data,$ident) || die "sql: $sql; ".$self->{dbh}->errstr();
93 my $sql = "select count(*) from $field";
94 return $self->{dbh}->do($sql);
102 my $what = shift || 'item'; # 'item,ident'
107 my $sql = "select $what from $field";
109 $sql .= " where upper(item) like upper(?)||'%'";
110 push @sql_args,$where;
112 $sql .= " order by item";
114 my $sth = $self->{dbh}->prepare($sql) || die "sql: $sql; ".$self->{dbh}->errstr();
115 $sth->execute(@sql_args) || die "sql: $sql; ".$self->{dbh}->errstr();
117 while (my $row = $sth->fetchrow_hashref) {
127 $self->{dbh}->commit || die $self->{dbh}->errstr();
128 $self->{dbh}->disconnect;
135 print STDERR "index_DBI fatal error: \$index->close() not called... $Count references left!\n" if ($Count > 0);
137 # print STDERR "usage\ttable\n";
138 # foreach (keys %Table) {
139 # print STDERR $Table{$_},"\t$_\n";