Mod_ruby postgres

Hello,

I’m trying some code with mod_ruby where I make a postgresql connection.
The following works fine:

require ‘postgres’

def with_db
db = PGconn.connect(‘localhost’, 5432, ‘’, ‘’, ‘template1’,
‘postgres’, ‘’)
begin
yield db
ensure
db.close
end
end

with_db do |db|
row = db.query(‘SELECT VERSION()’)
puts row
end

But when I alter the code extending the PGconn class to do the result
set ceanup like this:

require ‘postgres’

class PGconn
alias :exec_no_block :exec
def exec(sql)
res = exec_no_block(sql)
return res unless block_given?
begin
yield res
ensure
res.clear if res
end
end
end

def with_db
db = PGconn.connect(‘localhost’, 5432, ‘’, ‘’, ‘template1’,
‘postgres’, ‘’)
begin
yield db
ensure
db.close
end
end

with_db do |db|
db.exec(‘SELECT VERSION()’) do |res|
puts res[0]
end
end

The latter code works from command line, making the same output as the
first one, however, with mode_ruby I get the following error in apache
error.log:

[Sat Nov 04 13:28:08 2006] [error] mod_ruby: error in ruby
[Sat Nov 04 13:28:08 2006] [error] mod_ruby: /var/www/ruby/e1b.rb:20:in
with_db': undefined methodconnect’ for #Module:0xb6fb45b4::PGconn
(NoMethodError)

Could someone please help me out with this? It might be an obvious fault
in my code, but I cannot figure it out.

Thanks:
Szab

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs