Class: Databender::Connection

Inherits:
Object
  • Object
show all
Defined in:
lib/databender/connection.rb

Instance Method Summary collapse

Constructor Details

#initialize(connection_params) ⇒ Connection

Returns a new instance of Connection.



7
8
9
# File 'lib/databender/connection.rb', line 7

def initialize(connection_params)
  @conn = ActiveRecord::Base.establish_connection(connection_params).connection
end

Instance Method Details

#columns_for(db_name, table_name) ⇒ Object



27
28
29
30
31
32
33
34
# File 'lib/databender/connection.rb', line 27

def columns_for(db_name, table_name)
  execute(%[
              SELECT column_name
              FROM information_schema.columns
              WHERE table_schema = '#{db_name}'
                    and table_name = '#{table_name}';
           ])
end

#execute(sql) ⇒ Object



11
12
13
# File 'lib/databender/connection.rb', line 11

def execute(sql)
  @conn.execute(sql).entries.flatten.compact.map(&:to_sym)
end

#execute_count(sql) ⇒ Object



15
16
17
# File 'lib/databender/connection.rb', line 15

def execute_count(sql)
  @conn.execute(sql)
end

#foreign_key_dependency_map_for(db_name) ⇒ Object



36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/databender/connection.rb', line 36

def foreign_key_dependency_map_for(db_name)
  rows = @conn.execute(%[
                          SELECT table_name, column_name, referenced_table_name, referenced_column_name
                          FROM information_schema.key_column_usage
                          WHERE table_schema = '#{db_name}' AND referenced_table_name is not null
                          AND table_name != referenced_table_name
                          ORDER BY table_name;
                        ])
  rows.each_with_object({}) do |row, map|
    table, column, ref_table_name, ref_column_name = row
    parent = ForeignConstraint.new(table, column, ref_table_name, ref_column_name)
    map.has_key?(table) ? map[table] << parent : map[table] = [parent]
  end.symbolize_keys
end

#tables_for(db_name) ⇒ Object



19
20
21
22
23
24
25
# File 'lib/databender/connection.rb', line 19

def tables_for(db_name)
  execute(%[
              SELECT table_name
              FROM information_schema.tables
              WHERE table_schema = '#{db_name}' and table_type = 'BASE TABLE';
          ])
end