Thread: Perl and large objects
I have a script that reads and writes some large objects;
Write works fine, read doesn't. It gets this error:
Could not open lo ERROR: lo_lseek: invalid large obj descriptor (0)
I have confirmed that the oid that I get in the script is the same as what psql reports.
Here are some fragments of code:
sub doselect{ my $query = $_[0];
my ($result, $status, $i, $j);
my (@data);
if (!$conn) {
$conn = Pg::connectdb("dbname=jeremy");
if ( PGRES_CONNECTION_OK != $conn->status ) {
print "<pre>",$conn->errorMessage, "\n", $query, "\n</pre>\n";
print end_html;
exit(1);
}
}
@data=();
if ($result = $conn->exec($query)) {
if (PGRES_TUPLES_OK == ($status = $result->resultStatus)) {
for $i (0..$result->ntuples-1) {
for $j (0..$result->nfields-1) {
$data[$i]->{$result->fname($j)}=$result->getvalue($i,$j);
}
}
return \@data;
}
}
print "<pre>",$conn->errorMessage, "\n", $query, "\n</pre>\n";
print end_html;
exit(1);
}
my ($result, $status, $i, $j);
my (@data);
if (!$conn) {
$conn = Pg::connectdb("dbname=jeremy");
if ( PGRES_CONNECTION_OK != $conn->status ) {
print "<pre>",$conn->errorMessage, "\n", $query, "\n</pre>\n";
print end_html;
exit(1);
}
}
@data=();
if ($result = $conn->exec($query)) {
if (PGRES_TUPLES_OK == ($status = $result->resultStatus)) {
for $i (0..$result->ntuples-1) {
for $j (0..$result->nfields-1) {
$data[$i]->{$result->fname($j)}=$result->getvalue($i,$j);
}
}
return \@data;
}
}
print "<pre>",$conn->errorMessage, "\n", $query, "\n</pre>\n";
print end_html;
exit(1);
}
$emails = doselect("select long_body,short_body from email_body where email_id = $email_id");
if ($$emails[0]->{long_body} > 0) {
$loid = $$emails[0]->{long_body};
print "Long #", $loid;
$long=$conn->lo_open($loid, PGRES_INV_READ);
if ($long==-1) {
print("Could not open lo ",$conn->errorMessage) ;
} else {
$buf="";
while ($conn->lo_read($long, $buf, 2048) > 0) {
$body.=$buf;
$buf="";
}
$conn->lo_close($long);
}
}
if ($$emails[0]->{long_body} > 0) {
$loid = $$emails[0]->{long_body};
print "Long #", $loid;
$long=$conn->lo_open($loid, PGRES_INV_READ);
if ($long==-1) {
print("Could not open lo ",$conn->errorMessage) ;
} else {
$buf="";
while ($conn->lo_read($long, $buf, 2048) > 0) {
$body.=$buf;
$buf="";
}
$conn->lo_close($long);
}
}