"Fossies" - the Fresh Open Source Software Archive

Member "absence-v2.1/cgi-bin/dbtest.pl" (10 Jun 2012, 1083 Bytes) of package /linux/www/web-absence-2.1.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) Perl source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. For more information about "dbtest.pl" see the Fossies "Dox" file reference documentation.

    1 #!/usr/bin/perl
    2 
    3 use DBI;
    4 use Carp;
    5 
    6 my $dbname = 'absence';
    7 my $dbuser = 'absence';
    8 my $dbpass = 'absence';
    9 
   10 my $DBH = DBI->connect("dbi:Pg:dbname=$dbname", $dbuser, $dbpass, {AutoCommit => 1});
   11 defined($DBH) || die "connection failed";
   12 
   13 my $field = shift;
   14 my $pid = shift;
   15 
   16 #my $sql = qq{INSERT INTO $table ($keys) VALUES ($vals);};
   17 my $sql = qq{SELECT $field FROM v_object WHERE id = $pid;};
   18 print "SQL> $sql\n";
   19 
   20 #$rv = $DBH->do($sql);
   21 #defined($rv) || print "statement failed\n";
   22 
   23 my $res = dbSelect($sql);
   24 if (defined($res)) {
   25     print "result defined.\n";
   26 } else {
   27     print "result undefined.\n";
   28 }
   29 
   30 $DBH->disconnect;
   31 
   32 sub dbSelect
   33 {
   34     my $sql = shift;
   35 
   36     my $sth;
   37     eval {
   38         $sth = $DBH->prepare($sql);
   39     };
   40     if ($@) {
   41         confess $@;
   42     }
   43 
   44     $sth->execute || confess "statement [$sql] failed";
   45  
   46     my @result;      
   47     my $ref;
   48     while(defined($ref = $sth->fetchrow_hashref)) {
   49         push(@result, $ref);
   50     }   
   51  
   52     if (wantarray) {
   53         return @result;
   54     } else {
   55         @result || return undef;
   56         return $result[0];
   57     }
   58 }    
   59