\n"; + +#display the search form +if($field{'do'} eq "home" || $field{'do'} eq ""){ + +#isbn,title,authors,year,edition,publisher + + print <
+
Computer Science Library Collection Search
++
+ +
Search Results
"; + + #build query string here, query and display. + my $dbh = DBI->connect("dbi:Pg:dbname=library;host=localhost", + 'librarian', + 'KJI3RVgszrxrxe6W4gX3', + {AutoCommit => 1}) + or print "Couldn't connect to database: " . DBI->errstr; + + $searchterm = undef; + $orterm = undef; + @results; + @oresults; + $title = $field{'title'}; + $title =~ s/[';:"]/ /g; + $publisher = $field{'publisher'}; + $publisher =~ s/[';:"]/ /g; + $edition = $field{'edition'}; + $edition =~ s/[';:"]/ /g; + $year = $field{'year'}; + $year =~ s/[';:"]/ /g; + $isbn = $field{'isbn'}; + $isbn =~ s/[';:"]/ /g; + + if($title){ + + @tmp = split(/\+/,$title); + my @keywords; + my @orwords; + push @keywords,"'%$tmp[0]%'"; + push @orwords,"'%tmp[0]%'"; + shift @tmp; + + foreach $keyword (@tmp){ + push @keywords,"AND (title ILIKE '%$keyword%')"; + push @orwords,"OR (title ILIKE '%$keyword%')"; + } + + $searchterm = "(title ILIKE @keywords)"; + $orterm = "(title ILIKE @orwords)"; + + } + + if($publisher){ + + @tmp = split(/\+/,$publisher); + my @keywords; + my @orwords; + push @keywords,"'%$tmp[0]%'"; + shift @tmp; + + foreach $keyword (@tmp){ + push @keywords,"AND (publisher ILIKE '%$keyword%')"; + push @orwords,"OR (publisher ILIKE '%$keyword%')"; + } + + if($searchterm eq undef){ + $searchterm = "(publisher ILIKE @keywords)"; + $orterm = "(publisher ILIKE @orwords)"; + }else{ + $temp = $searchterm; + $searchterm = $temp." AND (publisher ILIKE @keywords)"; + $temp = $orterm; + $orterm = $temp." OR (publisher ILIKE @keywords)"; + } + + } + + if($edition){ + + @tmp = split(/\+/,$edition); + my @keywords; + my @orwords; + push @keywords,"'%$tmp[0]%'"; + push @orwords,"'%$tmp[0]%'"; + shift @tmp; + + foreach $keyword (@tmp){ + push @keywords,"AND (edition ILIKE '%$keyword%')"; + push @keywords,"OR (edition ILIKE '%$keyword%')"; + } + + if($searchterm eq undef){ + $searchterm = "(edition ILIKE @keywords)"; + $orterm = "(edition ILIKE @keywords)" + }else{ + $temp = $searchterm; + $searchterm = $temp." AND (edition ILIKE @keywords)"; + $temp = $orterm; + $orterm = $temp." OR (edition ILIKE @keywords)"; + } + + } + + if($year){ + + @tmp = split(/\+/,$year); + my @keywords; + my @orwords; + push @keywords,"'%$tmp[0]%'"; + push @orwords,"'%$tmp[0]%'"; + shift @tmp; + + foreach $keyword (@tmp){ + push @keywords,"AND (year ILIKE '%$keyword%')"; + push @orwords,"OR (year ILIKE '%$keyword%')"; + } + + if($searchterm eq undef){ + $searchterm = "(year ILIKE @keywords)"; + $orterm = "(year ILIKE $orwords)"; + }else{ + $temp = $searchterm; + $searchterm = $temp." AND (year ILIKE @keywords)"; + $temp = $orterm; + $orterm = $temp." OR (year ILIKE @orwords)"; + + } + + } + + if($isbn){ + + @tmp = split(/\+/,$isbn); + my @keywords; + my @orwords; + push @keywords,"'%$tmp[0]%'"; + push @orwords,"'%$tmp[0]%'"; + shift @tmp; + + foreach $keyword (@tmp){ + push @keywords,"AND (isbn ILIKE '%$keyword%')"; + push @orwords,"OR (isbn ILIKE '%keyword%')"; + } + + if($searchterm eq undef){ + $searchterm = "(isbn ILIKE @keywords)"; + $orterm = "(isbn ILIKE @keywords)"; + }else{ + $temp = $searchterm; + $searchterm = $temp." AND (isbn ILIKE @keywords)"; + $temp = $orterm; + $orterm = $temp." OR (year ILIKE @orwords)"; + } + + } + + if($searchterm){ + + my $sth = $dbh->prepare("SELECT title,isbn,year,publisher,edition FROM book WHERE $searchterm;") + or print "Couldn't prepare statement: " . $dbh->errstr; + + my $oth = $dbh->prepare("SELECT title,isbn,year,publisher,edition FROM book WHERE $orterm;") + or print "Couldn't prepare statement: " . $dbh->errstr; + + $sth->execute() # Execute the query + or print "Couldn't execute statement: " . $sth->errstr; + + while($data = $sth->fetchrow_arrayref()){ + push @results, [@{$data}]; + } + + $oth->execute() + or print "Couldn't execute statement: " . $oth->errstr; + + while($ordata = $oth->fetchrow_arrayref()){ + push @oresults, [@{$ordata}]; + } + + $sth->finish; + $oth->finish; + + + if ($sth->rows == 0 && $oth->rows == 0) { + print "Title | ISBN | Year | Publisher | Edition |
$field | \n"; + } + print "||||
$field | \n"; + } + print "
+ +