From 02d7b71479372f95b0d00d146b55ae7481241ae5 Mon Sep 17 00:00:00 2001 From: Dobrica Pavlinusic Date: Wed, 22 Jan 2003 20:24:32 +0000 Subject: [PATCH] display index (without pager for now) git-svn-id: file:///home/dpavlin/private/svn/webpac/trunk@11 13eb9ef6-21d5-0310-b721-a9d68796d827 --- WebPac.pm | 24 +++++++++++++++- index_DBI.pm | 57 ++++++++++++++++++++++++++++++------- template_html/no_index.html | 2 ++ 3 files changed, 71 insertions(+), 12 deletions(-) create mode 100644 template_html/no_index.html diff --git a/WebPac.pm b/WebPac.pm index 23ab460..946fab1 100644 --- a/WebPac.pm +++ b/WebPac.pm @@ -9,6 +9,9 @@ use SWISH; 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'; @@ -165,10 +168,29 @@ sub show_index { 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 $field"; + $html .= " for $limit" if ($limit); + + while (my $row = shift @index_arr) { + $html .= "
".$row->{item}."\n"; + } return $html; } diff --git a/index_DBI.pm b/index_DBI.pm index d24f130..9b09394 100644 --- a/index_DBI.pm +++ b/index_DBI.pm @@ -16,7 +16,7 @@ sub new { 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 @@ sub insert { 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 @@ sub insert { 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 @@ sub close { 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; diff --git a/template_html/no_index.html b/template_html/no_index.html new file mode 100644 index 0000000..59bf27f --- /dev/null +++ b/template_html/no_index.html @@ -0,0 +1,2 @@ +There is currently no index for field in this WebPac. +

Please contact administrative person and ask him/her to fix it. -- 2.20.1