OSDN Git Service

1.7.01.7.0
authorEdmund Mergl <E.Mergl@bawue.de>
Fri, 20 Feb 1998 21:26:08 +0000 (21:26 +0000)
committerEdmund Mergl <E.Mergl@bawue.de>
Fri, 20 Feb 1998 21:26:08 +0000 (21:26 +0000)
src/interfaces/perl5/eg/ApachePg.pl
src/interfaces/perl5/eg/example.newstyle
src/interfaces/perl5/eg/example.oldstyle

index a43476e..7885369 100644 (file)
@@ -1,44 +1,48 @@
 #!/usr/local/bin/perl
 
 # demo script, tested with:
-#  - PostgreSQL-6.2
-#  - apache_1.2.4
-#  - mod_perl-1.00
-#  - perl5.004_03
+#  - PostgreSQL-6.3
+#  - apache_1.3
+#  - mod_perl-1.08
+#  - perl5.004_04
 
 use CGI;
 use Pg;
+use strict;
 
-$query = new CGI;
+my $query = new CGI;
 
 print  $query->header,
        $query->start_html(-title=>'A Simple Example'),
        $query->startform,
        "<CENTER><H3>Testing Module Pg</H3></CENTER>",
-       "Enter database name: ",
-       $query->textfield(-name=>'dbname'),
-       "<P>",
-       "Enter select command: ",
-       $query->textfield(-name=>'cmd', -size=>40),
-       "<P>",
-       $query->submit(-value=>'Submit'),
+       "<P><CENTER><TABLE CELLPADDING=4 CELLSPACING=2 BORDER=1>",
+       "<TR><TD>Enter conninfo string: </TD>",
+           "<TD>", $query->textfield(-name=>'conninfo', -size=>40, -default=>'dbname=template1 host=localhost'), "</TD>",
+       "</TR>",
+       "<TR><TD>Enter select command: </TD>",
+           "<TD>", $query->textfield(-name=>'cmd', -size=>40), "</TD>",
+       "</TR>",
+       "</TABLE></CENTER><P>",
+       "<CENTER>", $query->submit(-value=>'Submit'), "</CENTER>",
        $query->endform;
 
 if ($query->param) {
 
-    $dbname = $query->param('dbname');
-    $conn = Pg::connectdb("dbname = $dbname");
-    $cmd = $query->param('cmd');
-    $result = $conn->exec($cmd);
-    print "<TABLE>";
-    for ($i = 0; $i < $result->ntuples; $i++) {
-        print "<TR>";
-        for ($j = 0; $j < $result->nfields; $j++) {
-            print "<TD>", $result->getvalue($i, $j), "</TD>";
+    my $conninfo = $query->param('conninfo');
+    my $conn = Pg::connectdb($conninfo);
+    if ($conn->status == PGRES_CONNECTION_OK) {
+        my $cmd = $query->param('cmd');
+        my $result = $conn->exec($cmd);
+        print "<P><CENTER><TABLE CELLPADDING=4 CELLSPACING=2 BORDER=1>\n";
+        my @row;
+        while (@row = $result->fetchrow) {
+            print "<TR><TD>", join("</TD><TD>", @row), "</TD></TR>\n";
         }
-        print "</TR>";
+        print "</TABLE></CENTER><P>\n";
+    } else {
+        print "<CENTER><H2>Connect to database failed</H2></CENTER>\n";
     }
-    print "</TABLE>";
 }
 
 print $query->end_html;
index 04d2964..8b80e94 100644 (file)
@@ -2,7 +2,7 @@
 
 #-------------------------------------------------------
 #
-# $Id: example.newstyle,v 1.2 1997/09/25 21:15:02 mergl Exp $
+# $Id: example.newstyle,v 1.3 1998/02/20 21:26:06 mergl Exp $
 #
 # Copyright (c) 1997  Edmund Mergl
 #
@@ -13,7 +13,7 @@
 
 ######################### We start with some black magic to print on failure.
 
-BEGIN { $| = 1; print "1..61\n"; }
+BEGIN { $| = 1; print "1..57\n"; }
 END {print "not ok 1\n" unless $loaded;}
 use Pg;
 $loaded = 1;
@@ -23,6 +23,7 @@ print "ok 1\n";
 
 $dbmain = 'template1';
 $dbname = 'pgperltest';
+$dbhost = 'localhost';
 $trace  = '/tmp/pgtrace.out';
 $cnt    = 2;
 $DEBUG  = 0; # set this to 1 for traces
@@ -88,7 +89,7 @@ $SIG{PIPE} = sub { print "broken pipe\n" };
 ######################### create and connect to test database
 # 2-4
 
-$conn = Pg::connectdb("dbname=$dbmain");
+$conn = Pg::connectdb("dbname=$dbmain host=$dbhost");
 cmp_eq(PGRES_CONNECTION_OK, $conn->status);
 
 # might fail if $dbname doesn't exist => don't check resultStatus
@@ -97,7 +98,7 @@ $result = $conn->exec("DROP DATABASE $dbname");
 $result = $conn->exec("CREATE DATABASE $dbname");
 cmp_eq(PGRES_COMMAND_OK, $result->resultStatus);
 
-$conn = Pg::connectdb("dbname=$dbname");
+$conn = Pg::connectdb("dbname=$dbname host=$dbhost");
 cmp_eq(PGRES_CONNECTION_OK, $conn->status);
 
 ######################### debug, PQtrace
@@ -200,17 +201,14 @@ for ($k = 0; $k < $result->nfields; $k++) {
     cmp_eq($k, $fnumber);
 }
 
-for ($k = 0; $k < $result->ntuples; $k++) {
-    $string = "";
-    for ($l = 0; $l < $result->nfields; $l++) {
-        $string .= $result->getvalue($k, $l) . " ";
-    }
-    $i = $k + 1;
-    cmp_eq("$i Edmund Mergl ", $string);
+$string = "";
+while (@row = $result->fetchrow) {
+    $string = join(" ", @row);
 }
+cmp_eq("5 Edmund Mergl", $string);
 
 ######################### PQnotifies
-# 49-51
+# 44-47
 
 if (! defined($pid = fork)) {
     die "can not fork: $!";
@@ -218,7 +216,7 @@ if (! defined($pid = fork)) {
     # i'm the child
     sleep 2;
     bless $conn;
-    $conn = Pg::connectdb("dbname=$dbname");
+    $conn = Pg::connectdb("dbname=$dbname host=$dbhost");
     $result = $conn->exec("NOTIFY person");
     exit;
 }
@@ -236,7 +234,7 @@ while (1) {
 cmp_eq("person", $table);
 
 ######################### PQprint
-# 52-53
+# 48-49
 
 $result = $conn->exec("SELECT name FROM person WHERE id = 2");
 cmp_eq(PGRES_TUPLES_OK, $result->resultStatus);
@@ -246,7 +244,7 @@ $result->print(PRINT, 0, 0, 0, 0, 1, 0, " ", "", "", "myName");
 close(PRINT) || die "bad PRINT: $!";
 
 ######################### PQlo_import, PQlo_export, PQlo_unlink
-# 54-59
+# 50-55
 
 $filename = 'ApachePg.pl';
 $cwd = `pwd`;
@@ -276,9 +274,9 @@ if ($DEBUG) {
 }
 
 ######################### disconnect and drop test database
-# 60-61
+# 56-57
 
-$conn = Pg::connectdb("dbname=$dbmain");
+$conn = Pg::connectdb("dbname=$dbmain host=$dbhost");
 cmp_eq(PGRES_CONNECTION_OK, $conn->status);
 
 $result = $conn->exec("DROP DATABASE $dbname");
index 7f0f46b..6c537af 100644 (file)
@@ -2,7 +2,7 @@
 
 #-------------------------------------------------------
 #
-# $Id: example.oldstyle,v 1.2 1997/09/25 21:15:04 mergl Exp $
+# $Id: example.oldstyle,v 1.3 1998/02/20 21:26:08 mergl Exp $
 #
 # Copyright (c) 1997  Edmund Mergl
 #
@@ -23,6 +23,7 @@ print "ok 1\n";
 
 $dbmain = 'template1';
 $dbname = 'pgperltest';
+$dbhost = 'localhost';
 $trace  = '/tmp/pgtrace.out';
 $cnt    = 2;
 $DEBUG  = 0; # set this to 1 for traces
@@ -88,7 +89,7 @@ $SIG{PIPE} = sub { print "broken pipe\n" };
 ######################### create and connect to test database
 # 2-4
 
-$conn = PQsetdb('', '', '', '', $dbmain);
+$conn = PQsetdb($dbhost, '', '', '', $dbmain);
 cmp_eq(PGRES_CONNECTION_OK, PQstatus($conn));
 
 # might fail if $dbname doesn't exist => don't check resultStatus
@@ -101,7 +102,7 @@ PQclear($result);
 
 PQfinish($conn);
 
-$conn = PQsetdb('', '', '', '', $dbname);
+$conn = PQsetdb($dbhost, '', '', '', $dbname);
 cmp_eq(PGRES_CONNECTION_OK, PQstatus($conn));
 
 ######################### debug, PQtrace
@@ -230,7 +231,7 @@ if (! defined($pid = fork)) {
 } elsif (! $pid) {
     # i'm the child
     sleep 2;
-    $conn = PQsetdb('', '', '', '', $dbname);
+    $conn = PQsetdb($dbhost, '', '', '', $dbname);
     $result = PQexec($conn, "NOTIFY person");
     PQclear($result);
     PQfinish($conn);
@@ -299,7 +300,7 @@ if ($DEBUG) {
 
 PQfinish($conn);
 
-$conn = PQsetdb('', '', '', '', $dbmain);
+$conn = PQsetdb($dbhost, '', '', '', $dbmain);
 cmp_eq(PGRES_CONNECTION_OK, PQstatus($conn));
 
 $result = PQexec($conn, "DROP DATABASE $dbname");