Module: SurveyorWarehouse::DB

Defined in:
lib/surveyor_warehouse/db.rb

Class Method Summary collapse

Class Method Details

.adapterObject



29
30
31
32
33
34
35
36
# File 'lib/surveyor_warehouse/db.rb', line 29

def self.adapter
  @adapter ||=
    if 'postgresql' == configurations['adapter']
      'postgres'
    else
      raise "Unsupported database adapter: #{db['adapter']}"
    end
end

.columns(tablename) ⇒ Object

Columns are returned a hash like below:

{ :column1 => [:primary_key], :column2 => [:not_null] }



42
43
44
45
46
# File 'lib/surveyor_warehouse/db.rb', line 42

def self.columns(tablename)
  connection.schema(tablename.to_sym).inject({}) do |attrs, (cname, cattrs)| 
    attrs.merge(cname => cattrs)
  end
end

.configurationsObject



13
14
15
# File 'lib/surveyor_warehouse/db.rb', line 13

def self.configurations
  @configurations ||= ::ActiveRecord::Base.configurations[Rails.env]
end

.connectionObject



3
4
5
6
7
8
9
10
11
# File 'lib/surveyor_warehouse/db.rb', line 3

def self.connection
  @connection ||= Sequel.connect(
      :adapter=> adapter, 
      :host=>'localhost', 
      :database=> database, 
      :user=> username, 
      :password=> password).extension(:pg_array)
  @connection.extend(SequelExtension::Connection)
end

.databaseObject



25
26
27
# File 'lib/surveyor_warehouse/db.rb', line 25

def self.database
  @database ||= configurations['database']
end

.passwordObject



21
22
23
# File 'lib/surveyor_warehouse/db.rb', line 21

def self.password
  @password ||= configurations['password']
end

.usernameObject



17
18
19
# File 'lib/surveyor_warehouse/db.rb', line 17

def self.username
  @username ||= configurations['username']
end