pg.ddx.io  pgsql-interfaces@postgresql.org mailing list archive  
help / color / mirror / Atom feed
Perl and large objects
4+ messages / 3 participants
[nested] [flat]

* Perl and large objects
@ 1999-07-22 18:02  Jeremy Bettis <jeremy@hksys.com>
  0 siblings, 0 replies; 4+ messages in thread

From: Jeremy Bettis @ 1999-07-22 18:02 UTC (permalink / raw)
  To: pgsql-interfaces

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);
}




        $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);
            }
}
--
Jeremy Bettis
jeremy@hksys.com

^ permalink  raw  reply  [nested|flat] 4+ messages in thread

* perl and large objects
@ 2005-05-18 22:36  Sean Davis <sdavis2@mail.nih.gov>
  0 siblings, 1 reply; 4+ messages in thread

From: Sean Davis @ 2005-05-18 22:36 UTC (permalink / raw)
  To: pgsql-interfaces

I am new to large objects and have a simple test script that dies (at 
the lo_import line) and I don't know why (no descriptive error given).  
Any suggestions (or a quick tutorial)?  The DBD manual just wasn't 
quite enough for me, I guess.

Thanks,
Sean


#!/usr/bin/perl
use strict;
use DBI;

my $dbh = DBI->connect('dbi:Pg:dbname=test2',
		       undef,
		       undef,
		       {AutoCommit=>1},) || die $@;

$dbh->{AutoCommit}=0;
my $lobjID = $dbh->func("./out.png",'lo_import') || die $DBI::errstr;
print $lobjID . "\n";

my $sth=$dbh->prepare('insert into lo_store (obj) values (?)');
$sth->execute($lobjID);
$dbh->commit();

$dbh->func($lobjID,"./lo.pl.copy",'lo_export') || die $@;
$dbh->commit();




^ permalink  raw  reply  [nested|flat] 4+ messages in thread

* Re: perl and large objects
@ 2005-05-18 23:07  Daniel Verite <daniel@manitou-mail.org>
  parent: Sean Davis <sdavis2@mail.nih.gov>
  0 siblings, 1 reply; 4+ messages in thread

From: Daniel Verite @ 2005-05-18 23:07 UTC (permalink / raw)
  To: pgsql-interfaces

	Sean Davis wrote:

> I am new to large objects and have a simple test script that dies (at 
> the lo_import line) and I don't know why (no descriptive error given).  
> Any suggestions (or a quick tutorial)?  The DBD manual just wasn't 
> quite enough for me, I guess.

The lack of error message feels like what happens when you call lo_*
functions outside of a transaction. IIRC you just get 0 as the resulting
oid to a lo_import, for instance.

You could try adding $dbh->do("select 1") just after $dbh->{AutoCommit}=0
to ensure that a transaction is started before the lo_import.

Also, your code snippet runs fine for me, unmodified, with DBD::Pg 1.21
Are you running an older version?

-- 
 Daniel
 PostgreSQL-powered mail user agent and storage: http://www.manitou-mail.org



^ permalink  raw  reply  [nested|flat] 4+ messages in thread

* Re: perl and large objects
@ 2005-05-19 10:05  Sean Davis <sdavis2@mail.nih.gov>
  parent: Daniel Verite <daniel@manitou-mail.org>
  0 siblings, 0 replies; 4+ messages in thread

From: Sean Davis @ 2005-05-19 10:05 UTC (permalink / raw)
  To: Daniel Verite <daniel@manitou-mail.org>; +Cc: pgsql-interfaces


On May 18, 2005, at 7:07 PM, Daniel Verite wrote:

> 	Sean Davis wrote:
>
>> I am new to large objects and have a simple test script that dies (at
>> the lo_import line) and I don't know why (no descriptive error given).
>> Any suggestions (or a quick tutorial)?  The DBD manual just wasn't
>> quite enough for me, I guess.
>
> The lack of error message feels like what happens when you call lo_*
> functions outside of a transaction. IIRC you just get 0 as the 
> resulting
> oid to a lo_import, for instance.
>
> You could try adding $dbh->do("select 1") just after 
> $dbh->{AutoCommit}=0
> to ensure that a transaction is started before the lo_import.
>
> Also, your code snippet runs fine for me, unmodified, with DBD::Pg 1.21
> Are you running an older version?
>

DBD::Pg-1.41

It was indeed that the transaction was not started.  Putting in a 
couple of "select 1" worked just fine.

Thanks for the help.

Sean




^ permalink  raw  reply  [nested|flat] 4+ messages in thread


end of thread, other threads:[~2005-05-19 10:05 UTC | newest]

Thread overview: 4+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
1999-07-22 18:02 Perl and large objects Jeremy Bettis <jeremy@hksys.com>
2005-05-18 22:36 perl and large objects Sean Davis <sdavis2@mail.nih.gov>
2005-05-18 23:07 ` Re: perl and large objects Daniel Verite <daniel@manitou-mail.org>
2005-05-19 10:05   ` Re: perl and large objects Sean Davis <sdavis2@mail.nih.gov>

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox