Class: SakaiInfo::DB

Inherits:
Object
  • Object
show all
Defined in:
lib/sakai-info/database.rb

Constant Summary collapse

DEFAULT_CONFIG_FILE =
File.expand_path("~/.sakai-info")
@@default_database_name =
nil
@@config =
nil
@@config_file =
DEFAULT_CONFIG_FILE
@@databases =
{}
@@logger =

set global logger

nil

Class Method Summary collapse

Class Method Details

.config_fileObject



64
65
66
# File 'lib/sakai-info/database.rb', line 64

def self.config_file
  @@config_file
end

.config_file=(newfile) ⇒ Object



60
61
62
# File 'lib/sakai-info/database.rb', line 60

def self.config_file=(newfile)
  @@config_file = newfile
end

.configure(config) ⇒ Object



68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
# 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) ⇒ Object



126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
# 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

.databasesObject



121
122
123
# File 'lib/sakai-info/database.rb', line 121

def self.databases
  @@config
end

.default_database=(database_name) ⇒ Object



146
147
148
149
150
151
# 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) ⇒ Object



113
114
115
116
117
118
119
# 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) ⇒ Object



155
156
157
158
159
160
161
162
163
164
165
# 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