#!/usr/bin/perl 
# 
# demo script, Pg.pm modul, pouzita nova metoda.
#  
my $dbmain = 'farnici';
my $dbname = 'farnici';
my ($conn, $result, $i);
my $pguser = 'jikou' ;
sub extrakt {
($prijmeni_, $jmeno_ ) = @pole ;
@prijmeni = split ( /=/, $prijmeni_) ;
($x, $prijmeni) = @prijmeni ;
	};
sub extrakt1 {
$delka = @pole ;
$i = 0 ;
	while ( $i <= $delka ) {
	$pole[$i] =~ s/^.*[=]// ;
	$i++ ;

			}
( $prijmeni, $jmeno, $rodnecislo, $datnar, $ulice, $cislo, $akce  ) = @pole ;
			};
print "Content-Type: text/html\n\n";
print '<html><body>' ;
print "Dekuji Vam za Vas zajem o program, nasleduje diagnosticka informace\n" ; 
print "Predany dotaz ma tvar\n" ;
$query = $ENV{'QUERY_STRING'} ;
print "$query" ;
print "<br>" ;
@pole = split ( /&/, $query) ;
print "@pole" ;
extrakt1 ;

print 'Hledane prijmeni je ' ;
print $prijmeni $jmeno;
use CGI;
use Pg;
sub printnestrukt
{
$string = "";
my @row;
while (@row = $result->fetchrow) {
    $string = join(" ", @row);
    print  "$string"; 
    print "<br>" ;
   }
} ;
sub printstrukt
{
#Jinak
$fout = soubor ;
$header = 1 ;
$align = 0 ;
$standard = 1;
$html3 = 1 ;  
$expanded = 0 ;
$pager = 0 ;
$fieldSep = "|" ;
$tableOpt = 0 ;
$caption = "*****" ;
$result->print( $fout, $header, $align, $standard, $html3, $expanded, $pager, $fieldSep, $tableOpt,$caption );     
};
my $DEBUG  = 1; # set this to 1 for traces ;
      $conn = Pg::connectdb("dbname=$dbmain"  );  
$status = $conn->status ;  
print '<br>';
print 'stav spojeni je ' ;
print  $status ;
####0 - zdarilo se , 1 - nezdarilo se
open (soubor, ">$soubor") ;  

if ( $akce =~ /1/ ) {    
print 'PRIDAT:' ;
$result = $conn->exec("INSERT INTO farnici VALUES ('$prijmeni', '$jmeno', '$rodnecislo', '$datnar', '$ulice',
'$cislo');")    	} 
	elsif ( $akce =~ /2/ )
	{
#print 'NAJIT' ;
$result = $conn->exec("SELECT * FROM farnici WHERE 
prijmeni LIKE '%$prijmeni%' AND 
jmeno LIKE '%$jmeno%' AND
rodnecislo LIKE '%$rodnecislo%' AND
datnar LIKE '%$datnar%' AND
ulice LIKE '%$ulice%'  AND
cislo LIKE '%$cislo%'
 ORDER BY ulice ;")  
	} 
 ;
print "\n" ;
print "<br>" ;
#Tady si zvolte dva druhy vypisu
#printnestrukt ;
printstrukt ;


print '</html></body>' ;


