using dbi with mod_ruby

jm <[email protected]> Mon, 16 Aug 2004 12:32:08 +1000
Newsgroups gmane.comp.apache.mod-ruby
Message-ID <[email protected]>
Are there any tricks using dbi with mod_ruby? This rather simple 
example raises an exception on sth.prepare() and the slightly more 
complicated real code segfaults(11) mod_ruby. Further this code works 
in irb even with $SAFE=1. Note, database details are fake of course.

require "singleton"
require "time"
require "dbi"

module Apache
  class UriTest
   include Singleton
   @@dsn = "dbi:Mysql:adatabase:hostip"
   @@user = "auser"
   @@pass = "apassword"
   @@dbh = nil
   @@sql_query = "SELECT ?"

   def translate_uri(r)
    exit(Apache::OK) if r.header_only?
    # do this so logging is available in other methods
    @r = r

    if r.initial? then
       do_sql(r.headers_in['Host'])
       @r.server.log_error("#{__FILE__} #{__LINE__}")
    end
    return Apache::DECLINED
   end

   def do_sql(host)
     @@dbh = DBI.connect(@@dsn,@@user,@@pass)
     sth = @@dbh.prepare(@sql_query)
     @r.server.log_error("#{__FILE__} #{__LINE__}")
     sth.execute(host)
     @r.server.log_error("#{__FILE__} #{__LINE__}")
     sth.finish()
     @@dbh.disconnect()
   end
  end
end