Class: Kibutsu::DatabaseConnection

Inherits:
Object
  • Object
show all
Defined in:
lib/kibutsu/database_connection.rb

Overview

Only class that directly interacts with Sequel. Used for retrieving data from the database and inserting fixtures into it.

Instance Method Summary collapse

Constructor Details

#initialize(connection_string) ⇒ DatabaseConnection

Returns a new instance of DatabaseConnection.



9
10
11
# File 'lib/kibutsu/database_connection.rb', line 9

def initialize(connection_string)
  @connection = Sequel.connect(connection_string)
end

Instance Method Details

#column_names(table_name) ⇒ Object



20
21
22
# File 'lib/kibutsu/database_connection.rb', line 20

def column_names(table_name)
  connection.schema(table_name.to_s).map(&:first)
end

#disconnectObject



38
39
40
# File 'lib/kibutsu/database_connection.rb', line 38

def disconnect
  connection.disconnect
end

#foreign_key_columns(table_name) ⇒ Object



24
25
26
27
28
29
30
31
32
# File 'lib/kibutsu/database_connection.rb', line 24

def foreign_key_columns(table_name)
  connection.foreign_key_list(table_name.to_s).map do |foreign_key_info|
    ForeignKeyColumn.new(
      FixtureWorld.instance.find_table(table_name.to_s),
      foreign_key_info[:columns].first.to_s,
      FixtureWorld.instance.find_table(foreign_key_info[:table].to_s)
    )
  end
end

#insert_fixture_tables(fixture_tables) ⇒ Object



13
14
15
16
17
18
# File 'lib/kibutsu/database_connection.rb', line 13

def insert_fixture_tables(fixture_tables)
  fixture_tables.each do |fixture_table|
    insert_table(fixture_table)
    insert_fixture_tables(fixture_table.foreign_key_source_tables)
  end
end

#table_namesObject



34
35
36
# File 'lib/kibutsu/database_connection.rb', line 34

def table_names
  connection.tables
end