Class: RuboCop::Isucon::DatabaseConnection

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

Overview

Manage database connection

Instance Method Summary collapse

Constructor Details

#initialize(database_config) ⇒ DatabaseConnection

Returns a new instance of DatabaseConnection.

Parameters:

  • database_config (Hash)

    Same as ActiveRecord::Base.establish_connection argument

See Also:



9
10
11
12
13
14
# File 'lib/rubocop/isucon/database_connection.rb', line 9

def initialize(database_config)
  ActiveRecord::Base.establish_connection(database_config)
  @column_names_by_table = {}
  @indexes_by_table = {}
  @primary_keys_by_table = {}
end

Instance Method Details

#column_names(table_name) ⇒ Array<String>

Parameters:

  • table_name (String)

Returns:

  • (Array<String>)


18
19
20
# File 'lib/rubocop/isucon/database_connection.rb', line 18

def column_names(table_name)
  @column_names_by_table[table_name] ||= ActiveRecord::Base.connection.columns(table_name).map(&:name)
end

#indexes(table_name) ⇒ Array<ActiveRecord::ConnectionAdapters::IndexDefinition>

Parameters:

  • table_name (String)

Returns:

  • (Array<ActiveRecord::ConnectionAdapters::IndexDefinition>)

See Also:



25
26
27
# File 'lib/rubocop/isucon/database_connection.rb', line 25

def indexes(table_name)
  @indexes_by_table[table_name] ||= ActiveRecord::Base.connection.indexes(table_name)
end

#primary_keys(table_name) ⇒ Array<String>

Returns primary key's column names.

Parameters:

  • table_name (String)

Returns:

  • (Array<String>)

    primary key's column names



37
38
39
# File 'lib/rubocop/isucon/database_connection.rb', line 37

def primary_keys(table_name)
  @primary_keys_by_table[table_name] ||= ActiveRecord::Base.connection.primary_keys(table_name)
end

#unique_index_columns(table_name) ⇒ Array<Array<String>>

Returns column names of indexes.

Parameters:

  • table_name (String)

Returns:

  • (Array<Array<String>>)

    column names of indexes



31
32
33
# File 'lib/rubocop/isucon/database_connection.rb', line 31

def unique_index_columns(table_name)
  indexes(table_name).select(&:unique).map(&:columns)
end