Large object sample script is out of date

Issue #35 resolved
fredericp created an issue

The documentation says: lo_create([mode]): Return the PGlarge instance on success. On failure, it raises PGError exception.

At the moment, the function returns a FIXNUM, not an instance of PGlarge. Looking at the source in ext/pg.c, PGlarge is nowhere to be found. Even the example at sample/losample.rb doesn't work: (losample.rb:10: undefined method `oid' for 2677586:Fixnum (NoMethodError).

At the moment the only way to import/export large objects to/from the database is to use lo_import/lo_export. This generates much IO-load and slows an otherwise simple script that cuts large images into small pieces extremely down.

ruby 1.8.7 (2010-01-10 patchlevel 249) [i686-darwin10]

Yours, Frederic Pollmann

Comments (10)

  1. Michael Granger repo owner

    Please use the 'pg' library instead of the 'postgres' one. The 'postgres' library has numerous known problems, not the least of which is its implementation of large objects.

    If you're looking at 'pg.c' (which is the extension source for 'pg', not 'postgres'), you'll notice that the documentation for both `lo_creat` and `lo_create` document that it returns a Fixnum, which is the Oid of the created object.

    You are correct about `sample/losample.rb`, though. Most the files in that directory are horribly out of date. I'll fix the losample ASAP to reflect pg's interface.

  2. fredericp reporter

    Sorry, wasn't clear enough in my first report. I am using the 'pg' library, and I know that these functions do return a FIXNUM from looking at the source-code. But IMHO that isn't documentation, is. And the information in that file is clearly not up to date.

    Which calling-sequence do I have to use to create and open a large object so that I can write to it? The appended example-script doesn't work.

    farnsworth: ~/ruby$ cat -n pgtest.rb 
         1	require 'rubygems'
         2	require 'pg'
         4	mydata = 'this is my special test string'
         5	db = => 'myhost', :user => 'myuser', :password => 'mypassword', :dbname => 'mydb', :sslmode => 'require')
         7	oid = db.locreat(PGconn::INV_READ | PGconn::INV_WRITE)
         8	lo = db.loopen(oid, PGconn::INV_READ | PGconn::INV_WRITE)
         9	lo.write(mydata)
        10	lo.close
        11	db.close
    farnsworth: ~/ruby$ ruby pgtest.rb 
    pgtest.rb:9: undefined method `write' for 0:Fixnum (NoMethodError)
  3. Michael Granger repo owner

    The 'postgres.html' file is documentation for the 'postgres' library, which is why I assumed you were using it. I will be removing it shortly, as it has been replaced by inline documentation which can be extracted using RDoc. I'll also post a link to the generated docs on my server so you don't need to generate them yourself.

    I've replaced the large-object sample script with one that works for the 'pg' library; I'd be happy to answer questions about it.

    You example code should look something like:

    require 'rubygems'
    require 'pg'
    mydata = 'this is my special test string'
    db = => 'myhost', :user => 'myuser', 
    	:password => 'mypassword', :dbname => 'mydb', 
    	:sslmode => 'require')
    oid = db.locreat(PGconn::INV_READ | PGconn::INV_WRITE)
    lo = db.loopen(oid, PGconn::INV_READ | PGconn::INV_WRITE)
    db.lowrite(lo, mydata)
  4. Log in to comment