require 'singleton' require 'dbi' ## # A singleton wrapper for the DB # # Written by Eric Hodel # # You'll need to write some way of setting the DB_ values in #open, since I'm # too lazy to write them now. class DB include Singleton ## # initialize the database def initialize @db = nil open end ## # pass messages off to the database handle # # Automatically attempts to opens the database if closed def method_missing(methodId, *args, &block) open unless open? @db.send(methodId, *args, &block) end ## # I don't like the way vim highlights db.do def run(query) @db.do query end ## # Open the database handle unless already open def open @db = DBI::connect( "DBI:#{DB_TYPE}:#{DB_SERVER};database=#{DB_DB}", DB_USER, DB_PASSWORD) unless open? return open? end ## # Test for a connection to the database def open? @db && @db.connected? end ## # Close the database handle unless closed def close @db.disconnect if open? end end # class DB