use Unicode::MapUTF8 qw(to_utf8 from_utf8 utf8_supported_charset);
use DBI;
+use lib '..';
+use index_DBI;
+
# configuration options
# FIX: they really should go in configuration file!
my $TEMPLATE_PATH = '/data/webpac/template_html';
my $q = $self->query();
+ my $field = $q->param("f$i");
+ my $limit = $q->param("v$i");
+
+
my $html;
- $html .= "show index of ".$q->param("f$i")." for ".$q->param("v$i");
+ my $index = new index_DBI();
+
+ if (! $index->check($field)) {
+ my $tmpl = $self->load_tmpl('no_index.html');
+ $tmpl->param('field',$field);
+ $html = $tmpl->output;
+ return $html;
+ }
+
+ my @index_arr = $index->fetch($field,'item',$limit);
+ $html .= "show index of <i>$field</i>";
+ $html .= " for <i>$limit</i>" if ($limit);
+
+ while (my $row = shift @index_arr) {
+ $html .= "<br>".$row->{item}."\n";
+ }
return $html;
}
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();
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";
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;
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;