pg.ddx.io  pgsql-interfaces@postgresql.org mailing list archive  
help / color / mirror / Atom feed
Writing transactions from pgtksh
2+ messages / 2 participants
[nested] [flat]

* Writing transactions from pgtksh
@ 2002-04-05 23:06 Denis Chavez <dnchavez@gmx.net>
  2002-04-09 08:24 ` Re: Writing transactions from pgtksh Andreas Kretzer <andi@kretzer-berlin.de>
  0 siblings, 1 reply; 2+ messages in thread

From: Denis Chavez @ 2002-04-05 23:06 UTC (permalink / raw)
  To: pgsql-interfaces

Hi!

I'm using pgtksh to develop a Postgres client application. I need to use
transactions, but
the Postgres Programmer's Guide just tells me to use large objects and
doesn't give any
example.

Any idea on how to use this pg_lo* with transactions in pgtksh?

Regards and thank you in advance.
Denis.

-- 
GMX - Die Kommunikationsplattform im Internet.
http://www.gmx.net




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

* Re: Writing transactions from pgtksh
  2002-04-05 23:06 Writing transactions from pgtksh Denis Chavez <dnchavez@gmx.net>
@ 2002-04-09 08:24 ` Andreas Kretzer <andi@kretzer-berlin.de>
  0 siblings, 0 replies; 2+ messages in thread

From: Andreas Kretzer @ 2002-04-09 08:24 UTC (permalink / raw)
  To: pgsql-interfaces

Denis Chavez schrieb:

> Hi!
>
> I'm using pgtksh to develop a Postgres client application. I need to use
> transactions, but
> the Postgres Programmer's Guide just tells me to use large objects and
> doesn't give any
> example.
>
> Any idea on how to use this pg_lo* with transactions in pgtksh?

I'm using the normal Tcl/Tk together with libpgtcl.so (or libpgtcl.dll under windoze).
It should effectively be the same thing.

The documentation states, that you must enclose your LargeObject operation
in a transaction -- and this is definetly true! So let's take a code fragment
to create a large object from a client-local file ($db is my connection handle):

proc create_lobject {db file} {
   # let's open the file
   if {[catch {set fd [open $file r]}]} {
      ... error message 'couldn't open file' ...
      return -1
   }
   fconfigure $fd -translation binary -encoding binary -eofchar {}

   # now create the object
   set oid [pg_lo_create $db INV_READ|INV_WRITE]
   if {[catch {set oid [expr $oid * 1]}]} {   # check for numeric value
      ... some error message ...
      return -1
   }

   # start with a transaction and open the object
   set res [pg_exec $db "BEGIN TRANSACTION;"]
   pg_result $res -clear;    # don't care for result - this should always work

   set lobj_fd [pg_lo_open $db $oid w]
   # one should check $lobj_fd for succesfull open - I don't do this for now :-(

   # now sequentially read file and write to the large object like
   # you would do with a regular file
   while {![eof $fd]} {
      set buf [read $fd 1024]
      set len [string bytelength $buf]
      pg_lo_write $db $lobj_fd $buf $len
   }

   # finish everything
   pg_lo_close $db $lobj_fd
   close $fd

   set res [pg_exec $db "COMMIT TRANSACTION;"]
   pg_result $res -clear

   return $oid
}

Remember to store the returned OID in some descriptive data set. The
large object can only be recovered by it's OID. Reading works exactly
like writing - except that you don't create the object before opening
it.

ATTENTION: This works under linux :-) but may give you some trouble
under Windows :-(
Under Windows you can't really import binary files as they are recoded
every time. Even the 'fconfigure' command can't do anything against
it (I'm still searching for the reason ...). One may be that you even
can't figure out the real length of what you read from the file and
in addition the 'buf' contains weired chars that won't make up the
real binary representation of the file (mainly because some UTF-8
chars or whatever).

There are two other methods to read and write large objects.
pg_lo_import and pg_lo_export that should do all that in one
step. I encountered the same problems with binary files on windows
so I just don't use them anymore.

Hope this helps

Andreas

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


end of thread, other threads:[~2002-04-09 08:24 UTC | newest]

Thread overview: 2+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2002-04-05 23:06 Writing transactions from pgtksh Denis Chavez <dnchavez@gmx.net>
2002-04-09 08:24 ` Andreas Kretzer <andi@kretzer-berlin.de>

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