--- trunk/index_DBI.pm 2003/01/16 17:35:54 10 +++ trunk/index_DBI.pm 2003/01/22 20:24:32 11 @@ -16,7 +16,7 @@ bless($self, $class); # FIX: config params - $self->{dbh} = DBI->connect("DBI:Pg:dbname=webpac","","") || die $DBI::errstr; + $self->{dbh} = DBI->connect("DBI:Pg:dbname=webpac","dpavlin","") || die $DBI::errstr; # begin transaction $self->{dbh}->begin_work || die $self->{dbh}->errstr(); @@ -58,7 +58,7 @@ my $field = shift; my $index_data = shift; - my $ident = shift; # e.g. Library ID + my $ident = shift; # e.g. library id if (! $index_data) { print STDERR "\$index->insert() -- no value to insert\n"; @@ -72,19 +72,54 @@ my $sql = "select item from $field where upper(item)=upper(?)"; my $sth = $self->{dbh}->prepare($sql) || die $self->{dbh}->errstr(); - $sth->execute($index_data) || die "SQL: $sql; ".$self->{dbh}->errstr(); + $sth->execute($index_data) || die "sql: $sql; ".$self->{dbh}->errstr(); if (! $sth->fetchrow_hashref) { my $sql = "insert into $field (item,ident,count) values (?,?,?)"; my $sth = $self->{dbh}->prepare($sql) || die $self->{dbh}->errstr(); - $sth->execute($index_data,$ident,1) || die "SQL: $sql; ".$self->{dbh}->errstr(); -#print STDERR "in index: $index_data\n"; + $sth->execute($index_data,$ident,1) || die "sql: $sql; ".$self->{dbh}->errstr(); +#print stderr "in index: $index_data\n"; } else { my $sql = "update $field set count = count + 1 where item = ? and ident = ?"; my $sth = $self->{dbh}->prepare($sql) || die $self->{dbh}->errstr(); - $sth->execute($index_data,$ident) || die "SQL: $sql; ".$self->{dbh}->errstr(); + $sth->execute($index_data,$ident) || die "sql: $sql; ".$self->{dbh}->errstr(); } } +sub check { + my $self = shift; + + my $field = shift; + + my $sql = "select count(*) from $field"; + return $self->{dbh}->do($sql); +} + + +sub fetch { + my $self = shift; + + my $field = shift; + my $what = shift || 'item'; # 'item,ident' + my $where = shift; + + my @sql_args; + + my $sql = "select $what from $field"; + if ($where) { + $sql .= " where upper(item) like upper(?)||'%'"; + push @sql_args,$where; + } + $sql .= " order by item"; + + my $sth = $self->{dbh}->prepare($sql) || die "sql: $sql; ".$self->{dbh}->errstr(); + $sth->execute(@sql_args) || die "sql: $sql; ".$self->{dbh}->errstr(); + my @arr; + while (my $row = $sth->fetchrow_hashref) { + push @arr,$row; + } + return @arr; +} + sub close { my $self = shift; @@ -97,12 +132,12 @@ END { $Count--; - print STDERR "index_DBI fatal error: \$index->close() not called... $Count references left!\n" if ($Count != 0); + print STDERR "index_DBI fatal error: \$index->close() not called... $Count references left!\n" if ($Count > 0); # FIX: debug output - print STDERR "usage\ttable\n"; - foreach (keys %Table) { - print STDERR $Table{$_},"\t$_\n"; - } +# print STDERR "usage\ttable\n"; +# foreach (keys %Table) { +# print STDERR $Table{$_},"\t$_\n"; +# } } 1;