open(TRACE,">$trace")||die"can not open $trace: $!";
$conn->trace(TRACE);
}
######################### check PGconn
# 5-8
$db=$conn->db;
cmp_eq($dbname,$db);
$user=$conn->user;
cmp_ne("",$user);
$host=$conn->host;
cmp_ne("",$host);
$port=$conn->port;
cmp_ne("",$port);
######################### create and insert into table
# 9-20
$result=$conn->exec("CREATE TABLE person (id int4, name char16)");
cmp_eq(PGRES_COMMAND_OK,$result->resultStatus);
cmp_eq("CREATE",$result->cmdStatus);
for($i=1;$i<=5;$i++){
$result=$conn->exec("INSERT INTO person VALUES ($i, 'Edmund Mergl')");
cmp_eq(PGRES_COMMAND_OK,$result->resultStatus);
cmp_ne(0,$result->oidStatus);
}
######################### copy to stdout, PQgetline
# 21-27
$result=$conn->exec("COPY person TO STDOUT");
cmp_eq(PGRES_COPY_OUT,$result->resultStatus);
$i=1;
while(-1!=$ret){
$ret=$conn->getline($string,256);
lastif$stringeq"\\.";
cmp_eq("$i Edmund Mergl",$string);
$i++;
}
cmp_eq(0,$conn->endcopy);
######################### delete and copy from stdin, PQputline
# 28-34
$result=$conn->exec("BEGIN");
cmp_eq(PGRES_COMMAND_OK,$result->resultStatus);
$result=$conn->exec("DELETE FROM person");
cmp_eq(PGRES_COMMAND_OK,$result->resultStatus);
cmp_eq("DELETE 5",$result->cmdStatus);
cmp_eq("5",$result->cmdTuples);
$result=$conn->exec("COPY person FROM STDIN");
cmp_eq(PGRES_COPY_IN,$result->resultStatus);
for($i=1;$i<=5;$i++){
# watch the tabs and do not forget the newlines
$conn->putline("$i Edmund Mergl\n");
}
$conn->putline("\\.\n");
cmp_eq(0,$conn->endcopy);
$result=$conn->exec("END");
cmp_eq(PGRES_COMMAND_OK,$result->resultStatus);
######################### select from person, PQgetvalue
# 35-48
$result=$conn->exec("SELECT * FROM person");
cmp_eq(PGRES_TUPLES_OK,$result->resultStatus);
for($k=0;$k<$result->nfields;$k++){
$fname=$result->fname($k);
$ftype=$result->ftype($k);
$fsize=$result->fsize($k);
if(0==$k){
cmp_eq("id",$fname);
cmp_eq(23,$ftype);
cmp_eq(4,$fsize);
}else{
cmp_eq("name",$fname);
cmp_eq(20,$ftype);
cmp_eq(16,$fsize);
}
$fnumber=$result->fnumber($fname);
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);
}
######################### PQnotifies
# 49-51
if(!defined($pid=fork)){
die"can not fork: $!";
}elsif(!$pid){
# i'm the child
sleep2;
bless$conn;
$conn=Pg::connectdb("dbname = $dbname");
$result=$conn->exec("NOTIFY person");
exit;
}
$result=$conn->exec("LISTEN person");
cmp_eq(PGRES_COMMAND_OK,$result->resultStatus);
cmp_eq("LISTEN",$result->cmdStatus);
while(1){
$result=$conn->exec("");
($table,$pid)=$conn->notifies;
lastif$pid;
}
cmp_eq("person",$table);
######################### PQprint
# 52-53
$result=$conn->exec("SELECT name FROM person WHERE id = 2");
cmp_eq(PGRES_TUPLES_OK,$result->resultStatus);
open(PRINT,"| read IN; read IN; if [ \"\$IN\" = \"myName Edmund Mergl\" ]; then echo \"ok $cnt\"; else echo \"not ok $cnt\"; fi ")||die"can not fork: $|";
open(TRACE,">$trace")||die"can not open $trace: $!";
PQtrace($conn,TRACE);
}
######################### check PGconn
# 5-8
$db=PQdb($conn);
cmp_eq($dbname,$db);
$user=PQuser($conn);
cmp_ne("",$user);
$host=PQhost($conn);
cmp_ne("",$host);
$port=PQport($conn);
cmp_ne("",$port);
######################### create and insert into table
# 9-20
$result=PQexec($conn,"CREATE TABLE person (id int4, name char16)");
cmp_eq(PGRES_COMMAND_OK,PQresultStatus($result));
cmp_eq("CREATE",PQcmdStatus($result));
PQclear($result);
for($i=1;$i<=5;$i++){
$result=PQexec($conn,"INSERT INTO person VALUES ($i, 'Edmund Mergl')");
cmp_eq(PGRES_COMMAND_OK,PQresultStatus($result));
cmp_ne(0,PQoidStatus($result));
PQclear($result);
}
######################### copy to stdout, PQgetline
# 21-27
$result=PQexec($conn,"COPY person TO STDOUT");
cmp_eq(PGRES_COPY_OUT,PQresultStatus($result));
PQclear($result);
$i=1;
while(-1!=$ret){
$ret=PQgetline($conn,$string,256);
lastif$stringeq"\\.";
cmp_eq("$i Edmund Mergl",$string);
$i++;
}
cmp_eq(0,PQendcopy($conn));
######################### delete and copy from stdin, PQputline
# 28-34
$result=PQexec($conn,"BEGIN");
cmp_eq(PGRES_COMMAND_OK,PQresultStatus($result));
PQclear($result);
$result=PQexec($conn,"DELETE FROM person");
cmp_eq(PGRES_COMMAND_OK,PQresultStatus($result));
cmp_eq("DELETE 5",PQcmdStatus($result));
cmp_eq("5",PQcmdTuples($result));
PQclear($result);
$result=PQexec($conn,"COPY person FROM STDIN");
cmp_eq(PGRES_COPY_IN,PQresultStatus($result));
PQclear($result);
for($i=1;$i<=5;$i++){
# watch the tabs and do not forget the newlines
PQputline($conn,"$i Edmund Mergl\n");
}
PQputline($conn,"\\.\n");
cmp_eq(0,PQendcopy($conn));
$result=PQexec($conn,"END");
cmp_eq(PGRES_COMMAND_OK,PQresultStatus($result));
PQclear($result);
######################### select from person, PQgetvalue
# 35-48
$result=PQexec($conn,"SELECT * FROM person");
cmp_eq(PGRES_TUPLES_OK,PQresultStatus($result));
for($k=0;$k<PQnfields($result);$k++){
$fname=PQfname($result,$k);
$ftype=PQftype($result,$k);
$fsize=PQfsize($result,$k);
if(0==$k){
cmp_eq("id",$fname);
cmp_eq(23,$ftype);
cmp_eq(4,$fsize);
}else{
cmp_eq("name",$fname);
cmp_eq(20,$ftype);
cmp_eq(16,$fsize);
}
$fnumber=PQfnumber($result,$fname);
cmp_eq($k,$fnumber);
}
for($k=0;$k<PQntuples($result);$k++){
$string="";
for($l=0;$l<PQnfields($result);$l++){
$string.=PQgetvalue($result,$k,$l)."";
}
$i=$k+1;
cmp_eq("$i Edmund Mergl ",$string);
}
PQclear($result);
######################### PQnotifies
# 49-51
if(!defined($pid=fork)){
die"can not fork: $!";
}elsif(!$pid){
# i'm the child
sleep2;
$conn=PQsetdb('','','','',$dbname);
$result=PQexec($conn,"NOTIFY person");
PQclear($result);
PQfinish($conn);
exit;
}
$result=PQexec($conn,"LISTEN person");
cmp_eq(PGRES_COMMAND_OK,PQresultStatus($result));
cmp_eq("LISTEN",PQcmdStatus($result));
PQclear($result);
while(1){
$result=PQexec($conn,"");
($table,$pid)=PQnotifies($conn);
PQclear($result);
lastif$pid;
}
cmp_eq("person",$table);
######################### PQprint
# 52-53
$result=PQexec($conn,"SELECT name FROM person WHERE id = 2");
cmp_eq(PGRES_TUPLES_OK,PQresultStatus($result));
open(PRINT,"| read IN; read IN; if [ \"\$IN\" = \"myName Edmund Mergl\" ]; then echo \"ok $cnt\"; else echo \"not ok $cnt\"; fi ")||die"can not fork: $|";