[Bug 326005] [NEW] Postgres array returned as String by ruby dbi

Brian Rawley brian.rawley at xtra.co.nz
Fri Feb 6 02:02:46 UTC 2009


Public bug reported:

Binary package hint: libdbd-pg-ruby1.8

Description:	Ubuntu 8.10
Release:	8.10

libdbd-pg-ruby1.8:
  Installed: 0.2.0-2
  Candidate: 0.2.0-2
  Version table:
 *** 0.2.0-2 0
        500 http://nz.archive.ubuntu.com intrepid/universe Packages
        100 /var/lib/dpkg/status

libdbi-ruby1.8:
  Installed: 0.2.0-2
  Candidate: 0.2.0-2
  Version table:
 *** 0.2.0-2 0
        500 http://nz.archive.ubuntu.com intrepid/universe Packages
        100 /var/lib/dpkg/status

postgresql-8.2:
  Installed: 8.2.7-1
  Candidate: 8.2.7-1
  Version table:
 *** 8.2.7-1 0
        100 /var/lib/dpkg/status

/usr/lib/ruby/1.8/DBD/Pg/Pg.rb contains
      VERSION          = "0.3.3"
      USED_DBD_VERSION = "0.2"

What I expected to happen
libdbd-pg-ruby should convert between a Postgresql array datatype and a ruby Array type in both directions.  This was the behaviour in Ubuntu 8.04.
 
What actually happened
In Ubuntu 8.10, conversion from ruby Array to Postgresql array is handled correctly but the result of conversion from Postgresql array is a ruby String; not a ruby Array.


The probable cause of the problem is in Database::load_type_map 
 /usr/lib/ruby/1.8/DBD/Pg/Pg.rb line 493

Changing:
=============================================
             if row["typname"] =~ /^_/ and row["typelem"].to_i > 0 then
                @elem_map[row["typname"].to_i] = row["typelem"].to_i
                :as_str
              else
                :as_str
              end
=============================================
To:
=============================================
             if row["typname"] =~ /^_/ and row["typelem"].to_i > 0 then
                @elem_map[row["oid"].to_i] = row["typelem"].to_i
                :as_str
              else
                :as_str
              end
=============================================
provides the correct behaviour and makes sense given the data that is being processed here.
However, I am new to libdbd-pg and do not know whether this fix is having undesirable side-effects.

Code to reproduce problem and test for correct behaviour
=============================================================
#!/usr/bin/ruby
## Test for correct conversion of Postgres Array datatypes to/from Ruby Arrays
require 'dbi'
require 'test/unit'

DSN = "DBI:pg:dbname="  ### Use default database for test.  This normally has same name as username
TMP_TABLE_NAME = 'tmp_dbi_test'

class TestPgArray < Test::Unit::TestCase
	
	def setup
			@dbh = DBI.connect(DSN) 
	end
	
	def teardown
		@dbh.do("DROP TABLE IF EXISTS #{TMP_TABLE_NAME}")
		@dbh.disconnect
	end
	
	def test_array_of_integers
		assert( @dbh.do("CREATE TABLE #{TMP_TABLE_NAME} ( a integer[] )") )
		
		### Insert an array into database
		assert(@dbh.do("INSERT INTO #{TMP_TABLE_NAME} VALUES (?)", [1,2,3,4] ) )
		
		### Extract array from database and check for correct type
		@dbh.execute("SELECT a FROM #{TMP_TABLE_NAME}")  do |sth|
			sth.each do |row|
				a = row["a"]
				assert_equal(::Array, a.class)   ### This will fail in Ubuntu 8.10
				assert_equal(::Fixnum,a[0].class)
				assert_equal(4,a.length)
			end
		end
	end
end
============================================================
Expected results with existing Pg.rb

  1) Failure:
test_array_of_integers(TestPgArray)
    [./test.rb:30:in `test_array_of_integers'
     /usr/lib/ruby/1.8/dbi.rb:686:in `fetch'
     /usr/lib/ruby/1.8/dbi.rb:709:in `each'
     ./test.rb:28:in `test_array_of_integers'
     /usr/lib/ruby/1.8/dbi.rb:509:in `execute'
     ./test.rb:27:in `test_array_of_integers']:
<Array> expected but was
<String>.

Expected results after fix:
Started
.
Finished in 0.034404 seconds.

1 tests, 5 assertions, 0 failures, 0 errors

** Affects: libdbi-ruby (Ubuntu)
     Importance: Undecided
         Status: New

-- 
Postgres array returned as String by ruby dbi
https://bugs.launchpad.net/bugs/326005
You received this bug notification because you are a member of Ubuntu
Bugs, which is subscribed to Ubuntu.

-- 
ubuntu-bugs mailing list
ubuntu-bugs at lists.ubuntu.com
https://lists.ubuntu.com/mailman/listinfo/ubuntu-bugs




More information about the universe-bugs mailing list