class SakaiInfo::DB

Constants

DEFAULT_CONFIG_FILE

Public Class Methods

config_file() click to toggle source
# File lib/sakai-info/database.rb, line 64
def self.config_file
  @@config_file
end
config_file=(newfile) click to toggle source
# File lib/sakai-info/database.rb, line 60
def self.config_file=(newfile)
  @@config_file = newfile
end
configure(config) click to toggle source
# File lib/sakai-info/database.rb, line 68
def self.configure(config)
  begin
    if config.is_a? Hash
      @@config = config
    elsif config.is_a? String and File.exist?(config)
      # try to parse as a filename first
      # first check to see if it points to a different config file
      File.open(config) do |f|
        first_line = f.readlines[0].chomp
        if first_line =~ /^@(.+)$/
          # use the rest of the line as an alternate filename
          config = $1
        end
      end
      if File.exist?(config)
        @@config = YAML::load_file(config)
        # loop through each connection, symbolicize keys in options hashes
        @@config.each do |name,connect_info|
          if connect_info.is_a?(Hash)
            conn = {}
            connect_info.each do |option,value|
              osym=option.to_sym
              # If a query is needed after connection, store it in a procedure
              # See: http://sequel.rubyforge.org/rdoc/files/doc/release_notes/3_11_0_txt.html
              if option=="after_connect"
                conn[osym]=proc{|c| c.exec(value)}
              else
                conn[osym]=value
              end
            end
            @@config[name]=conn
          end
        end
      else
        raise "No such file '#{config}'"
      end
    else
      # otherwise try to parse it generically
      @@config = YAML::load(config)
    end
  rescue Exception => e
    raise InvalidConfigException.new("Unable to parse configuration: #{e}")
  end
end
connect(database_name = :default) click to toggle source
# File lib/sakai-info/database.rb, line 126
def self.connect(database_name = :default)
  if @@config.nil?
    DB.load_config
  end
  if @@databases[database_name].nil?
    @@databases[database_name] =
      Database.new(if database_name == :default
                     if @@default_database_name.nil?
                       @@config[@@config.keys.first]
                     else
                       @@config[@@default_database_name]
                     end
                   else
                     @@config[database_name]
                   end,
                   @@logger)
  end
  @@databases[database_name].connect
end
databases() click to toggle source
# File lib/sakai-info/database.rb, line 121
def self.databases
  @@config
end
default_database=(database_name) click to toggle source
# File lib/sakai-info/database.rb, line 146
def self.default_database=(database_name)
  @@default_database_name = database_name
  # spin it up so that we throw a missing config exception if it's invalid
  DB.connect(@@default_database_name)
  @@default_database_name
end
load_config(config_file = DB.config_file) click to toggle source
# File lib/sakai-info/database.rb, line 113
def self.load_config(config_file = DB.config_file)
  if File.readable? config_file
    DB.configure(config_file)
  else
    raise MissingConfigException.new("No config file found at #{config_file}")
  end
end
logger=(logger) click to toggle source
# File lib/sakai-info/database.rb, line 155
def self.logger=(logger)
  @@logger = logger

  # also force it on any existing database connections
  @@databases.each do |name, dbconn|
    puts "updating #{name}"
    puts dbconn.class
    dbconn.logger = @@logger
    puts dbconn.connect.loggers.inspect
  end
end